From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-x229.google.com (mail-lj1-x229.google.com [IPv6:2a00:1450:4864:20::229]) by sourceware.org (Postfix) with ESMTPS id 0302C3858C5F for ; Tue, 25 Jul 2023 07:02:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0302C3858C5F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lj1-x229.google.com with SMTP id 38308e7fff4ca-2b95efb9d89so75220741fa.0 for ; Tue, 25 Jul 2023 00:02:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690268575; x=1690873375; 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=r2X5ztyH0vOjNyfHZjoKoSMZsMelQJboO6LvFNytijY=; b=a1WjULBHBsBv56LBEnrlrsdT8UslTFuA93c4Kt5xAAPEJHj9Usp7DnWtRTzqmRAvGZ HdzZ7kfCLAS97atD21c2TCnIm6ivAO4h9/MPmbKo24O8+EMOOS5J3HJ8lHHUqTPrzFoe mPJFOifiUnNHqRVzs7azp7DZeWm2UxyRYkzWY7WezLEXb50VLhBGMbDUFuIkOrP4VuIp 2Rxp04gR76GxaLwLNnNXovZKFSmU94T6pbTvFd+HnWPsUe//dgbWrgP3PBroossbZf8i 421LnXlylfaYp0IMKmk4I7nsihbQWy1ciwHmvrn7grZGLqk1EohSkNHGYdImlj/P/dO+ ruIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690268575; x=1690873375; 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=r2X5ztyH0vOjNyfHZjoKoSMZsMelQJboO6LvFNytijY=; b=UpXWGbHb3IGqicLXnqG7QnhhK4WaI+h2yi4O6beUTaT3ABARxZLdzTRgcVkyUcuiT5 IIrq8XhPJRyTmEtAPheSmFaMckPqASBQr0yNV7qycNpdqDci+fGaAkIJSDxuJox82SEe U5EyOfJ35WgrlICV/icfw0P4TxDFe6fIGb3O3ZV6TR760Vgucz4rp7Zl9vY68qMFlOc0 096pmWKcgHs8uwJyFG2823suW2IcHGJAfav/+i7aJWvGItzvTYakRb4FEki1dM5lgYqz jTo9y8nNL98H2LI1tTu7xteLkYIlzAUs4Lf+zHGN/FLv3tL89pxCrMh8zoibeMCobP7H ABgg== X-Gm-Message-State: ABy/qLYGtygxN+ORn1788OOPiBWhbfcGXZuc8TimIwP4WMnPIJaOm4FX 7G0669KadXYnkPqHg0WDYJb3qBWVKKXXPgg2pBsI4fLW X-Google-Smtp-Source: APBJJlFH3WZkiW6Piia5jy72XwlqXqH1icRcBev/RTuoiNt/rf8+zPvUqen15a87yWdr92J4uxptYBr9h0u+ohTHoBk= X-Received: by 2002:a2e:9283:0:b0:2b6:eeb3:da94 with SMTP id d3-20020a2e9283000000b002b6eeb3da94mr7359376ljh.22.1690268574934; Tue, 25 Jul 2023 00:02:54 -0700 (PDT) MIME-Version: 1.0 References: <20230724234851.1736326-1-apinski@marvell.com> In-Reply-To: <20230724234851.1736326-1-apinski@marvell.com> From: Richard Biener Date: Tue, 25 Jul 2023 09:02:11 +0200 Message-ID: Subject: Re: [PATCH] Fix PR 93044: extra cast is not removed To: Andrew Pinski Cc: gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.4 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,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 List-Id: On Tue, Jul 25, 2023 at 1:49=E2=80=AFAM Andrew Pinski via Gcc-patches wrote: > > In this case we are not removing convert to a bigger size > back to the same size (or smaller) if signedness does not > match. > For an example: > ``` > signed char _1; > ... > _1 =3D *a_4(D); > b_5 =3D (short unsigned int) _1; > _2 =3D (unsigned char) b_5; > ``` > The inner cast is not needed and can be removed but was not. > The match pattern for removing the extra cast is overly > complex so decided to add a new case for rather than trying > to modify the current if statement here. > > OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. > > gcc/ChangeLog: > > * match.pd (nested int casts): A truncation (to the same size or = smaller) > can always remove the inner cast. > > gcc/testsuite/ChangeLog: > > * gcc.dg/tree-ssa/cast-1.c: New test. > * gcc.dg/tree-ssa/cast-2.c: New test. > --- > gcc/match.pd | 10 ++++++++++ > gcc/testsuite/gcc.dg/tree-ssa/cast-1.c | 12 ++++++++++++ > gcc/testsuite/gcc.dg/tree-ssa/cast-2.c | 12 ++++++++++++ > 3 files changed, 34 insertions(+) > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cast-1.c > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cast-2.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index bfd15d6cd4a..bc8a3ab17eb 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -4257,6 +4257,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > && ! (final_ptr && inside_prec !=3D inter_prec)) > (ocvt @0)) > > + /* `(signed:M)(signed:N)(signed:O)` > + can be convert it to `(signed:M)a` Better say (outer:M)(inter:N) a:O can be converted to (outer:M) a since as you say below the signedness doesn't really matter and this would then also mention 'a' > + if M <=3D O && N >=3D O. No matter what signedness of the cast, of the casts OK with those comment changes. Thanks, Richard. > + as the final is either a truncation from the original or just > + a sign change of the type. */ > + (if (inside_int && inter_int && final_int > + && final_prec <=3D inside_prec > + && inter_prec >=3D inside_prec) > + (convert @0)) > + > /* A truncation to an unsigned type (a zero-extension) should be > canonicalized as bitwise and of a mask. */ > (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. = */ > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cast-1.c b/gcc/testsuite/gcc.d= g/tree-ssa/cast-1.c > new file mode 100644 > index 00000000000..0f33ab58b3e > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/cast-1.c > @@ -0,0 +1,12 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O1 -fdump-tree-optimized" } */ > + > + > +void f(signed char *a, unsigned char *c) > +{ > + unsigned short b =3D *a; > + *c =3D ((unsigned char)b); > +} > + > + > +/* { dg-final { scan-tree-dump-not "\\(short unsigned int\\)" "optimized= "} } */ > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cast-2.c b/gcc/testsuite/gcc.d= g/tree-ssa/cast-2.c > new file mode 100644 > index 00000000000..d665e924831 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/cast-2.c > @@ -0,0 +1,12 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O1 -fdump-tree-optimized" } */ > + > + > +void f(signed short *a, unsigned char *c) > +{ > + unsigned long b =3D *a; > + *c =3D ((unsigned char)b); > +} > + > + > +/* { dg-final { scan-tree-dump-not "\\(long unsigned int\\)" "optimized"= } } */ > -- > 2.31.1 >