<div dir="ltr">Looks like the expand memcmp pass expanded the bcmp. Then ran InstructionSimplify on every basic block in the function because a change was made. That decided that the compare was always false. But I'm not sure it had anything to do with the bcmp expansion.<br clear="all"><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><br></div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">~Craig</div></div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 17, 2021 at 9:42 PM Johannes Doerfert <<a href="mailto:johannesdoerfert@gmail.com">johannesdoerfert@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">So I looked at it again because the O3 output in IR has the<br>
return for trunk and 11 but for the latter it's not there in assembly,<br>
this was looking interesting: <a href="https://godbolt.org/z/EqhW4v" rel="noreferrer" target="_blank">https://godbolt.org/z/EqhW4v</a><br>
<br>
I think there is UB in the jump based on the memcmp because only<br>
one byte of the 3 is initialized. To me it seems there is some type<br>
punning going on and the two integers are used as storage for 3 bytes<br>
instead of the 3 bytes array. I thought this may lead to the<br>
selection of the abort branch and bad consequences. However, on<br>
the IR level we do not recognize abort as noreturn as far as I can tell,<br>
thus that is not it.<br>
<br>
I took the IR 11 produced as input and run it again through 11 and trunk,<br>
same result. Then I replaced the bcmp with `f` and the return magically<br>
appeared for 11 too: <a href="https://godbolt.org/z/j4T4hW" rel="noreferrer" target="_blank">https://godbolt.org/z/j4T4hW</a><br>
<br>
We are on to something here. bcmp is recognized and we act on it. The<br>
UB described earlier is present there too, bcmp compares 3 bytes in the two<br>
integer struct instead of the 3 byte array.<br>
<br>
I can manually do the optimization in the middle end that LLVM 11 does<br>
(removed two redundant stores from the entry) but no change: <br>
<a href="https://godbolt.org/z/7efdqb" rel="noreferrer" target="_blank">https://godbolt.org/z/7efdqb</a><br>
At this point I would guess the backend knows bcmp and utilizes the UB.<br>
<br>
Whatever it is, as far as I can tell it is technically correct given the UB<br>
in the input. However, understanding what made the difference might be worth<br>
while nevertheless.<br>
<br>
~ Johannes<br>
<br>
<br>
<br>
On 2/17/21 7:01 PM, Craig Topper wrote:<br>
> I ran the IR through clang -O2 on godbolt and didn't get a return. So I<br>
> think something is happening in the middle end?<br>
><br>
> ~Craig<br>
><br>
><br>
> On Wed, Feb 17, 2021 at 4:57 PM Johannes Doerfert <<br>
> <a href="mailto:johannesdoerfert@gmail.com" target="_blank">johannesdoerfert@gmail.com</a>> wrote:<br>
><br>
>> llc generates the return in all version I tried:<br>
>> <a href="https://godbolt.org/z/oh3fqh" rel="noreferrer" target="_blank">https://godbolt.org/z/oh3fqh</a><br>
>><br>
>><br>
>> On 2/17/21 6:46 PM, Craig Topper wrote:<br>
>>> What version of llvm are you using? Godbolt is showing trunk and llvm 10<br>
>>> have a conditional branch, llvm 11 does not.<br>
>>><br>
>>> ~Craig<br>
>>><br>
>>><br>
>>> On Wed, Feb 17, 2021 at 4:41 PM Richard Kenner via llvm-dev <<br>
>>> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
>>><br>
>>>>> Long story short, from what I can see there is no miscompilation<br>
>>>>> or change in semantics for that matter.<br>
>>>> So why does the .s file not contain the loop exit test?<br>
>>>><br>
>>>>           .text<br>
>>>>           .file   "c26006a.adb"<br>
>>>>           .globl  _ada_c26006a                    # -- Begin function<br>
>>>> _ada_c26006a<br>
>>>>           .p2align        4, 0x90<br>
>>>>           .type   _ada_c26006a,@function<br>
>>>> _ada_c26006a:                           # @_ada_c26006a<br>
>>>>           .cfi_startproc<br>
>>>> # %bb.0:                                # %entry<br>
>>>>           pushq   %rbx<br>
>>>>           .cfi_def_cfa_offset 16<br>
>>>>           subq    $32, %rsp<br>
>>>>           .cfi_def_cfa_offset 48<br>
>>>>           .cfi_offset %rbx, -16<br>
>>>>           movw    $8257, 16(%rsp)                 # imm = 0x2041<br>
>>>>           movb    $49, 18(%rsp)<br>
>>>>           movw    $8257, (%rsp)                   # imm = 0x2041<br>
>>>>           movb    $50, 2(%rsp)<br>
>>>>           xorl    %ebx, %ebx<br>
>>>>           jmp     .LBB0_1<br>
>>>>           .p2align        4, 0x90<br>
>>>> .LBB0_3:                                # %loop.cond.iter<br>
>>>>                                           #   in Loop: Header=BB0_1<br>
>> Depth=1<br>
>>>>           incb    %bl<br>
>>>> .LBB0_1:                                # %loop.cond<br>
>>>>                                           # =>This Inner Loop Header:<br>
>> Depth=1<br>
>>>>           movb    %bl, 17(%rsp)<br>
>>>>           movb    %bl, 1(%rsp)<br>
>>>>           movzwl  (%rsp), %eax<br>
>>>>           xorw    16(%rsp), %ax<br>
>>>>           movzbl  2(%rsp), %ecx<br>
>>>>           xorb    18(%rsp), %cl<br>
>>>>           movzbl  %cl, %ecx<br>
>>>>           orw     %ax, %cx<br>
>>>>           jne     .LBB0_3<br>
>>>> # %bb.2:                                #   in Loop: Header=BB0_1<br>
>> Depth=1<br>
>>>>           callq   abort<br>
>>>>           jmp     .LBB0_3<br>
>>>> .Lfunc_end0:<br>
>>>>           .size   _ada_c26006a, .Lfunc_end0-_ada_c26006a<br>
>>>>           .cfi_endproc<br>
>>>>                                           # -- End function<br>
>>>>           .section        ".note.GNU-stack","",@progbits<br>
>>>> _______________________________________________<br>
>>>> LLVM Developers mailing list<br>
>>>> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
>>>> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
>>>><br>
</blockquote></div>