From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [IPv6:2a00:1450:4864:20::12b]) by sourceware.org (Postfix) with ESMTPS id 2CFBB3858D37 for ; Tue, 24 Oct 2023 07:15:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2CFBB3858D37 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 2CFBB3858D37 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::12b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698131704; cv=none; b=VYmTwa0CzQPy6VA+gRaVZUdzXlDSa8d4UlzNF2aweuI4AJNEXpdD9CewCWSSbjI2t1y1k1NghA5oXLO4CSevo5Oi4NnYR3dCPJPmHHEx/KJWgEjEWKPfv+SSQwGZsqbBEucuF+JZaBRGY2ZwpSXizlQM8aAdsWv9YpKaPJH4y70= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698131704; c=relaxed/simple; bh=V0hAa73/k3/U4/X8xlBar9Qigtvsq46retkLCv/2y3Q=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=BtaD2DBxhSuwIGP1UCfKGABrxjVbHD0lqu9YNgAsfVDZt7wjAAVoS0ax7TcwVg9Ak6DDNrgvYiXX/gsFTHDla1Kx+3dEvb+1lJPe6003LtSIiuyi6CWBs4cuK6bLItqddR6M/gBzmAEC5B+A87pgj/BqmQxHjTBmpodvao9X1Lo= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lf1-x12b.google.com with SMTP id 2adb3069b0e04-5079fa1bbf8so6054677e87.0 for ; Tue, 24 Oct 2023 00:15:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698131700; x=1698736500; 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=9XXFiyAXSuwPPsGo6DSDeIO0+eLEx06ybSYbVIXjTAE=; b=L5Ovvd7aZkFVcTfLX0wpC79Z6EnNWwNxnHV4ztFCzK6+bQr1gaNTpf6vyxyKBFZTmU 4pbzbDCBUfHF73m9bNsdj7ydQpzf8fYfQM0eg+Dxt4nwjMXoTyFr9YWoMQBhgXLmUfyr YoRf6QZxl2kY+cZK7wDnMWGKHhmy8quKSbGlZ04s9QnzhUKbfeZQt7eJsotcxwTkXywZ v0FSmTZhOfneJAppk6xK+R7X3tycD0G3lLlVzVoWHzhFYZZDvyhWozXbyE241GojE4cV I+0VKLVqffd8sxEUND1OV6pqvqQeni59X9kGmf0aY5NXRsKCumYozexSaQ12RSd0Bx29 0ztA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698131700; x=1698736500; 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=9XXFiyAXSuwPPsGo6DSDeIO0+eLEx06ybSYbVIXjTAE=; b=R1rWTcrFQkRvun8LjpaFtFrJ7IAjm9m8Gks52PCDuZ+B/brh6HI+PWHI9vtNzxQ5SE qQpQdqaul1Y6DGeIQd2y+dmOq2dUuk/RJ1CcnVu+PWRPfd+ffoKfyKGTAsq2xf4/WY3y MbHmKtvuYzGz+HcwYp6caFHTdIn4g+OHfgoIcaKmAGd4vqQHQnLn9sxpj9fvdviz5KLo 7IvBllxdYkdomdofCYKAsj4PNJLl1vAj8JHqVANh0R1Tl8+HnxWvkD9UMu4EnQgqBMR7 nZcBCro/ZjWJMmApTsK4XxOgxh5v9S8y4k9rdRQ8kUFhEksqCXzije4H6pgefoxRML7V JOvw== X-Gm-Message-State: AOJu0YyhEYeE5XJNJF7YZQy1LQoM0m6VZW5MwOA2o4NjN+yBmH0EU9sO +usGGE0wehIqXAgKFVMMtiBr+hC4gND+dV5qiEcj59XO X-Google-Smtp-Source: AGHT+IG2YC4a326sfNmfZDa81Tt2649hiDKBpNX9Yvs1oa7NjGAivny9HolKFQETAH3d0uUiLf9gwDGaKYhrPxdaH/A= X-Received: by 2002:a05:6512:51c:b0:503:35b3:aa31 with SMTP id o28-20020a056512051c00b0050335b3aa31mr7870260lfb.27.1698131700431; Tue, 24 Oct 2023 00:15:00 -0700 (PDT) MIME-Version: 1.0 References: <20231024064557.213050-1-pinskia@gmail.com> In-Reply-To: <20231024064557.213050-1-pinskia@gmail.com> From: Richard Biener Date: Tue, 24 Oct 2023 09:14:47 +0200 Message-ID: Subject: Re: [PATCHv2] Improve factor_out_conditional_operation for conversions and constants 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 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, Oct 24, 2023 at 8:45=E2=80=AFAM Andrew Pinski w= rote: > > In the case of a NOP conversion (precisions of the 2 types are equal), > factoring out the conversion can be done even if int_fits_type_p returns > false and even when the conversion is defined by a statement inside the > conditional. Since it is a NOP conversion there is no zero/sign extending > happening which is why it is ok to be done here; we were trying to preven= t > an extra sign/zero extend from being moved away from definition which no-= op > conversions are not. > > Bootstrapped and tested on x86_64-linux-gnu with no regressions. OK. > gcc/ChangeLog: > > PR tree-optimization/104376 > PR tree-optimization/101541 > * tree-ssa-phiopt.cc (factor_out_conditional_operation): > Allow nop conversions even if it is defined by a statement > inside the conditional. > > gcc/testsuite/ChangeLog: > > PR tree-optimization/101541 > * gcc.dg/tree-ssa/phi-opt-39.c: New test. > --- > gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c | 43 ++++++++++++++++++++++ > gcc/tree-ssa-phiopt.cc | 16 ++++++-- > 2 files changed, 56 insertions(+), 3 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c > > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c b/gcc/testsuite/g= cc.dg/tree-ssa/phi-opt-39.c > new file mode 100644 > index 00000000000..6b6006a96db > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c > @@ -0,0 +1,43 @@ > +/* { dg-options "-O2 -fdump-tree-phiopt" } */ > + > +unsigned f0(int A) > +{ > +// A =3D=3D 0? A : -A same as -A > + if (A =3D=3D 0) return A; > + return -A; > +} > + > +unsigned f1(int A) > +{ > +// A !=3D 0? A : -A same as A > + if (A !=3D 0) return A; > + return -A; > +} > +unsigned f2(int A) > +{ > +// A >=3D 0? A : -A same as abs (A) > + if (A >=3D 0) return A; > + return -A; > +} > +unsigned f3(int A) > +{ > +// A > 0? A : -A same as abs (A) > + if (A > 0) return A; > + return -A; > +} > +unsigned f4(int A) > +{ > +// A <=3D 0? A : -A same as -abs (A) > + if (A <=3D 0) return A; > + return -A; > +} > +unsigned f5(int A) > +{ > +// A < 0? A : -A same as -abs (A) > + if (A < 0) return A; > + return -A; > +} > + > +/* f4 and f5 are not allowed to be optimized in early phi-opt. */ > +/* { dg-final { scan-tree-dump-times "if" 2 "phiopt1" } } */ > +/* { dg-final { scan-tree-dump-not "if" "phiopt2" } } */ > diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc > index 312a6f9082b..bb55a4fba33 100644 > --- a/gcc/tree-ssa-phiopt.cc > +++ b/gcc/tree-ssa-phiopt.cc > @@ -310,7 +310,9 @@ factor_out_conditional_operation (edge e0, edge e1, g= phi *phi, > return NULL; > /* If arg1 is an INTEGER_CST, fold it to new type. */ > if (INTEGRAL_TYPE_P (TREE_TYPE (new_arg0)) > - && int_fits_type_p (arg1, TREE_TYPE (new_arg0))) > + && (int_fits_type_p (arg1, TREE_TYPE (new_arg0)) > + || (TYPE_PRECISION (TREE_TYPE (new_arg0)) > + =3D=3D TYPE_PRECISION (TREE_TYPE (arg1))))) > { > if (gimple_assign_cast_p (arg0_def_stmt)) > { > @@ -322,8 +324,12 @@ factor_out_conditional_operation (edge e0, edge e1, = gphi *phi, > if arg0_def_stmt is the only non-debug stmt in > its basic block, because then it is possible this > could enable further optimizations (minmax replacement > - etc.). See PR71016. */ > - if (new_arg0 !=3D gimple_cond_lhs (cond_stmt) > + etc.). See PR71016. > + Note no-op conversions don't have this issue as > + it will not generate any zero/sign extend in that case. = */ > + if ((TYPE_PRECISION (TREE_TYPE (new_arg0)) > + !=3D TYPE_PRECISION (TREE_TYPE (arg1))) > + && new_arg0 !=3D gimple_cond_lhs (cond_stmt) > && new_arg0 !=3D gimple_cond_rhs (cond_stmt) > && gimple_bb (arg0_def_stmt) =3D=3D e0->src) > { > @@ -354,6 +360,10 @@ factor_out_conditional_operation (edge e0, edge e1, = gphi *phi, > return NULL; > } > new_arg1 =3D fold_convert (TREE_TYPE (new_arg0), arg1); > + > + /* Drop the overlow that fold_convert might add. */ > + if (TREE_OVERFLOW (new_arg1)) > + new_arg1 =3D drop_tree_overflow (new_arg1); > } > else > return NULL; > -- > 2.34.1 >