blob: aec470ffadc436097f27e354b83ff5e5f1b971a6 [file] [log] [blame]
Volkan Kelesdc40be72018-01-23 21:51:34 +00001//===- BlockExtractor.cpp - Extracts blocks into their own functions ------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Volkan Kelesdc40be72018-01-23 21:51:34 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This pass extracts the specified basic blocks from the module into their
10// own functions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/ADT/Statistic.h"
16#include "llvm/IR/Instructions.h"
17#include "llvm/IR/Module.h"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080018#include "llvm/InitializePasses.h"
Volkan Kelesdc40be72018-01-23 21:51:34 +000019#include "llvm/Pass.h"
20#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/Debug.h"
22#include "llvm/Support/MemoryBuffer.h"
23#include "llvm/Transforms/IPO.h"
24#include "llvm/Transforms/Utils/BasicBlockUtils.h"
25#include "llvm/Transforms/Utils/CodeExtractor.h"
Quentin Colombetea3364b2019-04-18 18:28:30 +000026
Volkan Kelesdc40be72018-01-23 21:51:34 +000027using namespace llvm;
28
29#define DEBUG_TYPE "block-extractor"
30
31STATISTIC(NumExtracted, "Number of basic blocks extracted");
32
33static cl::opt<std::string> BlockExtractorFile(
34 "extract-blocks-file", cl::value_desc("filename"),
35 cl::desc("A file containing list of basic blocks to extract"), cl::Hidden);
36
37cl::opt<bool> BlockExtractorEraseFuncs("extract-blocks-erase-funcs",
38 cl::desc("Erase the existing functions"),
39 cl::Hidden);
Volkan Kelesdc40be72018-01-23 21:51:34 +000040namespace {
41class BlockExtractor : public ModulePass {
Quentin Colombetea3364b2019-04-18 18:28:30 +000042 SmallVector<SmallVector<BasicBlock *, 16>, 4> GroupsOfBlocks;
Volkan Kelesdc40be72018-01-23 21:51:34 +000043 bool EraseFunctions;
Quentin Colombetea3364b2019-04-18 18:28:30 +000044 /// Map a function name to groups of blocks.
45 SmallVector<std::pair<std::string, SmallVector<std::string, 4>>, 4>
46 BlocksByName;
Volkan Kelesdc40be72018-01-23 21:51:34 +000047
Quentin Colombet31ce2742019-04-29 16:14:02 +000048 void init(const SmallVectorImpl<SmallVector<BasicBlock *, 16>>
49 &GroupsOfBlocksToExtract) {
50 for (const SmallVectorImpl<BasicBlock *> &GroupOfBlocks :
51 GroupsOfBlocksToExtract) {
52 SmallVector<BasicBlock *, 16> NewGroup;
53 NewGroup.append(GroupOfBlocks.begin(), GroupOfBlocks.end());
54 GroupsOfBlocks.emplace_back(NewGroup);
55 }
56 if (!BlockExtractorFile.empty())
57 loadFile();
58 }
59
Volkan Kelesdc40be72018-01-23 21:51:34 +000060public:
61 static char ID;
62 BlockExtractor(const SmallVectorImpl<BasicBlock *> &BlocksToExtract,
63 bool EraseFunctions)
Quentin Colombetea3364b2019-04-18 18:28:30 +000064 : ModulePass(ID), EraseFunctions(EraseFunctions) {
65 // We want one group per element of the input list.
Quentin Colombet31ce2742019-04-29 16:14:02 +000066 SmallVector<SmallVector<BasicBlock *, 16>, 4> MassagedGroupsOfBlocks;
Quentin Colombetea3364b2019-04-18 18:28:30 +000067 for (BasicBlock *BB : BlocksToExtract) {
68 SmallVector<BasicBlock *, 16> NewGroup;
69 NewGroup.push_back(BB);
Quentin Colombet31ce2742019-04-29 16:14:02 +000070 MassagedGroupsOfBlocks.push_back(NewGroup);
Quentin Colombetea3364b2019-04-18 18:28:30 +000071 }
Quentin Colombet31ce2742019-04-29 16:14:02 +000072 init(MassagedGroupsOfBlocks);
Volkan Kelesdc40be72018-01-23 21:51:34 +000073 }
Quentin Colombet31ce2742019-04-29 16:14:02 +000074
75 BlockExtractor(const SmallVectorImpl<SmallVector<BasicBlock *, 16>>
76 &GroupsOfBlocksToExtract,
77 bool EraseFunctions)
78 : ModulePass(ID), EraseFunctions(EraseFunctions) {
79 init(GroupsOfBlocksToExtract);
80 }
81
Volkan Kelesdc40be72018-01-23 21:51:34 +000082 BlockExtractor() : BlockExtractor(SmallVector<BasicBlock *, 0>(), false) {}
83 bool runOnModule(Module &M) override;
84
85private:
86 void loadFile();
87 void splitLandingPadPreds(Function &F);
88};
89} // end anonymous namespace
90
91char BlockExtractor::ID = 0;
92INITIALIZE_PASS(BlockExtractor, "extract-blocks",
93 "Extract basic blocks from module", false, false)
94
95ModulePass *llvm::createBlockExtractorPass() { return new BlockExtractor(); }
96ModulePass *llvm::createBlockExtractorPass(
97 const SmallVectorImpl<BasicBlock *> &BlocksToExtract, bool EraseFunctions) {
98 return new BlockExtractor(BlocksToExtract, EraseFunctions);
99}
Quentin Colombet31ce2742019-04-29 16:14:02 +0000100ModulePass *llvm::createBlockExtractorPass(
101 const SmallVectorImpl<SmallVector<BasicBlock *, 16>>
102 &GroupsOfBlocksToExtract,
103 bool EraseFunctions) {
104 return new BlockExtractor(GroupsOfBlocksToExtract, EraseFunctions);
105}
Volkan Kelesdc40be72018-01-23 21:51:34 +0000106
107/// Gets all of the blocks specified in the input file.
108void BlockExtractor::loadFile() {
109 auto ErrOrBuf = MemoryBuffer::getFile(BlockExtractorFile);
Volkan Kelesebf34ea2018-01-23 22:24:34 +0000110 if (ErrOrBuf.getError())
Volkan Kelesdc40be72018-01-23 21:51:34 +0000111 report_fatal_error("BlockExtractor couldn't load the file.");
112 // Read the file.
113 auto &Buf = *ErrOrBuf;
114 SmallVector<StringRef, 16> Lines;
115 Buf->getBuffer().split(Lines, '\n', /*MaxSplit=*/-1,
116 /*KeepEmpty=*/false);
117 for (const auto &Line : Lines) {
Quentin Colombetea3364b2019-04-18 18:28:30 +0000118 SmallVector<StringRef, 4> LineSplit;
119 Line.split(LineSplit, ' ', /*MaxSplit=*/-1,
120 /*KeepEmpty=*/false);
121 if (LineSplit.empty())
122 continue;
Jinsong Jicda334b2019-08-20 14:46:02 +0000123 if (LineSplit.size()!=2)
124 report_fatal_error("Invalid line format, expecting lines like: 'funcname bb1[;bb2..]'");
Quentin Colombetea3364b2019-04-18 18:28:30 +0000125 SmallVector<StringRef, 4> BBNames;
Quentin Colombetae2cbb32019-04-29 16:14:00 +0000126 LineSplit[1].split(BBNames, ';', /*MaxSplit=*/-1,
Quentin Colombetea3364b2019-04-18 18:28:30 +0000127 /*KeepEmpty=*/false);
128 if (BBNames.empty())
129 report_fatal_error("Missing bbs name");
130 BlocksByName.push_back({LineSplit[0], {BBNames.begin(), BBNames.end()}});
Volkan Kelesdc40be72018-01-23 21:51:34 +0000131 }
132}
133
134/// Extracts the landing pads to make sure all of them have only one
135/// predecessor.
136void BlockExtractor::splitLandingPadPreds(Function &F) {
137 for (BasicBlock &BB : F) {
138 for (Instruction &I : BB) {
139 if (!isa<InvokeInst>(&I))
140 continue;
141 InvokeInst *II = cast<InvokeInst>(&I);
142 BasicBlock *Parent = II->getParent();
143 BasicBlock *LPad = II->getUnwindDest();
144
145 // Look through the landing pad's predecessors. If one of them ends in an
146 // 'invoke', then we want to split the landing pad.
147 bool Split = false;
148 for (auto PredBB : predecessors(LPad)) {
149 if (PredBB->isLandingPad() && PredBB != Parent &&
150 isa<InvokeInst>(Parent->getTerminator())) {
151 Split = true;
152 break;
153 }
154 }
155
156 if (!Split)
157 continue;
158
159 SmallVector<BasicBlock *, 2> NewBBs;
160 SplitLandingPadPredecessors(LPad, Parent, ".1", ".2", NewBBs);
161 }
162 }
163}
164
165bool BlockExtractor::runOnModule(Module &M) {
166
167 bool Changed = false;
168
169 // Get all the functions.
170 SmallVector<Function *, 4> Functions;
171 for (Function &F : M) {
172 splitLandingPadPreds(F);
173 Functions.push_back(&F);
174 }
175
176 // Get all the blocks specified in the input file.
Quentin Colombetea3364b2019-04-18 18:28:30 +0000177 unsigned NextGroupIdx = GroupsOfBlocks.size();
178 GroupsOfBlocks.resize(NextGroupIdx + BlocksByName.size());
Volkan Kelesdc40be72018-01-23 21:51:34 +0000179 for (const auto &BInfo : BlocksByName) {
180 Function *F = M.getFunction(BInfo.first);
181 if (!F)
182 report_fatal_error("Invalid function name specified in the input file");
Quentin Colombetea3364b2019-04-18 18:28:30 +0000183 for (const auto &BBInfo : BInfo.second) {
184 auto Res = llvm::find_if(*F, [&](const BasicBlock &BB) {
185 return BB.getName().equals(BBInfo);
186 });
187 if (Res == F->end())
188 report_fatal_error("Invalid block name specified in the input file");
189 GroupsOfBlocks[NextGroupIdx].push_back(&*Res);
190 }
191 ++NextGroupIdx;
Volkan Kelesdc40be72018-01-23 21:51:34 +0000192 }
193
Quentin Colombetea3364b2019-04-18 18:28:30 +0000194 // Extract each group of basic blocks.
195 for (auto &BBs : GroupsOfBlocks) {
196 SmallVector<BasicBlock *, 32> BlocksToExtractVec;
197 for (BasicBlock *BB : BBs) {
198 // Check if the module contains BB.
199 if (BB->getParent()->getParent() != &M)
200 report_fatal_error("Invalid basic block");
201 LLVM_DEBUG(dbgs() << "BlockExtractor: Extracting "
202 << BB->getParent()->getName() << ":" << BB->getName()
203 << "\n");
204 BlocksToExtractVec.push_back(BB);
205 if (const InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
206 BlocksToExtractVec.push_back(II->getUnwindDest());
207 ++NumExtracted;
208 Changed = true;
209 }
Vedant Kumar98526992019-10-08 17:17:51 +0000210 CodeExtractorAnalysisCache CEAC(*BBs[0]->getParent());
211 Function *F = CodeExtractor(BlocksToExtractVec).extractCodeRegion(CEAC);
Quentin Colombetea3364b2019-04-18 18:28:30 +0000212 if (F)
213 LLVM_DEBUG(dbgs() << "Extracted group '" << (*BBs.begin())->getName()
214 << "' in: " << F->getName() << '\n');
215 else
216 LLVM_DEBUG(dbgs() << "Failed to extract for group '"
217 << (*BBs.begin())->getName() << "'\n");
Volkan Kelesdc40be72018-01-23 21:51:34 +0000218 }
219
220 // Erase the functions.
221 if (EraseFunctions || BlockExtractorEraseFuncs) {
222 for (Function *F : Functions) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000223 LLVM_DEBUG(dbgs() << "BlockExtractor: Trying to delete " << F->getName()
224 << "\n");
Volkan Keles4ecdb442018-03-12 22:28:18 +0000225 F->deleteBody();
Volkan Kelesdc40be72018-01-23 21:51:34 +0000226 }
227 // Set linkage as ExternalLinkage to avoid erasing unreachable functions.
228 for (Function &F : M)
229 F.setLinkage(GlobalValue::ExternalLinkage);
230 Changed = true;
231 }
232
233 return Changed;
234}