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