From patchwork Thu Mar 16 15:27:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manolis Tsamis X-Patchwork-Id: 66454 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 2881D3854831 for ; Thu, 16 Mar 2023 15:27:57 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lj1-x235.google.com (mail-lj1-x235.google.com [IPv6:2a00:1450:4864:20::235]) by sourceware.org (Postfix) with ESMTPS id 73B813850840 for ; Thu, 16 Mar 2023 15:27:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 73B813850840 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=vrull.eu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=vrull.eu Received: by mail-lj1-x235.google.com with SMTP id b13so2088485ljf.6 for ; Thu, 16 Mar 2023 08:27:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull.eu; s=google; t=1678980455; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=xO1imLnvKBt7bcZ7ZEBbMd0xiaKlJOPkSJs7ztDIGBM=; b=Nb9XjG8J3iSfqY99rbzMDy/t9mjO6XOspELEJbqPMqwf3F+K+mDRiVUhMCOcViG6HM HqGOczr2TtmK+IEfTJlAy+NVOppaMPexyH+oXWBkDC5gtcTErMJPqsGQRMiJGqp9+Vz2 h8HOnxLi7bxPHWYldlSMXq4UAOfiqI6kwEoG/lLtd9NIu7dE29DOJI9tjsBhfaDA2aFg R8zr0GTqZNTV01iVftbDZQk4FOByNe43BIpqmkkMT8FBkL87y7dGfHEu0A+KnmBKgEbP pG5s9BA1hp3GX06Io0o9+db56yz4EQdNVbJbEUTPKDBNMA6e5gwTDLPhi63pPx06b0yl Kakg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678980455; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=xO1imLnvKBt7bcZ7ZEBbMd0xiaKlJOPkSJs7ztDIGBM=; b=ZSeftxahLHRoSGq0fO2UBD5gFJG83IXQ1DyhhW0kSSyK/HClyVTQE+Qi/5h00ti5iS ufqqU2nsjAepT14vizAv0j3nCCR8bWMiskt5l8LJ7zMnNtweQRu3pLLK6tjO9cYrKUjH pVYKf6fI7qAIS9/J+HV8wfznVXD+y8QmzfxZg8jeyQyGErgxVXsQ0Gjq9FtVPLC4l9YV j6BrQuF3w6HS0J0OzH+WVNXeiY+ZKIGSNuOkQdyQ1IhNg8S7b9f57yNojE4cywLfJaQZ BSnExlrQOQW+n5zy9CluUCrysqbhOBlFx6whlE8y5Jbt8WGmqGrPzQA9chwEbimF7mne 7MUg== X-Gm-Message-State: AO0yUKUVqYdSFUZXG3L5oce2Z2oNiqIBfzdo3G2Fyrve4lX+vn4DALKM +eKXxCOa5kHoKv2+V/WoSa4p1ryoVdaRpo9hYFg= X-Google-Smtp-Source: AK7set/w5JV7KSGwBy0h5JtaPU6+Rn2LQRIiY0XcAiqJFFDiTEmNVqAMw5xnzQHzncwKPzTdCBeXCA== X-Received: by 2002:a2e:9d4a:0:b0:298:9fb6:77a with SMTP id y10-20020a2e9d4a000000b002989fb6077amr1945708ljj.32.1678980455521; Thu, 16 Mar 2023 08:27:35 -0700 (PDT) Received: from helsinki-03.engr ([2a01:4f9:6b:2a47::2]) by smtp.gmail.com with ESMTPSA id z1-20020a2e9b81000000b00295b597c8fasm1286115lji.22.2023.03.16.08.27.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Mar 2023 08:27:35 -0700 (PDT) From: Manolis Tsamis To: gcc-patches@gcc.gnu.org Cc: Philipp Tomsich , Richard Biener , Manolis Tsamis Subject: [PATCH v1] [RFC] Improve folding for comparisons with zero in tree-ssa-forwprop. Date: Thu, 16 Mar 2023 16:27:06 +0100 Message-Id: <20230316152706.2214124-1-manolis.tsamis@vrull.eu> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" For this C testcase: void g(); void f(unsigned int *a) { if (++*a == 1) g(); } GCC will currently emit a comparison with 1 by using the value of *a after the increment. This can be improved by comparing against 0 and using the value before the increment. As a result there is a potentially shorter dependancy chain (no need to wait for the result of +1) and on targets with compare zero instructions the generated code is one instruction shorter. Example from Aarch64: Before ldr w1, [x0] add w1, w1, 1 str w1, [x0] cmp w1, 1 beq .L4 ret After ldr w1, [x0] add w2, w1, 1 str w2, [x0] cbz w1, .L4 ret gcc/ChangeLog: * tree-ssa-forwprop.cc (combine_cond_expr_cond): (forward_propagate_into_comparison_1): Optimize for zero comparisons. Signed-off-by: Manolis Tsamis --- gcc/tree-ssa-forwprop.cc | 41 +++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/gcc/tree-ssa-forwprop.cc b/gcc/tree-ssa-forwprop.cc index e34f0888954..93d5043821b 100644 --- a/gcc/tree-ssa-forwprop.cc +++ b/gcc/tree-ssa-forwprop.cc @@ -373,12 +373,13 @@ rhs_to_tree (tree type, gimple *stmt) /* Combine OP0 CODE OP1 in the context of a COND_EXPR. Returns the folded result in a form suitable for COND_EXPR_COND or NULL_TREE, if there is no suitable simplified form. If - INVARIANT_ONLY is true only gimple_min_invariant results are - considered simplified. */ + ALWAYS_COMBINE is false then only combine it the resulting + expression is gimple_min_invariant or considered simplified + compared to the original. */ static tree combine_cond_expr_cond (gimple *stmt, enum tree_code code, tree type, - tree op0, tree op1, bool invariant_only) + tree op0, tree op1, bool always_combine) { tree t; @@ -398,17 +399,31 @@ combine_cond_expr_cond (gimple *stmt, enum tree_code code, tree type, /* Canonicalize the combined condition for use in a COND_EXPR. */ t = canonicalize_cond_expr_cond (t); - /* Bail out if we required an invariant but didn't get one. */ - if (!t || (invariant_only && !is_gimple_min_invariant (t))) + if (!t) { fold_undefer_overflow_warnings (false, NULL, 0); return NULL_TREE; } - bool nowarn = warning_suppressed_p (stmt, OPT_Wstrict_overflow); - fold_undefer_overflow_warnings (!nowarn, stmt, 0); + if (always_combine || is_gimple_min_invariant (t)) + { + bool nowarn = warning_suppressed_p (stmt, OPT_Wstrict_overflow); + fold_undefer_overflow_warnings (!nowarn, stmt, 0); + return t; + } - return t; + /* If the result of folding is a zero comparison treat it preferentially. */ + if (TREE_CODE_CLASS (TREE_CODE (t)) == tcc_comparison + && integer_zerop (TREE_OPERAND (t, 1)) + && !integer_zerop (op1)) + { + bool nowarn = warning_suppressed_p (stmt, OPT_Wstrict_overflow); + fold_undefer_overflow_warnings (!nowarn, stmt, 0); + return t; + } + + fold_undefer_overflow_warnings (false, NULL, 0); + return NULL_TREE; } /* Combine the comparison OP0 CODE OP1 at LOC with the defining statements @@ -432,7 +447,7 @@ forward_propagate_into_comparison_1 (gimple *stmt, if (def_stmt && can_propagate_from (def_stmt)) { enum tree_code def_code = gimple_assign_rhs_code (def_stmt); - bool invariant_only_p = !single_use0_p; + bool always_combine = single_use0_p; rhs0 = rhs_to_tree (TREE_TYPE (op1), def_stmt); @@ -442,10 +457,10 @@ forward_propagate_into_comparison_1 (gimple *stmt, && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs0, 0))) == BOOLEAN_TYPE) || TREE_CODE_CLASS (def_code) == tcc_comparison)) - invariant_only_p = false; + always_combine = true; tmp = combine_cond_expr_cond (stmt, code, type, - rhs0, op1, invariant_only_p); + rhs0, op1, always_combine); if (tmp) return tmp; } @@ -459,7 +474,7 @@ forward_propagate_into_comparison_1 (gimple *stmt, { rhs1 = rhs_to_tree (TREE_TYPE (op0), def_stmt); tmp = combine_cond_expr_cond (stmt, code, type, - op0, rhs1, !single_use1_p); + op0, rhs1, single_use1_p); if (tmp) return tmp; } @@ -470,7 +485,7 @@ forward_propagate_into_comparison_1 (gimple *stmt, && rhs1 != NULL_TREE) tmp = combine_cond_expr_cond (stmt, code, type, rhs0, rhs1, - !(single_use0_p && single_use1_p)); + single_use0_p && single_use1_p); return tmp; }