I see. That makes sense. Thanks for the explanation. I was looking at the 64992 and it seems all the implementation right now are only using INTEGER_CST at the moment.
But now it makes sense. Best wishes, Navid. ________________________________________ From: Andrew Pinski <pins...@gmail.com> Sent: Tuesday, November 30, 2021 15:18 To: Navid Rahimi Cc: Navid Rahimi via Gcc-patches Subject: Re: [EXTERNAL] Re: [PATCH] tree-optimization/98956 Optimizing out boolean left shift On Tue, Nov 30, 2021 at 3:08 PM Navid Rahimi <navidrah...@microsoft.com> wrote: > > Hi Andrew, > > Thanks for your detailed comment. There are two problem I wanted to discuss > with you about: > > a) The optimization I have sent patch, does optimize variable length "<<" > too(for example B0 << x, where x is variable). This [1] link shows the actual > optimization and a link for the proof is included in the editor. > > b) I am unable to prove the optimization you are describing for non-constant > length shift. You can take a look at the code example [2] and proof [3]. I am > getting "Transformation doesn't verify!" when I do implement the optimization > you mentioned for non-constant shift. > > The optimization you are describing only works for "(take: (t << 1) != 0) -> > ((t & 0x7fffffff) != 0)" which only is provable and works for INTEGER_CST. No it works with non constants too: t << y != 0 -> t & (-1u>>y) != 0 When y == 0, you have t != 0. Which is exactly what you think it should be. Which can be further reduced to t != 0 as y >= sizeof(t)*BITS_PER_UNIT is undefined. Thanks, Andrew Pinski > > My understanding might be incorrect here, please don't hesitate to correct me. > > 1) > https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fcompiler-explorer.com%2Fz%2Fr46znh4Tj&data=04%7C01%7Cnavidrahimi%40microsoft.com%7Caa5443e61a5e4cdc177f08d9b457d03e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739111521114457%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=hGkyUkh4Srjb5%2BhvYdT30VLaDLGlkM6jBt3TmfcHFUw%3D&reserved=0 > 2) > https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fcompiler-explorer.com%2Fz%2FK1so39dbK&data=04%7C01%7Cnavidrahimi%40microsoft.com%7Caa5443e61a5e4cdc177f08d9b457d03e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739111521124452%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=EiVD3aIDzds%2BIX5EY3onWVuc%2FdMjoeDSyc5I1B2Xr%2F4%3D&reserved=0 > 3) > https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Falive2.llvm.org%2Fce%2Fz%2F-54zZv&data=04%7C01%7Cnavidrahimi%40microsoft.com%7Caa5443e61a5e4cdc177f08d9b457d03e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739111521124452%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=vFiA3eWi5Ry3rFwp6iUc61JaVtzoWS6careE4I5rZvk%3D&reserved=0 > > Best wishes, > Navid. > > ________________________________________ > From: Andrew Pinski <pins...@gmail.com> > Sent: Tuesday, November 30, 2021 14:03 > To: Navid Rahimi > Cc: Navid Rahimi via Gcc-patches > Subject: [EXTERNAL] Re: [PATCH] tree-optimization/98956 Optimizing out > boolean left shift > > On Tue, Nov 30, 2021 at 8:35 AM Navid Rahimi via Gcc-patches > <gcc-patches@gcc.gnu.org> wrote: > > > > Hi GCC community, > > > > This patch will add the missed pattern described in bug 98956 [1] to the > > match.pd. The codegen and correctness proof for this pattern is here [2,3] > > in case anyone is curious. Tested on x86_64 Linux. > > > > A better way to optimize this is the following (which I describe in PR 64992): > take: (t << 1) != 0; > > This should be transformed into: > (t & 0x7fffffff) != 0 > > The rest will just fall out really. That is there is no reason to > special case bool here. > I have most of the patch except for creating the mask part which > should be simple, I just did not want to look up the wi:: functions at > the time I was writing it into the bug report. > > Thanks, > Andrew Pinski > > > > > Tree-optimization/98956: > > > > Adding new optimization to match.pd: > > * match.pd ((B0 << x) cmp 0) -> B0 cmp 0 : New optimization. > > * gcc.dg/tree-ssa/pr98956.c: testcase for this optimization. > > * gcc.dg/tree-ssa/pr98956-2.c: testcase for node with > > side-effect. > > > > 1) > > https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgcc.gnu.org%2Fbugzilla%2Fshow_bug.cgi%3Fid%3D98956&data=04%7C01%7Cnavidrahimi%40microsoft.com%7Caa5443e61a5e4cdc177f08d9b457d03e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739111521124452%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=D2%2Fsh1qyHezbiH8xfcwONjnob00Pvu5ktj2kcFlQSxE%3D&reserved=0 > > 2) > > https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fcompiler-explorer.com%2Fz%2Fnj4PTrecW&data=04%7C01%7Cnavidrahimi%40microsoft.com%7Caa5443e61a5e4cdc177f08d9b457d03e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739111521124452%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=cWJRaWXZBuR6Wt42YkUT5bFpvvWr3n81Kml5KF7gq38%3D&reserved=0 > > 3) > > https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Falive2.llvm.org%2Fce%2Fz%2FjyJAoS&data=04%7C01%7Cnavidrahimi%40microsoft.com%7Caa5443e61a5e4cdc177f08d9b457d03e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739111521124452%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=c1%2FRY7g4iSCwo5QwJQ3j%2FrpNvM2wlu2nr%2BtaDOOB7v8%3D&reserved=0 > > > > Best wishes, > > Navid.