From patchwork Tue Oct 26 00:04:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew MacLeod X-Patchwork-Id: 46646 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 4200D385842D for ; Tue, 26 Oct 2021 00:05:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4200D385842D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1635206708; bh=IbRIQYTOjaRNFXgPlDaQW8BffHGfsd2VLY+WgooWEP8=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Ut9qJrIxufSb93eo+qvrCKgyq44fAGcRF/65f6eOZcSoIouQER277Ex5b8hctxKhd Zom42LaQjLhsQlgTfOT7Ozsb9PXwq/bMh1HKquHqa8Exth8zuLdlIbDQ9PMxHSUUSz gOUK3QWJF1ew0gl9YJS2qW/p82rBYtHSnzAv5lNk= 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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTPS id 611C63858007 for ; Tue, 26 Oct 2021 00:04:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 611C63858007 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-581-HRy3aaNPPX2K-56XJ3Hg3g-1; Mon, 25 Oct 2021 20:04:22 -0400 X-MC-Unique: HRy3aaNPPX2K-56XJ3Hg3g-1 Received: by mail-qv1-f70.google.com with SMTP id j9-20020a0cc349000000b00384fcbc4316so6334322qvi.9 for ; Mon, 25 Oct 2021 17:04:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:to:cc:from:subject:message-id:date:user-agent :mime-version:content-language; bh=IbRIQYTOjaRNFXgPlDaQW8BffHGfsd2VLY+WgooWEP8=; b=5MN/913FTrCHJ30blLb+wxnD6uzje4i88KS/pDRON48tGGP+oHaFnkEJSQYkfhQl9X gBrXKY/8dnm3jD6mwTg+4VelBq9g50F+vTr89aZhYyIO5pJUzHT3c2O959pGdF0YbqY9 SCiufzLFJpV0eHca0LI2PpADZaoDfUKp7JoPRAvId5jclbqc0/W9EEG8wWYBzZA2z3KJ vG4d5d9xFDM8sNBf2Rwpd5Wn18diZMBoYy9z8punUi5sgznD0tHlt4SzMC0ybpW1Me8E MbAKBiOkzxJ0NAvdOyQAy8MeU0z/ooPsX95a3FEnzMYeLtrmryCsUVVVKWWsrrJ5x7Kb /ARw== X-Gm-Message-State: AOAM532hV4fWoDAJsHZBPGtsL7CkDlI0j9CIbgNMDKVGcVyRHNUpzMKC 4z41th+nyjvireHIqpfURac09W0rzh0oDhqYGH191dcNWqwIewgbwtRb1yY9xx/WgzPNSsmtLED ZprZKowlR1BKsL3HVVA== X-Received: by 2002:a05:620a:24d1:: with SMTP id m17mr281731qkn.22.1635206661676; Mon, 25 Oct 2021 17:04:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwJPpJwOE/Nm4EO6yRj+nu6m8ABECK2Hti2oKQQ8PgSju4ATRMwJyuFMb5Ra5yfz3Up82bJLg== X-Received: by 2002:a05:620a:24d1:: with SMTP id m17mr281697qkn.22.1635206661187; Mon, 25 Oct 2021 17:04:21 -0700 (PDT) Received: from [192.168.0.102] ([192.24.49.122]) by smtp.gmail.com with ESMTPSA id c9sm8597928qte.43.2021.10.25.17.04.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 Oct 2021 17:04:20 -0700 (PDT) To: gcc-patches Subject: [COMMITTED] Move vrp_simplify_cond_using_ranges to the simplifier. Message-ID: <76be2fd3-f36a-7eb7-d452-87c56424d51b@redhat.com> Date: Mon, 25 Oct 2021 20:04:19 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-CA X-Spam-Status: No, score=-11.5 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_H2, 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: Andrew MacLeod via Gcc-patches From: Andrew MacLeod Reply-To: Andrew MacLeod Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" VRP currently performs a simplification that we can move into the general simplification code. I'll just quote the comment:    If the conditional is of the form SSA_NAME op constant and the SSA_NAME    was set via a type conversion, try to replace the SSA_NAME with the RHS    of the type conversion.  Doing so makes the conversion dead which helps    subsequent passes.  */ This patch moves the routine to the simplify_using_ranges class, and calls it when other conditional simplifications fail.  It also moves the simplfy_cast_conds routine into the VRP folder instead of being a standalone static function in tree-vrp.c. Bootstrapped on x86_64-pc-linux-gnu with no regressions. Pushed. Andrew From f5bacd9c5be5e129688d9c91eeed05e7b968117e Mon Sep 17 00:00:00 2001 From: Andrew MacLeod Date: Mon, 25 Oct 2021 18:04:06 -0400 Subject: [PATCH 2/2] Move vrp_simplify_cond_using_ranges into the simplifier. This static VRP routine does a simplification with casted conditions. Add it to the general simplfier, and continue to invoke if from the VRP folder. * tree-vrp.c (vrp_simplify_cond_using_ranges): Add return type and move to vr-values.c. (simplify_casted_conds): Move to vrp_folder class. (execute_vrp): Call via vrp_folder now. * vr-values.c (simplify_cond_using_ranges_1): Call simplify_casted_cond. (simplify_using_ranges::simplify_casted_cond): Relocate from tree-vrp.c. * vr-values.h (simplify_casted_cond): Add prototype. --- gcc/tree-vrp.c | 91 ++++++++----------------------------------------- gcc/vr-values.c | 69 +++++++++++++++++++++++++++++++++++++ gcc/vr-values.h | 1 + 3 files changed, 85 insertions(+), 76 deletions(-) diff --git a/gcc/tree-vrp.c b/gcc/tree-vrp.c index a948c524098..38ea50303e0 100644 --- a/gcc/tree-vrp.c +++ b/gcc/tree-vrp.c @@ -4031,6 +4031,7 @@ class vrp_folder : public substitute_and_fold_engine : substitute_and_fold_engine (/* Fold all stmts. */ true), m_vr_values (v), simplifier (v) { } + void simplify_casted_conds (function *fun); private: tree value_of_expr (tree name, gimple *stmt) OVERRIDE @@ -4117,78 +4118,6 @@ vrp_folder::fold_stmt (gimple_stmt_iterator *si) return simplifier.simplify (si); } -/* STMT is a conditional at the end of a basic block. - - If the conditional is of the form SSA_NAME op constant and the SSA_NAME - was set via a type conversion, try to replace the SSA_NAME with the RHS - of the type conversion. Doing so makes the conversion dead which helps - subsequent passes. */ - -static void -vrp_simplify_cond_using_ranges (range_query *query, gcond *stmt) -{ - tree op0 = gimple_cond_lhs (stmt); - tree op1 = gimple_cond_rhs (stmt); - - /* If we have a comparison of an SSA_NAME (OP0) against a constant, - see if OP0 was set by a type conversion where the source of - the conversion is another SSA_NAME with a range that fits - into the range of OP0's type. - - If so, the conversion is redundant as the earlier SSA_NAME can be - used for the comparison directly if we just massage the constant in the - comparison. */ - if (TREE_CODE (op0) == SSA_NAME - && TREE_CODE (op1) == INTEGER_CST) - { - gimple *def_stmt = SSA_NAME_DEF_STMT (op0); - tree innerop; - - if (!is_gimple_assign (def_stmt)) - return; - - switch (gimple_assign_rhs_code (def_stmt)) - { - CASE_CONVERT: - innerop = gimple_assign_rhs1 (def_stmt); - break; - case VIEW_CONVERT_EXPR: - innerop = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0); - if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop))) - return; - break; - default: - return; - } - - if (TREE_CODE (innerop) == SSA_NAME - && !POINTER_TYPE_P (TREE_TYPE (innerop)) - && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop) - && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0))) - { - const value_range *vr = query->get_value_range (innerop); - - if (range_int_cst_p (vr) - && range_fits_type_p (vr, - TYPE_PRECISION (TREE_TYPE (op0)), - TYPE_SIGN (TREE_TYPE (op0))) - && int_fits_type_p (op1, TREE_TYPE (innerop))) - { - tree newconst = fold_convert (TREE_TYPE (innerop), op1); - gimple_cond_set_lhs (stmt, innerop); - gimple_cond_set_rhs (stmt, newconst); - update_stmt (stmt); - if (dump_file && (dump_flags & TDF_DETAILS)) - { - fprintf (dump_file, "Folded into: "); - print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); - fprintf (dump_file, "\n"); - } - } - } - } -} - /* A comparison of an SSA_NAME against a constant where the SSA_NAME was set by a type conversion can often be rewritten to use the RHS of the type conversion. Do this optimization for all conditionals @@ -4198,15 +4127,25 @@ vrp_simplify_cond_using_ranges (range_query *query, gcond *stmt) So that transformation is not performed until after jump threading is complete. */ -static void -simplify_casted_conds (function *fun, range_query *query) +void +vrp_folder::simplify_casted_conds (function *fun) { basic_block bb; FOR_EACH_BB_FN (bb, fun) { gimple *last = last_stmt (bb); if (last && gimple_code (last) == GIMPLE_COND) - vrp_simplify_cond_using_ranges (query, as_a (last)); + { + if (simplifier.simplify_casted_cond (as_a (last))) + { + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fprintf (dump_file, "Folded into: "); + print_gimple_stmt (dump_file, last, 0, TDF_SLIM); + fprintf (dump_file, "\n"); + } + } + } } } @@ -4301,7 +4240,7 @@ execute_vrp (struct function *fun, bool warn_array_bounds_p) array_checker.check (); } - simplify_casted_conds (fun, &vrp_vr_values); + folder.simplify_casted_conds (fun); free_numbers_of_iterations_estimates (fun); diff --git a/gcc/vr-values.c b/gcc/vr-values.c index d0f7cb41bc8..00246c9d3af 100644 --- a/gcc/vr-values.c +++ b/gcc/vr-values.c @@ -3630,6 +3630,75 @@ simplify_using_ranges::simplify_cond_using_ranges_1 (gcond *stmt) } } } + // Try to simplify casted conditions. + return simplify_casted_cond (stmt); +} + +/* STMT is a conditional at the end of a basic block. + + If the conditional is of the form SSA_NAME op constant and the SSA_NAME + was set via a type conversion, try to replace the SSA_NAME with the RHS + of the type conversion. Doing so makes the conversion dead which helps + subsequent passes. */ + +bool +simplify_using_ranges::simplify_casted_cond (gcond *stmt) +{ + tree op0 = gimple_cond_lhs (stmt); + tree op1 = gimple_cond_rhs (stmt); + + /* If we have a comparison of an SSA_NAME (OP0) against a constant, + see if OP0 was set by a type conversion where the source of + the conversion is another SSA_NAME with a range that fits + into the range of OP0's type. + + If so, the conversion is redundant as the earlier SSA_NAME can be + used for the comparison directly if we just massage the constant in the + comparison. */ + if (TREE_CODE (op0) == SSA_NAME + && TREE_CODE (op1) == INTEGER_CST) + { + gimple *def_stmt = SSA_NAME_DEF_STMT (op0); + tree innerop; + + if (!is_gimple_assign (def_stmt)) + return false; + + switch (gimple_assign_rhs_code (def_stmt)) + { + CASE_CONVERT: + innerop = gimple_assign_rhs1 (def_stmt); + break; + case VIEW_CONVERT_EXPR: + innerop = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0); + if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop))) + return false; + break; + default: + return false; + } + + if (TREE_CODE (innerop) == SSA_NAME + && !POINTER_TYPE_P (TREE_TYPE (innerop)) + && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop) + && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0))) + { + const value_range *vr = query->get_value_range (innerop); + + if (range_int_cst_p (vr) + && range_fits_type_p (vr, + TYPE_PRECISION (TREE_TYPE (op0)), + TYPE_SIGN (TREE_TYPE (op0))) + && int_fits_type_p (op1, TREE_TYPE (innerop))) + { + tree newconst = fold_convert (TREE_TYPE (innerop), op1); + gimple_cond_set_lhs (stmt, innerop); + gimple_cond_set_rhs (stmt, newconst); + update_stmt (stmt); + return true; + } + } + } return false; } diff --git a/gcc/vr-values.h b/gcc/vr-values.h index 821bcb9d58d..21dc4eaebce 100644 --- a/gcc/vr-values.h +++ b/gcc/vr-values.h @@ -44,6 +44,7 @@ public: tree vrp_evaluate_conditional_warnv_with_ops (gimple *stmt, enum tree_code, tree, tree, bool, bool *, bool *); + bool simplify_casted_cond (gcond *); private: bool simplify_truth_ops_using_ranges (gimple_stmt_iterator *, gimple *); -- 2.17.2