patch to update the line number information in pass -mem2reg.
[oota-llvm.git] / lib / Transforms / Utils / InlineFunction.cpp
index 1339740c37884a7197091c5039cc71f2ea6735df..a96c7ceaa8edb9edf254b4f089b808e8fd17f46e 100644 (file)
@@ -37,7 +37,7 @@ bool llvm::InlineFunction(InvokeInst *II, CallGraph *CG, const TargetData *TD) {
 /// in the body of the inlined function into invokes and turn unwind
 /// instructions into branches to the invoke unwind dest.
 ///
-/// II is the invoke instruction begin inlined.  FirstNewBlock is the first
+/// II is the invoke instruction being inlined.  FirstNewBlock is the first
 /// block of the inlined code (the last block is the end of the function),
 /// and InlineCodeInfo is information about the code that got inlined.
 static void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock,
@@ -88,7 +88,7 @@ static void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock,
                                InvokeArgs.begin(), InvokeArgs.end(),
                                CI->getName(), BB->getTerminator());
           II->setCallingConv(CI->getCallingConv());
-          II->setParamAttrs(CI->getParamAttrs());
+          II->setAttributes(CI->getAttributes());
 
           // Make sure that anything using the call now uses the invoke!
           CI->replaceAllUsesWith(II);
@@ -150,16 +150,22 @@ static void UpdateCallGraphAfterInlining(CallSite CS,
                                          CallGraph &CG) {
   const Function *Caller = CS.getInstruction()->getParent()->getParent();
   const Function *Callee = CS.getCalledFunction();
-
-  // Update the call graph by deleting the edge from Callee to Caller
   CallGraphNode *CalleeNode = CG[Callee];
   CallGraphNode *CallerNode = CG[Caller];
-  CallerNode->removeCallEdgeFor(CS);
 
   // Since we inlined some uninlined call sites in the callee into the caller,
   // add edges from the caller to all of the callees of the callee.
-  for (CallGraphNode::iterator I = CalleeNode->begin(),
-       E = CalleeNode->end(); I != E; ++I) {
+  CallGraphNode::iterator I = CalleeNode->begin(), E = CalleeNode->end();
+
+  // Consider the case where CalleeNode == CallerNode.
+  CallGraphNode::CalledFunctionsVector CallCache;
+  if (CalleeNode == CallerNode) {
+    CallCache.assign(I, E);
+    I = CallCache.begin();
+    E = CallCache.end();
+  }
+
+  for (; I != E; ++I) {
     const Instruction *OrigCall = I->first.getInstruction();
 
     DenseMap<const Value*, Value*>::iterator VMI = ValueMap.find(OrigCall);
@@ -171,6 +177,9 @@ static void UpdateCallGraphAfterInlining(CallSite CS,
         CallerNode->addCalledFunction(CallSite::get(NewCall), I->second);
     }
   }
+  // Update the call graph by deleting the edge from Callee to Caller.  We must
+  // do this after the loop above in case Caller and Callee are the same.
+  CallerNode->removeCallEdgeFor(CS);
 }
 
 
@@ -194,10 +203,10 @@ bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) {
       CalledFunc->getFunctionType()->isVarArg()) return false;
 
 
-  // If the call to the callee is a non-tail call, we must clear the 'tail'
+  // If the call to the callee is not a tail call, we must clear the 'tail'
   // flags on any calls that we inline.
   bool MustClearTailCallFlags =
-    isa<CallInst>(TheCall) && !cast<CallInst>(TheCall)->isTailCall();
+    !(isa<CallInst>(TheCall) && cast<CallInst>(TheCall)->isTailCall());
 
   // If the call to the callee cannot throw, set the 'nounwind' flag on any
   // calls that we inline.
@@ -246,7 +255,7 @@ bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) {
       // 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, ParamAttr::ByVal) &&
+      if (CalledFunc->paramHasAttr(ArgNo+1, Attribute::ByVal) &&
           !CalledFunc->onlyReadsMemory()) {
         const Type *AggTy = cast<PointerType>(I->getType())->getElementType();
         const Type *VoidPtrTy = PointerType::getUnqual(Type::Int8Ty);
@@ -257,8 +266,10 @@ bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) {
         Value *NewAlloca = new AllocaInst(AggTy, 0, Align, I->getName(),
                                           Caller->begin()->begin());
         // Emit a memcpy.
+        const Type *Tys[] = { Type::Int64Ty };
         Function *MemCpyFn = Intrinsic::getDeclaration(Caller->getParent(),
-                                                       Intrinsic::memcpy_i64);
+                                                       Intrinsic::memcpy, 
+                                                       Tys, 1);
         Value *DestCast = new BitCastInst(NewAlloca, VoidPtrTy, "tmp", TheCall);
         Value *SrcCast = new BitCastInst(*AI, VoidPtrTy, "tmp", TheCall);
 
@@ -512,8 +523,8 @@ bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) {
       TheCall->replaceAllUsesWith(PHI);
     }
 
-    // Loop over all of the return instructions adding entries to the PHI node as
-    // appropriate.
+    // Loop over all of the return instructions adding entries to the PHI node
+    // as appropriate.
     if (PHI) {
       for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
         ReturnInst *RI = Returns[i];
@@ -523,7 +534,7 @@ bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) {
       }
     }
 
-    // Add a branch to the merge points and remove retrun instructions.
+    // Add a branch to the merge points and remove return instructions.
     for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
       ReturnInst *RI = Returns[i];
       BranchInst::Create(AfterCallBB, RI);