Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 1 | //===- InlineFunction.cpp - Code to perform function inlining -------------===// |
| 2 | // |
| 3 | // This file implements inlining of a function into a call site, resolving |
| 4 | // parameters and the return value as appropriate. |
| 5 | // |
| 6 | // FIXME: This pass should transform alloca instructions in the called function |
| 7 | // into malloc/free pairs! Or perhaps it should refuse to inline them! |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #include "llvm/Transforms/Utils/Cloning.h" |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 12 | #include "llvm/Constant.h" |
Chris Lattner | 7152c23 | 2003-08-24 04:06:56 +0000 | [diff] [blame] | 13 | #include "llvm/DerivedTypes.h" |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 14 | #include "llvm/Module.h" |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 15 | #include "llvm/Instructions.h" |
| 16 | #include "llvm/Intrinsics.h" |
| 17 | #include "llvm/Support/CallSite.h" |
Chris Lattner | 7152c23 | 2003-08-24 04:06:56 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 20 | bool InlineFunction(CallInst *CI) { return InlineFunction(CallSite(CI)); } |
| 21 | bool InlineFunction(InvokeInst *II) { return InlineFunction(CallSite(II)); } |
| 22 | |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 23 | // InlineFunction - This function inlines the called function into the basic |
| 24 | // block of the caller. This returns false if it is not possible to inline this |
| 25 | // call. The program is still in a well defined state if this occurs though. |
| 26 | // |
| 27 | // Note that this only does one level of inlining. For example, if the |
| 28 | // instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now |
| 29 | // exists in the instruction stream. Similiarly this will inline a recursive |
| 30 | // function by one level. |
| 31 | // |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 32 | bool InlineFunction(CallSite CS) { |
| 33 | Instruction *TheCall = CS.getInstruction(); |
| 34 | assert(TheCall->getParent() && TheCall->getParent()->getParent() && |
| 35 | "Instruction not in function!"); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 37 | const Function *CalledFunc = CS.getCalledFunction(); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 38 | if (CalledFunc == 0 || // Can't inline external function or indirect |
| 39 | CalledFunc->isExternal() || // call, or call to a vararg function! |
| 40 | CalledFunc->getFunctionType()->isVarArg()) return false; |
| 41 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 42 | BasicBlock *OrigBB = TheCall->getParent(); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 43 | Function *Caller = OrigBB->getParent(); |
| 44 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 45 | // We want to clone the entire callee function into the whole between the |
| 46 | // "starter" and "ender" blocks. How we accomplish this depends on whether |
| 47 | // this is an invoke instruction or a call instruction. |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 49 | BasicBlock *InvokeDest = 0; // Exception handling destination |
Chris Lattner | 51d6816 | 2003-09-22 21:59:27 +0000 | [diff] [blame] | 50 | std::vector<Value*> InvokeDestPHIValues; // Values for PHI nodes in InvokeDest |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 51 | BasicBlock *AfterCallBB; |
Chris Lattner | 51d6816 | 2003-09-22 21:59:27 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 53 | if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 54 | InvokeDest = II->getExceptionalDest(); |
| 55 | |
| 56 | // Add an unconditional branch to make this look like the CallInst case... |
Chris Lattner | f98a084 | 2003-09-22 22:35:39 +0000 | [diff] [blame] | 57 | BranchInst *NewBr = new BranchInst(II->getNormalDest(), TheCall); |
| 58 | |
| 59 | // Split the basic block. This guarantees that no PHI nodes will have to be |
| 60 | // updated due to new incoming edges, and make the invoke case more |
| 61 | // symmetric to the call case. |
| 62 | AfterCallBB = OrigBB->splitBasicBlock(NewBr, |
| 63 | CalledFunc->getName()+".entry"); |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 51d6816 | 2003-09-22 21:59:27 +0000 | [diff] [blame] | 65 | // If there are PHI nodes in the exceptional destination block, we need to |
| 66 | // keep track of which values came into them from this invoke, then remove |
| 67 | // the entry for this block. |
| 68 | for (BasicBlock::iterator I = InvokeDest->begin(); |
| 69 | PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
| 70 | // Save the value to use for this edge... |
Chris Lattner | dd7036d | 2003-09-22 23:30:59 +0000 | [diff] [blame] | 71 | InvokeDestPHIValues.push_back(PN->getIncomingValueForBlock(AfterCallBB)); |
Chris Lattner | 51d6816 | 2003-09-22 21:59:27 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 74 | // Remove (unlink) the InvokeInst from the function... |
| 75 | OrigBB->getInstList().remove(TheCall); |
Chris Lattner | 51d6816 | 2003-09-22 21:59:27 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 77 | } else { // It's a call |
| 78 | // If this is a call instruction, we need to split the basic block that the |
| 79 | // call lives in. |
| 80 | // |
| 81 | AfterCallBB = OrigBB->splitBasicBlock(TheCall, |
| 82 | CalledFunc->getName()+".entry"); |
| 83 | // Remove (unlink) the CallInst from the function... |
| 84 | AfterCallBB->getInstList().remove(TheCall); |
| 85 | } |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 86 | |
| 87 | // If we have a return value generated by this call, convert it into a PHI |
| 88 | // node that gets values from each of the old RET instructions in the original |
| 89 | // function. |
| 90 | // |
| 91 | PHINode *PHI = 0; |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 92 | if (!TheCall->use_empty()) { |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 93 | // The PHI node should go at the front of the new basic block to merge all |
| 94 | // possible incoming values. |
| 95 | // |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 96 | PHI = new PHINode(CalledFunc->getReturnType(), TheCall->getName(), |
| 97 | AfterCallBB->begin()); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 98 | |
| 99 | // Anything that used the result of the function call should now use the PHI |
| 100 | // node as their operand. |
| 101 | // |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 102 | TheCall->replaceAllUsesWith(PHI); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // Get an iterator to the last basic block in the function, which will have |
| 106 | // the new function inlined after it. |
| 107 | // |
| 108 | Function::iterator LastBlock = &Caller->back(); |
| 109 | |
| 110 | // Calculate the vector of arguments to pass into the function cloner... |
| 111 | std::map<const Value*, Value*> ValueMap; |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 112 | assert(std::distance(CalledFunc->abegin(), CalledFunc->aend()) == |
| 113 | std::distance(CS.arg_begin(), CS.arg_end()) && |
| 114 | "No varargs calls can be inlined!"); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 116 | CallSite::arg_iterator AI = CS.arg_begin(); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 117 | for (Function::const_aiterator I = CalledFunc->abegin(), E=CalledFunc->aend(); |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 118 | I != E; ++I, ++AI) |
| 119 | ValueMap[I] = *AI; |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 121 | // Since we are now done with the Call/Invoke, we can delete it. |
| 122 | delete TheCall; |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 123 | |
| 124 | // Make a vector to capture the return instructions in the cloned function... |
| 125 | std::vector<ReturnInst*> Returns; |
| 126 | |
| 127 | // Populate the value map with all of the globals in the program. |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 128 | // FIXME: This should be the default for CloneFunctionInto! |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 129 | Module &M = *Caller->getParent(); |
| 130 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 131 | ValueMap[I] = I; |
| 132 | for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I) |
| 133 | ValueMap[I] = I; |
| 134 | |
| 135 | // Do all of the hard part of cloning the callee into the caller... |
| 136 | CloneFunctionInto(Caller, CalledFunc, ValueMap, Returns, ".i"); |
| 137 | |
| 138 | // Loop over all of the return instructions, turning them into unconditional |
| 139 | // branches to the merge point now... |
| 140 | for (unsigned i = 0, e = Returns.size(); i != e; ++i) { |
| 141 | ReturnInst *RI = Returns[i]; |
| 142 | BasicBlock *BB = RI->getParent(); |
| 143 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 144 | // Add a branch to the merge point where the PHI node lives if it exists. |
| 145 | new BranchInst(AfterCallBB, RI); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 146 | |
| 147 | if (PHI) { // The PHI node should include this value! |
| 148 | assert(RI->getReturnValue() && "Ret should have value!"); |
| 149 | assert(RI->getReturnValue()->getType() == PHI->getType() && |
| 150 | "Ret value not consistent in function!"); |
| 151 | PHI->addIncoming(RI->getReturnValue(), BB); |
| 152 | } |
| 153 | |
| 154 | // Delete the return instruction now |
| 155 | BB->getInstList().erase(RI); |
| 156 | } |
| 157 | |
| 158 | // Check to see if the PHI node only has one argument. This is a common |
| 159 | // case resulting from there only being a single return instruction in the |
| 160 | // function call. Because this is so common, eliminate the PHI node. |
| 161 | // |
| 162 | if (PHI && PHI->getNumIncomingValues() == 1) { |
| 163 | PHI->replaceAllUsesWith(PHI->getIncomingValue(0)); |
| 164 | PHI->getParent()->getInstList().erase(PHI); |
| 165 | } |
| 166 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 167 | // Change the branch that used to go to AfterCallBB to branch to the first |
| 168 | // basic block of the inlined function. |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 169 | // |
| 170 | TerminatorInst *Br = OrigBB->getTerminator(); |
| 171 | assert(Br && Br->getOpcode() == Instruction::Br && |
| 172 | "splitBasicBlock broken!"); |
| 173 | Br->setOperand(0, ++LastBlock); |
| 174 | |
| 175 | // If there are any alloca instructions in the block that used to be the entry |
| 176 | // block for the callee, move them to the entry block of the caller. First |
| 177 | // calculate which instruction they should be inserted before. We insert the |
| 178 | // instructions at the end of the current alloca list. |
| 179 | // |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 180 | if (isa<AllocaInst>(LastBlock->begin())) { |
| 181 | BasicBlock::iterator InsertPoint = Caller->begin()->begin(); |
| 182 | while (isa<AllocaInst>(InsertPoint)) ++InsertPoint; |
| 183 | |
| 184 | for (BasicBlock::iterator I = LastBlock->begin(), E = LastBlock->end(); |
| 185 | I != E; ) |
| 186 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) { |
| 187 | ++I; // Move to the next instruction |
| 188 | LastBlock->getInstList().remove(AI); |
| 189 | Caller->front().getInstList().insert(InsertPoint, AI); |
| 190 | } else { |
| 191 | ++I; |
| 192 | } |
| 193 | } |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 195 | // If we just inlined a call due to an invoke instruction, scan the inlined |
| 196 | // function checking for function calls that should now be made into invoke |
Chris Lattner | ee5457c | 2003-09-08 19:44:26 +0000 | [diff] [blame] | 197 | // instructions, and for unwind's which should be turned into branches. |
Chris Lattner | 51d6816 | 2003-09-22 21:59:27 +0000 | [diff] [blame] | 198 | if (InvokeDest) { |
Chris Lattner | ee5457c | 2003-09-08 19:44:26 +0000 | [diff] [blame] | 199 | for (Function::iterator BB = LastBlock, E = Caller->end(); BB != E; ++BB) { |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 200 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { |
| 201 | // We only need to check for function calls: inlined invoke instructions |
| 202 | // require no special handling... |
| 203 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
Chris Lattner | e4d9096 | 2003-09-08 19:00:30 +0000 | [diff] [blame] | 204 | // Convert this function call into an invoke instruction... |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 205 | |
Chris Lattner | e4d9096 | 2003-09-08 19:00:30 +0000 | [diff] [blame] | 206 | // First, split the basic block... |
| 207 | BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc"); |
| 208 | |
| 209 | // Next, create the new invoke instruction, inserting it at the end |
| 210 | // of the old basic block. |
Chris Lattner | e07007c | 2003-09-15 02:10:16 +0000 | [diff] [blame] | 211 | InvokeInst *II = |
| 212 | new InvokeInst(CI->getCalledValue(), Split, InvokeDest, |
| 213 | std::vector<Value*>(CI->op_begin()+1, CI->op_end()), |
| 214 | CI->getName(), BB->getTerminator()); |
| 215 | |
| 216 | // Make sure that anything using the call now uses the invoke! |
| 217 | CI->replaceAllUsesWith(II); |
| 218 | |
Chris Lattner | e4d9096 | 2003-09-08 19:00:30 +0000 | [diff] [blame] | 219 | // Delete the unconditional branch inserted by splitBasicBlock |
| 220 | BB->getInstList().pop_back(); |
| 221 | Split->getInstList().pop_front(); // Delete the original call |
| 222 | |
Chris Lattner | 51d6816 | 2003-09-22 21:59:27 +0000 | [diff] [blame] | 223 | // Update any PHI nodes in the exceptional block to indicate that |
| 224 | // there is now a new entry in them. |
| 225 | unsigned i = 0; |
| 226 | for (BasicBlock::iterator I = InvokeDest->begin(); |
| 227 | PHINode *PN = dyn_cast<PHINode>(I); ++I, ++i) |
| 228 | PN->addIncoming(InvokeDestPHIValues[i], BB); |
| 229 | |
Chris Lattner | e4d9096 | 2003-09-08 19:00:30 +0000 | [diff] [blame] | 230 | // This basic block is now complete, start scanning the next one. |
| 231 | break; |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 232 | } else { |
| 233 | ++I; |
| 234 | } |
| 235 | } |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 236 | |
Chris Lattner | ee5457c | 2003-09-08 19:44:26 +0000 | [diff] [blame] | 237 | if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) { |
| 238 | // An UnwindInst requires special handling when it gets inlined into an |
| 239 | // invoke site. Once this happens, we know that the unwind would cause |
| 240 | // a control transfer to the invoke exception destination, so we can |
| 241 | // transform it into a direct branch to the exception destination. |
| 242 | BranchInst *BI = new BranchInst(InvokeDest, UI); |
| 243 | |
| 244 | // Delete the unwind instruction! |
| 245 | UI->getParent()->getInstList().pop_back(); |
| 246 | } |
| 247 | } |
| 248 | |
Chris Lattner | 51d6816 | 2003-09-22 21:59:27 +0000 | [diff] [blame] | 249 | // Now that everything is happy, we have one final detail. The PHI nodes in |
| 250 | // the exception destination block still have entries due to the original |
| 251 | // invoke instruction. Eliminate these entries (which might even delete the |
| 252 | // PHI node) now. |
| 253 | for (BasicBlock::iterator I = InvokeDest->begin(); |
| 254 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
Chris Lattner | dd7036d | 2003-09-22 23:30:59 +0000 | [diff] [blame] | 255 | PN->removeIncomingValue(AfterCallBB); |
Chris Lattner | 51d6816 | 2003-09-22 21:59:27 +0000 | [diff] [blame] | 256 | } |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 257 | // Now that the function is correct, make it a little bit nicer. In |
| 258 | // particular, move the basic blocks inserted from the end of the function |
| 259 | // into the space made by splitting the source basic block. |
| 260 | // |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 261 | Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(), |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 262 | LastBlock, Caller->end()); |
| 263 | |
Chris Lattner | 7152c23 | 2003-08-24 04:06:56 +0000 | [diff] [blame] | 264 | // We should always be able to fold the entry block of the function into the |
| 265 | // single predecessor of the block... |
| 266 | assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!"); |
| 267 | BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0); |
| 268 | SimplifyCFG(CalleeEntry); |
| 269 | |
| 270 | // Okay, continue the CFG cleanup. It's often the case that there is only a |
| 271 | // single return instruction in the callee function. If this is the case, |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 272 | // then we have an unconditional branch from the return block to the |
| 273 | // 'AfterCallBB'. Check for this case, and eliminate the branch is possible. |
| 274 | SimplifyCFG(AfterCallBB); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 275 | return true; |
| 276 | } |