29 #define DEBUG_TYPE "block-extractor" 31 STATISTIC(NumExtracted,
"Number of basic blocks extracted");
38 cl::desc(
"Erase the existing functions"),
49 &GroupsOfBlocksToExtract) {
51 GroupsOfBlocksToExtract) {
53 NewGroup.
append(GroupOfBlocks.begin(), GroupOfBlocks.end());
64 :
ModulePass(ID), EraseFunctions(EraseFunctions) {
70 MassagedGroupsOfBlocks.
push_back(NewGroup);
72 init(MassagedGroupsOfBlocks);
76 &GroupsOfBlocksToExtract,
78 :
ModulePass(ID), EraseFunctions(EraseFunctions) {
79 init(GroupsOfBlocksToExtract);
83 bool runOnModule(
Module &M)
override;
93 "Extract basic blocks from module",
false,
false)
98 return new BlockExtractor(BlocksToExtract, EraseFunctions);
102 &GroupsOfBlocksToExtract,
103 bool EraseFunctions) {
104 return new BlockExtractor(GroupsOfBlocksToExtract, EraseFunctions);
110 if (ErrOrBuf.getError())
113 auto &Buf = *ErrOrBuf;
115 Buf->getBuffer().split(Lines,
'\n', -1,
117 for (
const auto &Line : Lines) {
119 Line.split(LineSplit,
' ', -1,
121 if (LineSplit.
empty())
123 if (LineSplit.
size()!=2)
124 report_fatal_error(
"Invalid line format, expecting lines like: 'funcname bb1[;bb2..]'");
126 LineSplit[1].split(BBNames,
';', -1,
136 void BlockExtractor::splitLandingPadPreds(
Function &
F) {
139 if (!isa<InvokeInst>(&
I))
149 if (PredBB->isLandingPad() && PredBB != Parent &&
150 isa<InvokeInst>(Parent->getTerminator())) {
165 bool BlockExtractor::runOnModule(
Module &M) {
167 bool Changed =
false;
172 splitLandingPadPreds(F);
177 unsigned NextGroupIdx = GroupsOfBlocks.
size();
178 GroupsOfBlocks.
resize(NextGroupIdx + BlocksByName.
size());
179 for (
const auto &BInfo : BlocksByName) {
183 for (
const auto &BBInfo : BInfo.second) {
189 GroupsOfBlocks[NextGroupIdx].
push_back(&*Res);
195 for (
auto &BBs : GroupsOfBlocks) {
199 if (BB->getParent()->getParent() != &M)
202 << BB->getParent()->getName() <<
":" << BB->getName()
205 if (
const InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
206 BlocksToExtractVec.
push_back(II->getUnwindDest());
214 <<
"' in: " << F->
getName() <<
'\n');
217 << (*BBs.begin())->
getName() <<
"'\n");
const Function & getFunction() const
reference emplace_back(ArgTypes &&... Args)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
A Module instance is used to store all the information related to an LLVM module. ...
void push_back(const T &Elt)
Externally visible function.
STATISTIC(NumFunctions, "Total number of functions")
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...
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...
initializer< Ty > init(const Ty &Val)
LLVM Basic Block Representation.
void deleteBody()
deleteBody - This method deletes the body of the function, and converts the linkage to external...
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...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
pred_range predecessors(BasicBlock *BB)
void setLinkage(LinkageTypes LT)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
LLVM_NODISCARD bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
LLVM_NODISCARD bool empty() const
StringRef getName() const
Return a constant reference to the value's name.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
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)
const BasicBlock * getParent() const