BlockExtractor.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //===- BlockExtractor.cpp - Extracts blocks into their own functions ------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This pass extracts the specified basic blocks from the module into their
  10. // own functions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/IPO/BlockExtractor.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/ADT/Statistic.h"
  16. #include "llvm/IR/Instructions.h"
  17. #include "llvm/IR/Module.h"
  18. #include "llvm/IR/PassManager.h"
  19. #include "llvm/InitializePasses.h"
  20. #include "llvm/Pass.h"
  21. #include "llvm/Support/CommandLine.h"
  22. #include "llvm/Support/Debug.h"
  23. #include "llvm/Support/MemoryBuffer.h"
  24. #include "llvm/Transforms/IPO.h"
  25. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  26. #include "llvm/Transforms/Utils/CodeExtractor.h"
  27. using namespace llvm;
  28. #define DEBUG_TYPE "block-extractor"
  29. STATISTIC(NumExtracted, "Number of basic blocks extracted");
  30. static cl::opt<std::string> BlockExtractorFile(
  31. "extract-blocks-file", cl::value_desc("filename"),
  32. cl::desc("A file containing list of basic blocks to extract"), cl::Hidden);
  33. cl::opt<bool> BlockExtractorEraseFuncs("extract-blocks-erase-funcs",
  34. cl::desc("Erase the existing functions"),
  35. cl::Hidden);
  36. namespace {
  37. class BlockExtractor {
  38. public:
  39. BlockExtractor(bool EraseFunctions) : EraseFunctions(EraseFunctions) {}
  40. bool runOnModule(Module &M);
  41. void init(const SmallVectorImpl<SmallVector<BasicBlock *, 16>>
  42. &GroupsOfBlocksToExtract) {
  43. for (const SmallVectorImpl<BasicBlock *> &GroupOfBlocks :
  44. GroupsOfBlocksToExtract) {
  45. SmallVector<BasicBlock *, 16> NewGroup;
  46. NewGroup.append(GroupOfBlocks.begin(), GroupOfBlocks.end());
  47. GroupsOfBlocks.emplace_back(NewGroup);
  48. }
  49. if (!BlockExtractorFile.empty())
  50. loadFile();
  51. }
  52. private:
  53. SmallVector<SmallVector<BasicBlock *, 16>, 4> GroupsOfBlocks;
  54. bool EraseFunctions;
  55. /// Map a function name to groups of blocks.
  56. SmallVector<std::pair<std::string, SmallVector<std::string, 4>>, 4>
  57. BlocksByName;
  58. void loadFile();
  59. void splitLandingPadPreds(Function &F);
  60. };
  61. class BlockExtractorLegacyPass : public ModulePass {
  62. BlockExtractor BE;
  63. bool runOnModule(Module &M) override;
  64. public:
  65. static char ID;
  66. BlockExtractorLegacyPass(const SmallVectorImpl<BasicBlock *> &BlocksToExtract,
  67. bool EraseFunctions)
  68. : ModulePass(ID), BE(EraseFunctions) {
  69. // We want one group per element of the input list.
  70. SmallVector<SmallVector<BasicBlock *, 16>, 4> MassagedGroupsOfBlocks;
  71. for (BasicBlock *BB : BlocksToExtract) {
  72. SmallVector<BasicBlock *, 16> NewGroup;
  73. NewGroup.push_back(BB);
  74. MassagedGroupsOfBlocks.push_back(NewGroup);
  75. }
  76. BE.init(MassagedGroupsOfBlocks);
  77. }
  78. BlockExtractorLegacyPass(const SmallVectorImpl<SmallVector<BasicBlock *, 16>>
  79. &GroupsOfBlocksToExtract,
  80. bool EraseFunctions)
  81. : ModulePass(ID), BE(EraseFunctions) {
  82. BE.init(GroupsOfBlocksToExtract);
  83. }
  84. BlockExtractorLegacyPass()
  85. : BlockExtractorLegacyPass(SmallVector<BasicBlock *, 0>(), false) {}
  86. };
  87. } // end anonymous namespace
  88. char BlockExtractorLegacyPass::ID = 0;
  89. INITIALIZE_PASS(BlockExtractorLegacyPass, "extract-blocks",
  90. "Extract basic blocks from module", false, false)
  91. ModulePass *llvm::createBlockExtractorPass() {
  92. return new BlockExtractorLegacyPass();
  93. }
  94. ModulePass *llvm::createBlockExtractorPass(
  95. const SmallVectorImpl<BasicBlock *> &BlocksToExtract, bool EraseFunctions) {
  96. return new BlockExtractorLegacyPass(BlocksToExtract, EraseFunctions);
  97. }
  98. ModulePass *llvm::createBlockExtractorPass(
  99. const SmallVectorImpl<SmallVector<BasicBlock *, 16>>
  100. &GroupsOfBlocksToExtract,
  101. bool EraseFunctions) {
  102. return new BlockExtractorLegacyPass(GroupsOfBlocksToExtract, EraseFunctions);
  103. }
  104. /// Gets all of the blocks specified in the input file.
  105. void BlockExtractor::loadFile() {
  106. auto ErrOrBuf = MemoryBuffer::getFile(BlockExtractorFile);
  107. if (ErrOrBuf.getError())
  108. report_fatal_error("BlockExtractor couldn't load the file.");
  109. // Read the file.
  110. auto &Buf = *ErrOrBuf;
  111. SmallVector<StringRef, 16> Lines;
  112. Buf->getBuffer().split(Lines, '\n', /*MaxSplit=*/-1,
  113. /*KeepEmpty=*/false);
  114. for (const auto &Line : Lines) {
  115. SmallVector<StringRef, 4> LineSplit;
  116. Line.split(LineSplit, ' ', /*MaxSplit=*/-1,
  117. /*KeepEmpty=*/false);
  118. if (LineSplit.empty())
  119. continue;
  120. if (LineSplit.size()!=2)
  121. report_fatal_error("Invalid line format, expecting lines like: 'funcname bb1[;bb2..]'");
  122. SmallVector<StringRef, 4> BBNames;
  123. LineSplit[1].split(BBNames, ';', /*MaxSplit=*/-1,
  124. /*KeepEmpty=*/false);
  125. if (BBNames.empty())
  126. report_fatal_error("Missing bbs name");
  127. BlocksByName.push_back(
  128. {std::string(LineSplit[0]), {BBNames.begin(), BBNames.end()}});
  129. }
  130. }
  131. /// Extracts the landing pads to make sure all of them have only one
  132. /// predecessor.
  133. void BlockExtractor::splitLandingPadPreds(Function &F) {
  134. for (BasicBlock &BB : F) {
  135. for (Instruction &I : BB) {
  136. if (!isa<InvokeInst>(&I))
  137. continue;
  138. InvokeInst *II = cast<InvokeInst>(&I);
  139. BasicBlock *Parent = II->getParent();
  140. BasicBlock *LPad = II->getUnwindDest();
  141. // Look through the landing pad's predecessors. If one of them ends in an
  142. // 'invoke', then we want to split the landing pad.
  143. bool Split = false;
  144. for (auto PredBB : predecessors(LPad)) {
  145. if (PredBB->isLandingPad() && PredBB != Parent &&
  146. isa<InvokeInst>(Parent->getTerminator())) {
  147. Split = true;
  148. break;
  149. }
  150. }
  151. if (!Split)
  152. continue;
  153. SmallVector<BasicBlock *, 2> NewBBs;
  154. SplitLandingPadPredecessors(LPad, Parent, ".1", ".2", NewBBs);
  155. }
  156. }
  157. }
  158. bool BlockExtractor::runOnModule(Module &M) {
  159. bool Changed = false;
  160. // Get all the functions.
  161. SmallVector<Function *, 4> Functions;
  162. for (Function &F : M) {
  163. splitLandingPadPreds(F);
  164. Functions.push_back(&F);
  165. }
  166. // Get all the blocks specified in the input file.
  167. unsigned NextGroupIdx = GroupsOfBlocks.size();
  168. GroupsOfBlocks.resize(NextGroupIdx + BlocksByName.size());
  169. for (const auto &BInfo : BlocksByName) {
  170. Function *F = M.getFunction(BInfo.first);
  171. if (!F)
  172. report_fatal_error("Invalid function name specified in the input file");
  173. for (const auto &BBInfo : BInfo.second) {
  174. auto Res = llvm::find_if(*F, [&](const BasicBlock &BB) {
  175. return BB.getName().equals(BBInfo);
  176. });
  177. if (Res == F->end())
  178. report_fatal_error("Invalid block name specified in the input file");
  179. GroupsOfBlocks[NextGroupIdx].push_back(&*Res);
  180. }
  181. ++NextGroupIdx;
  182. }
  183. // Extract each group of basic blocks.
  184. for (auto &BBs : GroupsOfBlocks) {
  185. SmallVector<BasicBlock *, 32> BlocksToExtractVec;
  186. for (BasicBlock *BB : BBs) {
  187. // Check if the module contains BB.
  188. if (BB->getParent()->getParent() != &M)
  189. report_fatal_error("Invalid basic block");
  190. LLVM_DEBUG(dbgs() << "BlockExtractor: Extracting "
  191. << BB->getParent()->getName() << ":" << BB->getName()
  192. << "\n");
  193. BlocksToExtractVec.push_back(BB);
  194. if (const InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
  195. BlocksToExtractVec.push_back(II->getUnwindDest());
  196. ++NumExtracted;
  197. Changed = true;
  198. }
  199. CodeExtractorAnalysisCache CEAC(*BBs[0]->getParent());
  200. Function *F = CodeExtractor(BlocksToExtractVec).extractCodeRegion(CEAC);
  201. if (F)
  202. LLVM_DEBUG(dbgs() << "Extracted group '" << (*BBs.begin())->getName()
  203. << "' in: " << F->getName() << '\n');
  204. else
  205. LLVM_DEBUG(dbgs() << "Failed to extract for group '"
  206. << (*BBs.begin())->getName() << "'\n");
  207. }
  208. // Erase the functions.
  209. if (EraseFunctions || BlockExtractorEraseFuncs) {
  210. for (Function *F : Functions) {
  211. LLVM_DEBUG(dbgs() << "BlockExtractor: Trying to delete " << F->getName()
  212. << "\n");
  213. F->deleteBody();
  214. }
  215. // Set linkage as ExternalLinkage to avoid erasing unreachable functions.
  216. for (Function &F : M)
  217. F.setLinkage(GlobalValue::ExternalLinkage);
  218. Changed = true;
  219. }
  220. return Changed;
  221. }
  222. bool BlockExtractorLegacyPass::runOnModule(Module &M) {
  223. return BE.runOnModule(M);
  224. }
  225. PreservedAnalyses BlockExtractorPass::run(Module &M,
  226. ModuleAnalysisManager &AM) {
  227. BlockExtractor BE(false);
  228. BE.init(SmallVector<SmallVector<BasicBlock *, 16>, 0>());
  229. return BE.runOnModule(M) ? PreservedAnalyses::none()
  230. : PreservedAnalyses::all();
  231. }