Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp1137577imm; Tue, 15 May 2018 14:27:58 -0700 (PDT) X-Google-Smtp-Source: AB8JxZr6Fhhq6dahdkw1lY7J9zEGxOiA3maRS3MUw11bmNe5AYRJ1izFSWJlRLn1v5fnyOvSQ1Og X-Received: by 2002:a17:902:24e:: with SMTP id 72-v6mr15889468plc.87.1526419678267; Tue, 15 May 2018 14:27:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526419678; cv=none; d=google.com; s=arc-20160816; b=Om5eDB2StPxGBnsWeSJkTjpChEjlkk2mVoj5cM795rFH+RS3kPkd+XHZGa58zTd8z8 wVD7lpzYWPlxRQgCMWGMYQVBweLPjHNaPjylYNdU0DWy6KckAPyimitHGd6MHXnJRTQb DumMlQxTq/z+6DVnlab7sx2/zIToM2PPyfx8KwQgFEmwQ+N3aIjpHqkc+XmOEOi+AwoX rLiUzd3ytTMvavqw0WwZQOKlh0KEPedeu5tSy18HLliDDGuZXABTRoXvYvcjXv4926Jr DisJ/uDaa0JGgEvIEQAHThrrLC9yZ68/4SLkrPol0Gic69339+5kekhM7LMPZJxNHK3T NYgg== 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=j2uv35hxJ/jXrCyW6Tk4P9Pgu9Np1F8BKpDTrVn1I2dGrqIQ4Glk3ocJbZhqV3fFF4 83zlMlVt4bNqhxLX+OWJ+48+BR7vKuX8HpLTy0x5cojOt4iBlxULyu/xtkrrhVpPfKJv G82g1Eb8hmoZmd8M8R1RvWU+1WHqIeNtVSRafwQ27C1fT6T7GcSxTJLyscLnL5clEqD+ E07WLvqrN+ZeQKBHZud/4p9XNe3EXp8bWU3I3MnMvjCiBKMRMX9qFnWoiekh5aqmgaY+ isBdgEn9chSER9g+K1FgjfmvCCrxwyXL6uPl8PfQuMhEWPfdBrkERSEpr/yUd7SgF3iO IVzg== 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 e14-v6si713775pgv.516.2018.05.15.14.27.43; Tue, 15 May 2018 14:27:58 -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 S1752943AbeEOV0W (ORCPT + 99 others); Tue, 15 May 2018 17:26:22 -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 S1752881AbeEOV0S (ORCPT ); Tue, 15 May 2018 17:26:18 -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 5C584B078A; 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:23 -0700 Message-ID: <20180515141124.84254-17-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 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