Skip to content

Commit 03da079

Browse files
authored
[LoopUtils] Saturate at INT_MAX when estimating TC (llvm#129683)
getLoopEstimatedTripCount returns std::nullopt when the trip count would overflow the return type, but since it is an estimate anyway, we might as well saturate at UINT_MAX, improving results.
1 parent db70d76 commit 03da079

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

llvm/include/llvm/Transforms/Utils/LoopUtils.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,8 @@ void addStringMetadataToLoop(Loop *TheLoop, const char *MDString,
318318
/// Returns a loop's estimated trip count based on branch weight metadata.
319319
/// In addition if \p EstimatedLoopInvocationWeight is not null it is
320320
/// initialized with weight of loop's latch leading to the exit.
321-
/// Returns a valid positive trip count, or std::nullopt when a meaningful
322-
/// estimate cannot be made (including when the trip count wouldn't fit in the
323-
/// result type).
321+
/// Returns a valid positive trip count, saturated at UINT_MAX, or std::nullopt
322+
/// when a meaningful estimate cannot be made.
324323
std::optional<unsigned>
325324
getLoopEstimatedTripCount(Loop *L,
326325
unsigned *EstimatedLoopInvocationWeight = nullptr);

llvm/lib/Transforms/Utils/LoopUtils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -843,9 +843,9 @@ static std::optional<unsigned> getEstimatedTripCount(BranchInst *ExitingBranch,
843843
// edge exiting the loop, rounded to nearest.
844844
uint64_t ExitCount = llvm::divideNearest(LoopWeight, ExitWeight);
845845

846-
// When ExitCount + 1 would wrap in unsigned, return std::nullopt instead.
846+
// When ExitCount + 1 would wrap in unsigned, saturate at UINT_MAX.
847847
if (ExitCount >= std::numeric_limits<unsigned>::max())
848-
return std::nullopt;
848+
return std::numeric_limits<unsigned>::max();
849849

850850
// Estimated trip count is one plus estimated exit count.
851851
return ExitCount + 1;

llvm/test/Transforms/LoopVectorize/AArch64/low_trip_memcheck_cost.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ outer.exit:
180180
define void @outer_pgo_minus1(ptr nocapture noundef %a, ptr nocapture noundef readonly %b, i64 noundef %m, i64 noundef %n) {
181181
; CHECK-LABEL: LV: Checking a loop in 'outer_pgo_minus1'
182182
; CHECK: Calculating cost of runtime checks:
183-
; CHECK: We expect runtime memory checks to be hoisted out of the outer loop. Cost reduced from 6 to 3
184-
; CHECK: Total cost of runtime checks: 3
183+
; CHECK: We expect runtime memory checks to be hoisted out of the outer loop. Cost reduced from 6 to 1
184+
; CHECK: Total cost of runtime checks: 1
185185
; CHECK-NEXT: LV: Minimum required TC for runtime checks to be profitable:16
186186
entry:
187187
br label %outer.loop

0 commit comments

Comments
 (0)