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: 159379 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp1768448vqb; Sun, 29 Oct 2023 09:41:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGNudHc8I7WoFnqWFnsU7tJvHC8d1BSott6bQj9CrSiBoF4o0qh5lDDBZyggu8eTAPpqkLv X-Received: by 2002:a05:620a:2445:b0:774:2bdc:aa4 with SMTP id h5-20020a05620a244500b007742bdc0aa4mr9313541qkn.54.1698597695641; Sun, 29 Oct 2023 09:41:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698597695; cv=pass; d=google.com; s=arc-20160816; b=leFRTpyTW4reJVpCmHWwNnWrZoUlrMEfq/vIthBkCAbC+NrAIagdHFJOoIlkDrxNZQ UXACfbB1dHi0xAachpcnI8e/ujEFudd9wEL8C8JtFv3Y+W9UdG+W562yRTRKKcxElyqY LPVvUBkdcCfjhRhK4FinaOeY2qIpyIqa32LdcP0BI63p3P3Hyl5Wfyln7Pxus9STodd1 XUT06+lERljdixIN14yc5NG1As8vfE13zR9nxrINkwk3JE1AHz6v640DRUhOJqHFuj3H zGBcGaTG0Y2cdHH1b09QAgHJRFOQLR/VXonmE2fLCr8GTcTBetOVilcYiwsdtpcpPEed FNPA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=eQnjkgw7GXTi1goC99zeT3VjDfLcwDQR6ljC+9t4U2o=; fh=6xfd3IYC8Zt5CA4TsXZHhPmGMhO9cjo3oiHaQc5bSUI=; b=yuqOqtcTRjZCAmv7L9eURseVDXpRDJp5e7eXSAexkGxwzWgu+fos2PgVG2lzqjt2gq z3gcpfMAAx0piB/QFzWLcgoMnpTaSOZ7Vd+WXOOV/8dMhVqCcgQfZZ0AQrWZLd/kFjce REnl1fHGoMcO/Y9hScVorsQlgClaAjUTw/PuTCvtWp9DiPgiqnF80H1ucDO/lnyo4TkP +7Zsif4kWwKHQzT5C+5p2nu9w6U0pOqhhKhZHkaDrCq4fiYTxbUrrI5Dx022hbB9fU9b wbMHQKs545YJoUB8d7i1V+q7J42tMb6TOkqyZ4KPjez7LAGHeFiS5nQUFjQljEXt9tKP k+ew== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=WPI7CXau; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id c2-20020a05620a268200b00776ff8c1216si4221069qkp.772.2023.10.29.09.41.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Oct 2023 09:41:35 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=WPI7CXau; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 02D12387546A for ; Sun, 29 Oct 2023 16:41:32 +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-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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781108777616441514 X-GMAIL-MSGID: 1781108777616441514 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" } } */