From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by sourceware.org (Postfix) with ESMTPS id EE8D93858402 for ; Wed, 4 Oct 2023 07:57:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EE8D93858402 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-x12a.google.com with SMTP id 2adb3069b0e04-5041335fb9cso2076602e87.0 for ; Wed, 04 Oct 2023 00:57:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696406251; x=1697011051; darn=gcc.gnu.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=EL16QCJ9fF8oAxAAfA2MKUWna+Dx4IDwbCjSHTWdMGA=; b=AI9b5thpdR1uOfy0E+SaW3iXPMmSRaQO0OUhPcc2nAzhfzEhwjZr5Lna8sMjGbx7Nh RTqKwIHdHfcTqNEVvgQ7CzibwIDQA7iy/Zw/X2fNsXQb2yYY2ClcABz0jvb9yVsKQZ3J nmcRDg71sc1D11PDmz4+7/nx34CB4usj8++RGS6rQtjWt1nzuFayPQZkls/W9e/gKEwY S21KZTauou7O2MxxSNKutt/5q9Lh9/8MAiOZT5hB4C5KRxz2xBEVSU5xgUWrNQUElLj0 JYgRX55KJsp10Jy2VL1A3cCeevP9mtZVYu67bG3fPPWMYNyhXMv0424VWgIv318y/VAf sS5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696406251; x=1697011051; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EL16QCJ9fF8oAxAAfA2MKUWna+Dx4IDwbCjSHTWdMGA=; b=pbfjF0CqvCSszaZG0ZOLiUpX9MTVAkoRCwuq7cUnNaEgIJF1oejsS9xU++muWQQFVd FsgaUczrpQ190IslT420plvg9uZPhw9YBEqzfQJam741XAVHRSEGd7Q8KH8vlURzLuS+ BB9QLhtbG+WlEFgYO2p2ALPoJaPOIKEyQYmn/0u+7rduQqGdfkRGri/R7OKjfUWOnK3o 7Nwl5GGYteTSzVr0itCZ786YRiUt4MtHVsbaDUls1L2uUs8aSZcA3bXeCksq6abl+Uxv InGpP5hhfJNOVKtB1Yexa6eF0sjZQPdFjbx9dOKJzfOJ6m+rclnEH6uhHMW9sbnJoZrV AHmQ== X-Gm-Message-State: AOJu0Yy8fJEyRBUK/HZa8sEbdHQuDkBE0LMM+ZrsYvpDW7bw998e+1I7 aPtF9aCTVlET8d/rdlWoEtl/H9ILSIppHwGuy3I= X-Google-Smtp-Source: AGHT+IEbYH8CHn7/+Q+u7IofGdpcYc734nCbvSW+YBbNgvan4gLvsa/R0Vl9bbjz6zjTC0i3/DJUuZ3i90qbGew6HGI= X-Received: by 2002:ac2:5f6e:0:b0:503:3281:2ffd with SMTP id c14-20020ac25f6e000000b0050332812ffdmr1052671lfc.41.1696406250990; Wed, 04 Oct 2023 00:57:30 -0700 (PDT) MIME-Version: 1.0 References: <3bf388e5-0c20-8294-d99f-5f7c8a74607b@inria.fr> In-Reply-To: From: Richard Biener Date: Wed, 4 Oct 2023 09:54:56 +0200 Message-ID: Subject: Re: Question about merging if-else blocks To: Hanke Zhang , Florian Weimer Cc: gcc@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,BODY_8BITS,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Sun, Oct 1, 2023 at 6:13=E2=80=AFAM Hanke Zhang w= rote: > > Richard Biener =E4=BA=8E2023=E5=B9=B49=E6=9C= =8827=E6=97=A5=E5=91=A8=E4=B8=89 15:30=E5=86=99=E9=81=93=EF=BC=9A > > > > On Wed, Sep 27, 2023 at 7:21=E2=80=AFAM Hanke Zhang via Gcc wrote: > > > > > > Thanks! I understand what you mean, then can I think that if the > > > function here is not an external function, but a function visible to > > > the compiler and the function doesn't modify `a`, then these two > > > blocks can be merged? > > > > Yes. The key transform you'd see before any of the merging is > > CSE of the loads from 'a', then the rest is equivalent to the local > > variable case. > > > > Richard. > > Hi, Richard > > I'm still a little confused about this. > > I want to change the default behavior of gcc. We know that printf > won't change the value of 'a'. I'd like to let the compiler to get > this information as well. How can I do that? Or which pass should I > focus on? GCC has a builtin for 'printf' so it can handle those specially in builtins.cc:builtin_fnspec (see attr-fnspec.h for how the magic strings are encoded). If that's taught that printf cannot modify global memory it should already work, note though ... > By disassembling the exe file generated by icc, I found that icc will > merge these two blocks with the example code below. So I think there > maybe some ways to make it. ... glibc for example allows user-provided printf format callbacks so printf might call back into the current TU and modify globals in such callback. That's a GNU extension to printf that ICC likely doesn't support (https://www.gnu.org/software/libc/manual/html_node/Customizing-Pri= ntf.html), so that we're currently not doing this is for correctness. I'm not sure if this extension is much used or if it is maybe deprecated. With LTO it _might_ be possible to check whether any of the functions dealing with this (register_printf_function) is used. Without LTO for symbols with hidden visibility that do not escape the TU this analysis can be done TU-local. Richard. > Thanks. > Hanke Zhang. > > > > > > Marc Glisse =E4=BA=8E2023=E5=B9=B49=E6=9C=8827= =E6=97=A5=E5=91=A8=E4=B8=89 12:51=E5=86=99=E9=81=93=EF=BC=9A > > > > > > > > On Wed, 27 Sep 2023, Hanke Zhang via Gcc wrote: > > > > > > > > > Hi, I have recently been working on merging if-else statement blo= cks, > > > > > and I found a rather bizarre phenomenon that I would like to ask > > > > > about. > > > > > A rough explanation is that for two consecutive if-else blocks, i= f > > > > > their if statements are exactly the same, they should be merged, = like > > > > > the following program: > > > > > > > > > > int a =3D atoi(argv[1]); > > > > > if (a) { > > > > > printf("if 1"); > > > > > } else { > > > > > printf("else 1"); > > > > > } > > > > > if (a) { > > > > > printf("if 2"); > > > > > } else { > > > > > printf("else 2"); > > > > > } > > > > > > > > > > After using the -O3 -flto optimization option, it can be optimize= d as follows: > > > > > > > > > > int a =3D atoi(argv[1]); > > > > > if (a) { > > > > > printf("if 1"); > > > > > printf("if 2"); > > > > > } else { > > > > > printf("else 1"); > > > > > printf("else 2"); > > > > > } > > > > > > > > > > But `a` here is a local variable. If I declare a as a global vari= able, > > > > > it cannot be optimized as above. I would like to ask why this is?= And > > > > > is there any solution? > > > > > > > > If 'a' is a global variable, how do you know 'printf' doesn't modif= y its > > > > value? (you could know it for printf, but it really depends on the > > > > function that is called) > > > > > > > > -- > > > > Marc Glisse