From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by sourceware.org (Postfix) with ESMTPS id 020493858D3C for ; Sun, 1 Oct 2023 04:13:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 020493858D3C 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-x12d.google.com with SMTP id 2adb3069b0e04-5056ca2b6d1so2476962e87.1 for ; Sat, 30 Sep 2023 21:13:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696133584; x=1696738384; 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=IzGpciyhtTsADNGKTQXrxNpiMe9SCM9pUTkr2oW/SkA=; b=WGabropNJsTjmg53es1wKLbd1ENn2jBYFlszrWGGjWgF2FeyC0tKwNI0Zfg1ZngTov 5KUDrR16a7h87CMgpt4zpPJbihmcLqJBuc7g2I58oWt+xkXMINshpamzVi71B1bgNfdk RWC5K8OG5X23EoQmNmWyp0sfqpS4PAvRVbISzR8nIo0qe5SWhakAcq5jaK9GtGzal+K9 l4y0lV6kDCMnayOjzDThD42jBblI48c1nMR4Mhies3jdGCr3sD2+8FZcsSvi2WdBQoGS YXYk1CCPObYml+gtFfEoQUpT086maCMD9zxtBHzHkhnoNBgpVYaKmcHg7l5C6CP1tGtk woWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696133584; x=1696738384; 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=IzGpciyhtTsADNGKTQXrxNpiMe9SCM9pUTkr2oW/SkA=; b=JGc8iJxKXHD+Rmzzw81y2d0+H2/DMrkL741axnv7cSQsB9nV6U9WmJaqOD97l31PC4 flh7sUWRehqoqXE2IRjZKGRcXxkhhVuytVJBBCXchJtOk4aJrztS2DyMPgiLe2GurjCk nSH+U3T7cejDC1tHK48C5GuDlZhYdfEbzOJVO8/Mz2qsUJdjc/76myieM55r9d4OMNRo yaTra0q78e/vmEn863JHTetbI34GwzrQ/lH8iGjOLilPfrL19xbav36Fj+mlBMFBD/Va KhWL7ZkQOjpGZGjVA/+gs/BW/ihuh0IyRLkSKsGTnYVETgaiezhTNOSmbNZEhQv3E3RB CMMA== X-Gm-Message-State: AOJu0Yx5kO+7tjKTeUUycqOMQKrebrmHDOAaQ001qEKtMyY6tWvCPspB 3UzIrtzhGHQIhEroz4oGJy35T6QePRj4t8pFpFI= X-Google-Smtp-Source: AGHT+IFwag/yfyroV2UVyTyPOi38g/5XQ6qo0YSvrBL9xME5sZlDpDjNr/kVCOEMYVxbZMHWLygNb4OI3vlMvTiEPgY= X-Received: by 2002:a05:6512:110f:b0:503:cca:e535 with SMTP id l15-20020a056512110f00b005030ccae535mr7123574lfg.12.1696133584029; Sat, 30 Sep 2023 21:13:04 -0700 (PDT) MIME-Version: 1.0 References: <3bf388e5-0c20-8294-d99f-5f7c8a74607b@inria.fr> In-Reply-To: From: Hanke Zhang Date: Sun, 1 Oct 2023 12:12:51 +0800 Message-ID: Subject: Re: Question about merging if-else blocks To: Richard Biener Cc: gcc@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=1.7 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: 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? 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. 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 block= s, > > > > 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, li= ke > > > > 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 variab= le, > > > > it cannot be optimized as above. I would like to ask why this is? A= nd > > > > 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