diff options
Diffstat (limited to 'llvm/lib/Transforms/Utils')
-rw-r--r-- | llvm/lib/Transforms/Utils/BasicBlockUtils.cpp | 18 | ||||
-rw-r--r-- | llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 5 | ||||
-rw-r--r-- | llvm/lib/Transforms/Utils/UnrollLoop.cpp | 9 |
3 files changed, 22 insertions, 10 deletions
diff --git a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp index 45cfe8f181f..431424ee9f3 100644 --- a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp +++ b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp @@ -54,6 +54,24 @@ void llvm::DeleteDeadBlock(BasicBlock *BB) { BB->eraseFromParent(); } +/// FoldSingleEntryPHINodes - We know that BB has one predecessor. If there are +/// any single-entry PHI nodes in it, fold them away. This handles the case +/// when all entries to the PHI nodes in a block are guaranteed equal, such as +/// when the block has exactly one predecessor. +void llvm::FoldSingleEntryPHINodes(BasicBlock *BB) { + if (!isa<PHINode>(BB->begin())) + return; + + while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) { + if (PN->getIncomingValue(0) != PN) + PN->replaceAllUsesWith(PN->getIncomingValue(0)); + else + PN->replaceAllUsesWith(UndefValue::get(PN->getType())); + PN->eraseFromParent(); + } +} + + /// 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) { diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index f4faeb3deb5..d6465f599cf 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -1101,10 +1101,7 @@ static bool FoldCondBranchOnPHI(BranchInst *BI) { // Degenerate case of a single entry PHI. if (PN->getNumIncomingValues() == 1) { - if (PN->getIncomingValue(0) != PN) - PN->replaceAllUsesWith(PN->getIncomingValue(0)); - else - PN->replaceAllUsesWith(UndefValue::get(PN->getType())); + FoldSingleEntryPHINodes(PN->getParent()); PN->eraseFromParent(); return true; } diff --git a/llvm/lib/Transforms/Utils/UnrollLoop.cpp b/llvm/lib/Transforms/Utils/UnrollLoop.cpp index 63493dc66a3..caef7ec5c45 100644 --- a/llvm/lib/Transforms/Utils/UnrollLoop.cpp +++ b/llvm/lib/Transforms/Utils/UnrollLoop.cpp @@ -25,13 +25,14 @@ #include "llvm/Analysis/ConstantFolding.h" #include "llvm/Analysis/LoopPass.h" #include "llvm/Support/Debug.h" +#include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/Cloning.h" #include "llvm/Transforms/Utils/Local.h" #include <cstdio> using namespace llvm; -/* TODO: Should these be here or in LoopUnroll? */ +// TODO: Should these be here or in LoopUnroll? STATISTIC(NumCompletelyUnrolled, "Number of loops completely unrolled"); STATISTIC(NumUnrolled, "Number of loops unrolled (completely or otherwise)"); @@ -68,11 +69,7 @@ static BasicBlock *FoldBlockIntoPredecessor(BasicBlock *BB, LoopInfo* LI) { // multiple duplicate (but guaranteed to be equal) entries for the // incoming edges. This occurs when there are multiple edges from // OnlyPred to OnlySucc. - // - while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) { - PN->replaceAllUsesWith(PN->getIncomingValue(0)); - BB->getInstList().pop_front(); // Delete the phi node... - } + FoldSingleEntryPHINodes(BB); // Delete the unconditional branch from the predecessor... OnlyPred->getInstList().pop_back(); |