{"id":2235283,"url":"http://patchwork.ozlabs.org/api/patches/2235283/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/patch/20260509020702.714331-1-yw987194828@gmail.com/","project":{"id":17,"url":"http://patchwork.ozlabs.org/api/projects/17/?format=json","name":"GNU Compiler Collection","link_name":"gcc","list_id":"gcc-patches.gcc.gnu.org","list_email":"gcc-patches@gcc.gnu.org","web_url":null,"scm_url":null,"webscm_url":null,"list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20260509020702.714331-1-yw987194828@gmail.com>","list_archive_url":null,"date":"2026-05-09T02:07:02","name":"tree-optimization: Fix profile update in loop splitting (initial_true=false)","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"605d20024f300a07d8e57d614d2c1539df4e879a","submitter":{"id":88584,"url":"http://patchwork.ozlabs.org/api/people/88584/?format=json","name":"Xin Wang","email":"yw987194828@gmail.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/gcc/patch/20260509020702.714331-1-yw987194828@gmail.com/mbox/","series":[{"id":503474,"url":"http://patchwork.ozlabs.org/api/series/503474/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/list/?series=503474","date":"2026-05-09T02:07:02","name":"tree-optimization: Fix profile update in loop splitting (initial_true=false)","version":1,"mbox":"http://patchwork.ozlabs.org/series/503474/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2235283/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2235283/checks/","tags":{},"related":[],"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=moMCUCWw;\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=moMCUCWw","sourceware.org;\n dmarc=pass (p=none dis=none) header.from=gmail.com","sourceware.org; spf=pass smtp.mailfrom=gmail.com","sourceware.org;\n arc=none smtp.remote-ip=2607:f8b0:4864:20::636"],"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 4gC8X25YRLz1yHW\n\tfor <incoming@patchwork.ozlabs.org>; Sat, 09 May 2026 12:07:46 +1000 (AEST)","from vm01.sourceware.org (localhost [IPv6:::1])\n\tby sourceware.org (Postfix) with ESMTP id 4854E4BA2E0F\n\tfor <incoming@patchwork.ozlabs.org>; Sat,  9 May 2026 02:07:44 +0000 (GMT)","from mail-pl1-x636.google.com (mail-pl1-x636.google.com\n [IPv6:2607:f8b0:4864:20::636])\n by sourceware.org (Postfix) with ESMTPS id 48FC34BA5435\n for <gcc-patches@gcc.gnu.org>; Sat,  9 May 2026 02:07:12 +0000 (GMT)","by mail-pl1-x636.google.com with SMTP id\n d9443c01a7336-2ba0714574fso15335025ad.2\n for <gcc-patches@gcc.gnu.org>; Fri, 08 May 2026 19:07:12 -0700 (PDT)","from localhost.localdomain ([175.152.51.41])\n by smtp.gmail.com with ESMTPSA id\n d9443c01a7336-2baf1e35eb3sm35400445ad.41.2026.05.08.19.07.07\n (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n Fri, 08 May 2026 19:07:10 -0700 (PDT)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org 4854E4BA2E0F","OpenDKIM Filter v2.11.0 sourceware.org 48FC34BA5435"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org 48FC34BA5435","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org 48FC34BA5435","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778292432; cv=none;\n b=EjYQOH06kHGQ72wU/5TVNZA9R+GLkWbZ5sCyi6It6EgJ987IYAtqtnNavmlFayGxn0yggRU8zkaW5awC6e9gz9W6RXRt4k7flo/Uua/fPUmA9z7ggpA63ilGffEgoOg97BSNB1xUOYrFhjDCIkpSsCKEsI4WsY9jRyjrvZo4/tI=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1778292432; c=relaxed/simple;\n bh=8ftL9zB5AQ976Vr0bM4UW5IUBRsiVL2PSnMcJ+5z3Oc=;\n h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version;\n b=c2fNdsnNI/e/4nNeh5xAI/Agdt4Dh/s5qpz3ZWxcm4N1kUECqNHW6i2YXPEtQg/GZ0VXRCqhb42UydylI6leSza+vaVqI3wgjqoS5gLYuJs6OtiBuUMe6V/K2PCJQZmUSKyni+6PN/HVlMoyfwIJl16gvCSXy7kuaQVENjqpe64=","ARC-Authentication-Results":"i=1; sourceware.org;\n dkim=pass (2048-bit key, unprotected)\n header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104\n header.b=moMCUCWw","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=gmail.com; s=20251104; t=1778292431; x=1778897231; darn=gcc.gnu.org;\n h=content-transfer-encoding:mime-version:message-id:date:subject:cc\n :to:from:from:to:cc:subject:date:message-id:reply-to;\n bh=v1lmDLibV7Yk4Tl1EyVU0IeFIe1m0nQduRWWy6szRtE=;\n b=moMCUCWwxB/MFjIbHd8rCu5nEz5tanxKRQNcE2r4fRyOIIey5wdGLHW/S5EM1ZCikJ\n Lf5zcGrUsPzWWGrrfrAm7CLMw/opgPtmCdjJ798MldSSjsj0evLF9T3PMaGyE73DNPaK\n EwpjIwJEQRaFaiz8Z9pZo13YRyLN9DECRri8vDt2JvHDLg+oqHZd6dOz6O0+r7Do6Xsy\n 2YpP0IQf0wXCqGNgOONC/CVBgMR8M6eNnenMumW0kDB4M9aqsR6nUjacEnB+hp1qMmjE\n XW72NVERfBlg/r5BBSYreXY3soQMQyXyJ05NLo5788PsDDz7y2MCotM/RFxenNtDa2e9\n vMGg==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1778292431; x=1778897231;\n h=content-transfer-encoding:mime-version:message-id:date:subject:cc\n :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date\n :message-id:reply-to;\n bh=v1lmDLibV7Yk4Tl1EyVU0IeFIe1m0nQduRWWy6szRtE=;\n b=VtGuspuNvzCW9I67VVFmpyWWhXPLQjdfdclD958jR+kE1X+aVQUnjjTCG3HDMP3A4l\n idSWJC5RObhqY6aNzWBa1BOdfbzu2JPL7Qqs0qXkIgoj3uF32Dfim2zA/slXw2tMVObE\n sZT1Xe1KgMPO185j9LAFdK6OAxzxeAVAg+PS3lsyrzGU9iA1e9wAqhWIFwYBZ3ujGmNF\n trpgZbX6pIk9IcwTZM/ZP+xtlZj7P2FtBW8JDId6hVnQwIsVZTLXgKijc+5rQmjsJDp4\n sWeDQTAKT0j5TgkZtwmODevV0DSPSj34ys9bpW5VuAQc3ajMuM5osKC7CO54M9YxpCQF\n s6hQ==","X-Gm-Message-State":"AOJu0YzJ3SNzaqKhwSKDh9fereXyPD65u6IrMt0mJuc9/mC85cODxIKk\n 0c8lLJuvqZjZzHOLRqG7t0nBr6MzAkDAlp8p6VmXPDqNk8W542kMYYGmsn4+UefUHTzBmg==","X-Gm-Gg":"Acq92OGKrgT0TZEGtWneOivbFc/NTcQ0jObJjGtdWlYJUoGoFNFkNkksPCPHHN0UWfr\n hVYb6Jb+0lPqGhgdcGHRQ1UfWKm/kmPq50xTxNjOXdPRJmgtWJ6ERD/41R+d0y9gs4s+QAOP6AW\n eV/279+wWMZ+b1Wr8bYJXAAqvxEY/Qdm8YXJ+Ns9sdcQGo2KyfK/7Bj0jHQZ83iNGXscW9NTjpc\n bgPtAS+0aKn0cyU6b4x3Rd9TP+7cz/430ZS32NRdRVW/TThjIPZR+yWoZiZT5QKN+iIjZOvKXcd\n 0mUz7VDHHfVCy8ov1ySPGA9wtxlOmXlEt1qPJMvPMxcmWey1U79TBGB9OsvxnPJX9cHytW6t/gI\n R2+mvHqOLkM3NOb5dz/KJeW+ALLojmyjOf1jav1Oc+k9dQQbauzb9U3hQfa7Fto4XXGSRF89Dyf\n WJ75ZpEbi48lK2mzCqCfDuhZYmgr3P2rVz5LoHWN8j+4B/6v4=","X-Received":"by 2002:a17:903:1ab0:b0:2b2:4ffc:a7c4 with SMTP id\n d9443c01a7336-2ba798c0a22mr134077305ad.24.1778292430798;\n Fri, 08 May 2026 19:07:10 -0700 (PDT)","From":"Xin Wang <yw987194828@gmail.com>","To":"gcc-patches@gcc.gnu.org","Cc":"jakub@redhat.com, rguenther@suse.de, luoxhu@linux.ibm.com,\n Xin Wang <wangxinw@hygon.cn>","Subject":"[PATCH] tree-optimization: Fix profile update in loop splitting\n (initial_true=false)","Date":"Sat,  9 May 2026 10:07:02 +0800","Message-Id":"<20260509020702.714331-1-yw987194828@gmail.com>","X-Mailer":"git-send-email 2.25.1","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","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"},"content":"From: Xin Wang <wangxinw@hygon.cn>\n\nWhen split_loop does iteration space splitting, split_at_bb_p may\nswap the guard condition so that operand 0 is always the loop IV\nand operand 1 is the invariant.  For example, \"t < i\" (LT_EXPR)\nbecomes \"i > t\" (GT_EXPR).  This can cause initial_true to be\nfalse, meaning loop1 handles iterations where the guard is false\nand loop2 handles iterations where the guard is true.\n\nThe function fix_loop_bb_probability scales loop1's body by\ntrue_edge->probability and loop2's body by its inverse.  But when\ninitial_true is false, loop1's body executes the false edge path.\nLoop1 should be scaled by false_edge->probability instead.\n\nThis inconsistency is visible in the guard patching code a few\nlines below, which does swap force_true/force_false based on\ninitial_true.  The profile scaling should apply the same logic.\n\nThe bug caused BB counts in the split loops to be swapped when\ninitial_true is false: the loop body whose guard is forced false\n(loop1, executing fewer iterations) would get the higher profile\ncount, and vice versa.\n\ngcc/ChangeLog:\n\n        * tree-ssa-loop-split.cc (split_loop): Pass edges to\n        fix_loop_bb_probability with consideration of initial_true,\n        so that loop1 is always scaled by the edge probability\n        corresponding to the branch that actually executes in it.\n\ngcc/testsuite/ChangeLog:\n\n        * gcc.dg/tree-prof/loop-split-4.c: New test.\n\nSigned-off-by: Xin Wang <wangxinw@hygon.cn>\n---\n gcc/testsuite/gcc.dg/tree-prof/loop-split-4.c | 34 +++++++++++++++++++\n gcc/tree-ssa-loop-split.cc                    | 13 ++++++-\n 2 files changed, 46 insertions(+), 1 deletion(-)\n create mode 100644 gcc/testsuite/gcc.dg/tree-prof/loop-split-4.c","diff":"diff --git a/gcc/testsuite/gcc.dg/tree-prof/loop-split-4.c b/gcc/testsuite/gcc.dg/tree-prof/loop-split-4.c\nnew file mode 100644\nindex 00000000000..7e0aa883276\n--- /dev/null\n+++ b/gcc/testsuite/gcc.dg/tree-prof/loop-split-4.c\n@@ -0,0 +1,34 @@\n+/* PR tree-optimization/XXXXX */\n+/* { dg-options \"-O2 -fdump-tree-lsplit-details\" } */\n+\n+volatile int sink;\n+\n+__attribute__((noinline)) int\n+helper (int a, int b)\n+{\n+  return a + b;\n+}\n+\n+int\n+main (void)\n+{\n+  int n = 100, t = 3, total = 0;\n+  /* With t=3, n=100 the guard \"t < i\" is false for i=0..3 (4 iterations,\n+     empty else) and true for i=4..99 (96 iterations, calls helper).\n+     split_at_bb_p swaps \"t < i\" to \"i > t\" (GT_EXPR), giving\n+     initial_true = false.  Loop1 (cold, 4 iterations) handles the false\n+     case, loop2 (hot, 96 iterations) handles the true case.\n+     Without the fix loop1 was scaled by true_edge->probability (96%),\n+     inverting the counts.  */\n+  for (int i = 0; i < n; i++)\n+    if (t < i)\n+      total += helper (i, t);\n+  sink = total;\n+  return 0;\n+}\n+/* { dg-final-use-not-autofdo { scan-tree-dump-times \"Loop split\" 1 \"lsplit\" } } */\n+/* { dg-final-use-not-autofdo { scan-tree-dump-times \"Invalid sum\" 0 \"lsplit\" } } */\n+/* With the fix loop1 (cold, 4 iterations) count ~4, loop2 (hot, 96\n+   iterations) count ~96.  Without the fix the counts are inverted.\n+   Check loop1 is a single-digit and loop2 is 90+.  */\n+/* { dg-final-use-not-autofdo { scan-tree-dump \"loop1 count \\[0-9\\], loop2 count 9\\[0-9\\]\" \"lsplit\" } } */\ndiff --git a/gcc/tree-ssa-loop-split.cc b/gcc/tree-ssa-loop-split.cc\nindex ba6cc45d7f0..8aa6275695b 100644\n--- a/gcc/tree-ssa-loop-split.cc\n+++ b/gcc/tree-ssa-loop-split.cc\n@@ -712,7 +712,18 @@ split_loop (class loop *loop1)\n \t\t(loop_preheader_edge (loop2)->src)->probability\n \t\t\t= loop1_prob.invert ();\n \n-\tfix_loop_bb_probability (loop1, loop2, true_edge, false_edge);\n+\tfix_loop_bb_probability (loop1, loop2,\n+\t\t\t\t initial_true ? true_edge : false_edge,\n+\t\t\t\t initial_true ? false_edge : true_edge);\n+\n+\tif (dump_file && (dump_flags & TDF_DETAILS))\n+\t  fprintf (dump_file,\n+\t\t   \";; Split loop: initial_true %s, \"\n+\t\t   \"loop1 count %\" PRId64 \", loop2 count %\" PRId64 \"\\n\",\n+\t\t   initial_true ? \"true\" : \"false\",\n+\t\t   (int64_t) loop1->header->count.to_gcov_type (),\n+\t\t   (int64_t) loop2->header->count.to_gcov_type ());\n+\n \t/* If conditional we split on has reliable profilea nd both\n \t   preconditionals of loop1 and loop2 are constant true, we can\n \t   only redistribute the iteration counts to the split loops.\n","prefixes":[]}