1dc40be75SVolkan Keles //===- BlockExtractor.cpp - Extracts blocks into their own functions ------===//
2dc40be75SVolkan Keles //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6dc40be75SVolkan Keles //
7dc40be75SVolkan Keles //===----------------------------------------------------------------------===//
8dc40be75SVolkan Keles //
9dc40be75SVolkan Keles // This pass extracts the specified basic blocks from the module into their
10dc40be75SVolkan Keles // own functions.
11dc40be75SVolkan Keles //
12dc40be75SVolkan Keles //===----------------------------------------------------------------------===//
13dc40be75SVolkan Keles 
148d9466a3SArthur Eubanks #include "llvm/Transforms/IPO/BlockExtractor.h"
15dc40be75SVolkan Keles #include "llvm/ADT/STLExtras.h"
16dc40be75SVolkan Keles #include "llvm/ADT/Statistic.h"
17dc40be75SVolkan Keles #include "llvm/IR/Instructions.h"
18dc40be75SVolkan Keles #include "llvm/IR/Module.h"
198d9466a3SArthur Eubanks #include "llvm/IR/PassManager.h"
2005da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
21dc40be75SVolkan Keles #include "llvm/Pass.h"
22dc40be75SVolkan Keles #include "llvm/Support/CommandLine.h"
23dc40be75SVolkan Keles #include "llvm/Support/Debug.h"
24dc40be75SVolkan Keles #include "llvm/Support/MemoryBuffer.h"
25dc40be75SVolkan Keles #include "llvm/Transforms/IPO.h"
26dc40be75SVolkan Keles #include "llvm/Transforms/Utils/BasicBlockUtils.h"
27dc40be75SVolkan Keles #include "llvm/Transforms/Utils/CodeExtractor.h"
28ea3364bfSQuentin Colombet 
29dc40be75SVolkan Keles using namespace llvm;
30dc40be75SVolkan Keles 
31dc40be75SVolkan Keles #define DEBUG_TYPE "block-extractor"
32dc40be75SVolkan Keles 
33dc40be75SVolkan Keles STATISTIC(NumExtracted, "Number of basic blocks extracted");
34dc40be75SVolkan Keles 
35dc40be75SVolkan Keles static cl::opt<std::string> BlockExtractorFile(
36dc40be75SVolkan Keles     "extract-blocks-file", cl::value_desc("filename"),
37dc40be75SVolkan Keles     cl::desc("A file containing list of basic blocks to extract"), cl::Hidden);
38dc40be75SVolkan Keles 
39*d8aba75aSFangrui Song static cl::opt<bool>
40*d8aba75aSFangrui Song     BlockExtractorEraseFuncs("extract-blocks-erase-funcs",
41dc40be75SVolkan Keles                              cl::desc("Erase the existing functions"),
42dc40be75SVolkan Keles                              cl::Hidden);
43dc40be75SVolkan Keles namespace {
448d9466a3SArthur Eubanks class BlockExtractor {
458d9466a3SArthur Eubanks public:
468d9466a3SArthur Eubanks   BlockExtractor(bool EraseFunctions) : EraseFunctions(EraseFunctions) {}
478d9466a3SArthur Eubanks   bool runOnModule(Module &M);
4831ce2742SQuentin Colombet   void init(const SmallVectorImpl<SmallVector<BasicBlock *, 16>>
4931ce2742SQuentin Colombet                 &GroupsOfBlocksToExtract) {
5031ce2742SQuentin Colombet     for (const SmallVectorImpl<BasicBlock *> &GroupOfBlocks :
5131ce2742SQuentin Colombet          GroupsOfBlocksToExtract) {
5231ce2742SQuentin Colombet       SmallVector<BasicBlock *, 16> NewGroup;
5331ce2742SQuentin Colombet       NewGroup.append(GroupOfBlocks.begin(), GroupOfBlocks.end());
5431ce2742SQuentin Colombet       GroupsOfBlocks.emplace_back(NewGroup);
5531ce2742SQuentin Colombet     }
5631ce2742SQuentin Colombet     if (!BlockExtractorFile.empty())
5731ce2742SQuentin Colombet       loadFile();
5831ce2742SQuentin Colombet   }
5931ce2742SQuentin Colombet 
608d9466a3SArthur Eubanks private:
618d9466a3SArthur Eubanks   SmallVector<SmallVector<BasicBlock *, 16>, 4> GroupsOfBlocks;
628d9466a3SArthur Eubanks   bool EraseFunctions;
638d9466a3SArthur Eubanks   /// Map a function name to groups of blocks.
648d9466a3SArthur Eubanks   SmallVector<std::pair<std::string, SmallVector<std::string, 4>>, 4>
658d9466a3SArthur Eubanks       BlocksByName;
668d9466a3SArthur Eubanks 
678d9466a3SArthur Eubanks   void loadFile();
688d9466a3SArthur Eubanks   void splitLandingPadPreds(Function &F);
698d9466a3SArthur Eubanks };
708d9466a3SArthur Eubanks 
718d9466a3SArthur Eubanks class BlockExtractorLegacyPass : public ModulePass {
728d9466a3SArthur Eubanks   BlockExtractor BE;
738d9466a3SArthur Eubanks   bool runOnModule(Module &M) override;
748d9466a3SArthur Eubanks 
75dc40be75SVolkan Keles public:
76dc40be75SVolkan Keles   static char ID;
778d9466a3SArthur Eubanks   BlockExtractorLegacyPass(const SmallVectorImpl<BasicBlock *> &BlocksToExtract,
78dc40be75SVolkan Keles                            bool EraseFunctions)
798d9466a3SArthur Eubanks       : ModulePass(ID), BE(EraseFunctions) {
80ea3364bfSQuentin Colombet     // We want one group per element of the input list.
8131ce2742SQuentin Colombet     SmallVector<SmallVector<BasicBlock *, 16>, 4> MassagedGroupsOfBlocks;
82ea3364bfSQuentin Colombet     for (BasicBlock *BB : BlocksToExtract) {
83ea3364bfSQuentin Colombet       SmallVector<BasicBlock *, 16> NewGroup;
84ea3364bfSQuentin Colombet       NewGroup.push_back(BB);
8531ce2742SQuentin Colombet       MassagedGroupsOfBlocks.push_back(NewGroup);
86ea3364bfSQuentin Colombet     }
878d9466a3SArthur Eubanks     BE.init(MassagedGroupsOfBlocks);
88dc40be75SVolkan Keles   }
8931ce2742SQuentin Colombet 
908d9466a3SArthur Eubanks   BlockExtractorLegacyPass(const SmallVectorImpl<SmallVector<BasicBlock *, 16>>
9131ce2742SQuentin Colombet                                &GroupsOfBlocksToExtract,
9231ce2742SQuentin Colombet                            bool EraseFunctions)
938d9466a3SArthur Eubanks       : ModulePass(ID), BE(EraseFunctions) {
948d9466a3SArthur Eubanks     BE.init(GroupsOfBlocksToExtract);
9531ce2742SQuentin Colombet   }
9631ce2742SQuentin Colombet 
978d9466a3SArthur Eubanks   BlockExtractorLegacyPass()
988d9466a3SArthur Eubanks       : BlockExtractorLegacyPass(SmallVector<BasicBlock *, 0>(), false) {}
99dc40be75SVolkan Keles };
1008d9466a3SArthur Eubanks 
101dc40be75SVolkan Keles } // end anonymous namespace
102dc40be75SVolkan Keles 
1038d9466a3SArthur Eubanks char BlockExtractorLegacyPass::ID = 0;
1048d9466a3SArthur Eubanks INITIALIZE_PASS(BlockExtractorLegacyPass, "extract-blocks",
105dc40be75SVolkan Keles                 "Extract basic blocks from module", false, false)
106dc40be75SVolkan Keles 
1078d9466a3SArthur Eubanks ModulePass *llvm::createBlockExtractorPass() {
1088d9466a3SArthur Eubanks   return new BlockExtractorLegacyPass();
1098d9466a3SArthur Eubanks }
110dc40be75SVolkan Keles ModulePass *llvm::createBlockExtractorPass(
111dc40be75SVolkan Keles     const SmallVectorImpl<BasicBlock *> &BlocksToExtract, bool EraseFunctions) {
1128d9466a3SArthur Eubanks   return new BlockExtractorLegacyPass(BlocksToExtract, EraseFunctions);
113dc40be75SVolkan Keles }
11431ce2742SQuentin Colombet ModulePass *llvm::createBlockExtractorPass(
11531ce2742SQuentin Colombet     const SmallVectorImpl<SmallVector<BasicBlock *, 16>>
11631ce2742SQuentin Colombet         &GroupsOfBlocksToExtract,
11731ce2742SQuentin Colombet     bool EraseFunctions) {
1188d9466a3SArthur Eubanks   return new BlockExtractorLegacyPass(GroupsOfBlocksToExtract, EraseFunctions);
11931ce2742SQuentin Colombet }
120dc40be75SVolkan Keles 
121dc40be75SVolkan Keles /// Gets all of the blocks specified in the input file.
122dc40be75SVolkan Keles void BlockExtractor::loadFile() {
123dc40be75SVolkan Keles   auto ErrOrBuf = MemoryBuffer::getFile(BlockExtractorFile);
124ebf34ea3SVolkan Keles   if (ErrOrBuf.getError())
125dc40be75SVolkan Keles     report_fatal_error("BlockExtractor couldn't load the file.");
126dc40be75SVolkan Keles   // Read the file.
127dc40be75SVolkan Keles   auto &Buf = *ErrOrBuf;
128dc40be75SVolkan Keles   SmallVector<StringRef, 16> Lines;
129dc40be75SVolkan Keles   Buf->getBuffer().split(Lines, '\n', /*MaxSplit=*/-1,
130dc40be75SVolkan Keles                          /*KeepEmpty=*/false);
131dc40be75SVolkan Keles   for (const auto &Line : Lines) {
132ea3364bfSQuentin Colombet     SmallVector<StringRef, 4> LineSplit;
133ea3364bfSQuentin Colombet     Line.split(LineSplit, ' ', /*MaxSplit=*/-1,
134ea3364bfSQuentin Colombet                /*KeepEmpty=*/false);
135ea3364bfSQuentin Colombet     if (LineSplit.empty())
136ea3364bfSQuentin Colombet       continue;
137cda334baSJinsong Ji     if (LineSplit.size()!=2)
138cda334baSJinsong Ji       report_fatal_error("Invalid line format, expecting lines like: 'funcname bb1[;bb2..]'");
139ea3364bfSQuentin Colombet     SmallVector<StringRef, 4> BBNames;
140ae2cbb34SQuentin Colombet     LineSplit[1].split(BBNames, ';', /*MaxSplit=*/-1,
141ea3364bfSQuentin Colombet                        /*KeepEmpty=*/false);
142ea3364bfSQuentin Colombet     if (BBNames.empty())
143ea3364bfSQuentin Colombet       report_fatal_error("Missing bbs name");
144adcd0268SBenjamin Kramer     BlocksByName.push_back(
145adcd0268SBenjamin Kramer         {std::string(LineSplit[0]), {BBNames.begin(), BBNames.end()}});
146dc40be75SVolkan Keles   }
147dc40be75SVolkan Keles }
148dc40be75SVolkan Keles 
149dc40be75SVolkan Keles /// Extracts the landing pads to make sure all of them have only one
150dc40be75SVolkan Keles /// predecessor.
151dc40be75SVolkan Keles void BlockExtractor::splitLandingPadPreds(Function &F) {
152dc40be75SVolkan Keles   for (BasicBlock &BB : F) {
153dc40be75SVolkan Keles     for (Instruction &I : BB) {
154dc40be75SVolkan Keles       if (!isa<InvokeInst>(&I))
155dc40be75SVolkan Keles         continue;
156dc40be75SVolkan Keles       InvokeInst *II = cast<InvokeInst>(&I);
157dc40be75SVolkan Keles       BasicBlock *Parent = II->getParent();
158dc40be75SVolkan Keles       BasicBlock *LPad = II->getUnwindDest();
159dc40be75SVolkan Keles 
160dc40be75SVolkan Keles       // Look through the landing pad's predecessors. If one of them ends in an
161dc40be75SVolkan Keles       // 'invoke', then we want to split the landing pad.
162dc40be75SVolkan Keles       bool Split = false;
163dc40be75SVolkan Keles       for (auto PredBB : predecessors(LPad)) {
164dc40be75SVolkan Keles         if (PredBB->isLandingPad() && PredBB != Parent &&
165dc40be75SVolkan Keles             isa<InvokeInst>(Parent->getTerminator())) {
166dc40be75SVolkan Keles           Split = true;
167dc40be75SVolkan Keles           break;
168dc40be75SVolkan Keles         }
169dc40be75SVolkan Keles       }
170dc40be75SVolkan Keles 
171dc40be75SVolkan Keles       if (!Split)
172dc40be75SVolkan Keles         continue;
173dc40be75SVolkan Keles 
174dc40be75SVolkan Keles       SmallVector<BasicBlock *, 2> NewBBs;
175dc40be75SVolkan Keles       SplitLandingPadPredecessors(LPad, Parent, ".1", ".2", NewBBs);
176dc40be75SVolkan Keles     }
177dc40be75SVolkan Keles   }
178dc40be75SVolkan Keles }
179dc40be75SVolkan Keles 
180dc40be75SVolkan Keles bool BlockExtractor::runOnModule(Module &M) {
181dc40be75SVolkan Keles 
182dc40be75SVolkan Keles   bool Changed = false;
183dc40be75SVolkan Keles 
184dc40be75SVolkan Keles   // Get all the functions.
185dc40be75SVolkan Keles   SmallVector<Function *, 4> Functions;
186dc40be75SVolkan Keles   for (Function &F : M) {
187dc40be75SVolkan Keles     splitLandingPadPreds(F);
188dc40be75SVolkan Keles     Functions.push_back(&F);
189dc40be75SVolkan Keles   }
190dc40be75SVolkan Keles 
191dc40be75SVolkan Keles   // Get all the blocks specified in the input file.
192ea3364bfSQuentin Colombet   unsigned NextGroupIdx = GroupsOfBlocks.size();
193ea3364bfSQuentin Colombet   GroupsOfBlocks.resize(NextGroupIdx + BlocksByName.size());
194dc40be75SVolkan Keles   for (const auto &BInfo : BlocksByName) {
195dc40be75SVolkan Keles     Function *F = M.getFunction(BInfo.first);
196dc40be75SVolkan Keles     if (!F)
197dc40be75SVolkan Keles       report_fatal_error("Invalid function name specified in the input file");
198ea3364bfSQuentin Colombet     for (const auto &BBInfo : BInfo.second) {
199dc40be75SVolkan Keles       auto Res = llvm::find_if(*F, [&](const BasicBlock &BB) {
200ea3364bfSQuentin Colombet         return BB.getName().equals(BBInfo);
201dc40be75SVolkan Keles       });
202dc40be75SVolkan Keles       if (Res == F->end())
203dc40be75SVolkan Keles         report_fatal_error("Invalid block name specified in the input file");
204ea3364bfSQuentin Colombet       GroupsOfBlocks[NextGroupIdx].push_back(&*Res);
205ea3364bfSQuentin Colombet     }
206ea3364bfSQuentin Colombet     ++NextGroupIdx;
207dc40be75SVolkan Keles   }
208dc40be75SVolkan Keles 
209ea3364bfSQuentin Colombet   // Extract each group of basic blocks.
210ea3364bfSQuentin Colombet   for (auto &BBs : GroupsOfBlocks) {
211ea3364bfSQuentin Colombet     SmallVector<BasicBlock *, 32> BlocksToExtractVec;
212ea3364bfSQuentin Colombet     for (BasicBlock *BB : BBs) {
213dc40be75SVolkan Keles       // Check if the module contains BB.
214dc40be75SVolkan Keles       if (BB->getParent()->getParent() != &M)
215dc40be75SVolkan Keles         report_fatal_error("Invalid basic block");
216d34e60caSNicola Zaghen       LLVM_DEBUG(dbgs() << "BlockExtractor: Extracting "
217d34e60caSNicola Zaghen                         << BB->getParent()->getName() << ":" << BB->getName()
218d34e60caSNicola Zaghen                         << "\n");
219dc40be75SVolkan Keles       BlocksToExtractVec.push_back(BB);
220dc40be75SVolkan Keles       if (const InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
221dc40be75SVolkan Keles         BlocksToExtractVec.push_back(II->getUnwindDest());
222dc40be75SVolkan Keles       ++NumExtracted;
223dc40be75SVolkan Keles       Changed = true;
224dc40be75SVolkan Keles     }
2259852699dSVedant Kumar     CodeExtractorAnalysisCache CEAC(*BBs[0]->getParent());
2269852699dSVedant Kumar     Function *F = CodeExtractor(BlocksToExtractVec).extractCodeRegion(CEAC);
227ea3364bfSQuentin Colombet     if (F)
228ea3364bfSQuentin Colombet       LLVM_DEBUG(dbgs() << "Extracted group '" << (*BBs.begin())->getName()
229ea3364bfSQuentin Colombet                         << "' in: " << F->getName() << '\n');
230ea3364bfSQuentin Colombet     else
231ea3364bfSQuentin Colombet       LLVM_DEBUG(dbgs() << "Failed to extract for group '"
232ea3364bfSQuentin Colombet                         << (*BBs.begin())->getName() << "'\n");
233ea3364bfSQuentin Colombet   }
234dc40be75SVolkan Keles 
235dc40be75SVolkan Keles   // Erase the functions.
236dc40be75SVolkan Keles   if (EraseFunctions || BlockExtractorEraseFuncs) {
237dc40be75SVolkan Keles     for (Function *F : Functions) {
238d34e60caSNicola Zaghen       LLVM_DEBUG(dbgs() << "BlockExtractor: Trying to delete " << F->getName()
2394ecdb44aSVolkan Keles                         << "\n");
2404ecdb44aSVolkan Keles       F->deleteBody();
241dc40be75SVolkan Keles     }
242dc40be75SVolkan Keles     // Set linkage as ExternalLinkage to avoid erasing unreachable functions.
243dc40be75SVolkan Keles     for (Function &F : M)
244dc40be75SVolkan Keles       F.setLinkage(GlobalValue::ExternalLinkage);
245dc40be75SVolkan Keles     Changed = true;
246dc40be75SVolkan Keles   }
247dc40be75SVolkan Keles 
248dc40be75SVolkan Keles   return Changed;
249dc40be75SVolkan Keles }
2508d9466a3SArthur Eubanks 
2518d9466a3SArthur Eubanks bool BlockExtractorLegacyPass::runOnModule(Module &M) {
2528d9466a3SArthur Eubanks   return BE.runOnModule(M);
2538d9466a3SArthur Eubanks }
2548d9466a3SArthur Eubanks 
2558d9466a3SArthur Eubanks PreservedAnalyses BlockExtractorPass::run(Module &M,
2568d9466a3SArthur Eubanks                                           ModuleAnalysisManager &AM) {
2578d9466a3SArthur Eubanks   BlockExtractor BE(false);
2588d9466a3SArthur Eubanks   BE.init(SmallVector<SmallVector<BasicBlock *, 16>, 0>());
2598d9466a3SArthur Eubanks   return BE.runOnModule(M) ? PreservedAnalyses::none()
2608d9466a3SArthur Eubanks                            : PreservedAnalyses::all();
2618d9466a3SArthur Eubanks }
262