2018-01-24 05:51:34 +08:00
|
|
|
//===- BlockExtractor.cpp - Extracts blocks into their own functions ------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-01-24 05:51:34 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass extracts the specified basic blocks from the module into their
|
|
|
|
// own functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-10-08 06:15:09 +08:00
|
|
|
#include "llvm/Transforms/IPO/BlockExtractor.h"
|
2018-01-24 05:51:34 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2020-10-08 06:15:09 +08:00
|
|
|
#include "llvm/IR/PassManager.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2018-01-24 05:51:34 +08:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
|
|
|
#include "llvm/Transforms/Utils/CodeExtractor.h"
|
[BlockExtractor] Extend the file format to support the grouping of basic blocks
Prior to this patch, each basic block listed in the extrack-blocks-file
would be extracted to a different function.
This patch adds the support for comma separated list of basic blocks
to form group.
When the region formed by a group is not extractable, e.g., not single
entry, all the blocks of that group are left untouched.
Let us see this new format in action (comments are not part of the
file format):
;; funcName bbName[,bbName...]
foo bb1 ;; Extract bb1 in its own function
foo bb2,bb3 ;; Extract bb2,bb3 in their own function
bar bb1,bb4 ;; Extract bb1,bb4 in their own function
bar bb2 ;; Extract bb2 in its own function
Assuming all regions are extractable, this will create one function and
thus one call per region.
Differential Revision: https://reviews.llvm.org/D60746
llvm-svn: 358701
2019-04-19 02:28:30 +08:00
|
|
|
|
2018-01-24 05:51:34 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "block-extractor"
|
|
|
|
|
|
|
|
STATISTIC(NumExtracted, "Number of basic blocks extracted");
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
cl::opt<bool> BlockExtractorEraseFuncs("extract-blocks-erase-funcs",
|
|
|
|
cl::desc("Erase the existing functions"),
|
|
|
|
cl::Hidden);
|
|
|
|
namespace {
|
2020-10-08 06:15:09 +08:00
|
|
|
class BlockExtractor {
|
|
|
|
public:
|
|
|
|
BlockExtractor(bool EraseFunctions) : EraseFunctions(EraseFunctions) {}
|
|
|
|
bool runOnModule(Module &M);
|
2019-04-30 00:14:02 +08:00
|
|
|
void init(const SmallVectorImpl<SmallVector<BasicBlock *, 16>>
|
|
|
|
&GroupsOfBlocksToExtract) {
|
|
|
|
for (const SmallVectorImpl<BasicBlock *> &GroupOfBlocks :
|
|
|
|
GroupsOfBlocksToExtract) {
|
|
|
|
SmallVector<BasicBlock *, 16> NewGroup;
|
|
|
|
NewGroup.append(GroupOfBlocks.begin(), GroupOfBlocks.end());
|
|
|
|
GroupsOfBlocks.emplace_back(NewGroup);
|
|
|
|
}
|
|
|
|
if (!BlockExtractorFile.empty())
|
|
|
|
loadFile();
|
|
|
|
}
|
|
|
|
|
2020-10-08 06:15:09 +08:00
|
|
|
private:
|
|
|
|
SmallVector<SmallVector<BasicBlock *, 16>, 4> GroupsOfBlocks;
|
|
|
|
bool EraseFunctions;
|
|
|
|
/// Map a function name to groups of blocks.
|
|
|
|
SmallVector<std::pair<std::string, SmallVector<std::string, 4>>, 4>
|
|
|
|
BlocksByName;
|
|
|
|
|
|
|
|
void loadFile();
|
|
|
|
void splitLandingPadPreds(Function &F);
|
|
|
|
};
|
|
|
|
|
|
|
|
class BlockExtractorLegacyPass : public ModulePass {
|
|
|
|
BlockExtractor BE;
|
|
|
|
bool runOnModule(Module &M) override;
|
|
|
|
|
2018-01-24 05:51:34 +08:00
|
|
|
public:
|
|
|
|
static char ID;
|
2020-10-08 06:15:09 +08:00
|
|
|
BlockExtractorLegacyPass(const SmallVectorImpl<BasicBlock *> &BlocksToExtract,
|
|
|
|
bool EraseFunctions)
|
|
|
|
: ModulePass(ID), BE(EraseFunctions) {
|
[BlockExtractor] Extend the file format to support the grouping of basic blocks
Prior to this patch, each basic block listed in the extrack-blocks-file
would be extracted to a different function.
This patch adds the support for comma separated list of basic blocks
to form group.
When the region formed by a group is not extractable, e.g., not single
entry, all the blocks of that group are left untouched.
Let us see this new format in action (comments are not part of the
file format):
;; funcName bbName[,bbName...]
foo bb1 ;; Extract bb1 in its own function
foo bb2,bb3 ;; Extract bb2,bb3 in their own function
bar bb1,bb4 ;; Extract bb1,bb4 in their own function
bar bb2 ;; Extract bb2 in its own function
Assuming all regions are extractable, this will create one function and
thus one call per region.
Differential Revision: https://reviews.llvm.org/D60746
llvm-svn: 358701
2019-04-19 02:28:30 +08:00
|
|
|
// We want one group per element of the input list.
|
2019-04-30 00:14:02 +08:00
|
|
|
SmallVector<SmallVector<BasicBlock *, 16>, 4> MassagedGroupsOfBlocks;
|
[BlockExtractor] Extend the file format to support the grouping of basic blocks
Prior to this patch, each basic block listed in the extrack-blocks-file
would be extracted to a different function.
This patch adds the support for comma separated list of basic blocks
to form group.
When the region formed by a group is not extractable, e.g., not single
entry, all the blocks of that group are left untouched.
Let us see this new format in action (comments are not part of the
file format):
;; funcName bbName[,bbName...]
foo bb1 ;; Extract bb1 in its own function
foo bb2,bb3 ;; Extract bb2,bb3 in their own function
bar bb1,bb4 ;; Extract bb1,bb4 in their own function
bar bb2 ;; Extract bb2 in its own function
Assuming all regions are extractable, this will create one function and
thus one call per region.
Differential Revision: https://reviews.llvm.org/D60746
llvm-svn: 358701
2019-04-19 02:28:30 +08:00
|
|
|
for (BasicBlock *BB : BlocksToExtract) {
|
|
|
|
SmallVector<BasicBlock *, 16> NewGroup;
|
|
|
|
NewGroup.push_back(BB);
|
2019-04-30 00:14:02 +08:00
|
|
|
MassagedGroupsOfBlocks.push_back(NewGroup);
|
[BlockExtractor] Extend the file format to support the grouping of basic blocks
Prior to this patch, each basic block listed in the extrack-blocks-file
would be extracted to a different function.
This patch adds the support for comma separated list of basic blocks
to form group.
When the region formed by a group is not extractable, e.g., not single
entry, all the blocks of that group are left untouched.
Let us see this new format in action (comments are not part of the
file format):
;; funcName bbName[,bbName...]
foo bb1 ;; Extract bb1 in its own function
foo bb2,bb3 ;; Extract bb2,bb3 in their own function
bar bb1,bb4 ;; Extract bb1,bb4 in their own function
bar bb2 ;; Extract bb2 in its own function
Assuming all regions are extractable, this will create one function and
thus one call per region.
Differential Revision: https://reviews.llvm.org/D60746
llvm-svn: 358701
2019-04-19 02:28:30 +08:00
|
|
|
}
|
2020-10-08 06:15:09 +08:00
|
|
|
BE.init(MassagedGroupsOfBlocks);
|
2018-01-24 05:51:34 +08:00
|
|
|
}
|
2019-04-30 00:14:02 +08:00
|
|
|
|
2020-10-08 06:15:09 +08:00
|
|
|
BlockExtractorLegacyPass(const SmallVectorImpl<SmallVector<BasicBlock *, 16>>
|
|
|
|
&GroupsOfBlocksToExtract,
|
|
|
|
bool EraseFunctions)
|
|
|
|
: ModulePass(ID), BE(EraseFunctions) {
|
|
|
|
BE.init(GroupsOfBlocksToExtract);
|
2019-04-30 00:14:02 +08:00
|
|
|
}
|
|
|
|
|
2020-10-08 06:15:09 +08:00
|
|
|
BlockExtractorLegacyPass()
|
|
|
|
: BlockExtractorLegacyPass(SmallVector<BasicBlock *, 0>(), false) {}
|
2018-01-24 05:51:34 +08:00
|
|
|
};
|
2020-10-08 06:15:09 +08:00
|
|
|
|
2018-01-24 05:51:34 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2020-10-08 06:15:09 +08:00
|
|
|
char BlockExtractorLegacyPass::ID = 0;
|
|
|
|
INITIALIZE_PASS(BlockExtractorLegacyPass, "extract-blocks",
|
2018-01-24 05:51:34 +08:00
|
|
|
"Extract basic blocks from module", false, false)
|
|
|
|
|
2020-10-08 06:15:09 +08:00
|
|
|
ModulePass *llvm::createBlockExtractorPass() {
|
|
|
|
return new BlockExtractorLegacyPass();
|
|
|
|
}
|
2018-01-24 05:51:34 +08:00
|
|
|
ModulePass *llvm::createBlockExtractorPass(
|
|
|
|
const SmallVectorImpl<BasicBlock *> &BlocksToExtract, bool EraseFunctions) {
|
2020-10-08 06:15:09 +08:00
|
|
|
return new BlockExtractorLegacyPass(BlocksToExtract, EraseFunctions);
|
2018-01-24 05:51:34 +08:00
|
|
|
}
|
2019-04-30 00:14:02 +08:00
|
|
|
ModulePass *llvm::createBlockExtractorPass(
|
|
|
|
const SmallVectorImpl<SmallVector<BasicBlock *, 16>>
|
|
|
|
&GroupsOfBlocksToExtract,
|
|
|
|
bool EraseFunctions) {
|
2020-10-08 06:15:09 +08:00
|
|
|
return new BlockExtractorLegacyPass(GroupsOfBlocksToExtract, EraseFunctions);
|
2019-04-30 00:14:02 +08:00
|
|
|
}
|
2018-01-24 05:51:34 +08:00
|
|
|
|
|
|
|
/// Gets all of the blocks specified in the input file.
|
|
|
|
void BlockExtractor::loadFile() {
|
|
|
|
auto ErrOrBuf = MemoryBuffer::getFile(BlockExtractorFile);
|
2018-01-24 06:24:34 +08:00
|
|
|
if (ErrOrBuf.getError())
|
2018-01-24 05:51:34 +08:00
|
|
|
report_fatal_error("BlockExtractor couldn't load the file.");
|
|
|
|
// Read the file.
|
|
|
|
auto &Buf = *ErrOrBuf;
|
|
|
|
SmallVector<StringRef, 16> Lines;
|
|
|
|
Buf->getBuffer().split(Lines, '\n', /*MaxSplit=*/-1,
|
|
|
|
/*KeepEmpty=*/false);
|
|
|
|
for (const auto &Line : Lines) {
|
[BlockExtractor] Extend the file format to support the grouping of basic blocks
Prior to this patch, each basic block listed in the extrack-blocks-file
would be extracted to a different function.
This patch adds the support for comma separated list of basic blocks
to form group.
When the region formed by a group is not extractable, e.g., not single
entry, all the blocks of that group are left untouched.
Let us see this new format in action (comments are not part of the
file format):
;; funcName bbName[,bbName...]
foo bb1 ;; Extract bb1 in its own function
foo bb2,bb3 ;; Extract bb2,bb3 in their own function
bar bb1,bb4 ;; Extract bb1,bb4 in their own function
bar bb2 ;; Extract bb2 in its own function
Assuming all regions are extractable, this will create one function and
thus one call per region.
Differential Revision: https://reviews.llvm.org/D60746
llvm-svn: 358701
2019-04-19 02:28:30 +08:00
|
|
|
SmallVector<StringRef, 4> LineSplit;
|
|
|
|
Line.split(LineSplit, ' ', /*MaxSplit=*/-1,
|
|
|
|
/*KeepEmpty=*/false);
|
|
|
|
if (LineSplit.empty())
|
|
|
|
continue;
|
2019-08-20 22:46:02 +08:00
|
|
|
if (LineSplit.size()!=2)
|
|
|
|
report_fatal_error("Invalid line format, expecting lines like: 'funcname bb1[;bb2..]'");
|
[BlockExtractor] Extend the file format to support the grouping of basic blocks
Prior to this patch, each basic block listed in the extrack-blocks-file
would be extracted to a different function.
This patch adds the support for comma separated list of basic blocks
to form group.
When the region formed by a group is not extractable, e.g., not single
entry, all the blocks of that group are left untouched.
Let us see this new format in action (comments are not part of the
file format):
;; funcName bbName[,bbName...]
foo bb1 ;; Extract bb1 in its own function
foo bb2,bb3 ;; Extract bb2,bb3 in their own function
bar bb1,bb4 ;; Extract bb1,bb4 in their own function
bar bb2 ;; Extract bb2 in its own function
Assuming all regions are extractable, this will create one function and
thus one call per region.
Differential Revision: https://reviews.llvm.org/D60746
llvm-svn: 358701
2019-04-19 02:28:30 +08:00
|
|
|
SmallVector<StringRef, 4> BBNames;
|
2019-04-30 00:14:00 +08:00
|
|
|
LineSplit[1].split(BBNames, ';', /*MaxSplit=*/-1,
|
[BlockExtractor] Extend the file format to support the grouping of basic blocks
Prior to this patch, each basic block listed in the extrack-blocks-file
would be extracted to a different function.
This patch adds the support for comma separated list of basic blocks
to form group.
When the region formed by a group is not extractable, e.g., not single
entry, all the blocks of that group are left untouched.
Let us see this new format in action (comments are not part of the
file format):
;; funcName bbName[,bbName...]
foo bb1 ;; Extract bb1 in its own function
foo bb2,bb3 ;; Extract bb2,bb3 in their own function
bar bb1,bb4 ;; Extract bb1,bb4 in their own function
bar bb2 ;; Extract bb2 in its own function
Assuming all regions are extractable, this will create one function and
thus one call per region.
Differential Revision: https://reviews.llvm.org/D60746
llvm-svn: 358701
2019-04-19 02:28:30 +08:00
|
|
|
/*KeepEmpty=*/false);
|
|
|
|
if (BBNames.empty())
|
|
|
|
report_fatal_error("Missing bbs name");
|
2020-01-29 03:23:46 +08:00
|
|
|
BlocksByName.push_back(
|
|
|
|
{std::string(LineSplit[0]), {BBNames.begin(), BBNames.end()}});
|
2018-01-24 05:51:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extracts the landing pads to make sure all of them have only one
|
|
|
|
/// predecessor.
|
|
|
|
void BlockExtractor::splitLandingPadPreds(Function &F) {
|
|
|
|
for (BasicBlock &BB : F) {
|
|
|
|
for (Instruction &I : BB) {
|
|
|
|
if (!isa<InvokeInst>(&I))
|
|
|
|
continue;
|
|
|
|
InvokeInst *II = cast<InvokeInst>(&I);
|
|
|
|
BasicBlock *Parent = II->getParent();
|
|
|
|
BasicBlock *LPad = II->getUnwindDest();
|
|
|
|
|
|
|
|
// Look through the landing pad's predecessors. If one of them ends in an
|
|
|
|
// 'invoke', then we want to split the landing pad.
|
|
|
|
bool Split = false;
|
|
|
|
for (auto PredBB : predecessors(LPad)) {
|
|
|
|
if (PredBB->isLandingPad() && PredBB != Parent &&
|
|
|
|
isa<InvokeInst>(Parent->getTerminator())) {
|
|
|
|
Split = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Split)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SmallVector<BasicBlock *, 2> NewBBs;
|
|
|
|
SplitLandingPadPredecessors(LPad, Parent, ".1", ".2", NewBBs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BlockExtractor::runOnModule(Module &M) {
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// Get all the functions.
|
|
|
|
SmallVector<Function *, 4> Functions;
|
|
|
|
for (Function &F : M) {
|
|
|
|
splitLandingPadPreds(F);
|
|
|
|
Functions.push_back(&F);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all the blocks specified in the input file.
|
[BlockExtractor] Extend the file format to support the grouping of basic blocks
Prior to this patch, each basic block listed in the extrack-blocks-file
would be extracted to a different function.
This patch adds the support for comma separated list of basic blocks
to form group.
When the region formed by a group is not extractable, e.g., not single
entry, all the blocks of that group are left untouched.
Let us see this new format in action (comments are not part of the
file format):
;; funcName bbName[,bbName...]
foo bb1 ;; Extract bb1 in its own function
foo bb2,bb3 ;; Extract bb2,bb3 in their own function
bar bb1,bb4 ;; Extract bb1,bb4 in their own function
bar bb2 ;; Extract bb2 in its own function
Assuming all regions are extractable, this will create one function and
thus one call per region.
Differential Revision: https://reviews.llvm.org/D60746
llvm-svn: 358701
2019-04-19 02:28:30 +08:00
|
|
|
unsigned NextGroupIdx = GroupsOfBlocks.size();
|
|
|
|
GroupsOfBlocks.resize(NextGroupIdx + BlocksByName.size());
|
2018-01-24 05:51:34 +08:00
|
|
|
for (const auto &BInfo : BlocksByName) {
|
|
|
|
Function *F = M.getFunction(BInfo.first);
|
|
|
|
if (!F)
|
|
|
|
report_fatal_error("Invalid function name specified in the input file");
|
[BlockExtractor] Extend the file format to support the grouping of basic blocks
Prior to this patch, each basic block listed in the extrack-blocks-file
would be extracted to a different function.
This patch adds the support for comma separated list of basic blocks
to form group.
When the region formed by a group is not extractable, e.g., not single
entry, all the blocks of that group are left untouched.
Let us see this new format in action (comments are not part of the
file format):
;; funcName bbName[,bbName...]
foo bb1 ;; Extract bb1 in its own function
foo bb2,bb3 ;; Extract bb2,bb3 in their own function
bar bb1,bb4 ;; Extract bb1,bb4 in their own function
bar bb2 ;; Extract bb2 in its own function
Assuming all regions are extractable, this will create one function and
thus one call per region.
Differential Revision: https://reviews.llvm.org/D60746
llvm-svn: 358701
2019-04-19 02:28:30 +08:00
|
|
|
for (const auto &BBInfo : BInfo.second) {
|
|
|
|
auto Res = llvm::find_if(*F, [&](const BasicBlock &BB) {
|
|
|
|
return BB.getName().equals(BBInfo);
|
|
|
|
});
|
|
|
|
if (Res == F->end())
|
|
|
|
report_fatal_error("Invalid block name specified in the input file");
|
|
|
|
GroupsOfBlocks[NextGroupIdx].push_back(&*Res);
|
|
|
|
}
|
|
|
|
++NextGroupIdx;
|
2018-01-24 05:51:34 +08:00
|
|
|
}
|
|
|
|
|
[BlockExtractor] Extend the file format to support the grouping of basic blocks
Prior to this patch, each basic block listed in the extrack-blocks-file
would be extracted to a different function.
This patch adds the support for comma separated list of basic blocks
to form group.
When the region formed by a group is not extractable, e.g., not single
entry, all the blocks of that group are left untouched.
Let us see this new format in action (comments are not part of the
file format):
;; funcName bbName[,bbName...]
foo bb1 ;; Extract bb1 in its own function
foo bb2,bb3 ;; Extract bb2,bb3 in their own function
bar bb1,bb4 ;; Extract bb1,bb4 in their own function
bar bb2 ;; Extract bb2 in its own function
Assuming all regions are extractable, this will create one function and
thus one call per region.
Differential Revision: https://reviews.llvm.org/D60746
llvm-svn: 358701
2019-04-19 02:28:30 +08:00
|
|
|
// Extract each group of basic blocks.
|
|
|
|
for (auto &BBs : GroupsOfBlocks) {
|
|
|
|
SmallVector<BasicBlock *, 32> BlocksToExtractVec;
|
|
|
|
for (BasicBlock *BB : BBs) {
|
|
|
|
// Check if the module contains BB.
|
|
|
|
if (BB->getParent()->getParent() != &M)
|
|
|
|
report_fatal_error("Invalid basic block");
|
|
|
|
LLVM_DEBUG(dbgs() << "BlockExtractor: Extracting "
|
|
|
|
<< BB->getParent()->getName() << ":" << BB->getName()
|
|
|
|
<< "\n");
|
|
|
|
BlocksToExtractVec.push_back(BB);
|
|
|
|
if (const InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator()))
|
|
|
|
BlocksToExtractVec.push_back(II->getUnwindDest());
|
|
|
|
++NumExtracted;
|
|
|
|
Changed = true;
|
|
|
|
}
|
2019-10-09 01:17:51 +08:00
|
|
|
CodeExtractorAnalysisCache CEAC(*BBs[0]->getParent());
|
|
|
|
Function *F = CodeExtractor(BlocksToExtractVec).extractCodeRegion(CEAC);
|
[BlockExtractor] Extend the file format to support the grouping of basic blocks
Prior to this patch, each basic block listed in the extrack-blocks-file
would be extracted to a different function.
This patch adds the support for comma separated list of basic blocks
to form group.
When the region formed by a group is not extractable, e.g., not single
entry, all the blocks of that group are left untouched.
Let us see this new format in action (comments are not part of the
file format):
;; funcName bbName[,bbName...]
foo bb1 ;; Extract bb1 in its own function
foo bb2,bb3 ;; Extract bb2,bb3 in their own function
bar bb1,bb4 ;; Extract bb1,bb4 in their own function
bar bb2 ;; Extract bb2 in its own function
Assuming all regions are extractable, this will create one function and
thus one call per region.
Differential Revision: https://reviews.llvm.org/D60746
llvm-svn: 358701
2019-04-19 02:28:30 +08:00
|
|
|
if (F)
|
|
|
|
LLVM_DEBUG(dbgs() << "Extracted group '" << (*BBs.begin())->getName()
|
|
|
|
<< "' in: " << F->getName() << '\n');
|
|
|
|
else
|
|
|
|
LLVM_DEBUG(dbgs() << "Failed to extract for group '"
|
|
|
|
<< (*BBs.begin())->getName() << "'\n");
|
2018-01-24 05:51:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Erase the functions.
|
|
|
|
if (EraseFunctions || BlockExtractorEraseFuncs) {
|
|
|
|
for (Function *F : Functions) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "BlockExtractor: Trying to delete " << F->getName()
|
|
|
|
<< "\n");
|
2018-03-13 06:28:18 +08:00
|
|
|
F->deleteBody();
|
2018-01-24 05:51:34 +08:00
|
|
|
}
|
|
|
|
// Set linkage as ExternalLinkage to avoid erasing unreachable functions.
|
|
|
|
for (Function &F : M)
|
|
|
|
F.setLinkage(GlobalValue::ExternalLinkage);
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
2020-10-08 06:15:09 +08:00
|
|
|
|
|
|
|
bool BlockExtractorLegacyPass::runOnModule(Module &M) {
|
|
|
|
return BE.runOnModule(M);
|
|
|
|
}
|
|
|
|
|
|
|
|
PreservedAnalyses BlockExtractorPass::run(Module &M,
|
|
|
|
ModuleAnalysisManager &AM) {
|
|
|
|
BlockExtractor BE(false);
|
|
|
|
BE.init(SmallVector<SmallVector<BasicBlock *, 16>, 0>());
|
|
|
|
return BE.runOnModule(M) ? PreservedAnalyses::none()
|
|
|
|
: PreservedAnalyses::all();
|
|
|
|
}
|