| Message ID | 20260407212033.2739886-1-daniel.barboza@oss.qualcomm.com |
|---|---|
| State | New |
| Headers |
Return-Path: <gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 271E74BA2E1F for <patchwork@sourceware.org>; Tue, 7 Apr 2026 21:21:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 271E74BA2E1F Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=Qk3d6vUN; dkim=pass (2048-bit key, unprotected) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.a=rsa-sha256 header.s=google header.b=iFsxuzjv X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by sourceware.org (Postfix) with ESMTPS id 653E34BA2E08 for <gcc-patches@gcc.gnu.org>; Tue, 7 Apr 2026 21:20:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 653E34BA2E08 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=oss.qualcomm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oss.qualcomm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 653E34BA2E08 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775596842; cv=none; b=mIHpO/KS6x+oGCG0YkciDBx94JRoRFrWecBRWlBuaGEafdvbWso5UFeq7FvMJLiYs/AeAPY6GaUn6QOmrsoTgreiAjlBQEs9AZ0aSU++ObIo/dxYH+1FxvLiQVULnmm+AetQVMO5Zc5CCbRj1czQjy14rvN2/KBrbon5zN91xcA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1775596842; c=relaxed/simple; bh=jS1ciLmYvV2ILLn9sgTRKVYo1oxWYU0pkm/xnz++qSU=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID: MIME-Version; b=gVERbtmfAZ/c9EoyU81xpVDrxyAGEVi2I8rkCzr2WOwi+aRB1r71ML9sTV2lDg9JMwXksjGHKtD7erKGDIlmQHFNfbQKbcVPPFG44PDd9za2S19wgSjm+VNkFDlnoNIfVVEkT1Bwd0Nu2kOdzWYJHp29Lfr5aB+C4b1L1rwt8zE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 653E34BA2E08 Received: from pps.filterd (m0279873.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 637J6ZCg071718 for <gcc-patches@gcc.gnu.org>; Tue, 7 Apr 2026 21:20:41 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:date:from:message-id:mime-version :subject:to; s=qcppdkim1; bh=YWZdjHps/s/KmsWX4bN2Nq+o2THIS3MkFt6 Z884U5XI=; b=Qk3d6vUNQltRG7fVb09G6jCL8C9wCrxazT3rlNfdt9iuVMBklWR GlQvtCZvljqyjcYSZJmMiL5SSgsj5Z829sjmE72/tGhjeDH5SC/J0ei9xCqmts9a 9VLPfG5AsFZrOvE3DjLq5UJYA8s4yEYcxpjY14JxPxcHY+37MFIGZRw/gjP1ElJs NbjM/GmCdwqud0cAk4vOiHZZzGZglyiR/SiovsPQ3Qo4VADRoW2ctX1CI6OYlqR1 XI/2Ef/FeWv6psQI8aoOSiopsdPh/HGTf1hG6AmmSLy5GuMSEVSxMhPrDKZZplOr iOM4GzE8nCa32Mhnb8cpEOcI8H5866T558A== Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4dd7sx8cjj-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for <gcc-patches@gcc.gnu.org>; Tue, 07 Apr 2026 21:20:41 +0000 (GMT) Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-50b802961ecso75295851cf.1 for <gcc-patches@gcc.gnu.org>; Tue, 07 Apr 2026 14:20:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1775596840; x=1776201640; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=YWZdjHps/s/KmsWX4bN2Nq+o2THIS3MkFt6Z884U5XI=; b=iFsxuzjv++/jjLHqhvy7OVHrJWQ81eT8P1MSHOTJIget/1Ubr5sATiQZlBT8f4Ncl5 DYs53mKFiG46a5E8TXnrA7Y1ImBmOtbtfhhrtxX7CbY/l2MCemweVZ7+zG6HglWABxwn 0sz+eAIdpAstdnXA15k0GGfngtv14bXyy3wmK0H+mvfkg1abLFz4f8DLir3Zs+ER20/J oNIc2VdsfJ/MePBOy04Hxe0OTJbY+5mnNDlWLon+wmNmYZRV2CIXU0swI9mGCDg8RVYo klrjME6b4SDkkxU8u7algytTFJ21DmJPI8fjpQCARuAlA4+ckXR9pFu+qoVAadJa+fip 9Qrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775596840; x=1776201640; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=YWZdjHps/s/KmsWX4bN2Nq+o2THIS3MkFt6Z884U5XI=; b=tQN+sqEnda/h4rAz/1viW1FbHQB8yeR6EJIhcLiHvYliS8fLFceVXZyN4xgqu+UG1p sOUoylBcJBmRlZUfG2v+wq2ToPLkdHwZfFGBpzRu2VtjyHLcIfUF4aKltuxaxgWFsQlO wnl+DoazHw3k9i8K+qX6W+xXzv8HOe+ShUnSQwfYp+zZxkdTV4irKMEWWmz2FIW8uVzQ a4cUMgSxzRioT0oiVVTigvlkoRK25BBBhQoJwt3PXaL6xuuF6bZga3xkpcEWH95wrgYi 8InZQgR8mqZzv9aavVbZ3zjStPxFJS1Tji/EA6GNg9aRMNR3sg3/pEyDdKxNjVNdsnwA RUog== X-Gm-Message-State: AOJu0YzaH5w/PZgbYkYE2PM/ATpaTGOOfTMjCsiowD1zwt2NoP4PTZtR 6PMTnN7Vq20X+PPN0aY48eB9IbnUN2s5LZnrOjwjbeQgpVe57mFQc5HIw/TzPNMIwg5u/SOGzTr EoD7D59e45zApEIXFRXLLF23G7SuLfTypBJ1QZF3rbVfdswh6tDOJ52bjA56NPgY6IaZX X-Gm-Gg: AeBDieuTquQcbRw2o8I2QB9xD3ppS3dUtrGseCUBNhBq9zUflqF6J/IwXgbd/CBvmxp P8xb4CBSgEqcoI9NHUn1lwxmuRnNDwlJwi1t35Wn+6ycDTiwoGk/YGhKiHvyj8F8Ljb2E6+W7X+ B4SIvB1FXX0v26ArzxjbTLnRSEuxpD4ajtSdLRz/yOGe/kr5MTH9RQmIsnz2aJ0TYvH+QnyF7W7 gIs0K4gqXJVNTsnqSdPDv97KX3vxpwGzy5aNMckOzWXm3+ul0InewF7eWKOzc3T1qcjDCZsEsqJ zEhIvhcMqFrwzRnqjLPNpBLHf06P8AUjsC64cf5Y1FLbGG8e4UeS5i8e70UzRoDQ6qYY8iU93i6 OCrN0K6aoKvJN+WGbDdO4jmwS6j2ppZA/qF3R0168kgtY8aCJG2mIhP+jgw== X-Received: by 2002:a05:622a:15c5:b0:50b:4922:4b8a with SMTP id d75a77b69052e-50d6277303amr289286951cf.10.1775596840039; Tue, 07 Apr 2026 14:20:40 -0700 (PDT) X-Received: by 2002:a05:622a:15c5:b0:50b:4922:4b8a with SMTP id d75a77b69052e-50d6277303amr289286461cf.10.1775596839426; Tue, 07 Apr 2026 14:20:39 -0700 (PDT) Received: from QCOM-UWl2o8bcGT.qualcomm.com ([177.18.66.130]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-50d4b73ea66sm177659811cf.14.2026.04.07.14.20.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Apr 2026 14:20:39 -0700 (PDT) From: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com> To: gcc-patches@gcc.gnu.org Cc: jeffrey.law@oss.qualcomm.com, andrew.pinski@oss.qualcomm.com, Daniel Barboza <daniel.barboza@oss.qualcomm.com> Subject: [PATCH v2] match.pd: (A>>bool) == 0 -> (unsigned)A) <= bool [PR119420] Date: Tue, 7 Apr 2026 18:20:33 -0300 Message-ID: <20260407212033.2739886-1-daniel.barboza@oss.qualcomm.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Authority-Analysis: v=2.4 cv=etfvCIpX c=1 sm=1 tr=0 ts=69d57529 cx=c_pps a=WeENfcodrlLV9YRTxbY/uA==:117 a=Wg2i10Q8swPuJs8jTFC2sw==:17 a=A5OVakUREuEA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=rJkE3RaqiGZ5pbrm-msn:22 a=mDV3o1hIAAAA:8 a=EUspDBNiAAAA:8 a=GemSLBTV9bmTgziJS1sA:9 a=kacYvNCVWA4VmyqE58fU:22 X-Proofpoint-ORIG-GUID: u5qWj4GQF3IHlIqdzW6vVV_FO0GEU7HD X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNDA3MDE5NCBTYWx0ZWRfX0RSacBU0NCA7 /ES1yB4JIfDOGsrNJlAw0BzbeXZXSuKkxJJ61f26fZHLL5Ms+tSbgx/+SyOEOciAUq5VzDQl9Dl 0H2B9waZX7fnKPyVUXa7jHWC7ijdkMFhGqPklxmj7mvwirH7Y+3C/NFWt7hTU6f2Iv1m9T4/r0I QH+N3YXgOfHEEhvRNJ+42suc7O6wcDwk98HhdrKQ1TrJjLnogQEY7nqRHlNng/338G1S2D/Gx8I yJQRpXk6Vt85mJ4dYwcKA0/XI5EDPhXxM3HFGHLFwvYzgmqKBOj1/BYIBEjSoeMYTFz652aEFe4 VUZvEH6nKx0v0XEle+QWwcxjL1kg/X0uchHokqbX1oevCj4JY7S0QBK9+m+fAxWLJ80XsUKhMyC +YLMHg9+9hPGA8zMZk03c2MQ9NW0KiuaNkhTNhmTPSiHJfj7ap4eIOm1Tg932Vrkq/Iw88/uOrO DJNL/YpBapihYmtFvXQ== X-Proofpoint-GUID: u5qWj4GQF3IHlIqdzW6vVV_FO0GEU7HD X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-04-07_04,2026-04-07_05,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 lowpriorityscore=0 phishscore=0 bulkscore=0 impostorscore=0 suspectscore=0 spamscore=0 clxscore=1015 malwarescore=0 adultscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2604010000 definitions=main-2604070194 X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_LOW, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, 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 sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org |
| Series |
[v2] match.pd: (A>>bool) == 0 -> (unsigned)A) <= bool [PR119420]
|
|
Commit Message
Daniel Henrique Barboza
April 7, 2026, 9:20 p.m. UTC
From: Daniel Barboza <daniel.barboza@oss.qualcomm.com>
Note that we're not adding its counterpart:
"(A>>bool) != 0 -> (unsigned)A) > bool"
Because it will collide with patterns that handles builtin_clz (e.g.
clz-complement-int.c tree-ssa test).
Bootstrapped and regression tested in x86.
PR tree-optimization/119420
gcc/ChangeLog:
* match.pd(`(A>>bool) EQ 0 -> (unsigned)A) LE bool`): New
pattern.
gcc/testsuite/ChangeLog:
* gcc.dg/tree-ssa/pr119420.c: New test.
---
Changes from v1:
- fixed formatting of (with ...) block;
- added match.pd comment about the interaction with builtin_clz and why
we're not adding the NE variation.
- v1 link: https://gcc.gnu.org/pipermail/gcc-patches/2026-March/709727.html
gcc/match.pd | 11 +++++++++++
gcc/testsuite/gcc.dg/tree-ssa/pr119420.c | 23 +++++++++++++++++++++++
2 files changed, 34 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr119420.c
Comments
On Tue, Apr 7, 2026 at 2:20 PM Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com> wrote: > > From: Daniel Barboza <daniel.barboza@oss.qualcomm.com> > > Note that we're not adding its counterpart: > > "(A>>bool) != 0 -> (unsigned)A) > bool" > > Because it will collide with patterns that handles builtin_clz (e.g. > clz-complement-int.c tree-ssa test). I think this can be done for any constant shift (and not just for bool value). From combine.cc: /* If we have (compare (xshiftrt FOO N) (const_int C)) and the low order N bits of FOO are known to be zero, we can do this by comparing FOO with C shifted left N bits so long as no overflow occurs. Even if the low order N bits of FOO aren't known to be zero, if the comparison is >= or < we can use the same optimization and for > or <= by setting all the low order N bits in the comparison constant. */ Handling more than just bool would also fix PR 124808. But maybe that should be handled separately since this case is for the non-constant bool case. Thanks, Andrew > > Bootstrapped and regression tested in x86. > > PR tree-optimization/119420 > > gcc/ChangeLog: > > * match.pd(`(A>>bool) EQ 0 -> (unsigned)A) LE bool`): New > pattern. > > gcc/testsuite/ChangeLog: > > * gcc.dg/tree-ssa/pr119420.c: New test. > --- > > Changes from v1: > - fixed formatting of (with ...) block; > - added match.pd comment about the interaction with builtin_clz and why > we're not adding the NE variation. > - v1 link: https://gcc.gnu.org/pipermail/gcc-patches/2026-March/709727.html > > gcc/match.pd | 11 +++++++++++ > gcc/testsuite/gcc.dg/tree-ssa/pr119420.c | 23 +++++++++++++++++++++++ > 2 files changed, 34 insertions(+) > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr119420.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index 7f16fd4e081..b7a8ce420b4 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -5367,6 +5367,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > @0))))) > #endif > > +/* PR119420: (A >> bool) == 0 -> (unsigned)a <= (unsigned)bool > + > + We're not adding its NE counterpart: > + "(A>>bool) != 0 -> (unsigned)A) > bool" > + Because it collides with patterns that handles builtin_clz, as > + shown in clz-complement-int.c tree-ssa test. */ > +(simplify > + (eq (rshift @0 zero_one_valued_p@1) integer_zerop) > + (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } > + (le (convert:utype @0) (convert:utype @1)))) > + > /* Rewrite an LROTATE_EXPR by a constant into an > RROTATE_EXPR by a new constant. */ > (simplify > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c b/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c > new file mode 100644 > index 00000000000..3169650fe66 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c > @@ -0,0 +1,23 @@ > +/* { dg-additional-options -O2 } */ > +/* { dg-additional-options -fdump-tree-optimized } */ > + > +int ll (signed a) > +{ > + int d = a >> 1; > + return d == 0; > +} > + > +int ll1 (signed a) > +{ > + int d = a & ~1; > + return d == 0; > +} > + > +int ll2 (signed a) > +{ > + unsigned aa = a; > + return aa <= 1; > +} > + > +/* { dg-final { scan-tree-dump-times " >> " 0 optimized } } */ > +/* { dg-final { scan-tree-dump-times " <= " 3 optimized } } */ > -- > 2.43.0 >
On 4/8/2026 2:26 AM, Andrew Pinski wrote: > On Tue, Apr 7, 2026 at 2:20 PM Daniel Henrique Barboza > <daniel.barboza@oss.qualcomm.com> wrote: >> >> From: Daniel Barboza <daniel.barboza@oss.qualcomm.com> >> >> Note that we're not adding its counterpart: >> >> "(A>>bool) != 0 -> (unsigned)A) > bool" >> >> Because it will collide with patterns that handles builtin_clz (e.g. >> clz-complement-int.c tree-ssa test). > > I think this can be done for any constant shift (and not just for bool value). > From combine.cc: > /* If we have (compare (xshiftrt FOO N) (const_int C)) and > the low order N bits of FOO are known to be zero, we can do this > by comparing FOO with C shifted left N bits so long as no > overflow occurs. Even if the low order N bits of FOO aren't known > to be zero, if the comparison is >= or < we can use the same > optimization and for > or <= by setting all the low > order N bits in the comparison constant. */ > > Handling more than just bool would also fix PR 124808. But maybe that > should be handled separately since this case is for the non-constant > bool case. I'll make changes to handle constant shifts and maybe we can help alleviate 124808. That PR might require additional work anyway but at least progress was made. Thanks, Daniel > > Thanks, > Andrew > >> >> Bootstrapped and regression tested in x86. >> >> PR tree-optimization/119420 >> >> gcc/ChangeLog: >> >> * match.pd(`(A>>bool) EQ 0 -> (unsigned)A) LE bool`): New >> pattern. >> >> gcc/testsuite/ChangeLog: >> >> * gcc.dg/tree-ssa/pr119420.c: New test. >> --- >> >> Changes from v1: >> - fixed formatting of (with ...) block; >> - added match.pd comment about the interaction with builtin_clz and why >> we're not adding the NE variation. >> - v1 link: https://gcc.gnu.org/pipermail/gcc-patches/2026-March/709727.html >> >> gcc/match.pd | 11 +++++++++++ >> gcc/testsuite/gcc.dg/tree-ssa/pr119420.c | 23 +++++++++++++++++++++++ >> 2 files changed, 34 insertions(+) >> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr119420.c >> >> diff --git a/gcc/match.pd b/gcc/match.pd >> index 7f16fd4e081..b7a8ce420b4 100644 >> --- a/gcc/match.pd >> +++ b/gcc/match.pd >> @@ -5367,6 +5367,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) >> @0))))) >> #endif >> >> +/* PR119420: (A >> bool) == 0 -> (unsigned)a <= (unsigned)bool >> + >> + We're not adding its NE counterpart: >> + "(A>>bool) != 0 -> (unsigned)A) > bool" >> + Because it collides with patterns that handles builtin_clz, as >> + shown in clz-complement-int.c tree-ssa test. */ >> +(simplify >> + (eq (rshift @0 zero_one_valued_p@1) integer_zerop) >> + (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } >> + (le (convert:utype @0) (convert:utype @1)))) >> + >> /* Rewrite an LROTATE_EXPR by a constant into an >> RROTATE_EXPR by a new constant. */ >> (simplify >> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c b/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c >> new file mode 100644 >> index 00000000000..3169650fe66 >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c >> @@ -0,0 +1,23 @@ >> +/* { dg-additional-options -O2 } */ >> +/* { dg-additional-options -fdump-tree-optimized } */ >> + >> +int ll (signed a) >> +{ >> + int d = a >> 1; >> + return d == 0; >> +} >> + >> +int ll1 (signed a) >> +{ >> + int d = a & ~1; >> + return d == 0; >> +} >> + >> +int ll2 (signed a) >> +{ >> + unsigned aa = a; >> + return aa <= 1; >> +} >> + >> +/* { dg-final { scan-tree-dump-times " >> " 0 optimized } } */ >> +/* { dg-final { scan-tree-dump-times " <= " 3 optimized } } */ >> -- >> 2.43.0 >>
On 4/8/2026 2:26 AM, Andrew Pinski wrote: > On Tue, Apr 7, 2026 at 2:20 PM Daniel Henrique Barboza > <daniel.barboza@oss.qualcomm.com> wrote: >> >> From: Daniel Barboza <daniel.barboza@oss.qualcomm.com> >> >> Note that we're not adding its counterpart: >> >> "(A>>bool) != 0 -> (unsigned)A) > bool" >> >> Because it will collide with patterns that handles builtin_clz (e.g. >> clz-complement-int.c tree-ssa test). > > I think this can be done for any constant shift (and not just for bool value). > From combine.cc: > /* If we have (compare (xshiftrt FOO N) (const_int C)) and > the low order N bits of FOO are known to be zero, we can do this > by comparing FOO with C shifted left N bits so long as no > overflow occurs. Even if the low order N bits of FOO aren't known > to be zero, if the comparison is >= or < we can use the same > optimization and for > or <= by setting all the low > order N bits in the comparison constant. */ > > Handling more than just bool would also fix PR 124808. But maybe that > should be handled separately since this case is for the non-constant > bool case. After trying to fit in the logic in this same patch I figured you're right. PR124808 is too complex to be added as a side case of this pattern. I'll create a separated patch for it. Let's keep this one as is. Thanks, Daniel > > Thanks, > Andrew > >> >> Bootstrapped and regression tested in x86. >> >> PR tree-optimization/119420 >> >> gcc/ChangeLog: >> >> * match.pd(`(A>>bool) EQ 0 -> (unsigned)A) LE bool`): New >> pattern. >> >> gcc/testsuite/ChangeLog: >> >> * gcc.dg/tree-ssa/pr119420.c: New test. >> --- >> >> Changes from v1: >> - fixed formatting of (with ...) block; >> - added match.pd comment about the interaction with builtin_clz and why >> we're not adding the NE variation. >> - v1 link: https://gcc.gnu.org/pipermail/gcc-patches/2026-March/709727.html >> >> gcc/match.pd | 11 +++++++++++ >> gcc/testsuite/gcc.dg/tree-ssa/pr119420.c | 23 +++++++++++++++++++++++ >> 2 files changed, 34 insertions(+) >> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr119420.c >> >> diff --git a/gcc/match.pd b/gcc/match.pd >> index 7f16fd4e081..b7a8ce420b4 100644 >> --- a/gcc/match.pd >> +++ b/gcc/match.pd >> @@ -5367,6 +5367,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) >> @0))))) >> #endif >> >> +/* PR119420: (A >> bool) == 0 -> (unsigned)a <= (unsigned)bool >> + >> + We're not adding its NE counterpart: >> + "(A>>bool) != 0 -> (unsigned)A) > bool" >> + Because it collides with patterns that handles builtin_clz, as >> + shown in clz-complement-int.c tree-ssa test. */ >> +(simplify >> + (eq (rshift @0 zero_one_valued_p@1) integer_zerop) >> + (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } >> + (le (convert:utype @0) (convert:utype @1)))) >> + >> /* Rewrite an LROTATE_EXPR by a constant into an >> RROTATE_EXPR by a new constant. */ >> (simplify >> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c b/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c >> new file mode 100644 >> index 00000000000..3169650fe66 >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c >> @@ -0,0 +1,23 @@ >> +/* { dg-additional-options -O2 } */ >> +/* { dg-additional-options -fdump-tree-optimized } */ >> + >> +int ll (signed a) >> +{ >> + int d = a >> 1; >> + return d == 0; >> +} >> + >> +int ll1 (signed a) >> +{ >> + int d = a & ~1; >> + return d == 0; >> +} >> + >> +int ll2 (signed a) >> +{ >> + unsigned aa = a; >> + return aa <= 1; >> +} >> + >> +/* { dg-final { scan-tree-dump-times " >> " 0 optimized } } */ >> +/* { dg-final { scan-tree-dump-times " <= " 3 optimized } } */ >> -- >> 2.43.0 >>
On Tue, Apr 7, 2026 at 2:20 PM Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com> wrote: > > From: Daniel Barboza <daniel.barboza@oss.qualcomm.com> > > Note that we're not adding its counterpart: > > "(A>>bool) != 0 -> (unsigned)A) > bool" > > Because it will collide with patterns that handles builtin_clz (e.g. > clz-complement-int.c tree-ssa test). > > Bootstrapped and regression tested in x86. > > PR tree-optimization/119420 > > gcc/ChangeLog: > > * match.pd(`(A>>bool) EQ 0 -> (unsigned)A) LE bool`): New > pattern. > > gcc/testsuite/ChangeLog: > > * gcc.dg/tree-ssa/pr119420.c: New test. > --- > > Changes from v1: > - fixed formatting of (with ...) block; > - added match.pd comment about the interaction with builtin_clz and why > we're not adding the NE variation. > - v1 link: https://gcc.gnu.org/pipermail/gcc-patches/2026-March/709727.html > > gcc/match.pd | 11 +++++++++++ > gcc/testsuite/gcc.dg/tree-ssa/pr119420.c | 23 +++++++++++++++++++++++ > 2 files changed, 34 insertions(+) > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr119420.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index 7f16fd4e081..b7a8ce420b4 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -5367,6 +5367,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > @0))))) > #endif > > +/* PR119420: (A >> bool) == 0 -> (unsigned)a <= (unsigned)bool > + > + We're not adding its NE counterpart: > + "(A>>bool) != 0 -> (unsigned)A) > bool" > + Because it collides with patterns that handles builtin_clz, as > + shown in clz-complement-int.c tree-ssa test. */ > +(simplify > + (eq (rshift @0 zero_one_valued_p@1) integer_zerop) > + (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } > + (le (convert:utype @0) (convert:utype @1)))) Maybe for the ne case you could add a :s on the rshift for now. I filed https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124833 for the non single use case. That seems like what LLVM does. I think the above also would need the :s on the rshift for the same reason. Thanks, Andrew Pinski > + > /* Rewrite an LROTATE_EXPR by a constant into an > RROTATE_EXPR by a new constant. */ > (simplify > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c b/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c > new file mode 100644 > index 00000000000..3169650fe66 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c > @@ -0,0 +1,23 @@ > +/* { dg-additional-options -O2 } */ > +/* { dg-additional-options -fdump-tree-optimized } */ > + > +int ll (signed a) > +{ > + int d = a >> 1; > + return d == 0; > +} > + > +int ll1 (signed a) > +{ > + int d = a & ~1; > + return d == 0; > +} > + > +int ll2 (signed a) > +{ > + unsigned aa = a; > + return aa <= 1; > +} > + > +/* { dg-final { scan-tree-dump-times " >> " 0 optimized } } */ > +/* { dg-final { scan-tree-dump-times " <= " 3 optimized } } */ > -- > 2.43.0 >
On 4/9/2026 3:10 PM, Andrew Pinski wrote: > On Tue, Apr 7, 2026 at 2:20 PM Daniel Henrique Barboza > <daniel.barboza@oss.qualcomm.com> wrote: >> >> From: Daniel Barboza <daniel.barboza@oss.qualcomm.com> >> >> Note that we're not adding its counterpart: >> >> "(A>>bool) != 0 -> (unsigned)A) > bool" >> >> Because it will collide with patterns that handles builtin_clz (e.g. >> clz-complement-int.c tree-ssa test). >> >> Bootstrapped and regression tested in x86. >> >> PR tree-optimization/119420 >> >> gcc/ChangeLog: >> >> * match.pd(`(A>>bool) EQ 0 -> (unsigned)A) LE bool`): New >> pattern. >> >> gcc/testsuite/ChangeLog: >> >> * gcc.dg/tree-ssa/pr119420.c: New test. >> --- >> >> Changes from v1: >> - fixed formatting of (with ...) block; >> - added match.pd comment about the interaction with builtin_clz and why >> we're not adding the NE variation. >> - v1 link: https://gcc.gnu.org/pipermail/gcc-patches/2026-March/709727.html >> >> gcc/match.pd | 11 +++++++++++ >> gcc/testsuite/gcc.dg/tree-ssa/pr119420.c | 23 +++++++++++++++++++++++ >> 2 files changed, 34 insertions(+) >> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr119420.c >> >> diff --git a/gcc/match.pd b/gcc/match.pd >> index 7f16fd4e081..b7a8ce420b4 100644 >> --- a/gcc/match.pd >> +++ b/gcc/match.pd >> @@ -5367,6 +5367,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) >> @0))))) >> #endif >> >> +/* PR119420: (A >> bool) == 0 -> (unsigned)a <= (unsigned)bool >> + >> + We're not adding its NE counterpart: >> + "(A>>bool) != 0 -> (unsigned)A) > bool" >> + Because it collides with patterns that handles builtin_clz, as >> + shown in clz-complement-int.c tree-ssa test. */ >> +(simplify >> + (eq (rshift @0 zero_one_valued_p@1) integer_zerop) >> + (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } >> + (le (convert:utype @0) (convert:utype @1)))) > > Maybe for the ne case you could add a :s on the rshift for now. I > filed https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124833 for the non > single use case. > That seems like what LLVM does. I think the above also would need the > :s on the rshift for the same reason. That brings me back to a patch that is pending a review/redo: "Re: [PATCH v2] match.pd: simplify lshift const cmp using bit_and [PR124019]" Jeff mentioned that this patch breaks a CRIS test, and adding ":s" would be a solution, and the pattern handled there: "A << CST1 EQ|NE CST2 -> (A & CSTmask) EQ|NE (CST2 >> CST1); " it is also a pattern that happens to collide with the builtin_ctz call with CST1 = 1 (which is not handled in that patch). And the pattern you suggested for PR124808 is also similar to these 2 (by the way your idea improves that loop): (A >> CST1) cmp CST2 -> A cmp (CST2 << CST1) My point here is that maybe there's an overall design/pattern in all these optimizations with this overall format "A lshift|rshift CST1 cmp CST2" due to how they match these CTZ/CLZ builtins, and maybe we should default to use ":s" in all related patterns. Does that make sense? Daniel > > > Thanks, > Andrew Pinski > >> + >> /* Rewrite an LROTATE_EXPR by a constant into an >> RROTATE_EXPR by a new constant. */ >> (simplify >> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c b/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c >> new file mode 100644 >> index 00000000000..3169650fe66 >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c >> @@ -0,0 +1,23 @@ >> +/* { dg-additional-options -O2 } */ >> +/* { dg-additional-options -fdump-tree-optimized } */ >> + >> +int ll (signed a) >> +{ >> + int d = a >> 1; >> + return d == 0; >> +} >> + >> +int ll1 (signed a) >> +{ >> + int d = a & ~1; >> + return d == 0; >> +} >> + >> +int ll2 (signed a) >> +{ >> + unsigned aa = a; >> + return aa <= 1; >> +} >> + >> +/* { dg-final { scan-tree-dump-times " >> " 0 optimized } } */ >> +/* { dg-final { scan-tree-dump-times " <= " 3 optimized } } */ >> -- >> 2.43.0 >>
On Thu, Apr 9, 2026 at 11:50 AM Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com> wrote: > > > > On 4/9/2026 3:10 PM, Andrew Pinski wrote: > > On Tue, Apr 7, 2026 at 2:20 PM Daniel Henrique Barboza > > <daniel.barboza@oss.qualcomm.com> wrote: > >> > >> From: Daniel Barboza <daniel.barboza@oss.qualcomm.com> > >> > >> Note that we're not adding its counterpart: > >> > >> "(A>>bool) != 0 -> (unsigned)A) > bool" > >> > >> Because it will collide with patterns that handles builtin_clz (e.g. > >> clz-complement-int.c tree-ssa test). > >> > >> Bootstrapped and regression tested in x86. > >> > >> PR tree-optimization/119420 > >> > >> gcc/ChangeLog: > >> > >> * match.pd(`(A>>bool) EQ 0 -> (unsigned)A) LE bool`): New > >> pattern. > >> > >> gcc/testsuite/ChangeLog: > >> > >> * gcc.dg/tree-ssa/pr119420.c: New test. > >> --- > >> > >> Changes from v1: > >> - fixed formatting of (with ...) block; > >> - added match.pd comment about the interaction with builtin_clz and why > >> we're not adding the NE variation. > >> - v1 link: https://gcc.gnu.org/pipermail/gcc-patches/2026-March/709727.html > >> > >> gcc/match.pd | 11 +++++++++++ > >> gcc/testsuite/gcc.dg/tree-ssa/pr119420.c | 23 +++++++++++++++++++++++ > >> 2 files changed, 34 insertions(+) > >> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr119420.c > >> > >> diff --git a/gcc/match.pd b/gcc/match.pd > >> index 7f16fd4e081..b7a8ce420b4 100644 > >> --- a/gcc/match.pd > >> +++ b/gcc/match.pd > >> @@ -5367,6 +5367,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > >> @0))))) > >> #endif > >> > >> +/* PR119420: (A >> bool) == 0 -> (unsigned)a <= (unsigned)bool > >> + > >> + We're not adding its NE counterpart: > >> + "(A>>bool) != 0 -> (unsigned)A) > bool" > >> + Because it collides with patterns that handles builtin_clz, as > >> + shown in clz-complement-int.c tree-ssa test. */ > >> +(simplify > >> + (eq (rshift @0 zero_one_valued_p@1) integer_zerop) > >> + (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } > >> + (le (convert:utype @0) (convert:utype @1)))) > > > > Maybe for the ne case you could add a :s on the rshift for now. I > > filed https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124833 for the non > > single use case. > > That seems like what LLVM does. I think the above also would need the > > :s on the rshift for the same reason. > > That brings me back to a patch that is pending a review/redo: > > "Re: [PATCH v2] match.pd: simplify lshift const cmp using bit_and [PR124019]" > > Jeff mentioned that this patch breaks a CRIS test, and adding ":s" would be > a solution, and the pattern handled there: > > "A << CST1 EQ|NE CST2 -> (A & CSTmask) EQ|NE (CST2 >> CST1); " > > it is also a pattern that happens to collide with the builtin_ctz call with > CST1 = 1 (which is not handled in that patch). > > And the pattern you suggested for PR124808 is also similar to these 2 (by the > way your idea improves that loop): > > (A >> CST1) cmp CST2 -> A cmp (CST2 << CST1) > > > My point here is that maybe there's an overall design/pattern in all these > optimizations with this overall format "A lshift|rshift CST1 cmp CST2" due > to how they match these CTZ/CLZ builtins, and maybe we should default to > use ":s" in all related patterns. Does that make sense? Yes, I think so. Though in some cases having `:s` there is worse. Maybe the issue is forwprop needs to be limiting how it handles conditionals for loop exits. Currently forwprop calls fold_stmt and not limiting the valueization here. But maybe for loop exits we should. This would allow us to remove the single_use/`:s` on some more patterns (e.g. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122186). Let me think about this some more. Thanks, Andrew > > > Daniel > > > > > > > Thanks, > > Andrew Pinski > > > >> + > >> /* Rewrite an LROTATE_EXPR by a constant into an > >> RROTATE_EXPR by a new constant. */ > >> (simplify > >> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c b/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c > >> new file mode 100644 > >> index 00000000000..3169650fe66 > >> --- /dev/null > >> +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c > >> @@ -0,0 +1,23 @@ > >> +/* { dg-additional-options -O2 } */ > >> +/* { dg-additional-options -fdump-tree-optimized } */ > >> + > >> +int ll (signed a) > >> +{ > >> + int d = a >> 1; > >> + return d == 0; > >> +} > >> + > >> +int ll1 (signed a) > >> +{ > >> + int d = a & ~1; > >> + return d == 0; > >> +} > >> + > >> +int ll2 (signed a) > >> +{ > >> + unsigned aa = a; > >> + return aa <= 1; > >> +} > >> + > >> +/* { dg-final { scan-tree-dump-times " >> " 0 optimized } } */ > >> +/* { dg-final { scan-tree-dump-times " <= " 3 optimized } } */ > >> -- > >> 2.43.0 > >> >
On Thu, Apr 9, 2026 at 9:04 PM Andrew Pinski <andrew.pinski@oss.qualcomm.com> wrote: > > On Thu, Apr 9, 2026 at 11:50 AM Daniel Henrique Barboza > <daniel.barboza@oss.qualcomm.com> wrote: > > > > > > > > On 4/9/2026 3:10 PM, Andrew Pinski wrote: > > > On Tue, Apr 7, 2026 at 2:20 PM Daniel Henrique Barboza > > > <daniel.barboza@oss.qualcomm.com> wrote: > > >> > > >> From: Daniel Barboza <daniel.barboza@oss.qualcomm.com> > > >> > > >> Note that we're not adding its counterpart: > > >> > > >> "(A>>bool) != 0 -> (unsigned)A) > bool" > > >> > > >> Because it will collide with patterns that handles builtin_clz (e.g. > > >> clz-complement-int.c tree-ssa test). > > >> > > >> Bootstrapped and regression tested in x86. > > >> > > >> PR tree-optimization/119420 > > >> > > >> gcc/ChangeLog: > > >> > > >> * match.pd(`(A>>bool) EQ 0 -> (unsigned)A) LE bool`): New > > >> pattern. > > >> > > >> gcc/testsuite/ChangeLog: > > >> > > >> * gcc.dg/tree-ssa/pr119420.c: New test. > > >> --- > > >> > > >> Changes from v1: > > >> - fixed formatting of (with ...) block; > > >> - added match.pd comment about the interaction with builtin_clz and why > > >> we're not adding the NE variation. > > >> - v1 link: https://gcc.gnu.org/pipermail/gcc-patches/2026-March/709727.html > > >> > > >> gcc/match.pd | 11 +++++++++++ > > >> gcc/testsuite/gcc.dg/tree-ssa/pr119420.c | 23 +++++++++++++++++++++++ > > >> 2 files changed, 34 insertions(+) > > >> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr119420.c > > >> > > >> diff --git a/gcc/match.pd b/gcc/match.pd > > >> index 7f16fd4e081..b7a8ce420b4 100644 > > >> --- a/gcc/match.pd > > >> +++ b/gcc/match.pd > > >> @@ -5367,6 +5367,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > > >> @0))))) > > >> #endif > > >> > > >> +/* PR119420: (A >> bool) == 0 -> (unsigned)a <= (unsigned)bool > > >> + > > >> + We're not adding its NE counterpart: > > >> + "(A>>bool) != 0 -> (unsigned)A) > bool" > > >> + Because it collides with patterns that handles builtin_clz, as > > >> + shown in clz-complement-int.c tree-ssa test. */ > > >> +(simplify > > >> + (eq (rshift @0 zero_one_valued_p@1) integer_zerop) > > >> + (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } > > >> + (le (convert:utype @0) (convert:utype @1)))) > > > > > > Maybe for the ne case you could add a :s on the rshift for now. I > > > filed https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124833 for the non > > > single use case. > > > That seems like what LLVM does. I think the above also would need the > > > :s on the rshift for the same reason. > > > > That brings me back to a patch that is pending a review/redo: > > > > "Re: [PATCH v2] match.pd: simplify lshift const cmp using bit_and [PR124019]" > > > > Jeff mentioned that this patch breaks a CRIS test, and adding ":s" would be > > a solution, and the pattern handled there: > > > > "A << CST1 EQ|NE CST2 -> (A & CSTmask) EQ|NE (CST2 >> CST1); " > > > > it is also a pattern that happens to collide with the builtin_ctz call with > > CST1 = 1 (which is not handled in that patch). > > > > And the pattern you suggested for PR124808 is also similar to these 2 (by the > > way your idea improves that loop): > > > > (A >> CST1) cmp CST2 -> A cmp (CST2 << CST1) > > > > > > My point here is that maybe there's an overall design/pattern in all these > > optimizations with this overall format "A lshift|rshift CST1 cmp CST2" due > > to how they match these CTZ/CLZ builtins, and maybe we should default to > > use ":s" in all related patterns. Does that make sense? > > Yes, I think so. Though in some cases having `:s` there is worse. > Maybe the issue is forwprop needs to be limiting how it handles > conditionals for loop exits. > Currently forwprop calls fold_stmt and not limiting the valueization > here. But maybe for loop exits we should. > This would allow us to remove the single_use/`:s` on some more > patterns (e.g. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122186). > Let me think about this some more. It's also if you have two uses that can be folded in a similar way, leading to CSE of the generated extra stmt that we cannot anticipate (or even try). At one point I thought that forwprop could do :s ignoring folding, but with the ability to roll-back, and do some form of costing after a local CSE on the result, costing before/after for "connected components". But I have never sat down to think about how to actually reasonably implement such thing - it might be interesting to get feedback from folding like "this would have simplified, but did not because of multi-use of @1, @2 ...", and then somehow gather possible multi-use folding roots, "unshare", fold, try to CSE and cost the set of expressions. But that does not play well with the somewhat nice lattice setup we have in forwprop right now. Richard. > > Thanks, > Andrew > > > > > > > > Daniel > > > > > > > > > > > Thanks, > > > Andrew Pinski > > > > > >> + > > >> /* Rewrite an LROTATE_EXPR by a constant into an > > >> RROTATE_EXPR by a new constant. */ > > >> (simplify > > >> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c b/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c > > >> new file mode 100644 > > >> index 00000000000..3169650fe66 > > >> --- /dev/null > > >> +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c > > >> @@ -0,0 +1,23 @@ > > >> +/* { dg-additional-options -O2 } */ > > >> +/* { dg-additional-options -fdump-tree-optimized } */ > > >> + > > >> +int ll (signed a) > > >> +{ > > >> + int d = a >> 1; > > >> + return d == 0; > > >> +} > > >> + > > >> +int ll1 (signed a) > > >> +{ > > >> + int d = a & ~1; > > >> + return d == 0; > > >> +} > > >> + > > >> +int ll2 (signed a) > > >> +{ > > >> + unsigned aa = a; > > >> + return aa <= 1; > > >> +} > > >> + > > >> +/* { dg-final { scan-tree-dump-times " >> " 0 optimized } } */ > > >> +/* { dg-final { scan-tree-dump-times " <= " 3 optimized } } */ > > >> -- > > >> 2.43.0 > > >> > >
diff --git a/gcc/match.pd b/gcc/match.pd index 7f16fd4e081..b7a8ce420b4 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5367,6 +5367,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) @0))))) #endif +/* PR119420: (A >> bool) == 0 -> (unsigned)a <= (unsigned)bool + + We're not adding its NE counterpart: + "(A>>bool) != 0 -> (unsigned)A) > bool" + Because it collides with patterns that handles builtin_clz, as + shown in clz-complement-int.c tree-ssa test. */ +(simplify + (eq (rshift @0 zero_one_valued_p@1) integer_zerop) + (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } + (le (convert:utype @0) (convert:utype @1)))) + /* Rewrite an LROTATE_EXPR by a constant into an RROTATE_EXPR by a new constant. */ (simplify diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c b/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c new file mode 100644 index 00000000000..3169650fe66 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr119420.c @@ -0,0 +1,23 @@ +/* { dg-additional-options -O2 } */ +/* { dg-additional-options -fdump-tree-optimized } */ + +int ll (signed a) +{ + int d = a >> 1; + return d == 0; +} + +int ll1 (signed a) +{ + int d = a & ~1; + return d == 0; +} + +int ll2 (signed a) +{ + unsigned aa = a; + return aa <= 1; +} + +/* { dg-final { scan-tree-dump-times " >> " 0 optimized } } */ +/* { dg-final { scan-tree-dump-times " <= " 3 optimized } } */