LLVM API Documentation
00001 //===- InlineSimple.cpp - Code to perform simple function inlining --------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements bottom-up inlining of functions into callees. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "Inliner.h" 00015 #include "llvm/CallingConv.h" 00016 #include "llvm/Instructions.h" 00017 #include "llvm/IntrinsicInst.h" 00018 #include "llvm/Function.h" 00019 #include "llvm/Type.h" 00020 #include "llvm/Support/CallSite.h" 00021 #include "llvm/Transforms/IPO.h" 00022 using namespace llvm; 00023 00024 namespace { 00025 struct ArgInfo { 00026 unsigned ConstantWeight; 00027 unsigned AllocaWeight; 00028 00029 ArgInfo(unsigned CWeight, unsigned AWeight) 00030 : ConstantWeight(CWeight), AllocaWeight(AWeight) {} 00031 }; 00032 00033 // FunctionInfo - For each function, calculate the size of it in blocks and 00034 // instructions. 00035 struct FunctionInfo { 00036 // NumInsts, NumBlocks - Keep track of how large each function is, which is 00037 // used to estimate the code size cost of inlining it. 00038 unsigned NumInsts, NumBlocks; 00039 00040 // ArgumentWeights - Each formal argument of the function is inspected to 00041 // see if it is used in any contexts where making it a constant or alloca 00042 // would reduce the code size. If so, we add some value to the argument 00043 // entry here. 00044 std::vector<ArgInfo> ArgumentWeights; 00045 00046 FunctionInfo() : NumInsts(0), NumBlocks(0) {} 00047 00048 /// analyzeFunction - Fill in the current structure with information gleaned 00049 /// from the specified function. 00050 void analyzeFunction(Function *F); 00051 }; 00052 00053 class SimpleInliner : public Inliner { 00054 std::map<const Function*, FunctionInfo> CachedFunctionInfo; 00055 public: 00056 int getInlineCost(CallSite CS); 00057 }; 00058 RegisterOpt<SimpleInliner> X("inline", "Function Integration/Inlining"); 00059 } 00060 00061 ModulePass *llvm::createFunctionInliningPass() { return new SimpleInliner(); } 00062 00063 // CountCodeReductionForConstant - Figure out an approximation for how many 00064 // instructions will be constant folded if the specified value is constant. 00065 // 00066 static unsigned CountCodeReductionForConstant(Value *V) { 00067 unsigned Reduction = 0; 00068 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI) 00069 if (isa<BranchInst>(*UI)) 00070 Reduction += 40; // Eliminating a conditional branch is a big win 00071 else if (SwitchInst *SI = dyn_cast<SwitchInst>(*UI)) 00072 // Eliminating a switch is a big win, proportional to the number of edges 00073 // deleted. 00074 Reduction += (SI->getNumSuccessors()-1) * 40; 00075 else if (CallInst *CI = dyn_cast<CallInst>(*UI)) { 00076 // Turning an indirect call into a direct call is a BIG win 00077 Reduction += CI->getCalledValue() == V ? 500 : 0; 00078 } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) { 00079 // Turning an indirect call into a direct call is a BIG win 00080 Reduction += II->getCalledValue() == V ? 500 : 0; 00081 } else { 00082 // Figure out if this instruction will be removed due to simple constant 00083 // propagation. 00084 Instruction &Inst = cast<Instruction>(**UI); 00085 bool AllOperandsConstant = true; 00086 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) 00087 if (!isa<Constant>(Inst.getOperand(i)) && Inst.getOperand(i) != V) { 00088 AllOperandsConstant = false; 00089 break; 00090 } 00091 00092 if (AllOperandsConstant) { 00093 // We will get to remove this instruction... 00094 Reduction += 7; 00095 00096 // And any other instructions that use it which become constants 00097 // themselves. 00098 Reduction += CountCodeReductionForConstant(&Inst); 00099 } 00100 } 00101 00102 return Reduction; 00103 } 00104 00105 // CountCodeReductionForAlloca - Figure out an approximation of how much smaller 00106 // the function will be if it is inlined into a context where an argument 00107 // becomes an alloca. 00108 // 00109 static unsigned CountCodeReductionForAlloca(Value *V) { 00110 if (!isa<PointerType>(V->getType())) return 0; // Not a pointer 00111 unsigned Reduction = 0; 00112 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){ 00113 Instruction *I = cast<Instruction>(*UI); 00114 if (isa<LoadInst>(I) || isa<StoreInst>(I)) 00115 Reduction += 10; 00116 else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) { 00117 // If the GEP has variable indices, we won't be able to do much with it. 00118 for (Instruction::op_iterator I = GEP->op_begin()+1, E = GEP->op_end(); 00119 I != E; ++I) 00120 if (!isa<Constant>(*I)) return 0; 00121 Reduction += CountCodeReductionForAlloca(GEP)+15; 00122 } else { 00123 // If there is some other strange instruction, we're not going to be able 00124 // to do much if we inline this. 00125 return 0; 00126 } 00127 } 00128 00129 return Reduction; 00130 } 00131 00132 /// analyzeFunction - Fill in the current structure with information gleaned 00133 /// from the specified function. 00134 void FunctionInfo::analyzeFunction(Function *F) { 00135 unsigned NumInsts = 0, NumBlocks = 0; 00136 00137 // Look at the size of the callee. Each basic block counts as 20 units, and 00138 // each instruction counts as 10. 00139 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { 00140 for (BasicBlock::const_iterator II = BB->begin(), E = BB->end(); 00141 II != E; ++II) 00142 if (!isa<DbgInfoIntrinsic>(II)) 00143 ++NumInsts; 00144 00145 ++NumBlocks; 00146 } 00147 00148 this->NumBlocks = NumBlocks; 00149 this->NumInsts = NumInsts; 00150 00151 // Check out all of the arguments to the function, figuring out how much 00152 // code can be eliminated if one of the arguments is a constant. 00153 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I) 00154 ArgumentWeights.push_back(ArgInfo(CountCodeReductionForConstant(I), 00155 CountCodeReductionForAlloca(I))); 00156 } 00157 00158 00159 // getInlineCost - The heuristic used to determine if we should inline the 00160 // function call or not. 00161 // 00162 int SimpleInliner::getInlineCost(CallSite CS) { 00163 Instruction *TheCall = CS.getInstruction(); 00164 Function *Callee = CS.getCalledFunction(); 00165 const Function *Caller = TheCall->getParent()->getParent(); 00166 00167 // Don't inline a directly recursive call. 00168 if (Caller == Callee) return 2000000000; 00169 00170 // InlineCost - This value measures how good of an inline candidate this call 00171 // site is to inline. A lower inline cost make is more likely for the call to 00172 // be inlined. This value may go negative. 00173 // 00174 int InlineCost = 0; 00175 00176 // If there is only one call of the function, and it has internal linkage, 00177 // make it almost guaranteed to be inlined. 00178 // 00179 if (Callee->hasInternalLinkage() && Callee->hasOneUse()) 00180 InlineCost -= 30000; 00181 00182 // If this function uses the coldcc calling convention, prefer not to inline 00183 // it. 00184 if (Callee->getCallingConv() == CallingConv::Cold) 00185 InlineCost += 2000; 00186 00187 // If the instruction after the call, or if the normal destination of the 00188 // invoke is an unreachable instruction, the function is noreturn. As such, 00189 // there is little point in inlining this. 00190 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { 00191 if (isa<UnreachableInst>(II->getNormalDest()->begin())) 00192 InlineCost += 10000; 00193 } else if (isa<UnreachableInst>(++BasicBlock::iterator(TheCall))) 00194 InlineCost += 10000; 00195 00196 // Get information about the callee... 00197 FunctionInfo &CalleeFI = CachedFunctionInfo[Callee]; 00198 00199 // If we haven't calculated this information yet, do so now. 00200 if (CalleeFI.NumBlocks == 0) 00201 CalleeFI.analyzeFunction(Callee); 00202 00203 // Add to the inline quality for properties that make the call valuable to 00204 // inline. This includes factors that indicate that the result of inlining 00205 // the function will be optimizable. Currently this just looks at arguments 00206 // passed into the function. 00207 // 00208 unsigned ArgNo = 0; 00209 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); 00210 I != E; ++I, ++ArgNo) { 00211 // Each argument passed in has a cost at both the caller and the callee 00212 // sides. This favors functions that take many arguments over functions 00213 // that take few arguments. 00214 InlineCost -= 20; 00215 00216 // If this is a function being passed in, it is very likely that we will be 00217 // able to turn an indirect function call into a direct function call. 00218 if (isa<Function>(I)) 00219 InlineCost -= 100; 00220 00221 // If an alloca is passed in, inlining this function is likely to allow 00222 // significant future optimization possibilities (like scalar promotion, and 00223 // scalarization), so encourage the inlining of the function. 00224 // 00225 else if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) { 00226 if (ArgNo < CalleeFI.ArgumentWeights.size()) 00227 InlineCost -= CalleeFI.ArgumentWeights[ArgNo].AllocaWeight; 00228 00229 // If this is a constant being passed into the function, use the argument 00230 // weights calculated for the callee to determine how much will be folded 00231 // away with this information. 00232 } else if (isa<Constant>(I)) { 00233 if (ArgNo < CalleeFI.ArgumentWeights.size()) 00234 InlineCost -= CalleeFI.ArgumentWeights[ArgNo].ConstantWeight; 00235 } 00236 } 00237 00238 // Now that we have considered all of the factors that make the call site more 00239 // likely to be inlined, look at factors that make us not want to inline it. 00240 00241 // Don't inline into something too big, which would make it bigger. Here, we 00242 // count each basic block as a single unit. 00243 // 00244 InlineCost += Caller->size()/20; 00245 00246 00247 // Look at the size of the callee. Each basic block counts as 20 units, and 00248 // each instruction counts as 5. 00249 InlineCost += CalleeFI.NumInsts*5 + CalleeFI.NumBlocks*20; 00250 return InlineCost; 00251 } 00252