Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp1140356imm; Tue, 15 May 2018 14:30:44 -0700 (PDT) X-Google-Smtp-Source: AB8JxZq9ETLzhc5L5HLU6qDmp5/qEEVg3lx2OAy4/bRcl2YJDTK4lPfdqZSXqT8nfTk0T4Azsr6t X-Received: by 2002:a62:12d4:: with SMTP id 81-v6mr16884057pfs.243.1526419844601; Tue, 15 May 2018 14:30:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526419844; cv=none; d=google.com; s=arc-20160816; b=EOsKX2j+2niEFEk+RjNv9T3LEy8yypnVvEFo/aP83cMjaL+O5KAqFGNQCmWCVyJQnt P3zzlPdDAjOCgfJfhx+hdKRgaUMWsBNasL5Q+yDUuNZVzPJdosfd4xlxuyhl9v+OB0vq 0pcP7ub7wplx4wwvBm9jMcb7bNi8BSPr/qU0AOLIfVbYbJPzTUW5hvA3I46fxJoEprJ0 9p8tqn7a9Q6p8Mr/D9EjvfzLSUhCQ4Aer0daqi6AH6KZnpo8Vf2yk0t8XfS0IPBBfmzs 8OR7514F8NOyicSvZmaQKQvJyPqeqMyMgzFmcbALYpxQ9E/GN+LVh22gikKu+E3+DnoH /xeA== 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=Cd67JC37WHS+m+E0f8CRh0zUNOw4i9iIVoP6ootMw4g=; b=XgJuSXVQ54wP0hLo0aZhDIRX52G9vR0SHvPDoE8n8ESW6eW2SBGkB3kO8nIxS/Nong J2ID3+wpVrqaNLEzc0BecrH5UKA1cC2FgZcyjfCfeH/F61p9fltqfwxn4z+De+t5BPbH FzQuWzclc2aZdg413omtKRKe4KwXhGW91Z3dJoN5X+yayLfAZxrVLnTyKoCFcGn08rNx 0WZcEVO8LeXQj34WjCdiGTbhwkEXirKpFPIov7Zo5XYYLYV3t2PvPKjTW+Ex8tg4gTuk cNocOXP9kyohDt2z8b7ErHw/YRPveK2xeXVbbymGyxsaWOqivco8rxg7De8kj4OAYo/R Gjeg== 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 e84-v6si907304pfk.198.2018.05.15.14.30.26; Tue, 15 May 2018 14:30: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 S1752740AbeEOV3S (ORCPT + 99 others); Tue, 15 May 2018 17:29:18 -0400 Received: from ex13-edg-ou-002.vmware.com ([208.91.0.190]:54610 "EHLO EX13-EDG-OU-002.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752601AbeEOV0N (ORCPT ); Tue, 15 May 2018 17:26:13 -0400 Received: from sc9-mailhost2.vmware.com (10.113.161.72) by EX13-EDG-OU-002.vmware.com (10.113.208.156) with Microsoft SMTP Server id 15.0.1156.6; Tue, 15 May 2018 14:26:07 -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 0B314B07BD; Tue, 15 May 2018 14:26:11 -0700 (PDT) From: Nadav Amit To: CC: , Nadav Amit , Randy Dunlap , Jonathan Corbet Subject: [RFC 7/8] ilog2: preventing compiler distortion due to big condition Date: Tue, 15 May 2018 07:11:14 -0700 Message-ID: <20180515141124.84254-8-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-002.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 ilog2() which performs a complicated condition 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 to inline functions such as tbl_size(). text data bss dec hex filename 18148888 10064016 2936832 31149736 1db4ea8 ./vmlinux before 18149165 10064176 2936832 31150173 1db505d ./vmlinux after (+437) Static text symbols: Before: 39650 After: 39643 (-7) Cc: Randy Dunlap Cc: Jonathan Corbet Signed-off-by: Nadav Amit --- include/linux/log2.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/include/linux/log2.h b/include/linux/log2.h index 41a1ae010993..aa4dac874339 100644 --- a/include/linux/log2.h +++ b/include/linux/log2.h @@ -83,7 +83,8 @@ unsigned long __rounddown_pow_of_two(unsigned long n) */ #define ilog2(n) \ ( \ - __builtin_constant_p(n) ? ( \ + __builtin_choose_expr( \ + __builtin_constant_p(n), ( \ (n) < 2 ? 0 : \ (n) & (1ULL << 63) ? 63 : \ (n) & (1ULL << 62) ? 62 : \ @@ -147,10 +148,10 @@ unsigned long __rounddown_pow_of_two(unsigned long n) (n) & (1ULL << 4) ? 4 : \ (n) & (1ULL << 3) ? 3 : \ (n) & (1ULL << 2) ? 2 : \ - 1 ) : \ - (sizeof(n) <= 4) ? \ - __ilog2_u32(n) : \ - __ilog2_u64(n) \ + 1), \ + (sizeof(n) <= 4) ? \ + __ilog2_u32(n) : \ + __ilog2_u64(n)) \ ) /** -- 2.17.0