Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4268148pxj; Wed, 12 May 2021 01:35:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyRMK7oLOjczRWgAdS3vQXW+1AW4AGRYfe6sqo4ug8khH9QB5C0VPQ3Q4j8/rU1D9eiXUu0 X-Received: by 2002:a05:6402:520a:: with SMTP id s10mr21316223edd.245.1620808539953; Wed, 12 May 2021 01:35:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620808539; cv=none; d=google.com; s=arc-20160816; b=jEodHtPdDhxcor0f+fFCB8iebDGBVZygWEiwWfzVp1Tn7TfEQhdpezF0X03OaJ9bsB WEPBU0wrc70T+rLpn6fPXaneAK/gqtKslXf93QmvOXC34gRS/GQOQEhdpgEtbtBHlq+C 1TUUR8bg9X4OfV9xuplrEQFXpxJWJWWMudQJCNCbVU9fZUZt36PB8JkCNRkAfBlvk2bW Ho7K9tb8Cse15kHpmEJMzUYOKcz1wWbIYdtXdaG4PDv0h/F8Uu/zMLe67FjlKblyWgKa vfJs6wqEcmAuU7vTszUq797WNb8X3mSiWFTkLjdnk0xk+WtSecuGdXCDjwnTVXlm+csH yU3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version; bh=x2KIP1Lt19GBPYjqpvU0SCB+ZbGr+py11aK8ip3aCfo=; b=Y5fKtWqlr77WDe/+40UpaQU1LdkW8yTpp1oLEytsctus31O5eCuVi5bXkt1GQHm2o8 Q3b2E02leqwb6a9aV6dlCaoUdQz6ZUI1tUHQfkMDmlvw11Y3oYvhWUoYnpl2R3BGi1Iq +vbUlaMsp1Bc/5FNBs65uf/CoIc3BpW8GSo0EUqGinSD48VC5YF5Q17shP0QNIPonEVn TT7DjM4Jjcwu3vAWdhYGor24q8MXx9tvNVSGNm1raXKlQN4KD45qLQXfkv47QyyFMCB8 +F80U/4uiRg/TaT56pxByPpF24P/p24aYvaaP3nqAIh+1SMuh+ANiQLF2naj/kuWbJeY ZPFQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e16si7281616ejj.154.2021.05.12.01.35.16; Wed, 12 May 2021 01:35:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230471AbhELIfS (ORCPT + 99 others); Wed, 12 May 2021 04:35:18 -0400 Received: from mout.kundenserver.de ([217.72.192.75]:41185 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230448AbhELIfQ (ORCPT ); Wed, 12 May 2021 04:35:16 -0400 Received: from mail-wr1-f53.google.com ([209.85.221.53]) by mrelayeu.kundenserver.de (mreue109 [213.165.67.113]) with ESMTPSA (Nemesis) id 1MXp1O-1m1k830XfA-00YCsK; Wed, 12 May 2021 10:34:07 +0200 Received: by mail-wr1-f53.google.com with SMTP id a4so22734660wrr.2; Wed, 12 May 2021 01:34:07 -0700 (PDT) X-Gm-Message-State: AOAM532SD+eOVl9Z8mgV7ATqHbgIqZTWB4MrXdq03G3K3rP1iIX6P1G9 L30kcFy6YY4U9Es5N0kCN/9YY49gAVA3EkZzHDA= X-Received: by 2002:a5d:6dc4:: with SMTP id d4mr44897685wrz.105.1620808446720; Wed, 12 May 2021 01:34:06 -0700 (PDT) MIME-Version: 1.0 References: <20210401003153.97325-1-yury.norov@gmail.com> <20210401003153.97325-12-yury.norov@gmail.com> <1ac7bbc2-45d9-26ed-0b33-bf382b8d858b@I-love.SAKURA.ne.jp> <151de51e-9302-1f59-407a-e0d68bbaf11c@i-love.sakura.ne.jp> <030ae370-967c-22d4-56f8-cb0435be7540@rasmusvillemoes.dk> In-Reply-To: <030ae370-967c-22d4-56f8-cb0435be7540@rasmusvillemoes.dk> From: Arnd Bergmann Date: Wed, 12 May 2021 10:33:09 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 11/12] tools: sync lib/find_bit implementation To: Rasmus Villemoes Cc: Rikard Falkeborn , Tetsuo Handa , Andy Shevchenko , Yury Norov , Linux Kernel Mailing List , Andrew Morton , linux-m68k , Linux-Arch , Linux-SH , Alexey Klimov , David Sterba , Dennis Zhou , Geert Uytterhoeven , Jianpeng Ma , Joe Perches , John Paul Adrian Glaubitz , Josh Poimboeuf , Rich Felker , Stefano Brivio , Wei Yang , Wolfram Sang , Yoshinori Sato Content-Type: text/plain; charset="UTF-8" X-Provags-ID: V03:K1:iwwMIpoktxhIg2ZtuxsDZQ19rEoQ9vHiaS1UBmgFHHzEYbUaoPH afKeWTQq8Bsq9mXWYpyObnr/sPTqQVLNthfJwhDg4hSzHApeTuUXmuSno0bmOE6d3gNgjp5 EZOnFM9lA+uI8kTeh5A9SnHAhd05m4qvQVFu9sUn99B9AXC3qJ66Ob/b0Go5zKHQmmkAA9A R2+ktz2vHFdKK770nvLYw== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:bGu15vQlJQ4=:0R7rGHp8xMcwmjhk2w+BTQ eDmFWvyk7KR3jLbyebWOT35vu2T7/BOIaeDve8Tcw0+2B4V/BUxJ+SEUnPWB+uziKBoSOJ/nY 49B+IFhiCrLi1DuhzUKVpgoPSvigvkJOkiCg1LAFP0B98GYjUqRnVhWkDlelwFNmIGCqb3zXl fMaQjsGDGUWlraezDVRC9F0Kmzsb/ChKma55OxoyHsI113agDePMCc63hNy5/Wk0bU8qcHWL5 OqnvXPywmacTsOpbnuk+Non/6P5vDcYRNDDUsh97THR3PUtWfoCBI9mkWz7jA586IpoJgShPP /3BTw3F6dlfGg+lelaK8LEfihniUD2gCMQVIdlQoFisHeWKyUiT124hzCs3o0+9o0QZoWb4ZD ffmYpFG1vbjNlZ/kLtBzWFbrjJA3qADNu5JTOr0u/7rZNlcdjkieK1YhUxZ88GCzIsP3x/zFX ZOTq7EN/XuMXRKcxBgmV7cwhZiFkrU+HE0uKopjS/TSBBfXLPE46+PAFEq29En5WaCi71Ncah 2/eT/1epHB34q/ztvjcfMKmtLzd/j1QdECU/m92Ct2NZ9jba2YIKEAf1+DSU1NmRW8zD0Myzd /iUnGSATuh2czhYbohotsWkOuYhPNpsMQbZUqjQO2f6C8DHAt/dmBa/zLlAWRJk8Oq4SZNyCM Ch8Y= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, May 12, 2021 at 10:16 AM Rasmus Villemoes wrote: > It's more complicated than that. __builtin_constant_p on something which > is a bona-fide Integer Constant Expression (ICE) gets folded early to a > 1. And then it turns out that such a __builtin_constant_p() that folds > early to a 1 can be "stronger" than a literal 1, in the sense that when > used as the controlling expression of a ?: with nonsense in the false > branch, the former is OK but the latter fails: > > https://lore.kernel.org/lkml/c68a0f46-346c-70a0-a9b8-31747888f05f@rasmusvillemoes.dk/ > > Now what happens when the argument to __builtin_constant_p is not an ICE > is a lot more complicated. The argument _may_ be so obviously > non-constant that it can be folded early to a 0, hence still be suitable > as first argument to __b_c_e. But it is also possible that the compiler > leaves it unevaluated, in the "hope" that a later optimization stage > could prove the argument constant. And that's the case where __b_c_e > will then break, because that can't be left unevaluated for very long - > the very _type_ of the result depends on which branch is chosen. > > tl;dr: there's no "order in which the compiler processes those", __b_c_p > can get evaluated (folded) early, before __b_c_e inspects it, or be left > for later stages. Thanks for the detailed explanation. Checking the actual behavior of a trivial example, I find that int f(void) { const int i = 1; return __builtin_choose_expr(__builtin_constant_p(i), 1, 2); } used to return '2' with gcc-7, which is what I remembered. With gcc-8 and up as well as any version of clang, it returns '1' now: https://godbolt.org/z/7eKjbMocb I have also seen a couple of cases where __builtin_constant_p() without a __builtin_choose_expr() ended up unexpectedly returning true when gcc found a code path that it would be constant (e.g. conditionally initializing a variable to one of two possible ICEs), but then later turning that back into a non-constant expression in a later optimization stage. There is probably also a much more detailed explanation behind those. Arnd