17 #define DEBUG_TYPE "loop-extract"
33 STATISTIC(NumExtracted,
"Number of loops extracted");
36 struct LoopExtractor :
public LoopPass {
40 explicit LoopExtractor(
unsigned numLoops = ~0)
57 "Extract loops into new functions",
false,
false)
74 "Extract at most one loop into a new function",
false,
false)
97 bool ShouldExtractLoop =
false;
101 L->
getHeader()->getParent()->getEntryBlock().getTerminator();
102 if (!isa<BranchInst>(EntryTI) ||
103 !cast<BranchInst>(EntryTI)->isUnconditional() ||
105 ShouldExtractLoop =
true;
111 for (
unsigned i = 0, e = ExitBlocks.
size(); i != e; ++i)
112 if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
113 ShouldExtractLoop =
true;
118 if (ShouldExtractLoop) {
125 for (
unsigned i = 0, e = ExitBlocks.
size(); i != e; ++i)
126 if (ExitBlocks[i]->isLandingPad()) {
127 ShouldExtractLoop =
false;
132 if (ShouldExtractLoop) {
133 if (NumLoops == 0)
return Changed;
136 if (Extractor.extractCodeRegion() != 0) {
152 return new SingleLoopExtractor();
160 cl::desc(
"A file containing list of basic blocks to not extract"),
167 class BlockExtractorPass :
public ModulePass {
168 void LoadFile(
const char *Filename);
171 std::vector<BasicBlock*> BlocksToNotExtract;
172 std::vector<std::pair<std::string, std::string> > BlocksToNotExtractByName;
180 bool runOnModule(
Module &M);
186 "Extract Basic Blocks From Module (for bugpoint use)",
193 return new BlockExtractorPass();
196 void BlockExtractorPass::LoadFile(
const char *Filename) {
198 std::ifstream
In(Filename);
200 errs() <<
"WARNING: BlockExtractor couldn't load file '" << Filename
205 std::string FunctionName, BlockName;
208 if (!BlockName.empty())
209 BlocksToNotExtractByName.push_back(
210 std::make_pair(FunctionName, BlockName));
218 void BlockExtractorPass::SplitLandingPadPreds(
Function *
F) {
238 if (!Split)
continue;
245 bool BlockExtractorPass::runOnModule(
Module &M) {
246 std::set<BasicBlock*> TranslatedBlocksToNotExtract;
247 for (
unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
258 TranslatedBlocksToNotExtract.insert(BBI);
261 while (!BlocksToNotExtractByName.empty()) {
266 std::string &
FuncName = BlocksToNotExtractByName.back().first;
267 std::string &BlockName = BlocksToNotExtractByName.back().second;
275 if (BB.
getName() != BlockName)
continue;
277 TranslatedBlocksToNotExtract.insert(BI);
281 BlocksToNotExtractByName.pop_back();
286 std::vector<BasicBlock*> BlocksToExtract;
288 SplitLandingPadPreds(&*F);
290 if (!TranslatedBlocksToNotExtract.count(BB))
291 BlocksToExtract.push_back(BB);
294 for (
unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i) {
296 BlocksToExtractVec.
push_back(BlocksToExtract[i]);
298 dyn_cast<InvokeInst>(BlocksToExtract[i]->getTerminator()))
303 return !BlocksToExtract.
empty();
void push_back(const T &Elt)
static PassRegistry * getPassRegistry()
void initializeLoopExtractorPass(PassRegistry &)
The main container class for the LLVM Intermediate Representation.
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
LoopT * getParentLoop() const
const Function * getParent() const
Return the enclosing method, or null if none.
Pass * createSingleLoopExtractorPass()
BlockT * getHeader() const
StringRef getName() const
ModulePass * createBlockExtractorPass()
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
static error_code advance(T &it, size_t Val)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
ID
LLVM Calling Convention Representation.
bool isLoopSimplifyForm() const
void getExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
static cl::opt< std::string > FuncName("cppfname", cl::desc("Specify the name of the generated function"), cl::value_desc("function name"))
Function * getFunction(StringRef Name) const
LLVM Basic Block Representation.
BasicBlock * getSuccessor(unsigned idx) const
char & BreakCriticalEdgesID
Interval::pred_iterator pred_begin(Interval *I)
Interval::pred_iterator pred_end(Interval *I)
BasicBlockListType::iterator iterator
static void Split(std::vector< std::string > &V, const StringRef S)
BasicBlock * getUnwindDest() const
AnalysisUsage & addRequiredID(const void *ID)
void deleteLoopFromQueue(Loop *L)
Delete loop from the loop queue and loop hierarchy (LoopInfo).
Pass * createLoopExtractorPass()
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
void SplitLandingPadPredecessors(BasicBlock *OrigBB, ArrayRef< BasicBlock * > Preds, const char *Suffix, const char *Suffix2, Pass *P, SmallVectorImpl< BasicBlock * > &NewBBs)
const BasicBlock * getParent() const