Add custom inliner that handles only functions that are marked as always_inline.
[oota-llvm.git] / lib / Transforms / IPO / InlineAlways.cpp
1 //===- InlineAlways.cpp - Code to perform simple function inlining --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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"
25 #include "llvm/Transforms/Utils/InlineCost.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27
28 using namespace llvm;
29
30 namespace {
31
32   // AlwaysInliner only inlines functions that are mark as "always inline".
33   class VISIBILITY_HIDDEN AlwaysInliner : public Inliner {
34     // Functions that are never inlined
35     SmallPtrSet<const Function*, 16> NeverInline; 
36     InlineCostAnalyzer CA;
37   public:
38     // Use extremely low threshold. 
39     AlwaysInliner() : Inliner(&ID, -2000000000) {}
40     static char ID; // Pass identification, replacement for typeid
41     int getInlineCost(CallSite CS) {
42       return CA.getInlineCost(CS, NeverInline);
43     }
44     float getInlineFudgeFactor(CallSite CS) {
45       return CA.getInlineFudgeFactor(CS);
46     }
47     virtual bool doInitialization(CallGraph &CG);
48   };
49 }
50
51 char AlwaysInliner::ID = 0;
52 static RegisterPass<AlwaysInliner>
53 X("always-inline", "Inliner that handles always_inline functions");
54
55 Pass *llvm::createAlwaysInlinerPass() { return new AlwaysInliner(); }
56
57 // doInitialization - Initializes the vector of functions that have not 
58 // been annotated with the "always inline" attribute.
59 bool AlwaysInliner::doInitialization(CallGraph &CG) {
60   
61   Module &M = CG.getModule();
62   
63   for (Module::iterator I = M.begin(), E = M.end();
64        I != E; ++I)
65     if (!I->isDeclaration() && I->getNotes() != FN_NOTE_AlwaysInline)
66       NeverInline.insert(I);
67
68   return false;
69 }
70