PartialInlining.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include "llvm/Transforms/IPO.h"
  15. #include "llvm/ADT/Statistic.h"
  16. #include "llvm/IR/CFG.h"
  17. #include "llvm/IR/Dominators.h"
  18. #include "llvm/IR/Instructions.h"
  19. #include "llvm/IR/Module.h"
  20. #include "llvm/Pass.h"
  21. #include "llvm/Transforms/Utils/Cloning.h"
  22. #include "llvm/Transforms/Utils/CodeExtractor.h"
  23. using namespace llvm;
  24. #define DEBUG_TYPE "partialinlining"
  25. STATISTIC(NumPartialInlined, "Number of functions partially inlined");
  26. namespace {
  27. struct PartialInliner : public ModulePass {
  28. void getAnalysisUsage(AnalysisUsage &AU) const override { }
  29. static char ID; // Pass identification, replacement for typeid
  30. PartialInliner() : ModulePass(ID) {
  31. initializePartialInlinerPass(*PassRegistry::getPassRegistry());
  32. }
  33. bool runOnModule(Module& M) override;
  34. private:
  35. Function* unswitchFunction(Function* F);
  36. };
  37. }
  38. char PartialInliner::ID = 0;
  39. INITIALIZE_PASS(PartialInliner, "partial-inliner",
  40. "Partial Inliner", false, false)
  41. ModulePass* llvm::createPartialInliningPass() { return new PartialInliner(); }
  42. Function* PartialInliner::unswitchFunction(Function* F) {
  43. // First, verify that this function is an unswitching candidate...
  44. BasicBlock* entryBlock = F->begin();
  45. BranchInst *BR = dyn_cast<BranchInst>(entryBlock->getTerminator());
  46. if (!BR || BR->isUnconditional())
  47. return nullptr;
  48. BasicBlock* returnBlock = nullptr;
  49. BasicBlock* nonReturnBlock = nullptr;
  50. unsigned returnCount = 0;
  51. for (BasicBlock *BB : successors(entryBlock)) {
  52. if (isa<ReturnInst>(BB->getTerminator())) {
  53. returnBlock = BB;
  54. returnCount++;
  55. } else
  56. nonReturnBlock = BB;
  57. }
  58. if (returnCount != 1)
  59. return nullptr;
  60. // Clone the function, so that we can hack away on it.
  61. ValueToValueMapTy VMap;
  62. Function* duplicateFunction = CloneFunction(F, VMap,
  63. /*ModuleLevelChanges=*/false);
  64. duplicateFunction->setLinkage(GlobalValue::InternalLinkage);
  65. F->getParent()->getFunctionList().push_back(duplicateFunction);
  66. BasicBlock* newEntryBlock = cast<BasicBlock>(VMap[entryBlock]);
  67. BasicBlock* newReturnBlock = cast<BasicBlock>(VMap[returnBlock]);
  68. BasicBlock* newNonReturnBlock = cast<BasicBlock>(VMap[nonReturnBlock]);
  69. // Go ahead and update all uses to the duplicate, so that we can just
  70. // use the inliner functionality when we're done hacking.
  71. F->replaceAllUsesWith(duplicateFunction);
  72. // Special hackery is needed with PHI nodes that have inputs from more than
  73. // one extracted block. For simplicity, just split the PHIs into a two-level
  74. // sequence of PHIs, some of which will go in the extracted region, and some
  75. // of which will go outside.
  76. BasicBlock* preReturn = newReturnBlock;
  77. newReturnBlock = newReturnBlock->splitBasicBlock(
  78. newReturnBlock->getFirstNonPHI());
  79. BasicBlock::iterator I = preReturn->begin();
  80. BasicBlock::iterator Ins = newReturnBlock->begin();
  81. while (I != preReturn->end()) {
  82. PHINode* OldPhi = dyn_cast<PHINode>(I);
  83. if (!OldPhi) break;
  84. PHINode* retPhi = PHINode::Create(OldPhi->getType(), 2, "", Ins);
  85. OldPhi->replaceAllUsesWith(retPhi);
  86. Ins = newReturnBlock->getFirstNonPHI();
  87. retPhi->addIncoming(I, preReturn);
  88. retPhi->addIncoming(OldPhi->getIncomingValueForBlock(newEntryBlock),
  89. newEntryBlock);
  90. OldPhi->removeIncomingValue(newEntryBlock);
  91. ++I;
  92. }
  93. newEntryBlock->getTerminator()->replaceUsesOfWith(preReturn, newReturnBlock);
  94. // Gather up the blocks that we're going to extract.
  95. std::vector<BasicBlock*> toExtract;
  96. toExtract.push_back(newNonReturnBlock);
  97. for (Function::iterator FI = duplicateFunction->begin(),
  98. FE = duplicateFunction->end(); FI != FE; ++FI)
  99. if (&*FI != newEntryBlock && &*FI != newReturnBlock &&
  100. &*FI != newNonReturnBlock)
  101. toExtract.push_back(FI);
  102. // The CodeExtractor needs a dominator tree.
  103. DominatorTree DT;
  104. DT.recalculate(*duplicateFunction);
  105. // Extract the body of the if.
  106. Function* extractedFunction
  107. = CodeExtractor(toExtract, &DT).extractCodeRegion();
  108. InlineFunctionInfo IFI;
  109. // Inline the top-level if test into all callers.
  110. std::vector<User *> Users(duplicateFunction->user_begin(),
  111. duplicateFunction->user_end());
  112. for (std::vector<User*>::iterator UI = Users.begin(), UE = Users.end();
  113. UI != UE; ++UI)
  114. if (CallInst *CI = dyn_cast<CallInst>(*UI))
  115. InlineFunction(CI, IFI);
  116. else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI))
  117. InlineFunction(II, IFI);
  118. // Ditch the duplicate, since we're done with it, and rewrite all remaining
  119. // users (function pointers, etc.) back to the original function.
  120. duplicateFunction->replaceAllUsesWith(F);
  121. duplicateFunction->eraseFromParent();
  122. ++NumPartialInlined;
  123. return extractedFunction;
  124. }
  125. bool PartialInliner::runOnModule(Module& M) {
  126. std::vector<Function*> worklist;
  127. worklist.reserve(M.size());
  128. for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
  129. if (!FI->use_empty() && !FI->isDeclaration())
  130. worklist.push_back(&*FI);
  131. bool changed = false;
  132. while (!worklist.empty()) {
  133. Function* currFunc = worklist.back();
  134. worklist.pop_back();
  135. if (currFunc->use_empty()) continue;
  136. bool recursive = false;
  137. for (User *U : currFunc->users())
  138. if (Instruction* I = dyn_cast<Instruction>(U))
  139. if (I->getParent()->getParent() == currFunc) {
  140. recursive = true;
  141. break;
  142. }
  143. if (recursive) continue;
  144. if (Function* newFunc = unswitchFunction(currFunc)) {
  145. worklist.push_back(newFunc);
  146. changed = true;
  147. }
  148. }
  149. return changed;
  150. }