[{"id":3683294,"web_url":"http://patchwork.ozlabs.org/comment/3683294/","msgid":"<CAFiYyc04YMChqBVR54C9e0MO6Vvth0LJywLNX=Lcx8xbF_6jFg@mail.gmail.com>","list_archive_url":null,"date":"2026-04-28T09:26:49","subject":"Re: [GCC17-PATCH 2/2] phiopt: Allow for more than 2 predecessors for\n join block for cselim-limited in phiopt [PR123113]","submitter":{"id":1765,"url":"http://patchwork.ozlabs.org/api/people/1765/","name":"Richard Biener","email":"richard.guenther@gmail.com"},"content":"On Fri, Apr 17, 2026 at 4:10 AM Andrew Pinski\n<andrew.pinski@oss.qualcomm.com> wrote:\n>\n> This is the first in the series to remove the constraint in phiopt when doing store\n> and operator sinking that the join block needs to have only 2 predecessor edges.\n> A forwarder block needs to be created for the new join block.  Since make_forwarder_block\n> keeps around the original bb as new forwarder block, this makes it easier to handle.\n> And the original phi nodes are also kept around too (though this is not needed\n> to be handled for cselim-limited but will be for factoring).\n\nIn general looks good.  Did you carefully check how creating a new forwarder\nwill mess with the iteration over blocks/phis?\n\n> Bootstrapped and tested on x86_64-linux-gnu.\n>\n>         PR tree-optimization/123113\n> gcc/ChangeLog:\n>\n>         * tree-ssa-phiopt.cc (edges_split): New function.\n>         (make_forwarder): New function.\n>         (cond_if_else_store_replacement_1): Create the new\n>         forwarder block right before doing the store sinking.\n>         (pass_phiopt::execute): Remove the restriction on cselim_limited\n>         for 2 predecessors.\n>\n> gcc/testsuite/ChangeLog:\n>\n>         * gcc.dg/tree-ssa/ssa-sink-13.c:\n>         * gcc.dg/tree-ssa/cselim-6.c: New test.\n>\n> Signed-off-by: Andrew Pinski <andrew.pinski@oss.qualcomm.com>\n> ---\n>  gcc/testsuite/gcc.dg/tree-ssa/cselim-6.c    | 18 +++++++++\n>  gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-13.c |  2 +-\n>  gcc/tree-ssa-phiopt.cc                      | 43 ++++++++++++++++++++-\n>  3 files changed, 61 insertions(+), 2 deletions(-)\n>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cselim-6.c\n>\n> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cselim-6.c b/gcc/testsuite/gcc.dg/tree-ssa/cselim-6.c\n> new file mode 100644\n> index 00000000000..1f726ccca78\n> --- /dev/null\n> +++ b/gcc/testsuite/gcc.dg/tree-ssa/cselim-6.c\n> @@ -0,0 +1,18 @@\n> +/* { dg-do compile } */\n> +/* { dg-options \"-O1 -fdump-tree-phiopt1\" } */\n> +\n> +int t;\n> +void f(int a, int b, int c, int d)\n> +{\n> +  if (a)\n> +  {\n> +    if (b)\n> +     t = c;\n> +    else\n> +     t = d;\n> +  }\n> +}\n> +\n> +/* We should sink/merge the store. */\n> +\n> +/* { dg-final { scan-tree-dump-times \"t = cstore\" 1 \"phiopt1\" } } */\n> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-13.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-13.c\n> index 584fd91f43a..75cb354052b 100644\n> --- a/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-13.c\n> +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-13.c\n> @@ -1,6 +1,6 @@\n>  /* PR33315 */\n>  /* { dg-do compile } */\n> -/* { dg-options \"-O2 -fdump-tree-sink\" } */\n> +/* { dg-options \"-O2 -fdump-tree-sink -fno-ssa-phiopt\" } */\n>\n>  int num;\n>  int a[20];\n> diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc\n> index 324559e6a7d..76527ff922c 100644\n> --- a/gcc/tree-ssa-phiopt.cc\n> +++ b/gcc/tree-ssa-phiopt.cc\n> @@ -3115,6 +3115,42 @@ cond_store_replacement (basic_block middle_bb, basic_block join_bb,\n>    return true;\n>  }\n>\n> +/* Callback function for make_forwarder_block,\n> +   returns true if the edge src is either of\n> +   the 2 basic blocks in the array BBS (DATA). */\n> +\n> +static bool\n> +edges_split (edge e, void *data)\n> +{\n> +  basic_block *bbs = (basic_block*)data;\n> +  return e->src == bbs[0] || e->src == bbs[1];\n> +}\n> +\n> +\n> +/* Makes a forwarder block for JOIN_BB such that\n> +   THEN_BB and ELSE_BB are the only 2 predecessors\n> +   of the block. Note the JOIN_BB is required to be\n> +   the same and the PHI nodes of JOIN_BB have to be\n> +   the same too.  */\n> +\n> +static void\n> +make_forwarder (basic_block then_bb, basic_block else_bb,\n> +               basic_block join_bb)\n> +{\n> +  basic_block bbs[2];\n> +\n> +  /* If the join block already has 2 predecessors, then there\n> +     is nothing to be done.  */\n> +  if (EDGE_COUNT (join_bb->preds) == 2)\n> +    return;\n> +  bbs[0] = then_bb;\n> +  bbs[1] = else_bb;\n> +  edge ne = make_forwarder_block (join_bb, edges_split, bbs);\n> +  gcc_assert (join_bb == ne->src);\n> +  statistics_counter_event (cfun, \"if-then-else split for inserting common\", 1);\n> +}\n> +\n> +\n>  /* Do the main work of conditional store replacement.  */\n>\n>  static bool\n> @@ -3201,6 +3237,11 @@ cond_if_else_store_replacement_1 (basic_block then_bb, basic_block else_bb,\n>    gsi_remove (&gsi, true);\n>    release_defs (else_assign);\n>\n> +  /* If the join bb has more than 2 predecessors,\n> +     then we need to create a new join.  */\n> +  if (EDGE_COUNT (join_bb->preds) != 2)\n> +    make_forwarder (then_bb, else_bb, join_bb);\n> +\n>    /* 2) Create a PHI node at the join block, with one argument\n>         holding the old RHS, and the other holding the temporary\n>         where we stored the old memory contents.  */\n> @@ -4050,7 +4091,7 @@ pass_phiopt::execute (function *)\n>           /* Try to see if there are only store in each side of the if\n>              and try to remove that; don't do this for -Og.\n>              With sinking the stores we might end up with empty blocks.  */\n> -         if (EDGE_COUNT (bb3->preds) == 2 && !optimize_debug)\n> +         if (!optimize_debug)\n>             while (cond_if_else_store_replacement_limited (bb1, bb2, bb3))\n>               cfgchanged = true;\n>         }\n> --\n> 2.43.0\n>","headers":{"Return-Path":"<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":["incoming@patchwork.ozlabs.org","gcc-patches@gcc.gnu.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","gcc-patches@gcc.gnu.org"],"Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256\n header.s=20251104 header.b=qwA7RZ4a;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=2620:52:6:3111::32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)","sourceware.org;\n\tdkim=pass (2048-bit key,\n unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256\n header.s=20251104 header.b=qwA7RZ4a","sourceware.org;\n dmarc=pass (p=none dis=none) header.from=gmail.com","sourceware.org; spf=pass smtp.mailfrom=gmail.com","server2.sourceware.org;\n arc=pass smtp.remote-ip=209.85.208.49"],"Received":["from vm01.sourceware.org (vm01.sourceware.org\n [IPv6:2620:52:6:3111::32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g4ZpZ3qcTz1xvV\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 28 Apr 2026 19:27:34 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id B003D4BA9018\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 28 Apr 2026 09:27:32 +0000 (GMT)","from mail-ed1-f49.google.com (mail-ed1-f49.google.com\n [209.85.208.49])\n by sourceware.org (Postfix) with ESMTPS id 8141D4BA9018\n for <gcc-patches@gcc.gnu.org>; Tue, 28 Apr 2026 09:27:03 +0000 (GMT)","by mail-ed1-f49.google.com with SMTP id\n 4fb4d7f45d1cf-6720c7968e4so10234443a12.0\n for <gcc-patches@gcc.gnu.org>; Tue, 28 Apr 2026 02:27:03 -0700 (PDT)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org B003D4BA9018","OpenDKIM Filter v2.11.0 sourceware.org 8141D4BA9018"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org 8141D4BA9018","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org 8141D4BA9018","ARC-Seal":["i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1777368423; cv=pass;\n b=TSKsQ3qSke2gRY3n70YvZpAzWxJqGRTYFd40BwhpxoKsonu2L6Fhqi/hvPX2rn+X5vVY6k5SWUks2G5gorJOeuMMhsgLB4rb3e3B12DVqsKjFhgCQ251LZXEu4Nmqn1GjHLX67eGhH6XtuGWB/kIckRDtP7v/0d3DewOW18HorY=","i=1; a=rsa-sha256; t=1777368422; cv=none;\n d=google.com; s=arc-20240605;\n b=T6ll1E8PFrX4Vtb/1ufGLk4adpG8+nyrMFuLGuBMW+9wUKmOOsaxhXRKHKwc6/knex\n yhqHwP/GdHfGtv0Uv9d+A42Q6j6v9UGqE2s1zU0OH4kuuudX+adJmUVq59u9e0YBJqnm\n vz0DbevhrtQww7+SvG1Q36HU9G+p1C3jNP2bAEBZlFrGcUqfHiV6RriXZiYkbgRP+7jG\n GgSPQAuFrieb+9D/2dTgAdbtKi33pr1cnx2pbiSo1Wj47MfP1F5um+Ynr+jkoDGRHpnv\n n+Zh1uY30HIzclgGS+H/Yh2NB00DuFRKWIPt5uaBURRS26tOQkkbHPPJ2fE+kMrL78TB\n 9dyg=="],"ARC-Message-Signature":["i=2; a=rsa-sha256; d=sourceware.org; s=key;\n t=1777368423; c=relaxed/simple;\n bh=ipguNaE8OR5hjc4PxbVvEmgrNFQbhhHeborLy2wRr08=;\n h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To;\n b=mnjj634zMmOcidEaCSHLRgduvGoeW56i2Mj8GlOmYfkCBddNPJC5VBKyWl6wRpXy3PAXKSnoIhcnPswqksHlJEQj9iVChBnDGN6m/zxvsEPRbVYw9jvMGmj0EtC7MnQY3OeSWERcoWm6yAYD0Aan6ZXwQNz/t9n8WT6ANfWZ0YI=","i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com;\n s=arc-20240605;\n h=content-transfer-encoding:cc:to:subject:message-id:date:from\n :in-reply-to:references:mime-version:dkim-signature;\n bh=be/0nlZrTjG5Ui1kEvulpTJ0x2qMrfBifrRDpRk0xI0=;\n fh=+uEDwx55CzEWtNA/tzpV1shOHaMh+Xh7AN17D33930Y=;\n b=A+2IoWNKj4MlJ4mhy62+IE0PpDm7NkeZSFF3OLMMdf6oiF8Cr4H9hLwH3TX3F927zW\n HRackjtkgtqDCLO+SkXY2U44qYKmz4QtVasqeU8GhDtTtKDOmMUOV/x9xPp5WdJ4Mx8V\n 4LoQg0bEMR1s3DMUl0RJY0QRFiX9XO12Y1pLPdrX2sSbzT+B3+TnHZjeX8dS5Et79P+h\n diOZOBTOm+STePW3Frd8JsE00YEuAAdn3Iq+j7u08+hoUGEhbkkxRXPrD3EcmCnuxAxg\n telum6vI7o7UhniuYG9cQXuUtOZCJE95sh+b+Rl63PSRb+es52v0/h4ZwAP9wt0mhfmW\n VrNA==; darn=gcc.gnu.org"],"ARC-Authentication-Results":["i=2; server2.sourceware.org","i=1; mx.google.com; arc=none"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=gmail.com; s=20251104; t=1777368422; x=1777973222; darn=gcc.gnu.org;\n h=content-transfer-encoding:cc:to:subject:message-id:date:from\n :in-reply-to:references:mime-version:from:to:cc:subject:date\n :message-id:reply-to;\n bh=be/0nlZrTjG5Ui1kEvulpTJ0x2qMrfBifrRDpRk0xI0=;\n b=qwA7RZ4aYBRchfNUVapV45isf/kKJIAZ1vpghllqRGIAobF8rl8wQ0MNfOBn8U8O00\n oGuUjJhmTkwDuooFY6KFWlh28ucFAmVzSxPjpeztm8ZQt2E+MFNX3vB1SnxUKakKAlsP\n Yu3W1e5Tg/lGPxsGivPdQyd4P1o5reGkVXOriG863/Yy1FSI0HMq374r/s7LCK/Opdgy\n 4RiPde4TejeTT5tT9KovOQcjK9hSQq64vpoONuSm43LUOyQqfygfF/e6oOPe67jbrViE\n xSK0B7tpnYCRLE3MomtUEkEghNmvdscrPVRwh24+t33P3s/GcAGa8eHYzkZvZZuTl6Ju\n gxbw==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1777368422; x=1777973222;\n h=content-transfer-encoding:cc:to:subject:message-id:date:from\n :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from\n :to:cc:subject:date:message-id:reply-to;\n bh=be/0nlZrTjG5Ui1kEvulpTJ0x2qMrfBifrRDpRk0xI0=;\n b=QtXvCuAEn4meqUdMruhXBZ8DRVQxLT2b3qBQJqCM/PD6HpGpL2ACjwWWHlRrzOM1On\n WGVQJPUvVdmFqZ5rtONrKXMZpld0Zz7Xovc3Bs1BiPVPMck6+hUiiX8YZU8UGDm7jmtz\n WNJYoV9RN1dLydti+jYjpapXYnhKz+v/xObkKegOTDMovzOhpIKgETSnMuk6T1esL/EY\n Rj1P68wUGZi7JVR3NufRftxFsR4hLTho7UD57Es2nBKeNKyB6D4kozPnXT+zkwu/n3St\n +uv7CDDc0SPfW0GFszl1jNIbUvC7pI3MGuHvmM7Q4XJJMBygZQ7N4IF6RCx6X05gMYJF\n DvUQ==","X-Gm-Message-State":"AOJu0YygLXsRh5Gn20I/GP8tbQzn6yvMXAdAeHcK5Liy9M2L0+NC1YXn\n 62agOVp+0ma6n1YDDGSxm27hFyYgkC4uU+o9Lr5Rl3VIdwBWxQo6nOmOHKtBD1btG1azgrZOoJy\n NTpJeTAkQwCV2boKOiJlihmJHwbfT3ss+iQ==","X-Gm-Gg":"AeBDiesZ7NInOdBM74vBteG0bwZh+33nL9s8+sQjVTTJaAgsc473px9X4U2gMV7zeMc\n CzwCxVc5qJzZuSmJvbyDj9vwJljommrlfeYPVfb8VeKYdXhwd8uu+5WZFSgm/hAqjBwDcV7HQ5G\n dMSg9eq3VLgwLBCTu+HztOfSdLZfKoXI0dTmMBliYNBTOgO44lkePRVLOuiyXCNQWBIzHNProv/\n h8wJ6TMeYTgauZSgxBcM93GukLFplQy1mT35FAslH90QCr5ZbSwywamM7+BnLImu3ssjhropdys\n suac73nqm/k6w7cu","X-Received":"by 2002:a05:6402:324c:b0:672:c664:38d8 with SMTP id\n 4fb4d7f45d1cf-679bafd7335mr768767a12.2.1777368422094; Tue, 28 Apr 2026\n 02:27:02 -0700 (PDT)","MIME-Version":"1.0","References":"<20260417020830.4154676-1-andrew.pinski@oss.qualcomm.com>\n <20260417020830.4154676-2-andrew.pinski@oss.qualcomm.com>","In-Reply-To":"<20260417020830.4154676-2-andrew.pinski@oss.qualcomm.com>","From":"Richard Biener <richard.guenther@gmail.com>","Date":"Tue, 28 Apr 2026 11:26:49 +0200","X-Gm-Features":"AVHnY4JBqiqk_OIye26_ulMKHePjdfnOLrSDJ3AWaVULVIIssoTaB8Cit2JzL28","Message-ID":"\n <CAFiYyc04YMChqBVR54C9e0MO6Vvth0LJywLNX=Lcx8xbF_6jFg@mail.gmail.com>","Subject":"Re: [GCC17-PATCH 2/2] phiopt: Allow for more than 2 predecessors for\n join block for cselim-limited in phiopt [PR123113]","To":"Andrew Pinski <andrew.pinski@oss.qualcomm.com>","Cc":"gcc-patches@gcc.gnu.org","Content-Type":"text/plain; charset=\"UTF-8\"","Content-Transfer-Encoding":"quoted-printable","X-BeenThere":"gcc-patches@gcc.gnu.org","X-Mailman-Version":"2.1.30","Precedence":"list","List-Id":"Gcc-patches mailing list <gcc-patches.gcc.gnu.org>","List-Unsubscribe":"<https://gcc.gnu.org/mailman/options/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe>","List-Archive":"<https://gcc.gnu.org/pipermail/gcc-patches/>","List-Post":"<mailto:gcc-patches@gcc.gnu.org>","List-Help":"<mailto:gcc-patches-request@gcc.gnu.org?subject=help>","List-Subscribe":"<https://gcc.gnu.org/mailman/listinfo/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"}}]