blob: e107a0023ce6ccad28bfefa11e465d164c094499 [file] [log] [blame]
Chris Lattner237ef562003-08-31 19:10:30 +00001//===- InlineSimple.cpp - Code to perform simple function inlining --------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattnerca398dc2003-05-29 15:11:31 +000010// This file implements bottom-up inlining of functions into callees.
Chris Lattner01545052002-04-18 18:52:03 +000011//
Chris Lattner00950542001-06-06 20:29:01 +000012//===----------------------------------------------------------------------===//
13
Tanya Lattner6f7426e2007-06-19 22:29:50 +000014#define DEBUG_TYPE "inline"
Chris Lattner49fbff42005-05-18 04:30:33 +000015#include "llvm/CallingConv.h"
Chris Lattner869adc22003-11-21 21:46:09 +000016#include "llvm/Instructions.h"
Chris Lattner8db93f12004-11-22 17:21:44 +000017#include "llvm/IntrinsicInst.h"
Tanya Lattner682f6832007-06-06 21:59:26 +000018#include "llvm/Module.h"
Chris Lattner619d3542004-03-13 23:15:45 +000019#include "llvm/Type.h"
Tanya Lattner682f6832007-06-06 21:59:26 +000020#include "llvm/Analysis/CallGraph.h"
Chris Lattnere5445332003-08-24 06:59:28 +000021#include "llvm/Support/CallSite.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000022#include "llvm/Support/Compiler.h"
Chris Lattner237ef562003-08-31 19:10:30 +000023#include "llvm/Transforms/IPO.h"
Tanya Lattner6f7426e2007-06-19 22:29:50 +000024#include "llvm/Transforms/IPO/InlinerPass.h"
Devang Patel6899b312007-07-25 18:00:25 +000025#include "llvm/Transforms/Utils/InlineCost.h"
Devang Patel29381fb2007-07-27 18:34:27 +000026#include "llvm/ADT/SmallPtrSet.h"
Tanya Lattner682f6832007-06-06 21:59:26 +000027
Chris Lattner869adc22003-11-21 21:46:09 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattnerbd0ef772002-02-26 21:46:54 +000030namespace {
Chris Lattner884d6c42003-10-06 15:52:43 +000031
Reid Spencer9133fe22007-02-05 23:32:05 +000032 class VISIBILITY_HIDDEN SimpleInliner : public Inliner {
Devang Patel29381fb2007-07-27 18:34:27 +000033 // Functions that are never inlined
34 SmallPtrSet<const Function*, 16> NeverInline;
Devang Patel6899b312007-07-25 18:00:25 +000035 InlineCostAnalyzer CA;
Chris Lattner884d6c42003-10-06 15:52:43 +000036 public:
Chris Lattner1ce6f8d2007-05-06 23:13:56 +000037 SimpleInliner() : Inliner(&ID) {}
Chris Lattner120d0532008-01-12 06:49:13 +000038 SimpleInliner(int Threshold) : Inliner(&ID, Threshold) {}
Nick Lewyckyecd94c82007-05-06 13:37:16 +000039 static char ID; // Pass identification, replacement for typeid
Daniel Dunbarc5e1ec42008-10-30 19:26:59 +000040 InlineCost getInlineCost(CallSite CS) {
Devang Patel6899b312007-07-25 18:00:25 +000041 return CA.getInlineCost(CS, NeverInline);
42 }
Evan Cheng8d84d5b2008-03-24 06:37:48 +000043 float getInlineFudgeFactor(CallSite CS) {
44 return CA.getInlineFudgeFactor(CS);
45 }
Dale Johannesene3455662009-01-09 01:30:11 +000046 void resetCachedCostInfo(Function *Caller) {
47 CA.resetCachedCostInfo(Caller);
48 }
Tanya Lattner682f6832007-06-06 21:59:26 +000049 virtual bool doInitialization(CallGraph &CG);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000050 };
51}
52
Dan Gohman844731a2008-05-13 00:00:25 +000053char SimpleInliner::ID = 0;
54static RegisterPass<SimpleInliner>
55X("inline", "Function Integration/Inlining");
56
Devang Patelc71ca3c2007-01-26 00:47:38 +000057Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
Chris Lattner869adc22003-11-21 21:46:09 +000058
Chris Lattner120d0532008-01-12 06:49:13 +000059Pass *llvm::createFunctionInliningPass(int Threshold) {
60 return new SimpleInliner(Threshold);
61}
62
Tanya Lattner682f6832007-06-06 21:59:26 +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 Patel910c1202008-09-03 18:10:21 +000069 for (Module::iterator I = M.begin(), E = M.end();
70 I != E; ++I)
Devang Patel2c9c3e72008-09-26 23:51:19 +000071 if (!I->isDeclaration() && I->hasFnAttr(Attribute::NoInline))
Devang Patel910c1202008-09-03 18:10:21 +000072 NeverInline.insert(I);
73
Tanya Lattner682f6832007-06-06 21:59:26 +000074 // Get llvm.noinline
75 GlobalVariable *GV = M.getNamedGlobal("llvm.noinline");
76
Tanya Lattnerb62fa8a2007-06-07 17:12:16 +000077 if (GV == 0)
Tanya Lattner682f6832007-06-06 21:59:26 +000078 return false;
79
Anton Korobeynikov311c4b62007-11-22 22:30:10 +000080 // Don't crash on invalid code
81 if (!GV->hasInitializer())
82 return false;
83
Tanya Lattner682f6832007-06-06 21:59:26 +000084 const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
85
Tanya Lattnerb62fa8a2007-06-07 17:12:16 +000086 if (InitList == 0)
Tanya Lattner682f6832007-06-06 21:59:26 +000087 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
Tanya Lattnerb62fa8a2007-06-07 17:12:16 +0000100 if (const Function *F = dyn_cast<Function>(Elt))
101 NeverInline.insert(F);
Tanya Lattner682f6832007-06-06 21:59:26 +0000102 }
103
104 return false;
105}
Devang Patel6899b312007-07-25 18:00:25 +0000106