<div dir="ltr">Yeah, there's a lot of things this could be.<div><br></div><div>On the memdep side:<br><div>Note that memdep is not actually properly updated in all cases by most passes that claim to not invalidate it (they don't invalidate dependent pointers, only pointers they directly touch).</div><div><br></div><div>There's already a bug filed about this.  So far we've only seen missed-opt, not wrong code from this.</div><div>But it should be possible to generate wrong code bugs with it (if you place the load/store in a place that invalidates the cached dependent result) , so i wonder if this is one.</div><div><div><br class="gmail-Apple-interchange-newline">Otherwise, the other reason it could give different answers after running a bunch of passes is the random scanning limits it has.</div></div><div><br></div><div>On the PRE side:<br>PHITranslateAddr goes through a bunch of crazy machinations to try to be able to prove things about inter-iteration variables instead of just doing phi translation.</div><div><br></div><div>LoopLoadElimination was written so that these could be removed, because they are pretty much crazytown.</div><div><br></div><div><br></div><div><br></div><div>--Dan</div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 13, 2017 at 10:31 AM, Friedman, Eli via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On 1/13/2017 12:31 AM, Mikael Holmén via llvm-dev wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
I've stumbled upon a case where I think gvn does a bad (wrong) optimization. It's a bit messy to debug though so I'm not sure if I should just write a PR about it a let someone who knows the code look at it instead.<br>
<br>
Anyway, for the bug to trigger I need to run the following passes in the same opt invocation:<br>
 -sroa -instcombine -simplifycfg -instcombine -gvn<br>
<br>
The problem seems to be that GVN::PerformLoadPRE triggers and I see a<br>
<br>
GVN REMOVING PRE LOAD:   %_tmp79 = load i16, i16* %_tmp78, align 2<br>
<br>
printout.<br>
<br>
If I instead first run<br>
<br>
 -sroa -instcombine -simplifycfg -instcombine<br>
<br>
and then<br>
<br>
 -gvn<br>
<br>
I don't get the<br>
<br>
GVN REMOVING PRE LOAD<br>
<br>
printout, and the resulting code looks ok to me.<br>
<br>
Is this expected? Should the output differ in these two cases?<br>
<br>
<br>
The input to gvn looks the same when running all passes and just gvn, but I see a difference in how GVN::processNonLocalLoad(LoadI<wbr>nst *LI) behaves:<br>
<br>
I get different results from<br>
<br>
  // Step 1: Find the non-local dependencies of the load.<br>
  LoadDepVect Deps;<br>
  MD->getNonLocalPointerDependen<wbr>cy(LI, Deps);<br>
<br>
So we get different results from MemoryDependenceResults when invoking gvn alone or after a bunch of other passes.<br>
<br>
Is this expected or not?<br>
<br>
<br>
I tried to dig into MemoryDependenceResults::getNo<wbr>nLocalPointerDependency but I have to say I'm quite lost.<br>
<br>
I ran some git bisect and as far as I can tell it starts going wrong with commit<br>
<br>
 [SimplifyCFG] Change the algorithm in SinkThenElseCodeToEnd<br>
<br>
2016-09-01, but that commit doesn't change GVN/MemoryDependenceResults so I suppose it just changes the input to GVN so the problem suddenly surfaces.<br>
<br>
Finally, the problem that I see is:<br>
<br>
In the input we have something like<br>
<br>
    for (int step1 = 0; step1 < LOOP_AMOUNT; step1++)<br>
    {<br>
        lb[step1] = step1 + 7;<br>
        ub[step1] = (step1 == 0 ? 10: ub[step1-1]*10);<br>
<br>
        switch(lb[step1]) {<br>
        case 7:    CVAL_VERIFY(step1 == 0);<br>
            CVAL_VERIFY(ub[step1] == 10);<br>
                        __label(511);<br>
            break;<br>
        case 8:    CVAL_VERIFY(step1 == 1);<br>
            CVAL_VERIFY(ub[step1] == 100);<br>
                        __label(512);<br>
            break;<br>
        case 9:    CVAL_VERIFY(step1 == 2);<br>
            CVAL_VERIFY(ub[step1] == 1000);<br>
                        __label(513);<br>
            break;<br>
        default:;<br>
        }<br>
<br>
                [...]<br>
    }<br>
<br>
    int i, j;<br>
    for ( j = 10, i = 0; j < 101; j=j*10, i++ )<br>
    {<br>
        CVAL_VERIFY(ub[i] == j);<br>
    }<br>
<br>
So we have two loops. In the first one the array ub is written (low index first, high index last), and then in the second loop the content of ub is checked (low index first, high index last).<br>
<br>
After gvn this is changed into something like<br>
<br>
        int tmp;<br>
    for (int step1 = 0; step1 < LOOP_AMOUNT; step1++)<br>
    {<br>
        lb[step1] = step1 + 7;<br>
        ub[step1] = (step1 == 0 ? 10: ub[step1-1]*10);<br>
<br>
        switch(lb[step1]) {<br>
        case 7:    CVAL_VERIFY(step1 == 0);<br>
                        tmp = ub[step1];<br>
            CVAL_VERIFY(tmp == 10);<br>
                        __label(511);<br>
            break;<br>
        case 8:    CVAL_VERIFY(step1 == 1);<br>
                        tmp = ub[step1];<br>
            CVAL_VERIFY(tmp == 100);<br>
                        __label(512);<br>
            break;<br>
        case 9:    CVAL_VERIFY(step1 == 2);<br>
                        tmp = ub[step1];<br>
            CVAL_VERIFY(tmp == 1000);<br>
                        __label(513);<br>
            break;<br>
        default:;<br>
        }<br>
<br>
                [...]<br>
    }<br>
<br>
    int i, j;<br>
    for ( j = 10, i = 0; j < 101; j=j*10, i++ )<br>
    {<br>
        CVAL_VERIFY((i == 0 ? tmp : ub[i]) == j);<br>
    }<br>
<br>
Note the introduced variable tmp.<br>
<br>
Also note that with this rewrite, after the first loop tmp will contain the value of the element at the highest index in ub, and then we use tmp in the first round of the second loop, but there we expect the value of the lowest index element in ub.<br>
<br>
Any ideas? Should I file a PR? <br>
</blockquote>
<br></div></div>
Ooh, wow, that's nasty: it looks like PRE is somehow deciding that ub[step1] and ub[0] are equivalent.  Please file a bug.<br>
<br>
The reason you're having trouble splitting the "opt" invocation is that you aren't passing in "-preserve-ll-uselistorder"; it usually doesn't matter, but occasionally you'll run into an optimization which is sensitive to it.<br>
<br>
-Eli<span class="HOEnZb"><font color="#888888"><br>
<br>
-- <br>
Employee of Qualcomm Innovation Center, Inc.<br>
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project<br>
<br>
______________________________<wbr>_________________<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="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
</font></span></blockquote></div><br></div>