blob: eea28a745c57902a637e9fe7b92c20312da8222f [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//
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//===----------------------------------------------------------------------===//
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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include <set>
27
28using namespace llvm;
29
30namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031
32 class VISIBILITY_HIDDEN SimpleInliner : public Inliner {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033 std::set<const Function*> NeverInline; // Functions that are never inlined
Devang Patel7c2e99c2007-07-25 18:00:25 +000034 InlineCostAnalyzer CA;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035 public:
36 SimpleInliner() : Inliner(&ID) {}
37 static char ID; // Pass identification, replacement for typeid
Devang Patel7c2e99c2007-07-25 18:00:25 +000038 int getInlineCost(CallSite CS) {
39 return CA.getInlineCost(CS, NeverInline);
40 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 virtual bool doInitialization(CallGraph &CG);
42 };
43 char SimpleInliner::ID = 0;
44 RegisterPass<SimpleInliner> X("inline", "Function Integration/Inlining");
45}
46
47Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
48
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049// doInitialization - Initializes the vector of functions that have been
50// annotated with the noinline attribute.
51bool SimpleInliner::doInitialization(CallGraph &CG) {
52
53 Module &M = CG.getModule();
54
55 // Get llvm.noinline
56 GlobalVariable *GV = M.getNamedGlobal("llvm.noinline");
57
58 if (GV == 0)
59 return false;
60
61 const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
62
63 if (InitList == 0)
64 return false;
65
66 // Iterate over each element and add to the NeverInline set
67 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
68
69 // Get Source
70 const Constant *Elt = InitList->getOperand(i);
71
72 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Elt))
73 if (CE->getOpcode() == Instruction::BitCast)
74 Elt = CE->getOperand(0);
75
76 // Insert into set of functions to never inline
77 if (const Function *F = dyn_cast<Function>(Elt))
78 NeverInline.insert(F);
79 }
80
81 return false;
82}
Devang Patel7c2e99c2007-07-25 18:00:25 +000083