From patchwork Mon Feb 7 14:29:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew MacLeod X-Patchwork-Id: 1589318 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=sG6qq2ms; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JspW24kRJz9s8q for ; Tue, 8 Feb 2022 01:31:58 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1F8CB385842B for ; Mon, 7 Feb 2022 14:31:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1F8CB385842B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1644244316; bh=6YxG+3jVq+2pBbclMv32GJkneE+P3gATqqcE6LaPK9M=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=sG6qq2msMOToGqQt61HFbXGcmK7g7wNRDquQgDpknW0dUH+L48Moak+MIsoOtKu2k b8ugEv2l5jnzEwhRP6JSO7LdSNOwHC4BcQusxBnmGumolgSkKex3KgUdIvhrMAVB61 nmqw9BXFCb3xZ7hnDRwNQs0PC7uB1IEFdW2YpCD4= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 8F4713858C83 for ; Mon, 7 Feb 2022 14:29:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8F4713858C83 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-164-SZrN76ELMjyS5IzRNxT5CQ-1; Mon, 07 Feb 2022 09:29:48 -0500 X-MC-Unique: SZrN76ELMjyS5IzRNxT5CQ-1 Received: by mail-qt1-f198.google.com with SMTP id x5-20020ac84d45000000b002cf826b1a18so11187629qtv.2 for ; Mon, 07 Feb 2022 06:29:48 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent :content-language:from:subject:to; bh=l5mJNFIOdexrhxEVdFgKuLQD1KHijZMD1XxpigBBmco=; b=R/43dmqyEJFPdDaEDeXgLupnbF0eauPVT2u3HH+KofQ2lgXQrPlBVmqV9RscjMYgjl tUAYe2yrJyaFTLJYrAw4hwvKbvDwGG811qdCauGVrQf8RU9hHXrrlJ1wtE3UHmrwKqYG ISNBR9YY+FS34ZrlWakXHUUTaUum7Kln6xOX34hi1B1W1ZtHuh/ZVZ4z879EKtm1sSjv OPCRfEZ3VdNLCjX9wekijfsuu1XxlXLkdnggSR6iSKTV2WkzrjL6Ye1CuT7cdqdfIJmn 8vcSVj11+X1j/MnWN8Tp539xGPjjia7eiltemNg4sabM9lskdJ6/Y7OLAjJr+co9QcrX GDTA== X-Gm-Message-State: AOAM531Voq7EUu9aTI5T7YvO08JpJfCXISnWCdD4ZGDBM51CJwGNYzqn Dx/JHMMFAi7eX5NzH5pnw1ASNi9gHtghoD1cwOdVzjdZlLFtDTb4FKCsXNtNuErGEJC9e2lqnoS Mfo0oyNV6aM+n7vyDuEd/fHG9ArgMIBUfKUktqqrVZF5kwgYSH4ps0hWNJ1w9aqKmEMGH7A== X-Received: by 2002:a05:620a:29d4:: with SMTP id s20mr6330251qkp.397.1644244187559; Mon, 07 Feb 2022 06:29:47 -0800 (PST) X-Google-Smtp-Source: ABdhPJwa5b/VA32JUPmDKkfXbz/dvJUbV5Jby0/AyNzxJAkZ7zO1UT0jKUBmzw/VaiGQbmHook14Ug== X-Received: by 2002:a05:620a:29d4:: with SMTP id s20mr6330232qkp.397.1644244187228; Mon, 07 Feb 2022 06:29:47 -0800 (PST) Received: from ?IPV6:2607:fea8:a262:5f00::9b6f? ([2607:fea8:a262:5f00::9b6f]) by smtp.gmail.com with ESMTPSA id 195sm5493964qkf.30.2022.02.07.06.29.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 07 Feb 2022 06:29:46 -0800 (PST) Message-ID: <53667fe7-7f67-8366-8363-ede9c3aebcb8@redhat.com> Date: Mon, 7 Feb 2022 09:29:45 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.1 Subject: [PATCH 2/4] tree-optimization/104288 - Register side effects during ranger vrp domwalk. To: gcc-patches , aldy Hernandez X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-CA X-Spam-Status: No, score=-12.1 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, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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: Andrew MacLeod via Gcc-patches From: Andrew MacLeod Reply-To: Andrew MacLeod Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This patch adds the ability to register side effects within a block to ranger. This is currently only for tracking non-null values. the rvrp_folder performs a DOM walk and then attempts to simplify and the fold each stmt during the walk.  This patch adds a call to register_side_effects() to record any effects the stmt might have before returning. This checks if the non-null property is set for any ssa-names on the statement, and if they are, moves the state to only non-null for the block...  This allows us to adjust the property within the block as we do the DOM walk.  All further queries within the block will then come back as non-null.   ALl other queries will be from outside the block, so they will see the same results anyway. Unfortunately, none of the non-null routines in gimple.cc appear to work "in bulk", but rather, on a specific tree-ssa operand request at a time.  For this patch, I need to scan the entire statement looking for any operand that is nonnull (or the performance impact is awful). I took the code in the various infer_nonnull routines, and packed it all together into the two routines block_apply_nonnull && infer_nonnull_call_attributes,  which basically perform the same functionality as  infer_nonnull_range_by_dereference and infer_nonnull_range_by_attribute, only on all the operands at once.  I think its right, but you may want to have a look.   I intend to leverage this code in GCC13  for a more generalized range_after_stmt mechanism that will do away with the immediate-use visits of the current non-null This patch also has zero effect on generated code as none of the nonnull_deref_p() queries make use of the new granularity available yet.  It does set the table for the next patch. Performance impact of this is also marginal, about a 1% hit to EVRP/VRP. Bootstraps with no regressions.  OK for trunk? Andrew From c4a1e28b5b1dd8f22e7d87f34961596a816c7ad7 Mon Sep 17 00:00:00 2001 From: Andrew MacLeod Date: Wed, 2 Feb 2022 14:44:57 -0500 Subject: [PATCH 2/3] Register side effects during ranger block walk. This patch adds the ability to register statement side effects within a block for ranger. This is currently only for tracking non-null values. * gimple-range-cache.cc (non_null_ref::try_update_to_always_nonnull): New. (non_null_ref::infer_nonnull_call_attributes): New. (non_null_loadstore): New. (non_null_ref::block_apply_nonnull): New. * gimple-range-cache.h (class non_null_ref): New prototypes. * gimple-range.cc (gimple_ranger::register_side_effects): New. * gimple-range.h (gimple_ranger::register_side_effects): New. * tree-vrp.cc (rvrp_folder::fold_stmt): Call register_side_effects. --- gcc/gimple-range-cache.cc | 106 ++++++++++++++++++++++++++++++++++++++ gcc/gimple-range-cache.h | 3 ++ gcc/gimple-range.cc | 11 ++++ gcc/gimple-range.h | 1 + gcc/tree-vrp.cc | 8 +-- 5 files changed, 126 insertions(+), 3 deletions(-) diff --git a/gcc/gimple-range-cache.cc b/gcc/gimple-range-cache.cc index 52094f0bc6a..d14dc1284f2 100644 --- a/gcc/gimple-range-cache.cc +++ b/gcc/gimple-range-cache.cc @@ -29,6 +29,10 @@ along with GCC; see the file COPYING3. If not see #include "gimple-pretty-print.h" #include "gimple-range.h" #include "tree-cfg.h" +#include "target.h" +#include "attribs.h" +#include "gimple-iterator.h" +#include "gimple-walk.h" #define DEBUG_RANGE_CACHE (dump_file \ && (param_ranger_debug & RANGER_DEBUG_CACHE)) @@ -197,6 +201,108 @@ non_null_ref::process_name (tree name) } } +// This routine will update NAME on stmt S to always_nonnull state, if it +// is in contains_nonull state. This is used during a block walk to move +// NAME to known nonnull. + +void +non_null_ref::try_update_to_always_nonnull (gimple *s, tree name) +{ + if (gimple_range_ssa_p (name) && POINTER_TYPE_P (TREE_TYPE (name))) + { + unsigned long state = get_bb_state (name, gimple_bb (s)); + // Only process when both conditions exist in the BB. + if (state != NN_BOTH) + return; + // From this point forward, we'll be NON-NULL for sure if we see one. + set_bb_state (name, gimple_bb (s), NN_NON_ZERO); + } +} + +// Adapted from infer_nonnull_range_by_attribute for calls to process +// all operands which have the nonnull attribute set in stmt. + +void +non_null_ref::infer_nonnull_call_attributes (gcall *stmt) +{ + if (gimple_call_internal_p (stmt)) + return; + + tree fntype = gimple_call_fntype (stmt); + tree attrs = TYPE_ATTRIBUTES (fntype); + for (; attrs; attrs = TREE_CHAIN (attrs)) + { + attrs = lookup_attribute ("nonnull", attrs); + + /* If "nonnull" wasn't specified, we know nothing about + the argument. */ + if (attrs == NULL_TREE) + return; + + /* Check if "nonnull" applies to all the arguments. */ + if (TREE_VALUE (attrs) == NULL_TREE) + { + for (unsigned int i = 0; i < gimple_call_num_args (stmt); i++) + { + tree op = gimple_call_arg (stmt, i); + try_update_to_always_nonnull (stmt, op); + } + return; + } + + /* Now see whats in this nonnull list. */ + for (tree t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t)) + { + unsigned int idx = TREE_INT_CST_LOW (TREE_VALUE (t)) - 1; + if (idx < gimple_call_num_args (stmt)) + { + tree op = gimple_call_arg (stmt, idx); + try_update_to_always_nonnull (stmt, op); + } + } + } +} + +// Adapted from infer_nonnull_range_by_dereference and check_loadstore +// to work for all operands. + +static bool +non_null_loadstore (gimple *s, tree op, tree, void *data) +{ + if (TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF) + { + /* Some address spaces may legitimately dereference zero. */ + addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (op)); + if (!targetm.addr_space.zero_address_valid (as)) + { + tree ssa = TREE_OPERAND (op, 0); + ((non_null_ref *)data)->try_update_to_always_nonnull (s, ssa); + } + } + return false; +} + +// This routine is used during a block walk to move the state of non-null for +// any operands on stmt S to always nonnull. + +void +non_null_ref::block_apply_nonnull (gimple *s) +{ + if (!flag_delete_null_pointer_checks) + return; + if (is_a (s)) + return; + if (gimple_code (s) == GIMPLE_ASM || gimple_clobber_p (s)) + return; + if (is_a (s)) + { + infer_nonnull_call_attributes (as_a (s)); + return; + } + walk_stmt_load_store_ops (s, (void *)this, non_null_loadstore, + non_null_loadstore); +} + // ------------------------------------------------------------------------- // This class represents the API into a cache of ranges for an SSA_NAME. diff --git a/gcc/gimple-range-cache.h b/gcc/gimple-range-cache.h index 2e61824593e..0d073213226 100644 --- a/gcc/gimple-range-cache.h +++ b/gcc/gimple-range-cache.h @@ -36,9 +36,12 @@ public: bool non_null_deref_p (tree name, basic_block bb, bool search_dom = true); bool adjust_range (irange &r, tree name, basic_block bb, bool search_dom = true); + void block_apply_nonnull (gimple *s); + void try_update_to_always_nonnull (gimple *s, tree name); private: vec m_nn; void process_name (tree name); + void infer_nonnull_call_attributes (gcall *stmt); unsigned long get_bb_state (tree name, basic_block bb); void set_bb_state (tree name, basic_block bb, unsigned long); bitmap_obstack m_bitmaps; diff --git a/gcc/gimple-range.cc b/gcc/gimple-range.cc index 73398ddef99..eb599d7f0d9 100644 --- a/gcc/gimple-range.cc +++ b/gcc/gimple-range.cc @@ -35,6 +35,7 @@ along with GCC; see the file COPYING3. If not see #include "tree-scalar-evolution.h" #include "gimple-range.h" #include "gimple-fold.h" +#include "gimple-walk.h" gimple_ranger::gimple_ranger () : non_executable_edge_flag (cfun), @@ -436,6 +437,16 @@ gimple_ranger::fold_stmt (gimple_stmt_iterator *gsi, tree (*valueize) (tree)) return ret; } +// Called during dominator walks to register any side effects that take effect +// from this point forward. Current release is only for tracking non-null +// within a block. + +void +gimple_ranger::register_side_effects (gimple *s) +{ + m_cache.m_non_null.block_apply_nonnull (s); +} + // This routine will export whatever global ranges are known to GCC // SSA_RANGE_NAME_INFO and SSA_NAME_PTR_INFO fields. diff --git a/gcc/gimple-range.h b/gcc/gimple-range.h index ec568153a8d..0733a534853 100644 --- a/gcc/gimple-range.h +++ b/gcc/gimple-range.h @@ -60,6 +60,7 @@ public: void dump_bb (FILE *f, basic_block bb); auto_edge_flag non_executable_edge_flag; bool fold_stmt (gimple_stmt_iterator *gsi, tree (*) (tree)); + void register_side_effects (gimple *s); protected: bool fold_range_internal (irange &r, gimple *s, tree name); void prefill_name (irange &r, tree name); diff --git a/gcc/tree-vrp.cc b/gcc/tree-vrp.cc index 62946450b13..e9f19d0c8b9 100644 --- a/gcc/tree-vrp.cc +++ b/gcc/tree-vrp.cc @@ -4309,9 +4309,11 @@ public: bool fold_stmt (gimple_stmt_iterator *gsi) OVERRIDE { - if (m_simplifier.simplify (gsi)) - return true; - return m_ranger->fold_stmt (gsi, follow_single_use_edges); + bool ret = m_simplifier.simplify (gsi); + if (!ret) + ret = m_ranger->fold_stmt (gsi, follow_single_use_edges); + m_ranger->register_side_effects (gsi_stmt (*gsi)); + return ret; } private: -- 2.17.2