Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1 | //===- FunctionInlining.cpp - Code to perform function inlining -----------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 3 | // This file implements inlining of functions. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 4 | // |
| 5 | // Specifically, this: |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 6 | // * Exports functionality to inline any function call |
| 7 | // * Inlines functions that consist of a single basic block |
| 8 | // * Is able to inline ANY function call |
| 9 | // . Has a smart heuristic for when to inline a function |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 10 | // |
Chris Lattner | 1c8d3f8 | 2002-04-18 18:52:03 +0000 | [diff] [blame] | 11 | // FIXME: This pass should transform alloca instructions in the called function |
Chris Lattner | 661841c | 2003-01-13 00:27:23 +0000 | [diff] [blame] | 12 | // into malloc/free pairs! Or perhaps it should refuse to inline them! |
Chris Lattner | 1c8d3f8 | 2002-04-18 18:52:03 +0000 | [diff] [blame] | 13 | // |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chris Lattner | 1666751 | 2002-11-19 20:59:41 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/IPO.h" |
| 17 | #include "llvm/Transforms/Utils/Cloning.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 19 | #include "llvm/Pass.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 20 | #include "llvm/iTerminators.h" |
Chris Lattner | fb5ae02 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 21 | #include "llvm/iPHINode.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 22 | #include "llvm/iOther.h" |
Chris Lattner | f8e4dc3 | 2002-04-08 22:03:00 +0000 | [diff] [blame] | 23 | #include "llvm/Type.h" |
Chris Lattner | 3cf3782 | 2002-10-01 22:38:37 +0000 | [diff] [blame] | 24 | #include "Support/Statistic.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 3cf3782 | 2002-10-01 22:38:37 +0000 | [diff] [blame] | 27 | static Statistic<> NumInlined("inline", "Number of functions inlined"); |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 28 | using std::cerr; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 29 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 30 | // InlineFunction - This function forcibly inlines the called function into the |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 31 | // basic block of the caller. This returns false if it is not possible to |
| 32 | // inline this call. The program is still in a well defined state if this |
| 33 | // occurs though. |
| 34 | // |
| 35 | // Note that this only does one level of inlining. For example, if the |
| 36 | // instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now |
| 37 | // exists in the instruction stream. Similiarly this will inline a recursive |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 38 | // function by one level. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 39 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 40 | bool InlineFunction(CallInst *CI) { |
| 41 | assert(isa<CallInst>(CI) && "InlineFunction only works on CallInst nodes"); |
| 42 | assert(CI->getParent() && "Instruction not embedded in basic block!"); |
| 43 | assert(CI->getParent()->getParent() && "Instruction not in function!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 45 | const Function *CalledFunc = CI->getCalledFunction(); |
| 46 | if (CalledFunc == 0 || // Can't inline external function or indirect call! |
| 47 | CalledFunc->isExternal()) return false; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 49 | //cerr << "Inlining " << CalledFunc->getName() << " into " |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 50 | // << CurrentMeth->getName() << "\n"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 51 | |
| 52 | BasicBlock *OrigBB = CI->getParent(); |
| 53 | |
| 54 | // Call splitBasicBlock - The original basic block now ends at the instruction |
| 55 | // immediately before the call. The original basic block now ends with an |
| 56 | // unconditional branch to NewBB, and NewBB starts with the call instruction. |
| 57 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 58 | BasicBlock *NewBB = OrigBB->splitBasicBlock(CI); |
Chris Lattner | f20671a | 2002-02-25 00:31:02 +0000 | [diff] [blame] | 59 | NewBB->setName("InlinedFunctionReturnNode"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 60 | |
| 61 | // Remove (unlink) the CallInst from the start of the new basic block. |
| 62 | NewBB->getInstList().remove(CI); |
| 63 | |
| 64 | // If we have a return value generated by this call, convert it into a PHI |
| 65 | // node that gets values from each of the old RET instructions in the original |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 66 | // function. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 67 | // |
| 68 | PHINode *PHI = 0; |
Chris Lattner | b112005 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 69 | if (!CI->use_empty()) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 70 | // The PHI node should go at the front of the new basic block to merge all |
| 71 | // possible incoming values. |
| 72 | // |
Chris Lattner | f80f7b0 | 2002-09-10 22:38:49 +0000 | [diff] [blame] | 73 | PHI = new PHINode(CalledFunc->getReturnType(), CI->getName(), |
| 74 | NewBB->begin()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 75 | |
| 76 | // Anything that used the result of the function call should now use the PHI |
| 77 | // node as their operand. |
| 78 | // |
| 79 | CI->replaceAllUsesWith(PHI); |
| 80 | } |
| 81 | |
Chris Lattner | b112005 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 82 | // Get a pointer to the last basic block in the function, which will have the |
| 83 | // new function inlined after it. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 84 | // |
Chris Lattner | b112005 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 85 | Function::iterator LastBlock = &OrigBB->getParent()->back(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 86 | |
Chris Lattner | b112005 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 87 | // Calculate the vector of arguments to pass into the function cloner... |
Chris Lattner | c362618 | 2002-11-19 22:54:01 +0000 | [diff] [blame] | 88 | std::map<const Value*, Value*> ValueMap; |
| 89 | assert((unsigned)std::distance(CalledFunc->abegin(), CalledFunc->aend()) == |
| 90 | CI->getNumOperands()-1 && "No varargs calls can be inlined yet!"); |
| 91 | |
| 92 | unsigned i = 1; |
| 93 | for (Function::const_aiterator I = CalledFunc->abegin(), E=CalledFunc->aend(); |
| 94 | I != E; ++I, ++i) |
| 95 | ValueMap[I] = CI->getOperand(i); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 96 | |
Chris Lattner | b112005 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 97 | // Since we are now done with the CallInst, we can delete it. |
| 98 | delete CI; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 99 | |
Chris Lattner | b112005 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 100 | // Make a vector to capture the return instructions in the cloned function... |
| 101 | std::vector<ReturnInst*> Returns; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 661841c | 2003-01-13 00:27:23 +0000 | [diff] [blame] | 103 | // Populate the value map with all of the globals in the program. |
| 104 | Module &M = *OrigBB->getParent()->getParent(); |
| 105 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 106 | ValueMap[I] = I; |
| 107 | for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I) |
| 108 | ValueMap[I] = I; |
| 109 | |
Chris Lattner | b112005 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 110 | // Do all of the hard part of cloning the callee into the caller... |
Chris Lattner | c362618 | 2002-11-19 22:54:01 +0000 | [diff] [blame] | 111 | CloneFunctionInto(OrigBB->getParent(), CalledFunc, ValueMap, Returns, ".i"); |
Chris Lattner | b112005 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 112 | |
| 113 | // Loop over all of the return instructions, turning them into unconditional |
| 114 | // branches to the merge point now... |
| 115 | for (unsigned i = 0, e = Returns.size(); i != e; ++i) { |
| 116 | ReturnInst *RI = Returns[i]; |
| 117 | BasicBlock *BB = RI->getParent(); |
| 118 | |
| 119 | // Add a branch to the merge point where the PHI node would live... |
| 120 | new BranchInst(NewBB, RI); |
| 121 | |
| 122 | if (PHI) { // The PHI node should include this value! |
| 123 | assert(RI->getReturnValue() && "Ret should have value!"); |
| 124 | assert(RI->getReturnValue()->getType() == PHI->getType() && |
| 125 | "Ret value not consistent in function!"); |
| 126 | PHI->addIncoming(RI->getReturnValue(), BB); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Chris Lattner | b112005 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 129 | // Delete the return instruction now |
| 130 | BB->getInstList().erase(RI); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Chris Lattner | b112005 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 133 | // Check to see if the PHI node only has one argument. This is a common |
| 134 | // case resulting from there only being a single return instruction in the |
| 135 | // function call. Because this is so common, eliminate the PHI node. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 136 | // |
Chris Lattner | b112005 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 137 | if (PHI && PHI->getNumIncomingValues() == 1) { |
| 138 | PHI->replaceAllUsesWith(PHI->getIncomingValue(0)); |
| 139 | PHI->getParent()->getInstList().erase(PHI); |
Chris Lattner | facc751 | 2002-09-22 18:41:25 +0000 | [diff] [blame] | 140 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 141 | |
| 142 | // Change the branch that used to go to NewBB to branch to the first basic |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 143 | // block of the inlined function. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 144 | // |
| 145 | TerminatorInst *Br = OrigBB->getTerminator(); |
Chris Lattner | b1ca9cb | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 146 | assert(Br && Br->getOpcode() == Instruction::Br && |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 147 | "splitBasicBlock broken!"); |
Chris Lattner | b112005 | 2002-11-19 21:54:07 +0000 | [diff] [blame] | 148 | Br->setOperand(0, ++LastBlock); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 149 | return true; |
| 150 | } |
| 151 | |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 152 | static inline bool ShouldInlineFunction(const CallInst *CI, const Function *F) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 153 | assert(CI->getParent() && CI->getParent()->getParent() && |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 154 | "Call not embedded into a function!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 155 | |
| 156 | // Don't inline a recursive call. |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 157 | if (CI->getParent()->getParent() == F) return false; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 158 | |
| 159 | // Don't inline something too big. This is a really crappy heuristic |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 160 | if (F->size() > 3) return false; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 161 | |
| 162 | // Don't inline into something too big. This is a **really** crappy heuristic |
Chris Lattner | 4cee8d8 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 163 | if (CI->getParent()->getParent()->size() > 10) return false; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 164 | |
| 165 | // Go ahead and try just about anything else. |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 170 | static inline bool DoFunctionInlining(BasicBlock *BB) { |
Chris Lattner | 4cee8d8 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 171 | for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 172 | if (CallInst *CI = dyn_cast<CallInst>(&*I)) { |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 173 | // Check to see if we should inline this function |
| 174 | Function *F = CI->getCalledFunction(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 175 | if (F && ShouldInlineFunction(CI, F)) { |
| 176 | return InlineFunction(CI); |
| 177 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | return false; |
| 181 | } |
| 182 | |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 183 | // doFunctionInlining - Use a heuristic based approach to inline functions that |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 184 | // seem to look good. |
| 185 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 186 | static bool doFunctionInlining(Function &F) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 187 | bool Changed = false; |
| 188 | |
| 189 | // Loop through now and inline instructions a basic block at a time... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 190 | for (Function::iterator I = F.begin(); I != F.end(); ) |
| 191 | if (DoFunctionInlining(I)) { |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 192 | ++NumInlined; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 193 | Changed = true; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 194 | } else { |
| 195 | ++I; |
| 196 | } |
| 197 | |
| 198 | return Changed; |
| 199 | } |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 200 | |
| 201 | namespace { |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 202 | struct FunctionInlining : public FunctionPass { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 203 | virtual bool runOnFunction(Function &F) { |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 204 | return doFunctionInlining(F); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 205 | } |
| 206 | }; |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 207 | RegisterOpt<FunctionInlining> X("inline", "Function Integration/Inlining"); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 210 | Pass *createFunctionInliningPass() { return new FunctionInlining(); } |