Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp6256907iob; Tue, 10 May 2022 14:08:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzusaxymd1ddwXZ/DgUKpcW9ilthc0hWaUhtj++JtbVjG+k551tKk2OEyW/b0nKKsXSnE/v X-Received: by 2002:a17:90b:f03:b0:1d9:a8e9:9e35 with SMTP id br3-20020a17090b0f0300b001d9a8e99e35mr1802024pjb.48.1652216917437; Tue, 10 May 2022 14:08:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652216917; cv=none; d=google.com; s=arc-20160816; b=mvx+HkSSG5RAmqw2Vva+tfBEt+W+OCIVbtYurvHQ+LvcwwvqCr4FJSnABOZqyVltg0 WXkHlxugLONh5LVaEZFhSf5wNVWvg3/RF1s9hAvaMvE4pdEfiRdWazDvQDtz1QMXf1WF DQhtBVXAkbJgRiykGHrTB/JDMnQm56kwoCaNWaUi7GzoUcDd/B5KUZ4Deq89vtORhejC 8KakKH/sD5vZFxlVSazOd6x+RDbY5/w245EEmEPog67W+8IpmY+17T7agUNzJZ6uhr5v vgfca+77uaYaHJHn2+cC8CosJHxVW9IOI00U0ByhmjYYqe+Vm9RKsI0btEg+1YM4Hc2F b/4g== 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 :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=+wxOYG9ghH/9a5UJTVaKlcRNHbqfracDkNPfmduVwBo=; b=gLQAD23x33O5LcvJReTXUHsInZpUYMd/cAwjC5BHW8gM5kCNb+IAx397it5M2C7+Q4 jP0yLUDQ8aKzfb/RwEPQVgq4c/AcWQlzh7XI9bo8NxD3Na7l5x814UdHUMz3dDOTf1Dk 6lABxqCE3//2BghCNOWro90QfmmaqWfNPr2177sAmSb5VZ2xm868N2HlyQFccSf5z2fS ZsFj5PukOvmexYrFoFHhS/pUW64RE8/vJbLgumH5HHcO65PuAAaOLyJbkTI5gsbilIsN XADs4FmNvnZbMKrlAUFs/WhYz8ESdVO51GJmclcPMzX90nExSXcIDjwY1m+9ukI4+3lG sT1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=k9+Pjtpt; 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 y10-20020a1709027c8a00b00156c1fb5366si99471pll.539.2022.05.10.14.08.21; Tue, 10 May 2022 14:08:37 -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=k9+Pjtpt; 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 S241120AbiEJPEM (ORCPT + 99 others); Tue, 10 May 2022 11:04:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345281AbiEJPDP (ORCPT ); Tue, 10 May 2022 11:03:15 -0400 Received: from mail-pg1-x52f.google.com (mail-pg1-x52f.google.com [IPv6:2607:f8b0:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D01981F8C4E for ; Tue, 10 May 2022 07:26:31 -0700 (PDT) Received: by mail-pg1-x52f.google.com with SMTP id 15so14774213pgf.4 for ; Tue, 10 May 2022 07:26:31 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=+wxOYG9ghH/9a5UJTVaKlcRNHbqfracDkNPfmduVwBo=; b=k9+PjtptzZEnQKChCLzYZbzQWE1mIn4uv/pIHXhTfxtWMVh6eoOGw/gxJ18YqLkHgF OcXq0HcCsygqny/0P5I3xfRlLYnC/4i1d1MdP4jQ0vEvkcxdDC5MpVNhl95F7ySjfy++ fLE85uau3pjLVWJphwgCt99DKyeOVnAFy6GKh9mjGuCTONQWTu12tHjPIXMl7xGxBAhk eXPIDnyz5ieePPSibI/A7Froy8CR3JKh35KviAiQ2gOHcyQICY4x01MUFHy/lUZEMyLi tlcuOx6E23i7gvDFTKtngwvPvWakQp29Rhr3LB+zZxrksse5AKghVr26S1IS5y73jY4U FVzg== 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 :in-reply-to:references:mime-version:content-transfer-encoding; bh=+wxOYG9ghH/9a5UJTVaKlcRNHbqfracDkNPfmduVwBo=; b=VtxkF9ZNaFP9+owfnkAIj97+7ZCYADMZ45fJK9QwiC7qPJdfm4jXiQky7ncDp7Mx3n hrgp3GvkI6mo3DbGTfR+gctVdKYisF/UHckfy3gKmfm6m3t4LenzufEYdI2uizJISw0g 5wyQF1DyA1Edm6HuN42HwBPpbTovTkiSFcmqiayLIriSkP6RnPpIObZtjsrF0ET44IJG DzR9699TQDqLd0atiGRlI6lzmeOj7snUazOBU/rV+m+px79r/wqlBeb8C43+JXHHkk3B 6Gsao/1j/+TBvSgAuop5I2CtsbttEQu70nBKoqtSVAyry16XKP7EzxiWmwhJbQX6FT7a URVQ== X-Gm-Message-State: AOAM532JKGgsy0PGNpuU77do7D/iyKHatdSsN4Cr/u5nkCHMeHmHP/Dr BGlJsjkXOR3ZOgP5OuIQ3ybGIrxFrNA8b23H X-Received: by 2002:a05:6a00:ac1:b0:4f1:29e4:b3a1 with SMTP id c1-20020a056a000ac100b004f129e4b3a1mr20596239pfl.63.1652192790629; Tue, 10 May 2022 07:26:30 -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.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 May 2022 07:26:30 -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 1/2] x86/asm/bitops: ffs: use __builtin_ffs to evaluate constant expressions Date: Tue, 10 May 2022 23:25:49 +0900 Message-Id: <20220510142550.1686866-2-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220510142550.1686866-1-mailhol.vincent@wanadoo.fr> References: <20220510142550.1686866-1-mailhol.vincent@wanadoo.fr> 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 For x86_64, the current ffs() implementation does not produce optimized code when called with a constant expression. On the contrary, the __builtin_ffs() function of both GCC and clang is able to simplify the expression into a single instruction. * Example * Let's consider two dummy functions foo() and bar() as below: | #include | #define CONST 0x01000000 | | unsigned int foo(void) | { | return ffs(CONST); | } | | unsigned int bar(void) | { | return __builtin_ffs(CONST); | } GCC would produce below assembly code: | 0000000000000000 : | 0: b8 ff ff ff ff mov $0xffffffff,%eax | 5: 0f bc c7 bsf %edi,%eax | 8: 83 c0 01 add $0x1,%eax | b: c3 ret | c: 0f 1f 40 00 nopl 0x0(%rax) | | 0000000000000010 : | 10: b8 19 00 00 00 mov $0x19,%eax | 15: c3 ret And clang would produce: | 0000000000000000 : | 0: 55 push %rbp | 1: 48 89 e5 mov %rsp,%rbp | 4: b8 ff ff ff ff mov $0xffffffff,%eax | 9: 0f bc 05 00 00 00 00 bsf 0x0(%rip),%eax # 10 | 10: ff c0 inc %eax | 12: 5d pop %rbp | 13: c3 ret | 14: 66 2e 0f 1f 84 00 00 cs nopw 0x0(%rax,%rax,1) | 1b: 00 00 00 | 1e: 66 90 xchg %ax,%ax | | 0000000000000020 : | 20: 55 push %rbp | 21: 48 89 e5 mov %rsp,%rbp | 24: b8 19 00 00 00 mov $0x19,%eax | 29: 5d pop %rbp | 2a: c3 ret For both examples, we clearly see the benefit of using __builtin_ffs() instead of the kernel's asm implementation for constant expressions. However, for non constant expressions, the ffs() asm version of the kernel remains better for x86_64 because, contrary to GCC, it doesn't emit the CMOV assembly instruction, c.f. [1] (noticeably, clang is able optimize out the CMOV call). This patch uses the __builtin_constant_p() to select between the kernel's ffs() and the __builtin_ffs() depending on whether the argument is constant or not. As a side benefit, this patch also removes below -Wshadow warning: | ./arch/x86/include/asm/bitops.h:283:28: warning: declaration of 'ffs' shadows a built-in function [-Wshadow] | 283 | static __always_inline int ffs(int x) [1] commit ca3d30cc02f7 ("x86_64, asm: Optimise fls(), ffs() and fls64()") http://lkml.kernel.org/r/20111213145654.14362.39868.stgit@warthog.procyon.org.uk Signed-off-by: Vincent Mailhol --- arch/x86/include/asm/bitops.h | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/arch/x86/include/asm/bitops.h b/arch/x86/include/asm/bitops.h index a288ecd230ab..535a7a358c14 100644 --- a/arch/x86/include/asm/bitops.h +++ b/arch/x86/include/asm/bitops.h @@ -269,18 +269,7 @@ static __always_inline unsigned long __fls(unsigned long word) #undef ADDR #ifdef __KERNEL__ -/** - * ffs - find first set bit in word - * @x: the word to search - * - * This is defined the same way as the libc and compiler builtin ffs - * routines, therefore differs in spirit from the other bitops. - * - * ffs(value) returns 0 if value is 0 or the position of the first - * set bit if value is nonzero. The first (least significant) bit - * is at position 1. - */ -static __always_inline int ffs(int x) +static __always_inline int __ffs_asm(int x) { int r; @@ -310,6 +299,22 @@ static __always_inline int ffs(int x) return r + 1; } +/** + * ffs - find first set bit in word + * @x: the word to search + * + * This is defined the same way as the libc and compiler builtin ffs + * routines, therefore differs in spirit from the other bitops. + * + * ffs(value) returns 0 if value is 0 or the position of the first + * set bit if value is nonzero. The first (least significant) bit + * is at position 1. + */ +#define ffs(x) \ + (__builtin_constant_p(x) ? \ + __builtin_ffs(x) : \ + __ffs_asm(x)) + /** * fls - find last set bit in word * @x: the word to search -- 2.35.1