LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PartialInlining.cpp
Go to the documentation of this file.
1 //===- PartialInlining.cpp - Inline parts of functions --------------------===//
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 pass performs partial inlining, typically by inlining an if statement
11 // that surrounds the body of the function.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #define DEBUG_TYPE "partialinlining"
16 #include "llvm/Transforms/IPO.h"
17 #include "llvm/ADT/Statistic.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Support/CFG.h"
25 using namespace llvm;
26 
27 STATISTIC(NumPartialInlined, "Number of functions partially inlined");
28 
29 namespace {
30  struct PartialInliner : public ModulePass {
31  virtual void getAnalysisUsage(AnalysisUsage &AU) const { }
32  static char ID; // Pass identification, replacement for typeid
33  PartialInliner() : ModulePass(ID) {
35  }
36 
37  bool runOnModule(Module& M);
38 
39  private:
40  Function* unswitchFunction(Function* F);
41  };
42 }
43 
44 char PartialInliner::ID = 0;
45 INITIALIZE_PASS(PartialInliner, "partial-inliner",
46  "Partial Inliner", false, false)
47 
48 ModulePass* llvm::createPartialInliningPass() { return new PartialInliner(); }
49 
50 Function* PartialInliner::unswitchFunction(Function* F) {
51  // First, verify that this function is an unswitching candidate...
52  BasicBlock* entryBlock = F->begin();
53  BranchInst *BR = dyn_cast<BranchInst>(entryBlock->getTerminator());
54  if (!BR || BR->isUnconditional())
55  return 0;
56 
57  BasicBlock* returnBlock = 0;
58  BasicBlock* nonReturnBlock = 0;
59  unsigned returnCount = 0;
60  for (succ_iterator SI = succ_begin(entryBlock), SE = succ_end(entryBlock);
61  SI != SE; ++SI)
62  if (isa<ReturnInst>((*SI)->getTerminator())) {
63  returnBlock = *SI;
64  returnCount++;
65  } else
66  nonReturnBlock = *SI;
67 
68  if (returnCount != 1)
69  return 0;
70 
71  // Clone the function, so that we can hack away on it.
72  ValueToValueMapTy VMap;
73  Function* duplicateFunction = CloneFunction(F, VMap,
74  /*ModuleLevelChanges=*/false);
75  duplicateFunction->setLinkage(GlobalValue::InternalLinkage);
76  F->getParent()->getFunctionList().push_back(duplicateFunction);
77  BasicBlock* newEntryBlock = cast<BasicBlock>(VMap[entryBlock]);
78  BasicBlock* newReturnBlock = cast<BasicBlock>(VMap[returnBlock]);
79  BasicBlock* newNonReturnBlock = cast<BasicBlock>(VMap[nonReturnBlock]);
80 
81  // Go ahead and update all uses to the duplicate, so that we can just
82  // use the inliner functionality when we're done hacking.
83  F->replaceAllUsesWith(duplicateFunction);
84 
85  // Special hackery is needed with PHI nodes that have inputs from more than
86  // one extracted block. For simplicity, just split the PHIs into a two-level
87  // sequence of PHIs, some of which will go in the extracted region, and some
88  // of which will go outside.
89  BasicBlock* preReturn = newReturnBlock;
90  newReturnBlock = newReturnBlock->splitBasicBlock(
91  newReturnBlock->getFirstNonPHI());
92  BasicBlock::iterator I = preReturn->begin();
93  BasicBlock::iterator Ins = newReturnBlock->begin();
94  while (I != preReturn->end()) {
95  PHINode* OldPhi = dyn_cast<PHINode>(I);
96  if (!OldPhi) break;
97 
98  PHINode* retPhi = PHINode::Create(OldPhi->getType(), 2, "", Ins);
99  OldPhi->replaceAllUsesWith(retPhi);
100  Ins = newReturnBlock->getFirstNonPHI();
101 
102  retPhi->addIncoming(I, preReturn);
103  retPhi->addIncoming(OldPhi->getIncomingValueForBlock(newEntryBlock),
104  newEntryBlock);
105  OldPhi->removeIncomingValue(newEntryBlock);
106 
107  ++I;
108  }
109  newEntryBlock->getTerminator()->replaceUsesOfWith(preReturn, newReturnBlock);
110 
111  // Gather up the blocks that we're going to extract.
112  std::vector<BasicBlock*> toExtract;
113  toExtract.push_back(newNonReturnBlock);
114  for (Function::iterator FI = duplicateFunction->begin(),
115  FE = duplicateFunction->end(); FI != FE; ++FI)
116  if (&*FI != newEntryBlock && &*FI != newReturnBlock &&
117  &*FI != newNonReturnBlock)
118  toExtract.push_back(FI);
119 
120  // The CodeExtractor needs a dominator tree.
121  DominatorTree DT;
122  DT.runOnFunction(*duplicateFunction);
123 
124  // Extract the body of the if.
125  Function* extractedFunction
126  = CodeExtractor(toExtract, &DT).extractCodeRegion();
127 
128  InlineFunctionInfo IFI;
129 
130  // Inline the top-level if test into all callers.
131  std::vector<User*> Users(duplicateFunction->use_begin(),
132  duplicateFunction->use_end());
133  for (std::vector<User*>::iterator UI = Users.begin(), UE = Users.end();
134  UI != UE; ++UI)
135  if (CallInst *CI = dyn_cast<CallInst>(*UI))
136  InlineFunction(CI, IFI);
137  else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI))
138  InlineFunction(II, IFI);
139 
140  // Ditch the duplicate, since we're done with it, and rewrite all remaining
141  // users (function pointers, etc.) back to the original function.
142  duplicateFunction->replaceAllUsesWith(F);
143  duplicateFunction->eraseFromParent();
144 
145  ++NumPartialInlined;
146 
147  return extractedFunction;
148 }
149 
150 bool PartialInliner::runOnModule(Module& M) {
151  std::vector<Function*> worklist;
152  worklist.reserve(M.size());
153  for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
154  if (!FI->use_empty() && !FI->isDeclaration())
155  worklist.push_back(&*FI);
156 
157  bool changed = false;
158  while (!worklist.empty()) {
159  Function* currFunc = worklist.back();
160  worklist.pop_back();
161 
162  if (currFunc->use_empty()) continue;
163 
164  bool recursive = false;
165  for (Function::use_iterator UI = currFunc->use_begin(),
166  UE = currFunc->use_end(); UI != UE; ++UI)
167  if (Instruction* I = dyn_cast<Instruction>(*UI))
168  if (I->getParent()->getParent() == currFunc) {
169  recursive = true;
170  break;
171  }
172  if (recursive) continue;
173 
174 
175  if (Function* newFunc = unswitchFunction(currFunc)) {
176  worklist.push_back(newFunc);
177  changed = true;
178  }
179 
180  }
181 
182  return changed;
183 }
use_iterator use_end()
Definition: Value.h:152
Utility class for extracting code into a new function.
Definition: CodeExtractor.h:44
void addIncoming(Value *V, BasicBlock *BB)
static PassRegistry * getPassRegistry()
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
bool InlineFunction(CallInst *C, InlineFunctionInfo &IFI, bool InsertLifetime=true)
iterator end()
Definition: Function.h:397
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:266
Function * extractCodeRegion()
Perform the extraction, returning the new function.
F(f)
iv Induction Variable Users
Definition: IVUsers.cpp:39
iterator begin()
Definition: BasicBlock.h:193
bool isUnconditional() const
void push_back(NodeTy *val)
Definition: ilist.h:554
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
Interval::succ_iterator succ_begin(Interval *I)
Definition: Interval.h:107
STATISTIC(NumPartialInlined,"Number of functions partially inlined")
const BasicBlock & back() const
Definition: Function.h:404
void replaceAllUsesWith(Value *V)
Definition: Value.cpp:303
iterator begin()
Definition: Function.h:395
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:110
void replaceUsesOfWith(Value *From, Value *To)
Definition: User.cpp:26
Control flow instructions. These all have token chains.
Definition: ISDOpcodes.h:475
value_use_iterator< User > use_iterator
Definition: Value.h:146
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=0)
virtual bool runOnFunction(Function &F)
const FunctionListType & getFunctionList() const
Get the Module's list of functions (constant).
Definition: Module.h:492
See the file comment.
Definition: ValueMap.h:75
iterator end()
Definition: BasicBlock.h:195
void initializePartialInlinerPass(PassRegistry &)
Type * getType() const
Definition: Value.h:111
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:217
Value * getIncomingValueForBlock(const BasicBlock *BB) const
size_t size() const
Definition: Module.h:535
use_iterator use_begin()
Definition: Value.h:150
iterator end()
Definition: Module.h:533
INITIALIZE_PASS(PartialInliner,"partial-inliner","Partial Inliner", false, false) ModulePass *llvm
#define I(x, y, z)
Definition: MD5.cpp:54
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:120
Function * CloneFunction(const Function *F, ValueToValueMapTy &VMap, bool ModuleLevelChanges, ClonedCodeInfo *CodeInfo=0)
iterator begin()
Definition: Module.h:531
Rename collisions when linking (static functions).
Definition: GlobalValue.h:41
virtual void eraseFromParent()
Definition: Function.cpp:187
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
Definition: BasicBlock.cpp:298
bool use_empty() const
Definition: Value.h:149
ModulePass * createPartialInliningPass()
Module * getParent()
Definition: GlobalValue.h:286