From patchwork Sun Mar 10 18:05:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Law X-Patchwork-Id: 1910240 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=ventanamicro.com header.i=@ventanamicro.com header.a=rsa-sha256 header.s=google header.b=delm7zxs; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tt7BL6W48z23qC for ; Mon, 11 Mar 2024 05:06:01 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9C741385829A for ; Sun, 10 Mar 2024 18:05:58 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oo1-xc33.google.com (mail-oo1-xc33.google.com [IPv6:2607:f8b0:4864:20::c33]) by sourceware.org (Postfix) with ESMTPS id 8B32C3858CD1 for ; Sun, 10 Mar 2024 18:05:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8B32C3858CD1 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=ventanamicro.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=ventanamicro.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8B32C3858CD1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::c33 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710093938; cv=none; b=u4MM+COKlBAJsno1uoxp4oVGg29hy9ZUm4F8QgvAd2AM9ZDNDr+4Z4wygqTnSLCCdkIca/XTdhUYrUJ1sNaD3EuSfjVXzbr0L4DdtbqwksKjLJ56iXJ509Kl/1UPl90VmEIfCFXscuv7fw0HWGU6a9UEqlbTP5Kwu0kDS/6TBgQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710093938; c=relaxed/simple; bh=gxRHJuar0qmcqvnzTmiijMdDwcpgRrEz60aeY3Dj/wQ=; h=DKIM-Signature:Message-ID:Date:MIME-Version:From:To:Subject; b=nXt23TKvR0r65MyDPGnQ5IKe0UuVJFJ70JflrUV+zDjn6GRhkbfamLCyikN5SeMDV1FxBbc5fj/ULYYC95D2an/NsXYjTjiR5KqJVYz1CajwCKvnDBUBc+xtixnTTkoH0Xr/2beQS/oB6/+jj7OgF4kugJo6GzBWT5kPBrpxYkk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oo1-xc33.google.com with SMTP id 006d021491bc7-5a207813b1dso56600eaf.2 for ; Sun, 10 Mar 2024 11:05:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1710093934; x=1710698734; darn=gcc.gnu.org; h=subject:to:from:content-language:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=xyhFiq7GPaERc2jxPutFzSnFDqIpfQvDRASdUabZmPc=; b=delm7zxsU+RMO2/l3wVQP7bBwxLzJc0HA3rjPOGfvrEJGmdSlelIe9y0tUEfQbq0jr WW+oKYVST96gLuBYJIa0bxGtNpZdbGaBQkmW2S64pv704PrqqGuwDuIBR7QO+t0Ia85b +GlGbXMKY68fjqyFcj2R2C7cRA1eCsPYs5xVNXtjJ0q/MkFTU1QgvOj6uTXSO95SjWcL GDOlJ+tTEJTqF/0PPYHK6WppJAaSRhF9Y7h731DU/y9N5giALLVqSl1wLFhV6904/u6d Qc3XJcIH7A3Rhn0AP9+kqKhtSub+i+Y6d8siQbCfpFSsnUy4+ctc9d51rxXXJowI7iIJ tfJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710093934; x=1710698734; h=subject:to:from:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=xyhFiq7GPaERc2jxPutFzSnFDqIpfQvDRASdUabZmPc=; b=pnTiPOClLIFrSdSEZydB1ULft+lm/+6FqpocB/FB28r39xjtLsnNI7FOVccAyl8dq2 FomUyzGVD86aK8nkmETkUJmhUDXg8A4Q2KqUGH+TdPft7GvCZ/4iokOD3xUwIBGD73pZ foZUnqbOHAMBPYbejrdWoFxpbFo0YwHpVeWUEWfxAP/Fm/RZN1iRG+7VlJ/zkRHKZxHU aaJXJCyLq8YhrKNWZutiUIhklorrYy8l8oOdPJJwfXfC0LcGVWTmlUyTxHvGvfksnZ17 lx9bpyom4w+j/cev0eI2ixTNlAbgVLQaJI4aDqxBdfXRtT/HeV97NsKB6RjSDgFHGRpx T8mg== X-Gm-Message-State: AOJu0YyeFb04zHMxdtpmbTMk0HZ9zonJvvHFyFpa2u/lZm0e9HYyW4Bf K28nbNXw6V5+yz9fUnkxNkg6x5ndnbNNUfT26o1iFdaN3f73fyrOPaZB8nKD1PnSWTS40fi+6Rz s X-Google-Smtp-Source: AGHT+IEBfM2+FJE86kZ6ueOiRmABUHL/HG9XDJKw5A46e8p2Ovk7mHoUQDoOr2oiyhfU3+TvC2h4hw== X-Received: by 2002:a9d:6946:0:b0:6e4:dff7:75dd with SMTP id p6-20020a9d6946000000b006e4dff775ddmr4053234oto.17.1710093934067; Sun, 10 Mar 2024 11:05:34 -0700 (PDT) Received: from [172.31.1.103] ([172.56.168.149]) by smtp.gmail.com with ESMTPSA id ex20-20020a056830731400b006e519a20a2esm728336otb.22.2024.03.10.11.05.32 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 10 Mar 2024 11:05:33 -0700 (PDT) Message-ID: <9603a093-a2e4-4ae9-b9c7-38b6effdbe20@ventanamicro.com> Date: Sun, 10 Mar 2024 12:05:31 -0600 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Beta Content-Language: en-US From: Jeff Law To: "gcc-patches@gcc.gnu.org" Subject: [committed] [PR tree-optimization/110199] Simplify MIN/MAX more often X-Spam-Status: No, score=-8.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, RCVD_IN_SBL_CSS, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org So as I mentioned in the BZ, the case of t = MIN_EXPR (A, B) where we know something about the relationship between A and B can be trivially handled by some existing code in DOM. That existing code would simplify when A == B. But by testing GE and LE instead of EQ we can cover more cases with minimal effort. When applicable the MIN/MAX turns into a simple copy. I made one other change. We have other binary operations that we simplify when we know something about the relationship between the operands. That code was not canonicalizing the order of operands when building the expression to lookup in the hash tables to discover that relationship. Since those paths are only testing for equality, we can trivially reverse them and not have to worry about changing codes or anything like that. So extremely safe and avoids having to come back and fix that code to match the MIN_EXPR/MAX_EXPR case later. Bootstrapped on x86 and also tested on the crosses. I briefly thought there was an sh regression, but that was actually the recent fwprop changes twiddling code generation for one test. Pushed to the trunk. Jeff commit 8fe27ed193d60f6cd8b34761858a720c95eabbdb Author: jlaw Date: Sun Mar 10 11:58:00 2024 -0600 [committed] [PR tree-optimization/110199] Simplify MIN/MAX more often So as I mentioned in the BZ, the case of t = MIN_EXPR (A, B) where we know something about the relationship between A and B can be trivially handled by some existing code in DOM. That existing code would simplify when A == B. But by testing GE and LE instead of EQ we can cover more cases with minimal effort. When applicable the MIN/MAX turns into a simple copy. I made one other change. We have other binary operations that we simplify when we know something about the relationship between the operands. That code was not canonicalizing the order of operands when building the expression to lookup in the hash tables to discover that relationship. Since those paths are only testing for equality, we can trivially reverse them and not have to worry about changing codes or anything like that. So extremely safe and avoids having to come back and fix that code to match the MIN_EXPR/MAX_EXPR case later. Bootstrapped on x86 and also tested on the crosses. I briefly thought there was an sh regression, but that was actually the recent fwprop changes twiddling code generation for one test. PR tree-optimization/110199 gcc/ * tree-ssa-scopedtables.cc (avail_exprs_stack::simplify_binary_operation): Generalize handling of MIN_EXPR/MAX_EXPR to allow additional simplifications. Canonicalize comparison operands for other cases. gcc/testsuite * gcc.dg/tree-ssa/minmax-27.c: New test. * gcc.dg/tree-ssa/minmax-28.c: New test. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-27.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-27.c new file mode 100644 index 00000000000..4b94203b0d0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-27.c @@ -0,0 +1,118 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-dom2" } */ + + +int min1(int a, int b) +{ + if (a <= b) + return a < b ? a : b; + return 0; +} + +int min2(int a, int b) +{ + if (a <= b) + return a > b ? b : a; + return 0; +} + +int min3(int a, int b) +{ + if (a < b) + return a < b ? a : b; + return 0; +} + +int min4(int a, int b) +{ + if (a < b) + return a > b ? b : a; + return 0; +} + +int min5(int a, int b) +{ + if (a <= b) + return a <= b ? a : b; + return 0; +} + +int min6(int a, int b) +{ + if (a <= b) + return a >= b ? b : a; + return 0; +} + +int min7(int a, int b) +{ + if (a < b) + return a <= b ? a : b; + return 0; +} + +int min8(int a, int b) +{ + if (b > a) + return a >= b ? b : a; + return 0; +} + +int min9(int a, int b) +{ + if (b >= a) + return a < b ? a : b; + return 0; +} + +int min10(int a, int b) +{ + if (b >= a) + return a > b ? b : a; + return 0; +} + +int min11(int a, int b) +{ + if (b > a) + return a < b ? a : b; + return 0; +} + +int min12(int a, int b) +{ + if (b > a) + return a > b ? b : a; + return 0; +} + +int min13(int a, int b) +{ + if (b >= a) + return a <= b ? a : b; + return 0; +} + +int min14(int a, int b) +{ + if (b >= a) + return a >= b ? b : a; + return 0; +} + +int min15(int a, int b) +{ + if (b > a) + return a <= b ? a : b; + return 0; +} + +int min16(int a, int b) +{ + if (b > a) + return a >= b ? b : a; + return 0; +} + +/* { dg-final { scan-tree-dump-not "MIN_EXPR" "dom2" } } */ + diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-28.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-28.c new file mode 100644 index 00000000000..732126d7449 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-28.c @@ -0,0 +1,117 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-dom2" } */ + +int max1(int a, int b) +{ + if (a <= b) + return a < b ? b : a; + return 0; +} + +int max2(int a, int b) +{ + if (a <= b) + return a > b ? a : b; + return 0; +} + +int max3(int a, int b) +{ + if (a < b) + return a < b ? b : a; + return 0; +} + +int max4(int a, int b) +{ + if (a < b) + return a > b ? a : b; + return 0; +} + +int max5(int a, int b) +{ + if (a <= b) + return a <= b ? b : a; + return 0; +} + +int max6(int a, int b) +{ + if (a <= b) + return a >= b ? a : b; + return 0; +} + +int max7(int a, int b) +{ + if (a < b) + return a <= b ? b : a; + return 0; +} + +int max8(int a, int b) +{ + if (b > a) + return a >= b ? a : b; + return 0; +} + +int max9(int a, int b) +{ + if (b >= a) + return a < b ? b : a; + return 0; +} + +int max10(int a, int b) +{ + if (b >= a) + return a > b ? a : b; + return 0; +} + +int max11(int a, int b) +{ + if (b > a) + return a < b ? b : a; + return 0; +} + +int max12(int a, int b) +{ + if (b > a) + return a > b ? a : b; + return 0; +} + +int max13(int a, int b) +{ + if (b >= a) + return a <= b ? b : a; + return 0; +} + +int max14(int a, int b) +{ + if (b >= a) + return a >= b ? a : b; + return 0; +} + +int max15(int a, int b) +{ + if (b > a) + return a <= b ? b : a; + return 0; +} + +int max16(int a, int b) +{ + if (b > a) + return a >= b ? a : b; + return 0; +} + +/* { dg-final { scan-tree-dump-not "MAX_EXPR" "dom2" } } */ + diff --git a/gcc/tree-ssa-scopedtables.cc b/gcc/tree-ssa-scopedtables.cc index e53dfd445ea..c367d37fa9b 100644 --- a/gcc/tree-ssa-scopedtables.cc +++ b/gcc/tree-ssa-scopedtables.cc @@ -127,10 +127,49 @@ avail_exprs_stack::simplify_binary_operation (gimple *stmt, switch (code) { - /* For these cases, if we know the operands - are equal, then we know the result. */ + /* For these cases, if we know some relationships + between the operands, then we can simplify. */ case MIN_EXPR: case MAX_EXPR: + { + /* Build a simple equality expr and query the hash table + for it. */ + struct hashable_expr expr; + expr.type = boolean_type_node; + expr.kind = EXPR_BINARY; + expr.ops.binary.op = LE_EXPR; + tree rhs1 = gimple_assign_rhs1 (stmt); + tree rhs2 = gimple_assign_rhs2 (stmt); + if (tree_swap_operands_p (rhs1, rhs2)) + std::swap (rhs1, rhs2); + expr.ops.binary.opnd0 = rhs1; + expr.ops.binary.opnd1 = rhs2; + class expr_hash_elt element2 (&expr, NULL_TREE); + expr_hash_elt **slot + = m_avail_exprs->find_slot (&element2, NO_INSERT); + + /* If the query was successful and returned a nonzero + result, then we know the result of the MIN/MAX, even + though it is not a constant value. */ + if (slot && *slot && integer_onep ((*slot)->lhs ())) + return code == MIN_EXPR ? rhs1 : rhs2; + + /* Try again, this time with GE_EXPR. */ + expr.ops.binary.op = GE_EXPR; + class expr_hash_elt element3 (&expr, NULL_TREE); + slot = m_avail_exprs->find_slot (&element3, NO_INSERT); + + /* If the query was successful and returned a nonzero + result, then we know the result of the MIN/MAX, even + though it is not a constant value. */ + if (slot && *slot && integer_onep ((*slot)->lhs ())) + return code == MIN_EXPR ? rhs2 : rhs1; + + break; + } + + /* For these cases, if we know the operands + are equal, then we know the result. */ case BIT_IOR_EXPR: case BIT_AND_EXPR: case BIT_XOR_EXPR: @@ -151,8 +190,12 @@ avail_exprs_stack::simplify_binary_operation (gimple *stmt, expr.type = boolean_type_node; expr.kind = EXPR_BINARY; expr.ops.binary.op = EQ_EXPR; - expr.ops.binary.opnd0 = gimple_assign_rhs1 (stmt); - expr.ops.binary.opnd1 = gimple_assign_rhs2 (stmt); + tree rhs1 = gimple_assign_rhs1 (stmt); + tree rhs2 = gimple_assign_rhs2 (stmt); + if (tree_swap_operands_p (rhs1, rhs2)) + std::swap (rhs1, rhs2); + expr.ops.binary.opnd0 = rhs1; + expr.ops.binary.opnd1 = rhs2; class expr_hash_elt element2 (&expr, NULL_TREE); expr_hash_elt **slot = m_avail_exprs->find_slot (&element2, NO_INSERT); @@ -168,8 +211,6 @@ avail_exprs_stack::simplify_binary_operation (gimple *stmt, { switch (code) { - case MIN_EXPR: - case MAX_EXPR: case BIT_IOR_EXPR: case BIT_AND_EXPR: return gimple_assign_rhs1 (stmt);