[EXTERNAL] Re: [PATCH] tree-optimization/98956 Optimizing out boolean left shift

Navid Rahimi navidrahimi@microsoft.com
Tue Nov 30 23:08:46 GMT 2021

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.

My understanding might be incorrect here, please don't hesitate to correct me.

1) https://compiler-explorer.com/z/r46znh4Tj
2) https://compiler-explorer.com/z/K1so39dbK
3) https://alive2.llvm.org/ce/z/-54zZv

Best wishes,

From: Andrew Pinski <pinskia@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.

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%7Cd83f36080fd94b563ab608d9b44d4d1f%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739066369079450%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=EO7zAIa9sux4JklTDeALImoX3Kcjqeug%2BssU0E%2Fp6mY%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%7Cd83f36080fd94b563ab608d9b44d4d1f%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739066369079450%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=GyivNuda31%2FPXJQQ4Z9tK2cFtj3N9YcvRdtM7rVkhHg%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%7Cd83f36080fd94b563ab608d9b44d4d1f%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637739066369079450%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=esqOKjKS5JZDbNBmAi0Bwwk0JTTHzInQ2Lgeq%2BPHJ9w%3D&reserved=0
> Best wishes,
> Navid.

More information about the Gcc-patches mailing list