From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x130.google.com (mail-lf1-x130.google.com [IPv6:2a00:1450:4864:20::130]) by sourceware.org (Postfix) with ESMTPS id 113A93858017 for ; Wed, 27 Sep 2023 07:30:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 113A93858017 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-x130.google.com with SMTP id 2adb3069b0e04-50307759b65so17055511e87.0 for ; Wed, 27 Sep 2023 00:30:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695799840; x=1696404640; 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=q4URXZ7BDYDhyCo+ZrklwNsNc39dWEGjwZS8sTL28bo=; b=Fa3byvJ55F7x3CO7wtcHNmZ05lqrIR797WguoKsNf4NvK7g5Ju9F8MTxTq27jBXoUz ZFfePsiQb/CxnwmBwXvqjWeFQzAvC9ab6MdD2/9f1/IzqniT8XcPsTXkk7/BaJV0BnPr 6HBOYJj5JlvIWJvG5oWDpUFbonSBa491GYs19Fv+lYWvGpv/b2w3QyLkZP24hcZkSRdp F04HHX4hZFLBatF2bKQBoJjPQTGM3IDyzj3L6dZ1teKpCzHwQS9jQrQ5xdXUaLzdfykF ix970vokFU30t27OzJXty7dkFit0kJOPRw0B4QVnRuycdRf73KK5A/rek34hi7oTNkae ARnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695799840; x=1696404640; 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=q4URXZ7BDYDhyCo+ZrklwNsNc39dWEGjwZS8sTL28bo=; b=vj9Jcuz/W1HJJkoGKdIfci/Ne+p6D0Q0cJTp9CjBnjRd9jMcS+lQhk5arTOir7awdO sokSgQuKC0qayu+IXgEkQLYZfr/Uu627kokzyY0eP3cdVTC0aLHV1dH5ASVB/s4huo6R 6VyGU09ai2LVN+3c0AE4d0GvB/HT4IyuQzxfutVcQLH0FdIC4N2T/++B9jQMqN2xqdGd y1V+GGPmSrJiAzSpMoBHl1FXhmeed0wiH+suETdAZ722RT/q2EhfPgc4p2MVSJjL8xil OSTQdjMe+Z4neuIXCLgb6tIFeFXaDkkirZGNfo9k07SlUzXsmsWemXI+OloCu+fHQeZu Ot1w== X-Gm-Message-State: AOJu0YxE1MLIP/y53tlaoOkqJw56IGkwaaI0zbDIT0p5EiNe4WpO2zpd 1Wr/b7T0AmXKphrD7Qimpa5f3jYa5cTtMTzqVwE= X-Google-Smtp-Source: AGHT+IFyvv31zX4lsOrjxLDQKbf266S+9XCohdVNL+Q7uwgx0XylVIr2q0blKhvCRkhBvZu3erxkF0mcHzq4uNMSB60= X-Received: by 2002:a05:6512:324e:b0:503:264b:efc9 with SMTP id c14-20020a056512324e00b00503264befc9mr851622lfr.18.1695799840257; Wed, 27 Sep 2023 00:30:40 -0700 (PDT) MIME-Version: 1.0 References: <3bf388e5-0c20-8294-d99f-5f7c8a74607b@inria.fr> In-Reply-To: From: Richard Biener Date: Wed, 27 Sep 2023 09:28:16 +0200 Message-ID: Subject: Re: Question about merging if-else blocks To: Hanke Zhang 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,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 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. > 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 blocks, > > > 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, if > > > 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 optimized 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 variable= , > > > 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 modify it= s > > value? (you could know it for printf, but it really depends on the > > function that is called) > > > > -- > > Marc Glisse