Make some intrinsics safe to speculatively execute.
[oota-llvm.git] / lib / Transforms / Utils / InlineFunction.cpp
index 135455e08129ac802fe0b1c5bd616671ad111df7..f89e1b1c537b4cb24d9724edb58739a1bb5b53ea 100644 (file)
@@ -45,6 +45,9 @@ bool llvm::InlineFunction(InvokeInst *II, InlineFunctionInfo &IFI) {
   return InlineFunction(CallSite(II), IFI);
 }
 
+// FIXME: New EH - Remove the functions marked [LIBUNWIND] when new EH is
+// turned on.
+
 /// [LIBUNWIND] Look for an llvm.eh.exception call in the given block.
 static EHExceptionInst *findExceptionInBlock(BasicBlock *bb) {
   for (BasicBlock::iterator i = bb->begin(), e = bb->end(); i != e; i++) {
@@ -250,29 +253,33 @@ namespace {
     PHINode *InnerSelectorPHI;
     SmallVector<Value*, 8> UnwindDestPHIValues;
 
-    SmallVector<LandingPadInst*, 16> CalleeLPads;
-    LandingPadInst *CallerLPad;
-    BasicBlock *SplitLPad;
+    // FIXME: New EH - These will replace the analogous ones above.
+    BasicBlock *OuterResumeDest; //< Destination of the invoke's unwind.
+    BasicBlock *InnerResumeDest; //< Destination for the callee's resume.
+    LandingPadInst *CallerLPad;  //< LandingPadInst associated with the invoke.
+    PHINode *InnerEHValuesPHI;   //< PHI for EH values from landingpad insts.
 
   public:
     InvokeInliningInfo(InvokeInst *II)
       : OuterUnwindDest(II->getUnwindDest()), OuterSelector(0),
         InnerUnwindDest(0), InnerExceptionPHI(0), InnerSelectorPHI(0),
-        CallerLPad(0), SplitLPad(0) {
-      // If there are PHI nodes in the unwind destination block, we
-      // need to keep track of which values came into them from the
-      // invoke before removing the edge from this block.
-      llvm::BasicBlock *invokeBB = II->getParent();
+        OuterResumeDest(II->getUnwindDest()), InnerResumeDest(0),
+        CallerLPad(0), InnerEHValuesPHI(0) {
+      // If there are PHI nodes in the unwind destination block, we need to keep
+      // track of which values came into them from the invoke before removing
+      // the edge from this block.
+      llvm::BasicBlock *InvokeBB = II->getParent();
       BasicBlock::iterator I = OuterUnwindDest->begin();
       for (; isa<PHINode>(I); ++I) {
         // Save the value to use for this edge.
-        PHINode *phi = cast<PHINode>(I);
-        UnwindDestPHIValues.push_back(phi->getIncomingValueForBlock(invokeBB));
+        PHINode *PHI = cast<PHINode>(I);
+        UnwindDestPHIValues.push_back(PHI->getIncomingValueForBlock(InvokeBB));
       }
 
       // FIXME: With the new EH, this if/dyn_cast should be a 'cast'.
-      if (LandingPadInst *LPI = dyn_cast<LandingPadInst>(I))
+      if (LandingPadInst *LPI = dyn_cast<LandingPadInst>(I)) {
         CallerLPad = LPI;
+      }
     }
 
     /// The outer unwind destination is the target of unwind edges
@@ -289,18 +296,10 @@ namespace {
 
     BasicBlock *getInnerUnwindDest();
 
-    void addCalleeLandingPad(LandingPadInst *LPI) {
-      CalleeLPads.push_back(LPI);
-    }
+    // FIXME: New EH - Rename when new EH is turned on.
+    BasicBlock *getInnerUnwindDestNewEH();
 
     LandingPadInst *getLandingPadInst() const { return CallerLPad; }
-    BasicBlock *getSplitLandingPad() {
-      if (SplitLPad) return SplitLPad;
-      assert(CallerLPad && "Trying to split a block that isn't a landing pad!");
-      BasicBlock::iterator I = CallerLPad; ++I;
-      SplitLPad = CallerLPad->getParent()->splitBasicBlock(I, "split.lpad");
-      return SplitLPad;
-    }
 
     bool forwardEHResume(CallInst *call, BasicBlock *src);
 
@@ -311,17 +310,13 @@ namespace {
     /// to there.
     void forwardResume(ResumeInst *RI);
 
-    /// mergeLandingPadClauses - Visit all of the landing pad instructions which
-    /// supply the value for the ResumeInst, and merge the clauses from the new
-    /// destination (the caller's landing pad).
-    void mergeLandingPadClauses(ResumeInst *RI);
-
     /// addIncomingPHIValuesFor - Add incoming-PHI values to the unwind
     /// destination block for the given basic block, using the values for the
     /// original invoke's source block.
     void addIncomingPHIValuesFor(BasicBlock *BB) const {
       addIncomingPHIValuesForInto(BB, OuterUnwindDest);
     }
+
     void addIncomingPHIValuesForInto(BasicBlock *src, BasicBlock *dest) const {
       BasicBlock::iterator I = dest->begin();
       for (unsigned i = 0, e = UnwindDestPHIValues.size(); i != e; ++i, ++I) {
@@ -332,7 +327,7 @@ namespace {
   };
 }
 
-/// Get or create a target for the branch out of rewritten calls to
+/// [LIBUNWIND] Get or create a target for the branch out of rewritten calls to
 /// llvm.eh.resume.
 BasicBlock *InvokeInliningInfo::getInnerUnwindDest() {
   if (InnerUnwindDest) return InnerUnwindDest;
@@ -436,14 +431,40 @@ bool InvokeInliningInfo::forwardEHResume(CallInst *call, BasicBlock *src) {
   return true;
 }
 
-/// mergeLandingPadClauses - Visit all of the landing pad instructions merge the
-/// clauses from the new destination (the caller's landing pad).
-void InvokeInliningInfo::mergeLandingPadClauses(ResumeInst *RI) {
-  for (SmallVectorImpl<LandingPadInst*>::iterator
-         I = CalleeLPads.begin(), E = CalleeLPads.end(); I != E; ++I)
-    for (unsigned i = 0, e = CallerLPad->getNumClauses(); i != e; ++i)
-      (*I)->addClause(CallerLPad->getClauseType(i),
-                      CallerLPad->getClauseValue(i));
+/// Get or create a target for the branch from ResumeInsts.
+BasicBlock *InvokeInliningInfo::getInnerUnwindDestNewEH() {
+  // FIXME: New EH - rename this function when new EH is turned on.
+  if (InnerResumeDest) return InnerResumeDest;
+
+  // Split the landing pad.
+  BasicBlock::iterator SplitPoint = CallerLPad; ++SplitPoint;
+  InnerResumeDest =
+    OuterResumeDest->splitBasicBlock(SplitPoint,
+                                     OuterResumeDest->getName() + ".body");
+
+  // The number of incoming edges we expect to the inner landing pad.
+  const unsigned PHICapacity = 2;
+
+  // Create corresponding new PHIs for all the PHIs in the outer landing pad.
+  BasicBlock::iterator InsertPoint = InnerResumeDest->begin();
+  BasicBlock::iterator I = OuterResumeDest->begin();
+  for (unsigned i = 0, e = UnwindDestPHIValues.size(); i != e; ++i, ++I) {
+    PHINode *OuterPHI = cast<PHINode>(I);
+    PHINode *InnerPHI = PHINode::Create(OuterPHI->getType(), PHICapacity,
+                                        OuterPHI->getName() + ".lpad-body",
+                                        InsertPoint);
+    OuterPHI->replaceAllUsesWith(InnerPHI);
+    InnerPHI->addIncoming(OuterPHI, OuterResumeDest);
+  }
+
+  // Create a PHI for the exception values.
+  InnerEHValuesPHI = PHINode::Create(CallerLPad->getType(), PHICapacity,
+                                     "eh.lpad-body", InsertPoint);
+  CallerLPad->replaceAllUsesWith(InnerEHValuesPHI);
+  InnerEHValuesPHI->addIncoming(CallerLPad, OuterResumeDest);
+
+  // All done.
+  return InnerResumeDest;
 }
 
 /// forwardResume - Forward the 'resume' instruction to the caller's landing pad
@@ -451,33 +472,16 @@ void InvokeInliningInfo::mergeLandingPadClauses(ResumeInst *RI) {
 /// branch. When there is more than one predecessor, we need to split the
 /// landing pad block after the landingpad instruction and jump to there.
 void InvokeInliningInfo::forwardResume(ResumeInst *RI) {
-  BasicBlock *LPadBB = CallerLPad->getParent();
-  Value *ResumeOp = RI->getOperand(0);
-
-  if (!LPadBB->getSinglePredecessor()) {
-    // There are multiple predecessors to this landing pad block. Split this
-    // landing pad block and jump to the new BB.
-    BasicBlock *SplitLPad = getSplitLandingPad();
-    BranchInst::Create(SplitLPad, RI->getParent());
-
-    if (CallerLPad->hasOneUse() && isa<PHINode>(CallerLPad->use_back())) {
-      PHINode *PN = cast<PHINode>(CallerLPad->use_back());
-      PN->addIncoming(ResumeOp, RI->getParent());
-    } else {
-      PHINode *PN = PHINode::Create(ResumeOp->getType(), 0, "lpad.phi",
-                                    &SplitLPad->front());
-      CallerLPad->replaceAllUsesWith(PN);
-      PN->addIncoming(ResumeOp, RI->getParent());
-      PN->addIncoming(CallerLPad, LPadBB);
-    }
+  BasicBlock *Dest = getInnerUnwindDestNewEH();
+  BasicBlock *Src = RI->getParent();
 
-    RI->eraseFromParent();
-    return;
-  }
+  BranchInst::Create(Dest, Src);
+
+  // Update the PHIs in the destination. They were inserted in an order which
+  // makes this work.
+  addIncomingPHIValuesForInto(Src, Dest);
 
-  BranchInst::Create(LPadBB, RI->getParent());
-  CallerLPad->replaceAllUsesWith(ResumeOp);
-  CallerLPad->eraseFromParent();
+  InnerEHValuesPHI->addIncoming(RI->getOperand(0), Src);
   RI->eraseFromParent();
 }
 
@@ -498,15 +502,19 @@ static bool isCleanupOnlySelector(EHSelectorInst *selector) {
 /// Returns true to indicate that the next block should be skipped.
 static bool HandleCallsInBlockInlinedThroughInvoke(BasicBlock *BB,
                                                    InvokeInliningInfo &Invoke) {
+  LandingPadInst *LPI = Invoke.getLandingPadInst();
+
   for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
     Instruction *I = BBI++;
 
-    // Collect the callee's landingpad instructions.
-    if (LandingPadInst *LPI = dyn_cast<LandingPadInst>(I)) {
-      Invoke.addCalleeLandingPad(LPI);
-      continue;
-    }
-    
+    if (LPI) // FIXME: New EH - This won't be NULL in the new EH.
+      if (LandingPadInst *L = dyn_cast<LandingPadInst>(I)) {
+        unsigned NumClauses = LPI->getNumClauses();
+        L->reserveClauses(NumClauses);
+        for (unsigned i = 0; i != NumClauses; ++i)
+          L->addClause(LPI->getClause(i));
+      }
+
     // We only need to check for function calls: inlined invoke
     // instructions require no special handling.
     CallInst *CI = dyn_cast<CallInst>(I);
@@ -642,7 +650,6 @@ static void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock,
     }
 
     if (ResumeInst *RI = dyn_cast<ResumeInst>(BB->getTerminator())) {
-      Invoke.mergeLandingPadClauses(RI);
       Invoke.forwardResume(RI);
     }
   }
@@ -917,40 +924,44 @@ bool llvm::InlineFunction(CallSite CS, InlineFunctionInfo &IFI) {
       return false;
   }
 
-  // Find the personality function used by the landing pads of the caller. If it
-  // exists, then check to see that it matches the personality function used in
-  // the callee.
-  for (Function::const_iterator
-         I = Caller->begin(), E = Caller->end(); I != E; ++I)
+  // Get the personality function from the callee if it contains a landing pad.
+  Value *CalleePersonality = 0;
+  for (Function::const_iterator I = CalledFunc->begin(), E = CalledFunc->end();
+       I != E; ++I)
     if (const InvokeInst *II = dyn_cast<InvokeInst>(I->getTerminator())) {
       const BasicBlock *BB = II->getUnwindDest();
-      // FIXME: This 'isa' here should become go away once the new EH system is
-      // in place.
-      if (!isa<LandingPadInst>(BB->getFirstNonPHI()))
-        continue;
-      const LandingPadInst *LP = cast<LandingPadInst>(BB->getFirstNonPHI());
-      const Value *CallerPersFn = LP->getPersonalityFn();
-
-      // If the personality functions match, then we can perform the
-      // inlining. Otherwise, we can't inline.
-      // TODO: This isn't 100% true. Some personality functions are proper
-      //       supersets of others and can be used in place of the other.
-      for (Function::const_iterator
-             I = CalledFunc->begin(), E = CalledFunc->end(); I != E; ++I)
-        if (const InvokeInst *II = dyn_cast<InvokeInst>(I->getTerminator())) {
-          const BasicBlock *BB = II->getUnwindDest();
-          // FIXME: This 'if/dyn_cast' here should become a normal 'cast' once
-          // the new EH system is in place.
-          if (const LandingPadInst *LP =
-              dyn_cast<LandingPadInst>(BB->getFirstNonPHI()))
-            if (CallerPersFn != LP->getPersonalityFn())
-              return false;
-          break;
-        }
-
+      // FIXME: This 'if/dyn_cast' here should become a normal 'cast' once
+      // the new EH system is in place.
+      if (const LandingPadInst *LP =
+          dyn_cast<LandingPadInst>(BB->getFirstNonPHI()))
+        CalleePersonality = LP->getPersonalityFn();
       break;
     }
 
+  // Find the personality function used by the landing pads of the caller. If it
+  // exists, then check to see that it matches the personality function used in
+  // the callee.
+  if (CalleePersonality)
+    for (Function::const_iterator I = Caller->begin(), E = Caller->end();
+         I != E; ++I)
+      if (const InvokeInst *II = dyn_cast<InvokeInst>(I->getTerminator())) {
+        const BasicBlock *BB = II->getUnwindDest();
+        // FIXME: This 'isa' here should become go away once the new EH system
+        // is in place.
+        if (!isa<LandingPadInst>(BB->getFirstNonPHI()))
+          continue;
+        const LandingPadInst *LP = cast<LandingPadInst>(BB->getFirstNonPHI());
+
+        // If the personality functions match, then we can perform the
+        // inlining. Otherwise, we can't inline.
+        // TODO: This isn't 100% true. Some personality functions are proper
+        //       supersets of others and can be used in place of the other.
+        if (LP->getPersonalityFn() != CalleePersonality)
+          return false;
+
+        break;
+      }
+
   // Get an iterator to the last basic block in the function, which will have
   // the new function inlined after it.
   //
@@ -980,7 +991,7 @@ bool llvm::InlineFunction(CallSite CS, InlineFunctionInfo &IFI) {
       // by them explicit.  However, we don't do this if the callee is readonly
       // or readnone, because the copy would be unneeded: the callee doesn't
       // modify the struct.
-      if (CalledFunc->paramHasAttr(ArgNo+1, Attribute::ByVal)) {
+      if (CS.isByValArgument(ArgNo)) {
         ActualArg = HandleByValArgument(ActualArg, TheCall, CalledFunc, IFI,
                                         CalledFunc->getParamAlignment(ArgNo+1));