Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp211716rwe; Wed, 31 Aug 2022 01:07:47 -0700 (PDT) X-Google-Smtp-Source: AA6agR7JgFJN+l2iy37E+LBwktmcMkHahGwo56wHDPUCAt+dldFKTNe0lZHbConV/BeBvCs3lm8N X-Received: by 2002:a63:4e12:0:b0:430:2184:eba2 with SMTP id c18-20020a634e12000000b004302184eba2mr2088274pgb.148.1661933267012; Wed, 31 Aug 2022 01:07:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661933267; cv=none; d=google.com; s=arc-20160816; b=A5MueB3rBgqPIyRwdnSmrSIi5E7Ta+bDU/BaAHi7oVakVwduJb4H89cPTCOFiR03mB psWqe16MoZT7sw6WpAWxgl3NyV/ymJsd3mylSRuiZSgk5rKGpvhvfB5B+vLiAUdMRa5Y rkWfVcuBfBYeYBgZAjwRi1tDqWL3BmzDEsO7vg3d9cTBTO+WBLzKLj3umodemvZBleVI TXBXyUwC7x7mNsTNngCmrghocBbEzMGd0UtN55TWRmpX79YbzOvM3en6dhRXKi6unmuB 28QLtwru5/unIa0PuF3g7BQyLQjhtjDaEVazfamYgFtP1a+tPKuU2icBtE+ifhDUPaHs ErVw== 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; bh=o3sfskUkX8dkFep1usnw78GsS4vNKR4sL1lOc+1JEUk=; b=SAWt6tvpIx0RjMSrzg6MleL+BuwOcwdDhHEwQdEa/w3CRP0UY4W3q4H+ZRskqLQmlw 0obtTG82AFdfqYnDkTr4QxMzyMk74hW2ns3FQr414fQoS3YVtQj3D81x/jyhRkcQr4Cz G9zn5xoF7pS04Wota/UFy91BrB3pzEOoKTX4V5lUyU1byDIkI+qHW1wngOeZYHxJKx8S QR6duV04dxXxTP2r1TGytpbT8wYRxQiDu0i7Sj1NFPX3Q53rfyANSzCFOmPbE0+l6opl Vhbc8bd8BeRIoHDnXuJewqXrIHJddJ55/zMg6fSvcABAzdPwGbxvmK6l9eZPuB7TgiwG e5ig== ARC-Authentication-Results: i=1; mx.google.com; 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 w15-20020a63fb4f000000b0042bc2bd5792si4260781pgj.20.2022.08.31.01.07.35; Wed, 31 Aug 2022 01:07:47 -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; 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 S231137AbiHaH6X (ORCPT + 99 others); Wed, 31 Aug 2022 03:58:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229800AbiHaH6T (ORCPT ); Wed, 31 Aug 2022 03:58:19 -0400 Received: from smtp.smtpout.orange.fr (smtp-29.smtpout.orange.fr [80.12.242.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4209D42AC6 for ; Wed, 31 Aug 2022 00:58:16 -0700 (PDT) Received: from YC20090004.ad.ts.tri-ad.global ([103.175.111.222]) by smtp.orange.fr with ESMTPA id TIbgoo5DzXFXxTIbrogpIH; Wed, 31 Aug 2022 09:58:14 +0200 X-ME-Helo: YC20090004.ad.ts.tri-ad.global X-ME-Auth: bWFpbGhvbC52aW5jZW50QHdhbmFkb28uZnI= X-ME-Date: Wed, 31 Aug 2022 09:58:14 +0200 X-ME-IP: 103.175.111.222 From: Vincent Mailhol To: Borislav Petkov Cc: Nick Desaulniers , Thomas Gleixner , Ingo Molnar , x86@kernel.org, Peter Zijlstra , Dave Hansen , "H . Peter Anvin" , Nathan Chancellor , Tom Rix , linux-kernel@vger.kernel.org, llvm@lists.linux.dev, David Howells , Jan Beulich , Christophe Jaillet , Joe Perches , Josh Poimboeuf , Vincent Mailhol Subject: [PATCH v6 0/2] x86/asm/bitops: optimize ff{s,z} functions for constant expressions Date: Wed, 31 Aug 2022 16:57:40 +0900 Message-Id: <20220831075742.295-1-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220511160319.1045812-1-mailhol.vincent@wanadoo.fr> References: <20220511160319.1045812-1-mailhol.vincent@wanadoo.fr> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 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 provide some builtin expression equivalent to the ffs(), __ffs() and ffz() functions of the kernel. The kernel uses optimized assembly which produces better code than the builtin functions. However, such assembly code can not be folded when used with constant expressions. 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. ** Statistics ** Patch 1/2 optimizes 26.7% of ffs() calls and patch 2/2 optimizes 27.9% of __ffs() and ffz() calls (details of the calculation in each patch). ** Changelog ** v5 -> v6: * Rename variable___ffs() into variable__ffs() (two underscores instead of three) v4 -> v5: * (no changes on code, only commit comment was modified) * Rewrite the commit log: - Use two spaces instead of `| ' to indent code snippets. - Do not use `we'. - Do not use `this patch' in the commit description. Instead, use imperative tone. Link: https://lore.kernel.org/all/YvUZVYxbOMcZtR5G@zn.tnic/ v3 -> v4: * (no changes on code, only commit comment was modified) * Remove note and link to Nick's message in patch 1/2, c.f.: Link: https://lore.kernel.org/all/CAKwvOdnnDaiJcV1gr9vV+ya-jWxx7+2KJNTDThyFctVDOgt9zQ@mail.gmail.com/ * Add Reviewed-by: Nick Desaulniers tag in patch 2/2. v2 -> v3: * Redacted out the instructions after ret and before next function in the assembly output. * Added a note and a link to Nick's message on the constant propagation missed-optimization in clang: Link: https://lore.kernel.org/all/CAKwvOdnH_gYv4qRN9pKY7jNTQK95xNeH1w1KZJJmvCkh8xJLBg@mail.gmail.com/ * Fix copy/paste typo in statistics of patch 1/2. Number of occurences before patches are 1081 and not 3607 (percentage reduction of 26.7% remains correct) * Rename the functions as follow: - __varible_ffs() -> variable___ffs() - __variable_ffz() -> variable_ffz() * Add Reviewed-by: Nick Desaulniers tag in patch 1/2. 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 | 64 +++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 26 deletions(-) -- 2.35.1