Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 1 | //===- InlineFunction.cpp - Code to perform function inlining -------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements inlining of a function into a call site, resolving |
| 11 | // parameters and the return value as appropriate. |
| 12 | // |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Transforms/Utils/Cloning.h" |
Chris Lattner | 6e79e55 | 2004-10-17 23:21:07 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
Chris Lattner | fc3fe5c | 2003-08-24 04:06:56 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Chris Lattner | 0cc265e | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 19 | #include "llvm/Instructions.h" |
| 20 | #include "llvm/Intrinsics.h" |
Chris Lattner | 0841fb1 | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/CallGraph.h" |
Chris Lattner | 0cc265e | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CallSite.h" |
Chris Lattner | df3c342 | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 0841fb1 | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 25 | bool llvm::InlineFunction(CallInst *CI, CallGraph *CG) { |
| 26 | return InlineFunction(CallSite(CI), CG); |
| 27 | } |
| 28 | bool llvm::InlineFunction(InvokeInst *II, CallGraph *CG) { |
| 29 | return InlineFunction(CallSite(II), CG); |
| 30 | } |
Chris Lattner | 0cc265e | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 908d795 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 32 | /// HandleInlinedInvoke - If we inlined an invoke site, we need to convert calls |
| 33 | /// in the body of the inlined function into invokes and turn unwind |
| 34 | /// instructions into branches to the invoke unwind dest. |
| 35 | /// |
| 36 | /// II is the invoke instruction begin inlined. FirstNewBlock is the first |
| 37 | /// block of the inlined code (the last block is the end of the function), |
| 38 | /// and InlineCodeInfo is information about the code that got inlined. |
| 39 | static void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock, |
| 40 | ClonedCodeInfo &InlinedCodeInfo) { |
| 41 | BasicBlock *InvokeDest = II->getUnwindDest(); |
| 42 | std::vector<Value*> InvokeDestPHIValues; |
| 43 | |
| 44 | // If there are PHI nodes in the unwind destination block, we need to |
| 45 | // keep track of which values came into them from this invoke, then remove |
| 46 | // the entry for this block. |
| 47 | BasicBlock *InvokeBlock = II->getParent(); |
| 48 | for (BasicBlock::iterator I = InvokeDest->begin(); isa<PHINode>(I); ++I) { |
| 49 | PHINode *PN = cast<PHINode>(I); |
| 50 | // Save the value to use for this edge. |
| 51 | InvokeDestPHIValues.push_back(PN->getIncomingValueForBlock(InvokeBlock)); |
| 52 | } |
| 53 | |
| 54 | Function *Caller = FirstNewBlock->getParent(); |
| 55 | |
| 56 | // The inlined code is currently at the end of the function, scan from the |
| 57 | // start of the inlined code to its end, checking for stuff we need to |
| 58 | // rewrite. |
Chris Lattner | 19e6a08 | 2006-01-13 19:15:15 +0000 | [diff] [blame] | 59 | if (InlinedCodeInfo.ContainsCalls || InlinedCodeInfo.ContainsUnwinds) { |
| 60 | for (Function::iterator BB = FirstNewBlock, E = Caller->end(); |
| 61 | BB != E; ++BB) { |
| 62 | if (InlinedCodeInfo.ContainsCalls) { |
| 63 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ){ |
| 64 | Instruction *I = BBI++; |
| 65 | |
| 66 | // We only need to check for function calls: inlined invoke |
| 67 | // instructions require no special handling. |
| 68 | if (!isa<CallInst>(I)) continue; |
| 69 | CallInst *CI = cast<CallInst>(I); |
Chris Lattner | 908d795 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 19e6a08 | 2006-01-13 19:15:15 +0000 | [diff] [blame] | 71 | // If this is an intrinsic function call, don't convert it to an |
| 72 | // invoke. |
| 73 | if (CI->getCalledFunction() && |
| 74 | CI->getCalledFunction()->getIntrinsicID()) |
| 75 | continue; |
| 76 | |
| 77 | // Convert this function call into an invoke instruction. |
| 78 | // First, split the basic block. |
| 79 | BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc"); |
| 80 | |
| 81 | // Next, create the new invoke instruction, inserting it at the end |
| 82 | // of the old basic block. |
| 83 | InvokeInst *II = |
| 84 | new InvokeInst(CI->getCalledValue(), Split, InvokeDest, |
| 85 | std::vector<Value*>(CI->op_begin()+1, CI->op_end()), |
| 86 | CI->getName(), BB->getTerminator()); |
| 87 | II->setCallingConv(CI->getCallingConv()); |
| 88 | |
| 89 | // Make sure that anything using the call now uses the invoke! |
| 90 | CI->replaceAllUsesWith(II); |
| 91 | |
| 92 | // Delete the unconditional branch inserted by splitBasicBlock |
| 93 | BB->getInstList().pop_back(); |
| 94 | Split->getInstList().pop_front(); // Delete the original call |
| 95 | |
| 96 | // Update any PHI nodes in the exceptional block to indicate that |
| 97 | // there is now a new entry in them. |
| 98 | unsigned i = 0; |
| 99 | for (BasicBlock::iterator I = InvokeDest->begin(); |
| 100 | isa<PHINode>(I); ++I, ++i) { |
| 101 | PHINode *PN = cast<PHINode>(I); |
| 102 | PN->addIncoming(InvokeDestPHIValues[i], BB); |
| 103 | } |
| 104 | |
| 105 | // This basic block is now complete, start scanning the next one. |
| 106 | break; |
| 107 | } |
Chris Lattner | 908d795 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 108 | } |
Chris Lattner | 19e6a08 | 2006-01-13 19:15:15 +0000 | [diff] [blame] | 109 | |
| 110 | if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { |
| 111 | // An UnwindInst requires special handling when it gets inlined into an |
| 112 | // invoke site. Once this happens, we know that the unwind would cause |
| 113 | // a control transfer to the invoke exception destination, so we can |
| 114 | // transform it into a direct branch to the exception destination. |
| 115 | new BranchInst(InvokeDest, UI); |
Chris Lattner | 908d795 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 19e6a08 | 2006-01-13 19:15:15 +0000 | [diff] [blame] | 117 | // Delete the unwind instruction! |
| 118 | UI->getParent()->getInstList().pop_back(); |
| 119 | |
| 120 | // Update any PHI nodes in the exceptional block to indicate that |
| 121 | // there is now a new entry in them. |
| 122 | unsigned i = 0; |
| 123 | for (BasicBlock::iterator I = InvokeDest->begin(); |
| 124 | isa<PHINode>(I); ++I, ++i) { |
| 125 | PHINode *PN = cast<PHINode>(I); |
| 126 | PN->addIncoming(InvokeDestPHIValues[i], BB); |
| 127 | } |
Chris Lattner | 908d795 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // Now that everything is happy, we have one final detail. The PHI nodes in |
| 133 | // the exception destination block still have entries due to the original |
| 134 | // invoke instruction. Eliminate these entries (which might even delete the |
| 135 | // PHI node) now. |
| 136 | InvokeDest->removePredecessor(II->getParent()); |
| 137 | } |
| 138 | |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 139 | /// UpdateCallGraphAfterInlining - Once we have cloned code over from a callee |
| 140 | /// into the caller, update the specified callgraph to reflect the changes we |
| 141 | /// made. Note that it's possible that not all code was copied over, so only |
| 142 | /// some edges of the callgraph will be remain. |
| 143 | static void UpdateCallGraphAfterInlining(const Function *Caller, |
Chris Lattner | 0841fb1 | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 144 | const Function *Callee, |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 145 | Function::iterator FirstNewBlock, |
| 146 | std::map<const Value*, Value*> &ValueMap, |
Chris Lattner | 0841fb1 | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 147 | CallGraph &CG) { |
| 148 | // Update the call graph by deleting the edge from Callee to Caller |
| 149 | CallGraphNode *CalleeNode = CG[Callee]; |
| 150 | CallGraphNode *CallerNode = CG[Caller]; |
| 151 | CallerNode->removeCallEdgeTo(CalleeNode); |
| 152 | |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 153 | // Since we inlined some uninlined call sites in the callee into the caller, |
Chris Lattner | 0841fb1 | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 154 | // add edges from the caller to all of the callees of the callee. |
| 155 | for (CallGraphNode::iterator I = CalleeNode->begin(), |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 156 | E = CalleeNode->end(); I != E; ++I) { |
| 157 | const Instruction *OrigCall = I->first.getInstruction(); |
| 158 | |
| 159 | std::map<const Value*, Value*>::iterator VMI = ValueMap.find(OrigCall); |
| 160 | if (VMI != ValueMap.end()) { // Only copy the edge if the call was inlined! |
Chris Lattner | 6148456 | 2006-07-12 18:37:18 +0000 | [diff] [blame^] | 161 | // If the call was inlined, but then constant folded, there is no edge to |
| 162 | // add. Check for this case. |
| 163 | if (Instruction *NewCall = dyn_cast<Instruction>(VMI->second)) |
| 164 | CallerNode->addCalledFunction(CallSite::get(NewCall), I->second); |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
Chris Lattner | 0841fb1 | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Chris Lattner | 908d795 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 169 | |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 170 | // InlineFunction - This function inlines the called function into the basic |
| 171 | // block of the caller. This returns false if it is not possible to inline this |
| 172 | // call. The program is still in a well defined state if this occurs though. |
| 173 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 174 | // Note that this only does one level of inlining. For example, if the |
| 175 | // instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 176 | // exists in the instruction stream. Similiarly this will inline a recursive |
| 177 | // function by one level. |
| 178 | // |
Chris Lattner | 0841fb1 | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 179 | bool llvm::InlineFunction(CallSite CS, CallGraph *CG) { |
Chris Lattner | 0cc265e | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 180 | Instruction *TheCall = CS.getInstruction(); |
| 181 | assert(TheCall->getParent() && TheCall->getParent()->getParent() && |
| 182 | "Instruction not in function!"); |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 0cc265e | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 184 | const Function *CalledFunc = CS.getCalledFunction(); |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 185 | if (CalledFunc == 0 || // Can't inline external function or indirect |
| 186 | CalledFunc->isExternal() || // call, or call to a vararg function! |
| 187 | CalledFunc->getFunctionType()->isVarArg()) return false; |
| 188 | |
Chris Lattner | 9f3dced | 2005-05-06 06:47:52 +0000 | [diff] [blame] | 189 | |
| 190 | // If the call to the callee is a non-tail call, we must clear the 'tail' |
| 191 | // flags on any calls that we inline. |
| 192 | bool MustClearTailCallFlags = |
Chris Lattner | 7effa0e | 2005-05-06 17:13:16 +0000 | [diff] [blame] | 193 | isa<CallInst>(TheCall) && !cast<CallInst>(TheCall)->isTailCall(); |
Chris Lattner | 9f3dced | 2005-05-06 06:47:52 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 0cc265e | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 195 | BasicBlock *OrigBB = TheCall->getParent(); |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 196 | Function *Caller = OrigBB->getParent(); |
| 197 | |
Chris Lattner | 9fc977e | 2004-02-04 01:41:09 +0000 | [diff] [blame] | 198 | // Get an iterator to the last basic block in the function, which will have |
| 199 | // the new function inlined after it. |
| 200 | // |
| 201 | Function::iterator LastBlock = &Caller->back(); |
| 202 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 203 | // Make sure to capture all of the return instructions from the cloned |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 204 | // function. |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 205 | std::vector<ReturnInst*> Returns; |
Chris Lattner | 908d795 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 206 | ClonedCodeInfo InlinedFunctionInfo; |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 207 | Function::iterator FirstNewBlock; |
| 208 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 209 | { // Scope to destroy ValueMap after cloning. |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 210 | std::map<const Value*, Value*> ValueMap; |
Chris Lattner | be853d7 | 2006-05-27 01:28:04 +0000 | [diff] [blame] | 211 | |
| 212 | // Calculate the vector of arguments to pass into the function cloner, which |
| 213 | // matches up the formal to the actual argument values. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 214 | assert(std::distance(CalledFunc->arg_begin(), CalledFunc->arg_end()) == |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 215 | std::distance(CS.arg_begin(), CS.arg_end()) && |
| 216 | "No varargs calls can be inlined!"); |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 217 | CallSite::arg_iterator AI = CS.arg_begin(); |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 218 | for (Function::const_arg_iterator I = CalledFunc->arg_begin(), |
| 219 | E = CalledFunc->arg_end(); I != E; ++I, ++AI) |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 220 | ValueMap[I] = *AI; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 221 | |
Chris Lattner | be853d7 | 2006-05-27 01:28:04 +0000 | [diff] [blame] | 222 | // We want the inliner to prune the code as it copies. We would LOVE to |
| 223 | // have no dead or constant instructions leftover after inlining occurs |
| 224 | // (which can happen, e.g., because an argument was constant), but we'll be |
| 225 | // happy with whatever the cloner can do. |
| 226 | CloneAndPruneFunctionInto(Caller, CalledFunc, ValueMap, Returns, ".i", |
| 227 | &InlinedFunctionInfo); |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 228 | |
| 229 | // Remember the first block that is newly cloned over. |
| 230 | FirstNewBlock = LastBlock; ++FirstNewBlock; |
| 231 | |
| 232 | // Update the callgraph if requested. |
| 233 | if (CG) |
| 234 | UpdateCallGraphAfterInlining(Caller, CalledFunc, FirstNewBlock, ValueMap, |
| 235 | *CG); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 236 | } |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 238 | // If there are any alloca instructions in the block that used to be the entry |
| 239 | // block for the callee, move them to the entry block of the caller. First |
| 240 | // calculate which instruction they should be inserted before. We insert the |
| 241 | // instructions at the end of the current alloca list. |
| 242 | // |
Chris Lattner | 257492c | 2006-01-13 18:16:48 +0000 | [diff] [blame] | 243 | { |
Chris Lattner | 0cc265e | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 244 | BasicBlock::iterator InsertPoint = Caller->begin()->begin(); |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 245 | for (BasicBlock::iterator I = FirstNewBlock->begin(), |
| 246 | E = FirstNewBlock->end(); I != E; ) |
Chris Lattner | b4778c7 | 2003-10-14 01:11:07 +0000 | [diff] [blame] | 247 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I++)) |
| 248 | if (isa<Constant>(AI->getArraySize())) { |
Chris Lattner | 257492c | 2006-01-13 18:16:48 +0000 | [diff] [blame] | 249 | // Scan for the block of allocas that we can move over, and move them |
| 250 | // all at once. |
Chris Lattner | 6f8865b | 2004-02-04 21:33:42 +0000 | [diff] [blame] | 251 | while (isa<AllocaInst>(I) && |
| 252 | isa<Constant>(cast<AllocaInst>(I)->getArraySize())) |
| 253 | ++I; |
| 254 | |
| 255 | // Transfer all of the allocas over in a block. Using splice means |
| 256 | // that they instructions aren't removed from the symbol table, then |
| 257 | // reinserted. |
| 258 | Caller->front().getInstList().splice(InsertPoint, |
| 259 | FirstNewBlock->getInstList(), |
| 260 | AI, I); |
Chris Lattner | b4778c7 | 2003-10-14 01:11:07 +0000 | [diff] [blame] | 261 | } |
Chris Lattner | 0cc265e | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 262 | } |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 263 | |
Chris Lattner | 2be0607 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 264 | // If the inlined code contained dynamic alloca instructions, wrap the inlined |
| 265 | // code with llvm.stacksave/llvm.stackrestore intrinsics. |
| 266 | if (InlinedFunctionInfo.ContainsDynamicAllocas) { |
| 267 | Module *M = Caller->getParent(); |
| 268 | const Type *SBytePtr = PointerType::get(Type::SByteTy); |
| 269 | // Get the two intrinsics we care about. |
| 270 | Function *StackSave, *StackRestore; |
| 271 | StackSave = M->getOrInsertFunction("llvm.stacksave", SBytePtr, NULL); |
| 272 | StackRestore = M->getOrInsertFunction("llvm.stackrestore", Type::VoidTy, |
| 273 | SBytePtr, NULL); |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 274 | |
| 275 | // If we are preserving the callgraph, add edges to the stacksave/restore |
| 276 | // functions for the calls we insert. |
| 277 | CallGraphNode *StackSaveCGN, *StackRestoreCGN, *CallerNode; |
| 278 | if (CG) { |
| 279 | StackSaveCGN = CG->getOrInsertFunction(StackSave); |
| 280 | StackRestoreCGN = CG->getOrInsertFunction(StackRestore); |
| 281 | CallerNode = (*CG)[Caller]; |
| 282 | } |
| 283 | |
Chris Lattner | 2be0607 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 284 | // Insert the llvm.stacksave. |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 285 | CallInst *SavedPtr = new CallInst(StackSave, "savedstack", |
| 286 | FirstNewBlock->begin()); |
| 287 | if (CG) CallerNode->addCalledFunction(SavedPtr, StackSaveCGN); |
| 288 | |
Chris Lattner | 2be0607 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 289 | // Insert a call to llvm.stackrestore before any return instructions in the |
| 290 | // inlined function. |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 291 | for (unsigned i = 0, e = Returns.size(); i != e; ++i) { |
| 292 | CallInst *CI = new CallInst(StackRestore, SavedPtr, "", Returns[i]); |
| 293 | if (CG) CallerNode->addCalledFunction(CI, StackRestoreCGN); |
| 294 | } |
Chris Lattner | 0841fb1 | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 295 | |
| 296 | // Count the number of StackRestore calls we insert. |
| 297 | unsigned NumStackRestores = Returns.size(); |
Chris Lattner | 2be0607 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 298 | |
| 299 | // If we are inlining an invoke instruction, insert restores before each |
| 300 | // unwind. These unwinds will be rewritten into branches later. |
| 301 | if (InlinedFunctionInfo.ContainsUnwinds && isa<InvokeInst>(TheCall)) { |
| 302 | for (Function::iterator BB = FirstNewBlock, E = Caller->end(); |
| 303 | BB != E; ++BB) |
Chris Lattner | 0841fb1 | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 304 | if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { |
Chris Lattner | 2be0607 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 305 | new CallInst(StackRestore, SavedPtr, "", UI); |
Chris Lattner | 0841fb1 | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 306 | ++NumStackRestores; |
| 307 | } |
| 308 | } |
Chris Lattner | 2be0607 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Chris Lattner | e24f79a | 2006-01-13 19:18:11 +0000 | [diff] [blame] | 311 | // If we are inlining tail call instruction through a call site that isn't |
| 312 | // marked 'tail', we must remove the tail marker for any calls in the inlined |
| 313 | // code. |
| 314 | if (MustClearTailCallFlags && InlinedFunctionInfo.ContainsCalls) { |
Chris Lattner | 9f3dced | 2005-05-06 06:47:52 +0000 | [diff] [blame] | 315 | for (Function::iterator BB = FirstNewBlock, E = Caller->end(); |
| 316 | BB != E; ++BB) |
| 317 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 318 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 319 | CI->setTailCall(false); |
| 320 | } |
| 321 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 322 | // If we are inlining for an invoke instruction, we must make sure to rewrite |
| 323 | // any inlined 'unwind' instructions into branches to the invoke exception |
| 324 | // destination, and call instructions into invoke instructions. |
Chris Lattner | 908d795 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 325 | if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) |
| 326 | HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo); |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 327 | |
Chris Lattner | 0fa8c7c | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 328 | // If we cloned in _exactly one_ basic block, and if that block ends in a |
| 329 | // return instruction, we splice the body of the inlined callee directly into |
| 330 | // the calling basic block. |
| 331 | if (Returns.size() == 1 && std::distance(FirstNewBlock, Caller->end()) == 1) { |
| 332 | // Move all of the instructions right before the call. |
| 333 | OrigBB->getInstList().splice(TheCall, FirstNewBlock->getInstList(), |
| 334 | FirstNewBlock->begin(), FirstNewBlock->end()); |
| 335 | // Remove the cloned basic block. |
| 336 | Caller->getBasicBlockList().pop_back(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 337 | |
Chris Lattner | 0fa8c7c | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 338 | // If the call site was an invoke instruction, add a branch to the normal |
| 339 | // destination. |
| 340 | if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) |
| 341 | new BranchInst(II->getNormalDest(), TheCall); |
| 342 | |
| 343 | // If the return instruction returned a value, replace uses of the call with |
| 344 | // uses of the returned value. |
| 345 | if (!TheCall->use_empty()) |
| 346 | TheCall->replaceAllUsesWith(Returns[0]->getReturnValue()); |
| 347 | |
| 348 | // Since we are now done with the Call/Invoke, we can delete it. |
| 349 | TheCall->getParent()->getInstList().erase(TheCall); |
| 350 | |
| 351 | // Since we are now done with the return instruction, delete it also. |
| 352 | Returns[0]->getParent()->getInstList().erase(Returns[0]); |
| 353 | |
| 354 | // We are now done with the inlining. |
| 355 | return true; |
| 356 | } |
| 357 | |
| 358 | // Otherwise, we have the normal case, of more than one block to inline or |
| 359 | // multiple return sites. |
| 360 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 361 | // We want to clone the entire callee function into the hole between the |
| 362 | // "starter" and "ender" blocks. How we accomplish this depends on whether |
| 363 | // this is an invoke instruction or a call instruction. |
| 364 | BasicBlock *AfterCallBB; |
| 365 | if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 366 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 367 | // Add an unconditional branch to make this look like the CallInst case... |
| 368 | BranchInst *NewBr = new BranchInst(II->getNormalDest(), TheCall); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 369 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 370 | // Split the basic block. This guarantees that no PHI nodes will have to be |
| 371 | // updated due to new incoming edges, and make the invoke case more |
| 372 | // symmetric to the call case. |
| 373 | AfterCallBB = OrigBB->splitBasicBlock(NewBr, |
Chris Lattner | ffefea0 | 2004-12-11 16:59:54 +0000 | [diff] [blame] | 374 | CalledFunc->getName()+".exit"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 375 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 376 | } else { // It's a call |
Chris Lattner | 0fa8c7c | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 377 | // If this is a call instruction, we need to split the basic block that |
| 378 | // the call lives in. |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 379 | // |
| 380 | AfterCallBB = OrigBB->splitBasicBlock(TheCall, |
Chris Lattner | ffefea0 | 2004-12-11 16:59:54 +0000 | [diff] [blame] | 381 | CalledFunc->getName()+".exit"); |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Chris Lattner | 0fa8c7c | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 384 | // Change the branch that used to go to AfterCallBB to branch to the first |
| 385 | // basic block of the inlined function. |
| 386 | // |
| 387 | TerminatorInst *Br = OrigBB->getTerminator(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 388 | assert(Br && Br->getOpcode() == Instruction::Br && |
Chris Lattner | 0fa8c7c | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 389 | "splitBasicBlock broken!"); |
| 390 | Br->setOperand(0, FirstNewBlock); |
| 391 | |
| 392 | |
| 393 | // Now that the function is correct, make it a little bit nicer. In |
| 394 | // particular, move the basic blocks inserted from the end of the function |
| 395 | // into the space made by splitting the source basic block. |
| 396 | // |
| 397 | Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(), |
| 398 | FirstNewBlock, Caller->end()); |
| 399 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 400 | // Handle all of the return instructions that we just cloned in, and eliminate |
| 401 | // any users of the original call/invoke instruction. |
| 402 | if (Returns.size() > 1) { |
| 403 | // The PHI node should go at the front of the new basic block to merge all |
| 404 | // possible incoming values. |
| 405 | // |
| 406 | PHINode *PHI = 0; |
| 407 | if (!TheCall->use_empty()) { |
| 408 | PHI = new PHINode(CalledFunc->getReturnType(), |
| 409 | TheCall->getName(), AfterCallBB->begin()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 410 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 411 | // Anything that used the result of the function call should now use the |
| 412 | // PHI node as their operand. |
| 413 | // |
| 414 | TheCall->replaceAllUsesWith(PHI); |
| 415 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 416 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 417 | // Loop over all of the return instructions, turning them into unconditional |
| 418 | // branches to the merge point now, and adding entries to the PHI node as |
| 419 | // appropriate. |
| 420 | for (unsigned i = 0, e = Returns.size(); i != e; ++i) { |
| 421 | ReturnInst *RI = Returns[i]; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 422 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 423 | if (PHI) { |
| 424 | assert(RI->getReturnValue() && "Ret should have value!"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 425 | assert(RI->getReturnValue()->getType() == PHI->getType() && |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 426 | "Ret value not consistent in function!"); |
| 427 | PHI->addIncoming(RI->getReturnValue(), RI->getParent()); |
| 428 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 429 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 430 | // Add a branch to the merge point where the PHI node lives if it exists. |
| 431 | new BranchInst(AfterCallBB, RI); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 432 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 433 | // Delete the return instruction now |
| 434 | RI->getParent()->getInstList().erase(RI); |
| 435 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 437 | } else if (!Returns.empty()) { |
| 438 | // Otherwise, if there is exactly one return value, just replace anything |
| 439 | // using the return value of the call with the computed value. |
| 440 | if (!TheCall->use_empty()) |
| 441 | TheCall->replaceAllUsesWith(Returns[0]->getReturnValue()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 442 | |
Chris Lattner | 0328d75 | 2004-04-16 05:17:59 +0000 | [diff] [blame] | 443 | // Splice the code from the return block into the block that it will return |
| 444 | // to, which contains the code that was after the call. |
| 445 | BasicBlock *ReturnBB = Returns[0]->getParent(); |
Chris Lattner | 45b50d1 | 2004-07-20 05:45:24 +0000 | [diff] [blame] | 446 | AfterCallBB->getInstList().splice(AfterCallBB->begin(), |
| 447 | ReturnBB->getInstList()); |
Chris Lattner | 0328d75 | 2004-04-16 05:17:59 +0000 | [diff] [blame] | 448 | |
Chris Lattner | 45b50d1 | 2004-07-20 05:45:24 +0000 | [diff] [blame] | 449 | // Update PHI nodes that use the ReturnBB to use the AfterCallBB. |
| 450 | ReturnBB->replaceAllUsesWith(AfterCallBB); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 451 | |
Chris Lattner | 45b50d1 | 2004-07-20 05:45:24 +0000 | [diff] [blame] | 452 | // Delete the return instruction now and empty ReturnBB now. |
Chris Lattner | 6e79e55 | 2004-10-17 23:21:07 +0000 | [diff] [blame] | 453 | Returns[0]->eraseFromParent(); |
| 454 | ReturnBB->eraseFromParent(); |
| 455 | } else if (!TheCall->use_empty()) { |
| 456 | // No returns, but something is using the return value of the call. Just |
| 457 | // nuke the result. |
| 458 | TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType())); |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 459 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 460 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 461 | // Since we are now done with the Call/Invoke, we can delete it. |
Chris Lattner | 6e79e55 | 2004-10-17 23:21:07 +0000 | [diff] [blame] | 462 | TheCall->eraseFromParent(); |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 463 | |
Chris Lattner | fc3fe5c | 2003-08-24 04:06:56 +0000 | [diff] [blame] | 464 | // We should always be able to fold the entry block of the function into the |
| 465 | // single predecessor of the block... |
Chris Lattner | 0328d75 | 2004-04-16 05:17:59 +0000 | [diff] [blame] | 466 | assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!"); |
Chris Lattner | fc3fe5c | 2003-08-24 04:06:56 +0000 | [diff] [blame] | 467 | BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0); |
Chris Lattner | 0fa8c7c | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 468 | |
Chris Lattner | 0328d75 | 2004-04-16 05:17:59 +0000 | [diff] [blame] | 469 | // Splice the code entry block into calling block, right before the |
| 470 | // unconditional branch. |
| 471 | OrigBB->getInstList().splice(Br, CalleeEntry->getInstList()); |
| 472 | CalleeEntry->replaceAllUsesWith(OrigBB); // Update PHI nodes |
| 473 | |
| 474 | // Remove the unconditional branch. |
| 475 | OrigBB->getInstList().erase(Br); |
| 476 | |
| 477 | // Now we can remove the CalleeEntry block, which is now empty. |
| 478 | Caller->getBasicBlockList().erase(CalleeEntry); |
Chris Lattner | 0841fb1 | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 479 | |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 480 | return true; |
| 481 | } |