From patchwork Tue Aug 20 19:14:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1974589 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=quicinc.com header.i=@quicinc.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=j4+4/2CG; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WpK0f60D6z1yfg for ; Wed, 21 Aug 2024 05:14:57 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7735E387086A for ; Tue, 20 Aug 2024 19:14:54 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by sourceware.org (Postfix) with ESMTPS id 65A163858D34 for ; Tue, 20 Aug 2024 19:14:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 65A163858D34 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 65A163858D34 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724181270; cv=none; b=P/hJmulvwZIJtYigWaXzzuJxWMu1LlprM2TvvXtIqWdN5vKIDKee79efjh481DWeM1uzcI4CmaRoSt87XznqbdqiowdGxvjyOTKq5wblWygFmLoRC30p4hs/FfStITmfpgtVyxvD7LAHqD5pxiOavhQoBm5hYX8tM2KNZs41Rbo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724181270; c=relaxed/simple; bh=d83kf9srmw8gtzNvvWYDMen4shQtVkfbwtfcuyQF3iE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=YIYSwwaKP3A+j0sNutWsVCF5Suq8QiykG/68gwFxz9xIIjPt3Cn2ReL5wWy+UerlgSuycsfqrIF5HkiGOPILx67UN2ZzcY4YH+IRLFuNrbDeNBiOLpljKbptbedRPew+BnjCdgh4vT+Y8znnb19AFJjz1dLfEESM3nurKqpp9ZQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279873.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 47K9WcX0011258 for ; Tue, 20 Aug 2024 19:14:29 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:message-id :mime-version:subject:to; s=qcppdkim1; bh=PUQLr31ImZXqg5EWeZpi4c kYvepF+xZFDFKUzYsn9vA=; b=j4+4/2CGdhSb5urTVJG73linaqU38OrcPvSp3V qiL7QG+OUcbRRZloP2kyv/AkhM9AWpe+WBdWy/B46ohdUbD8ceDysGfg0dXDorCg 6D8TQCThQlTXtUaXar5HFL80M5AAz8iN//pVrPQ/H173PZqtxhD98xddOzw0cFkG YzsOuifztSUkuGPWJX16XU4X8j2Ao9YgTnNeaBH09oJ9gcZOZLJ7TMkn57afvI92 ibhFNi8yuIMs3MZaixPjOhl/SL9gnquVviDUrwHHab/NPZ5vGMGobxhMzxJYcuX5 Ve9sdTkvrSnHe4lVYz8Gu2gM3fep6RO6IMsLnQipgVWntjHw== Received: from nasanppmta05.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 414j572qdw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 20 Aug 2024 19:14:28 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA05.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 47KJERXB005480 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 20 Aug 2024 19:14:27 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; Tue, 20 Aug 2024 12:14:27 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH v2 1/2] phi-opt: Fix for failing maybe_push_res_to_seq in factor_out_conditional_operation [PR 116409] Date: Tue, 20 Aug 2024 12:14:16 -0700 Message-ID: <20240820191417.3585143-1-quic_apinski@quicinc.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01c.na.qualcomm.com (10.47.97.35) 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: ldh1DeKyW3KykoNKprGtJqCm8vl_zA8W X-Proofpoint-GUID: ldh1DeKyW3KykoNKprGtJqCm8vl_zA8W X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-08-20_15,2024-08-19_03,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=999 priorityscore=1501 clxscore=1015 bulkscore=0 phishscore=0 impostorscore=0 adultscore=0 suspectscore=0 mlxscore=0 spamscore=0 lowpriorityscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2407110000 definitions=main-2408200141 X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org The code was assuming that maybe_push_res_to_seq would not fail if the gimple_extract_op returned true. But for some cases when the function is pure rather than const, then it can fail. This change moves around the code to check the result of maybe_push_res_to_seq instead of assuming it will always work. Changes since v1: * v2: Instead of directly testing non-pure builtin functions change to test if maybe_push_res_to_seq fails. Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/116409 gcc/ChangeLog: * tree-ssa-phiopt.cc (factor_out_conditional_operation): Move maybe_push_res_to_seq before creating the phi node and the debug dump. Return false if maybe_push_res_to_seq fails. gcc/testsuite/ChangeLog: * gcc.dg/torture/pr116409-1.c: New test. * gcc.dg/torture/pr116409-2.c: New test. Signed-off-by: Andrew Pinski --- gcc/testsuite/gcc.dg/torture/pr116409-1.c | 7 ++++++ gcc/testsuite/gcc.dg/torture/pr116409-2.c | 7 ++++++ gcc/tree-ssa-phiopt.cc | 30 +++++++++++++++-------- 3 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr116409-1.c create mode 100644 gcc/testsuite/gcc.dg/torture/pr116409-2.c diff --git a/gcc/testsuite/gcc.dg/torture/pr116409-1.c b/gcc/testsuite/gcc.dg/torture/pr116409-1.c new file mode 100644 index 00000000000..7bf8d49c9a0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr116409-1.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ +/* { dg-additional-options "-frounding-math -fno-math-errno" } */ +double f(int c, double a, double b) { + if (c) + return __builtin_sqrt(a); + return __builtin_sqrt(b); +} diff --git a/gcc/testsuite/gcc.dg/torture/pr116409-2.c b/gcc/testsuite/gcc.dg/torture/pr116409-2.c new file mode 100644 index 00000000000..c27f11312d9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr116409-2.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ + +int f (int t, char *a, char *b) { + if (t) + return __builtin_strlen (a); + return __builtin_strlen (b); +} diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc index 2d4aba5b087..95bac330c8f 100644 --- a/gcc/tree-ssa-phiopt.cc +++ b/gcc/tree-ssa-phiopt.cc @@ -54,6 +54,7 @@ along with GCC; see the file COPYING3. If not see #include "dbgcnt.h" #include "tree-ssa-propagate.h" #include "tree-ssa-dce.h" +#include "calls.h" /* Return the singleton PHI in the SEQ of PHIs for edges E0 and E1. */ @@ -370,6 +371,25 @@ factor_out_conditional_operation (edge e0, edge e1, gphi *phi, /* Create a new PHI stmt. */ result = PHI_RESULT (phi); temp = make_ssa_name (TREE_TYPE (new_arg0), NULL); + + gimple_match_op new_op = arg0_op; + + /* Create the operation stmt if possible and insert it. */ + new_op.ops[0] = temp; + gimple_seq seq = NULL; + result = maybe_push_res_to_seq (&new_op, &seq, result); + + /* If we can't create the new statement, release the temp name + and return back. */ + if (!result) + { + release_ssa_name (temp); + return NULL; + } + + gsi = gsi_after_labels (gimple_bb (phi)); + gsi_insert_seq_before (&gsi, seq, GSI_CONTINUE_LINKING); + newphi = create_phi_node (temp, gimple_bb (phi)); if (dump_file && (dump_flags & TDF_DETAILS)) @@ -398,16 +418,6 @@ factor_out_conditional_operation (edge e0, edge e1, gphi *phi, add_phi_arg (newphi, new_arg0, e0, locus); add_phi_arg (newphi, new_arg1, e1, locus); - gimple_match_op new_op = arg0_op; - - /* Create the operation stmt and insert it. */ - new_op.ops[0] = temp; - gimple_seq seq = NULL; - result = maybe_push_res_to_seq (&new_op, &seq, result); - gcc_assert (result); - gsi = gsi_after_labels (gimple_bb (phi)); - gsi_insert_seq_before (&gsi, seq, GSI_CONTINUE_LINKING); - /* Remove the original PHI stmt. */ gsi = gsi_for_stmt (phi); gsi_remove (&gsi, true);