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 C7A743858C74 for ; Wed, 27 Sep 2023 03:47:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C7A743858C74 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-50433d8385cso15659068e87.0 for ; Tue, 26 Sep 2023 20:47:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695786449; x=1696391249; darn=gcc.gnu.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=lCv28bMmacZx0YS+wCjM1N1VfuW/fEWgsnefC4nk8Oo=; b=CFaEA3xJEzZqN/fIOFs5Bz2C5i5nyjDsllqv/bBmIuQQE9pn4yTHen6wfuWCbku9lc nB42z5+zJkXnPHpbjDb6G88St1fj5giNm+sWbMg77tVa4zJfy/26jvlaQOoMjYErXrdb Ea8U/fOMopm9dgdtbzteGmC0MQk/TdheZxfB26HUdgH4Rv6wBNJ75WBsyql9PuGiV5XO 7KpS6cXh/ta3q7UPSHE+t4jxbhJL0poQ3mR3ho8PuVGK+dP+lHigfLkCpsba2P7JkNTq 15JyurpLepWCRucWvCT+WafIVdaFXygrkHzPQCHhNdfHCs2crc8rKTu5njGtewX972Qr 2puA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695786449; x=1696391249; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=lCv28bMmacZx0YS+wCjM1N1VfuW/fEWgsnefC4nk8Oo=; b=Lv62WtZSEV3xxeiX3fiTLIPf2Ang8eKbkPkBQ1sV43Q3szT/9cTASMXYSd+GHqtc80 sULsG+1tDFOoYIn6WxFi43Q2g4hQPJVJLR2v57AIcm0QWPaHdqpNJfZ9PfqQBmAapNgf LDaXpPUsj8z9t8n7XE5SXPPkIb3/p7rA8KIj00Usv5TYkAASnoON4XxD4n4M5OQ8NCfm MnGknFPUVUYbX+xyjl+cHaapeMorPYRjzVlpn3VVtBWWN/BaxCcJfTyf7mhwiJndVGLw x6CQZjMd4u1/PZCKiZsFYibBAD2hiF8foBEXMGdcVE+aOMhOJ1DGG1MsbpWC0/2AbO4A NB/g== X-Gm-Message-State: AOJu0YylD4gPvA51e+DawNWhCLmAdieFK+JoK/RJvUiwaCwaz2QOunTy mAvgwiXcEs13TeW6HsAJFc5DODqurBIGyKbvlf8nKRoTH0socA== X-Google-Smtp-Source: AGHT+IE212/IciI3BUhnHeSoujMZ6wCk7s5G9b5kZxln8bV0Hy7HiElJJTgmP0sERpAsnSI4fu8E9H6s5XoU4IwZdFI= X-Received: by 2002:a05:6512:4021:b0:4fe:551:3d3c with SMTP id br33-20020a056512402100b004fe05513d3cmr837948lfb.36.1695786448562; Tue, 26 Sep 2023 20:47:28 -0700 (PDT) MIME-Version: 1.0 From: Hanke Zhang Date: Wed, 27 Sep 2023 11:47:16 +0800 Message-ID: Subject: Question about merging if-else blocks To: gcc@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=0.7 required=5.0 tests=BAYES_00,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-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 = 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 = 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? Thanks. Hanke Zhang.