blob: 38bf0610b3872e84c2b5792cef96db1b77facc24 [file] [log] [blame]
Chris Lattnerd075cc22003-08-31 19:10:30 +00001//===- InlineSimple.cpp - Code to perform simple function inlining --------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// 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.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner530d4bf2003-05-29 15:11:31 +000010// This file implements bottom-up inlining of functions into callees.
Chris Lattner1c8d3f82002-04-18 18:52:03 +000011//
Chris Lattner2f7c9632001-06-06 20:29:01 +000012//===----------------------------------------------------------------------===//
13
Tanya Lattnerab11b1c2007-06-19 22:29:50 +000014#define DEBUG_TYPE "inline"
Chris Lattner05deb042005-05-18 04:30:33 +000015#include "llvm/CallingConv.h"
Chris Lattner51c28a52003-11-21 21:46:09 +000016#include "llvm/Instructions.h"
Chris Lattner79e87e32004-11-22 17:21:44 +000017#include "llvm/IntrinsicInst.h"
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000018#include "llvm/Module.h"
Chris Lattner2dc85b22004-03-13 23:15:45 +000019#include "llvm/Type.h"
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000020#include "llvm/Analysis/CallGraph.h"
Chris Lattnerd367d052003-08-24 06:59:28 +000021#include "llvm/Support/CallSite.h"
Reid Spencer557ab152007-02-05 23:32:05 +000022#include "llvm/Support/Compiler.h"
Chris Lattnerd075cc22003-08-31 19:10:30 +000023#include "llvm/Transforms/IPO.h"
Tanya Lattnerab11b1c2007-06-19 22:29:50 +000024#include "llvm/Transforms/IPO/InlinerPass.h"
Devang Patel33227112007-07-25 18:00:25 +000025#include "llvm/Transforms/Utils/InlineCost.h"
Devang Patele3206cb2007-07-27 18:34:27 +000026#include "llvm/ADT/SmallPtrSet.h"
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000027
Chris Lattner51c28a52003-11-21 21:46:09 +000028using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000029
Chris Lattner04805fa2002-02-26 21:46:54 +000030namespace {
Chris Lattner6dc0ae22003-10-06 15:52:43 +000031
Reid Spencer557ab152007-02-05 23:32:05 +000032 class VISIBILITY_HIDDEN SimpleInliner : public Inliner {
Devang Patele3206cb2007-07-27 18:34:27 +000033 // Functions that are never inlined
34 SmallPtrSet<const Function*, 16> NeverInline;
Devang Patel33227112007-07-25 18:00:25 +000035 InlineCostAnalyzer CA;
Chris Lattner6dc0ae22003-10-06 15:52:43 +000036 public:
Chris Lattner3b6f75c2007-05-06 23:13:56 +000037 SimpleInliner() : Inliner(&ID) {}
Nick Lewyckye7da2d62007-05-06 13:37:16 +000038 static char ID; // Pass identification, replacement for typeid
Devang Patel33227112007-07-25 18:00:25 +000039 int getInlineCost(CallSite CS) {
40 return CA.getInlineCost(CS, NeverInline);
41 }
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000042 virtual bool doInitialization(CallGraph &CG);
Chris Lattner04805fa2002-02-26 21:46:54 +000043 };
Devang Patel8c78a0b2007-05-03 01:11:54 +000044 char SimpleInliner::ID = 0;
Chris Lattnerc2d3d312006-08-27 22:42:52 +000045 RegisterPass<SimpleInliner> X("inline", "Function Integration/Inlining");
Chris Lattner04805fa2002-02-26 21:46:54 +000046}
47
Devang Patel13058a52007-01-26 00:47:38 +000048Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
Chris Lattner51c28a52003-11-21 21:46:09 +000049
Tanya Lattnercb90f1d2007-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 Lattner5801c232007-06-07 17:12:16 +000059 if (GV == 0)
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000060 return false;
61
62 const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
63
Tanya Lattner5801c232007-06-07 17:12:16 +000064 if (InitList == 0)
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000065 return false;
66
67 // Iterate over each element and add to the NeverInline set
68 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
69
70 // Get Source
71 const Constant *Elt = InitList->getOperand(i);
72
73 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Elt))
74 if (CE->getOpcode() == Instruction::BitCast)
75 Elt = CE->getOperand(0);
76
77 // Insert into set of functions to never inline
Tanya Lattner5801c232007-06-07 17:12:16 +000078 if (const Function *F = dyn_cast<Function>(Elt))
79 NeverInline.insert(F);
Tanya Lattnercb90f1d2007-06-06 21:59:26 +000080 }
81
82 return false;
83}
Devang Patel33227112007-07-25 18:00:25 +000084