Update the block cloner which fixes bugpoint on code using unwind_to (phew!)
authorNick Lewycky <nicholas@mxc.ca>
Sun, 9 Mar 2008 05:24:34 +0000 (05:24 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Sun, 9 Mar 2008 05:24:34 +0000 (05:24 +0000)
and also update the cloning interface's major user, the loop optimizations.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@48088 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Analysis/LoopInfo.h
lib/Transforms/Scalar/LoopUnroll.cpp
lib/Transforms/Scalar/LoopUnswitch.cpp
lib/Transforms/Utils/CloneFunction.cpp
lib/Transforms/Utils/CloneLoop.cpp
lib/Transforms/Utils/CloneTrace.cpp
lib/Transforms/Utils/LoopSimplify.cpp

index 789b9048578b8fa39e9dbccd63d0104d1ef1f2ef..29781b2185db08e6762db8a3de1e981827a9a5d2 100644 (file)
@@ -118,8 +118,8 @@ public:
   block_iterator block_begin() const { return Blocks.begin(); }
   block_iterator block_end() const { return Blocks.end(); }
 
-  /// isLoopExit - True if terminator in the block can branch to another block
-  /// that is outside of the current loop.
+  /// isLoopExit - True if this block can branch to another block that is
+  /// outside of the current loop.
   ///
   bool isLoopExit(const BlockT *BB) const {
     typedef GraphTraits<BlockT*> BlockTraits;
index 2140b22f2ff590cfc13097396ee414c7c89f2e09..ff8ece2aa8c1c2520dc361c98449cbcc5b77c17f 100644 (file)
@@ -396,10 +396,14 @@ bool LoopUnroll::unrollLoop(Loop *L, unsigned Count, unsigned Threshold) {
     }
     
     // Remap all instructions in the most recent iteration
-    for (unsigned i = 0; i < NewBlocks.size(); ++i)
-      for (BasicBlock::iterator I = NewBlocks[i]->begin(),
-           E = NewBlocks[i]->end(); I != E; ++I)
+    for (unsigned i = 0; i < NewBlocks.size(); ++i) {
+      BasicBlock *NB = NewBlocks[i];
+      if (BasicBlock *UnwindDest = NB->getUnwindDest())
+        NB->setUnwindDest(cast<BasicBlock>(LastValueMap[UnwindDest]));
+
+      for (BasicBlock::iterator I = NB->begin(), E = NB->end(); I != E; ++I)
         RemapInstruction(I, LastValueMap);
+    }
   }
   
   // The latch block exits the loop.  If there are any PHI nodes in the
index 8d39a4dece31e4374d8727bb897376e6ca3a0441..966038ddff4ea4256ccc12c2bc5f615f6916608d 100644 (file)
@@ -819,10 +819,14 @@ void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val,
   }
 
   // Rewrite the code to refer to itself.
-  for (unsigned i = 0, e = NewBlocks.size(); i != e; ++i)
-    for (BasicBlock::iterator I = NewBlocks[i]->begin(),
-           E = NewBlocks[i]->end(); I != E; ++I)
+  for (unsigned i = 0, e = NewBlocks.size(); i != e; ++i) {
+    BasicBlock *NB = NewBlocks[i];
+    if (BasicBlock *UnwindDest = NB->getUnwindDest())
+      NB->setUnwindDest(cast<BasicBlock>(ValueMap[UnwindDest]));
+
+    for (BasicBlock::iterator I = NB->begin(), E = NB->end(); I != E; ++I)
       RemapInstruction(I, ValueMap);
+  }
   
   // Rewrite the original preheader to select between versions of the loop.
   BranchInst *OldBR = cast<BranchInst>(OrigPreheader->getTerminator());
index 513de4e893e9b03fcb05994290d07580304a88ac..5d0d1d8c6786374b2668a9e35b7861eae8961d81 100644 (file)
@@ -33,6 +33,7 @@ BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB,
                                   ClonedCodeInfo *CodeInfo) {
   BasicBlock *NewBB = new BasicBlock("", F);
   if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
+  NewBB->setUnwindDest(const_cast<BasicBlock*>(BB->getUnwindDest()));
 
   bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
   
@@ -103,10 +104,15 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
   // references as we go.  This uses ValueMap to do all the hard work.
   //
   for (Function::iterator BB = cast<BasicBlock>(ValueMap[OldFunc->begin()]),
-         BE = NewFunc->end(); BB != BE; ++BB)
+         BE = NewFunc->end(); BB != BE; ++BB) {
+    // Fix up the unwind_to label.
+    if (BasicBlock *UnwindDest = BB->getUnwindDest())
+      BB->setUnwindDest(cast<BasicBlock>(ValueMap[UnwindDest]));
+
     // Loop over all instructions, fixing each one as we find it...
     for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
       RemapInstruction(II, ValueMap);
+  }
 }
 
 /// CloneFunction - Return a copy of the specified function, but without
index d52d79598f0518fcf854e0522d6f415f8f9f2af4..7b3359b2f9c6d47009b61075066dd8ae7f726fc9 100644 (file)
@@ -130,6 +130,10 @@ Loop *llvm::CloneLoop(Loop *OrigL, LPPassManager  *LPM, LoopInfo *LI,
   for(SmallVector<BasicBlock *, 16>::iterator NBItr = NewBlocks.begin(), 
         NBE = NewBlocks.end();  NBItr != NBE; ++NBItr) {
     BasicBlock *NB = *NBItr;
+
+    if (BasicBlock *UnwindDest = NB->getUnwindDest())
+      NB->setUnwindDest(cast<BasicBlock>(ValueMap[UnwindDest]));
+
     for(BasicBlock::iterator BI = NB->begin(), BE = NB->end(); 
         BI != BE; ++BI) {
       Instruction *Insn = BI;
index 07111393e2757a638fd52af0028e4bdec61380f0..1cedfd872c6a3135c81cca54766719174fb10e0e 100644 (file)
@@ -68,6 +68,11 @@ llvm::CloneTrace(const std::vector<BasicBlock*> &origTrace) {
   //Second loop to do the remapping
   for (std::vector<BasicBlock *>::const_iterator BB = clonedTrace.begin(),
     BE = clonedTrace.end(); BB != BE; ++BB) {
+
+    //Remap the unwind_to label
+    if (BasicBlock *UnwindDest = (*BB)->getUnwindDest())
+      (*BB)->setUnwindDest(cast<BasicBlock>(ValueMap[UnwindDest]));
+
     for (BasicBlock::iterator I = (*BB)->begin(); I != (*BB)->end(); ++I) {
       //Loop over all the operands of the instruction
       for (unsigned op=0, E = I->getNumOperands(); op != E; ++op) {
index e25ff90b47798584280a6264df2f17b2a1da53dd..16cb30ca9c219fd5f5d2b32399c98aa8943d4313 100644 (file)
@@ -126,18 +126,17 @@ bool LoopSimplify::runOnFunction(Function &F) {
     if (LI->getLoopFor(BB)) continue;
     
     bool BlockUnreachable = false;
-    TerminatorInst *TI = BB->getTerminator();
 
     // Check to see if any successors of this block are non-loop-header loops
     // that are not the header.
-    for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
+    for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
       // If this successor is not in a loop, BB is clearly ok.
-      Loop *L = LI->getLoopFor(TI->getSuccessor(i));
+      Loop *L = LI->getLoopFor(*I);
       if (!L) continue;
       
       // If the succ is the loop header, and if L is a top-level loop, then this
       // is an entrance into a loop through the header, which is also ok.
-      if (L->getHeader() == TI->getSuccessor(i) && L->getParentLoop() == 0)
+      if (L->getHeader() == *I && L->getParentLoop() == 0)
         continue;
       
       // Otherwise, this is an entrance into a loop from some place invalid.
@@ -155,10 +154,11 @@ bool LoopSimplify::runOnFunction(Function &F) {
     // loop by replacing the terminator.
     
     // Remove PHI entries from the successors.
-    for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
-      TI->getSuccessor(i)->removePredecessor(BB);
+    for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I)
+      (*I)->removePredecessor(BB);
    
     // Add a new unreachable instruction before the old terminator.
+    TerminatorInst *TI = BB->getTerminator();
     new UnreachableInst(TI);
     
     // Delete the dead terminator.
@@ -342,6 +342,9 @@ BasicBlock *LoopSimplify::SplitBlockPredecessors(BasicBlock *BB,
       for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s)
         if (TI->getSuccessor(s) == BB)
           TI->setSuccessor(s, NewBB);
+
+      if (Preds[i]->getUnwindDest() == BB)
+        Preds[i]->setUnwindDest(NewBB);
     }
 
   } else {                       // Otherwise the loop is dead...
@@ -681,12 +684,15 @@ void LoopSimplify::InsertUniqueBackedgeBlock(Loop *L) {
   }
 
   // Now that all of the PHI nodes have been inserted and adjusted, modify the
-  // backedge blocks to just to the BEBlock instead of the header.
+  // backedge blocks to branch to the BEBlock instead of the header.
   for (unsigned i = 0, e = BackedgeBlocks.size(); i != e; ++i) {
     TerminatorInst *TI = BackedgeBlocks[i]->getTerminator();
     for (unsigned Op = 0, e = TI->getNumSuccessors(); Op != e; ++Op)
       if (TI->getSuccessor(Op) == Header)
         TI->setSuccessor(Op, BEBlock);
+
+    if (BackedgeBlocks[i]->getUnwindDest() == Header)
+      BackedgeBlocks[i]->setUnwindDest(BEBlock);
   }
 
   //===--- Update all analyses which we must preserve now -----------------===//