Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp6205821iob; Tue, 10 May 2022 12:49:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxc2PRh1YqIHKKL6I6XJQtLM/790kLQZI57EymrLQoAxzJ54EZEAOfgwDbfUuS/3/HeLmLj X-Received: by 2002:a05:6a00:1805:b0:50d:ee59:b579 with SMTP id y5-20020a056a00180500b0050dee59b579mr21879465pfa.70.1652212158119; Tue, 10 May 2022 12:49:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652212158; cv=none; d=google.com; s=arc-20160816; b=MZ0ncTj2+HHBw+mN+W+MdqzqNi/LtDd4N6bAuC8kyMNc1x+97HqXnprLfcXr6YxN6+ eIlm2+qwEUKcYPYOxRUbHEmj6zM9neRXcQMXZvA6byagK1WhSdKHm9IiegWACOGTdWDA GJg0mb774V/rFCwjlofxFT+Dxh+dDFrZ0cvOwW0x8v3oq3MqBrnSmCUPcXXxJQXG/Z7m mTbOzkPNoHe9gutu9FyBogGZvNOwiv8tEDYt/MANghZkFsFrFZ/IeBywOUZ19r2tWKXj UMmA8j32tz5pIIQEMY03xqCBpd/mYiqKsM8qQ0ZAsDwdsR/aBVtXl4wGpJC+W5daUV/z LbJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:sender:dkim-signature; bh=Q/4OMoOisXN8wgorRRpJSfEX8ZMva/BqopRiI+UKWb0=; b=IZHFNyMAPmPr7C21aFfDo2B/9MmoNhD+d6shehsGOWB2i6pR9/ahJz+Ofkb/scpuuQ PBLAraD3uNGhlBJXOVpUkSv/GJHW194lojOYEkXBA6SjUHl9icYiNCcUq56CjASdKZ/F e5k/NxbD/D2Y3AAKHwbyKwHWfNrSV+0atVisa87WqStwxORtN+DOzi+WLmCJGbyDgYBi ngNLWTuS5HKPDlAUOxrfTgvOG7Y3OGA27ttd0KUF68zZluOtJMM2SNs9XHb7o5iAjRwc BnIy6TcSfnKdcgo0mDAREcXXKLvU6NaKpA6myh/s8qJCiW/0yheDrMlOANYNMwKZHxo+ krIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=BlPulR8F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y28-20020a637d1c000000b003c61f2570dcsi266884pgc.586.2022.05.10.12.49.02; Tue, 10 May 2022 12:49:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=BlPulR8F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345163AbiEJPET (ORCPT + 99 others); Tue, 10 May 2022 11:04:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244733AbiEJPDM (ORCPT ); Tue, 10 May 2022 11:03:12 -0400 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38A871ECBB4 for ; Tue, 10 May 2022 07:26:24 -0700 (PDT) Received: by mail-pg1-x533.google.com with SMTP id 202so14766248pgc.9 for ; Tue, 10 May 2022 07:26:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Q/4OMoOisXN8wgorRRpJSfEX8ZMva/BqopRiI+UKWb0=; b=BlPulR8Fp/JTyA0pfCnpTqyw6Nu0O+9DBLCDHXHeZ8S+CH7g8744UkBQRVCIetJOK/ IIAH24/fVld/Cs4G3GzQEUa57M75T5KOVuq+Vqadszmt3leFQ/K/jtZTvfh3xUWHpy5e 8Neqw/wspJbx8pYPACTS5J/g3ir1tRKMlWncMuVsFKlFFzlX06pE7caJP3TbMJVNMLyI JFX5RrOcI/M710QVHbdjJj7lD8vw9nuF+mj5oKSPzx8C6Q5BNWeP6P3FKw9WMnxHU3iC 0Ym/nc8MHBH/YAg3KywTYNHkvJDIp8V0py7u9AEa/ffKfe8uDpXrShPmF/Oxy6gysOQx eZVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :mime-version:content-transfer-encoding; bh=Q/4OMoOisXN8wgorRRpJSfEX8ZMva/BqopRiI+UKWb0=; b=RJWh8HNYMEpSNvPhaYmqa1A1OAwqjQsi14rl+iTekA66V1inQvecs4lEnYk2a5t5nI wx2bsO4/39J8Ulb1O/P86uta546ho9Yk1u/CUQBeqMhFbvkyHhd24DHgn11qHVGJpMuk aoqEw4MYlRKlPSTu44W/0bSqKRzh0Qpozo3R6cpPNHn0nNwaqisOgFQH6yVOvnXB0cKo UGxljr1VKtSBuyrWHphtf3fiGSQ9nqnP/3zryqXFtDUZ8lJABZM7eWnVq07oBL6avbhW Fz/IxPRbvg9MVjj+tv7xWxKIrBesSyFbNAtaXwLLKTLNgGVx8FLPlMxLpoBK6BYmT7bi G/qQ== X-Gm-Message-State: AOAM530hjKzn0r/DnuoKBancKA+9wa9PNLaq0LRRrqVoCccU9SA4MokM nKPMl+D8FBbpRMRP4Mnq518= X-Received: by 2002:a05:6a00:198a:b0:50e:a2a:32e9 with SMTP id d10-20020a056a00198a00b0050e0a2a32e9mr20402461pfl.82.1652192783580; Tue, 10 May 2022 07:26:23 -0700 (PDT) Received: from localhost.localdomain (124x33x176x97.ap124.ftth.ucom.ne.jp. [124.33.176.97]) by smtp.gmail.com with ESMTPSA id x10-20020a1709028eca00b0015ea8b4b8f3sm2057568plo.263.2022.05.10.07.26.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 May 2022 07:26:23 -0700 (PDT) Sender: Vincent Mailhol From: Vincent Mailhol To: Thomas Gleixner , Ingo Molnar , Borislav Petkov Cc: Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Nathan Chancellor , Nick Desaulniers , Tom Rix , linux-kernel@vger.kernel.org, llvm@lists.linux.dev, Vincent Mailhol Subject: [PATCH 0/2] x86/asm/bitops: optimize ff{s,z} functions for constant expressions Date: Tue, 10 May 2022 23:25:48 +0900 Message-Id: <20220510142550.1686866-1-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,RCVD_IN_SBL_CSS, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: * X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The compilers provides some builtin expression equivalent to the ffs(), __ffs() and ffz() function of the kernel. The kernel uses optimized assembly which produces better code than the builtin functions. However, such assembly code can not be optimized when used on constant expression. This series relies on __builtin_constant_p to select the optimal solution: * use kernel assembly for non constant expressions * use compiler's __builtin function for constant expressions. I also think that the fls() and fls64() can be optimized in a similar way, using __builtin_ctz() and __builtin_ctzll() but it is a bit less trivial so I want to focus on this series first. If it get accepted, I will then work on those two additionnal function. ** Statistics ** On a allyesconfig, before applying this series, I get: | $ objdump -d vmlinux.o | grep bsf | wc -l | 1081 After applying this series: | $ objdump -d vmlinux.o | grep bsf | wc -l | 792 So, roughly 26.7% of the call to either ffs() or __ffs() were using constant expression and can be optimized (I did not produce the figures for ffz()). (tests done on linux v5.18-rc5 x86_64 using GCC 11.2.1) Vincent Mailhol (2): x86/asm/bitops: ffs: use __builtin_ffs to evaluate constant expressions x86/asm/bitops: __ffs,ffz: use __builtin_ctzl to evaluate constant expressions arch/x86/include/asm/bitops.h | 65 +++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 25 deletions(-) -- 2.35.1