From 77beb479f556093c5f1b4854fcbb095cda34f202 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 11 Jan 2010 23:41:09 +0000 Subject: [PATCH] some cleanup, and make it obvious that ProcessJumpOnPHI only works on branches by renaming it and checking for a branch at the call site. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@93208 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/JumpThreading.cpp | 38 +++++++++---------------- 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/lib/Transforms/Scalar/JumpThreading.cpp b/lib/Transforms/Scalar/JumpThreading.cpp index ab7d9718ed7..63d32e65111 100644 --- a/lib/Transforms/Scalar/JumpThreading.cpp +++ b/lib/Transforms/Scalar/JumpThreading.cpp @@ -102,7 +102,7 @@ namespace { bool ProcessBranchOnDuplicateCond(BasicBlock *PredBB, BasicBlock *DestBB); bool ProcessSwitchOnDuplicateCond(BasicBlock *PredBB, BasicBlock *DestBB); - bool ProcessJumpOnPHI(PHINode *PN); + bool ProcessBranchOnPHI(PHINode *PN); bool SimplifyPartiallyRedundantLoad(LoadInst *LI); }; @@ -550,11 +550,6 @@ bool JumpThreading::ProcessBlock(BasicBlock *BB) { } - // See if this is a phi node in the current block. - if (PHINode *PN = dyn_cast(CondInst)) - if (PN->getParent() == BB) - return ProcessJumpOnPHI(PN); - if (CmpInst *CondCmp = dyn_cast(CondInst)) { if (!LVI && (!isa(CondCmp->getOperand(0)) || @@ -583,8 +578,6 @@ bool JumpThreading::ProcessBlock(BasicBlock *BB) { // we see one, check to see if it's partially redundant. If so, insert a PHI // which can then be used to thread the values. // - // This is particularly important because reg2mem inserts loads and stores all - // over the place, and this blocks jump threading if we don't zap them. Value *SimplifyValue = CondInst; if (CmpInst *CondCmp = dyn_cast(SimplifyValue)) if (isa(CondCmp->getOperand(1))) @@ -604,9 +597,14 @@ bool JumpThreading::ProcessBlock(BasicBlock *BB) { if (ProcessThreadableEdges(CondInst, BB)) return true; + // If this is an otherwise-unfoldable branch on a phi node in the current + // block, see if we can simplify. + if (PHINode *PN = dyn_cast(CondInst)) + if (PN->getParent() == BB && isa(BB->getTerminator())) + return ProcessBranchOnPHI(PN); // TODO: If we have: "br (X > 0)" and we have a predecessor where we know - // "(X == 4)" thread through this block. + // "(X == 4)", thread through this block. return false; } @@ -1068,24 +1066,17 @@ bool JumpThreading::ProcessThreadableEdges(Value *Cond, BasicBlock *BB) { return ThreadEdge(BB, PredsToFactor, MostPopularDest); } -/// ProcessJumpOnPHI - We have a conditional branch or switch on a PHI node in -/// the current block. See if there are any simplifications we can do based on -/// inputs to the phi node. +/// ProcessBranchOnPHI - We have an otherwise unthreadable conditional branch on +/// a PHI node in the current block. See if there are any simplifications we +/// can do based on inputs to the phi node. /// -bool JumpThreading::ProcessJumpOnPHI(PHINode *PN) { +bool JumpThreading::ProcessBranchOnPHI(PHINode *PN) { BasicBlock *BB = PN->getParent(); // If any of the predecessor blocks end in an unconditional branch, we can - // *duplicate* the jump into that block in order to further encourage jump - // threading and to eliminate cases where we have branch on a phi of an icmp - // (branch on icmp is much better). - - // We don't want to do this tranformation for switches, because we don't - // really want to duplicate a switch. - if (isa(BB->getTerminator())) - return false; - - // Look for unconditional branch predecessors. + // *duplicate* the conditional branch into that block in order to further + // encourage jump threading and to eliminate cases where we have branch on a + // phi of an icmp (branch on icmp is much better). for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { BasicBlock *PredBB = PN->getIncomingBlock(i); if (BranchInst *PredBr = dyn_cast(PredBB->getTerminator())) @@ -1098,7 +1089,6 @@ bool JumpThreading::ProcessJumpOnPHI(PHINode *PN) { return false; } - /// AddPHINodeEntriesForMappedBlock - We're adding 'NewPred' as a new /// predecessor to the PHIBB block. If it has PHI nodes, add entries for /// NewPred using the entries from OldPred (suitably mapped).