From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by sourceware.org (Postfix) with ESMTPS id F32783858D37 for ; Thu, 2 May 2024 13:08:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F32783858D37 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=vrull.eu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=vrull.eu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F32783858D37 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1034 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714655332; cv=none; b=ZRmcskQKo7FA/9ZkdUL6QP8dNlRrcWHoBjLRcy5NQ45rLM7iiWBGqMzN5vqi/nX28g33aVhB2/cbvvgluwSPvN7X6kcv39NG1qup3UgQophIcxIFRnDaum4RuTqS+DnA8O/mI3HPz8XKHAdwG5xmcTLE7AxgV8UlvdnAcMF1jRM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714655332; c=relaxed/simple; bh=0YyM5iT5tDWhRUzdZkRdYB1sgJxdWN6x83KNwbFiSz4=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=rD3l/2SzY0IZrhz3DuetlpmBFIKuQpRq3smdojOkXtijOyUxyE1abfxfKgEa3Q1t54JwLsGIycbUgdygj+ZZdHus7pMBK8mO/23iPNdIn9aYVpnElFw25cw4ZA3rim2hrwplasIg6PcJuQVQBDkxPcG/kt+gKwNMD8oOhOt5RdM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pj1-x1034.google.com with SMTP id 98e67ed59e1d1-2b338460546so1039506a91.1 for ; Thu, 02 May 2024 06:08:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull.eu; s=google; t=1714655329; x=1715260129; 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=y97paPj5mZRrFNJLiZWdDNHXUyY3FMmHGMVUcWf5ewI=; b=QN0VXpF8jUNtRqk39lWvFdy25pJw8aUmwHFfleY5bsfQwBIGasuLf/ODuwMTcszekQ a442X4D03UvpySrVOuIEH23WV5KRA6G64D34U2M/pM02VYLlXmmXvMUy9zZAa3HM3NdO NaWVbYcx6dHq0nczyoJftFDWQXpImwUMNyEbSzNflJBBBQ6YwYtFwCzgVr96zmNiQlwX Ezjqaz9ySzeujADJQJm/zc34J3Bdw5A447Ghh5Ro6LWFDkcwt3Z8uF7MKRIP/7SDyJVJ mZS13s99QRLeP9bSzMlFi63CkKsGQUflPmme4UeG+1wwmzSodcxe0Z0gvTnicQkAr6cM q3QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714655329; x=1715260129; 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=y97paPj5mZRrFNJLiZWdDNHXUyY3FMmHGMVUcWf5ewI=; b=LQ5clRX7vO2fNJQveVReRsj70L3c1K2rmTMAuworbPA76uUSWRyUQ0wqDAtaQUYtHy dm6Adihl2KnjFSs3DV2IHgnRS1H8vp47lWm39myhzcawwbi/rqkK7To6sfZ91Pm9j+bm rO/Xt/4oMfTVVW1KsT/m0Rv6ee69FTHtwyhu6cpMUq7dncxfDXPd39iHLuvtI5P9Brtu p8glTFoU/BK/p//VCdfp/W83/53xkCPCOsxre3PY4L9dd+6rbLdHQSpvJ0XaKNSpO+CI EmV4U04vHxVzJoDAMnKRB/B8y/dfVYv2corADiB28vnA3klhzBa13y4z7bNfaH/7rqR8 DROQ== X-Gm-Message-State: AOJu0YwAg6aj9Vzc00Cgz4tK5Tz19LLp9hwEgBUnZC8fVeoGcQb9V5gk jiuwpvmETXWHBqEJu0IKiJUAyWGkf8D6yfG8/YeGNu53m58uR+BPXNc5sE26vz6XBQ2AXo/A8aB 2KGhyOmtkot+qjT0TJ6sDq+F5/4bnBIgrtrjTlg== X-Google-Smtp-Source: AGHT+IGzuVYj41RQWqsbmBxB5vAL5OKrX5baDSRo2WuCMCIU7a+mOK/wVGylolWmMr94PxBBCecUV5uNM8FsOKpuoy0= X-Received: by 2002:a17:90a:c903:b0:2b3:a444:3960 with SMTP id v3-20020a17090ac90300b002b3a4443960mr2102646pjt.26.1714655327878; Thu, 02 May 2024 06:08:47 -0700 (PDT) MIME-Version: 1.0 References: <20240423103336.4024491-1-manolis.tsamis@vrull.eu> <4727nn42-pp9r-qo07-5151-oos9n49s94p4@fhfr.qr> In-Reply-To: <4727nn42-pp9r-qo07-5151-oos9n49s94p4@fhfr.qr> From: Manolis Tsamis Date: Thu, 2 May 2024 16:08:11 +0300 Message-ID: Subject: Re: [PATCH] MATCH: Maybe expand (T)(A + C1) * C2 and (T)(A + C1) * C2 + C3 [PR109393] To: Richard Biener Cc: gcc-patches@gcc.gnu.org, Jiangning Liu , Philipp Tomsich , Andrew Pinski Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-9.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 List-Id: On Thu, May 2, 2024 at 4:00=E2=80=AFPM Richard Biener w= rote: > > On Tue, 23 Apr 2024, Manolis Tsamis wrote: > > > The original motivation for this pattern was that the following functio= n does > > not fold to 'return 1': > > > > int foo(int *a, int j) > > { > > int k =3D j - 1; > > return a[j - 1] =3D=3D a[k]; > > } > > > > The expression ((unsigned long) (X +- C1) * C2) appears frequently as p= art of > > address calculations (e.g. arrays). These patterns help fold and simpli= fy more > > expressions. > > > > PR tree-optimization/109393 > > > > gcc/ChangeLog: > > > > * match.pd: Add new patterns for ((T)(A +- CST1)) * CST2 and > > ((T)(A +- CST1)) * CST2 + CST3. > > > > gcc/testsuite/ChangeLog: > > > > * gcc.dg/pr109393.c: New test. > > > > Signed-off-by: Manolis Tsamis > > --- > > > > gcc/match.pd | 30 ++++++++++++++++++++++++++++++ > > gcc/testsuite/gcc.dg/pr109393.c | 16 ++++++++++++++++ > > 2 files changed, 46 insertions(+) > > create mode 100644 gcc/testsuite/gcc.dg/pr109393.c > > > > diff --git a/gcc/match.pd b/gcc/match.pd > > index d401e7503e6..13c828ba70d 100644 > > --- a/gcc/match.pd > > +++ b/gcc/match.pd > > @@ -3650,6 +3650,36 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > > (plus (convert @0) (op @2 (convert @1)))))) > > #endif > > > > +/* ((T)(A + CST1)) * CST2 + CST3 > > + -> ((T)(A) * CST2) + ((T)CST1 * CST2 + CST3) > > + Where (A + CST1) doesn't need to have a single use. */ > > +#if GIMPLE > > + (for op (plus minus) > > + (simplify > > + (plus (mult (convert:s (op @0 INTEGER_CST@1)) INTEGER_CST@2) INTEG= ER_CST@3) > > + (if (TREE_CODE (TREE_TYPE (@0)) =3D=3D INTEGER_TYPE > > + && TREE_CODE (type) =3D=3D INTEGER_TYPE > > + && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0)) > > + && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) > > + && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)) > > + && TYPE_OVERFLOW_WRAPS (type)) > > + (op (mult @2 (convert @0)) (plus (mult @2 (convert @1)) @3))))) > > +#endif > > + > > +/* ((T)(A + CST1)) * CST2 -> ((T)(A) * CST2) + ((T)CST1 * CST2) */ > > +#if GIMPLE > > + (for op (plus minus) > > + (simplify > > + (mult (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2) > > + (if (TREE_CODE (TREE_TYPE (@0)) =3D=3D INTEGER_TYPE > > Please use INTEGRAL_TYPE_P > > > + && TREE_CODE (type) =3D=3D INTEGER_TYPE > > + && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0)) > > + && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) > > + && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)) > > + && TYPE_OVERFLOW_WRAPS (type)) > > + (op (mult @2 (convert @0)) (mult @2 (convert @1)))))) > > (mult @2 (convert @0)) is non-canonical for no good reason if @0 > isn't constant - constant should be 2nd, please swap operands here. > > > +#endif > > The first pattern is an extension of the second, why's the first > necessary at all? The add of CST3 is unchanged (OK, you seem to > associate here, but that's again a different thing). > > I'd say the 2nd pattern is OK with the above changes but the first > looks redundant. > Hi Richard, Thanks for the comments, I'll fix these. The difference is that the second uses op:s while the first uses just op. In the second case if A + CST1 has other uses expanding the pattern may not be a good idea but in the first case it always is because we know + CST1 * CST2 will merge with + CST3. Thanks, Manolis > Thanks, > Richard. > > > + > > /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplif= ied > > to a simple value. */ > > (for op (plus minus) > > diff --git a/gcc/testsuite/gcc.dg/pr109393.c b/gcc/testsuite/gcc.dg/pr1= 09393.c > > new file mode 100644 > > index 00000000000..e9051273672 > > --- /dev/null > > +++ b/gcc/testsuite/gcc.dg/pr109393.c > > @@ -0,0 +1,16 @@ > > +/* PR tree-optimization/109393 */ > > +/* { dg-do compile } */ > > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > > +/* { dg-final { scan-tree-dump-times "return 1;" 2 "optimized" } } */ > > + > > +int foo(int *a, int j) > > +{ > > + int k =3D j - 1; > > + return a[j - 1] =3D=3D a[k]; > > +} > > + > > +int bar(int *a, int j) > > +{ > > + int k =3D j - 1; > > + return (&a[j + 1] - 2) =3D=3D &a[k]; > > +} > > > > -- > Richard Biener > SUSE Software Solutions Germany GmbH, > Frankenstrasse 146, 90461 Nuernberg, Germany; > GF: Ivo Totev, Andrew McDonald, Werner Knoblich; (HRB 36809, AG Nuernberg= )