@@ -868,23 +868,42 @@ static std::optional<unsigned> estimateLoopTripCount(Loop *L) {
868
868
std::optional<unsigned >
869
869
llvm::getLoopEstimatedTripCount (Loop *L,
870
870
unsigned *EstimatedLoopInvocationWeight) {
871
+ // If EstimatedLoopInvocationWeight, we do not support this loop if
872
+ // getExpectedExitLoopLatchBranch returns nullptr.
873
+ //
874
+ // FIXME: Also, this is a stop-gap solution for nested loops. It avoids
875
+ // mistaking LLVMLoopEstimatedTripCount metadata to be for an outer loop when
876
+ // it was created for an inner loop. The problem is that loop metadata is
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.
880
+ //
881
+ // Until that happens, we work around the problem as follows.
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.
888
+ BranchInst *ExitingBranch = getExpectedExitLoopLatchBranch (L);
889
+ if (!ExitingBranch)
890
+ return std::nullopt ;
891
+
871
892
// If requested, either compute *EstimatedLoopInvocationWeight or return
872
893
// nullopt if cannot.
873
894
//
874
895
// TODO: Eventually, once all passes have migrated away from setting branch
875
896
// weights to indicate estimated trip counts, this function will drop the
876
897
// EstimatedLoopInvocationWeight parameter.
877
898
if (EstimatedLoopInvocationWeight) {
878
- if (BranchInst *ExitingBranch = getExpectedExitLoopLatchBranch (L)) {
879
- uint64_t LoopWeight = 0 , ExitWeight = 0 ; // Inits expected to be unused.
880
- if (!extractBranchWeights (*ExitingBranch, LoopWeight, ExitWeight))
881
- return std::nullopt ;
882
- if (L->contains (ExitingBranch->getSuccessor (1 )))
883
- std::swap (LoopWeight, ExitWeight);
884
- if (!ExitWeight)
885
- return std::nullopt ;
886
- *EstimatedLoopInvocationWeight = ExitWeight;
887
- }
899
+ uint64_t LoopWeight = 0 , ExitWeight = 0 ; // Inits expected to be unused.
900
+ if (!extractBranchWeights (*ExitingBranch, LoopWeight, ExitWeight))
901
+ return std::nullopt ;
902
+ if (L->contains (ExitingBranch->getSuccessor (1 )))
903
+ std::swap (LoopWeight, ExitWeight);
904
+ if (!ExitWeight)
905
+ return std::nullopt ;
906
+ *EstimatedLoopInvocationWeight = ExitWeight;
888
907
}
889
908
890
909
// Return the estimated trip count from metadata unless the metadata is
@@ -903,6 +922,15 @@ llvm::getLoopEstimatedTripCount(Loop *L,
903
922
bool llvm::setLoopEstimatedTripCount (
904
923
Loop *L, unsigned EstimatedTripCount,
905
924
std::optional<unsigned > EstimatedloopInvocationWeight) {
925
+ // If EstimatedLoopInvocationWeight, we do not support this loop if
926
+ // getExpectedExitLoopLatchBranch returns nullptr.
927
+ //
928
+ // FIXME: See comments in getLoopEstimatedTripCount for why this is required
929
+ // here regardless of EstimatedLoopInvocationWeight.
930
+ BranchInst *LatchBranch = getExpectedExitLoopLatchBranch (L);
931
+ if (!LatchBranch)
932
+ return false ;
933
+
906
934
// Set the metadata.
907
935
addStringMetadataToLoop (L, LLVMLoopEstimatedTripCount, EstimatedTripCount);
908
936
@@ -915,7 +943,6 @@ bool llvm::setLoopEstimatedTripCount(
915
943
// here at all.
916
944
if (!EstimatedloopInvocationWeight)
917
945
return true ;
918
- BranchInst *LatchBranch = getExpectedExitLoopLatchBranch (L);
919
946
if (!LatchBranch)
920
947
return false ;
921
948
0 commit comments