LLVM  10.0.0
BlockExtractor.cpp
Go to the documentation of this file.
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 
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/InitializePasses.h"
19 #include "llvm/Pass.h"
21 #include "llvm/Support/Debug.h"
23 #include "llvm/Transforms/IPO.h"
26 
27 using namespace llvm;
28 
29 #define DEBUG_TYPE "block-extractor"
30 
31 STATISTIC(NumExtracted, "Number of basic blocks extracted");
32 
34  "extract-blocks-file", cl::value_desc("filename"),
35  cl::desc("A file containing list of basic blocks to extract"), cl::Hidden);
36 
37 cl::opt<bool> BlockExtractorEraseFuncs("extract-blocks-erase-funcs",
38  cl::desc("Erase the existing functions"),
39  cl::Hidden);
40 namespace {
41 class BlockExtractor : public ModulePass {
43  bool EraseFunctions;
44  /// Map a function name to groups of blocks.
46  BlocksByName;
47 
49  &GroupsOfBlocksToExtract) {
50  for (const SmallVectorImpl<BasicBlock *> &GroupOfBlocks :
51  GroupsOfBlocksToExtract) {
53  NewGroup.append(GroupOfBlocks.begin(), GroupOfBlocks.end());
54  GroupsOfBlocks.emplace_back(NewGroup);
55  }
56  if (!BlockExtractorFile.empty())
57  loadFile();
58  }
59 
60 public:
61  static char ID;
62  BlockExtractor(const SmallVectorImpl<BasicBlock *> &BlocksToExtract,
63  bool EraseFunctions)
64  : ModulePass(ID), EraseFunctions(EraseFunctions) {
65  // We want one group per element of the input list.
66  SmallVector<SmallVector<BasicBlock *, 16>, 4> MassagedGroupsOfBlocks;
67  for (BasicBlock *BB : BlocksToExtract) {
69  NewGroup.push_back(BB);
70  MassagedGroupsOfBlocks.push_back(NewGroup);
71  }
72  init(MassagedGroupsOfBlocks);
73  }
74 
76  &GroupsOfBlocksToExtract,
77  bool EraseFunctions)
78  : ModulePass(ID), EraseFunctions(EraseFunctions) {
79  init(GroupsOfBlocksToExtract);
80  }
81 
82  BlockExtractor() : BlockExtractor(SmallVector<BasicBlock *, 0>(), false) {}
83  bool runOnModule(Module &M) override;
84 
85 private:
86  void loadFile();
87  void splitLandingPadPreds(Function &F);
88 };
89 } // end anonymous namespace
90 
91 char BlockExtractor::ID = 0;
92 INITIALIZE_PASS(BlockExtractor, "extract-blocks",
93  "Extract basic blocks from module", false, false)
94 
95 ModulePass *llvm::createBlockExtractorPass() { return new BlockExtractor(); }
97  const SmallVectorImpl<BasicBlock *> &BlocksToExtract, bool EraseFunctions) {
98  return new BlockExtractor(BlocksToExtract, EraseFunctions);
99 }
102  &GroupsOfBlocksToExtract,
103  bool EraseFunctions) {
104  return new BlockExtractor(GroupsOfBlocksToExtract, EraseFunctions);
105 }
106 
107 /// Gets all of the blocks specified in the input file.
109  auto ErrOrBuf = MemoryBuffer::getFile(BlockExtractorFile);
110  if (ErrOrBuf.getError())
111  report_fatal_error("BlockExtractor couldn't load the file.");
112  // Read the file.
113  auto &Buf = *ErrOrBuf;
115  Buf->getBuffer().split(Lines, '\n', /*MaxSplit=*/-1,
116  /*KeepEmpty=*/false);
117  for (const auto &Line : Lines) {
118  SmallVector<StringRef, 4> LineSplit;
119  Line.split(LineSplit, ' ', /*MaxSplit=*/-1,
120  /*KeepEmpty=*/false);
121  if (LineSplit.empty())
122  continue;
123  if (LineSplit.size()!=2)
124  report_fatal_error("Invalid line format, expecting lines like: 'funcname bb1[;bb2..]'");
126  LineSplit[1].split(BBNames, ';', /*MaxSplit=*/-1,
127  /*KeepEmpty=*/false);
128  if (BBNames.empty())
129  report_fatal_error("Missing bbs name");
130  BlocksByName.push_back({LineSplit[0], {BBNames.begin(), BBNames.end()}});
131  }
132 }
133 
134 /// Extracts the landing pads to make sure all of them have only one
135 /// predecessor.
136 void 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 
160  SplitLandingPadPredecessors(LPad, Parent, ".1", ".2", NewBBs);
161  }
162  }
163 }
164 
165 bool 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.
177  unsigned NextGroupIdx = GroupsOfBlocks.size();
178  GroupsOfBlocks.resize(NextGroupIdx + BlocksByName.size());
179  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");
183  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;
192  }
193 
194  // 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  }
210  CodeExtractorAnalysisCache CEAC(*BBs[0]->getParent());
211  Function *F = CodeExtractor(BlocksToExtractVec).extractCodeRegion(CEAC);
212  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");
218  }
219 
220  // Erase the functions.
221  if (EraseFunctions || BlockExtractorEraseFuncs) {
222  for (Function *F : Functions) {
223  LLVM_DEBUG(dbgs() << "BlockExtractor: Trying to delete " << F->getName()
224  << "\n");
225  F->deleteBody();
226  }
227  // Set linkage as ExternalLinkage to avoid erasing unreachable functions.
228  for (Function &F : M)
230  Changed = true;
231  }
232 
233  return Changed;
234 }
const Function & getFunction() const
Definition: Function.h:133
Utility class for extracting code into a new function.
Definition: CodeExtractor.h:85
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:641
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
iterator end()
Definition: Function.h:693
Externally visible function.
Definition: GlobalValue.h:48
STATISTIC(NumFunctions, "Total number of functions")
cl::opt< bool > BlockExtractorEraseFuncs("extract-blocks-erase-funcs", cl::desc("Erase the existing functions"), cl::Hidden)
F(f)
static std::unique_ptr< Module > loadFile(const std::string &FileName, LLVMContext &Context)
ModulePass * createBlockExtractorPass()
createBlockExtractorPass - This pass extracts all the specified blocks from the functions in the modu...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
static StringRef getName(Value *V)
void SplitLandingPadPredecessors(BasicBlock *OrigBB, ArrayRef< BasicBlock *> Preds, const char *Suffix, const char *Suffix2, SmallVectorImpl< BasicBlock *> &NewBBs, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, bool PreserveLCSSA=false)
This method transforms the landing pad, OrigBB, by introducing two new basic blocks into the function...
INITIALIZE_PASS(BlockExtractor, "extract-blocks", "Extract basic blocks from module", false, false) ModulePass *llvm
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:435
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
void deleteBody()
deleteBody - This method deletes the body of the function, and converts the linkage to external...
Definition: Function.h:644
A cache for the CodeExtractor analysis.
Definition: CodeExtractor.h:46
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1210
size_t size() const
Definition: SmallVector.h:52
static cl::opt< std::string > BlockExtractorFile("extract-blocks-file", cl::value_desc("filename"), cl::desc("A file containing list of basic blocks to extract"), cl::Hidden)
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
Module.h This file contains the declarations for the Module class.
pred_range predecessors(BasicBlock *BB)
Definition: CFG.h:124
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:454
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:387
Function * extractCodeRegion(const CodeExtractorAnalysisCache &CEAC)
Perform the extraction, returning the new function.
LLVM_NODISCARD bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:181
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:215
#define I(x, y, z)
Definition: MD5.cpp:58
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:222
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
BasicBlock * getUnwindDest() const
static const Function * getParent(const Value *V)
Invoke instruction.
#define LLVM_DEBUG(X)
Definition: Debug.h:122
const BasicBlock * getParent() const
Definition: Instruction.h:66
void resize(size_type N)
Definition: SmallVector.h:344