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 |
| 50 | BasicBlock *AfterCallBB; |
| 51 | if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { |
| 52 | AfterCallBB = II->getNormalDest(); |
| 53 | InvokeDest = II->getExceptionalDest(); |
| 54 | |
| 55 | // Add an unconditional branch to make this look like the CallInst case... |
| 56 | new BranchInst(AfterCallBB, TheCall); |
| 57 | |
| 58 | // Remove (unlink) the InvokeInst from the function... |
| 59 | OrigBB->getInstList().remove(TheCall); |
| 60 | } else { // It's a call |
| 61 | // If this is a call instruction, we need to split the basic block that the |
| 62 | // call lives in. |
| 63 | // |
| 64 | AfterCallBB = OrigBB->splitBasicBlock(TheCall, |
| 65 | CalledFunc->getName()+".entry"); |
| 66 | // Remove (unlink) the CallInst from the function... |
| 67 | AfterCallBB->getInstList().remove(TheCall); |
| 68 | } |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 69 | |
| 70 | // If we have a return value generated by this call, convert it into a PHI |
| 71 | // node that gets values from each of the old RET instructions in the original |
| 72 | // function. |
| 73 | // |
| 74 | PHINode *PHI = 0; |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 75 | if (!TheCall->use_empty()) { |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 76 | // The PHI node should go at the front of the new basic block to merge all |
| 77 | // possible incoming values. |
| 78 | // |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 79 | PHI = new PHINode(CalledFunc->getReturnType(), TheCall->getName(), |
| 80 | AfterCallBB->begin()); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 81 | |
| 82 | // Anything that used the result of the function call should now use the PHI |
| 83 | // node as their operand. |
| 84 | // |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 85 | TheCall->replaceAllUsesWith(PHI); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // Get an iterator to the last basic block in the function, which will have |
| 89 | // the new function inlined after it. |
| 90 | // |
| 91 | Function::iterator LastBlock = &Caller->back(); |
| 92 | |
| 93 | // Calculate the vector of arguments to pass into the function cloner... |
| 94 | std::map<const Value*, Value*> ValueMap; |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 95 | assert(std::distance(CalledFunc->abegin(), CalledFunc->aend()) == |
| 96 | std::distance(CS.arg_begin(), CS.arg_end()) && |
| 97 | "No varargs calls can be inlined!"); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 99 | CallSite::arg_iterator AI = CS.arg_begin(); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 100 | for (Function::const_aiterator I = CalledFunc->abegin(), E=CalledFunc->aend(); |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 101 | I != E; ++I, ++AI) |
| 102 | ValueMap[I] = *AI; |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 104 | // Since we are now done with the Call/Invoke, we can delete it. |
| 105 | delete TheCall; |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 106 | |
| 107 | // Make a vector to capture the return instructions in the cloned function... |
| 108 | std::vector<ReturnInst*> Returns; |
| 109 | |
| 110 | // Populate the value map with all of the globals in the program. |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 111 | // FIXME: This should be the default for CloneFunctionInto! |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 112 | Module &M = *Caller->getParent(); |
| 113 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 114 | ValueMap[I] = I; |
| 115 | for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I) |
| 116 | ValueMap[I] = I; |
| 117 | |
| 118 | // Do all of the hard part of cloning the callee into the caller... |
| 119 | CloneFunctionInto(Caller, CalledFunc, ValueMap, Returns, ".i"); |
| 120 | |
| 121 | // Loop over all of the return instructions, turning them into unconditional |
| 122 | // branches to the merge point now... |
| 123 | for (unsigned i = 0, e = Returns.size(); i != e; ++i) { |
| 124 | ReturnInst *RI = Returns[i]; |
| 125 | BasicBlock *BB = RI->getParent(); |
| 126 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 127 | // Add a branch to the merge point where the PHI node lives if it exists. |
| 128 | new BranchInst(AfterCallBB, RI); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 129 | |
| 130 | if (PHI) { // The PHI node should include this value! |
| 131 | assert(RI->getReturnValue() && "Ret should have value!"); |
| 132 | assert(RI->getReturnValue()->getType() == PHI->getType() && |
| 133 | "Ret value not consistent in function!"); |
| 134 | PHI->addIncoming(RI->getReturnValue(), BB); |
| 135 | } |
| 136 | |
| 137 | // Delete the return instruction now |
| 138 | BB->getInstList().erase(RI); |
| 139 | } |
| 140 | |
| 141 | // Check to see if the PHI node only has one argument. This is a common |
| 142 | // case resulting from there only being a single return instruction in the |
| 143 | // function call. Because this is so common, eliminate the PHI node. |
| 144 | // |
| 145 | if (PHI && PHI->getNumIncomingValues() == 1) { |
| 146 | PHI->replaceAllUsesWith(PHI->getIncomingValue(0)); |
| 147 | PHI->getParent()->getInstList().erase(PHI); |
| 148 | } |
| 149 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 150 | // Change the branch that used to go to AfterCallBB to branch to the first |
| 151 | // basic block of the inlined function. |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 152 | // |
| 153 | TerminatorInst *Br = OrigBB->getTerminator(); |
| 154 | assert(Br && Br->getOpcode() == Instruction::Br && |
| 155 | "splitBasicBlock broken!"); |
| 156 | Br->setOperand(0, ++LastBlock); |
| 157 | |
| 158 | // If there are any alloca instructions in the block that used to be the entry |
| 159 | // block for the callee, move them to the entry block of the caller. First |
| 160 | // calculate which instruction they should be inserted before. We insert the |
| 161 | // instructions at the end of the current alloca list. |
| 162 | // |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 163 | if (isa<AllocaInst>(LastBlock->begin())) { |
| 164 | BasicBlock::iterator InsertPoint = Caller->begin()->begin(); |
| 165 | while (isa<AllocaInst>(InsertPoint)) ++InsertPoint; |
| 166 | |
| 167 | for (BasicBlock::iterator I = LastBlock->begin(), E = LastBlock->end(); |
| 168 | I != E; ) |
| 169 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) { |
| 170 | ++I; // Move to the next instruction |
| 171 | LastBlock->getInstList().remove(AI); |
| 172 | Caller->front().getInstList().insert(InsertPoint, AI); |
| 173 | } else { |
| 174 | ++I; |
| 175 | } |
| 176 | } |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 177 | |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 178 | // If we just inlined a call due to an invoke instruction, scan the inlined |
| 179 | // function checking for function calls that should now be made into invoke |
| 180 | // instructions, and for llvm.exc.rethrow()'s which should be turned into |
| 181 | // branches. |
| 182 | if (InvokeDest) |
| 183 | for (Function::iterator BB = LastBlock, E = Caller->end(); BB != E; ++BB) |
| 184 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { |
| 185 | // We only need to check for function calls: inlined invoke instructions |
| 186 | // require no special handling... |
| 187 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 188 | // FIXME: this should use annotations of the LLVM functions themselves |
| 189 | // to determine whether or not the function can throw. |
| 190 | bool ShouldInvokify = true; |
| 191 | |
| 192 | if (Function *F = CI->getCalledFunction()) |
| 193 | if (unsigned ID = F->getIntrinsicID()) |
Chris Lattner | 657fbe6 | 2003-08-24 12:24:11 +0000 | [diff] [blame^] | 194 | if (ID == LLVMIntrinsic::unwind) { |
| 195 | // llvm.unwind requires special handling when it gets inlined |
| 196 | // into an invoke site. Once this happens, we know that the |
| 197 | // unwind would cause a control transfer to the invoke exception |
| 198 | // destination, so we can transform it into a direct branch to |
| 199 | // the exception destination. |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 200 | BranchInst *BI = new BranchInst(InvokeDest, CI); |
| 201 | |
| 202 | // Note that since any instructions after the rethrow/branch are |
| 203 | // dead, we must delete them now (otherwise the terminator we |
| 204 | // just inserted wouldn't be at the end of the basic block!) |
| 205 | BasicBlock *CurBB = BB; |
| 206 | while (&CurBB->back() != BI) { |
| 207 | Instruction *I = &CurBB->back(); |
| 208 | if (!I->use_empty()) |
| 209 | I->replaceAllUsesWith(Constant::getNullValue(I->getType())); |
| 210 | CurBB->getInstList().pop_back(); |
| 211 | } |
| 212 | |
| 213 | break; // Done with this basic block! |
Chris Lattner | 657fbe6 | 2003-08-24 12:24:11 +0000 | [diff] [blame^] | 214 | } else if (ID == LLVMIntrinsic::exc_setcurrent || |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 215 | ID == LLVMIntrinsic::exc_getcurrent) { |
| 216 | ShouldInvokify = false; // Not correct to invokify exc.throw! |
| 217 | } |
| 218 | |
| 219 | // If we should convert this function into an invoke instruction, do |
| 220 | // so now. |
| 221 | if (ShouldInvokify) { |
| 222 | // First, split the basic block... |
| 223 | BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc"); |
| 224 | |
| 225 | // Next, create the new invoke instruction, inserting it at the end |
| 226 | // of the old basic block. |
| 227 | new InvokeInst(CI->getCalledValue(), Split, InvokeDest, |
| 228 | std::vector<Value*>(CI->op_begin()+1, CI->op_end()), |
| 229 | CI->getName(), BB->getTerminator()); |
| 230 | |
| 231 | // Delete the unconditional branch inserted by splitBasicBlock |
| 232 | BB->getInstList().pop_back(); |
| 233 | Split->getInstList().pop_front(); // Delete the original call |
| 234 | |
| 235 | // This basic block is now complete, start scanning the next one. |
| 236 | break; |
| 237 | } else { |
| 238 | ++I; |
| 239 | } |
| 240 | } else { |
| 241 | ++I; |
| 242 | } |
| 243 | } |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 244 | |
| 245 | // Now that the function is correct, make it a little bit nicer. In |
| 246 | // particular, move the basic blocks inserted from the end of the function |
| 247 | // into the space made by splitting the source basic block. |
| 248 | // |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 249 | Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(), |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 250 | LastBlock, Caller->end()); |
| 251 | |
Chris Lattner | 7152c23 | 2003-08-24 04:06:56 +0000 | [diff] [blame] | 252 | // We should always be able to fold the entry block of the function into the |
| 253 | // single predecessor of the block... |
| 254 | assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!"); |
| 255 | BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0); |
| 256 | SimplifyCFG(CalleeEntry); |
| 257 | |
| 258 | // Okay, continue the CFG cleanup. It's often the case that there is only a |
| 259 | // single return instruction in the callee function. If this is the case, |
Chris Lattner | 80a38d2 | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 260 | // then we have an unconditional branch from the return block to the |
| 261 | // 'AfterCallBB'. Check for this case, and eliminate the branch is possible. |
| 262 | SimplifyCFG(AfterCallBB); |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 263 | return true; |
| 264 | } |