From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by sourceware.org (Postfix) with ESMTPS id A13F9385802F for ; Thu, 26 Oct 2023 12:21:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A13F9385802F 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 A13F9385802F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::436 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698322890; cv=none; b=ANU4skFGyZmXt81Ao0YIDM0d7vbs2+o1T/EoumlxVxOuJAoBOnqFrDyb5TrSAB5bA+4KMD1BGO751xRzMiiJMCb2MVeN3+Pk0t0UXskxcpCffRuSQGHq8K8TyV/GVEVkG1IV/0e3lzhodBXIE0D3mQTZiJq5IK4oZpgajgCUMm4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698322890; c=relaxed/simple; bh=2aDMoH+7JLLJkD5DAwfjC5Yng2cM47RWaKnW/Me1Tfo=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=uTXVKUxyl59f+Y5k+3TIpLFDpFQHW7sPgFIc+X8vd6JyKjTyPWH6Nz35k7iqYe9VyKu4bm96d06ZO0rnNn1Ky+9qqXZipkFdBxGQ7vIbsyZ1btMJSA5NWe+C06iSzyKDuve1Pge8bJXAW8LAcodO4Fk4Z9rL/oyD7Zvra3Wh9gs= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-6b9af7d41d2so771108b3a.0 for ; Thu, 26 Oct 2023 05:21:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698322887; x=1698927687; darn=gcc.gnu.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=hW3v/Pq0tGbVlzFu96tUyz39WGLED75uSRxJJybom6I=; b=hJZ2LJcHV14YwweQQk/BRFH1BaUpV60Ykq7+4UwN1gUfH4auePKiGYTbvtPcxltJ2Y JDQ23pII6HREoUU+Z5NVhIuZ0dFjyhzHxc7fgy/bjX853qzo9zAXUtaPXrO/3QrxBFeR 8xg8sYtJ9gfXpf774EkE00XiJolWF6hQpRdVurF9+IneBLud8Sw8A6X2bWjSR7REjjac oJFj90m1IOdOEdaMFIIUSylWxbOZF/QRd7ye2zLXk+gFcc1256zGpCDtTGQ35HCXSp4j 9Bpb/Q18dn93nZIwf6XIX/TdL6wuMOFj6IEnOZOy4IfumDCCqseRGoyGcrjYWCBEBjXN pHUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698322887; x=1698927687; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hW3v/Pq0tGbVlzFu96tUyz39WGLED75uSRxJJybom6I=; b=pd+J7hV52hKtSp52p/pUhGiC9rxwoIXIJfb9MRTjJuOhXSosjAG3mY7BIjB7Vz0g7U VDY63z+O4ErKS5NidiOrUIgKksoUIFjydtB1JuiADv7R77HZVzufa1gGfg+uFNnJNMEA Llm5oNiNwaE3lACK9M+exp9A6dYFA9tB6icoCY2ymxjpnTJQ9StChCeMi0z6hWAb0uRV m/z1ZICeHd25+ej7N9xxhWVglY0ylujmzov9bvHSuAQf0W24QdelOL12PQjUDl6VMFfo wGnaK3qCQS2UqIrs4oE03FSccYMPBoyuVo0I0nxPsy8jR+aCvslc8RZi65vRMTdmdV+n k58A== X-Gm-Message-State: AOJu0YwjXEb+YJ7BYsbKVA/MPiWoJMB9dIRKKXburzmlO9AFAW7KwapD G3Snoai6EdWZbFzpU/6Hwqol0ojegFUDGciwpPRMZU+1 X-Google-Smtp-Source: AGHT+IFYzbkeFWVEO9RsmMk1ttt86ExFwD82tkQgQLg2yi4y8KKx2uMliy9tW+L+/vM48RBnd5XL5ZWv4YMAhH7TX6g= X-Received: by 2002:a17:90b:51d0:b0:268:b0b:a084 with SMTP id sf16-20020a17090b51d000b002680b0ba084mr16289039pjb.46.1698322887054; Thu, 26 Oct 2023 05:21:27 -0700 (PDT) MIME-Version: 1.0 References: <20231025033749.3363044-1-pinskia@gmail.com> In-Reply-To: From: Andrew Pinski Date: Thu, 26 Oct 2023 05:21:14 -0700 Message-ID: Subject: Re: [PATCH] match: Simplify `a != C1 ? abs(a) : C2` when C2 == abs(C1) [PR111957] To: Richard Biener Cc: gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,PLING_QUERY,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 List-Id: On Thu, Oct 26, 2023 at 2:24=E2=80=AFAM Richard Biener wrote: > > On Wed, Oct 25, 2023 at 5:37=E2=80=AFAM Andrew Pinski = wrote: > > > > This adds a match pattern for `a !=3D C1 ? abs(a) : C2` which gets simp= lified > > to `abs(a)`. if C1 was originally *_MIN then change it over to use absu= instead > > of abs. > > > > Bootstrapped and tested on x86_64-linux-gnu with no regressions. > > > > PR tree-optimization/111957 > > > > gcc/ChangeLog: > > > > * match.pd (`a !=3D C1 ? abs(a) : C2`): New pattern. > > > > gcc/testsuite/ChangeLog: > > > > * gcc.dg/tree-ssa/phi-opt-40.c: New test. > > --- > > gcc/match.pd | 10 +++++++++ > > gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c | 25 ++++++++++++++++++++++ > > 2 files changed, 35 insertions(+) > > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c > > > > diff --git a/gcc/match.pd b/gcc/match.pd > > index 5df04ebba77..370ee35de52 100644 > > --- a/gcc/match.pd > > +++ b/gcc/match.pd > > @@ -5622,6 +5622,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > > (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2))) > > @3)) > > > > +/* X !=3D C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) =3D=3D C2= . */ > > +(for op (abs absu) > > + (simplify > > + (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2) > > + (if (wi::abs (wi::to_wide (@1)) =3D=3D wi::to_wide (@2)) > > Why not use > > (cond (ne @0 INTEGER_CST@1) (op@3 @0) @1) Because that does not work for: `a !=3D -1 ? abs(a) : -1` We want to match -1 and 1 here (in that order). It just happens 0 and INT_MIN have the same value as their abs and matches but we could expand it to match all others too. Thanks, Andrew > > ? OK with that change. > > Richard. > > > + (if (op !=3D ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1))) > > + (with { tree utype =3D unsigned_type_for (TREE_TYPE (@0)); } > > + (convert (absu:utype @0))) > > + @3)))) > > + > > /* (X + 1) > Y ? -X : 1 simplifies to X >=3D Y ? -X : 1 when > > X is unsigned, as when X + 1 overflows, X is -1, so -X =3D=3D 1. *= / > > (simplify > > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c b/gcc/testsuite= /gcc.dg/tree-ssa/phi-opt-40.c > > new file mode 100644 > > index 00000000000..a9011ce97fb > > --- /dev/null > > +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c > > @@ -0,0 +1,25 @@ > > +/* { dg-do compile } */ > > +/* { dg-options "-O1 -fdump-tree-phiopt" } */ > > +/* PR tree-optimization/111957 */ > > + > > +int f(int a) > > +{ > > + if (a) > > + return a > 0 ? a : -a; > > + return 0; > > +} > > + > > +int f1(int x) > > +{ > > + int intmin =3D (-1u >> 1); > > + intmin =3D -intmin - 1; > > + if (x !=3D intmin) > > + return x > 0 ? x : -x; > > + return intmin; > > +} > > + > > +/* { dg-final { scan-tree-dump-times "if " 1 "phiopt1" } } */ > > +/* { dg-final { scan-tree-dump-not "if " "phiopt2" } } */ > > +/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 2 "phiopt1" } } */ > > +/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 1 "phiopt2" } } */ > > +/* { dg-final { scan-tree-dump-times "ABSU_EXPR <" 1 "phiopt2" } } */ > > -- > > 2.34.1 > >