blob: b1c643b558c57e5d6c5ce51d13e66b50facd5783 [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"
23#include "llvm/Support/Compiler.h"
24#include "llvm/Transforms/IPO.h"
25#include "llvm/Transforms/IPO/InlinerPass.h"
Devang Pateld6605a82007-07-27 18:34:27 +000026#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027
28using namespace llvm;
29
30namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031
32 class VISIBILITY_HIDDEN SimpleInliner : public Inliner {
Devang Pateld6605a82007-07-27 18:34:27 +000033 // Functions that are never inlined
34 SmallPtrSet<const Function*, 16> NeverInline;
Devang Patel7c2e99c2007-07-25 18:00:25 +000035 InlineCostAnalyzer CA;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036 public:
37 SimpleInliner() : Inliner(&ID) {}
Chris Lattner758296d2008-01-12 06:49:13 +000038 SimpleInliner(int Threshold) : Inliner(&ID, Threshold) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039 static char ID; // Pass identification, replacement for typeid
Daniel Dunbarde4982c2008-10-30 19:26:59 +000040 InlineCost getInlineCost(CallSite CS) {
Devang Patel7c2e99c2007-07-25 18:00:25 +000041 return CA.getInlineCost(CS, NeverInline);
42 }
Evan Cheng9880e572008-03-24 06:37:48 +000043 float getInlineFudgeFactor(CallSite CS) {
44 return CA.getInlineFudgeFactor(CS);
45 }
Dale Johannesenec46e482009-01-09 01:30:11 +000046 void resetCachedCostInfo(Function *Caller) {
47 CA.resetCachedCostInfo(Caller);
48 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 virtual bool doInitialization(CallGraph &CG);
50 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051}
52
Dan Gohman089efff2008-05-13 00:00:25 +000053char SimpleInliner::ID = 0;
54static RegisterPass<SimpleInliner>
55X("inline", "Function Integration/Inlining");
56
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
58
Chris Lattner758296d2008-01-12 06:49:13 +000059Pass *llvm::createFunctionInliningPass(int Threshold) {
60 return new SimpleInliner(Threshold);
61}
62
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063// doInitialization - Initializes the vector of functions that have been
64// annotated with the noinline attribute.
65bool SimpleInliner::doInitialization(CallGraph &CG) {
66
67 Module &M = CG.getModule();
68
Devang Patel41b03392008-09-03 18:10:21 +000069 for (Module::iterator I = M.begin(), E = M.end();
70 I != E; ++I)
Devang Patel008cd3e2008-09-26 23:51:19 +000071 if (!I->isDeclaration() && I->hasFnAttr(Attribute::NoInline))
Devang Patel41b03392008-09-03 18:10:21 +000072 NeverInline.insert(I);
73
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 // Get llvm.noinline
75 GlobalVariable *GV = M.getNamedGlobal("llvm.noinline");
76
77 if (GV == 0)
78 return false;
79
Anton Korobeynikov5286e132007-11-22 22:30:10 +000080 // Don't crash on invalid code
Dan Gohman5e423ed2009-08-19 18:20:44 +000081 if (!GV->hasDefinitiveInitializer())
Anton Korobeynikov5286e132007-11-22 22:30:10 +000082 return false;
83
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
85
86 if (InitList == 0)
87 return false;
88
89 // Iterate over each element and add to the NeverInline set
90 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
91
92 // Get Source
93 const Constant *Elt = InitList->getOperand(i);
94
95 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Elt))
96 if (CE->getOpcode() == Instruction::BitCast)
97 Elt = CE->getOperand(0);
98
99 // Insert into set of functions to never inline
100 if (const Function *F = dyn_cast<Function>(Elt))
101 NeverInline.insert(F);
102 }
103
104 return false;
105}
Devang Patel7c2e99c2007-07-25 18:00:25 +0000106