<div dir="ltr"><div dir="ltr">On Thu, Feb 18, 2021 at 5:16 AM Richard Kenner <<a href="mailto:kenner@adacore.com">kenner@adacore.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">> Looks like the expand memcmp pass expanded the bcmp. Then ran<br>
> InstructionSimplify on every basic block in the function because a<br>
> change was made. That decided that the compare was always false. But<br>
> I'm not sure it had anything to do with the bcmp expansion.<br>
<br>
But that's not the relevant compare. That compare is actually<br>
provably false (abort is never called). The compare that's at issue<br>
is the loop end compare.<br></blockquote><div><br></div><div>Sorry, I wasn't clear on which compare. I was referring to the loop end compare. After the loop strength reduce pass in the llc codegen pipeline we have this IR.</div><div><br></div><div><span style="color:rgb(0,0,0);font-family:LucidaGrande;font-size:12px">%c.0 = phi i8 [ 0, %entry ], [ %next.loop.var, %loop.cond.iter ] </span></div><div>...</div><div><span style="color:rgb(0,0,0);font-family:LucidaGrande;font-size:12px">%next.loop.var = add nuw i8 %c.0, 1</span></div><div><span style="color:rgb(0,0,0);font-family:LucidaGrande;font-size:12px">%loop.iter.cond = icmp eq i8 %next.loop.var, 0 </span><br></div><div> </div><div>Then the memcmp expand pass runs and expands the bcmp. Because this made a change to the IR, it runs InstructionSimplify on every basic block in the function. Including basic blocks that didn't contain the bcmp. InstructionSimplify notices that loop end icmp uses an add nuw and phi that started at 0. Since the nuw says it can never get back to zero, the icmp is replaced with false.</div><div><br></div><div>Had the bcmp not been there, this late run of InstructionSimplify wouldn't have happened. There are no other optimizations that always run in this part of the pipeline that would see the bad IR created from loop strength reduce and optimize based on it. SelectionDAG can do optimizations with nuw, but it only runs on a single basic block so it won't see the phi.</div><div><br></div><div>Nikita's patch, 835104a1141a06ae7821fe2b642b9603e00aa17b removes the nuw from the add on trunk. I didn't look at why llvm 10 works.</div><div><br></div><div><br></div></div></div>