llvm.org GIT mirror llvm / 8f78a58
Revert r80926. It causes loop unswitch assertion and slow down some JIT tests significantly. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@81101 91177308-0d34-0410-b5e6-96231b3b80d8 Evan Cheng 11 years ago
11 changed file(s) with 198 addition(s) and 290 deletion(s). Raw diff Collapse all Expand all
375375 /// verifyLoop - Verify loop structure
376376 void verifyLoop() const {
377377 #ifndef NDEBUG
378 assert(!Blocks.empty() && "Loop header is missing");
379 assert(getHeader() && "Loop header is missing");
380
381 // Verify the individual blocks.
382 for (block_iterator I = block_begin(), E = block_end(); I != E; ++I) {
383 BlockT *BB = *I;
384 bool HasInsideLoopSuccs = false;
385 bool HasInsideLoopPreds = false;
386 SmallVector OutsideLoopPreds;
387
388 typedef GraphTraits BlockTraits;
389 for (typename BlockTraits::ChildIteratorType SI =
390 BlockTraits::child_begin(BB), SE = BlockTraits::child_end(BB);
391 SI != SE; ++SI)
392 if (contains(*SI))
393 HasInsideLoopSuccs = true;
394 typedef GraphTraits > InvBlockTraits;
395 for (typename InvBlockTraits::ChildIteratorType PI =
396 InvBlockTraits::child_begin(BB), PE = InvBlockTraits::child_end(BB);
397 PI != PE; ++PI) {
398 if (contains(*PI))
399 HasInsideLoopPreds = true;
400 else
401 OutsideLoopPreds.push_back(*PI);
402 }
403
404 if (BB == getHeader()) {
405 assert(!OutsideLoopPreds.empty() && "Loop is unreachable!");
406 } else if (!OutsideLoopPreds.empty()) {
407 // A non-header loop shouldn't be reachable from outside the loop,
408 // though it is permitted if the predecessor is not itself actually
409 // reachable.
410 BlockT *EntryBB = BB->getParent()->begin();
411 for (df_iterator NI = df_begin(EntryBB),
412 NE = df_end(EntryBB); NI != NE; ++NI)
413 for (unsigned i = 0, e = OutsideLoopPreds.size(); i != e; ++i)
414 assert(*NI != OutsideLoopPreds[i] &&
415 "Loop has multiple entry points!");
416 }
417 assert(HasInsideLoopPreds && "Loop block has no in-loop predecessors!");
418 assert(HasInsideLoopSuccs && "Loop block has no in-loop successors!");
419 assert(BB != getHeader()->getParent()->begin() &&
420 "Loop contains function entry block!");
421 }
422
423 // Verify the subloops.
424 for (iterator I = begin(), E = end(); I != E; ++I) {
425 // Each block in each subloop should be contained within this loop.
426 for (block_iterator BI = (*I)->block_begin(), BE = (*I)->block_end();
427 BI != BE; ++BI) {
428 assert(contains(*BI) &&
429 "Loop does not contain all the blocks of a subloop!");
430 }
431 // Recursively check the subloop.
378 assert (getHeader() && "Loop header is missing");
379 assert (getLoopPreheader() && "Loop preheader is missing");
380 assert (getLoopLatch() && "Loop latch is missing");
381 for (iterator I = SubLoops.begin(), E = SubLoops.end(); I != E; ++I)
432382 (*I)->verifyLoop();
433 }
434
435 // Verify the parent loop.
436 if (ParentLoop) {
437 bool FoundSelf = false;
438 for (iterator I = ParentLoop->begin(), E = ParentLoop->end(); I != E; ++I)
439 if (*I == this) {
440 FoundSelf = true;
441 break;
442 }
443 assert(FoundSelf && "Loop is not a subloop of its parent!");
444 }
445383 #endif
446384 }
447385
934872 ///
935873 virtual bool runOnFunction(Function &F);
936874
937 virtual void verifyAnalysis() const;
938
939875 virtual void releaseMemory() { LI.releaseMemory(); }
940876
941877 virtual void print(raw_ostream &O, const Module* M = 0) const;
125125 /// dest go to one block instead of each going to a different block, but isn't
126126 /// the standard definition of a "critical edge".
127127 ///
128 BasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
129 Pass *P = 0, bool MergeIdenticalEdges = false);
128 bool SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P = 0,
129 bool MergeIdenticalEdges = false);
130130
131 inline BasicBlock *SplitCriticalEdge(BasicBlock *BB, succ_iterator SI, Pass *P = 0) {
131 inline bool SplitCriticalEdge(BasicBlock *BB, succ_iterator SI, Pass *P = 0) {
132132 return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), P);
133133 }
134134
142142 TerminatorInst *TI = (*PI)->getTerminator();
143143 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
144144 if (TI->getSuccessor(i) == Succ)
145 MadeChange |= !!SplitCriticalEdge(TI, i, P);
145 MadeChange |= SplitCriticalEdge(TI, i, P);
146146 return MadeChange;
147147 }
148148
150150 /// and return true, otherwise return false. This method requires that there be
151151 /// an edge between the two blocks. If P is specified, it updates the analyses
152152 /// described above.
153 inline BasicBlock *SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
154 Pass *P = 0,
155 bool MergeIdenticalEdges = false) {
153 inline bool SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst, Pass *P = 0,
154 bool MergeIdenticalEdges = false) {
156155 TerminatorInst *TI = Src->getTerminator();
157156 unsigned i = 0;
158157 while (1) {
180179 /// Preds array, which has NumPreds elements in it. The new block is given a
181180 /// suffix of 'Suffix'. This function returns the new block.
182181 ///
183 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
184 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses.
185 /// In particular, it does not preserve LoopSimplify (because it's
186 /// complicated to handle the case where one of the edges being split
187 /// is an exit of a loop with other exits).
188 ///
182 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree and
183 /// DominanceFrontier, but no other analyses.
189184 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, BasicBlock *const *Preds,
190185 unsigned NumPreds, const char *Suffix,
191186 Pass *P = 0);
299299 ///
300300 void
301301 Loop::getUniqueExitBlocks(SmallVectorImpl &ExitBlocks) const {
302 assert(isLoopSimplifyForm() &&
303 "getUniqueExitBlocks assumes the loop is in canonical form!");
304
305302 // Sort the blocks vector so that we can use binary search to do quick
306303 // lookups.
307304 SmallVector LoopBBs(block_begin(), block_end());
373370 return false;
374371 }
375372
376 void LoopInfo::verifyAnalysis() const {
377 for (iterator I = begin(), E = end(); I != E; ++I) {
378 assert(!(*I)->getParentLoop() && "Top-level loop has a parent!");
379 (*I)->verifyLoop();
380 }
381 }
382
383373 void LoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
384374 AU.setPreservesAll();
385375 AU.addRequired();
9090 AU.addRequired();
9191 AU.addPreserved();
9292 AU.addPreserved();
93 AU.addPreservedID(LoopSimplifyID);
9493 }
9594
9695 bool doFinalization() {
483483 // loop because multiple copies sometimes do useful sinking of code in
484484 // that case(?).
485485 Instruction *OldLoc = dyn_cast(OperandValToReplace);
486 BasicBlock *PHIPred = PN->getIncomingBlock(i);
487486 if (L->contains(OldLoc->getParent())) {
488487 // If this is a critical edge, split the edge so that we do not insert
489488 // the code on all predecessor/successor paths. We do this unless this
490489 // is the canonical backedge for this loop, as this can make some
491490 // inserted code be in an illegal position.
491 BasicBlock *PHIPred = PN->getIncomingBlock(i);
492492 if (e != 1 && PHIPred->getTerminator()->getNumSuccessors() > 1 &&
493493 (PN->getParent() != L->getHeader() || !L->contains(PHIPred))) {
494494
495495 // First step, split the critical edge.
496 BasicBlock *NewBB = SplitCriticalEdge(PHIPred, PN->getParent(),
497 P, false);
496 SplitCriticalEdge(PHIPred, PN->getParent(), P, false);
498497
499498 // Next step: move the basic block. In particular, if the PHI node
500499 // is outside of the loop, and PredTI is in the loop, we want to
501500 // move the block to be immediately before the PHI block, not
502501 // immediately after PredTI.
503 if (L->contains(PHIPred) && !L->contains(PN->getParent()))
502 if (L->contains(PHIPred) && !L->contains(PN->getParent())) {
503 BasicBlock *NewBB = PN->getIncomingBlock(i);
504504 NewBB->moveBefore(PN->getParent());
505 }
505506
506507 // Splitting the edge can reduce the number of PHI entries we have.
507508 e = PN->getNumIncomingValues();
508 PHIPred = NewBB;
509 i = PN->getBasicBlockIndex(PHIPred);
510509 }
511510 }
512 Value *&Code = InsertedCode[PHIPred];
511 Value *&Code = InsertedCode[PN->getIncomingBlock(i)];
513512 if (!Code) {
514513 // Insert the code into the end of the predecessor block.
515514 Instruction *InsertPt = (L->contains(OldLoc->getParent())) ?
516 PHIPred->getTerminator() :
515 PN->getIncomingBlock(i)->getTerminator() :
517516 OldLoc->getParent()->getTerminator();
518517 Code = InsertCodeForBaseAtPosition(NewBase, PN->getType(),
519518 Rewriter, InsertPt, L, LI);
517517 std::swap(TrueDest, FalseDest);
518518
519519 // Insert the new branch.
520 BranchInst *BI = BranchInst::Create(TrueDest, FalseDest, BranchVal, InsertPt);
521
522 // If either edge is critical, split it. This helps preserve LoopSimplify
523 // form for enclosing loops.
524 SplitCriticalEdge(BI, 0, this);
525 SplitCriticalEdge(BI, 1, this);
520 BranchInst::Create(TrueDest, FalseDest, BranchVal, InsertPt);
526521 }
527522
528523 /// UnswitchTrivialCondition - Given a loop that has a trivial unswitchable
579574
580575 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
581576 BasicBlock *ExitBlock = ExitBlocks[i];
582 SmallVector Preds(pred_begin(ExitBlock),
583 pred_end(ExitBlock));
584 SplitBlockPredecessors(ExitBlock, Preds.data(), Preds.size(),
585 ".us-lcssa", this);
586 }
577 std::vector Preds(pred_begin(ExitBlock), pred_end(ExitBlock));
578
579 for (unsigned j = 0, e = Preds.size(); j != e; ++j) {
580 BasicBlock* NewExitBlock = SplitEdge(Preds[j], ExitBlock, this);
581 BasicBlock* StartBlock = Preds[j];
582 BasicBlock* EndBlock;
583 if (NewExitBlock->getSinglePredecessor() == ExitBlock) {
584 EndBlock = NewExitBlock;
585 NewExitBlock = EndBlock->getSinglePredecessor();
586 } else {
587 EndBlock = ExitBlock;
588 }
589
590 std::set InsertedPHIs;
591 PHINode* OldLCSSA = 0;
592 for (BasicBlock::iterator I = EndBlock->begin();
593 (OldLCSSA = dyn_cast(I)); ++I) {
594 Value* OldValue = OldLCSSA->getIncomingValueForBlock(NewExitBlock);
595 PHINode* NewLCSSA = PHINode::Create(OldLCSSA->getType(),
596 OldLCSSA->getName() + ".us-lcssa",
597 NewExitBlock->getTerminator());
598 NewLCSSA->addIncoming(OldValue, StartBlock);
599 OldLCSSA->setIncomingValue(OldLCSSA->getBasicBlockIndex(NewExitBlock),
600 NewLCSSA);
601 InsertedPHIs.insert(NewLCSSA);
602 }
603
604 BasicBlock::iterator InsertPt = EndBlock->getFirstNonPHI();
605 for (BasicBlock::iterator I = NewExitBlock->begin();
606 (OldLCSSA = dyn_cast(I)) && InsertedPHIs.count(OldLCSSA) == 0;
607 ++I) {
608 PHINode *NewLCSSA = PHINode::Create(OldLCSSA->getType(),
609 OldLCSSA->getName() + ".us-lcssa",
610 InsertPt);
611 OldLCSSA->replaceAllUsesWith(NewLCSSA);
612 NewLCSSA->addIncoming(OldLCSSA, NewExitBlock);
613 }
614
615 }
616 }
617
587618 }
588619
589620 /// UnswitchNontrivialCondition - We determined that the loop is profitable
913944 // FIXME: This is a hack. We need to keep the successor around
914945 // and hooked up so as to preserve the loop structure, because
915946 // trying to update it is complicated. So instead we preserve the
916 // loop structure and put the block on a dead code path.
917 BasicBlock *Switch = SI->getParent();
918 SplitEdge(Switch, SI->getSuccessor(i), this);
919 // Compute the successors instead of relying on the return value
920 // of SplitEdge, since it may have split the switch successor
921 // after PHI nodes.
922 BasicBlock *NewSISucc = SI->getSuccessor(i);
923 BasicBlock *OldSISucc = *succ_begin(NewSISucc);
924 // Create an "unreachable" destination.
925 BasicBlock *Abort = BasicBlock::Create(Context, "us-unreachable",
926 Switch->getParent(),
927 OldSISucc);
928 new UnreachableInst(Context, Abort);
929 // Force the new case destination to branch to the "unreachable"
930 // block while maintaining a (dead) CFG edge to the old block.
931 NewSISucc->getTerminator()->eraseFromParent();
932 BranchInst::Create(Abort, OldSISucc,
933 ConstantInt::getTrue(Context), NewSISucc);
934 // Release the PHI operands for this edge.
935 for (BasicBlock::iterator II = NewSISucc->begin();
936 PHINode *PN = dyn_cast(II); ++II)
937 PN->setIncomingValue(PN->getBasicBlockIndex(Switch),
938 UndefValue::get(PN->getType()));
947 // loop structure and put the block on an dead code path.
948
949 BasicBlock *SISucc = SI->getSuccessor(i);
950 BasicBlock* Old = SI->getParent();
951 BasicBlock* Split = SplitBlock(Old, SI, this);
952
953 Instruction* OldTerm = Old->getTerminator();
954 BranchInst::Create(Split, SISucc,
955 ConstantInt::getTrue(Context), OldTerm);
956
957 LPM->deleteSimpleAnalysisValue(Old->getTerminator(), L);
958 Old->getTerminator()->eraseFromParent();
959
960 PHINode *PN;
961 for (BasicBlock::iterator II = SISucc->begin();
962 (PN = dyn_cast(II)); ++II) {
963 Value *InVal = PN->removeIncomingValue(Split, false);
964 PN->addIncoming(InVal, Old);
965 }
966
967 SI->removeCase(i);
939968 break;
940969 }
941970 }
2323 #include "llvm/Analysis/Dominators.h"
2424 #include "llvm/Target/TargetData.h"
2525 #include "llvm/Transforms/Utils/Local.h"
26 #include "llvm/Transforms/Scalar.h"
2726 #include "llvm/Support/ErrorHandling.h"
2827 #include "llvm/Support/ValueHandle.h"
2928 #include
319318 ++SplitIt;
320319 BasicBlock *New = Old->splitBasicBlock(SplitIt, Old->getName()+".split");
321320
322 // The new block lives in whichever loop the old one did. This preserves
323 // LCSSA as well, because we force the split point to be after any PHI nodes.
321 // The new block lives in whichever loop the old one did.
324322 if (LoopInfo* LI = P->getAnalysisIfAvailable())
325323 if (Loop *L = LI->getLoopFor(Old))
326324 L->addBasicBlockToLoop(New, LI->getBase());
354352 /// Preds array, which has NumPreds elements in it. The new block is given a
355353 /// suffix of 'Suffix'.
356354 ///
357 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
358 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses.
359 /// In particular, it does not preserve LoopSimplify (because it's
360 /// complicated to handle the case where one of the edges being split
361 /// is an exit of a loop with other exits).
362 ///
355 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree and
356 /// DominanceFrontier, but no other analyses.
363357 BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,
364358 BasicBlock *const *Preds,
365359 unsigned NumPreds, const char *Suffix,
371365 // The new block unconditionally branches to the old block.
372366 BranchInst *BI = BranchInst::Create(BB, NewBB);
373367
374 LoopInfo *LI = P ? P->getAnalysisIfAvailable() : 0;
375 Loop *L = LI ? LI->getLoopFor(BB) : 0;
376 bool PreserveLCSSA = P->mustPreserveAnalysisID(LCSSAID);
377
378368 // Move the edges from Preds to point to NewBB instead of BB.
379 // While here, if we need to preserve loop analyses, collect
380 // some information about how this split will affect loops.
381 bool HasLoopExit = false;
382 bool IsLoopEntry = !!L;
383 bool SplitMakesNewLoopHeader = false;
384 for (unsigned i = 0; i != NumPreds; ++i) {
369 for (unsigned i = 0; i != NumPreds; ++i)
385370 Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB);
386
387 if (LI) {
388 // If we need to preserve LCSSA, determine if any of
389 // the preds is a loop exit.
390 if (PreserveLCSSA)
391 if (Loop *PL = LI->getLoopFor(Preds[i]))
392 if (!PL->contains(BB))
393 HasLoopExit = true;
394 // If we need to preserve LoopInfo, note whether any of the
395 // preds crosses an interesting loop boundary.
396 if (L) {
397 if (L->contains(Preds[i]))
398 IsLoopEntry = false;
399 else
400 SplitMakesNewLoopHeader = true;
401 }
402 }
403 }
404
371
405372 // Update dominator tree and dominator frontier if available.
406373 DominatorTree *DT = P ? P->getAnalysisIfAvailable() : 0;
407374 if (DT)
408375 DT->splitBlock(NewBB);
409376 if (DominanceFrontier *DF = P ? P->getAnalysisIfAvailable():0)
410377 DF->splitBlock(NewBB);
411
378 AliasAnalysis *AA = P ? P->getAnalysisIfAvailable() : 0;
379
380
412381 // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI
413382 // node becomes an incoming value for BB's phi node. However, if the Preds
414383 // list is empty, we need to insert dummy entries into the PHI nodes in BB to
419388 cast(I)->addIncoming(UndefValue::get(I->getType()), NewBB);
420389 return NewBB;
421390 }
422
423 AliasAnalysis *AA = P ? P->getAnalysisIfAvailable() : 0;
424
425 if (L) {
426 if (IsLoopEntry) {
427 if (Loop *PredLoop = LI->getLoopFor(Preds[0])) {
428 // Add the new block to the nearest enclosing loop (and not an
429 // adjacent loop).
430 while (PredLoop && !PredLoop->contains(BB))
431 PredLoop = PredLoop->getParentLoop();
432 if (PredLoop)
433 PredLoop->addBasicBlockToLoop(NewBB, LI->getBase());
434 }
435 } else {
436 L->addBasicBlockToLoop(NewBB, LI->getBase());
437 if (SplitMakesNewLoopHeader)
438 L->moveToHeader(NewBB);
439 }
440 }
441391
442392 // Otherwise, create a new PHI node in NewBB for each PHI node in BB.
443393 for (BasicBlock::iterator I = BB->begin(); isa(I); ) {
444394 PHINode *PN = cast(I++);
445395
446396 // Check to see if all of the values coming in are the same. If so, we
447 // don't need to create a new PHI node, unless it's needed for LCSSA.
448 Value *InVal = 0;
449 if (!HasLoopExit) {
450 InVal = PN->getIncomingValueForBlock(Preds[0]);
451 for (unsigned i = 1; i != NumPreds; ++i)
452 if (InVal != PN->getIncomingValueForBlock(Preds[i])) {
453 InVal = 0;
454 break;
455 }
456 }
457
397 // don't need to create a new PHI node.
398 Value *InVal = PN->getIncomingValueForBlock(Preds[0]);
399 for (unsigned i = 1; i != NumPreds; ++i)
400 if (InVal != PN->getIncomingValueForBlock(Preds[i])) {
401 InVal = 0;
402 break;
403 }
404
458405 if (InVal) {
459406 // If all incoming values for the new PHI would be the same, just don't
460407 // make a new PHI. Instead, just remove the incoming values from the old
479426 // Add an incoming value to the PHI node in the loop for the preheader
480427 // edge.
481428 PN->addIncoming(InVal, NewBB);
429
430 // Check to see if we can eliminate this phi node.
431 if (Value *V = PN->hasConstantValue(DT)) {
432 PN->replaceAllUsesWith(V);
433 if (AA) AA->deleteValue(PN);
434 PN->eraseFromParent();
435 }
482436 }
483437
484438 return NewBB;
121121 /// false otherwise. This ensures that all edges to that dest go to one block
122122 /// instead of each going to a different block.
123123 //
124 BasicBlock *llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
125 Pass *P, bool MergeIdenticalEdges) {
126 if (!isCriticalEdge(TI, SuccNum, MergeIdenticalEdges)) return 0;
124 bool llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P,
125 bool MergeIdenticalEdges) {
126 if (!isCriticalEdge(TI, SuccNum, MergeIdenticalEdges)) return false;
127127 BasicBlock *TIBB = TI->getParent();
128128 BasicBlock *DestBB = TI->getSuccessor(SuccNum);
129129
171171
172172
173173 // If we don't have a pass object, we can't update anything...
174 if (P == 0) return NewBB;
174 if (P == 0) return true;
175175
176176 // Now update analysis information. Since the only predecessor of NewBB is
177177 // the TIBB, TIBB clearly dominates NewBB. TIBB usually doesn't dominate
253253
254254 // Update LoopInfo if it is around.
255255 if (LoopInfo *LI = P->getAnalysisIfAvailable()) {
256 if (Loop *TIL = LI->getLoopFor(TIBB)) {
257 // If one or the other blocks were not in a loop, the new block is not
258 // either, and thus LI doesn't need to be updated.
256 // If one or the other blocks were not in a loop, the new block is not
257 // either, and thus LI doesn't need to be updated.
258 if (Loop *TIL = LI->getLoopFor(TIBB))
259259 if (Loop *DestLoop = LI->getLoopFor(DestBB)) {
260260 if (TIL == DestLoop) {
261261 // Both in the same loop, the NewBB joins loop.
277277 P->addBasicBlockToLoop(NewBB, LI->getBase());
278278 }
279279 }
280 // If TIBB is in a loop and DestBB is outside of that loop, split the
281 // other exit blocks of the loop that also have predecessors outside
282 // the loop, to maintain a LoopSimplify guarantee.
283 if (!TIL->contains(DestBB) &&
284 P->mustPreserveAnalysisID(LoopSimplifyID)) {
285 // For each unique exit block...
286 SmallVector ExitBlocks;
287 TIL->getExitBlocks(ExitBlocks);
288 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
289 // Collect all the preds that are inside the loop, and note
290 // whether there are any preds outside the loop.
291 SmallVector Preds;
292 bool AllPredsInLoop = false;
293 BasicBlock *Exit = ExitBlocks[i];
294 for (pred_iterator I = pred_begin(Exit), E = pred_end(Exit);
295 I != E; ++I)
296 if (TIL->contains(*I))
297 Preds.push_back(*I);
298 else
299 AllPredsInLoop = true;
300 // If there are any preds not in the loop, we'll need to split
301 // the edges. The Preds.empty() check is needed because a block
302 // may appear multiple times in the list. We can't use
303 // getUniqueExitBlocks above because that depends on LoopSimplify
304 // form, which we're in the process of restoring!
305 if (Preds.empty() || !AllPredsInLoop) continue;
306 BasicBlock *NewBB = SplitBlockPredecessors(Exit,
307 Preds.data(), Preds.size(),
308 "split", P);
309 // Update LCSSA form. This is fairly simple in LoopSimplify form:
310 // just move the existing LCSSA-mandated PHI nodes from the old exit
311 // block to the new one.
312 if (P->mustPreserveAnalysisID(LCSSAID))
313 for (BasicBlock::iterator I = Exit->begin();
314 PHINode *PN = dyn_cast(I); ++I)
315 PN->moveBefore(NewBB->getTerminator());
316 }
317 }
318 // LCSSA form was updated above for the case where LoopSimplify is
319 // available, which means that all predecessors of loop exit blocks
320 // are within the loop. Without LoopSimplify form, it would be
321 // necessary to insert a new phi.
322 assert((!P->mustPreserveAnalysisID(LCSSAID) ||
323 P->mustPreserveAnalysisID(LoopSimplifyID)) &&
324 "SplitCriticalEdge doesn't know how to update LCCSA form "
325 "without LoopSimplify!");
326 }
327
328 }
329
330 return NewBB;
331 }
280 }
281 return true;
282 }
5757 DominatorTree *DT;
5858 std::vector LoopBlocks;
5959 PredIteratorCache PredCache;
60 Loop *L;
6160
6261 virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
6362
7271 AU.setPreservesCFG();
7372 AU.addRequiredID(LoopSimplifyID);
7473 AU.addPreservedID(LoopSimplifyID);
75 AU.addRequiredTransitive();
74 AU.addRequired();
7675 AU.addPreserved();
77 AU.addRequiredTransitive();
76 AU.addRequired();
7877 AU.addPreserved();
7978 AU.addPreserved();
8079
8685 AU.addPreserved();
8786 }
8887 private:
89
90 /// verifyAnalysis() - Verify loop nest.
91 virtual void verifyAnalysis() const {
92 #ifndef NDEBUG
93 // Sanity check: Check basic loop invariants.
94 L->verifyLoop();
95 // Check the special guarantees that LCSSA makes.
96 assert(L->isLCSSAForm());
97 #endif
98 }
99
10088 void getLoopValuesUsedOutsideLoop(Loop *L,
10189 SetVector &AffectedValues,
10290 const SmallVector& exitBlocks);
118106 const PassInfo *const llvm::LCSSAID = &X;
119107
120108 /// runOnFunction - Process all loops in the function, inner-most out.
121 bool LCSSA::runOnLoop(Loop *l, LPPassManager &LPM) {
122 L = l;
109 bool LCSSA::runOnLoop(Loop *L, LPPassManager &LPM) {
123110 PredCache.clear();
124111
125112 LI = &LPM.getAnalysis();
6868
6969 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
7070 // We need loop information to identify the loops...
71 AU.addRequiredTransitive();
72 AU.addRequiredTransitive>();
71 AU.addRequired>();
72 AU.addRequired();
7373
7474 AU.addPreserved();
7575 AU.addPreserved();
8282 void verifyAnalysis() const {
8383 #ifndef NDEBUG
8484 LoopInfo *NLI = &getAnalysis();
85 for (LoopInfo::iterator I = NLI->begin(), E = NLI->end(); I != E; ++I) {
86 // Sanity check: Check basic loop invariants.
85 for (LoopInfo::iterator I = NLI->begin(), E = NLI->end(); I != E; ++I)
8786 (*I)->verifyLoop();
88 // Check the special guarantees that LoopSimplify makes.
89 assert((*I)->isLoopSimplifyForm());
90 }
91 #endif
87 #endif
9288 }
9389
9490 private:
349345 BasicBlock *NewBB =
350346 SplitBlockPredecessors(Header, &OutsideBlocks[0], OutsideBlocks.size(),
351347 ".preheader", this);
348
349
350 //===--------------------------------------------------------------------===//
351 // Update analysis results now that we have performed the transformation
352 //
353
354 // We know that we have loop information to update... update it now.
355 if (Loop *Parent = L->getParentLoop())
356 Parent->addBasicBlockToLoop(NewBB, LI->getBase());
352357
353358 // Make sure that NewBB is put someplace intelligent, which doesn't mess up
354359 // code layout too horribly.
370375 BasicBlock *NewBB = SplitBlockPredecessors(Exit, &LoopBlocks[0],
371376 LoopBlocks.size(), ".loopexit",
372377 this);
378
379 // Update Loop Information - we know that the new block will be in whichever
380 // loop the Exit block is in. Note that it may not be in that immediate loop,
381 // if the successor is some other loop header. In that case, we continue
382 // walking up the loop tree to find a loop that contains both the successor
383 // block and the predecessor block.
384 Loop *SuccLoop = LI->getLoopFor(Exit);
385 while (SuccLoop && !SuccLoop->contains(L->getHeader()))
386 SuccLoop = SuccLoop->getParentLoop();
387 if (SuccLoop)
388 SuccLoop->addBasicBlockToLoop(NewBB, LI->getBase());
373389
374390 return NewBB;
375391 }
504520 else
505521 LI->changeTopLevelLoop(L, NewOuter);
506522
523 // This block is going to be our new header block: add it to this loop and all
524 // parent loops.
525 NewOuter->addBasicBlockToLoop(NewBB, LI->getBase());
526
507527 // L is now a subloop of our outer loop.
508528 NewOuter->addChildLoop(L);
509529
510530 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
511531 I != E; ++I)
512532 NewOuter->addBlockEntry(*I);
513
514 // Now reset the header in L, which had been moved by
515 // SplitBlockPredecessors for the outer loop.
516 L->moveToHeader(Header);
517533
518534 // Determine which blocks should stay in L and which should be moved out to
519535 // the Outer loop now.
0 ; RUN: llvm-as < %s | opt -loop-unswitch -disable-output
1 ; rdar://7197574
2
3 target datalayout = "e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-i64:32:32-f32:32:32-f64:32:32-v64:64:64-v128:128:128-a0:0:32"
4 target triple = "thumbv7-apple-darwin9"
5 %struct.frame = type { i16*, i16*, i16* }
6
7 declare arm_apcscc i32 @ercCollect8PredBlocks(i32* nocapture, i32, i32, i32* nocapture, i32, i32, i32, i8 zeroext) nounwind
8
9 define arm_apcscc void @concealBlocks(i32 %lastColumn, i32 %lastRow, i32 %comp, %struct.frame* nocapture %recfr, i32 %picSizeX, i32* nocapture %condition) nounwind {
10 entry:
11 br i1 undef, label %bb.nph12, label %return
12
13 bb28: ; preds = %bb.nph12
14 unreachable
15
16 bb42: ; preds = %bb.nph12
17 br label %bb43
18
19 bb43: ; preds = %bb61, %bb42
20 %0 = call arm_apcscc i32 @ercCollect8PredBlocks(i32* undef, i32 undef, i32 0, i32* %condition, i32 %lastRow, i32 %lastColumn, i32 undef, i8 zeroext 1) nounwind ; [#uses=0]
21 switch i32 %comp, label %bb58 [
22 i32 0, label %bb52
23 i32 1, label %bb54
24 i32 2, label %bb56
25 ]
26
27 bb52: ; preds = %bb43
28 br label %bb58
29
30 bb54: ; preds = %bb43
31 br label %bb58
32
33 bb56: ; preds = %bb43
34 unreachable
35
36 bb58: ; preds = %bb54, %bb52, %bb43
37 br i1 %1, label %bb59, label %bb61
38
39 bb59: ; preds = %bb58
40 br label %bb61
41
42 bb61: ; preds = %bb59, %bb58
43 br label %bb43
44
45 bb.nph12: ; preds = %entry
46 %1 = icmp eq i32 %comp, 0 ; [#uses=1]
47 br i1 undef, label %bb28, label %bb42
48
49 return: ; preds = %entry
50 ret void
51 }