llvm.org GIT mirror llvm / e27904f
[LoopSimplify] Re-instate r306081 with a bug fix w.r.t. indirectbr. This was reverted in r306252, but I already had the bug fixed and was just trying to form a test case. The original commit factored the logic for forming dedicated exits inside of LoopSimplify into a helper that could be used elsewhere and with an approach that required fewer intermediate data structures. See that commit for full details including the change to the statistic, etc. The code looked fine to me and my reviewers, but in fact didn't handle indirectbr correctly -- it left the 'InLoopPredecessors' vector dirty. If you have code that looks *just* right, you can end up leaking these predecessors into a subsequent rewrite, and crash deep down when trying to update PHI nodes for predecessors that don't exist. I've added an assert that makes the bug much more obvious, and then changed the code to reliably clear the vector so we don't get this bug again in some other form as the code changes. I've also added a test case that *does* manage to catch this while also giving some nice positive coverage in the face of indirectbr. The real code that found this came out of what I think is CPython's interpreter loop, but any code with really "creative" interpreter loops mixing indirectbr and other exit paths could manage to tickle the bug. I was hard to reduce the original test case because in addition to having a particular pattern of IR, the whole thing depends on the order of the predecessors which is in turn depends on use list order. The test case added here was designed so that in multiple different predecessor orderings it should always end up going down the same path and tripping the same bug. I hope. At least, it tripped it for me without manipulating the use list order which is better than anything bugpoint could do... git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@306257 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 2 years ago
7 changed file(s) with 175 addition(s) and 65 deletion(s). Raw diff Collapse all Expand all
361361 BasicBlock *InsertPreheaderForLoop(Loop *L, DominatorTree *DT, LoopInfo *LI,
362362 bool PreserveLCSSA);
363363
364 /// Ensure that all exit blocks of the loop are dedicated exits.
365 ///
366 /// For any loop exit block with non-loop predecessors, we split the loop
367 /// predecessors to use a dedicated loop exit block. We update the dominator
368 /// tree and loop info if provided, and will preserve LCSSA if requested.
369 bool formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI,
370 bool PreserveLCSSA);
371
364372 /// Ensures LCSSA form for every instruction from the Worklist in the scope of
365373 /// innermost containing loop.
366374 ///
7171
7272 #define DEBUG_TYPE "loop-simplify"
7373
74 STATISTIC(NumInserted, "Number of pre-header or exit blocks inserted");
7574 STATISTIC(NumNested , "Number of nested loops split out");
7675
7776 // If the block isn't already, move the new block to right after some 'outside
149148 placeSplitBlockCarefully(PreheaderBB, OutsideBlocks, L);
150149
151150 return PreheaderBB;
152 }
153
154 /// \brief Ensure that the loop preheader dominates all exit blocks.
155 ///
156 /// This method is used to split exit blocks that have predecessors outside of
157 /// the loop.
158 static BasicBlock *rewriteLoopExitBlock(Loop *L, BasicBlock *Exit,
159 DominatorTree *DT, LoopInfo *LI,
160 bool PreserveLCSSA) {
161 SmallVector LoopBlocks;
162 for (pred_iterator I = pred_begin(Exit), E = pred_end(Exit); I != E; ++I) {
163 BasicBlock *P = *I;
164 if (L->contains(P)) {
165 // Don't do this if the loop is exited via an indirect branch.
166 if (isa(P->getTerminator())) return nullptr;
167
168 LoopBlocks.push_back(P);
169 }
170 }
171
172 assert(!LoopBlocks.empty() && "No edges coming in from outside the loop?");
173 BasicBlock *NewExitBB = nullptr;
174
175 NewExitBB = SplitBlockPredecessors(Exit, LoopBlocks, ".loopexit", DT, LI,
176 PreserveLCSSA);
177 if (!NewExitBB)
178 return nullptr;
179
180 DEBUG(dbgs() << "LoopSimplify: Creating dedicated exit block "
181 << NewExitBB->getName() << "\n");
182 return NewExitBB;
183151 }
184152
185153 /// Add the specified block, and all of its predecessors, to the specified set,
345313
346314 // Split edges to exit blocks from the inner loop, if they emerged in the
347315 // process of separating the outer one.
348 SmallVector ExitBlocks;
349 L->getExitBlocks(ExitBlocks);
350 SmallSetVector ExitBlockSet(ExitBlocks.begin(),
351 ExitBlocks.end());
352 for (BasicBlock *ExitBlock : ExitBlockSet) {
353 if (any_of(predecessors(ExitBlock),
354 [L](BasicBlock *BB) { return !L->contains(BB); })) {
355 rewriteLoopExitBlock(L, ExitBlock, DT, LI, PreserveLCSSA);
356 }
357 }
316 formDedicatedExitBlocks(L, DT, LI, PreserveLCSSA);
358317
359318 if (PreserveLCSSA) {
360319 // Fix LCSSA form for L. Some values, which previously were only used inside
562521 BasicBlock *Preheader = L->getLoopPreheader();
563522 if (!Preheader) {
564523 Preheader = InsertPreheaderForLoop(L, DT, LI, PreserveLCSSA);
565 if (Preheader) {
566 ++NumInserted;
524 if (Preheader)
567525 Changed = true;
568 }
569526 }
570527
571528 // Next, check to make sure that all exit nodes of the loop only have
572529 // predecessors that are inside of the loop. This check guarantees that the
573530 // loop preheader/header will dominate the exit blocks. If the exit block has
574531 // predecessors from outside of the loop, split the edge now.
575 SmallVector ExitBlocks;
576 L->getExitBlocks(ExitBlocks);
577
578 SmallSetVector ExitBlockSet(ExitBlocks.begin(),
579 ExitBlocks.end());
580 for (BasicBlock *ExitBlock : ExitBlockSet) {
581 if (any_of(predecessors(ExitBlock),
582 [L](BasicBlock *BB) { return !L->contains(BB); })) {
583 rewriteLoopExitBlock(L, ExitBlock, DT, LI, PreserveLCSSA);
584 ++NumInserted;
585 Changed = true;
586 }
587 }
532 if (formDedicatedExitBlocks(L, DT, LI, PreserveLCSSA))
533 Changed = true;
588534
589535 // If the header has more than two predecessors at this point (from the
590536 // preheader and from multiple backedges), we must adjust the loop.
613559 // insert a new block that all backedges target, then make it jump to the
614560 // loop header.
615561 LoopLatch = insertUniqueBackedgeBlock(L, Preheader, DT, LI);
616 if (LoopLatch) {
617 ++NumInserted;
562 if (LoopLatch)
618563 Changed = true;
619 }
620564 }
621565
622566 const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
644588 // loop-invariant instructions out of the way to open up more
645589 // opportunities, and the disadvantage of having the responsibility
646590 // to preserve dominator information.
647 if (ExitBlockSet.size() == 1) {
591 auto HasUniqueExitBlock = [&]() {
592 BasicBlock *UniqueExit = nullptr;
593 for (auto *ExitingBB : ExitingBlocks)
594 for (auto *SuccBB : successors(ExitingBB)) {
595 if (L->contains(SuccBB))
596 continue;
597
598 if (!UniqueExit)
599 UniqueExit = SuccBB;
600 else if (UniqueExit != SuccBB)
601 return false;
602 }
603
604 return true;
605 };
606 if (HasUniqueExitBlock()) {
648607 for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
649608 BasicBlock *ExitingBlock = ExitingBlocks[i];
650609 if (!ExitingBlock->getSinglePredecessor()) continue;
1111 //===----------------------------------------------------------------------===//
1212
1313 #include "llvm/Transforms/Utils/LoopUtils.h"
14 #include "llvm/ADT/ScopeExit.h"
1415 #include "llvm/Analysis/AliasAnalysis.h"
1516 #include "llvm/Analysis/BasicAliasAnalysis.h"
1617 #include "llvm/Analysis/GlobalsModRef.h"
2829 #include "llvm/IR/ValueHandle.h"
2930 #include "llvm/Pass.h"
3031 #include "llvm/Support/Debug.h"
32 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
3133
3234 using namespace llvm;
3335 using namespace llvm::PatternMatch;
919921 true /* signed */);
920922 D = InductionDescriptor(StartValue, IK_PtrInduction, StepValue);
921923 return true;
924 }
925
926 bool llvm::formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI,
927 bool PreserveLCSSA) {
928 bool Changed = false;
929
930 // We re-use a vector for the in-loop predecesosrs.
931 SmallVector InLoopPredecessors;
932
933 auto RewriteExit = [&](BasicBlock *BB) {
934 assert(InLoopPredecessors.empty() &&
935 "Must start with an empty predecessors list!");
936 auto Cleanup = make_scope_exit([&] { InLoopPredecessors.clear(); });
937
938 // See if there are any non-loop predecessors of this exit block and
939 // keep track of the in-loop predecessors.
940 bool IsDedicatedExit = true;
941 for (auto *PredBB : predecessors(BB))
942 if (L->contains(PredBB)) {
943 if (isa(PredBB->getTerminator()))
944 // We cannot rewrite exiting edges from an indirectbr.
945 return false;
946
947 InLoopPredecessors.push_back(PredBB);
948 } else {
949 IsDedicatedExit = false;
950 }
951
952 assert(!InLoopPredecessors.empty() && "Must have *some* loop predecessor!");
953
954 // Nothing to do if this is already a dedicated exit.
955 if (IsDedicatedExit)
956 return false;
957
958 auto *NewExitBB = SplitBlockPredecessors(
959 BB, InLoopPredecessors, ".loopexit", DT, LI, PreserveLCSSA);
960
961 if (!NewExitBB)
962 DEBUG(dbgs() << "WARNING: Can't create a dedicated exit block for loop: "
963 << *L << "\n");
964 else
965 DEBUG(dbgs() << "LoopSimplify: Creating dedicated exit block "
966 << NewExitBB->getName() << "\n");
967 return true;
968 };
969
970 // Walk the exit blocks directly rather than building up a data structure for
971 // them, but only visit each one once.
972 SmallPtrSet Visited;
973 for (auto *BB : L->blocks())
974 for (auto *SuccBB : successors(BB)) {
975 // We're looking for exit blocks so skip in-loop successors.
976 if (L->contains(SuccBB))
977 continue;
978
979 // Visit each exit block exactly once.
980 if (!Visited.insert(SuccBB).second)
981 continue;
982
983 Changed |= RewriteExit(SuccBB);
984 }
985
986 return Changed;
922987 }
923988
924989 /// \brief Returns the instructions that use values defined in the loop.
152152 ; CHECK: non_dedicated_exit:
153153 ; CHECK-NEXT: ret void
154154 }
155
156 ; Check that we form what dedicated exits we can even when some exits are
157 ; reached via indirectbr which precludes forming dedicated exits.
158 define void @test_form_some_dedicated_exits_despite_indirectbr(i8 %a, i8* %ptr, i8** %addr.ptr) {
159 ; CHECK-LABEL: define void @test_form_some_dedicated_exits_despite_indirectbr(
160 entry:
161 switch i8 %a, label %loop.ph [
162 i8 0, label %exit.a
163 i8 1, label %exit.b
164 i8 2, label %exit.c
165 ]
166 ; CHECK: entry:
167 ; CHECK-NEXT: switch i8 %a, label %loop.ph [
168 ; CHECK-NEXT: i8 0, label %exit.a
169 ; CHECK-NEXT: i8 1, label %exit.b
170 ; CHECK-NEXT: i8 2, label %exit.c
171 ; CHECK-NEXT: ]
172
173 loop.ph:
174 br label %loop.header
175 ; CHECK: loop.ph:
176 ; CHECK-NEXT: br label %loop.header
177
178 loop.header:
179 %addr1 = load volatile i8*, i8** %addr.ptr
180 indirectbr i8* %addr1, [label %loop.body1, label %exit.a]
181 ; CHECK: loop.header:
182 ; CHECK-NEXT: %[[ADDR1:.*]] = load volatile i8*, i8** %addr.ptr
183 ; CHECK-NEXT: indirectbr i8* %[[ADDR1]], [label %loop.body1, label %exit.a]
184
185 loop.body1:
186 %b = load volatile i8, i8* %ptr
187 switch i8 %b, label %loop.body2 [
188 i8 0, label %exit.a
189 i8 1, label %exit.b
190 i8 2, label %exit.c
191 ]
192 ; CHECK: loop.body1:
193 ; CHECK-NEXT: %[[B:.*]] = load volatile i8, i8* %ptr
194 ; CHECK-NEXT: switch i8 %[[B]], label %loop.body2 [
195 ; CHECK-NEXT: i8 0, label %exit.a
196 ; CHECK-NEXT: i8 1, label %[[LOOPEXIT:.*]]
197 ; CHECK-NEXT: i8 2, label %exit.c
198 ; CHECK-NEXT: ]
199
200 loop.body2:
201 %addr2 = load volatile i8*, i8** %addr.ptr
202 indirectbr i8* %addr2, [label %loop.backedge, label %exit.c]
203 ; CHECK: loop.body2:
204 ; CHECK-NEXT: %[[ADDR2:.*]] = load volatile i8*, i8** %addr.ptr
205 ; CHECK-NEXT: indirectbr i8* %[[ADDR2]], [label %loop.backedge, label %exit.c]
206
207 loop.backedge:
208 br label %loop.header
209 ; CHECK: loop.backedge:
210 ; CHECK-NEXT: br label %loop.header
211
212 exit.a:
213 ret void
214 ; Check that there isn't a split loop exit.
215 ; CHECK-NOT: br label %exit.a
216 ;
217 ; CHECK: exit.a:
218 ; CHECK-NEXT: ret void
219
220 exit.b:
221 ret void
222 ; CHECK: [[LOOPEXIT]]:
223 ; CHECK-NEXT: br label %exit.b
224 ;
225 ; CHECK: exit.b:
226 ; CHECK-NEXT: ret void
227
228 exit.c:
229 ret void
230 ; Check that there isn't a split loop exit.
231 ; CHECK-NOT: br label %exit.c
232 ;
233 ; CHECK: exit.c:
234 ; CHECK-NEXT: ret void
235 }
11 ; RUN: opt -loop-unswitch -disable-output -stats -info-output-file - < %s | FileCheck --check-prefix=STATS %s
22 ; RUN: opt -S -loop-unswitch -verify-loop-info -verify-dom-info < %s | FileCheck %s
33
4 ; STATS: 1 loop-simplify - Number of pre-header or exit blocks inserted
54 ; STATS: 2 loop-unswitch - Number of switches unswitched
65
76 ; CHECK: %1 = icmp eq i32 %c, 1
11 ; RUN: opt -loop-unswitch -loop-unswitch-threshold 13 -disable-output -stats -info-output-file - < %s | FileCheck --check-prefix=STATS %s
22 ; RUN: opt -S -loop-unswitch -loop-unswitch-threshold 13 -verify-loop-info -verify-dom-info < %s | FileCheck %s
33
4 ; STATS: 1 loop-simplify - Number of pre-header or exit blocks inserted
54 ; STATS: 1 loop-unswitch - Number of switches unswitched
65
76 ; ModuleID = '../llvm/test/Transforms/LoopUnswitch/2011-11-18-TwoSwitches.ll'
11 ; RUN: opt -loop-unswitch -loop-unswitch-threshold 1000 -disable-output -stats -info-output-file - < %s | FileCheck --check-prefix=STATS %s
22 ; RUN: opt -S -loop-unswitch -loop-unswitch-threshold 1000 -verify-loop-info -verify-dom-info < %s | FileCheck %s
33
4 ; STATS: 1 loop-simplify - Number of pre-header or exit blocks inserted
54 ; STATS: 3 loop-unswitch - Number of switches unswitched
65
76 ; CHECK: %1 = icmp eq i32 %c, 1