[PATCH] D87293: [GVN] Fix undef incoming value for phi node when new loop exit block created.

Alina Sbirlea via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 5 23:31:12 PDT 2020


asbirlea added inline comments.


================
Comment at: llvm/lib/Transforms/Scalar/GVN.cpp:2751
     }
 
+    for (BasicBlock *C : Candidates) {
----------------
wwei wrote:
> asbirlea wrote:
> > Could you please explain in more details the mechanics here? 
> > When added into Candidates, the llvm::any_of condition is true. What makes the condition become false below?
> > What's the purpose of the intersection of non-dead succs and preds?
> I have updated the code, and some comments to descibe the details for it.
> 
> ```
>     //     - - - - - - - -
>     //    |    ... ...    |
>     //    |[D1]           |   =>  A simplified loop<L>
>     //    | |  \          |
>     //    | |   [D2] [B1] |
>     //     -|- - |- - |- -
>     //      |    |    |
>     //      \    |    /
>     //       \   |   /
>     //        [ DF1 ]        => exit block for loop<L>
>     //
>     // Consider the above scenario, there're two dead blocks [D1] + [D2], and
> // a live block[B1]([D1][D2][B1] in one loop). [DF1] will a common dominance
>     // frontier block for [D1] and [D2], so Candidates will contain two elements
>     // [D1, D2]. [D1] has two succs: [D2] and [DF1], for [D1],first llvm::any_of
>     // will return true, so splitCriticalEdges will be called.
>     //
>     //     - - - - - - - -
>     //    |    ... ...    |
>     //    |[D1]           |   =>  A simplified loop<L>
>     //    | |  \          |
>     //    | |   [D2] [B1] |
>     //     -|- - |- - |- -
>     //      |    |    |
>     //      |    \    /
>     //     [D3]   [DF2]      => New Exit block[DF2] for loop<L>
>     //       \      |
>     //        [ DF1 ]
>     //
>     // After splitCriticalEdges, a new crit_edge block[D3] will be created, and
>     // it is added into dead blocks. Also, a new loop exit block[DF2] is created
>     // to preserve LoopSimplify. After that, llvm::any_of will return false for
>     // [D2], and we need to add [DF2] as a new dominance frontier block for [D2]
>     // since it has a pred edge to non-dead block[B1], and then, we need to
>     // update the undef incoming value for phi in [DF2].
> ```
> 
> About the purpose of the intersection of non-dead succs and preds, we should consider another case like below:
> 
> ```
> //     - - - - - - - -
> //    |    ... ...    |
> //    |    ... ...    |  =>  A simplified loop<L>
> //    | [  D1 ]  [B1] |
> //     -|- |- |- -|- - 
> //      |  |  |   |
> //      |  |  |   |
> //      \  |  |   |
> //       \ |  /  /
> //        [ DF1 ]        => exit block for loop<L>
> //
> // After split edeges,
> //     - - - - - - - -
> //    |    ... ...    |
> //    |    ... ...    |  =>  A simplified loop<L>
> //    | [  D1  ]  [B1]|
> //     -|-  |- |- -|-- 
> //      |   |  |   |
> //      |   |  \   /
> //     [D2][D3][DF2]     => New Exit block[DF2] for loop<L>
> //       \  |    /
> //        [ DF1 ]
> //
> //    switch-case: [  D1  ] will have multi edges to  [ DF1 ], after split,  [D2][D3] are new created,
> //    both will be dead blocks, so we need to use  the intersection of non-dead succs and preds to get [DF2]
> ```
Thank you for adding the example in code, that's very helpful!

I may be missing some details in the last example, but it seems like the non-dead predecessors for B (the IDF block) should cover the new blocks? (`[D2]`and `[D3]` should be in DeadBlocks following the split). 


================
Comment at: llvm/lib/Transforms/Scalar/GVN.cpp:2748
 
       if (llvm::any_of(successors(P),
                        [B](BasicBlock *Succ) { return Succ == B; }) &&
----------------
It doesn't look like this condition is needed here anymore, since splitting critical edges is done below. So just checking isCriticalEdge should be enough to make it a candidate.

Simplify this to iterate over B's predecessors directly; the vector is not needed when predecessors don't change.


================
Comment at: llvm/test/Transforms/GVN/pr47462.ll:1
+; RUN: opt -S -gvn -enable-load-pre < %s | FileCheck %s
+
----------------
Can you add a RUN line using the new pass manager? (`-passes=...`)


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D87293/new/

https://reviews.llvm.org/D87293



More information about the llvm-commits mailing list