diff options
author | Pablo Barrio <pablo.barrio@arm.com> | 2016-11-15 15:42:23 +0000 |
---|---|---|
committer | Pablo Barrio <pablo.barrio@arm.com> | 2016-11-15 15:42:23 +0000 |
commit | 4f80c93a2ed931fc71acce58383eee1a67679878 (patch) | |
tree | 99129751836e3a8cf4bd6a7d5e671324afaf3e36 /llvm/lib/Transforms/Scalar/JumpThreading.cpp | |
parent | 5f782bb0489046017c7023287e4592c0f2b71758 (diff) | |
download | bcm5719-llvm-4f80c93a2ed931fc71acce58383eee1a67679878.tar.gz bcm5719-llvm-4f80c93a2ed931fc71acce58383eee1a67679878.zip |
Revert "[JumpThreading] Unfold selects that depend on the same condition"
This reverts commit ac54d0066c478a09c7cd28d15d0f9ff8af984afc.
llvm-svn: 286976
Diffstat (limited to 'llvm/lib/Transforms/Scalar/JumpThreading.cpp')
-rw-r--r-- | llvm/lib/Transforms/Scalar/JumpThreading.cpp | 115 |
1 files changed, 38 insertions, 77 deletions
diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp index 05ac11fc376..ddad3004fc7 100644 --- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp +++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp @@ -1963,100 +1963,61 @@ bool JumpThreadingPass::TryToUnfoldSelect(CmpInst *CondCmp, BasicBlock *BB) { return false; } -/// GetSelectFedByPhi - Look for PHI/Select in the same BB of the form +/// TryToUnfoldSelectInCurrBB - Look for PHI/Select in the same BB of the form /// bb: /// %p = phi [false, %bb1], [true, %bb2], [false, %bb3], [true, %bb4], ... /// %s = select p, trueval, falseval /// -/// And return the select. Unfolding it into a branch structure later enables +/// And expand the select into a branch structure. This later enables /// jump-threading over bb in this pass. /// -/// Using the similar approach of SimplifyCFG::FoldCondBranchOnPHI(), return -/// select if the associated PHI has at least one constant. -SelectInst *JumpThreadingPass::getSelectFedByPhi(PHINode *PN) { - - unsigned NumPHIValues = PN->getNumIncomingValues(); - if (NumPHIValues == 0 || !PN->hasOneUse()) - return nullptr; - - SelectInst *SI = dyn_cast<SelectInst>(PN->user_back()); - BasicBlock *BB = PN->getParent(); - if (!SI || SI->getParent() != BB) - return nullptr; - - Value *Cond = SI->getCondition(); - if (!Cond || Cond != PN || !Cond->getType()->isIntegerTy(1)) - return nullptr; - - for (unsigned i = 0; i != NumPHIValues; ++i) { - if (PN->getIncomingBlock(i) == BB) - return nullptr; - if (isa<ConstantInt>(PN->getIncomingValue(i))) - return SI; - } - - return nullptr; -} - -/// ExpandSelect - Expand a select into an if-then-else construct. -void JumpThreadingPass::expandSelect(SelectInst *SI) { - - BasicBlock *BB = SI->getParent(); - TerminatorInst *Term = - SplitBlockAndInsertIfThen(SI->getCondition(), SI, false); - PHINode *NewPN = PHINode::Create(SI->getType(), 2, "", SI); - NewPN->addIncoming(SI->getTrueValue(), Term->getParent()); - NewPN->addIncoming(SI->getFalseValue(), BB); - SI->replaceAllUsesWith(NewPN); - SI->eraseFromParent(); -} - -/// TryToUnfoldSelectInCurrBB - Unfold selects that could be jump-threaded were -/// they if-then-elses. If the unfolded selects are not jump-threaded, it will -/// be folded again in the later optimizations. +/// Using the similar approach of SimplifyCFG::FoldCondBranchOnPHI(), unfold +/// select if the associated PHI has at least one constant. If the unfolded +/// select is not jump-threaded, it will be folded again in the later +/// optimizations. bool JumpThreadingPass::TryToUnfoldSelectInCurrBB(BasicBlock *BB) { - // If threading this would thread across a loop header, don't thread the edge. // See the comments above FindLoopHeaders for justifications and caveats. if (LoopHeaders.count(BB)) return false; - bool Changed = false; - for (auto &I : *BB) { - - // Look for a Phi/Select pair in the same basic block. The Phi feeds the - // condition of the Select and at least one of the incoming values is a - // constant. - PHINode *PN; - SelectInst *SI; - if ((PN = dyn_cast<PHINode>(&I)) && (SI = getSelectFedByPhi(PN))) { - expandSelect(SI); - Changed = true; + // Look for a Phi/Select pair in the same basic block. The Phi feeds the + // condition of the Select and at least one of the incoming values is a + // constant. + for (BasicBlock::iterator BI = BB->begin(); + PHINode *PN = dyn_cast<PHINode>(BI); ++BI) { + unsigned NumPHIValues = PN->getNumIncomingValues(); + if (NumPHIValues == 0 || !PN->hasOneUse()) continue; - } - - if (I.getType()->isIntegerTy(1)) { - SmallVector<SelectInst *, 4> Selects; + SelectInst *SI = dyn_cast<SelectInst>(PN->user_back()); + if (!SI || SI->getParent() != BB) + continue; - // Look for scalar booleans used in selects as conditions. If there are - // several selects that use the same boolean, they are candidates for jump - // threading and therefore we should unfold them. - for (Value *U : I.users()) - if (auto *SI = dyn_cast<SelectInst>(U)) - Selects.push_back(SI); - if (Selects.size() <= 1) - continue; + Value *Cond = SI->getCondition(); + if (!Cond || Cond != PN || !Cond->getType()->isIntegerTy(1)) + continue; - // Remove duplicates - std::sort(Selects.begin(), Selects.end()); - auto NewEnd = std::unique(Selects.begin(), Selects.end()); + bool HasConst = false; + for (unsigned i = 0; i != NumPHIValues; ++i) { + if (PN->getIncomingBlock(i) == BB) + return false; + if (isa<ConstantInt>(PN->getIncomingValue(i))) + HasConst = true; + } - Changed = true; - for (auto SI = Selects.begin(); SI != NewEnd; ++SI) - expandSelect(*SI); + if (HasConst) { + // Expand the select. + TerminatorInst *Term = + SplitBlockAndInsertIfThen(SI->getCondition(), SI, false); + PHINode *NewPN = PHINode::Create(SI->getType(), 2, "", SI); + NewPN->addIncoming(SI->getTrueValue(), Term->getParent()); + NewPN->addIncoming(SI->getFalseValue(), BB); + SI->replaceAllUsesWith(NewPN); + SI->eraseFromParent(); + return true; } } - - return Changed; + + return false; } |