Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp1137363imm; Tue, 15 May 2018 14:27:44 -0700 (PDT) X-Google-Smtp-Source: AB8JxZr6iQ0uhdtncGaMGjCy6HsS9cfhzZL5gD6DbU1ItObLJ1j0CJxyiVAReLW5ursGECP7JVjQ X-Received: by 2002:a63:3286:: with SMTP id y128-v6mr13877498pgy.419.1526419664715; Tue, 15 May 2018 14:27:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526419664; cv=none; d=google.com; s=arc-20160816; b=MrfPXsTeKAOum00PWtuHJBHCL8NNMb6Q+zmCgkEXTdNANcBDeytGYjaJ1NLgWNTMYI JjF7qB1IGW5rLyBeVjKqyfgXWLjyKFW3zsPa3zpCtCe/C/kXEuDNSqmk1rmQJRLQWU1U q2lHynbUItDiB4ciw3PcjuvGQby7MuJDs+gYfjki3CcrVWd5OWfXoZMOqFhwsYV/TSC7 bdOzv8/0vqGJLozsa5FOyKTsIPyHi2tXBNrXx7l71noX+e61SFs6c70Giie5wVR2OYtB py9Lwe1pDtsRGKYplbGQztNOj5rO/+9uHVHJjjW2gwZww5L+yeoJvyAaBjmZpcTYgc4V nzVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=c1wNdoT26Od9KN/oW5rl5iaUiYR8sScVJH8NXs76qac=; b=mzN9rc9r0vJVYJbLQus6uGNAwIu5rFEO8E0ieklMbNeTyzI9odeDFxXbzs1VblngRP 7WYk2w88CbkA2PkJ57Myt9j3LbIQO4+TkxfeiXos0WsZv2ezNa+HuH623NkfXkbXG9nY x29G34a6r2cogcvcdYWiz8/TWcBD4ot3rhCkxJcUVUwcb2UTM/NFURb11HU2NdNT4roo xpvi9snjmAd6vcJFNZaDFBeIaBnAGsO2Q41kqSiBpTRNNbD7CiS/V58gX4yEebzPMO+o 3ZgGg3Qr4RM0wJiOPO3Gp3ns45Ii973tpv3Cuh/m1rmSOn82cSWe9xgmLP0WVgh9GV32 YV1A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x12-v6si760405pgv.556.2018.05.15.14.27.29; Tue, 15 May 2018 14:27:44 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752911AbeEOV0V (ORCPT + 99 others); Tue, 15 May 2018 17:26:21 -0400 Received: from ex13-edg-ou-001.vmware.com ([208.91.0.189]:48538 "EHLO EX13-EDG-OU-001.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752891AbeEOV0T (ORCPT ); Tue, 15 May 2018 17:26:19 -0400 Received: from sc9-mailhost2.vmware.com (10.113.161.72) by EX13-EDG-OU-001.vmware.com (10.113.208.155) with Microsoft SMTP Server id 15.0.1156.6; Tue, 15 May 2018 14:25:47 -0700 Received: from sc2-haas01-esx0118.eng.vmware.com (sc2-haas01-esx0118.eng.vmware.com [10.172.44.118]) by sc9-mailhost2.vmware.com (Postfix) with ESMTP id 615F9B0792; Tue, 15 May 2018 14:26:11 -0700 (PDT) From: Nadav Amit To: CC: , Nadav Amit , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , Subject: [RFC 8/8] bitops: prevent compiler inline decision distortion Date: Tue, 15 May 2018 07:11:24 -0700 Message-ID: <20180515141124.84254-18-namit@vmware.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180515141124.84254-1-namit@vmware.com> References: <20180515141124.84254-1-namit@vmware.com> MIME-Version: 1.0 Content-Type: text/plain Received-SPF: None (EX13-EDG-OU-001.vmware.com: namit@vmware.com does not designate permitted sender hosts) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There are several places in the kernel in which there is a condition that is based on whether the input is known to be constant in compilation time. If it is, there are complex computations, which only take place during compilation time. Although this scheme works correctly, when GCC computes the expected cost of this code in time and size, it disregards the fact that the computations of the "constant" case will not take place during runtime for the non-constant case. The cost of these functions is considered to be much higher. As a result, inline and branching decisions of the compiler are distorted. Specifically, functions are less likely to be inlined due to their preserved big size and execution time. One of this cases is test_bit() which performs some computations for constant inputs. The solution is to use __builtin_choose_expr() to detect whether the input is constant instead of a C condition. GCC evaluates the builtin earlier, which allows it to improve code-generation decisions. This patch allows function such as bitmap_pos_to_ord() to be inlined. Its overall effect on size: text data bss dec hex filename 18149165 10064176 2936832 31150173 1db505d ./vmlinux before 18149210 10064048 2936832 31150090 1db500a ./vmlinux after (-83) Static text symbols: Before: 39643 After: 39632 (-11) Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: x86@kernel.org Signed-off-by: Nadav Amit --- arch/x86/include/asm/bitops.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/bitops.h b/arch/x86/include/asm/bitops.h index 9f645ba57dbb..f1cb1c9125a9 100644 --- a/arch/x86/include/asm/bitops.h +++ b/arch/x86/include/asm/bitops.h @@ -349,10 +349,10 @@ static __always_inline bool variable_test_bit(long nr, volatile const unsigned l static bool test_bit(int nr, const volatile unsigned long *addr); #endif -#define test_bit(nr, addr) \ - (__builtin_constant_p((nr)) \ - ? constant_test_bit((nr), (addr)) \ - : variable_test_bit((nr), (addr))) +#define test_bit(nr, addr) \ + __builtin_choose_expr(__builtin_constant_p((nr)), \ + constant_test_bit((nr), (addr)), \ + variable_test_bit((nr), (addr))) /** * __ffs - find first set bit in word -- 2.17.0