From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by sourceware.org (Postfix) with ESMTPS id D79E53858D20 for ; Sat, 18 May 2024 23:11:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D79E53858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=quicinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=quicinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D79E53858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.168.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716073921; cv=none; b=nqRvZCtKV1xMgCZORSVot3HJkO9qy8JF/qFR2p8u8aJf9M6H+4yrOkx0STzpG8405ltFDyF0jR2ZQAg1v5llxAmAu2PPe84MOneqXHQ0K46T9UnMyULWw8G3R4IIBl3CA/2lAUiBedXwHBbrZicEQ9/xO8eYAvtkGtpR1M4boUQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716073921; c=relaxed/simple; bh=LA0wQ+44eidnyucXvc+rnv47zxz/BGKeGQgMl+UMgtM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ZeMSGTfUKOLRzJ7TfSFzghf5xPmddiOD9Ge98CvOwOOojGI+OkrMVzoo3SoPWiUXNGQ6Atj7evsBkI6Vza8+YJh6+quDHtFRvNAIOH7hJZr8nOvyJyN4Ns8lDwjSNVlDrP1cVRPzmsfr+00iX90r0O4IQXjz3CLRpZK2RmxMfwI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279863.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 44IMeExQ017019 for ; Sat, 18 May 2024 23:11:57 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding:content-type; s=qcppdkim1; bh=McdaeId FDQ81Mcv2uoJF2lu0rrw0veUSEYxcRLE3kw4=; b=XHSYIBtyRVqwqV0Ab2KQIcm ast4XvW0ZP5/3vBxd/4ObOoOxKpM2Pfldgr68fZp7N1zX436Ygt9vaZlRdvcIa0T jye82Sc1RWI9N+cjqgb/kJz/ypvr2pelOvJJ5j1lvEspqFHVWyKzotsdd5CkuGlV Fvwiv+fU2M6G8AEIEKKZshzKUdJuEgOhPPKlxUdrTlQUQyqCGpqx8vK5yQYKKBNX IJezmpRrAfpGNHF6uGkdD8gWVLFQDDwt0ls2fscESBRNPJ0zVKdYtcxKVRyzBgW3 xvqZTlgvNi1M96e9EUqzsQ7OCf7yQi6JH2GaXiBlizctmNYb+IU8c2FFk0/GDhQ= = Received: from nasanppmta04.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3y6pr2gt5a-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Sat, 18 May 2024 23:11:57 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA04.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 44INBuJO016060 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Sat, 18 May 2024 23:11:56 GMT Received: from hu-apinski-lv.qualcomm.com (10.49.16.6) by nasanex01c.na.qualcomm.com (10.45.79.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Sat, 18 May 2024 16:11:56 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] PHIOPT: Don't transform minmax if middle bb contains a phi [PR115143] Date: Sat, 18 May 2024 16:11:46 -0700 Message-ID: <20240518231146.954808-1-quic_apinski@quicinc.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01a.na.qualcomm.com (10.47.209.196) To nasanex01c.na.qualcomm.com (10.45.79.139) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-ORIG-GUID: R9c5Pz-2c-fisXwBVsEl8bPkvQlx6SfA X-Proofpoint-GUID: R9c5Pz-2c-fisXwBVsEl8bPkvQlx6SfA X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.650,FMLib:17.11.176.26 definitions=2024-05-18_14,2024-05-17_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=922 bulkscore=0 malwarescore=0 spamscore=0 priorityscore=1501 impostorscore=0 clxscore=1015 lowpriorityscore=0 adultscore=0 suspectscore=0 mlxscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2405010000 definitions=main-2405180190 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,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: The problem here is even if last_and_only_stmt returns a statement, the bb might still contain a phi node which defines a ssa name which is used in that statement so we need to add a check to make sure that the phi nodes are empty for the middle bbs in both the `CMP?MINMAX:MINMAX` case and the `CMP?MINMAX:B` cases. OK for trunk and backport to all open branches since r14-3827-g30e6ee074588ba was backported? Bootstrapped and tested on x86_64_linux-gnu with no regressions. PR tree-optimization/115143 gcc/ChangeLog: * tree-ssa-phiopt.cc (minmax_replacement): Check for empty phi nodes for middle bbs for the case where middle bb is not empty. gcc/testsuite/ChangeLog: * gcc.c-torture/compile/pr115143-1.c: New test. * gcc.c-torture/compile/pr115143-2.c: New test. * gcc.c-torture/compile/pr115143-3.c: New test. Signed-off-by: Andrew Pinski --- .../gcc.c-torture/compile/pr115143-1.c | 21 +++++++++++++ .../gcc.c-torture/compile/pr115143-2.c | 30 +++++++++++++++++++ .../gcc.c-torture/compile/pr115143-3.c | 29 ++++++++++++++++++ gcc/tree-ssa-phiopt.cc | 12 ++++++++ 4 files changed, 92 insertions(+) create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr115143-1.c create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr115143-2.c create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr115143-3.c diff --git a/gcc/testsuite/gcc.c-torture/compile/pr115143-1.c b/gcc/testsuite/gcc.c-torture/compile/pr115143-1.c new file mode 100644 index 00000000000..5cb119ea432 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr115143-1.c @@ -0,0 +1,21 @@ +/* PR tree-optimization/115143 */ +/* This used to ICE. + minmax part of phiopt would transform, + would transform `a!=0?min(a, b) : 0` into `min(a,b)` + which was correct except b was defined by a phi in the inner + bb which was not handled. */ +short a, d; +char b; +long c; +unsigned long e, f; +void g(unsigned long h) { + if (c ? e : b) + if (e) + if (d) { + a = f ? ({ + unsigned long i = d ? f : 0, j = e ? h : 0; + i < j ? i : j; + }) : 0; + } +} + diff --git a/gcc/testsuite/gcc.c-torture/compile/pr115143-2.c b/gcc/testsuite/gcc.c-torture/compile/pr115143-2.c new file mode 100644 index 00000000000..05c3bbe9738 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr115143-2.c @@ -0,0 +1,30 @@ +/* { dg-options "-fgimple" } */ +/* PR tree-optimization/115143 */ +/* This used to ICE. + minmax part of phiopt would transform, + would transform `a!=0?min(a, b) : 0` into `min(a,b)` + which was correct except b was defined by a phi in the inner + bb which was not handled. */ +unsigned __GIMPLE (ssa,startwith("phiopt")) +foo (unsigned a, unsigned b) +{ + unsigned j; + unsigned _23; + unsigned _12; + + __BB(2): + if (a_6(D) != 0u) + goto __BB3; + else + goto __BB4; + + __BB(3): + j_10 = __PHI (__BB2: b_11(D)); + _23 = __MIN (a_6(D), j_10); + goto __BB4; + + __BB(4): + _12 = __PHI (__BB3: _23, __BB2: 0u); + return _12; + +} diff --git a/gcc/testsuite/gcc.c-torture/compile/pr115143-3.c b/gcc/testsuite/gcc.c-torture/compile/pr115143-3.c new file mode 100644 index 00000000000..53c5fb5588e --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr115143-3.c @@ -0,0 +1,29 @@ +/* { dg-options "-fgimple" } */ +/* PR tree-optimization/115143 */ +/* This used to ICE. + minmax part of phiopt would transform, + would transform `a!=0?min(a, b) : 0` into `min(a,b)` + which was correct except b was defined by a phi in the inner + bb which was not handled. */ +unsigned __GIMPLE (ssa,startwith("phiopt")) +foo (unsigned a, unsigned b) +{ + unsigned j; + unsigned _23; + unsigned _12; + + __BB(2): + if (a_6(D) > 0u) + goto __BB3; + else + goto __BB4; + + __BB(3): + j_10 = __PHI (__BB2: b_7(D)); + _23 = __MIN (a_6(D), j_10); + goto __BB4; + + __BB(4): + _12 = __PHI (__BB3: _23, __BB2: 0u); + return _12; +} diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc index f166c3132cb..918cf50b589 100644 --- a/gcc/tree-ssa-phiopt.cc +++ b/gcc/tree-ssa-phiopt.cc @@ -1925,6 +1925,10 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_ || gimple_code (assign) != GIMPLE_ASSIGN) return false; + /* There cannot be any phi nodes in the middle bb. */ + if (!gimple_seq_empty_p (phi_nodes (middle_bb))) + return false; + lhs = gimple_assign_lhs (assign); ass_code = gimple_assign_rhs_code (assign); if (ass_code != MAX_EXPR && ass_code != MIN_EXPR) @@ -1938,6 +1942,10 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_ || gimple_code (assign) != GIMPLE_ASSIGN) return false; + /* There cannot be any phi nodes in the alt middle bb. */ + if (!gimple_seq_empty_p (phi_nodes (alt_middle_bb))) + return false; + alt_lhs = gimple_assign_lhs (assign); if (ass_code != gimple_assign_rhs_code (assign)) return false; @@ -2049,6 +2057,10 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_ || gimple_code (assign) != GIMPLE_ASSIGN) return false; + /* There cannot be any phi nodes in the middle bb. */ + if (!gimple_seq_empty_p (phi_nodes (middle_bb))) + return false; + lhs = gimple_assign_lhs (assign); ass_code = gimple_assign_rhs_code (assign); if (ass_code != MAX_EXPR && ass_code != MIN_EXPR) -- 2.43.0