Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 1 | //===- InlineCommon.cpp - Code common to all inliners ---------------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the code shared between the LLVM inliners. This |
| 11 | // implements all of the boring mechanics of the bottom-up inlining. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "Inliner.h" |
Chris Lattner | bb9ae15 | 2003-10-31 21:05:58 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" // ConstantPointerRef should die |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/iOther.h" |
| 19 | #include "llvm/iTerminators.h" |
| 20 | #include "llvm/Analysis/CallGraph.h" |
| 21 | #include "llvm/Support/CallSite.h" |
| 22 | #include "llvm/Transforms/Utils/Cloning.h" |
| 23 | #include "Support/CommandLine.h" |
| 24 | #include "Support/Debug.h" |
| 25 | #include "Support/Statistic.h" |
Chris Lattner | a51bcb5 | 2003-11-21 21:45:31 +0000 | [diff] [blame] | 26 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | Statistic<> NumInlined("inline", "Number of functions inlined"); |
| 30 | Statistic<> NumDeleted("inline", "Number of functions deleted because all callers found"); |
| 31 | cl::opt<unsigned> // FIXME: 200 is VERY conservative |
| 32 | InlineLimit("inline-threshold", cl::Hidden, cl::init(200), |
| 33 | cl::desc("Control the amount of inlining to perform (default = 200)")); |
| 34 | } |
| 35 | |
| 36 | Inliner::Inliner() : InlineThreshold(InlineLimit) {} |
| 37 | |
| 38 | int Inliner::getRecursiveInlineCost(CallSite CS) { |
| 39 | return getInlineCost(CS); |
| 40 | } |
| 41 | |
| 42 | bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) { |
| 43 | CallGraph &CG = getAnalysis<CallGraph>(); |
| 44 | |
| 45 | std::set<Function*> SCCFunctions; |
| 46 | DEBUG(std::cerr << "Inliner visiting SCC:"); |
| 47 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) { |
| 48 | SCCFunctions.insert(SCC[i]->getFunction()); |
| 49 | DEBUG(std::cerr << " " << (SCC[i]->getFunction() ? |
| 50 | SCC[i]->getFunction()->getName() : "INDIRECTNODE")); |
| 51 | } |
| 52 | DEBUG(std::cerr << "\n"); |
| 53 | |
| 54 | bool Changed = false; |
| 55 | for (std::set<Function*>::iterator SCCI = SCCFunctions.begin(), |
| 56 | E = SCCFunctions.end(); SCCI != E; ++SCCI) { |
| 57 | Function *F = *SCCI; |
| 58 | if (F == 0 || F->isExternal()) continue; |
| 59 | DEBUG(std::cerr << " Inspecting function: " << F->getName() << "\n"); |
| 60 | |
| 61 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 62 | for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ) { |
| 63 | bool ShouldInc = true; |
| 64 | // Found a call or invoke instruction? |
| 65 | if (isa<CallInst>(I) || isa<InvokeInst>(I)) { |
| 66 | CallSite CS = CallSite::get(I); |
| 67 | if (Function *Callee = CS.getCalledFunction()) |
Chris Lattner | d77922f | 2003-11-09 05:05:36 +0000 | [diff] [blame] | 68 | if (!Callee->isExternal() && !IsRecursiveFunction.count(Callee)) { |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 69 | // Determine whether this is a function IN the SCC... |
| 70 | bool inSCC = SCCFunctions.count(Callee); |
| 71 | |
Chris Lattner | d77922f | 2003-11-09 05:05:36 +0000 | [diff] [blame] | 72 | // Keep track of whether this is a directly recursive function. |
| 73 | if (Callee == F) IsRecursiveFunction.insert(F); |
| 74 | |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 75 | // If the policy determines that we should inline this function, |
| 76 | // try to do so... |
| 77 | int InlineCost = inSCC ? getRecursiveInlineCost(CS) : |
| 78 | getInlineCost(CS); |
Chris Lattner | a51bcb5 | 2003-11-21 21:45:31 +0000 | [diff] [blame] | 79 | if (InlineCost >= (int)InlineThreshold) { |
| 80 | DEBUG(std::cerr << " NOT Inlining: cost=" << InlineCost |
| 81 | << ", Call: " << *CS.getInstruction()); |
| 82 | } else { |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 83 | DEBUG(std::cerr << " Inlining: cost=" << InlineCost |
| 84 | << ", Call: " << *CS.getInstruction()); |
| 85 | |
| 86 | // Save an iterator to the instruction before the call if it |
| 87 | // exists, otherwise get an iterator at the end of the |
| 88 | // block... because the call will be destroyed. |
| 89 | // |
| 90 | BasicBlock::iterator SI; |
| 91 | if (I != BB->begin()) { |
| 92 | SI = I; --SI; // Instruction before the call... |
| 93 | } else { |
| 94 | SI = BB->end(); |
| 95 | } |
| 96 | |
| 97 | if (performInlining(CS, SCCFunctions)) { |
| 98 | // Move to instruction before the call... |
| 99 | I = (SI == BB->end()) ? BB->begin() : SI; |
| 100 | ShouldInc = false; // Don't increment iterator until next time |
| 101 | Changed = true; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | if (ShouldInc) ++I; |
| 107 | } |
| 108 | } |
| 109 | return Changed; |
| 110 | } |
| 111 | |
| 112 | bool Inliner::performInlining(CallSite CS, std::set<Function*> &SCC) { |
| 113 | Function *Callee = CS.getCalledFunction(); |
| 114 | Function *Caller = CS.getInstruction()->getParent()->getParent(); |
| 115 | |
| 116 | // Attempt to inline the function... |
| 117 | if (!InlineFunction(CS)) return false; |
| 118 | ++NumInlined; |
Chris Lattner | bb9ae15 | 2003-10-31 21:05:58 +0000 | [diff] [blame] | 119 | |
| 120 | if (Callee->hasOneUse()) |
| 121 | if (ConstantPointerRef *CPR = |
| 122 | dyn_cast<ConstantPointerRef>(Callee->use_back())) |
| 123 | if (CPR->use_empty()) |
| 124 | CPR->destroyConstant(); |
| 125 | |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 126 | // If we inlined the last possible call site to the function, |
| 127 | // delete the function body now. |
| 128 | if (Callee->use_empty() && Callee != Caller && |
| 129 | (Callee->hasInternalLinkage() || Callee->hasLinkOnceLinkage())) { |
Chris Lattner | bb9ae15 | 2003-10-31 21:05:58 +0000 | [diff] [blame] | 130 | DEBUG(std::cerr << " -> Deleting dead function: " << (void*)Callee |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 131 | << Callee->getName() << "\n"); |
| 132 | std::set<Function*>::iterator I = SCC.find(Callee); |
| 133 | if (I != SCC.end()) // Remove function from this SCC... |
| 134 | SCC.erase(I); |
| 135 | |
| 136 | Callee->getParent()->getFunctionList().erase(Callee); |
| 137 | ++NumDeleted; |
| 138 | } |
| 139 | return true; |
| 140 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 141 | |