+ if (LI.end()-LI.begin() > 1) {
+ for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i) {
+ if (NumLoops == 0) return Changed;
+ --NumLoops;
+ Changed |= (ExtractLoop(*i) != 0);
+ }
+ } else {
+ // Otherwise there is exactly one top-level loop. If this function is more
+ // than a minimal wrapper around the loop, extract the loop.
+ Loop *TLL = *LI.begin();
+ bool ShouldExtractLoop = false;
+
+ // Extract the loop if the entry block doesn't branch to the loop header.
+ TerminatorInst *EntryTI = F.getEntryBlock().getTerminator();
+ if (!isa<BranchInst>(EntryTI) ||
+ !cast<BranchInst>(EntryTI)->isUnconditional() ||
+ EntryTI->getSuccessor(0) != TLL->getHeader())
+ ShouldExtractLoop = true;
+ else {
+ // Check to see if any exits from the loop are more than just return
+ // blocks.
+ for (unsigned i = 0, e = TLL->getExitBlocks().size(); i != e; ++i)
+ if (!isa<ReturnInst>(TLL->getExitBlocks()[i]->getTerminator())) {
+ ShouldExtractLoop = true;
+ break;
+ }
+ }
+
+ if (ShouldExtractLoop) {
+ if (NumLoops == 0) return Changed;
+ --NumLoops;
+ Changed |= (ExtractLoop(TLL) != 0);
+ } else {
+ // Okay, this function is a minimal container around the specified loop.
+ // If we extract the loop, we will continue to just keep extracting it
+ // infinitely... so don't extract it. However, if the loop contains any
+ // subloops, extract them.
+ for (Loop::iterator i = TLL->begin(), e = TLL->end(); i != e; ++i) {
+ if (NumLoops == 0) return Changed;
+ --NumLoops;
+ Changed |= (ExtractLoop(*i) != 0);
+ }
+ }