Revert commit 131534 since it seems to have broken several buildbots.
[oota-llvm.git] / lib / Transforms / Scalar / CodeGenPrepare.cpp
index e951d98c0c81e13b2b1d9ff73812d658479b161e..018439018553be5fca6891639f85f67a848b6093 100644 (file)
@@ -81,9 +81,9 @@ namespace {
     /// multiple load/stores of the same address.
     DenseMap<Value*, Value*> SunkAddrs;
 
-    /// UpdateDT - If CFG is modified in anyway, dominator tree may need to
+    /// ModifiedDT - If CFG is modified in anyway, dominator tree may need to
     /// be updated.
-    bool UpdateDT;
+    bool ModifiedDT;
 
   public:
     static char ID; // Pass identification, replacement for typeid
@@ -124,7 +124,7 @@ FunctionPass *llvm::createCodeGenPreparePass(const TargetLowering *TLI) {
 bool CodeGenPrepare::runOnFunction(Function &F) {
   bool EverMadeChange = false;
 
-  UpdateDT = false;
+  ModifiedDT = false;
   DT = getAnalysisIfAvailable<DominatorTree>();
   PFI = getAnalysisIfAvailable<ProfileInfo>();
 
@@ -150,11 +150,11 @@ bool CodeGenPrepare::runOnFunction(Function &F) {
       MadeChange |= ConstantFoldTerminator(BB);
 
     if (MadeChange)
-      UpdateDT = true;
+      ModifiedDT = true;
     EverMadeChange |= MadeChange;
   }
 
-  if (UpdateDT && DT)
+  if (ModifiedDT && DT)
     DT->DT->recalculate(F);
 
   return EverMadeChange;
@@ -331,7 +331,7 @@ void CodeGenPrepare::EliminateMostlyEmptyBlock(BasicBlock *BB) {
   // The PHIs are now updated, change everything that refers to BB to use
   // DestBB and remove BB.
   BB->replaceAllUsesWith(DestBB);
-  if (DT && !UpdateDT) {
+  if (DT && !ModifiedDT) {
     BasicBlock *BBIDom  = DT->getNode(BB)->getIDom()->getBlock();
     BasicBlock *DestBBIDom = DT->getNode(DestBB)->getIDom()->getBlock();
     BasicBlock *NewIDom = DT->findNearestCommonDominator(BBIDom, DestBBIDom);
@@ -535,7 +535,7 @@ bool CodeGenPrepare::OptimizeCallInst(CallInst *CI) {
     WeakVH IterHandle(CurInstIterator);
     
     ReplaceAndSimplifyAllUses(CI, RetVal, TLI ? TLI->getTargetData() : 0,
-                              UpdateDT ? 0 : DT);
+                              ModifiedDT ? 0 : DT);
 
     // If the iterator instruction was recursively deleted, start over at the
     // start of the block.
@@ -594,15 +594,12 @@ bool CodeGenPrepare::DupRetToEnableTailCallOpts(ReturnInst *RI) {
     return false;
 
   Value *V = RI->getReturnValue();
-  if (!V)
-    return false;
-
-  PHINode *PN = dyn_cast<PHINode>(V);
-  if (!PN)
+  PHINode *PN = V ? dyn_cast<PHINode>(V) : NULL;
+  if (V && !PN)
     return false;
 
   BasicBlock *BB = RI->getParent();
-  if (PN->getParent() != BB)
+  if (PN && PN->getParent() != BB)
     return false;
 
   // It's not safe to eliminate the sign / zero extension of the return value.
@@ -612,21 +609,48 @@ bool CodeGenPrepare::DupRetToEnableTailCallOpts(ReturnInst *RI) {
   if ((CallerRetAttr & Attribute::ZExt) || (CallerRetAttr & Attribute::SExt))
     return false;
 
-  // Make sure there are no instructions between PHI and return.
-  BasicBlock::iterator BI = PN;
-  do { ++BI; } while (isa<DbgInfoIntrinsic>(BI));
-  if (&*BI != RI)
-    return false;
+  // Make sure there are no instructions between the PHI and return, or that the
+  // return is the first instruction in the block.
+  if (PN) {
+    BasicBlock::iterator BI = BB->begin();
+    do { ++BI; } while (isa<DbgInfoIntrinsic>(BI));
+    if (&*BI != RI)
+      return false;
+  } else {
+    BasicBlock::iterator BI = BB->begin();
+    while (isa<DbgInfoIntrinsic>(BI)) ++BI;
+    if (&*BI != RI)
+      return false;
+  }
 
   /// Only dup the ReturnInst if the CallInst is likely to be emitted as a tail
   /// call.
   SmallVector<CallInst*, 4> TailCalls;
-  for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) {
-    CallInst *CI = dyn_cast<CallInst>(PN->getIncomingValue(I));
-    // Make sure the phi value is indeed produced by the tail call.
-    if (CI && CI->hasOneUse() && CI->getParent() == PN->getIncomingBlock(I) &&
-        TLI->mayBeEmittedAsTailCall(CI))
-      TailCalls.push_back(CI);
+  if (PN) {
+    for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) {
+      CallInst *CI = dyn_cast<CallInst>(PN->getIncomingValue(I));
+      // Make sure the phi value is indeed produced by the tail call.
+      if (CI && CI->hasOneUse() && CI->getParent() == PN->getIncomingBlock(I) &&
+          TLI->mayBeEmittedAsTailCall(CI))
+        TailCalls.push_back(CI);
+    }
+  } else {
+    SmallPtrSet<BasicBlock*, 4> VisitedBBs;
+    for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
+      if (!VisitedBBs.insert(*PI))
+        continue;
+
+      BasicBlock::InstListType &InstList = (*PI)->getInstList();
+      BasicBlock::InstListType::reverse_iterator RI = InstList.rbegin();
+      BasicBlock::InstListType::reverse_iterator RE = InstList.rend();
+      do { ++RI; } while (RI != RE && isa<DbgInfoIntrinsic>(&*RI));
+      if (RI == RE)
+        continue;
+
+      CallInst *CI = dyn_cast<CallInst>(&*RI);
+      if (CI && CI->use_empty() && TLI->mayBeEmittedAsTailCall(CI))
+        TailCalls.push_back(CI);
+    }
   }
 
   bool Changed = false;
@@ -649,7 +673,7 @@ bool CodeGenPrepare::DupRetToEnableTailCallOpts(ReturnInst *RI) {
 
     // Duplicate the return into CallBB.
     (void)FoldReturnIntoUncondBranch(RI, BB, CallBB);
-    UpdateDT = Changed = true;
+    ModifiedDT = Changed = true;
     ++NumRetsDup;
   }
 
@@ -865,11 +889,26 @@ bool CodeGenPrepare::OptimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
 
   MemoryInst->replaceUsesOfWith(Repl, SunkAddr);
 
+  // If we have no uses, recursively delete the value and all dead instructions
+  // using it.
   if (Repl->use_empty()) {
+    // This can cause recursive deletion, which can invalidate our iterator.
+    // Use a WeakVH to hold onto it in case this happens.
+    WeakVH IterHandle(CurInstIterator);
+    BasicBlock *BB = CurInstIterator->getParent();
+    
     RecursivelyDeleteTriviallyDeadInstructions(Repl);
-    // This address is now available for reassignment, so erase the table entry;
-    // we don't want to match some completely different instruction.
-    SunkAddrs[Addr] = 0;
+
+    if (IterHandle != CurInstIterator) {
+      // If the iterator instruction was recursively deleted, start over at the
+      // start of the block.
+      CurInstIterator = BB->begin();
+      SunkAddrs.clear();
+    } else {
+      // This address is now available for reassignment, so erase the table
+      // entry; we don't want to match some completely different instruction.
+      SunkAddrs[Addr] = 0;
+    }    
   }
   ++NumMemoryInsts;
   return true;