From patchwork Wed Oct 20 12:37:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 46436 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0D23A385842D for ; Wed, 20 Oct 2021 12:38:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0D23A385842D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1634733507; bh=FzT/6Qxd+MO/HCFTAtF1KC0HqjsuylouFqnkvQYIF7g=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=HL+CVUK5KvLyH//Ar729KdzGxPIaAi5bXUQb6aC/59iEiTCkUvbDbbTXiUU5hXTHw DN2aP6gnZkkNdOjXdoji5e4lGdZiPBmX0SrLbMIL24gp9o/+cHG+4enYwTBE+l43ol cmgfKQvjqDX+VwDbI0AHmcDmOx7cfwOnBXFefJNA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id C6B3B3858412 for ; Wed, 20 Oct 2021 12:37:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C6B3B3858412 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-141-KCfaQWNhOSWfZAJbLkX4hg-1; Wed, 20 Oct 2021 08:37:48 -0400 X-MC-Unique: KCfaQWNhOSWfZAJbLkX4hg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6E1D01800D41; Wed, 20 Oct 2021 12:37:47 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.192.71]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2303D60BF1; Wed, 20 Oct 2021 12:37:43 +0000 (UTC) Received: from abulafia.quesejoda.com (localhost [127.0.0.1]) by abulafia.quesejoda.com (8.16.1/8.15.2) with ESMTPS id 19KCbZ2g666276 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 20 Oct 2021 14:37:36 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.16.1/8.16.1/Submit) id 19KCbZMw666275; Wed, 20 Oct 2021 14:37:35 +0200 To: GCC patches , Jeff Law Subject: [PATCH] Attempt to resolve all incoming paths to a PHI. Date: Wed, 20 Oct 2021 14:37:33 +0200 Message-Id: <20211020123733.666221-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Aldy Hernandez via Gcc-patches From: Aldy Hernandez Reply-To: Aldy Hernandez Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" The code that threads incoming paths to a PHI is duplicating what we do generically in find_paths_to_names. This shortcoming is actually one of the reasons we aren't threading all possible paths into a PHI. For example, we give up after finding one threadable path, but some PHIs have multiple threadable paths: // x_5 = PHI <10(4), 20(5), ...> // if (x_5 > 5) Addressing this not only fixes the oversight, but simplifies the PHI handling code, since we can consider the PHI fully resolved upon return. Interestingly, for ssa-thread-12.c the main thread everything was hinging on was unreachable. With this patch, we call maybe_register_path() earlier. In doing so, the solver realizes that any path starting with 4->8 is unreachable and can be avoided. This caused the cascade of threadable paths that depended on this to no longer happen. Since threadable paths in thread[34] was the only thing this test was testing, there's no longer anything to test. Neat! Tested on x86-64 Linux. OK for trunk? gcc/ChangeLog: * tree-ssa-threadbackward.c (back_threader::resolve_phi): Attempt to resolve all incoming paths to a PHI. (back_threader::resolve_def): Always return true for PHIs. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr21090.c: Adjust for threading. * gcc.dg/tree-ssa/ssa-thread-12.c: Removed. --- gcc/testsuite/gcc.dg/tree-ssa/pr21090.c | 2 +- gcc/testsuite/gcc.dg/tree-ssa/ssa-thread-12.c | 73 ------------------- gcc/tree-ssa-threadbackward.c | 70 +++++------------- 3 files changed, 21 insertions(+), 124 deletions(-) delete mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-thread-12.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr21090.c b/gcc/testsuite/gcc.dg/tree-ssa/pr21090.c index 3909adb72d4..92a87688601 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/pr21090.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr21090.c @@ -1,5 +1,5 @@ /* { dg-do compile } */ -/* { dg-options "-O2 -fdisable-tree-evrp -fdump-tree-vrp1 -fdelete-null-pointer-checks" } */ +/* { dg-options "-O2 -fno-thread-jumps -fdisable-tree-evrp -fdump-tree-vrp1 -fdelete-null-pointer-checks" } */ int g, h; diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-thread-12.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-thread-12.c deleted file mode 100644 index 08c0b8d3bcc..00000000000 --- a/gcc/testsuite/gcc.dg/tree-ssa/ssa-thread-12.c +++ /dev/null @@ -1,73 +0,0 @@ -/* { dg-do compile } */ -/* { dg-options "-O2 -fdump-tree-thread3-details -fdump-tree-thread4-details -fno-finite-loops --param early-inlining-insns=14 -fno-inline-functions" } */ -/* { dg-final { scan-tree-dump "Registering jump thread" "thread3" } } */ -/* { dg-final { scan-tree-dump "Registering jump thread" "thread4" } } */ - -typedef struct bitmap_head_def *bitmap; -typedef const struct bitmap_head_def *const_bitmap; -typedef struct VEC_int_base -{ -} -VEC_int_base; -typedef struct VEC_int_heap -{ - VEC_int_base base; -} -VEC_int_heap; -typedef unsigned long BITMAP_WORD; -typedef struct bitmap_element_def -{ - struct bitmap_element_def *next; - unsigned int indx; -} -bitmap_element; -typedef struct bitmap_head_def -{ -} -bitmap_head; -typedef struct -{ - bitmap_element *elt1; - bitmap_element *elt2; - BITMAP_WORD bits; -} -bitmap_iterator; -static __inline__ void -bmp_iter_and_compl_init (bitmap_iterator * bi, const_bitmap map1, - const_bitmap map2, unsigned start_bit, - unsigned *bit_no) -{ -} - -static __inline__ void -bmp_iter_next (bitmap_iterator * bi, unsigned *bit_no) -{ -} - -static __inline__ unsigned char -bmp_iter_and_compl (bitmap_iterator * bi, unsigned *bit_no) -{ - if (bi->bits) - { - while (bi->elt2 && bi->elt2->indx < bi->elt1->indx) - bi->elt2 = bi->elt2->next; - } -} - -extern int VEC_int_base_length (VEC_int_base *); -bitmap -compute_idf (bitmap def_blocks, bitmap_head * dfs) -{ - bitmap_iterator bi; - unsigned bb_index, i; - VEC_int_heap *work_stack; - bitmap phi_insertion_points; - while ((VEC_int_base_length (((work_stack) ? &(work_stack)->base : 0))) > 0) - { - for (bmp_iter_and_compl_init - (&(bi), (&dfs[bb_index]), (phi_insertion_points), (0), &(i)); - bmp_iter_and_compl (&(bi), &(i)); bmp_iter_next (&(bi), &(i))) - { - } - } -} diff --git a/gcc/tree-ssa-threadbackward.c b/gcc/tree-ssa-threadbackward.c index edb396b3d6f..a6b9893abbd 100644 --- a/gcc/tree-ssa-threadbackward.c +++ b/gcc/tree-ssa-threadbackward.c @@ -83,7 +83,7 @@ private: edge maybe_register_path (); bool find_paths_to_names (basic_block bb, bitmap imports); bool resolve_def (tree name, bitmap interesting, vec &worklist); - bool resolve_phi (gphi *phi, bitmap imports); + void resolve_phi (gphi *phi, bitmap imports); edge find_taken_edge (const vec &path); edge find_taken_edge_cond (const vec &path, gcond *); edge find_taken_edge_switch (const vec &path, gswitch *); @@ -243,17 +243,14 @@ populate_worklist (vec &worklist, bitmap bits) } } -// If taking any of the incoming edges to a PHI causes the final -// conditional of the current path to be constant, register the -// path(s), and return TRUE. +// Find jump threading paths that go through a PHI. -bool +void back_threader::resolve_phi (gphi *phi, bitmap interesting) { if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (phi))) - return true; + return; - bool done = false; for (size_t i = 0; i < gimple_phi_num_args (phi); ++i) { edge e = gimple_phi_arg_edge (phi, i); @@ -275,53 +272,24 @@ back_threader::resolve_phi (gphi *phi, bitmap interesting) continue; } - // FIXME: We currently stop looking if we find a threadable path - // through a PHI. This is pessimistic, as there can be multiple - // paths that can resolve the path. For example: - // - // x_5 = PHI <10(4), 20(5), ...> - // if (x_5 > 5) - tree arg = gimple_phi_arg_def (phi, i); - if (TREE_CODE (arg) == SSA_NAME) - { - unsigned v = SSA_NAME_VERSION (arg); - - // Avoid loops as in: x_5 = PHI . - if (bitmap_bit_p (interesting, v)) - continue; + unsigned v = 0; + if (TREE_CODE (arg) == SSA_NAME + && !bitmap_bit_p (interesting, SSA_NAME_VERSION (arg))) + { + // Record that ARG is interesting when searching down this path. + v = SSA_NAME_VERSION (arg); + gcc_checking_assert (v != 0); bitmap_set_bit (interesting, v); bitmap_set_bit (m_imports, v); + } - // When resolving unknowns, see if the incoming SSA can be - // resolved on entry without having to keep looking back. - bool keep_looking = true; - if (m_resolve) - { - m_path.safe_push (e->src); - if (maybe_register_path ()) - { - keep_looking = false; - m_visited_bbs.add (e->src); - } - m_path.pop (); - } - if (keep_looking) - done |= find_paths_to_names (e->src, interesting); + find_paths_to_names (e->src, interesting); - bitmap_clear_bit (interesting, v); - } - else if (TREE_CODE (arg) == INTEGER_CST) - { - m_path.safe_push (e->src); - edge taken_edge = maybe_register_path (); - if (taken_edge && taken_edge != UNREACHABLE_EDGE) - done = true; - m_path.pop (); - } + if (v) + bitmap_clear_bit (interesting, v); } - return done; } // If the definition of NAME causes the final conditional of the @@ -333,9 +301,11 @@ back_threader::resolve_def (tree name, bitmap interesting, vec &worklist) gimple *def_stmt = SSA_NAME_DEF_STMT (name); // Handle PHIs. - if (is_a (def_stmt) - && resolve_phi (as_a (def_stmt), interesting)) - return true; + if (is_a (def_stmt)) + { + resolve_phi (as_a (def_stmt), interesting); + return true; + } // Defer copies of SSAs by adding the source to the worklist. if (gimple_assign_single_p (def_stmt)