From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [IPv6:2a00:1450:4864:20::129]) by sourceware.org (Postfix) with ESMTPS id 3337D3858017 for ; Wed, 27 Sep 2023 05:20:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3337D3858017 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-x129.google.com with SMTP id 2adb3069b0e04-50437f39c9dso14200917e87.3 for ; Tue, 26 Sep 2023 22:20:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695792045; x=1696396845; darn=gcc.gnu.org; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=xN0dXWTftP5Fzf+jIVZ7aZiA70ITp7DEiSYVf7O4T2w=; b=OeqFOgU1EUYle5WTp+VlcSAizMDxxaNEBvONC+RTTS7n1l1A6cYZwuKaij7QoLDvzc IEWJxMdVm1r4WMBizeqb4kqEV5fLsAqDEHHkS3VRZJypKNKGffv9Kqz4Bv64S+ASlsjJ nQHDcoVrB8iPu+TTP3E/ICfnJXS79KWhFt1ujmIqWFBMeX0/AisNsAloDPvLA8j22+cP GmVklg2kWEmOQVteSWRfuacb6TXMxthV/Yf21Hr8kq5cjmhhlkijm/Zv0Gsv4Lj1fjj9 53y79RaaQZURyOBuKhFkLtVOp7u9n0GSod0+lVzk2Qicv+TQCUOLYbjAjtSroW/njNRL B4qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695792045; x=1696396845; h=content-transfer-encoding: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=xN0dXWTftP5Fzf+jIVZ7aZiA70ITp7DEiSYVf7O4T2w=; b=poT/H+7a4Fja7CqnvpG+Dnty+YbnQcpOcEltUY4ldzbU/rLJIACRcCQQBjYoO2CT/S YXaSeBPJxJURGwovyNEnchYDobh0WD06dLTWBXAr4wuibZUS6gtAfEF2oLdKfl10Xfw4 yVwXRcxexNEQ+/KyZiZ8IqQjsoP7W9k8qnvjqsmH+ykrcqtah/+Rb259hxIHZ547NxPU EdK9bDwvJQ5EzfgE8nmaF8ud7zWs9LQsD3C5LkQdjoZR8H8SgWMOPgWfmow23K+6ACuV vIJDcB+5jAOKDHKu7vHipqf7RrqnHdzw6aBxqASIkMcbYC3ufDEfGJr/IWBBJA1I0PBm 0HxQ== X-Gm-Message-State: AOJu0YzqMAkOnho11ffrS+B7yMw144SWeeryxjGxuY+21pXKYiT1Z3A7 +WUP7n00Hg7HUg4GjAuyGplmYFr5FWmx6D5PgExt3G10YOr9wA== X-Google-Smtp-Source: AGHT+IF/lXvt18VDaFlSX/0oxpFIUmh5MjRNHNsQC9yX1LAFy9v0/BbK02ARqtVkc/5JruOjhif7RQY/hIz62dgD4fk= X-Received: by 2002:a05:6512:2343:b0:4f9:6842:afc with SMTP id p3-20020a056512234300b004f968420afcmr843725lfu.64.1695792045144; Tue, 26 Sep 2023 22:20:45 -0700 (PDT) MIME-Version: 1.0 References: <3bf388e5-0c20-8294-d99f-5f7c8a74607b@inria.fr> In-Reply-To: <3bf388e5-0c20-8294-d99f-5f7c8a74607b@inria.fr> From: Hanke Zhang Date: Wed, 27 Sep 2023 13:20:33 +0800 Message-ID: Subject: Re: Question about merging if-else blocks To: gcc@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=2.2 required=5.0 tests=BAYES_00,BODY_8BITS,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Level: ** X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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? 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 f= ollows: > > > > 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 its > value? (you could know it for printf, but it really depends on the > function that is called) > > -- > Marc Glisse