32 #define DEBUG_TYPE "loop-extract"
34 STATISTIC(NumExtracted,
"Number of loops extracted");
37 struct LoopExtractor :
public LoopPass {
41 explicit LoopExtractor(
unsigned numLoops = ~0)
59 "Extract loops into new functions",
false,
false)
76 "Extract at most one loop into a new function",
false,
false)
95 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
96 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
103 bool ShouldExtractLoop =
false;
107 L->
getHeader()->getParent()->getEntryBlock().getTerminator();
108 if (!isa<BranchInst>(EntryTI) ||
109 !cast<BranchInst>(EntryTI)->isUnconditional() ||
111 ShouldExtractLoop =
true;
117 for (
unsigned i = 0, e = ExitBlocks.
size();
i != e; ++
i)
118 if (!isa<ReturnInst>(ExitBlocks[
i]->getTerminator())) {
119 ShouldExtractLoop =
true;
124 if (ShouldExtractLoop) {
131 for (
unsigned i = 0, e = ExitBlocks.
size();
i != e; ++
i)
132 if (ExitBlocks[
i]->isEHPad()) {
133 ShouldExtractLoop =
false;
138 if (ShouldExtractLoop) {
139 if (NumLoops == 0)
return Changed;
142 if (Extractor.extractCodeRegion() !=
nullptr) {
158 return new SingleLoopExtractor();
166 cl::desc(
"A file containing list of basic blocks to not extract"),
173 class BlockExtractorPass :
public ModulePass {
174 void LoadFile(
const char *Filename);
177 std::vector<BasicBlock*> BlocksToNotExtract;
178 std::vector<std::pair<std::string, std::string> > BlocksToNotExtractByName;
186 bool runOnModule(
Module &M)
override;
192 "Extract Basic Blocks From Module (for bugpoint use)",
199 return new BlockExtractorPass();
202 void BlockExtractorPass::LoadFile(
const char *Filename) {
204 std::ifstream
In(Filename);
206 errs() <<
"WARNING: BlockExtractor couldn't load file '" << Filename
211 std::string FunctionName, BlockName;
214 if (!BlockName.empty())
215 BlocksToNotExtractByName.push_back(
216 std::make_pair(FunctionName, BlockName));
224 void BlockExtractorPass::SplitLandingPadPreds(
Function *
F) {
244 if (!Split)
continue;
251 bool BlockExtractorPass::runOnModule(
Module &M) {
255 std::set<BasicBlock*> TranslatedBlocksToNotExtract;
256 for (
unsigned i = 0, e = BlocksToNotExtract.size();
i != e; ++
i) {
267 TranslatedBlocksToNotExtract.insert(&*BBI);
270 while (!BlocksToNotExtractByName.empty()) {
275 std::string &FuncName = BlocksToNotExtractByName.back().first;
276 std::string &BlockName = BlocksToNotExtractByName.back().second;
279 if (F.
getName() != FuncName)
continue;
282 if (BB.
getName() != BlockName)
continue;
284 TranslatedBlocksToNotExtract.insert(&BB);
288 BlocksToNotExtractByName.pop_back();
293 std::vector<BasicBlock*> BlocksToExtract;
295 SplitLandingPadPreds(&F);
297 if (!TranslatedBlocksToNotExtract.count(&BB))
298 BlocksToExtract.push_back(&BB);
301 for (
BasicBlock *BlockToExtract : BlocksToExtract) {
303 BlocksToExtractVec.
push_back(BlockToExtract);
305 dyn_cast<InvokeInst>(BlockToExtract->getTerminator()))
310 return !BlocksToExtract.
empty();
Pass interface - Implemented by all 'passes'.
void push_back(const T &Elt)
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void initializeLoopExtractorPass(PassRegistry &)
STATISTIC(NumFunctions,"Total number of functions")
A Module instance is used to store all the information related to an LLVM module. ...
LoopT * getParentLoop() const
const Function * getParent() const
Return the enclosing method, or null if none.
Pass * createSingleLoopExtractorPass()
createSingleLoopExtractorPass - This pass extracts one natural loop from the program into a function ...
BlockT * getHeader() const
StringRef getName() const
Return a constant reference to the value's name.
ModulePass * createBlockExtractorPass()
createBlockExtractorPass - This pass extracts all blocks (except those specified in the argument list...
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
static void advance(T &it, size_t Val)
bool isLoopSimplifyForm() const
Return true if the Loop is in the form that the LoopSimplify form transforms loops to...
void getExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
Return all of the successor blocks of this loop.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Subclasses of this class are all able to terminate a basic block.
LLVM Basic Block Representation.
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
void SplitLandingPadPredecessors(BasicBlock *OrigBB, ArrayRef< BasicBlock * > Preds, const char *Suffix, const char *Suffix2, SmallVectorImpl< BasicBlock * > &NewBBs, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, bool PreserveLCSSA=false)
This method transforms the landing pad, OrigBB, by introducing two new basic blocks into the function...
char & BreakCriticalEdgesID
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Represent the analysis usage information of a pass.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
Interval::pred_iterator pred_end(Interval *I)
void markAsRemoved(Loop *L)
Update LoopInfo after removing the last backedge from a loop.
BasicBlockListType::iterator iterator
Iterator for intrusive lists based on ilist_node.
BasicBlock * getUnwindDest() const
AnalysisUsage & addRequiredID(const void *ID)
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.
Pass * createLoopExtractorPass()
createLoopExtractorPass - This pass extracts all natural loops from the program into a function if it...
Represents a single loop in the control flow graph.
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
bool isLandingPad() const
Return true if this basic block is a landing pad.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
FunctionType * getFunctionType() const
Returns the FunctionType for me.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
The legacy pass manager's analysis pass to compute loop information.
Legacy analysis pass which computes a DominatorTree.
static void Split(std::vector< std::string > &V, StringRef S)
Split - Splits a string of comma separated items in to a vector of strings.
const BasicBlock * getParent() const