Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 1 | //===- InlineFunction.cpp - Code to perform function inlining -------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | ca398dc | 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 | // |
| 13 | // FIXME: This pass should transform alloca instructions in the called function |
Chris Lattner | 5052c91 | 2004-02-04 01:41:09 +0000 | [diff] [blame] | 14 | // into alloca/dealloca pairs! Or perhaps it should refuse to inline them! |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "llvm/Transforms/Utils/Cloning.h" |
Chris Lattner | 3787e76 | 2004-10-17 23:21:07 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
Chris Lattner | 7152c23 | 2003-08-24 04:06:56 +0000 | [diff] [blame] | 20 | #include "llvm/DerivedTypes.h" |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 21 | #include "llvm/Module.h" |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 22 | #include "llvm/Instructions.h" |
| 23 | #include "llvm/Intrinsics.h" |
| 24 | #include "llvm/Support/CallSite.h" |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 26 | |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 27 | bool llvm::InlineFunction(CallInst *CI) { return InlineFunction(CallSite(CI)); } |
| 28 | bool llvm::InlineFunction(InvokeInst *II) {return InlineFunction(CallSite(II));} |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 29 | |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 30 | // InlineFunction - This function inlines the called function into the basic |
| 31 | // block of the caller. This returns false if it is not possible to inline this |
| 32 | // call. The program is still in a well defined state if this occurs though. |
| 33 | // |
| 34 | // Note that this only does one level of inlining. For example, if the |
| 35 | // instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now |
| 36 | // exists in the instruction stream. Similiarly this will inline a recursive |
| 37 | // function by one level. |
| 38 | // |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 39 | bool llvm::InlineFunction(CallSite CS) { |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 40 | Instruction *TheCall = CS.getInstruction(); |
| 41 | assert(TheCall->getParent() && TheCall->getParent()->getParent() && |
| 42 | "Instruction not in function!"); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 44 | const Function *CalledFunc = CS.getCalledFunction(); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 45 | if (CalledFunc == 0 || // Can't inline external function or indirect |
| 46 | CalledFunc->isExternal() || // call, or call to a vararg function! |
| 47 | CalledFunc->getFunctionType()->isVarArg()) return false; |
| 48 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 49 | BasicBlock *OrigBB = TheCall->getParent(); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 50 | Function *Caller = OrigBB->getParent(); |
| 51 | |
Chris Lattner | 5052c91 | 2004-02-04 01:41:09 +0000 | [diff] [blame] | 52 | // Get an iterator to the last basic block in the function, which will have |
| 53 | // the new function inlined after it. |
| 54 | // |
| 55 | Function::iterator LastBlock = &Caller->back(); |
| 56 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 57 | // Make sure to capture all of the return instructions from the cloned |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 58 | // function. |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 59 | std::vector<ReturnInst*> Returns; |
| 60 | { // Scope to destroy ValueMap after cloning. |
| 61 | // Calculate the vector of arguments to pass into the function cloner... |
| 62 | std::map<const Value*, Value*> ValueMap; |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame^] | 63 | assert(std::distance(CalledFunc->arg_begin(), CalledFunc->arg_end()) == |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 64 | std::distance(CS.arg_begin(), CS.arg_end()) && |
| 65 | "No varargs calls can be inlined!"); |
| 66 | |
| 67 | CallSite::arg_iterator AI = CS.arg_begin(); |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame^] | 68 | for (Function::const_arg_iterator I = CalledFunc->arg_begin(), |
| 69 | E = CalledFunc->arg_end(); I != E; ++I, ++AI) |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 70 | ValueMap[I] = *AI; |
| 71 | |
| 72 | // Clone the entire body of the callee into the caller. |
| 73 | CloneFunctionInto(Caller, CalledFunc, ValueMap, Returns, ".i"); |
| 74 | } |
Chris Lattner | c1df7e1 | 2004-02-04 21:33:42 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 76 | // Remember the first block that is newly cloned over. |
| 77 | Function::iterator FirstNewBlock = LastBlock; ++FirstNewBlock; |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 78 | |
| 79 | // If there are any alloca instructions in the block that used to be the entry |
| 80 | // block for the callee, move them to the entry block of the caller. First |
| 81 | // calculate which instruction they should be inserted before. We insert the |
| 82 | // instructions at the end of the current alloca list. |
| 83 | // |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 84 | if (isa<AllocaInst>(FirstNewBlock->begin())) { |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 85 | BasicBlock::iterator InsertPoint = Caller->begin()->begin(); |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 86 | for (BasicBlock::iterator I = FirstNewBlock->begin(), |
| 87 | E = FirstNewBlock->end(); I != E; ) |
Chris Lattner | f775f95 | 2003-10-14 01:11:07 +0000 | [diff] [blame] | 88 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I++)) |
| 89 | if (isa<Constant>(AI->getArraySize())) { |
Chris Lattner | c1df7e1 | 2004-02-04 21:33:42 +0000 | [diff] [blame] | 90 | // Scan for the block of allocas that we can move over. |
| 91 | while (isa<AllocaInst>(I) && |
| 92 | isa<Constant>(cast<AllocaInst>(I)->getArraySize())) |
| 93 | ++I; |
| 94 | |
| 95 | // Transfer all of the allocas over in a block. Using splice means |
| 96 | // that they instructions aren't removed from the symbol table, then |
| 97 | // reinserted. |
| 98 | Caller->front().getInstList().splice(InsertPoint, |
| 99 | FirstNewBlock->getInstList(), |
| 100 | AI, I); |
Chris Lattner | f775f95 | 2003-10-14 01:11:07 +0000 | [diff] [blame] | 101 | } |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 102 | } |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 104 | // If we are inlining for an invoke instruction, we must make sure to rewrite |
| 105 | // any inlined 'unwind' instructions into branches to the invoke exception |
| 106 | // destination, and call instructions into invoke instructions. |
| 107 | if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 108 | BasicBlock *InvokeDest = II->getUnwindDest(); |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 109 | std::vector<Value*> InvokeDestPHIValues; |
| 110 | |
| 111 | // If there are PHI nodes in the exceptional destination block, we need to |
| 112 | // keep track of which values came into them from this invoke, then remove |
| 113 | // the entry for this block. |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 114 | for (BasicBlock::iterator I = InvokeDest->begin(); isa<PHINode>(I); ++I) { |
| 115 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 116 | // Save the value to use for this edge... |
| 117 | InvokeDestPHIValues.push_back(PN->getIncomingValueForBlock(OrigBB)); |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 118 | } |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 119 | |
| 120 | for (Function::iterator BB = FirstNewBlock, E = Caller->end(); |
| 121 | BB != E; ++BB) { |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 122 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { |
| 123 | // We only need to check for function calls: inlined invoke instructions |
| 124 | // require no special handling... |
| 125 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
Chris Lattner | 494b692 | 2004-02-13 16:47:35 +0000 | [diff] [blame] | 126 | // Convert this function call into an invoke instruction... if it's |
| 127 | // not an intrinsic function call (which are known to not throw). |
| 128 | if (CI->getCalledFunction() && |
| 129 | CI->getCalledFunction()->getIntrinsicID()) { |
| 130 | ++I; |
| 131 | } else { |
| 132 | // First, split the basic block... |
| 133 | BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc"); |
| 134 | |
| 135 | // Next, create the new invoke instruction, inserting it at the end |
| 136 | // of the old basic block. |
| 137 | InvokeInst *II = |
| 138 | new InvokeInst(CI->getCalledValue(), Split, InvokeDest, |
| 139 | std::vector<Value*>(CI->op_begin()+1, CI->op_end()), |
| 140 | CI->getName(), BB->getTerminator()); |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 494b692 | 2004-02-13 16:47:35 +0000 | [diff] [blame] | 142 | // Make sure that anything using the call now uses the invoke! |
| 143 | CI->replaceAllUsesWith(II); |
| 144 | |
| 145 | // Delete the unconditional branch inserted by splitBasicBlock |
| 146 | BB->getInstList().pop_back(); |
| 147 | Split->getInstList().pop_front(); // Delete the original call |
| 148 | |
| 149 | // Update any PHI nodes in the exceptional block to indicate that |
| 150 | // there is now a new entry in them. |
| 151 | unsigned i = 0; |
| 152 | for (BasicBlock::iterator I = InvokeDest->begin(); |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 153 | isa<PHINode>(I); ++I, ++i) { |
| 154 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 494b692 | 2004-02-13 16:47:35 +0000 | [diff] [blame] | 155 | PN->addIncoming(InvokeDestPHIValues[i], BB); |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 156 | } |
Chris Lattner | 494b692 | 2004-02-13 16:47:35 +0000 | [diff] [blame] | 157 | |
| 158 | // This basic block is now complete, start scanning the next one. |
| 159 | break; |
| 160 | } |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 161 | } else { |
| 162 | ++I; |
| 163 | } |
| 164 | } |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 165 | |
Chris Lattner | ee5457c | 2003-09-08 19:44:26 +0000 | [diff] [blame] | 166 | if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { |
| 167 | // An UnwindInst requires special handling when it gets inlined into an |
| 168 | // invoke site. Once this happens, we know that the unwind would cause |
| 169 | // a control transfer to the invoke exception destination, so we can |
| 170 | // transform it into a direct branch to the exception destination. |
Chris Lattner | f8485c6 | 2003-11-20 18:25:24 +0000 | [diff] [blame] | 171 | new BranchInst(InvokeDest, UI); |
Chris Lattner | ee5457c | 2003-09-08 19:44:26 +0000 | [diff] [blame] | 172 | |
| 173 | // Delete the unwind instruction! |
| 174 | UI->getParent()->getInstList().pop_back(); |
Chris Lattner | 198f450 | 2003-10-27 05:33:09 +0000 | [diff] [blame] | 175 | |
| 176 | // Update any PHI nodes in the exceptional block to indicate that |
| 177 | // there is now a new entry in them. |
| 178 | unsigned i = 0; |
| 179 | for (BasicBlock::iterator I = InvokeDest->begin(); |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 180 | isa<PHINode>(I); ++I, ++i) { |
| 181 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 198f450 | 2003-10-27 05:33:09 +0000 | [diff] [blame] | 182 | PN->addIncoming(InvokeDestPHIValues[i], BB); |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 183 | } |
Chris Lattner | ee5457c | 2003-09-08 19:44:26 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Chris Lattner | 51d6816 | 2003-09-22 21:59:27 +0000 | [diff] [blame] | 187 | // Now that everything is happy, we have one final detail. The PHI nodes in |
| 188 | // the exception destination block still have entries due to the original |
| 189 | // invoke instruction. Eliminate these entries (which might even delete the |
| 190 | // PHI node) now. |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 191 | InvokeDest->removePredecessor(II->getParent()); |
Chris Lattner | 51d6816 | 2003-09-22 21:59:27 +0000 | [diff] [blame] | 192 | } |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 194 | // If we cloned in _exactly one_ basic block, and if that block ends in a |
| 195 | // return instruction, we splice the body of the inlined callee directly into |
| 196 | // the calling basic block. |
| 197 | if (Returns.size() == 1 && std::distance(FirstNewBlock, Caller->end()) == 1) { |
| 198 | // Move all of the instructions right before the call. |
| 199 | OrigBB->getInstList().splice(TheCall, FirstNewBlock->getInstList(), |
| 200 | FirstNewBlock->begin(), FirstNewBlock->end()); |
| 201 | // Remove the cloned basic block. |
| 202 | Caller->getBasicBlockList().pop_back(); |
| 203 | |
| 204 | // If the call site was an invoke instruction, add a branch to the normal |
| 205 | // destination. |
| 206 | if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) |
| 207 | new BranchInst(II->getNormalDest(), TheCall); |
| 208 | |
| 209 | // If the return instruction returned a value, replace uses of the call with |
| 210 | // uses of the returned value. |
| 211 | if (!TheCall->use_empty()) |
| 212 | TheCall->replaceAllUsesWith(Returns[0]->getReturnValue()); |
| 213 | |
| 214 | // Since we are now done with the Call/Invoke, we can delete it. |
| 215 | TheCall->getParent()->getInstList().erase(TheCall); |
| 216 | |
| 217 | // Since we are now done with the return instruction, delete it also. |
| 218 | Returns[0]->getParent()->getInstList().erase(Returns[0]); |
| 219 | |
| 220 | // We are now done with the inlining. |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | // Otherwise, we have the normal case, of more than one block to inline or |
| 225 | // multiple return sites. |
| 226 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 227 | // We want to clone the entire callee function into the hole between the |
| 228 | // "starter" and "ender" blocks. How we accomplish this depends on whether |
| 229 | // this is an invoke instruction or a call instruction. |
| 230 | BasicBlock *AfterCallBB; |
| 231 | if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 233 | // Add an unconditional branch to make this look like the CallInst case... |
| 234 | BranchInst *NewBr = new BranchInst(II->getNormalDest(), TheCall); |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 236 | // Split the basic block. This guarantees that no PHI nodes will have to be |
| 237 | // updated due to new incoming edges, and make the invoke case more |
| 238 | // symmetric to the call case. |
| 239 | AfterCallBB = OrigBB->splitBasicBlock(NewBr, |
Chris Lattner | 284d1b8 | 2004-12-11 16:59:54 +0000 | [diff] [blame] | 240 | CalledFunc->getName()+".exit"); |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 242 | } else { // It's a call |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 243 | // If this is a call instruction, we need to split the basic block that |
| 244 | // the call lives in. |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 245 | // |
| 246 | AfterCallBB = OrigBB->splitBasicBlock(TheCall, |
Chris Lattner | 284d1b8 | 2004-12-11 16:59:54 +0000 | [diff] [blame] | 247 | CalledFunc->getName()+".exit"); |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 250 | // Change the branch that used to go to AfterCallBB to branch to the first |
| 251 | // basic block of the inlined function. |
| 252 | // |
| 253 | TerminatorInst *Br = OrigBB->getTerminator(); |
| 254 | assert(Br && Br->getOpcode() == Instruction::Br && |
| 255 | "splitBasicBlock broken!"); |
| 256 | Br->setOperand(0, FirstNewBlock); |
| 257 | |
| 258 | |
| 259 | // Now that the function is correct, make it a little bit nicer. In |
| 260 | // particular, move the basic blocks inserted from the end of the function |
| 261 | // into the space made by splitting the source basic block. |
| 262 | // |
| 263 | Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(), |
| 264 | FirstNewBlock, Caller->end()); |
| 265 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 266 | // Handle all of the return instructions that we just cloned in, and eliminate |
| 267 | // any users of the original call/invoke instruction. |
| 268 | if (Returns.size() > 1) { |
| 269 | // The PHI node should go at the front of the new basic block to merge all |
| 270 | // possible incoming values. |
| 271 | // |
| 272 | PHINode *PHI = 0; |
| 273 | if (!TheCall->use_empty()) { |
| 274 | PHI = new PHINode(CalledFunc->getReturnType(), |
| 275 | TheCall->getName(), AfterCallBB->begin()); |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 277 | // Anything that used the result of the function call should now use the |
| 278 | // PHI node as their operand. |
| 279 | // |
| 280 | TheCall->replaceAllUsesWith(PHI); |
| 281 | } |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 282 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 283 | // Loop over all of the return instructions, turning them into unconditional |
| 284 | // branches to the merge point now, and adding entries to the PHI node as |
| 285 | // appropriate. |
| 286 | for (unsigned i = 0, e = Returns.size(); i != e; ++i) { |
| 287 | ReturnInst *RI = Returns[i]; |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 288 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 289 | if (PHI) { |
| 290 | assert(RI->getReturnValue() && "Ret should have value!"); |
| 291 | assert(RI->getReturnValue()->getType() == PHI->getType() && |
| 292 | "Ret value not consistent in function!"); |
| 293 | PHI->addIncoming(RI->getReturnValue(), RI->getParent()); |
| 294 | } |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 295 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 296 | // Add a branch to the merge point where the PHI node lives if it exists. |
| 297 | new BranchInst(AfterCallBB, RI); |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 299 | // Delete the return instruction now |
| 300 | RI->getParent()->getInstList().erase(RI); |
| 301 | } |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 302 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 303 | } else if (!Returns.empty()) { |
| 304 | // Otherwise, if there is exactly one return value, just replace anything |
| 305 | // using the return value of the call with the computed value. |
| 306 | if (!TheCall->use_empty()) |
| 307 | TheCall->replaceAllUsesWith(Returns[0]->getReturnValue()); |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 308 | |
Chris Lattner | cd01ae5 | 2004-04-16 05:17:59 +0000 | [diff] [blame] | 309 | // Splice the code from the return block into the block that it will return |
| 310 | // to, which contains the code that was after the call. |
| 311 | BasicBlock *ReturnBB = Returns[0]->getParent(); |
Chris Lattner | adfd32f | 2004-07-20 05:45:24 +0000 | [diff] [blame] | 312 | AfterCallBB->getInstList().splice(AfterCallBB->begin(), |
| 313 | ReturnBB->getInstList()); |
Chris Lattner | cd01ae5 | 2004-04-16 05:17:59 +0000 | [diff] [blame] | 314 | |
Chris Lattner | adfd32f | 2004-07-20 05:45:24 +0000 | [diff] [blame] | 315 | // Update PHI nodes that use the ReturnBB to use the AfterCallBB. |
| 316 | ReturnBB->replaceAllUsesWith(AfterCallBB); |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 317 | |
Chris Lattner | adfd32f | 2004-07-20 05:45:24 +0000 | [diff] [blame] | 318 | // Delete the return instruction now and empty ReturnBB now. |
Chris Lattner | 3787e76 | 2004-10-17 23:21:07 +0000 | [diff] [blame] | 319 | Returns[0]->eraseFromParent(); |
| 320 | ReturnBB->eraseFromParent(); |
| 321 | } else if (!TheCall->use_empty()) { |
| 322 | // No returns, but something is using the return value of the call. Just |
| 323 | // nuke the result. |
| 324 | TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType())); |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 325 | } |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 326 | |
Chris Lattner | 5e923de | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 327 | // Since we are now done with the Call/Invoke, we can delete it. |
Chris Lattner | 3787e76 | 2004-10-17 23:21:07 +0000 | [diff] [blame] | 328 | TheCall->eraseFromParent(); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 329 | |
Chris Lattner | 7152c23 | 2003-08-24 04:06:56 +0000 | [diff] [blame] | 330 | // We should always be able to fold the entry block of the function into the |
| 331 | // single predecessor of the block... |
Chris Lattner | cd01ae5 | 2004-04-16 05:17:59 +0000 | [diff] [blame] | 332 | assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!"); |
Chris Lattner | 7152c23 | 2003-08-24 04:06:56 +0000 | [diff] [blame] | 333 | BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0); |
Chris Lattner | 44a6807 | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 334 | |
Chris Lattner | cd01ae5 | 2004-04-16 05:17:59 +0000 | [diff] [blame] | 335 | // Splice the code entry block into calling block, right before the |
| 336 | // unconditional branch. |
| 337 | OrigBB->getInstList().splice(Br, CalleeEntry->getInstList()); |
| 338 | CalleeEntry->replaceAllUsesWith(OrigBB); // Update PHI nodes |
| 339 | |
| 340 | // Remove the unconditional branch. |
| 341 | OrigBB->getInstList().erase(Br); |
| 342 | |
| 343 | // Now we can remove the CalleeEntry block, which is now empty. |
| 344 | Caller->getBasicBlockList().erase(CalleeEntry); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 345 | return true; |
| 346 | } |