[llvm-commits] [llvm] r85670 - in /llvm/trunk: include/llvm/Transforms/Utils/BasicBlockUtils.h lib/Transforms/Utils/BasicBlockUtils.cpp lib/Transforms/Utils/LoopUnroll.cpp test/Transforms/SimplifyCFG/PhiBlockMerge.ll
Dan Gohman
gohman at apple.com
Sat Oct 31 10:33:02 PDT 2009
Author: djg
Date: Sat Oct 31 12:33:01 2009
New Revision: 85670
URL: http://llvm.org/viewvc/llvm-project?rev=85670&view=rev
Log:
Revert r85667. LoopUnroll currently can't call utility functions which
auto-update the DominatorTree because it doesn't keep the DominatorTree
current while it works.
Modified:
llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h
llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp
llvm/trunk/lib/Transforms/Utils/LoopUnroll.cpp
llvm/trunk/test/Transforms/SimplifyCFG/PhiBlockMerge.ll
Modified: llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h?rev=85670&r1=85669&r2=85670&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h (original)
+++ llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h Sat Oct 31 12:33:01 2009
@@ -43,11 +43,9 @@
/// it is ultimately unused or if it reaches an unused cycle.
void DeleteDeadPHIs(BasicBlock *BB);
-/// MergeBlockIntoPredecessor - Folds a basic block into its predecessor if it
-/// only has one predecessor, and that predecessor only has one successor.
-/// 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);
+/// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
+/// if possible. The return value indicates success or failure.
+bool MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P = 0);
// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
// with a value, then remove and delete the original instruction.
Modified: llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp?rev=85670&r1=85669&r2=85670&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp Sat Oct 31 12:33:01 2009
@@ -92,28 +92,54 @@
RecursivelyDeleteDeadPHINode(PN);
}
-/// MergeBlockIntoPredecessor - Folds a basic block into its predecessor if it
-/// only has one predecessor, and that predecessor only has one successor.
-/// 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 *llvm::MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P) {
- // Don't merge if the block has multiple predecessors.
- BasicBlock *PredBB = BB->getSinglePredecessor();
- if (!PredBB) return 0;
- // Don't merge if the predecessor has multiple successors.
- if (PredBB->getTerminator()->getNumSuccessors() != 1) return 0;
+/// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
+/// if possible. The return value indicates success or failure.
+bool llvm::MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P) {
+ pred_iterator PI(pred_begin(BB)), PE(pred_end(BB));
+ // Can't merge the entry block.
+ if (pred_begin(BB) == pred_end(BB)) return false;
+
+ BasicBlock *PredBB = *PI++;
+ for (; PI != PE; ++PI) // Search all predecessors, see if they are all same
+ if (*PI != PredBB) {
+ 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.
- if (PredBB == BB) return 0;
+ if (PredBB == BB) return false;
// Don't break invokes.
- if (isa<InvokeInst>(PredBB->getTerminator())) return 0;
+ if (isa<InvokeInst>(PredBB->getTerminator())) return false;
+
+ succ_iterator SI(succ_begin(PredBB)), SE(succ_end(PredBB));
+ BasicBlock* OnlySucc = BB;
+ for (; SI != SE; ++SI)
+ if (*SI != OnlySucc) {
+ OnlySucc = 0; // There are multiple distinct successors!
+ break;
+ }
- // 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
- // PredBB to BB.
- 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...
PredBB->getInstList().pop_back();
@@ -124,7 +150,7 @@
// source...
BB->replaceAllUsesWith(PredBB);
- // If the predecessor doesn't have a name, take the successor's name.
+ // Inherit predecessors name if it exists.
if (!PredBB->hasName())
PredBB->takeName(BB);
@@ -143,14 +169,12 @@
DT->eraseNode(BB);
}
}
- // Notify LoopInfo that the block is removed.
- if (LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>())
- LI->removeBlock(BB);
}
BB->eraseFromParent();
- return PredBB;
+
+ return true;
}
/// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
Modified: llvm/trunk/lib/Transforms/Utils/LoopUnroll.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/LoopUnroll.cpp?rev=85670&r1=85669&r2=85670&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/LoopUnroll.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/LoopUnroll.cpp Sat Oct 31 12:33:01 2009
@@ -49,6 +49,52 @@
}
}
+/// 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
/// 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
@@ -287,7 +333,7 @@
} else {
Term->setUnconditionalDest(Dest);
// Merge adjacent basic blocks, if possible.
- if (BasicBlock *Fold = MergeBlockIntoPredecessor(Dest, LI)) {
+ if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest, LI)) {
std::replace(Latches.begin(), Latches.end(), Dest, Fold);
std::replace(Headers.begin(), Headers.end(), Dest, Fold);
}
Modified: llvm/trunk/test/Transforms/SimplifyCFG/PhiBlockMerge.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SimplifyCFG/PhiBlockMerge.ll?rev=85670&r1=85669&r2=85670&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/SimplifyCFG/PhiBlockMerge.ll (original)
+++ llvm/trunk/test/Transforms/SimplifyCFG/PhiBlockMerge.ll Sat Oct 31 12:33:01 2009
@@ -9,7 +9,6 @@
O: ; preds = %0
br i1 %b, label %N, label %Q
Q: ; preds = %O
- call void @foo()
br label %N
N: ; preds = %Q, %O
; This block should be foldable into M
@@ -21,4 +20,3 @@
ret i32 %R
}
-declare void @foo()
More information about the llvm-commits
mailing list