32 #define DEBUG_TYPE "loop-extract"
34 STATISTIC(NumExtracted,
"Number of loops extracted");
37 struct LoopExtractor :
public LoopPass {
41 explicit LoopExtractor(
unsigned numLoops = ~0)
58 "Extract loops into new functions",
false,
false)
75 "Extract at most one loop into a new function",
false,
false)
83 if (skipOptnoneFunction(L))
94 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
101 bool ShouldExtractLoop =
false;
105 L->
getHeader()->getParent()->getEntryBlock().getTerminator();
106 if (!isa<BranchInst>(EntryTI) ||
107 !cast<BranchInst>(EntryTI)->isUnconditional() ||
109 ShouldExtractLoop =
true;
115 for (
unsigned i = 0, e = ExitBlocks.
size(); i != e; ++i)
116 if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
117 ShouldExtractLoop =
true;
122 if (ShouldExtractLoop) {
129 for (
unsigned i = 0, e = ExitBlocks.
size(); i != e; ++i)
130 if (ExitBlocks[i]->isLandingPad()) {
131 ShouldExtractLoop =
false;
136 if (ShouldExtractLoop) {
137 if (NumLoops == 0)
return Changed;
140 if (Extractor.extractCodeRegion() !=
nullptr) {
156 return new SingleLoopExtractor();
164 cl::desc(
"A file containing list of basic blocks to not extract"),
171 class BlockExtractorPass :
public ModulePass {
172 void LoadFile(
const char *Filename);
175 std::vector<BasicBlock*> BlocksToNotExtract;
176 std::vector<std::pair<std::string, std::string> > BlocksToNotExtractByName;
184 bool runOnModule(
Module &M)
override;
190 "Extract Basic Blocks From Module (for bugpoint use)",
197 return new BlockExtractorPass();
200 void BlockExtractorPass::LoadFile(
const char *Filename) {
202 std::ifstream
In(Filename);
204 errs() <<
"WARNING: BlockExtractor couldn't load file '" << Filename
209 std::string FunctionName, BlockName;
212 if (!BlockName.empty())
213 BlocksToNotExtractByName.push_back(
214 std::make_pair(FunctionName, BlockName));
222 void BlockExtractorPass::SplitLandingPadPreds(
Function *
F) {
242 if (!Split)
continue;
249 bool BlockExtractorPass::runOnModule(
Module &M) {
250 std::set<BasicBlock*> TranslatedBlocksToNotExtract;
251 for (
unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
262 TranslatedBlocksToNotExtract.insert(BBI);
265 while (!BlocksToNotExtractByName.empty()) {
270 std::string &
FuncName = BlocksToNotExtractByName.back().first;
271 std::string &BlockName = BlocksToNotExtractByName.back().second;
279 if (BB.
getName() != BlockName)
continue;
281 TranslatedBlocksToNotExtract.insert(BI);
285 BlocksToNotExtractByName.pop_back();
290 std::vector<BasicBlock*> BlocksToExtract;
292 SplitLandingPadPreds(&*F);
294 if (!TranslatedBlocksToNotExtract.count(BB))
295 BlocksToExtract.push_back(BB);
298 for (
unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i) {
300 BlocksToExtractVec.
push_back(BlocksToExtract[i]);
302 dyn_cast<InvokeInst>(BlocksToExtract[i]->getTerminator()))
307 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)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
bool isLoopSimplifyForm() const
isLoopSimplifyForm - Return true if the Loop is in the form that the LoopSimplify form transforms loo...
void getExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
getExitBlocks - Return all of the successor blocks of this loop.
static cl::opt< std::string > FuncName("cppfname", cl::desc("Specify the name of the generated function"), cl::value_desc("function name"))
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
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.
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.
Interval::pred_iterator pred_end(Interval *I)
BasicBlockListType::iterator iterator
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.
void deleteLoopFromQueue(Loop *L)
Delete loop from the loop queue and loop hierarchy (LoopInfo).
Pass * createLoopExtractorPass()
createLoopExtractorPass - This pass extracts all natural loops from the program into a function if it...
void SplitLandingPadPredecessors(BasicBlock *OrigBB, ArrayRef< BasicBlock * > Preds, const char *Suffix, const char *Suffix2, SmallVectorImpl< BasicBlock * > &NewBBs, AliasAnalysis *AA=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, bool PreserveLCSSA=false)
SplitLandingPadPredecessors - This method transforms the landing pad, OrigBB, by introducing two new ...
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
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.
FunctionType * getFunctionType() const
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
InvokeInst - Invoke instruction.
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