From patchwork Sat Dec 23 18:35:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Andrew Pinski (QUIC)" X-Patchwork-Id: 183001 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2483:b0:fb:cd0c:d3e with SMTP id q3csp1815633dyi; Sat, 23 Dec 2023 10:35:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IEwfUzZ9L1tQcacdYEwA5/pLCIGIqPlmby3NqvV0vAJCCfGWdwlq+9TUG20Ob7+JqVQ7S2s X-Received: by 2002:a05:6214:2488:b0:67f:3f0:c1d2 with SMTP id gi8-20020a056214248800b0067f03f0c1d2mr4759047qvb.22.1703356555668; Sat, 23 Dec 2023 10:35:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1703356555; cv=pass; d=google.com; s=arc-20160816; b=rjD7um/ZbkDr+pBe/UrGcqnrUZ8HFzt5Advcps2ZYUVladWCV8Uw9kBYTSuQuqBKlz cWYsB52slkxTsa22XV2PeojFDP2OeQQEf88S4E5yPEB/noYs24M/ttL3TXDFDPns0aEN Fom6Tw4t9PQs+2xsy9K6WBw6QU+jNNHUH9JWoYgW6Am38zm96N58dengP71jmPpdtLqc 0xE7a29RpmKDhraVd6egGrf+clXroSi2fE7HEimGXZhKHn8qFBSUmr5POo9JMM6/nRqs rSpxQW+eqSXQGl1O7Sg3AIpsYgzTnXBtvr4Pp8Z/j5Cs64PRR0DV4vUdZ8m4hr6dMWVs uxKQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=m43cUrlrdr2Xu4avJBNvYPLwf/y0n7ggVqh3MR263uM=; fh=o3CEdS3yGN/eIa89Zn0Kf01WYLxTE9IjKuSQGVPeOHI=; b=rVSwh4qnr/HAREKwVGG+oiorjNmxpF08nANFrvBXaCx79yCxK3dI8VOdC6ebwlfRgM dzW7yZ71AGecGhRcVQFNFS0qL8XpZ4qx7AV1DAn2hrxr0ohdh/mXMCM070psloNbN8hf XUmj6h+vOtf/Ix9ue3gwK7bYMPKQqEnVf0n0ESukQ0TrngQ3XWjQunfqZ0vFs9IFiA+o P6OpiylSPxFwJgUuOZzNPHVgnf0RgkQ4pkvw72T1aWudNmlfkGsPrkFU/OFMZLcYoVAX EfnLg7W4xdqAfakq20UXODSm4C/b1yB1VlkJIvChfZsj3nWZZ/gF1ElueGi54/bhZ2/p gxcg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=IFHwYkKx; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id m10-20020a0cf18a000000b0067eb145cf98si7107483qvl.388.2023.12.23.10.35.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Dec 2023 10:35:55 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=IFHwYkKx; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5F821385828E for ; Sat, 23 Dec 2023 18:35:55 +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 C52CC3858D33 for ; Sat, 23 Dec 2023 18:35:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C52CC3858D33 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 C52CC3858D33 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=1703356532; cv=none; b=AmqUoKExSH+Qk0I1O0ixV0ul4yQpDjJbIZFFwekgf9gJ+xW7bG4XAdHNz4Q+wbQC8uBA8HwCOiasLZ+84hVfij8MJzYZWfBeL19FymsM0Cwh9csC3tFSpM0y50ETRUNhg1rrcDgLXslkbscjnP9IsOJljH3Y68TTAIrD4Jrjk2I= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703356532; c=relaxed/simple; bh=MtGnlT8eGbC/zo+E7wxIBoqV1C9dSUd+H3AdHMI93pA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=A3buMUeyBY39V0QxRtwZZv2zwfDSZH17XW4P+LCMNMLE4wr3piwEtUc76wmEn4OsWxOEkibYUHLx90HokDypki+SM/YGuXHd6tKELxqnZEf8xfv1hPITqBraQhUlnGknNmTGsoCne2JfeYshh6cz8t94B8DRPtqxnh1BBujtwDM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279868.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 3BNI0gfj016957 for ; Sat, 23 Dec 2023 18:35:29 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=m43cUrl rdr2Xu4avJBNvYPLwf/y0n7ggVqh3MR263uM=; b=IFHwYkKxyv8zb3PM1PQwHLu 47o+AjnJ8vLLn+y4W+FXHSVtjOmmppllPXrXarAMUQ/NztptAHvLjijRCoN9zGHY 4dk9W5HA+fLsYJDWprhJzCBwovVnJ20xl54mHHD4kHZs3I66gtO5/WYDaTSgCxcY 2vE0ADLY1/o2dJmPDU1iNTgxweozNt3q4+uMrTpynYFoZxr9EsDrrzHZNJQRwi3P he81KiuwpQAlHkxdB5hjTnZ1DoJXe51O2rfv/mJcq8LR5B82tdk8UcDt2+kPmPwd rRLSIm5LUAiLnqwBGOYDAd5y+4aSG5yZYKkjdF1jzhMLDycFjJ7ZyZqku4x3TNg= = Received: from nasanppmta01.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3v5pvnh1gm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Sat, 23 Dec 2023 18:35:28 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA01.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 3BNIZRie023143 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Sat, 23 Dec 2023 18:35: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.1118.40; Sat, 23 Dec 2023 10:35:27 -0800 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] reassoc vs uninitialized variable {PR112581] Date: Sat, 23 Dec 2023 10:35:16 -0800 Message-ID: <20231223183516.3712049-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: nalasex01b.na.qualcomm.com (10.47.209.197) 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-GUID: _x0QVWv4RIlvEvOuAkBYTySvmDSYgzhu X-Proofpoint-ORIG-GUID: _x0QVWv4RIlvEvOuAkBYTySvmDSYgzhu X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.997,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-12-09_02,2023-12-07_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 adultscore=0 phishscore=0 mlxscore=0 mlxlogscore=999 spamscore=0 impostorscore=0 malwarescore=0 suspectscore=0 lowpriorityscore=0 clxscore=1015 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2311290000 definitions=main-2312230147 X-Spam-Status: No, score=-13.1 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, 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1786098803439782655 X-GMAIL-MSGID: 1786098803439782655 Like r14-2293-g11350734240dba and r14-2289-gb083203f053f16, reassociation can combine across a few bb and one of the usage can be an uninitializated variable and if going from an conditional usage to an unconditional usage can cause wrong code. This uses maybe_undef_p like other passes where this can happen. Note if-to-switch uses the function (init_range_entry) provided by ressociation so we need to call mark_ssa_maybe_undefs there; otherwise we assume almost all ssa names are uninitialized. Bootstrapped and tested on x86_64-linux-gnu. gcc/ChangeLog: PR tree-optimization/112581 * gimple-if-to-switch.cc (pass_if_to_switch::execute): Call mark_ssa_maybe_undefs. * tree-ssa-reassoc.cc (can_reassociate_op_p): Uninitialized variables can not be reassociated. (init_range_entry): Check for uninitialized variables too. (init_reassoc): Call mark_ssa_maybe_undefs. gcc/testsuite/ChangeLog: PR tree-optimization/112581 * gcc.c-torture/execute/pr112581-1.c: New test. Signed-off-by: Andrew Pinski --- gcc/gimple-if-to-switch.cc | 3 ++ .../gcc.c-torture/execute/pr112581-1.c | 37 +++++++++++++++++++ gcc/tree-ssa-reassoc.cc | 7 +++- 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr112581-1.c diff --git a/gcc/gimple-if-to-switch.cc b/gcc/gimple-if-to-switch.cc index 7792a6024cd..af8d6684d32 100644 --- a/gcc/gimple-if-to-switch.cc +++ b/gcc/gimple-if-to-switch.cc @@ -54,6 +54,7 @@ along with GCC; see the file COPYING3. If not see #include "alloc-pool.h" #include "tree-switch-conversion.h" #include "tree-ssa-reassoc.h" +#include "tree-ssa.h" using namespace tree_switch_conversion; @@ -494,6 +495,8 @@ pass_if_to_switch::execute (function *fun) auto_vec all_candidates; hash_map conditions_in_bbs; + mark_ssa_maybe_undefs (); + basic_block bb; FOR_EACH_BB_FN (bb, fun) find_conditions (bb, &conditions_in_bbs); diff --git a/gcc/testsuite/gcc.c-torture/execute/pr112581-1.c b/gcc/testsuite/gcc.c-torture/execute/pr112581-1.c new file mode 100644 index 00000000000..14081c96d58 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr112581-1.c @@ -0,0 +1,37 @@ +/* { dg-require-effective-target int32plus } */ +/* PR tree-optimization/112581 */ +/* reassociation, used to combine 2 bb to together, + that made an unitialized variable unconditional used + which then at runtime would cause an infinite loop. */ +int a = -1, b = 2501896061, c, d, e, f = 3, g; +int main() { + unsigned h; + int i; + d = 0; + for (; d < 1; d++) { + int j = ~-((6UL ^ a) / b); + if (b) + L: + if (!f) + continue; + if (c) + i = 1; + if (j) { + i = 0; + while (e) + ; + } + g = -1 % b; + h = ~(b || h); + f = g || 0; + a = a || 0; + if (!a) + h = 0; + while (h > 4294967294) + if (i) + break; + if (c) + goto L; + } + return 0; +} diff --git a/gcc/tree-ssa-reassoc.cc b/gcc/tree-ssa-reassoc.cc index cdef9f7cdc3..94873745928 100644 --- a/gcc/tree-ssa-reassoc.cc +++ b/gcc/tree-ssa-reassoc.cc @@ -647,6 +647,9 @@ can_reassociate_op_p (tree op) { if (TREE_CODE (op) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op)) return false; + /* Uninitialized variables can't participate in reassociation. */ + if (TREE_CODE (op) == SSA_NAME && ssa_name_maybe_undef_p (op)) + return false; /* Make sure asm goto outputs do not participate in reassociation since we have no way to find an insertion place after asm goto. */ if (TREE_CODE (op) == SSA_NAME @@ -2600,7 +2603,8 @@ init_range_entry (struct range_entry *r, tree exp, gimple *stmt) } if (TREE_CODE (arg0) != SSA_NAME - || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg0)) + || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg0) + || ssa_name_maybe_undef_p (arg0)) break; loc = gimple_location (stmt); switch (code) @@ -7418,6 +7422,7 @@ init_reassoc (void) free (bbs); calculate_dominance_info (CDI_POST_DOMINATORS); plus_negates = vNULL; + mark_ssa_maybe_undefs (); } /* Cleanup after the reassociation pass, and print stats if