Merge the enhancements from LoopUnroll's FoldBlockIntoPredecessor into
MergeBlockIntoPredecessor. This makes SimplifyCFG slightly more aggressive, and makes it unnecessary for LoopUnroll to have its own copy of this code. llvm-svn: 85667
This commit is contained in:
parent
bff4e7d180
commit
041e2dbad1
|
@ -43,9 +43,11 @@ void FoldSingleEntryPHINodes(BasicBlock *BB);
|
||||||
/// it is ultimately unused or if it reaches an unused cycle.
|
/// it is ultimately unused or if it reaches an unused cycle.
|
||||||
void DeleteDeadPHIs(BasicBlock *BB);
|
void DeleteDeadPHIs(BasicBlock *BB);
|
||||||
|
|
||||||
/// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
|
/// MergeBlockIntoPredecessor - Folds a basic block into its predecessor if it
|
||||||
/// if possible. The return value indicates success or failure.
|
/// only has one predecessor, and that predecessor only has one successor.
|
||||||
bool MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P = 0);
|
/// If a Pass is given, the LoopInfo and DominatorTree analyses will be kept
|
||||||
|
/// current. Returns the combined block, or null if no merging was performed.
|
||||||
|
BasicBlock *MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P = 0);
|
||||||
|
|
||||||
// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
|
// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
|
||||||
// with a value, then remove and delete the original instruction.
|
// with a value, then remove and delete the original instruction.
|
||||||
|
|
|
@ -95,53 +95,27 @@ void llvm::DeleteDeadPHIs(BasicBlock *BB) {
|
||||||
RecursivelyDeleteDeadPHINode(PN);
|
RecursivelyDeleteDeadPHINode(PN);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
|
/// MergeBlockIntoPredecessor - Folds a basic block into its predecessor if it
|
||||||
/// if possible. The return value indicates success or failure.
|
/// only has one predecessor, and that predecessor only has one successor.
|
||||||
bool llvm::MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P) {
|
/// If a Pass is given, the LoopInfo and DominatorTree analyses will be kept
|
||||||
pred_iterator PI(pred_begin(BB)), PE(pred_end(BB));
|
/// current. Returns the combined block, or null if no merging was performed.
|
||||||
// Can't merge the entry block.
|
BasicBlock *llvm::MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P) {
|
||||||
if (pred_begin(BB) == pred_end(BB)) return false;
|
// Don't merge if the block has multiple predecessors.
|
||||||
|
BasicBlock *PredBB = BB->getSinglePredecessor();
|
||||||
BasicBlock *PredBB = *PI++;
|
if (!PredBB) return 0;
|
||||||
for (; PI != PE; ++PI) // Search all predecessors, see if they are all same
|
// Don't merge if the predecessor has multiple successors.
|
||||||
if (*PI != PredBB) {
|
if (PredBB->getTerminator()->getNumSuccessors() != 1) return 0;
|
||||||
PredBB = 0; // There are multiple different predecessors...
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Can't merge if there are multiple predecessors.
|
|
||||||
if (!PredBB) return false;
|
|
||||||
// Don't break self-loops.
|
// Don't break self-loops.
|
||||||
if (PredBB == BB) return false;
|
if (PredBB == BB) return 0;
|
||||||
// Don't break invokes.
|
// Don't break invokes.
|
||||||
if (isa<InvokeInst>(PredBB->getTerminator())) return false;
|
if (isa<InvokeInst>(PredBB->getTerminator())) return 0;
|
||||||
|
|
||||||
succ_iterator SI(succ_begin(PredBB)), SE(succ_end(PredBB));
|
// Resolve any PHI nodes at the start of the block. They are all
|
||||||
BasicBlock* OnlySucc = BB;
|
// guaranteed to have exactly one entry if they exist, unless there are
|
||||||
for (; SI != SE; ++SI)
|
// multiple duplicate (but guaranteed to be equal) entries for the
|
||||||
if (*SI != OnlySucc) {
|
// incoming edges. This occurs when there are multiple edges from
|
||||||
OnlySucc = 0; // There are multiple distinct successors!
|
// PredBB to BB.
|
||||||
break;
|
FoldSingleEntryPHINodes(BB);
|
||||||
}
|
|
||||||
|
|
||||||
// Can't merge if there are multiple successors.
|
|
||||||
if (!OnlySucc) return false;
|
|
||||||
|
|
||||||
// Can't merge if there is PHI loop.
|
|
||||||
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI) {
|
|
||||||
if (PHINode *PN = dyn_cast<PHINode>(BI)) {
|
|
||||||
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
|
|
||||||
if (PN->getIncomingValue(i) == PN)
|
|
||||||
return false;
|
|
||||||
} else
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Begin by getting rid of unneeded PHIs.
|
|
||||||
while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) {
|
|
||||||
PN->replaceAllUsesWith(PN->getIncomingValue(0));
|
|
||||||
BB->getInstList().pop_front(); // Delete the phi node...
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete the unconditional branch from the predecessor...
|
// Delete the unconditional branch from the predecessor...
|
||||||
PredBB->getInstList().pop_back();
|
PredBB->getInstList().pop_back();
|
||||||
|
@ -153,7 +127,7 @@ bool llvm::MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P) {
|
||||||
// source...
|
// source...
|
||||||
BB->replaceAllUsesWith(PredBB);
|
BB->replaceAllUsesWith(PredBB);
|
||||||
|
|
||||||
// Inherit predecessors name if it exists.
|
// If the predecessor doesn't have a name, take the successor's name.
|
||||||
if (!PredBB->hasName())
|
if (!PredBB->hasName())
|
||||||
PredBB->takeName(BB);
|
PredBB->takeName(BB);
|
||||||
|
|
||||||
|
@ -172,12 +146,14 @@ bool llvm::MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P) {
|
||||||
DT->eraseNode(BB);
|
DT->eraseNode(BB);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Notify LoopInfo that the block is removed.
|
||||||
|
if (LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>())
|
||||||
|
LI->removeBlock(BB);
|
||||||
}
|
}
|
||||||
|
|
||||||
BB->eraseFromParent();
|
BB->eraseFromParent();
|
||||||
|
|
||||||
|
return PredBB;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
|
/// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
|
||||||
|
|
|
@ -49,52 +49,6 @@ static inline void RemapInstruction(Instruction *I,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// FoldBlockIntoPredecessor - Folds a basic block into its predecessor if it
|
|
||||||
/// only has one predecessor, and that predecessor only has one successor.
|
|
||||||
/// The LoopInfo Analysis that is passed will be kept consistent.
|
|
||||||
/// Returns the new combined block.
|
|
||||||
static BasicBlock *FoldBlockIntoPredecessor(BasicBlock *BB, LoopInfo* LI) {
|
|
||||||
// Merge basic blocks into their predecessor if there is only one distinct
|
|
||||||
// pred, and if there is only one distinct successor of the predecessor, and
|
|
||||||
// if there are no PHI nodes.
|
|
||||||
BasicBlock *OnlyPred = BB->getSinglePredecessor();
|
|
||||||
if (!OnlyPred) return 0;
|
|
||||||
|
|
||||||
if (OnlyPred->getTerminator()->getNumSuccessors() != 1)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
DEBUG(errs() << "Merging: " << *BB << "into: " << *OnlyPred);
|
|
||||||
|
|
||||||
// Resolve any PHI nodes at the start of the block. They are all
|
|
||||||
// guaranteed to have exactly one entry if they exist, unless there are
|
|
||||||
// multiple duplicate (but guaranteed to be equal) entries for the
|
|
||||||
// incoming edges. This occurs when there are multiple edges from
|
|
||||||
// OnlyPred to OnlySucc.
|
|
||||||
FoldSingleEntryPHINodes(BB);
|
|
||||||
|
|
||||||
// Delete the unconditional branch from the predecessor...
|
|
||||||
OnlyPred->getInstList().pop_back();
|
|
||||||
|
|
||||||
// Move all definitions in the successor to the predecessor...
|
|
||||||
OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
|
|
||||||
|
|
||||||
// Make all PHI nodes that referred to BB now refer to Pred as their
|
|
||||||
// source...
|
|
||||||
BB->replaceAllUsesWith(OnlyPred);
|
|
||||||
|
|
||||||
std::string OldName = BB->getName();
|
|
||||||
|
|
||||||
// Erase basic block from the function...
|
|
||||||
LI->removeBlock(BB);
|
|
||||||
BB->eraseFromParent();
|
|
||||||
|
|
||||||
// Inherit predecessor's name if it exists...
|
|
||||||
if (!OldName.empty() && !OnlyPred->hasName())
|
|
||||||
OnlyPred->setName(OldName);
|
|
||||||
|
|
||||||
return OnlyPred;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Unroll the given loop by Count. The loop must be in LCSSA form. Returns true
|
/// Unroll the given loop by Count. The loop must be in LCSSA form. Returns true
|
||||||
/// if unrolling was succesful, or false if the loop was unmodified. Unrolling
|
/// if unrolling was succesful, or false if the loop was unmodified. Unrolling
|
||||||
/// can only fail when the loop's latch block is not terminated by a conditional
|
/// can only fail when the loop's latch block is not terminated by a conditional
|
||||||
|
@ -333,7 +287,7 @@ bool llvm::UnrollLoop(Loop *L, unsigned Count, LoopInfo* LI, LPPassManager* LPM)
|
||||||
} else {
|
} else {
|
||||||
Term->setUnconditionalDest(Dest);
|
Term->setUnconditionalDest(Dest);
|
||||||
// Merge adjacent basic blocks, if possible.
|
// Merge adjacent basic blocks, if possible.
|
||||||
if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest, LI)) {
|
if (BasicBlock *Fold = MergeBlockIntoPredecessor(Dest, LI)) {
|
||||||
std::replace(Latches.begin(), Latches.end(), Dest, Fold);
|
std::replace(Latches.begin(), Latches.end(), Dest, Fold);
|
||||||
std::replace(Headers.begin(), Headers.end(), Dest, Fold);
|
std::replace(Headers.begin(), Headers.end(), Dest, Fold);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ define i32 @test(i1 %a, i1 %b) {
|
||||||
O: ; preds = %0
|
O: ; preds = %0
|
||||||
br i1 %b, label %N, label %Q
|
br i1 %b, label %N, label %Q
|
||||||
Q: ; preds = %O
|
Q: ; preds = %O
|
||||||
|
call void @foo()
|
||||||
br label %N
|
br label %N
|
||||||
N: ; preds = %Q, %O
|
N: ; preds = %Q, %O
|
||||||
; This block should be foldable into M
|
; This block should be foldable into M
|
||||||
|
@ -20,3 +21,4 @@ M: ; preds = %N, %0
|
||||||
ret i32 %R
|
ret i32 %R
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declare void @foo()
|
||||||
|
|
Loading…
Reference in New Issue