{"id":2222871,"url":"http://patchwork.ozlabs.org/api/1.1/patches/2222871/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/patch/20260413222006.3862501-6-andrew.pinski@oss.qualcomm.com/","project":{"id":17,"url":"http://patchwork.ozlabs.org/api/1.1/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},"msgid":"<20260413222006.3862501-6-andrew.pinski@oss.qualcomm.com>","date":"2026-04-13T22:20:05","name":"[GCC17-PATCH,5/6] phiprop: Allow for one store inbetween the load and the phi which is being used to insert [PR123120]","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"6ab0a731fa39bcc552f1ad9cb5f13ad95735e099","submitter":{"id":91428,"url":"http://patchwork.ozlabs.org/api/1.1/people/91428/?format=json","name":"Andrew Pinski","email":"andrew.pinski@oss.qualcomm.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/gcc/patch/20260413222006.3862501-6-andrew.pinski@oss.qualcomm.com/mbox/","series":[{"id":499758,"url":"http://patchwork.ozlabs.org/api/1.1/series/499758/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/list/?series=499758","date":"2026-04-13T22:20:03","name":"Improve Phiprop slightly","version":1,"mbox":"http://patchwork.ozlabs.org/series/499758/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2222871/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2222871/checks/","tags":{},"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=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256\n header.s=qcppdkim1 header.b=HBPfoHI+;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com\n header.a=rsa-sha256 header.s=google header.b=BP4NEmQW;\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=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256\n header.s=qcppdkim1 header.b=HBPfoHI+;\n\tdkim=pass (2048-bit key,\n unprotected) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com\n header.a=rsa-sha256 header.s=google header.b=BP4NEmQW","sourceware.org; dmarc=none (p=none dis=none)\n header.from=oss.qualcomm.com","sourceware.org;\n spf=pass smtp.mailfrom=oss.qualcomm.com","server2.sourceware.org;\n arc=none smtp.remote-ip=205.220.180.131"],"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 4fvhk548Gtz1y2d\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 14 Apr 2026 08:22:53 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id 84AFF4BA23C1\n\tfor <incoming@patchwork.ozlabs.org>; Mon, 13 Apr 2026 22:22:51 +0000 (GMT)","from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com\n [205.220.180.131])\n by sourceware.org (Postfix) with ESMTPS id 1532E4BA2E13\n for <gcc-patches@gcc.gnu.org>; Mon, 13 Apr 2026 22:20:19 +0000 (GMT)","from pps.filterd (m0279873.ppops.net [127.0.0.1])\n by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id\n 63DLB1YM2509966\n for <gcc-patches@gcc.gnu.org>; Mon, 13 Apr 2026 22:20:18 GMT","from mail-pg1-f197.google.com (mail-pg1-f197.google.com\n [209.85.215.197])\n by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4dh867r5bt-1\n (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT)\n for <gcc-patches@gcc.gnu.org>; Mon, 13 Apr 2026 22:20:18 +0000 (GMT)","by mail-pg1-f197.google.com with SMTP id\n 41be03b00d2f7-c76c2bb3149so2599920a12.1\n for <gcc-patches@gcc.gnu.org>; Mon, 13 Apr 2026 15:20:18 -0700 (PDT)","from xeond2.wrightpinski.org ([98.97.32.212])\n by smtp.gmail.com with ESMTPSA id\n 41be03b00d2f7-c79219f55c4sm12129600a12.22.2026.04.13.15.20.15\n (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n Mon, 13 Apr 2026 15:20:15 -0700 (PDT)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org 84AFF4BA23C1","OpenDKIM Filter v2.11.0 sourceware.org 1532E4BA2E13"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org 1532E4BA2E13","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org 1532E4BA2E13","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776118819; cv=none;\n b=gSVK9svEyqGEVQ3xNVzMblBEHq2pUiGdX5La8NEA2GhkXWEi1s3x6C+jT0W/F2C4Ea4nUhRVAMfu5TCt0eQBHliz1kRd63wy8pzmbM4R/YHhz910oFOTD19xOSr9fSW6J+FrXrxRdEsfzqAdvJ2pXTJyFb+5FmTbC8a8fIt+poM=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1776118819; c=relaxed/simple;\n bh=VYeu+kDIuMgCfZqFCCzGPfzCuB+eZ0rqexRsbIQMs2E=;\n h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID:\n MIME-Version;\n b=GEJPWGQ1Kxq4yU2/FFqWdP7Umtq6Wp+M6mpLvMqOjWGprMjhEJOPFw9NRJD0TGaMEmcol+8tqHuMXbpFe8jgu4brSNZwqGlf3o6Yg+zTozmZH97LmSTRyRkHwbVqRyRWlRzbnNy3FjAB3K45z6IW/P0BEIEGQaWlk0w8tgP0d2s=","ARC-Authentication-Results":"i=1; server2.sourceware.org","DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h=\n cc:content-transfer-encoding:date:from:in-reply-to:message-id\n :mime-version:references:subject:to; s=qcppdkim1; bh=gPHNwhec4M6\n wqp9oxSN6NLHNBtD4wHv3R70lK76dpSA=; b=HBPfoHI+Bd6lUmon0rNC0Bzw4nN\n zyuvMldPGyZLW3c+4ms4C4mG1RzpN3MFHVgfynX8baZyhuoNC/oc1OmRSlc2AeXj\n ZMJUPXfGTayBsdL4h1hJ7v2yjWBghLE7xmO/7DnCaKEBZgfW0OTWz6sSZx/pBsVo\n AtAUMQxk43+kyCvrqpPjf6xWe4zZ0EiHDbSSvCiS9ZzW6fjfpqjH8dJPo1BK27wx\n t88HyuTyrlygFTJ46kvWXV8G15kH53+3PXJnHkUM+ybxkAN6N2MF/xasc/nDJCqB\n unfvmL6hp45JGypCPXPD93cHnRd9JjfvePdWXJ7IYlYgItR0E4lXTRi/zpQ==","v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=oss.qualcomm.com; s=google; t=1776118817; x=1776723617; darn=gcc.gnu.org;\n h=content-transfer-encoding:mime-version:references:in-reply-to\n :message-id:date:subject:cc:to:from:from:to:cc:subject:date\n :message-id:reply-to;\n bh=gPHNwhec4M6wqp9oxSN6NLHNBtD4wHv3R70lK76dpSA=;\n b=BP4NEmQWI6gSzcijgaGOEvwEeLtKmT1ZboxUJBWRqjOuTe842dxr8z7gvHhZtTYZkW\n b51PJoy625R10AMTOdSZCnYJHbpdmzM0d3i1dLkPRWfex0qVNGGOE77DTcNAG+zl4v4X\n doiIKsUtXa1QZRjCg974Au9O++BnKdrslng5MoHbOr6CZErnNxnAwHdio05EVrKa60HV\n f1AB8IGa3PPkteKwotmF6JOeMeCJnU1brWo8znD77lAziTfJBqNYE+b9rKuqxKZsktYl\n f1bYFcEmIAcOoQpMQ+BS8aWYlm8Ww0Fggd8CKyhdDDjwozYrR/Q2tsbDDsXZF7ULXDb+\n tHSA=="],"X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1776118817; x=1776723617;\n h=content-transfer-encoding:mime-version:references:in-reply-to\n :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from\n :to:cc:subject:date:message-id:reply-to;\n bh=gPHNwhec4M6wqp9oxSN6NLHNBtD4wHv3R70lK76dpSA=;\n b=kFNGpo6qsJRt3sRp3QSQfLrYbePA9AQq5OgxvPQL4KHvuRk2MfGOQEZMMhOT97jOmq\n G+h6zewuqZmm40kKkgC40dNrecb4JYj8ZZYBOo5posTMElj11Ac+lSkZUYAFj1Pc9ITr\n hzxaSeK4Q4t6GjeMEHVJjglq6jYJWCXRgg8VPTZTSncGT9o+HLWCK6/bEu/a2sx7jJgY\n jGXlHJ2zi8grAWXLk6SM8MyQ28fJE56r0BOHoMTRGz3TVulViIC0zCWJlfhjK1wINpun\n uWLbJSTpm1EgoNUQjaIUHaK3EyKeBpDpkawEn8LAuPaTTsBNPYLDzo5ZFltlJquU9Z1B\n xyJw==","X-Gm-Message-State":"AOJu0YxKb3ffVbvA59Sfvc6b7DeqZxuQKBegaXk8y4/wGD2vbTFuGkTt\n 3CQOGH8AJ7vLw7nnctAZFkjzodeuOZ0dN8XVLQKmZSd7qge1RYhY8i9kc83nb3FQJy5Ab4ZGwnB\n 3umrVv/HNTpyju5bPbLLc2U5e9ebfvlp9MSq7tuZyZUdU/KiwaxFH2Gf6MY/7mgMUWjY7","X-Gm-Gg":"AeBDietrFVzTiFDfW3P7lPTdFoo/hEeged4R7iXthBD4XVLb/JwKS097yjLfpo0T5Db\n pvWX5fl+Vha+ZzH7HGKZ3h2dmif4Q/Hmkam8G1Kc6IlEnaO1IWuTRsKbgqdMTQMBxQFEEa4ywWF\n rTpl7yY25nMsm6boGnmfTM0KhMmNYQwWF31LtMhqOha6XV9QPScGH45Spk4jIWSwQFcI7uOl7JY\n UUmWNp8FuSw4ma6boFZcqlrP2Vk890b9+az8Zh5wLTmEOTQ8U2y9SqJYlST5rCc3F2nYX0njOi1\n jKwtgkei88matJjESc9UdyqAzwLeOu/J8eWlhbjHPyyouKdeZtf3fWHAzFjbfaFZfK2EUcCLtmA\n cwqM6khAPe9woRB6e5+BFMg1bEN7Kv0LS0HBKKGAflC1waWYV","X-Received":["by 2002:a05:6a21:328c:b0:39f:47f1:87be with SMTP id\n adf61e73a8af0-39fc9286b23mr18503205637.1.1776118816753;\n Mon, 13 Apr 2026 15:20:16 -0700 (PDT)","by 2002:a05:6a21:328c:b0:39f:47f1:87be with SMTP id\n adf61e73a8af0-39fc9286b23mr18503182637.1.1776118816054;\n Mon, 13 Apr 2026 15:20:16 -0700 (PDT)"],"From":"Andrew Pinski <andrew.pinski@oss.qualcomm.com>","To":"gcc-patches@gcc.gnu.org","Cc":"Andrew Pinski <andrew.pinski@oss.qualcomm.com>","Subject":"[GCC17-PATCH 5/6] phiprop: Allow for one store inbetween the load and\n the phi which is being used to insert [PR123120]","Date":"Mon, 13 Apr 2026 15:20:05 -0700","Message-ID":"<20260413222006.3862501-6-andrew.pinski@oss.qualcomm.com>","X-Mailer":"git-send-email 2.43.0","In-Reply-To":"<20260413222006.3862501-1-andrew.pinski@oss.qualcomm.com>","References":"<20260413222006.3862501-1-andrew.pinski@oss.qualcomm.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","X-Proofpoint-ORIG-GUID":"ch7yC3q1boiEa1b8sH7-8j1K51DvjCQ8","X-Proofpoint-GUID":"ch7yC3q1boiEa1b8sH7-8j1K51DvjCQ8","X-Proofpoint-Spam-Details-Enc":"AW1haW4tMjYwNDEzMDIyMCBTYWx0ZWRfX529jk4sgsVbD\n TEc8Fk80YZw0FwsT0NLlR3BYGiffCzO9lUdkIOewX1ewoKtoH+16vbBc+ppTXY7OMGhQBUq3tMP\n i5h/ilD6q0zTfVGz7EuY18W506dRUQDXK5DnwB7FVXy0oz/1Snbq8hBYERY4ClTES5i0WnNkQjE\n RI/IWxwTqsSHUI2fL2gMnto+8XHqrPCTzAvBzQU7xrFrvISN5dqJ32PwAbx+N9IrMQMa+sSuhK0\n 07C7rtoZq94KfY4n8Gkv1x0uaSTQ64I1CBJS7nT2Mxo0LcEG8JXHzkR3PjJ4OPTz6gfeTi4jvAx\n tYEx/zj36I/zUz8zud/yLn3/0owyIFo5ScXBOlBh7wgTh1A/ZRY/CNwYOpum2TZeWrCsjUHHDen\n nzI8YYXCYp3g8uFa4tML9z+dEXSpyyVSfzWXNqkMR3oAQHcOQFrgro/5hOo5m8eDwQ1UUuUMVgC\n yfO9LQxfIxNQgDrGUzg==","X-Authority-Analysis":"v=2.4 cv=etzvCIpX c=1 sm=1 tr=0 ts=69dd6c22 cx=c_pps\n a=rz3CxIlbcmazkYymdCej/Q==:117 a=i7c1TCcCr6Kt3kgLFmYdbA==:17\n a=IvqslEMCfeOTk3xp:21 a=A5OVakUREuEA:10 a=s4-Qcg_JpJYA:10\n a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=rJkE3RaqiGZ5pbrm-msn:22\n a=EUspDBNiAAAA:8 a=kApbupQWTbeeXeoJebsA:9 a=bFCP_H2QrGi7Okbo017w:22","X-Proofpoint-Virus-Version":"vendor=baseguard\n engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49\n definitions=2026-04-13_03,2026-04-13_04,2025-10-01_01","X-Proofpoint-Spam-Details":"rule=outbound_notspam policy=outbound score=0\n malwarescore=0 phishscore=0 lowpriorityscore=0 adultscore=0 impostorscore=0\n priorityscore=1501 bulkscore=0 suspectscore=0 spamscore=0 clxscore=1015\n classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0\n reason=mlx scancount=1 engine=8.22.0-2604070000 definitions=main-2604130220","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":"So phiprop has one disadvantage is that if there is store between the\nphi with the addresses and the new load, phiprop will no do anything.\nThis means for some C++ code where you have a min of a max (or the opposite),\ndepending on the argument order of evaluation phiprop might do\nthe transformation or it might not (see tree-ssa/phiprop-3.C for examples).\nSo we need to allow skipping of one store inbetween the load and\nwhere the phi is located.\n\nAggregates include a store when doing phiprop so we need to check\nif there are also loads between the original store/load and the\nstore we are skipping. This can be added afterwards but I didn't\nsee aggregate case happening enough to make a big dent. I added\ntestcases (phiprop-{10,11}.c) to make sure cases where the load\nwould make a different shows up though.\n\n\tPR tree-optimization/123120\n\tPR tree-optimization/116823\ngcc/ChangeLog:\n\n\t* tree-ssa-phiprop.cc (phiprop_insert_phi): Add other_vuse\n\targument, use it instead of the vuse on the use_stmt.\n\t(can_handle_load): Add aggregate argument. Also return the vuse\n\tof the load/store when the insert is allowed.\n\tSkipping over one non-modifying store for !aggregate.\n\t(propagate_with_phi): Update call to can_handle_load\n\tand phiprop_insert_phi.\n\ngcc/testsuite/ChangeLog:\n\n\t* gcc.dg/tree-ssa/phiprop-8.c: New test.\n\t* gcc.dg/tree-ssa/phiprop-9.c: New test.\n\t* gcc.dg/tree-ssa/phiprop-10.c: New test.\n\t* gcc.dg/tree-ssa/phiprop-11.c: New test.\n\t* gcc.dg/tree-ssa/phiprop-12.c: New test.\n\t* g++.dg/tree-ssa/phiprop-3.C: New test.\n\nSigned-off-by: Andrew Pinski <andrew.pinski@oss.qualcomm.com>\n---\n gcc/testsuite/g++.dg/tree-ssa/phiprop-3.C  |  23 +++++\n gcc/testsuite/gcc.dg/tree-ssa/phiprop-10.c |  30 ++++++\n gcc/testsuite/gcc.dg/tree-ssa/phiprop-11.c |  30 ++++++\n gcc/testsuite/gcc.dg/tree-ssa/phiprop-12.c |  30 ++++++\n gcc/testsuite/gcc.dg/tree-ssa/phiprop-8.c  |  27 ++++++\n gcc/testsuite/gcc.dg/tree-ssa/phiprop-9.c  |  24 +++++\n gcc/tree-ssa-phiprop.cc                    | 103 ++++++++++++++++-----\n 7 files changed, 246 insertions(+), 21 deletions(-)\n create mode 100644 gcc/testsuite/g++.dg/tree-ssa/phiprop-3.C\n create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phiprop-10.c\n create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phiprop-11.c\n create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phiprop-12.c\n create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phiprop-8.c\n create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phiprop-9.c","diff":"diff --git a/gcc/testsuite/g++.dg/tree-ssa/phiprop-3.C b/gcc/testsuite/g++.dg/tree-ssa/phiprop-3.C\nnew file mode 100644\nindex 00000000000..86ecb02b81b\n--- /dev/null\n+++ b/gcc/testsuite/g++.dg/tree-ssa/phiprop-3.C\n@@ -0,0 +1,23 @@\n+/* { dg-do compile } */\n+/* { dg-options \"-O1 -fdump-tree-phiprop1-details -fdump-tree-release_ssa\" } */\n+\n+/* PR tree-optimization/123120 */\n+/* The order of min/max arguments should not cause any issues with phiprop,\n+   the stores to the temps should not interfere with the loads from the temp.  */\n+\n+#include <algorithm>\n+\n+long f0(short c, short d, long long a)\n+{\n+   return std::max(std::min((long long)3, a), (long long)d);\n+}\n+\n+long f1(short c, short d, long long a)\n+{\n+   return std::max((long long)d, std::min((long long)3, a));\n+}\n+\n+/* { dg-final { scan-tree-dump-times \"Inserting PHI for result of load\" 4 \"phiprop1\"} } */\n+/* { dg-final { scan-tree-dump-times \"MIN_EXPR\" 2 \"release_ssa\"} } */\n+/* { dg-final { scan-tree-dump-times \"MAX_EXPR\" 2 \"release_ssa\"} } */\n+\ndiff --git a/gcc/testsuite/gcc.dg/tree-ssa/phiprop-10.c b/gcc/testsuite/gcc.dg/tree-ssa/phiprop-10.c\nnew file mode 100644\nindex 00000000000..b630148faa5\n--- /dev/null\n+++ b/gcc/testsuite/gcc.dg/tree-ssa/phiprop-10.c\n@@ -0,0 +1,30 @@\n+/* { dg-do compile } */\n+/* { dg-options \"-O2 -fno-tree-sra -fdump-tree-phiprop-details\" } */\n+/* PR tree-optimization/123120 */\n+/* Make sure the store to *e conflicts with the store to *d */\n+\n+\n+struct s1\n+{\n+  int a;\n+};\n+\n+void f(int *);\n+\n+void g(struct s1 i, struct s1 *d, struct s1 *e)\n+{\n+  const struct s1 t = {10};\n+  const struct s1 *a;\n+  struct s1 t1 = {2};\n+  if (t.a < i.a)\n+    a = &t;\n+  else\n+    a = &i;\n+  *e = t1;\n+  t1 = *a;\n+  *d = t1;\n+}\n+\n+/* { dg-final { scan-tree-dump-not \"Inserting PHI for result of load\" \"phiprop1\"} } */\n+/* { dg-final { scan-tree-dump-not \"Inserting PHI for result of load\" \"phiprop2\"} } */\n+\ndiff --git a/gcc/testsuite/gcc.dg/tree-ssa/phiprop-11.c b/gcc/testsuite/gcc.dg/tree-ssa/phiprop-11.c\nnew file mode 100644\nindex 00000000000..6f078d175ef\n--- /dev/null\n+++ b/gcc/testsuite/gcc.dg/tree-ssa/phiprop-11.c\n@@ -0,0 +1,30 @@\n+/* { dg-do compile } */\n+/* { dg-options \"-O2 -fno-tree-sra -fdump-tree-phiprop-details\" } */\n+/* PR tree-optimization/123120 */\n+/* Make sure the load *d conflicts with the store to *d so that phiprop\n+   does not move the store into conditionals. */\n+\n+struct s1\n+{\n+  int a;\n+};\n+\n+void f(int *);\n+\n+int g(struct s1 i, struct s1 *d, struct s1 *e)\n+{\n+  const struct s1 t = {10};\n+  const struct s1 *a;\n+  struct s1 t1 = {2};\n+  if (t.a < i.a)\n+    a = &t;\n+  else\n+    a = &i;\n+  t1 = *d;\n+  *d = *a;\n+  return t1.a;\n+}\n+\n+/* { dg-final { scan-tree-dump-not \"Inserting PHI for result of load\" \"phiprop1\"} } */\n+/* { dg-final { scan-tree-dump-not \"Inserting PHI for result of load\" \"phiprop2\"} } */\n+\ndiff --git a/gcc/testsuite/gcc.dg/tree-ssa/phiprop-12.c b/gcc/testsuite/gcc.dg/tree-ssa/phiprop-12.c\nnew file mode 100644\nindex 00000000000..4cc2e5b8cd0\n--- /dev/null\n+++ b/gcc/testsuite/gcc.dg/tree-ssa/phiprop-12.c\n@@ -0,0 +1,30 @@\n+/* { dg-do compile } */\n+/* { dg-options \"-O2 -fdump-tree-phiprop-details\" } */\n+/* PR tree-optimization/123120 */\n+/* Make sure the load *d conflicts with the store to *d so that phiprop\n+   does not move the store into conditionals. */\n+\n+struct s1\n+{\n+  int a;\n+};\n+\n+void f(int *);\n+\n+int g(struct s1 i, struct s1 *d, struct s1 *e)\n+{\n+  const struct s1 t = {10};\n+  const struct s1 *a;\n+  struct s1 t1 = {2};\n+  if (t.a < i.a)\n+    a = &t;\n+  else\n+    a = &i;\n+  int t2 = d->a;\n+  *d = *a;\n+  return t2;\n+}\n+\n+/* { dg-final { scan-tree-dump-not \"Inserting PHI for result of load\" \"phiprop1\"} } */\n+/* { dg-final { scan-tree-dump-not \"Inserting PHI for result of load\" \"phiprop2\"} } */\n+\ndiff --git a/gcc/testsuite/gcc.dg/tree-ssa/phiprop-8.c b/gcc/testsuite/gcc.dg/tree-ssa/phiprop-8.c\nnew file mode 100644\nindex 00000000000..546031e63d7\n--- /dev/null\n+++ b/gcc/testsuite/gcc.dg/tree-ssa/phiprop-8.c\n@@ -0,0 +1,27 @@\n+/* { dg-do compile } */\n+/* { dg-options \"-O1 -fdump-tree-phiprop1-details -fdump-tree-release_ssa\" } */\n+\n+/* PR tree-optimization/116823 */\n+/* The clobber on b should not get in the way of phiprop here. */\n+/* We should have MIN_EXPR early on. */\n+\n+void f(int *);\n+\n+int g(int i)\n+{\n+  const int t = 10;\n+  const int *a;\n+  {\n+    int b;\n+    f(&b);\n+    if (t < i)\n+      a = &t;\n+    else\n+      a = &i;\n+  }\n+  return *a;\n+}\n+\n+/* { dg-final { scan-tree-dump-times \"Inserting PHI for result of load\" 1 \"phiprop1\"} } */\n+/* { dg-final { scan-tree-dump-times \"MIN_EXPR\" 1 \"release_ssa\"} } */\n+\ndiff --git a/gcc/testsuite/gcc.dg/tree-ssa/phiprop-9.c b/gcc/testsuite/gcc.dg/tree-ssa/phiprop-9.c\nnew file mode 100644\nindex 00000000000..bbd91eda95d\n--- /dev/null\n+++ b/gcc/testsuite/gcc.dg/tree-ssa/phiprop-9.c\n@@ -0,0 +1,24 @@\n+/* { dg-do compile } */\n+/* { dg-options \"-O1 -fdump-tree-phiprop1-details -fdump-tree-release_ssa\" } */\n+\n+/* PR tree-optimization/123120 */\n+/* The store to *d should not get in the way of phiprop here. */\n+/* We should have MIN_EXPR early on. */\n+\n+void f(int *);\n+\n+int g(int i, int *d)\n+{\n+  const int t = 10;\n+  const int *a;\n+  if (t < i)\n+    a = &t;\n+  else\n+    a = &i;\n+  *d = 1;\n+  return *a;\n+}\n+\n+/* { dg-final { scan-tree-dump-times \"Inserting PHI for result of load\" 1 \"phiprop1\"} } */\n+/* { dg-final { scan-tree-dump-times \"MIN_EXPR\" 1 \"release_ssa\"} } */\n+\ndiff --git a/gcc/tree-ssa-phiprop.cc b/gcc/tree-ssa-phiprop.cc\nindex 3b00dbf661f..bfafb8fddbd 100644\n--- a/gcc/tree-ssa-phiprop.cc\n+++ b/gcc/tree-ssa-phiprop.cc\n@@ -101,12 +101,14 @@ struct phiprop_d\n };\n \n /* Insert a new phi node for the dereference of PHI at basic_block\n-   BB with the virtual operands from USE_STMT.  */\n+   BB with the virtual operands from USE_STMT. The vuse for\n+   the load will be set to OTHER_VUSE unless there is virtual op\n+   phi for BB.  */\n \n static tree\n phiprop_insert_phi (basic_block bb, gphi *phi, gimple *use_stmt,\n \t\t    struct phiprop_d *phivn, size_t n,\n-\t\t    bitmap dce_ssa_names)\n+\t\t    bitmap dce_ssa_names, tree other_vuse)\n {\n   tree res;\n   gphi *new_phi = NULL;\n@@ -176,7 +178,7 @@ phiprop_insert_phi (basic_block bb, gphi *phi, gimple *use_stmt,\n \t      if (vphi)\n \t\tvuse = PHI_ARG_DEF_FROM_EDGE (vphi, e);\n \t      else\n-\t\tvuse = gimple_vuse (use_stmt);\n+\t\tvuse = other_vuse;\n \t    }\n \t  else\n \t    /* For the aggregate copy case updating virtual operands\n@@ -242,26 +244,81 @@ chk_uses (tree, tree *idx, void *data)\n    incoming VUSE (up_vuse).  If neither is present\n    make sure the def stmt of the virtual use is in a\n    different basic block dominating BB.  When the def\n-   is an edge-inserted one we know it dominates us.  */\n-static bool\n-can_handle_load (gimple *load_stmt, basic_block bb,\n-\t\t gphi *vphi, tree up_vuse)\n+   is an edge-inserted one we know it dominates us.\n+   Returns the vuse to use for the inserting.  NULL_TREE\n+   is returned when we can't do the insert.  */\n+\n+static tree\n+can_handle_load (gimple *load_stmt,\n+\t\t basic_block bb,\n+\t\t gphi *vphi, tree up_vuse, bool aggregate)\n {\n   tree vuse = gimple_vuse (load_stmt);\n+  /* If there is a vphi, use that as the end point for the stores.  */\n   if (vphi)\n-    return vuse == gimple_phi_result (vphi);\n+    {\n+      tree vphi_res = gimple_phi_result (vphi);\n+      /* If the vuse on the load is the same as the vphi,\n+\t there are no stores inbetween.  */\n+      if (vuse == vphi_res)\n+\treturn vuse;\n+      /* For aggregates, skipping the store is too\n+\t hard to handle as you need to check for loads\n+\t and it is not worth the check. */\n+      if (aggregate)\n+\treturn NULL_TREE;\n+      /* Try to see if the store does not effect the load.  */\n+      gimple *other_store = SSA_NAME_DEF_STMT (vuse);\n+      if (is_a<gphi*> (other_store))\n+\treturn NULL_TREE;\n+      /* There is only one store between the phi and the load. */\n+      if (gimple_vdef (other_store) != vphi_res)\n+\treturn NULL_TREE;\n+      tree src = gimple_assign_rhs1 (load_stmt);\n+      ao_ref read;\n+      ao_ref_init (&read, src);\n+      if (stmt_may_clobber_ref_p_1 (other_store, &read, false))\n+\treturn NULL_TREE;\n+      return vphi_res;\n+    }\n+  /* If there is a common incoming vuse then that must be checked first.  */\n   if (up_vuse)\n-    return vuse == up_vuse;\n-  gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);\n+    return vuse == up_vuse ? vuse : NULL_TREE;\n   /* If the load does not have a store beforehand,\n      then we can do the load in conditional. */\n   if (SSA_NAME_IS_DEFAULT_DEF (vuse))\n-    return true;\n-  if (gimple_bb (def_stmt) != bb\n-      && !dominated_by_p (CDI_DOMINATORS,\n-\t\t\t  bb, gimple_bb (def_stmt)))\n-    return true;\n-  return false;\n+    return vuse;\n+  gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);\n+\n+  /* If the store is in the same bb as the phi,\n+     see if skipping one store is possible.  */\n+  if (gimple_bb (def_stmt) == bb)\n+    {\n+      /* For aggregates, skipping the store is too\n+\t hard to handle as you need to check for loads\n+\t and it is not worth the check. */\n+      if (aggregate)\n+\treturn NULL_TREE;\n+      tree store_vuse = gimple_vuse (def_stmt);\n+      gimple *other = SSA_NAME_DEF_STMT (store_vuse);\n+      /* Only handle one store in the basic block between\n+\t the load and the phi. */\n+      if (!SSA_NAME_IS_DEFAULT_DEF (store_vuse)\n+\t  && gimple_bb (other) == bb)\n+\treturn NULL_TREE;\n+      tree src = gimple_assign_rhs1 (load_stmt);\n+      ao_ref read;\n+      ao_ref_init (&read, src);\n+      if (stmt_may_clobber_ref_p_1 (def_stmt, &read, false))\n+\treturn NULL_TREE;\n+      return store_vuse;\n+    }\n+\n+  if (dominated_by_p (CDI_DOMINATORS,\n+\t\t      bb, gimple_bb (def_stmt)))\n+    return vuse;\n+\n+  return NULL_TREE;\n }\n \n /* Propagate between the phi node arguments of PHI in BB and phi result\n@@ -384,13 +441,15 @@ propagate_with_phi (basic_block bb, gphi *vphi, gphi *phi,\n \t    && !gimple_has_volatile_ops (use_stmt)))\n \tcontinue;\n \n-      if (!can_handle_load (use_stmt, bb, vphi, up_vuse))\n-\tcontinue;\n-\n       bool aggregate = false;\n       if (!is_gimple_reg_type (TREE_TYPE (gimple_assign_lhs (use_stmt))))\n \taggregate = true;\n \n+      tree other_vuse;\n+      other_vuse = can_handle_load (use_stmt, bb, vphi, up_vuse, aggregate);\n+      if (!other_vuse)\n+\tcontinue;\n+\n       if ((canpossible_trap || aggregate)\n \t  && !dom_info_available_p (cfun, CDI_POST_DOMINATORS))\n \tcalculate_dominance_info (CDI_POST_DOMINATORS);\n@@ -458,7 +517,8 @@ propagate_with_phi (basic_block bb, gphi *vphi, gphi *phi,\n \t\tgoto next;\n \t    }\n \n-\t  phiprop_insert_phi (bb, phi, use_stmt, phivn, n, dce_ssa_names);\n+\t  phiprop_insert_phi (bb, phi, use_stmt, phivn, n,\n+\t\t\t      dce_ssa_names, other_vuse);\n \n \t  /* Remove old stmt. The phi and all of maybe its depedencies\n \t     will be removed later via simple_dce_from_worklist. */\n@@ -491,7 +551,8 @@ propagate_with_phi (basic_block bb, gphi *vphi, gphi *phi,\n       else\n \t{\n \t  tree vuse = gimple_vuse (use_stmt);\n-\t  res = phiprop_insert_phi (bb, phi, use_stmt, phivn, n, dce_ssa_names);\n+\t  res = phiprop_insert_phi (bb, phi, use_stmt, phivn, n,\n+\t\t\t\t    dce_ssa_names, other_vuse);\n \t  type = TREE_TYPE (res);\n \n \t  /* Remember the value we created for *ptr.  */\n","prefixes":["GCC17-PATCH","5/6"]}