1 //===- InlineSimple.cpp - Code to perform simple function inlining --------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements bottom-up inlining of functions into callees.
12 //===----------------------------------------------------------------------===//
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"
25 #include "llvm/Transforms/Utils/InlineCost.h"
26 #include "llvm/ADT/SmallPtrSet.h"
32 class VISIBILITY_HIDDEN SimpleInliner : public Inliner {
33 // Functions that are never inlined
34 SmallPtrSet<const Function*, 16> NeverInline;
35 InlineCostAnalyzer CA;
37 SimpleInliner() : Inliner(&ID) {}
38 static char ID; // Pass identification, replacement for typeid
39 int getInlineCost(CallSite CS) {
40 return CA.getInlineCost(CS, NeverInline);
42 virtual bool doInitialization(CallGraph &CG);
44 char SimpleInliner::ID = 0;
45 RegisterPass<SimpleInliner> X("inline", "Function Integration/Inlining");
48 Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
50 // doInitialization - Initializes the vector of functions that have been
51 // annotated with the noinline attribute.
52 bool SimpleInliner::doInitialization(CallGraph &CG) {
54 Module &M = CG.getModule();
57 GlobalVariable *GV = M.getNamedGlobal("llvm.noinline");
62 const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
67 // Iterate over each element and add to the NeverInline set
68 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
71 const Constant *Elt = InitList->getOperand(i);
73 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Elt))
74 if (CE->getOpcode() == Instruction::BitCast)
75 Elt = CE->getOperand(0);
77 // Insert into set of functions to never inline
78 if (const Function *F = dyn_cast<Function>(Elt))
79 NeverInline.insert(F);