@@ -875,16 +875,21 @@ llvm::getLoopEstimatedTripCount(Loop *L,
875
875
// mistaking LLVMLoopEstimatedTripCount metadata to be for an outer loop when
876
876
// it was created for an inner loop. The problem is that loop metadata is
877
877
// attached to the branch instruction in the loop latch block, but that can be
878
- // shared by the loops. The solution is to attach loop metadata to loop
879
- // headers instead, but that would be a large change to LLVM.
878
+ // shared by the loops. A solution is to attach loop metadata to loop headers
879
+ // instead, but that would be a large change to LLVM.
880
880
//
881
881
// Until that happens, we work around the problem as follows.
882
882
// getExpectedExitLoopLatchBranch (which also guards
883
- // setLoopEstimatedTripCount) will not recognize the same latch for both loops
884
- // unless the latch exits both loops and has only two successors. However, to
885
- // exit both loops, the latch must have at least three successors: the inner
886
- // loop header, the outer loop header (exit for the inner loop), and an exit
887
- // for the outer loop.
883
+ // setLoopEstimatedTripCount) returns nullptr for a loop unless the loop has
884
+ // one latch and that latch has exactly two successors one of which is an exit
885
+ // from the loop. If the latch is shared by nested loops, then that condition
886
+ // might hold for the inner loop but cannot hold for the outer loop:
887
+ // - Because the latch is shared, it must have at least two successors: the
888
+ // inner loop header and the outer loop header, which is also an exit for
889
+ // the inner loop. That satisifies the condition for the inner loop.
890
+ // - To satsify the condition for the outer loop, the latch must have a third
891
+ // successor that is an exit for the outer loop. But that violates the
892
+ // condition for both loops.
888
893
BranchInst *ExitingBranch = getExpectedExitLoopLatchBranch (L);
889
894
if (!ExitingBranch)
890
895
return std::nullopt ;
0 commit comments