blob: d72edf84805ee0707f441ad504f95314823c7cc0 [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) {}
Nick Lewyckyecd94c82007-05-06 13:37:16 +000038 static char ID; // Pass identification, replacement for typeid
Devang Patel6899b312007-07-25 18:00:25 +000039 int getInlineCost(CallSite CS) {
40 return CA.getInlineCost(CS, NeverInline);
41 }
Tanya Lattner682f6832007-06-06 21:59:26 +000042 virtual bool doInitialization(CallGraph &CG);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000043 };
Devang Patel19974732007-05-03 01:11:54 +000044 char SimpleInliner::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000045 RegisterPass<SimpleInliner> X("inline", "Function Integration/Inlining");
Chris Lattnerbd0ef772002-02-26 21:46:54 +000046}
47
Devang Patelc71ca3c2007-01-26 00:47:38 +000048Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
Chris Lattner869adc22003-11-21 21:46:09 +000049
Tanya Lattner682f6832007-06-06 21:59:26 +000050// doInitialization - Initializes the vector of functions that have been
51// annotated with the noinline attribute.
52bool SimpleInliner::doInitialization(CallGraph &CG) {
53
54 Module &M = CG.getModule();
55
56 // Get llvm.noinline
57 GlobalVariable *GV = M.getNamedGlobal("llvm.noinline");
58
Tanya Lattnerb62fa8a2007-06-07 17:12:16 +000059 if (GV == 0)
Tanya Lattner682f6832007-06-06 21:59:26 +000060 return false;
61
Anton Korobeynikov311c4b62007-11-22 22:30:10 +000062 // Don't crash on invalid code
63 if (!GV->hasInitializer())
64 return false;
65
Tanya Lattner682f6832007-06-06 21:59:26 +000066 const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
67
Tanya Lattnerb62fa8a2007-06-07 17:12:16 +000068 if (InitList == 0)
Tanya Lattner682f6832007-06-06 21:59:26 +000069 return false;
70
71 // Iterate over each element and add to the NeverInline set
72 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
73
74 // Get Source
75 const Constant *Elt = InitList->getOperand(i);
76
77 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Elt))
78 if (CE->getOpcode() == Instruction::BitCast)
79 Elt = CE->getOperand(0);
80
81 // Insert into set of functions to never inline
Tanya Lattnerb62fa8a2007-06-07 17:12:16 +000082 if (const Function *F = dyn_cast<Function>(Elt))
83 NeverInline.insert(F);
Tanya Lattner682f6832007-06-06 21:59:26 +000084 }
85
86 return false;
87}
Devang Patel6899b312007-07-25 18:00:25 +000088