<div dir="ltr">> Any idea about how the compiler could remove the lshr and use a add -16?<br>Actually, I just figured that doing this test is like solving this:<br><br>8 <= x/2 <= 13<br>16 <= x <= 26<br>0 <= x - 16 <= 10 => 0 <= x < 11<br>The left part is know since it's unsigned<br>The right part could be done x <= 11 => x < 12 because it's actually an integer division.<br>Wow... I would be really happy to know what pass does that.<br><br>Best,<br>- Stefanos</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Στις Δευ, 6 Απρ 2020 στις 12:00 π.μ., ο/η Stefanos Baziotis <<a href="mailto:stefanos.baziotis@gmail.com">stefanos.baziotis@gmail.com</a>> έγραψε:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi,<br><br>> I think the IR in both of your examples makes things harder for the compiler than expected from the original C source.<br><br>Note that both versions are from clang with -O2. The first is with version 9.0 and the second is with the trunk.<br><br>> but in the branch only %0 is used. Sinking the lshr too early made the analysis harder.<br><br>Yes, exactly! That's what I figured too.<br><br>> The version in <a href="https://godbolt.org/z/_ipKhb" target="_blank">https://godbolt.org/z/_ipKhb</a>  is probably the easiest for analysis (basically the original C source code built with `clang -O0 -S -emit-llvm`, followed by running `opt -mem2reg`). There’s a patch under review that adds support for conditional range propagation (<a href="https://reviews.llvm.org/D76611" target="_blank">https://reviews.llvm.org/D76611</a>) and with the patch it can be simplified to the code below by running `opt  -ipsccp -simplifycfg -instcombine`<br>> <br>> define i32 @test(i32 %0) {<br>>   %.off = add i32 %0, -16<br>>   %2 = icmp ult i32 %.off, 12<br>>   %spec.select = select i1 %2, i32 66, i32 45<br>>   ret i32 %spec.select<br>> }<br>> <br>> <br>> The reason it does not yet work in the default pipeline is that the branch condition will be combined to use an AND before the conditional propagation and D76611 does not support that yet. But once it lands that should be an easy extension.<br>> <br>> The problems with your two versions could be addressed as well of course for that special case relatively easily I think, but the challenge is to fix it in a general and efficient (compile-time wise) way. I hope the conditional propagation should cover such cases soon though.<br><br>Ok, I checked the patch, best of luck! I see how it does the transformation, it's interesting. I think that <a href="https://reviews.llvm.org/rGe9963034314edf49a12ea5e29f694d8f9f52734a" target="_blank">https://reviews.llvm.org/rGe9963034314edf49a12ea5e29f694d8f9f52734a</a> could maybe help in such things.<br>Any idea about how the compiler could remove the lshr and use a add -16?<br><br>Best,<br>Stefanos Baziotis<br><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Στις Κυρ, 5 Απρ 2020 στις 10:06 μ.μ., ο/η Florian Hahn <<a href="mailto:florian_hahn@apple.com" target="_blank">florian_hahn@apple.com</a>> έγραψε:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi,<br>
<br>
> On Apr 5, 2020, at 14:08, Stefanos Baziotis via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
> To be more specific for everyone:<br>
> - First of all, I hope it's clear that in the original (C) code, the region - 0x8 > 1000 branch should<br>
> be eliminated. That is because it is inside a block that has ensured that 8 < region < 12. But for some reason,<br>
> it is not eliminated. I'm trying to find that reason. :)<br>
> - I understand that in the -O2 .ll output, we take %0 and do it a right shift. That means that we don't have<br>
> any range info about %0 or %0 >>= 1, so we can't eliminate the branch. What I don't understand<br>
> is why we chose to reuse %0 and do it a right shift and instead we didn't have a phi there.<br>
> - Finally, I saw that putting nuw in the addition with -8 eliminates the branch. This makes sense since,<br>
> we know probably know from the code above that %0 >>= 1 is less than 12. Although, I don't understand<br>
> why the right shift was translated to an add with -16.<br>
> <br>
> I hope this made some sense. You may ignore the last 2 and focus on the first, i.e. what optimization<br>
> should have been done but it's not and what we can do about it.<br>
> <br>
> A clearer version of the .ll: <a href="https://godbolt.org/z/2t4RU5" rel="noreferrer" target="_blank">https://godbolt.org/z/2t4RU5</a><br>
<br>
I think the IR in both of your examples makes things harder for the compiler than expected from the original C source.<br>
<br>
With the IR in your original example (<a href="https://godbolt.org/z/BL-4jL" rel="noreferrer" target="_blank">https://godbolt.org/z/BL-4jL</a>), I think the problem is that the branch condition is '%0 - 16 < 12’, which allows us to limit the range of `%0 - 16` easily, but in the branch only %0 is used. Sinking the lshr too early made the analysis harder.<br>
<br>
In the second example (<a href="https://godbolt.org/z/2t4RU5" rel="noreferrer" target="_blank">https://godbolt.org/z/2t4RU5</a>), things are hard to simplify because we need to use the information from the condition of one select to simplify the earlier select. <br>
<br>
The version in <a href="https://godbolt.org/z/_ipKhb" rel="noreferrer" target="_blank">https://godbolt.org/z/_ipKhb</a>  is probably the easiest for analysis (basically the original C source code built with `clang -O0 -S -emit-llvm`, followed by running `opt -mem2reg`). There’s a patch under review that adds support for conditional range propagation (<a href="https://reviews.llvm.org/D76611" rel="noreferrer" target="_blank">https://reviews.llvm.org/D76611</a>) and with the patch it can be simplified to the code below by running `opt  -ipsccp -simplifycfg -instcombine`<br>
<br>
define i32 @test(i32 %0) {<br>
  %.off = add i32 %0, -16<br>
  %2 = icmp ult i32 %.off, 12<br>
  %spec.select = select i1 %2, i32 66, i32 45<br>
  ret i32 %spec.select<br>
}<br>
<br>
<br>
The reason it does not yet work in the default pipeline is that the branch condition will be combined to use an AND before the conditional propagation and D76611 does not support that yet. But once it lands that should be an easy extension.<br>
<br>
The problems with your two versions could be addressed as well of course for that special case relatively easily I think, but the challenge is to fix it in a general and efficient (compile-time wise) way. I hope the conditional propagation should cover such cases soon though.<br>
<br>
Cheers,<br>
Florian</blockquote></div>
</blockquote></div>