LLVM  3.7.0
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 #include "llvm/Transforms/IPO.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/IR/CFG.h"
18 #include "llvm/IR/Dominators.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/Pass.h"
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "partialinlining"
27 
28 STATISTIC(NumPartialInlined, "Number of functions partially inlined");
29 
30 namespace {
31  struct PartialInliner : public ModulePass {
32  void getAnalysisUsage(AnalysisUsage &AU) const override { }
33  static char ID; // Pass identification, replacement for typeid
34  PartialInliner() : ModulePass(ID) {
36  }
37 
38  bool runOnModule(Module& M) override;
39 
40  private:
41  Function* unswitchFunction(Function* F);
42  };
43 }
44 
45 char PartialInliner::ID = 0;
46 INITIALIZE_PASS(PartialInliner, "partial-inliner",
47  "Partial Inliner", false, false)
48 
49 ModulePass* llvm::createPartialInliningPass() { return new PartialInliner(); }
50 
51 Function* PartialInliner::unswitchFunction(Function* F) {
52  // First, verify that this function is an unswitching candidate...
53  BasicBlock* entryBlock = F->begin();
54  BranchInst *BR = dyn_cast<BranchInst>(entryBlock->getTerminator());
55  if (!BR || BR->isUnconditional())
56  return nullptr;
57 
58  BasicBlock* returnBlock = nullptr;
59  BasicBlock* nonReturnBlock = nullptr;
60  unsigned returnCount = 0;
61  for (BasicBlock *BB : successors(entryBlock)) {
62  if (isa<ReturnInst>(BB->getTerminator())) {
63  returnBlock = BB;
64  returnCount++;
65  } else
66  nonReturnBlock = BB;
67  }
68 
69  if (returnCount != 1)
70  return nullptr;
71 
72  // Clone the function, so that we can hack away on it.
73  ValueToValueMapTy VMap;
74  Function* duplicateFunction = CloneFunction(F, VMap,
75  /*ModuleLevelChanges=*/false);
76  duplicateFunction->setLinkage(GlobalValue::InternalLinkage);
77  F->getParent()->getFunctionList().push_back(duplicateFunction);
78  BasicBlock* newEntryBlock = cast<BasicBlock>(VMap[entryBlock]);
79  BasicBlock* newReturnBlock = cast<BasicBlock>(VMap[returnBlock]);
80  BasicBlock* newNonReturnBlock = cast<BasicBlock>(VMap[nonReturnBlock]);
81 
82  // Go ahead and update all uses to the duplicate, so that we can just
83  // use the inliner functionality when we're done hacking.
84  F->replaceAllUsesWith(duplicateFunction);
85 
86  // Special hackery is needed with PHI nodes that have inputs from more than
87  // one extracted block. For simplicity, just split the PHIs into a two-level
88  // sequence of PHIs, some of which will go in the extracted region, and some
89  // of which will go outside.
90  BasicBlock* preReturn = newReturnBlock;
91  newReturnBlock = newReturnBlock->splitBasicBlock(
92  newReturnBlock->getFirstNonPHI());
93  BasicBlock::iterator I = preReturn->begin();
94  BasicBlock::iterator Ins = newReturnBlock->begin();
95  while (I != preReturn->end()) {
96  PHINode* OldPhi = dyn_cast<PHINode>(I);
97  if (!OldPhi) break;
98 
99  PHINode* retPhi = PHINode::Create(OldPhi->getType(), 2, "", Ins);
100  OldPhi->replaceAllUsesWith(retPhi);
101  Ins = newReturnBlock->getFirstNonPHI();
102 
103  retPhi->addIncoming(I, preReturn);
104  retPhi->addIncoming(OldPhi->getIncomingValueForBlock(newEntryBlock),
105  newEntryBlock);
106  OldPhi->removeIncomingValue(newEntryBlock);
107 
108  ++I;
109  }
110  newEntryBlock->getTerminator()->replaceUsesOfWith(preReturn, newReturnBlock);
111 
112  // Gather up the blocks that we're going to extract.
113  std::vector<BasicBlock*> toExtract;
114  toExtract.push_back(newNonReturnBlock);
115  for (Function::iterator FI = duplicateFunction->begin(),
116  FE = duplicateFunction->end(); FI != FE; ++FI)
117  if (&*FI != newEntryBlock && &*FI != newReturnBlock &&
118  &*FI != newNonReturnBlock)
119  toExtract.push_back(FI);
120 
121  // The CodeExtractor needs a dominator tree.
122  DominatorTree DT;
123  DT.recalculate(*duplicateFunction);
124 
125  // Extract the body of the if.
126  Function* extractedFunction
127  = CodeExtractor(toExtract, &DT).extractCodeRegion();
128 
129  InlineFunctionInfo IFI;
130 
131  // Inline the top-level if test into all callers.
132  std::vector<User *> Users(duplicateFunction->user_begin(),
133  duplicateFunction->user_end());
134  for (std::vector<User*>::iterator UI = Users.begin(), UE = Users.end();
135  UI != UE; ++UI)
136  if (CallInst *CI = dyn_cast<CallInst>(*UI))
137  InlineFunction(CI, IFI);
138  else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI))
139  InlineFunction(II, IFI);
140 
141  // Ditch the duplicate, since we're done with it, and rewrite all remaining
142  // users (function pointers, etc.) back to the original function.
143  duplicateFunction->replaceAllUsesWith(F);
144  duplicateFunction->eraseFromParent();
145 
146  ++NumPartialInlined;
147 
148  return extractedFunction;
149 }
150 
151 bool PartialInliner::runOnModule(Module& M) {
152  std::vector<Function*> worklist;
153  worklist.reserve(M.size());
154  for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
155  if (!FI->use_empty() && !FI->isDeclaration())
156  worklist.push_back(&*FI);
157 
158  bool changed = false;
159  while (!worklist.empty()) {
160  Function* currFunc = worklist.back();
161  worklist.pop_back();
162 
163  if (currFunc->use_empty()) continue;
164 
165  bool recursive = false;
166  for (User *U : currFunc->users())
167  if (Instruction* I = dyn_cast<Instruction>(U))
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 }
Utility class for extracting code into a new function.
Definition: CodeExtractor.h:44
void addIncoming(Value *V, BasicBlock *BB)
addIncoming - Add an incoming value to the end of the PHI list
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
bool InlineFunction(CallInst *C, InlineFunctionInfo &IFI, bool InsertLifetime=true)
InlineFunction - This function inlines the called function into the basic block of the caller...
iterator end()
Definition: Function.h:459
CallInst - This class represents a function call, abstracting a target machine's calling convention...
Function * extractCodeRegion()
Perform the extraction, returning the new function.
F(f)
iv Induction Variable Users
Definition: IVUsers.cpp:43
InlineFunctionInfo - This class captures the data input to the InlineFunction call, and records the auxiliary results produced by it.
Definition: Cloning.h:193
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:231
bool isUnconditional() const
void push_back(NodeTy *val)
Definition: ilist.h:554
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
removeIncomingValue - Remove an incoming value.
const BasicBlock & back() const
Definition: Function.h:466
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:351
iterator begin()
Definition: Function.h:457
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:67
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition: User.cpp:24
Control flow instructions. These all have token chains.
Definition: ISDOpcodes.h:533
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
BranchInst - Conditional or Unconditional Branch instruction.
Represent the analysis usage information of a pass.
const FunctionListType & getFunctionList() const
Get the Module's list of functions (constant).
Definition: Module.h:519
iterator end()
Definition: BasicBlock.h:233
void initializePartialInlinerPass(PassRegistry &)
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:284
Value * getIncomingValueForBlock(const BasicBlock *BB) const
iterator_range< user_iterator > users()
Definition: Value.h:300
size_t size() const
Definition: Module.h:577
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:285
void eraseFromParent() override
eraseFromParent - This method unlinks 'this' from the containing module and deletes it...
Definition: Function.cpp:241
iterator end()
Definition: Module.h:571
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:124
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:236
iterator begin()
Definition: Module.h:569
Rename collisions when linking (static functions).
Definition: GlobalValue.h:47
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
Definition: BasicBlock.cpp:348
bool use_empty() const
Definition: Value.h:275
user_iterator user_begin()
Definition: Value.h:294
ModulePass * createPartialInliningPass()
createPartialInliningPass - This pass inlines parts of functions.
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
succ_range successors(BasicBlock *BB)
Definition: IR/CFG.h:271
Function * CloneFunction(const Function *F, ValueToValueMapTy &VMap, bool ModuleLevelChanges, ClonedCodeInfo *CodeInfo=nullptr)
CloneFunction - Return a copy of the specified function, but without embedding the function into anot...
InvokeInst - Invoke instruction.
void recalculate(FT &F)
recalculate - compute a dominator tree for the given function
user_iterator user_end()
Definition: Value.h:296