LLVM  9.0.0svn
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/Pass.h"
20 #include "llvm/Support/Debug.h"
22 #include "llvm/Transforms/IPO.h"
25 
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "block-extractor"
29 
30 STATISTIC(NumExtracted, "Number of basic blocks extracted");
31 
33  "extract-blocks-file", cl::value_desc("filename"),
34  cl::desc("A file containing list of basic blocks to extract"), cl::Hidden);
35 
36 cl::opt<bool> BlockExtractorEraseFuncs("extract-blocks-erase-funcs",
37  cl::desc("Erase the existing functions"),
38  cl::Hidden);
39 namespace {
40 class BlockExtractor : public ModulePass {
42  bool EraseFunctions;
43  /// Map a function name to groups of blocks.
45  BlocksByName;
46 
47 public:
48  static char ID;
49  BlockExtractor(const SmallVectorImpl<BasicBlock *> &BlocksToExtract,
50  bool EraseFunctions)
51  : ModulePass(ID), EraseFunctions(EraseFunctions) {
52  // We want one group per element of the input list.
53  for (BasicBlock *BB : BlocksToExtract) {
55  NewGroup.push_back(BB);
56  GroupsOfBlocks.push_back(NewGroup);
57  }
58  if (!BlockExtractorFile.empty())
59  loadFile();
60  }
61  BlockExtractor() : BlockExtractor(SmallVector<BasicBlock *, 0>(), false) {}
62  bool runOnModule(Module &M) override;
63 
64 private:
65  void loadFile();
66  void splitLandingPadPreds(Function &F);
67 };
68 } // end anonymous namespace
69 
70 char BlockExtractor::ID = 0;
71 INITIALIZE_PASS(BlockExtractor, "extract-blocks",
72  "Extract basic blocks from module", false, false)
73 
74 ModulePass *llvm::createBlockExtractorPass() { return new BlockExtractor(); }
76  const SmallVectorImpl<BasicBlock *> &BlocksToExtract, bool EraseFunctions) {
77  return new BlockExtractor(BlocksToExtract, EraseFunctions);
78 }
79 
80 /// Gets all of the blocks specified in the input file.
83  if (ErrOrBuf.getError())
84  report_fatal_error("BlockExtractor couldn't load the file.");
85  // Read the file.
86  auto &Buf = *ErrOrBuf;
88  Buf->getBuffer().split(Lines, '\n', /*MaxSplit=*/-1,
89  /*KeepEmpty=*/false);
90  for (const auto &Line : Lines) {
91  SmallVector<StringRef, 4> LineSplit;
92  Line.split(LineSplit, ' ', /*MaxSplit=*/-1,
93  /*KeepEmpty=*/false);
94  if (LineSplit.empty())
95  continue;
97  LineSplit[1].split(BBNames, ',', /*MaxSplit=*/-1,
98  /*KeepEmpty=*/false);
99  if (BBNames.empty())
100  report_fatal_error("Missing bbs name");
101  BlocksByName.push_back({LineSplit[0], {BBNames.begin(), BBNames.end()}});
102  }
103 }
104 
105 /// Extracts the landing pads to make sure all of them have only one
106 /// predecessor.
107 void BlockExtractor::splitLandingPadPreds(Function &F) {
108  for (BasicBlock &BB : F) {
109  for (Instruction &I : BB) {
110  if (!isa<InvokeInst>(&I))
111  continue;
112  InvokeInst *II = cast<InvokeInst>(&I);
113  BasicBlock *Parent = II->getParent();
114  BasicBlock *LPad = II->getUnwindDest();
115 
116  // Look through the landing pad's predecessors. If one of them ends in an
117  // 'invoke', then we want to split the landing pad.
118  bool Split = false;
119  for (auto PredBB : predecessors(LPad)) {
120  if (PredBB->isLandingPad() && PredBB != Parent &&
121  isa<InvokeInst>(Parent->getTerminator())) {
122  Split = true;
123  break;
124  }
125  }
126 
127  if (!Split)
128  continue;
129 
131  SplitLandingPadPredecessors(LPad, Parent, ".1", ".2", NewBBs);
132  }
133  }
134 }
135 
136 bool BlockExtractor::runOnModule(Module &M) {
137 
138  bool Changed = false;
139 
140  // Get all the functions.
141  SmallVector<Function *, 4> Functions;
142  for (Function &F : M) {
143  splitLandingPadPreds(F);
144  Functions.push_back(&F);
145  }
146 
147  // Get all the blocks specified in the input file.
148  unsigned NextGroupIdx = GroupsOfBlocks.size();
149  GroupsOfBlocks.resize(NextGroupIdx + BlocksByName.size());
150  for (const auto &BInfo : BlocksByName) {
151  Function *F = M.getFunction(BInfo.first);
152  if (!F)
153  report_fatal_error("Invalid function name specified in the input file");
154  for (const auto &BBInfo : BInfo.second) {
155  auto Res = llvm::find_if(*F, [&](const BasicBlock &BB) {
156  return BB.getName().equals(BBInfo);
157  });
158  if (Res == F->end())
159  report_fatal_error("Invalid block name specified in the input file");
160  GroupsOfBlocks[NextGroupIdx].push_back(&*Res);
161  }
162  ++NextGroupIdx;
163  }
164 
165  // Extract each group of basic blocks.
166  for (auto &BBs : GroupsOfBlocks) {
167  SmallVector<BasicBlock *, 32> BlocksToExtractVec;
168  for (BasicBlock *BB : BBs) {
169  // Check if the module contains BB.
170  if (BB->getParent()->getParent() != &M)
171  report_fatal_error("Invalid basic block");
172  LLVM_DEBUG(dbgs() << "BlockExtractor: Extracting "
173  << BB->getParent()->getName() << ":" << BB->getName()
174  << "\n");
175  BlocksToExtractVec.push_back(BB);
176  if (const InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
177  BlocksToExtractVec.push_back(II->getUnwindDest());
178  ++NumExtracted;
179  Changed = true;
180  }
181  Function *F = CodeExtractor(BlocksToExtractVec).extractCodeRegion();
182  if (F)
183  LLVM_DEBUG(dbgs() << "Extracted group '" << (*BBs.begin())->getName()
184  << "' in: " << F->getName() << '\n');
185  else
186  LLVM_DEBUG(dbgs() << "Failed to extract for group '"
187  << (*BBs.begin())->getName() << "'\n");
188  }
189 
190  // Erase the functions.
191  if (EraseFunctions || BlockExtractorEraseFuncs) {
192  for (Function *F : Functions) {
193  LLVM_DEBUG(dbgs() << "BlockExtractor: Trying to delete " << F->getName()
194  << "\n");
195  F->deleteBody();
196  }
197  // Set linkage as ExternalLinkage to avoid erasing unreachable functions.
198  for (Function &F : M)
200  Changed = true;
201  }
202 
203  return Changed;
204 }
const Function & getFunction() const
Definition: Function.h:133
Utility class for extracting code into a new function.
Definition: CodeExtractor.h:52
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:139
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:65
iterator end()
Definition: Function.h:660
Function * extractCodeRegion()
Perform the extraction, returning the new function.
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...
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
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:611
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:1213
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:841
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:444
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
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:160
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:214
#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:224
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
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