blob: 598043de694a69046b905010054cb2179443403f [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- InlineSimple.cpp - Code to perform simple function inlining --------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements bottom-up inlining of functions into callees.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "inline"
15#include "llvm/CallingConv.h"
16#include "llvm/Instructions.h"
17#include "llvm/IntrinsicInst.h"
18#include "llvm/Module.h"
19#include "llvm/Type.h"
20#include "llvm/Analysis/CallGraph.h"
Dan Gohmanfad07182009-10-13 18:30:07 +000021#include "llvm/Analysis/InlineCost.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Support/CallSite.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/Transforms/IPO.h"
24#include "llvm/Transforms/IPO/InlinerPass.h"
Devang Pateld6605a82007-07-27 18:34:27 +000025#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026
27using namespace llvm;
28
29namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030
Nick Lewycky492d06e2009-10-25 06:33:48 +000031 class SimpleInliner : public Inliner {
Devang Pateld6605a82007-07-27 18:34:27 +000032 // Functions that are never inlined
33 SmallPtrSet<const Function*, 16> NeverInline;
Devang Patel7c2e99c2007-07-25 18:00:25 +000034 InlineCostAnalyzer CA;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035 public:
36 SimpleInliner() : Inliner(&ID) {}
Chris Lattner758296d2008-01-12 06:49:13 +000037 SimpleInliner(int Threshold) : Inliner(&ID, Threshold) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038 static char ID; // Pass identification, replacement for typeid
Daniel Dunbarde4982c2008-10-30 19:26:59 +000039 InlineCost getInlineCost(CallSite CS) {
Devang Patel7c2e99c2007-07-25 18:00:25 +000040 return CA.getInlineCost(CS, NeverInline);
41 }
Evan Cheng9880e572008-03-24 06:37:48 +000042 float getInlineFudgeFactor(CallSite CS) {
43 return CA.getInlineFudgeFactor(CS);
44 }
Dale Johannesenec46e482009-01-09 01:30:11 +000045 void resetCachedCostInfo(Function *Caller) {
46 CA.resetCachedCostInfo(Caller);
47 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 virtual bool doInitialization(CallGraph &CG);
49 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050}
51
Dan Gohman089efff2008-05-13 00:00:25 +000052char SimpleInliner::ID = 0;
53static RegisterPass<SimpleInliner>
54X("inline", "Function Integration/Inlining");
55
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
57
Chris Lattner758296d2008-01-12 06:49:13 +000058Pass *llvm::createFunctionInliningPass(int Threshold) {
59 return new SimpleInliner(Threshold);
60}
61
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062// doInitialization - Initializes the vector of functions that have been
63// annotated with the noinline attribute.
64bool SimpleInliner::doInitialization(CallGraph &CG) {
65
66 Module &M = CG.getModule();
67
Devang Patel41b03392008-09-03 18:10:21 +000068 for (Module::iterator I = M.begin(), E = M.end();
69 I != E; ++I)
Devang Patel008cd3e2008-09-26 23:51:19 +000070 if (!I->isDeclaration() && I->hasFnAttr(Attribute::NoInline))
Devang Patel41b03392008-09-03 18:10:21 +000071 NeverInline.insert(I);
72
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 // Get llvm.noinline
74 GlobalVariable *GV = M.getNamedGlobal("llvm.noinline");
75
76 if (GV == 0)
77 return false;
78
Anton Korobeynikov5286e132007-11-22 22:30:10 +000079 // Don't crash on invalid code
Dan Gohman5e423ed2009-08-19 18:20:44 +000080 if (!GV->hasDefinitiveInitializer())
Anton Korobeynikov5286e132007-11-22 22:30:10 +000081 return false;
82
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
84
85 if (InitList == 0)
86 return false;
87
88 // Iterate over each element and add to the NeverInline set
89 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
90
91 // Get Source
92 const Constant *Elt = InitList->getOperand(i);
93
94 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Elt))
95 if (CE->getOpcode() == Instruction::BitCast)
96 Elt = CE->getOperand(0);
97
98 // Insert into set of functions to never inline
99 if (const Function *F = dyn_cast<Function>(Elt))
100 NeverInline.insert(F);
101 }
102
103 return false;
104}
Devang Patel7c2e99c2007-07-25 18:00:25 +0000105