From patchwork Sun Oct 29 16:40:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1856759 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=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=WPI7CXau; 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 4SJMc13JcBz1yQ4 for ; Mon, 30 Oct 2023 03:41:21 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 80786387103F for ; Sun, 29 Oct 2023 16:41:19 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oi1-x22d.google.com (mail-oi1-x22d.google.com [IPv6:2607:f8b0:4864:20::22d]) by sourceware.org (Postfix) with ESMTPS id 572F6385276F for ; Sun, 29 Oct 2023 16:40:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 572F6385276F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 572F6385276F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::22d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698597658; cv=none; b=ffgqUqh+Jw+6xBY0BAeIQHjXEmvA8MCt96tJiBTBsEvePW04t/luXCgJAKNLN1DMcOXyJ/wwM9qLAQB/FHcql0m83116VraxwWI1fU3rBeJo2nxsLO1tSnNgB5RSDX+BqMtggOCzIXZsZg9yfdqPyyOV36coyaP/nSuZ9VRtv04= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698597658; c=relaxed/simple; bh=nd1DK2TugqSHUdvWFR/tpqiukW3g5WsCqFpnIViTV04=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Gan5RpsbmnryUkIGZ2PcvbQ0WTOQPSf4lE44F9A4dR9L+5oo3FcJtRzQ2+L5GzRw3S9hVH1h54WSOtMh3y1k0/Jx8uqZRNaNKXfO7eeOgDNKJS5r8koxeeuBh5F+iiPkQ7LcygX7Vdsmq5XHsFZKpVJXsLgeA9BIi4QcOQEsgS8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oi1-x22d.google.com with SMTP id 5614622812f47-3b2e44c7941so2644323b6e.2 for ; Sun, 29 Oct 2023 09:40:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698597656; x=1699202456; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eQnjkgw7GXTi1goC99zeT3VjDfLcwDQR6ljC+9t4U2o=; b=WPI7CXaufuQlaf01jk4zWyeGh9G4QqO0gvlgV5fC9hkBDFM1zTZGIjVxBTLb1qlHn9 d8CEWjrZAVFzd+Lhcy3qn0977th/Rx67IpxgsVQOM+QmgA4lxGtIzEkDepGYvYQpeX8k fF9UQc1s/61bjo6V3SQpXA+yBb1GHm9afgHjglKcyWNZzM51dH9MyXdu5tYjAf2dZ0YF y9C8qn8YtE++0DfL+edZqPDdT7msRfR7cAfWSH7KR3xCFXhLnCf2wyVj6anqqnbqMKLC /fxfTwz6XrQWaIo2On0vvlp7DJdTRMLJcEE3YaRnFfv3gx3UAGe0lo5grpB9/UkGvtt7 ucDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698597656; x=1699202456; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eQnjkgw7GXTi1goC99zeT3VjDfLcwDQR6ljC+9t4U2o=; b=rvZtnfVSKj3WExjBZwkpV8SUsRRomP4KgL1unczHZ/YLWo2+jW8fKbYqKUecK/0Jq9 5XCFGf87kIUU2xRs9Pf7qw6oJoJToPV/gEADzY3YXw1rktIIB7CPGLRJfZmroCCUWCeZ Y2LCR/AQmkY2oal9QVMBRcO1CU9bJaViHOiPG3jL6N1KleCk6S5HVrwd6r11+PP1zHeq ty9MZcppo09epZSbrhhKJY8CkQRbVmdM4StVGIsRGwrMFTOjV2H2PrfDSeO0hmIR1/pC kvgi49Ah6BZV24cxTFZVa3BrnSjA47qLPbvIX7BGB4RRMDVZLKBKEEMgdbHlxpD6kT7E z08A== X-Gm-Message-State: AOJu0YzKDo27hxvU3GMjvSfNl8FokWYM8QQExsxYzCJO9TMxKiQ4JC7z /ZedjNXvl/FbOjuVy35JEs2QqI6pZcs= X-Google-Smtp-Source: AGHT+IE/eAcOExv76tFW+nwL+F4lLt9vCMNMvGpTBaA7W5i+yaw9p/AIqPbmmzmNMksGCxDnSf/B6A== X-Received: by 2002:a05:6808:191c:b0:3b2:e219:5518 with SMTP id bf28-20020a056808191c00b003b2e2195518mr9567637oib.11.1698597656263; Sun, 29 Oct 2023 09:40:56 -0700 (PDT) Received: from xeond2.wrightpinski.org ([98.97.118.94]) by smtp.gmail.com with ESMTPSA id w27-20020a63af1b000000b005acd5d7e11bsm3591005pge.35.2023.10.29.09.40.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Oct 2023 09:40:55 -0700 (PDT) From: Andrew Pinski To: gcc-patches@gcc.gnu.org Cc: Andrew Pinski Subject: [PATCH 3/3] MATCH: Add some more value_replacement simplifications to match Date: Sun, 29 Oct 2023 09:40:49 -0700 Message-Id: <20231029164049.994454-4-pinskia@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231029164049.994454-1-pinskia@gmail.com> References: <20231029164049.994454-1-pinskia@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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.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 This moves a few more value_replacements simplifications to match. /* a == 1 ? b : a * b -> a * b */ /* a == 1 ? b : b / a -> b / a */ /* a == -1 ? b : a & b -> a & b */ Also adds a testcase to show can we catch these where value_replacement would not (but other passes would). Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: * match.pd (`a == 1 ? b : a OP b`): New pattern. (`a == -1 ? b : a & b`): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/phi-opt-value-4.c: New test. --- gcc/match.pd | 18 ++++++++++ .../gcc.dg/tree-ssa/phi-opt-value-4.c | 36 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-4.c diff --git a/gcc/match.pd b/gcc/match.pd index 9bc945ccada..6efa97cc6ae 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4159,6 +4159,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (cond (eq @0 integer_zerop) @1 (op@2 @1 @0)) @2)) +/* a == 1 ? b : b / a -> b / a */ +(for op (trunc_div ceil_div floor_div round_div exact_div) + (simplify + (cond (eq @0 integer_onep) @1 (op@2 @1 @0)) + @2)) + +/* a == 1 ? b : a * b -> a * b */ +(for op (mult) + (simplify + (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0)) + @2)) + +/* a == -1 ? b : a & b -> a & b */ +(for op (bit_and) + (simplify + (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0)) + @2)) + /* PTR == 0 ? 0 : &PTR->field -> PTR if field offset was 0. */ (simplify (cond (eq @0 integer_zerop) integer_zerop ADDR_EXPR@1) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-4.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-4.c new file mode 100644 index 00000000000..380082cb463 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-value-4.c @@ -0,0 +1,36 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-fre3 -fdump-tree-phiopt1 -fdump-tree-optimized" } */ + +[[gnu::const]] +int constcall(int); + +int fdiv(int a, int b) +{ + int c = b/a; + int t = constcall(c); + int d; + if (a == 1) d = b; else d = c; + return constcall(d) + t; +} +int fmult(int a, int b) +{ + int c = b*a; + int t = constcall(c); + int d; + if (a == 1) d = b; else d = c; + return constcall(d) + t; +} +int fand(int a, int b) +{ + int c = b&a; + int t = constcall(c); + int d; + if (a == -1) d = b; else d = c; + return constcall(d) + t; +} + +/* Should be able to optimize away the if statements in phiopt1. */ +/* { dg-final { scan-tree-dump-not "if " "phiopt1" } } */ +/* fre3 should be optimize each function to just `return constcall(a OP b) * 2;`. */ +/* { dg-final { scan-tree-dump-times "constcall " 3 "fre3" } } */ +/* { dg-final { scan-tree-dump-times "constcall " 3 "optimized" } } */