llvm.org GIT mirror llvm / 6f2ae41
Merge 183060: Prevent loop-unroll from making assumptions about undefined behavior. Fixes rdar:14036816, PR16130. There is an opportunity to compute precise trip counts for 'or' expressions and multi-exit loops. rdar:14038809: Optimize trip count computation for multi-exit loops. To do this we need to record the fact that ExitLimit assumes NSW. When it does not we can safely assume that the loop trip count is the minimum ExitLimt across all subexpressions and loop exits. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@183060 91177308-0d34-0410-b5e6-96231b3b80d8 git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_33@183066 91177308-0d34-0410-b5e6-96231b3b80d8 Andrew Trick 7 years ago
3 changed file(s) with 76 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
39363936 /// before taking the branch. For loops with multiple exits, it may not be the
39373937 /// number times that the loop header executes because the loop may exit
39383938 /// prematurely via another branch.
3939 ///
3940 /// FIXME: We conservatively call getBackedgeTakenCount(L) instead of
3941 /// getExitCount(L, ExitingBlock) to compute a safe trip count considering all
3942 /// loop exits. getExitCount() may return an exact count for this branch
3943 /// assuming no-signed-wrap. The number of well-defined iterations may actually
3944 /// be higher than this trip count if this exit test is skipped and the loop
3945 /// exits via a different branch. Ideally, getExitCount() would know whether it
3946 /// depends on a NSW assumption, and we would only fall back to a conservative
3947 /// trip count in that case.
39393948 unsigned ScalarEvolution::
3940 getSmallConstantTripCount(Loop *L, BasicBlock *ExitingBlock) {
3949 getSmallConstantTripCount(Loop *L, BasicBlock */*ExitingBlock*/) {
39413950 const SCEVConstant *ExitCount =
3942 dyn_cast(getExitCount(L, ExitingBlock));
3951 dyn_cast(getBackedgeTakenCount(L));
39433952 if (!ExitCount)
39443953 return 0;
39453954
39663975 /// As explained in the comments for getSmallConstantTripCount, this assumes
39673976 /// that control exits the loop via ExitingBlock.
39683977 unsigned ScalarEvolution::
3969 getSmallConstantTripMultiple(Loop *L, BasicBlock *ExitingBlock) {
3970 const SCEV *ExitCount = getExitCount(L, ExitingBlock);
3978 getSmallConstantTripMultiple(Loop *L, BasicBlock */*ExitingBlock*/) {
3979 const SCEV *ExitCount = getBackedgeTakenCount(L);
39713980 if (ExitCount == getCouldNotCompute())
39723981 return 1;
39733982
39964005 }
39974006
39984007 // getExitCount - Get the expression for the number of loop iterations for which
3999 // this loop is guaranteed not to exit via ExitintBlock. Otherwise return
4008 // this loop is guaranteed not to exit via ExitingBlock. Otherwise return
40004009 // SCEVCouldNotCompute.
40014010 const SCEV *ScalarEvolution::getExitCount(Loop *L, BasicBlock *ExitingBlock) {
40024011 return getBackedgeTakenInfo(L).getExact(ExitingBlock, this);
6565
6666 ; SCEV properly unrolls multi-exit loops.
6767 ;
68 ; SCEV cannot currently unroll this loop.
69 ; It should ideally detect a trip count of 5.
70 ; rdar:14038809 [SCEV]: Optimize trip count computation for multi-exit loops.
6871 ; CHECK: @multiExit
69 ; CHECK: getelementptr i32* %base, i32 10
70 ; CHECK-NEXT: load i32*
71 ; CHECK: br i1 false, label %l2.10, label %exit1
72 ; CHECK: l2.10:
73 ; CHECK-NOT: br
74 ; CHECK: ret i32
72 ; CHECKFIXME: getelementptr i32* %base, i32 10
73 ; CHECKFIXME-NEXT: load i32*
74 ; CHECKFIXME: br i1 false, label %l2.10, label %exit1
75 ; CHECKFIXME: l2.10:
76 ; CHECKFIXME-NOT: br
77 ; CHECKFIXME: ret i32
7578 define i32 @multiExit(i32* %base) nounwind {
7679 entry:
7780 br label %l1
169172 br label %for.body87
170173 }
171174
175 ; PR16130: clang produces incorrect code with loop/expression at -O2
176 ; rdar:14036816 loop-unroll makes assumptions about undefined behavior
177 ;
178 ; The loop latch is assumed to exit after the first iteration because
179 ; of the induction variable's NSW flag. However, the loop latch's
180 ; equality test is skipped and the loop exits after the second
181 ; iteration via the early exit. So loop unrolling cannot assume that
182 ; the loop latch's exit count of zero is an upper bound on the number
183 ; of iterations.
184 ;
185 ; CHECK: @nsw_latch
186 ; CHECK: for.body:
187 ; CHECK: %b.03 = phi i32 [ 0, %entry ], [ %add, %for.cond ]
188 ; CHECK: return:
189 ; CHECK: %b.03.lcssa = phi i32 [ %b.03, %for.body ], [ %b.03, %for.cond ]
190 define void @nsw_latch(i32* %a) nounwind {
191 entry:
192 br label %for.body
193
194 for.body: ; preds = %for.cond, %entry
195 %b.03 = phi i32 [ 0, %entry ], [ %add, %for.cond ]
196 %tobool = icmp eq i32 %b.03, 0
197 %add = add nsw i32 %b.03, 8
198 br i1 %tobool, label %for.cond, label %return
199
200 for.cond: ; preds = %for.body
201 %cmp = icmp eq i32 %add, 13
202 br i1 %cmp, label %return, label %for.body
203
204 return: ; preds = %for.body, %for.cond
205 %b.03.lcssa = phi i32 [ %b.03, %for.body ], [ %b.03, %for.cond ]
206 %retval.0 = phi i32 [ 1, %for.body ], [ 0, %for.cond ]
207 store i32 %b.03.lcssa, i32* %a, align 4
208 ret void
209 }
2020 inner:
2121 %iv = phi i32 [ 0, %outer ], [ %inc, %tail ]
2222 %inc = add i32 %iv, 1
23 %wbucond = call zeroext i1 @check()
24 br i1 %wbucond, label %outer.backedge, label %tail
23 call zeroext i1 @check()
24 br i1 true, label %outer.backedge, label %tail
2525
2626 tail:
2727 br i1 false, label %inner, label %exit
125125 ; Ensure that only the middle loop is removed and rely on verify-loopinfo to
126126 ; check soundness.
127127 ;
128 ; CHECK: @unloopDeepNested
128 ; This test must be disabled until trip count computation can be optimized...
129 ; rdar:14038809 [SCEV]: Optimize trip count computation for multi-exit loops.
130 ; CHECKFIXME: @unloopDeepNested
129131 ; Inner-inner loop control.
130 ; CHECK: while.cond.us.i:
131 ; CHECK: br i1 %cmp.us.i, label %next_data.exit, label %while.body.us.i
132 ; CHECK: if.then.us.i:
133 ; CHECK: br label %while.cond.us.i
132 ; CHECKFIXME: while.cond.us.i:
133 ; CHECKFIXME: br i1 %cmp.us.i, label %next_data.exit, label %while.body.us.i
134 ; CHECKFIXME: if.then.us.i:
135 ; CHECKFIXME: br label %while.cond.us.i
134136 ; Inner loop tail.
135 ; CHECK: if.else.i:
136 ; CHECK: br label %while.cond.outer.i
137 ; CHECKFIXME: if.else.i:
138 ; CHECKFIXME: br label %while.cond.outer.i
137139 ; Middle loop control (removed).
138 ; CHECK: valid_data.exit:
139 ; CHECK-NOT: br
140 ; CHECK: %cmp = call zeroext i1 @check()
140 ; CHECKFIXME: valid_data.exit:
141 ; CHECKFIXME-NOT: br
142 ; CHECKFIXME: %cmp = call zeroext i1 @check()
141143 ; Outer loop control.
142 ; CHECK: copy_data.exit:
143 ; CHECK: br i1 %cmp38, label %if.then39, label %while.cond.outer
144 ; CHECKFIXME: copy_data.exit:
145 ; CHECKFIXME: br i1 %cmp38, label %if.then39, label %while.cond.outer
144146 ; Outer-outer loop tail.
145 ; CHECK: while.cond.outer.outer.backedge:
146 ; CHECK: br label %while.cond.outer.outer
147 ; CHECKFIXME: while.cond.outer.outer.backedge:
148 ; CHECKFIXME: br label %while.cond.outer.outer
147149 define void @unloopDeepNested() nounwind {
148150 for.cond8.preheader.i:
149151 %cmp113.i = call zeroext i1 @check()