blob: a007103a59902dfea745795b91e02cce4f9e1b10 [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"
21#include "llvm/Support/CallSite.h"
22#include "llvm/Support/Compiler.h"
23#include "llvm/Transforms/IPO.h"
24#include "llvm/Transforms/IPO/InlinerPass.h"
Devang Patel7c2e99c2007-07-25 18:00:25 +000025#include "llvm/Transforms/Utils/InlineCost.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
Devang Patel7c2e99c2007-07-25 18:00:25 +000040 int getInlineCost(CallSite CS) {
41 return CA.getInlineCost(CS, NeverInline);
42 }
Evan Cheng9880e572008-03-24 06:37:48 +000043 float getInlineFudgeFactor(CallSite CS) {
44 return CA.getInlineFudgeFactor(CS);
45 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 virtual bool doInitialization(CallGraph &CG);
47 };
48 char SimpleInliner::ID = 0;
49 RegisterPass<SimpleInliner> X("inline", "Function Integration/Inlining");
50}
51
52Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
53
Chris Lattner758296d2008-01-12 06:49:13 +000054Pass *llvm::createFunctionInliningPass(int Threshold) {
55 return new SimpleInliner(Threshold);
56}
57
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058// doInitialization - Initializes the vector of functions that have been
59// annotated with the noinline attribute.
60bool SimpleInliner::doInitialization(CallGraph &CG) {
61
62 Module &M = CG.getModule();
63
64 // Get llvm.noinline
65 GlobalVariable *GV = M.getNamedGlobal("llvm.noinline");
66
67 if (GV == 0)
68 return false;
69
Anton Korobeynikov5286e132007-11-22 22:30:10 +000070 // Don't crash on invalid code
71 if (!GV->hasInitializer())
72 return false;
73
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
75
76 if (InitList == 0)
77 return false;
78
79 // Iterate over each element and add to the NeverInline set
80 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
81
82 // Get Source
83 const Constant *Elt = InitList->getOperand(i);
84
85 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Elt))
86 if (CE->getOpcode() == Instruction::BitCast)
87 Elt = CE->getOperand(0);
88
89 // Insert into set of functions to never inline
90 if (const Function *F = dyn_cast<Function>(Elt))
91 NeverInline.insert(F);
92 }
93
94 return false;
95}
Devang Patel7c2e99c2007-07-25 18:00:25 +000096