InlineSimple.cpp revision da78b002ca6bdaf9fd58443d943f60b8529bcf36
1237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner//===- InlineSimple.cpp - Code to perform simple function inlining --------===// 2009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner// 3ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// This file implements bottom-up inlining of functions into callees. 40154505ab74e7bd0d4dc85dbddc1ff0df6357606Chris Lattner// 5009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===----------------------------------------------------------------------===// 6009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner 7237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner#include "Inliner.h" 8237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner#include "llvm/Function.h" 9ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner#include "llvm/iMemory.h" 10e54453387486c1d5e61401e1d4febd3f6ebe86cfChris Lattner#include "llvm/Support/CallSite.h" 11237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner#include "llvm/Transforms/IPO.h" 127e70829632f82de15db187845666aaca6e04b792Chris Lattner 13ca398dc3989d35e8516489fd163e012133bd41cbChris Lattnernamespace { 14884d6c4e10501116a8ff45616231564a2738daddChris Lattner // FunctionInfo - For each function, calculate the size of it in blocks and 15884d6c4e10501116a8ff45616231564a2738daddChris Lattner // instructions. 16884d6c4e10501116a8ff45616231564a2738daddChris Lattner struct FunctionInfo { 17884d6c4e10501116a8ff45616231564a2738daddChris Lattner unsigned NumInsts, NumBlocks; 18884d6c4e10501116a8ff45616231564a2738daddChris Lattner 19884d6c4e10501116a8ff45616231564a2738daddChris Lattner FunctionInfo() : NumInsts(0), NumBlocks(0) {} 20884d6c4e10501116a8ff45616231564a2738daddChris Lattner }; 21884d6c4e10501116a8ff45616231564a2738daddChris Lattner 22884d6c4e10501116a8ff45616231564a2738daddChris Lattner class SimpleInliner : public Inliner { 23884d6c4e10501116a8ff45616231564a2738daddChris Lattner std::map<const Function*, FunctionInfo> CachedFunctionInfo; 24884d6c4e10501116a8ff45616231564a2738daddChris Lattner public: 25237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner int getInlineCost(CallSite CS); 26ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner }; 27237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner RegisterOpt<SimpleInliner> X("inline", "Function Integration/Inlining"); 28ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner} 29ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 30237ef567f6764f24a47c63121cc0a599ddc8f56dChris LattnerPass *createFunctionInliningPass() { return new SimpleInliner(); } 31009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner 32237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner// getInlineCost - The heuristic used to determine if we should inline the 33237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner// function call or not. 34ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner// 35237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattnerint SimpleInliner::getInlineCost(CallSite CS) { 36e54453387486c1d5e61401e1d4febd3f6ebe86cfChris Lattner Instruction *TheCall = CS.getInstruction(); 37e54453387486c1d5e61401e1d4febd3f6ebe86cfChris Lattner const Function *Callee = CS.getCalledFunction(); 38e54453387486c1d5e61401e1d4febd3f6ebe86cfChris Lattner const Function *Caller = TheCall->getParent()->getParent(); 39009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner 40237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner // Don't inline a directly recursive call. 41237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner if (Caller == Callee) return 2000000000; 42237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner 43237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner // InlineCost - This value measures how good of an inline candidate this call 44237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner // site is to inline. A lower inline cost make is more likely for the call to 45237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner // be inlined. This value may go negative. 46009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner // 47237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner int InlineCost = 0; 48009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner 49ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // If there is only one call of the function, and it has internal linkage, 50ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // make it almost guaranteed to be inlined. 51009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner // 52ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner if (Callee->use_size() == 1 && Callee->hasInternalLinkage()) 53237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner InlineCost -= 30000; 54009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner 55ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // Add to the inline quality for properties that make the call valueable to 56ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // inline. This includes factors that indicate that the result of inlining 57ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // the function will be optimizable. Currently this just looks at arguments 58ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // passed into the function. 59ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // 60e54453387486c1d5e61401e1d4febd3f6ebe86cfChris Lattner for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); 61e54453387486c1d5e61401e1d4febd3f6ebe86cfChris Lattner I != E; ++I) { 62ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // Each argument passed in has a cost at both the caller and the callee 63ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // sides. This favors functions that take many arguments over functions 64ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // that take few arguments. 65237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner InlineCost -= 20; 66ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 67ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // If this is a function being passed in, it is very likely that we will be 68ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // able to turn an indirect function call into a direct function call. 69ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner if (isa<Function>(I)) 70237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner InlineCost -= 100; 71ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner 72ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // If a constant, global variable or alloca is passed in, inlining this 73ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // function is likely to allow significant future optimization possibilities 74ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // (constant propagation, scalar promotion, and scalarization), so encourage 75ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // the inlining of the function. 76009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner // 77ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner else if (isa<Constant>(I) || isa<GlobalVariable>(I) || isa<AllocaInst>(I)) 78237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner InlineCost -= 60; 79009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner } 80009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner 81ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // Now that we have considered all of the factors that make the call site more 82ca398dc3989d35e8516489fd163e012133bd41cbChris Lattner // likely to be inlined, look at factors that make us not want to inline it. 83884d6c4e10501116a8ff45616231564a2738daddChris Lattner FunctionInfo &CalleeFI = CachedFunctionInfo[Callee]; 84dcd8040d115803e427dc1caf9feb44a894eef927Chris Lattner 85884d6c4e10501116a8ff45616231564a2738daddChris Lattner // If we haven't calculated this information yet... 86884d6c4e10501116a8ff45616231564a2738daddChris Lattner if (CalleeFI.NumBlocks == 0) { 87884d6c4e10501116a8ff45616231564a2738daddChris Lattner unsigned NumInsts = 0, NumBlocks = 0; 88dcd8040d115803e427dc1caf9feb44a894eef927Chris Lattner 89884d6c4e10501116a8ff45616231564a2738daddChris Lattner // Look at the size of the callee. Each basic block counts as 20 units, and 90884d6c4e10501116a8ff45616231564a2738daddChris Lattner // each instruction counts as 10. 91884d6c4e10501116a8ff45616231564a2738daddChris Lattner for (Function::const_iterator BB = Callee->begin(), E = Callee->end(); 92884d6c4e10501116a8ff45616231564a2738daddChris Lattner BB != E; ++BB) { 93884d6c4e10501116a8ff45616231564a2738daddChris Lattner NumInsts += BB->size(); 94884d6c4e10501116a8ff45616231564a2738daddChris Lattner NumBlocks++; 95884d6c4e10501116a8ff45616231564a2738daddChris Lattner } 96884d6c4e10501116a8ff45616231564a2738daddChris Lattner CalleeFI.NumBlocks = NumBlocks; 97884d6c4e10501116a8ff45616231564a2738daddChris Lattner CalleeFI.NumInsts = NumInsts; 98884d6c4e10501116a8ff45616231564a2738daddChris Lattner } 99009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner 100da78b002ca6bdaf9fd58443d943f60b8529bcf36Chris Lattner // Don't inline into something too big, which would make it bigger. Here, we 101da78b002ca6bdaf9fd58443d943f60b8529bcf36Chris Lattner // count each basic block as a single unit. 102da78b002ca6bdaf9fd58443d943f60b8529bcf36Chris Lattner InlineCost += Caller->size()*2; 103da78b002ca6bdaf9fd58443d943f60b8529bcf36Chris Lattner 104da78b002ca6bdaf9fd58443d943f60b8529bcf36Chris Lattner 105da78b002ca6bdaf9fd58443d943f60b8529bcf36Chris Lattner // Look at the size of the callee. Each basic block counts as 20 units, and 106884d6c4e10501116a8ff45616231564a2738daddChris Lattner // each instruction counts as 10. 107884d6c4e10501116a8ff45616231564a2738daddChris Lattner InlineCost += CalleeFI.NumInsts*10 + CalleeFI.NumBlocks*20; 108237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner return InlineCost; 109009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner} 110