From patchwork Thu Nov 2 10:30:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 160822 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp254162vqu; Thu, 2 Nov 2023 03:31:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFVgnHnrOODDf6iN3+sCRZjnzQ40n80ACgJAQU3BgAO2f5J/dRVhEmKpMd/qh3Wq/UBTrBC X-Received: by 2002:a05:620a:bd3:b0:778:90a1:d3e7 with SMTP id s19-20020a05620a0bd300b0077890a1d3e7mr18699335qki.41.1698921070172; Thu, 02 Nov 2023 03:31:10 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698921070; cv=pass; d=google.com; s=arc-20160816; b=VBNHlhxrxZKMzvuvMk/WUr/Ls32+TNg+puD0JkpYq/U3p+ddVCNz/AF6DkRY+OTkgK P2fTQ8J9xSztqfUrzvkYPPb5QUpT6SnjAxXoDlFJ4PNkBNLmdMB+ubtMI6cbqviG8P1K /TjgVb+n9jZ8rPZiA/ujLG+glDcGp92Yady3J6gKhB0+PDStm/7oW3PmVui+m/y55xiB NPDGgl9VTxypEZZ8yvrleWQYUVj8AcogrMlxH/jrJOV7H6z0YDHqPM+mdl1nTZMo1w4z kQOwaonGTK+Lu/6qLM6yOMEnQBfCQUWVHsq6+x6ZtLWkMUSYGiuI3f8vNaBMEQrSSn5T al7Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:errors-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:mime-version :user-agent:subject:to:from:date:dkim-signature:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=4ABLcaTbcw/S/R11P9yWf1IZSogDLmL8PkOLYlO/q9Q=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=dTCke7NH5xo0Ex/euzUYJSmGqfianvN7zOV+2KnabR5BVS0mzykDFlBI2UshxN6tXR A8MQU7R04oBDNcDhjqmQ0QIL3lqApQkkXqZPaFreuQDDZhEpOUBRD7hG32csdTpYJTK8 dr+HuHW7cUrP1TKLe3EvErG937AQBfLW8y88V+nycHCR3h1CVdg647BLGd8V3+Vkx9jm lKmTnD4WETrG/YalM03AhrK0XRrXZaXpQERq9KpkBOG3vk5AeLd//vw7eWNOyvUxcKi9 bN+hqpmL3XMWOjayL3/3BAT04iLI55p93RzUyQrv8/ssBNhA0g190O8R/jZvt4B8TkwB zhdg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=wgYAlyoT; dkim=neutral (no key) header.i=@suse.de; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id pa17-20020a05620a831100b0077a065c875csi3904513qkn.161.2023.11.02.03.31.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 03:31:10 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=wgYAlyoT; dkim=neutral (no key) header.i=@suse.de; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E2A02385B522 for ; Thu, 2 Nov 2023 10:31:09 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by sourceware.org (Postfix) with ESMTPS id 611063858D35 for ; Thu, 2 Nov 2023 10:30:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 611063858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 611063858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:67c:2178:6::1d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698921047; cv=none; b=GRZCtlEn/TE6SbFWrIjXyCHLv3oUxCAaepDpTjn13NrLWDx7L9KJQje0zRKwW9VfvNBWhRQtOiABuqvyWAhDsaMZ6pT2fg4iMs1HIvBTYZbK8zPV9DfiXSYcnWsCzG5+5NNHoGomn5C+UGlBPQ4u2pb7ovfPMC2EvY/XehRJ62Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698921047; c=relaxed/simple; bh=ZfiaMMCoyQZZg/4krQGv+Afqnb7xEi6hfln4U+EnIcs=; h=DKIM-Signature:DKIM-Signature:Date:From:To:Subject:MIME-Version; b=PeoNPBbWnqHo1unJ2jeez4XHTa/VChp/lGbFBb+9F+OjhvRffh3v3V4bJ1gmPDLaJ/PVPPpQ4SmIwvOiuad+URsSf+HLKXMwGt2BIJ/g5QFXBuE5TCBkohOwAxV9EFTOlG0h7BD5cIb6FdZ5BMDOBPBJa31+uebNOCb8K5tSY/Y= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 5A65E1F896 for ; Thu, 2 Nov 2023 10:30:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1698921036; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=4ABLcaTbcw/S/R11P9yWf1IZSogDLmL8PkOLYlO/q9Q=; b=wgYAlyoThiAJ5sTvDXsvMayJysnQ+PuH3xnyg+N3/ogU2gKlpH64MfsFHUsfQ77BOPq4rR VQqMdcl1wiKejbUH0Cl6FLsdgPEmecDS/OvGI7KLrpcgunLZpjQCxNEJU4taMKzWaQLxOM ccGCjf+ZWLdekYeY/2FEsUfM1Rf/u1U= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1698921036; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=4ABLcaTbcw/S/R11P9yWf1IZSogDLmL8PkOLYlO/q9Q=; b=/bVs9LCfdusLwzM4gugR3aeqXF/W8am1kQJAMPxrX9v3xgTdxopj1GJbo/MpB+ThMUV3PP Frw5xriopvvP0eBg== Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 1A7132C1AD for ; Thu, 2 Nov 2023 10:30:36 +0000 (UTC) Date: Thu, 2 Nov 2023 10:30:36 +0000 (UTC) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/112320 - bougs debug IL after SCCP User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, MISSING_MID, 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 Message-Id: <20231102103109.E2A02385B522@sourceware.org> X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781447860429070633 X-GMAIL-MSGID: 1781447860429070633 The following addresses wrong debug IL created by SCCP rewriting stmts to defined overflow. I addressed another inefficiency there but needed to adjust the API of rewrite_to_defined_overflow for this which is now taking a stmt iterator for in-place operation and a stmt for sequence producing because gsi_for_stmt doesn't work for stmts not in the IL. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/112320 * gimple-fold.h (rewrite_to_defined_overflow): New overload for in-place operation. * gimple-fold.cc (rewrite_to_defined_overflow): Add stmt iterator argument to worker, define separate API for in-place and not in-place operation. * tree-if-conv.cc (predicate_statements): Simplify. * tree-scalar-evolution.cc (final_value_replacement_loop): Likewise. * tree-ssa-ifcombine.cc (pass_tree_ifcombine::execute): Adjust. * tree-ssa-reassoc.cc (update_range_test): Likewise. * gcc.dg/pr112320.c: New testcase. --- gcc/gimple-fold.cc | 25 ++++++++++++++++++------- gcc/gimple-fold.h | 3 ++- gcc/testsuite/gcc.dg/pr112320.c | 14 ++++++++++++++ gcc/tree-if-conv.cc | 19 +------------------ gcc/tree-scalar-evolution.cc | 15 ++++----------- gcc/tree-ssa-ifcombine.cc | 2 +- gcc/tree-ssa-reassoc.cc | 2 +- 7 files changed, 41 insertions(+), 39 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr112320.c diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc index 853edd9e5d4..a5be2ee048b 100644 --- a/gcc/gimple-fold.cc +++ b/gcc/gimple-fold.cc @@ -8769,12 +8769,14 @@ arith_code_with_undefined_signed_overflow (tree_code code) its operand, carrying out the operation in the corresponding unsigned type and converting the result back to the original type. - If IN_PLACE is true, adjust the stmt in place and return NULL. + If IN_PLACE is true, *GSI points to STMT, adjust the stmt in place and + return NULL. Otherwise returns a sequence of statements that replace STMT and also contain a modified form of STMT itself. */ -gimple_seq -rewrite_to_defined_overflow (gimple *stmt, bool in_place /* = false */) +static gimple_seq +rewrite_to_defined_overflow (gimple_stmt_iterator *gsi, gimple *stmt, + bool in_place) { if (dump_file && (dump_flags & TDF_DETAILS)) { @@ -8801,9 +8803,8 @@ rewrite_to_defined_overflow (gimple *stmt, bool in_place /* = false */) gimple_set_modified (stmt, true); if (in_place) { - gimple_stmt_iterator gsi = gsi_for_stmt (stmt); if (stmts) - gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT); + gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT); stmts = NULL; } else @@ -8811,8 +8812,7 @@ rewrite_to_defined_overflow (gimple *stmt, bool in_place /* = false */) gimple *cvt = gimple_build_assign (lhs, NOP_EXPR, gimple_assign_lhs (stmt)); if (in_place) { - gimple_stmt_iterator gsi = gsi_for_stmt (stmt); - gsi_insert_after (&gsi, cvt, GSI_SAME_STMT); + gsi_insert_after (gsi, cvt, GSI_SAME_STMT); update_stmt (stmt); } else @@ -8821,6 +8821,17 @@ rewrite_to_defined_overflow (gimple *stmt, bool in_place /* = false */) return stmts; } +void +rewrite_to_defined_overflow (gimple_stmt_iterator *gsi) +{ + rewrite_to_defined_overflow (gsi, gsi_stmt (*gsi), true); +} + +gimple_seq +rewrite_to_defined_overflow (gimple *stmt) +{ + return rewrite_to_defined_overflow (nullptr, stmt, false); +} /* The valueization hook we use for the gimple_build API simplification. This makes us match fold_buildN behavior by only combining with diff --git a/gcc/gimple-fold.h b/gcc/gimple-fold.h index 2fd58db9a2e..f69bcc7d3e4 100644 --- a/gcc/gimple-fold.h +++ b/gcc/gimple-fold.h @@ -60,7 +60,8 @@ extern tree gimple_fold_indirect_ref (tree); extern bool gimple_fold_builtin_sprintf (gimple_stmt_iterator *); extern bool gimple_fold_builtin_snprintf (gimple_stmt_iterator *); extern bool arith_code_with_undefined_signed_overflow (tree_code); -extern gimple_seq rewrite_to_defined_overflow (gimple *, bool = false); +extern void rewrite_to_defined_overflow (gimple_stmt_iterator *); +extern gimple_seq rewrite_to_defined_overflow (gimple *); extern void replace_call_with_value (gimple_stmt_iterator *, tree); extern tree tree_vec_extract (gimple_stmt_iterator *, tree, tree, tree, tree); extern void gsi_replace_with_seq_vops (gimple_stmt_iterator *, gimple_seq); diff --git a/gcc/testsuite/gcc.dg/pr112320.c b/gcc/testsuite/gcc.dg/pr112320.c new file mode 100644 index 00000000000..15cf39f898c --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr112320.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O -g" } */ + +unsigned void0_effective_addr2; +int void0_i, void0_m, void0_p2; +void void0() +{ + void0_m = 800 - (void0_effective_addr2 & 5); + int b1; + void0_i = 0; + for (; void0_i < void0_m; void0_i++) + b1++; + void0_p2 = b1++; +} diff --git a/gcc/tree-if-conv.cc b/gcc/tree-if-conv.cc index 262765139ff..16e06d82f59 100644 --- a/gcc/tree-if-conv.cc +++ b/gcc/tree-if-conv.cc @@ -2774,24 +2774,7 @@ predicate_statements (loop_p loop) && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (lhs)) && arith_code_with_undefined_signed_overflow (gimple_assign_rhs_code (stmt))) - { - gsi_remove (&gsi, true); - gimple_seq stmts = rewrite_to_defined_overflow (stmt); - bool first = true; - for (gimple_stmt_iterator gsi2 = gsi_start (stmts); - !gsi_end_p (gsi2);) - { - gassign *stmt2 = as_a (gsi_stmt (gsi2)); - gsi_remove (&gsi2, false); - if (first) - { - gsi_insert_before (&gsi, stmt2, GSI_NEW_STMT); - first = false; - } - else - gsi_insert_after (&gsi, stmt2, GSI_NEW_STMT); - } - } + rewrite_to_defined_overflow (&gsi); else if (gimple_vdef (stmt)) { tree lhs = gimple_assign_lhs (stmt); diff --git a/gcc/tree-scalar-evolution.cc b/gcc/tree-scalar-evolution.cc index a6524de7b92..70b17c5bca1 100644 --- a/gcc/tree-scalar-evolution.cc +++ b/gcc/tree-scalar-evolution.cc @@ -3864,21 +3864,14 @@ final_value_replacement_loop (class loop *loop) while (!gsi_end_p (gsi2)) { gimple *stmt = gsi_stmt (gsi2); - gimple_stmt_iterator gsi3 = gsi2; - gsi_next (&gsi2); - gsi_remove (&gsi3, false); if (is_gimple_assign (stmt) && arith_code_with_undefined_signed_overflow - (gimple_assign_rhs_code (stmt))) - gsi_insert_seq_before (&gsi, - rewrite_to_defined_overflow (stmt), - GSI_SAME_STMT); - else - gsi_insert_before (&gsi, stmt, GSI_SAME_STMT); + (gimple_assign_rhs_code (stmt))) + rewrite_to_defined_overflow (&gsi2); + gsi_next (&gsi2); } } - else - gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT); + gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT); if (dump_file) { fprintf (dump_file, "\n final stmt:\n "); diff --git a/gcc/tree-ssa-ifcombine.cc b/gcc/tree-ssa-ifcombine.cc index 46b076804f4..1906a7e14c6 100644 --- a/gcc/tree-ssa-ifcombine.cc +++ b/gcc/tree-ssa-ifcombine.cc @@ -882,7 +882,7 @@ pass_tree_ifcombine::execute (function *fun) || POINTER_TYPE_P (TREE_TYPE (lhs))) && arith_code_with_undefined_signed_overflow (gimple_assign_rhs_code (ass))) - rewrite_to_defined_overflow (ass, true); + rewrite_to_defined_overflow (&gsi); } cfg_changed |= true; } diff --git a/gcc/tree-ssa-reassoc.cc b/gcc/tree-ssa-reassoc.cc index 41ee36413b5..26321aa4fc5 100644 --- a/gcc/tree-ssa-reassoc.cc +++ b/gcc/tree-ssa-reassoc.cc @@ -2933,7 +2933,7 @@ update_range_test (struct range_entry *range, struct range_entry *otherrange, gimple_stmt_iterator gsin = gsi; gsi_prev (&gsip); gsi_next (&gsin); - rewrite_to_defined_overflow (stmt, true); + rewrite_to_defined_overflow (&gsi); unsigned uid = gimple_uid (stmt); if (gsi_end_p (gsip)) gsip = gsi_after_labels (bb);