#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
+#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Instructions.h"
+#include "llvm/IntrinsicInst.h"
#include "llvm/Intrinsics.h"
#include "llvm/Attributes.h"
#include "llvm/Analysis/CallGraph.h"
+#include "llvm/Analysis/DebugInfo.h"
#include "llvm/Target/TargetData.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
/// 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,
- ClonedCodeInfo &InlinedCodeInfo) {
+ ClonedCodeInfo &InlinedCodeInfo,
+ CallGraph *CG) {
BasicBlock *InvokeDest = II->getUnwindDest();
std::vector<Value*> InvokeDestPHIValues;
// Make sure that anything using the call now uses the invoke!
CI->replaceAllUsesWith(II);
+ // Update the callgraph.
+ if (CG) {
+ // We should be able to do this:
+ // (*CG)[Caller]->replaceCallSite(CI, II);
+ // but that fails if the old call site isn't in the call graph,
+ // which, because of LLVM bug 3601, it sometimes isn't.
+ CallGraphNode *CGN = (*CG)[Caller];
+ for (CallGraphNode::iterator NI = CGN->begin(), NE = CGN->end();
+ NI != NE; ++NI) {
+ if (NI->first == CI) {
+ NI->first = II;
+ break;
+ }
+ }
+ }
+
// Delete the unconditional branch inserted by splitBasicBlock
BB->getInstList().pop_back();
Split->getInstList().pop_front(); // Delete the original call
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);
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);
}
+/// findFnRegionEndMarker - This is a utility routine that is used by
+/// InlineFunction. Return llvm.dbg.region.end intrinsic that corresponds
+/// to the llvm.dbg.func.start of the function F. Otherwise return NULL.
+static const DbgRegionEndInst *findFnRegionEndMarker(const Function *F) {
+
+ GlobalVariable *FnStart = NULL;
+ const DbgRegionEndInst *FnEnd = NULL;
+ for (Function::const_iterator FI = F->begin(), FE =F->end(); FI != FE; ++FI)
+ for (BasicBlock::const_iterator BI = FI->begin(), BE = FI->end(); BI != BE;
+ ++BI) {
+ if (FnStart == NULL) {
+ if (const DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI)) {
+ DISubprogram SP(cast<GlobalVariable>(FSI->getSubprogram()));
+ assert (SP.isNull() == false && "Invalid llvm.dbg.func.start");
+ if (SP.describes(F))
+ FnStart = SP.getGV();
+ }
+ } else {
+ if (const DbgRegionEndInst *REI = dyn_cast<DbgRegionEndInst>(BI))
+ if (REI->getContext() == FnStart)
+ FnEnd = REI;
+ }
+ }
+ return FnEnd;
+}
// InlineFunction - This function inlines the called function into the basic
// block of the caller. This returns false if it is not possible to inline this
//
bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) {
Instruction *TheCall = CS.getInstruction();
+ LLVMContext &Context = TheCall->getContext();
assert(TheCall->getParent() && TheCall->getParent()->getParent() &&
"Instruction not in function!");
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.
// Create the alloca. If we have TargetData, use nice alignment.
unsigned Align = 1;
if (TD) Align = TD->getPrefTypeAlignment(AggTy);
- Value *NewAlloca = new AllocaInst(AggTy, 0, Align, I->getName(),
- Caller->begin()->begin());
+ 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);
if (TD == 0)
Size = ConstantExpr::getSizeOf(AggTy);
else
- Size = ConstantInt::get(Type::Int64Ty, TD->getTypeStoreSize(AggTy));
+ Size = ConstantInt::get(Type::Int64Ty,
+ TD->getTypeStoreSize(AggTy));
// Always generate a memcpy of alignment 1 here because we don't know
// the alignment of the src pointer. Other optimizations can infer
ValueMap[I] = ActualArg;
}
+ // Adjust llvm.dbg.region.end. If the CalledFunc has region end
+ // marker then clone that marker after next stop point at the
+ // call site. The function body cloner does not clone original
+ // region end marker from the CalledFunc. This will ensure that
+ // inlined function's scope ends at the right place.
+ const DbgRegionEndInst *DREI = findFnRegionEndMarker(CalledFunc);
+ if (DREI) {
+ for (BasicBlock::iterator BI = TheCall,
+ BE = TheCall->getParent()->end(); BI != BE; ++BI) {
+ if (DbgStopPointInst *DSPI = dyn_cast<DbgStopPointInst>(BI)) {
+ if (DbgRegionEndInst *NewDREI =
+ dyn_cast<DbgRegionEndInst>(DREI->clone(Context)))
+ NewDREI->insertAfter(DSPI);
+ break;
+ }
+ }
+ }
+
// We want the inliner to prune the code as it copies. We would LOVE to
// have no dead or constant instructions leftover after inlining occurs
// (which can happen, e.g., because an argument was constant), but we'll be
// any inlined 'unwind' instructions into branches to the invoke exception
// destination, and call instructions into invoke instructions.
if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
- HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo);
+ HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo, CG);
// If we cloned in _exactly one_ basic block, and if that block ends in a
// return instruction, we splice the body of the inlined callee directly into
// uses of the returned value.
if (!TheCall->use_empty()) {
ReturnInst *R = Returns[0];
- TheCall->replaceAllUsesWith(R->getReturnValue());
+ if (TheCall == R->getReturnValue())
+ TheCall->replaceAllUsesWith(Context.getUndef(TheCall->getType()));
+ else
+ TheCall->replaceAllUsesWith(R->getReturnValue());
}
// Since we are now done with the Call/Invoke, we can delete it.
TheCall->eraseFromParent();
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];
}
}
- // 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);
} else if (!Returns.empty()) {
// Otherwise, if there is exactly one return value, just replace anything
// using the return value of the call with the computed value.
- if (!TheCall->use_empty())
- TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
+ if (!TheCall->use_empty()) {
+ if (TheCall == Returns[0]->getReturnValue())
+ TheCall->replaceAllUsesWith(Context.getUndef(TheCall->getType()));
+ else
+ TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
+ }
// Splice the code from the return block into the block that it will return
// to, which contains the code that was after the call.
} else if (!TheCall->use_empty()) {
// No returns, but something is using the return value of the call. Just
// nuke the result.
- TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
+ TheCall->replaceAllUsesWith(Context.getUndef(TheCall->getType()));
}
// Since we are now done with the Call/Invoke, we can delete it.