llvm.org GIT mirror llvm / d7e1379
[PlaceSafepoints] Make the width of a counted loop settable. Summary: This change lets a `PlaceSafepoints` client change how wide the trip count of a loop has to be for the loop to be considerd "counted", via `CountedLoopTripWidth`. It also removes the boolean `SkipCounted` flag and the `upperTripBound` constant -- we can get the old behavior of `SkipCounted` == `false` by setting `CountedLoopTripWidth` to `13` (2 ^ 13 == 8192). Reviewers: reames Subscribers: llvm-commits, sanjoy Differential Revision: http://reviews.llvm.org/D12789 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@247656 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjoy Das 4 years ago
2 changed file(s) with 68 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
9595 static cl::opt AllBackedges("spp-all-backedges", cl::Hidden,
9696 cl::init(false));
9797
98 /// If true, do not place backedge safepoints in counted loops.
99 static cl::opt SkipCounted("spp-counted", cl::Hidden, cl::init(true));
98 /// How narrow does the trip count of a loop have to be to have to be considered
99 /// "counted"? Counted loops do not get safepoints at backedges.
100 static cl::opt CountedLoopTripWidth("spp-counted-loop-trip-width",
101 cl::Hidden, cl::init(32));
100102
101103 // If true, split the backedge of a loop when placing the safepoint, otherwise
102104 // split the latch block itself. Both are useful to support for
254256 /// conservatism in the analysis.
255257 static bool mustBeFiniteCountedLoop(Loop *L, ScalarEvolution *SE,
256258 BasicBlock *Pred) {
257 // Only used when SkipCounted is off
258 const unsigned upperTripBound = 8192;
259
260259 // A conservative bound on the loop as a whole.
261260 const SCEV *MaxTrips = SE->getMaxBackedgeTakenCount(L);
262 if (MaxTrips != SE->getCouldNotCompute()) {
263 if (SE->getUnsignedRange(MaxTrips).getUnsignedMax().ult(upperTripBound))
264 return true;
265 if (SkipCounted &&
266 SE->getUnsignedRange(MaxTrips).getUnsignedMax().isIntN(32))
267 return true;
268 }
261 if (MaxTrips != SE->getCouldNotCompute() &&
262 SE->getUnsignedRange(MaxTrips).getUnsignedMax().isIntN(
263 CountedLoopTripWidth))
264 return true;
269265
270266 // If this is a conditional branch to the header with the alternate path
271267 // being outside the loop, we can ask questions about the execution frequency
274270 // This returns an exact expression only. TODO: We really only need an
275271 // upper bound here, but SE doesn't expose that.
276272 const SCEV *MaxExec = SE->getExitCount(L, Pred);
277 if (MaxExec != SE->getCouldNotCompute()) {
278 if (SE->getUnsignedRange(MaxExec).getUnsignedMax().ult(upperTripBound))
273 if (MaxExec != SE->getCouldNotCompute() &&
274 SE->getUnsignedRange(MaxExec).getUnsignedMax().isIntN(
275 CountedLoopTripWidth))
279276 return true;
280 if (SkipCounted &&
281 SE->getUnsignedRange(MaxExec).getUnsignedMax().isIntN(32))
282 return true;
283 }
284277 }
285278
286279 return /* not finite */ false;
0 ; Tests to ensure that we are not placing backedge safepoints in
11 ; loops which are clearly finite.
2 ;; RUN: opt %s -place-safepoints -S | FileCheck %s
2 ;; RUN: opt %s -place-safepoints -spp-counted-loop-trip-width=32 -S | FileCheck %s
3 ;; RUN: opt %s -place-safepoints -spp-counted-loop-trip-width=64 -S | FileCheck %s -check-prefix=COUNTED-64
34
45
56 ; A simple counted loop with trivially known range
6869 ret void
6970 }
7071
72 ; The range is a 64 bit value
73 define void @test4(i64 %upper) gc "statepoint-example" {
74 ; CHECK-LABEL: test4
75 ; CHECK-LABEL: entry
76 ; CHECK: statepoint
77 ; CHECK-LABEL: loop
78 ; CHECK: statepoint
79
80 ; COUNTED-64-LABEL: test4
81 ; COUNTED-64-LABEL: entry
82 ; COUNTED-64: statepoint
83 ; COUNTED-64-LABEL: loop
84 ; COUNTED-64-NOT: statepoint
85
86 entry:
87 br label %loop
88
89 loop:
90 %counter = phi i64 [ 0 , %entry ], [ %counter.inc , %loop ]
91 %counter.inc = add i64 %counter, 1
92 %counter.cmp = icmp slt i64 %counter.inc, %upper
93 br i1 %counter.cmp, label %loop, label %exit
94
95 exit:
96 ret void
97 }
98
99 ; This loop can run infinitely (for %upper == INT64_MAX) so it needs a
100 ; safepoint.
101 define void @test5(i64 %upper) gc "statepoint-example" {
102 ; CHECK-LABEL: test5
103 ; CHECK-LABEL: entry
104 ; CHECK: statepoint
105 ; CHECK-LABEL: loop
106 ; CHECK: statepoint
107
108 ; COUNTED-64-LABEL: test5
109 ; COUNTED-64-LABEL: entry
110 ; COUNTED-64: statepoint
111 ; COUNTED-64-LABEL: loop
112 ; COUNTED-64: statepoint
113
114 entry:
115 br label %loop
116
117 loop:
118 %counter = phi i64 [ 0 , %entry ], [ %counter.inc , %loop ]
119 %counter.inc = add i64 %counter, 1
120 %counter.cmp = icmp sle i64 %counter.inc, %upper
121 br i1 %counter.cmp, label %loop, label %exit
122
123 exit:
124 ret void
125 }
126
71127
72128 ; This function is inlined when inserting a poll.
73129 declare void @do_safepoint()