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