summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
authorSanjay Patel <spatel@rotateright.com>2018-11-29 18:44:39 +0000
committerSanjay Patel <spatel@rotateright.com>2018-11-29 18:44:39 +0000
commitd802270808c9daa9bf689560f2da652fb907b5e8 (patch)
treec629598b354222f6aea02f4cf8e16ef1a4aa4cc3 /llvm/lib
parentb74d6368974c4b4b2913c1c20bd8c632c6492cb6 (diff)
downloadbcm5719-llvm-d802270808c9daa9bf689560f2da652fb907b5e8.tar.gz
bcm5719-llvm-d802270808c9daa9bf689560f2da652fb907b5e8.zip
[InstSimplify] fold select with implied condition
This is an almost direct move of the functionality from InstCombine to InstSimplify. There's no reason not to do this in InstSimplify because we never create a new value with this transform. (There's a question of whether any dominance-based transform belongs in either of these passes, but that's a separate issue.) I've changed 1 of the conditions for the fold (1 of the blocks for the branch must be the block we started with) into an assert because I'm not sure how that could ever be false. We need 1 extra check to make sure that the instruction itself is in a basic block because passes other than InstCombine may be using InstSimplify as an analysis on values that are not wired up yet. The 3-way compare changes show that InstCombine has some kind of phase-ordering hole. Otherwise, we would have already gotten the intended final result that we now show here. llvm-svn: 347896
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/Analysis/InstructionSimplify.cpp39
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp18
2 files changed, 39 insertions, 18 deletions
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 9fc10af9668..ddebcfaad06 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -3924,6 +3924,42 @@ static Value *simplifySelectWithFCmp(Value *Cond, Value *T, Value *F) {
return nullptr;
}
+/// Try to determine the result of a select based on a dominating condition.
+static Value *foldSelectWithDominatingCond(Value *Cond, Value *TV, Value *FV,
+ const SimplifyQuery &Q) {
+ // First, make sure that we have a select in a basic block.
+ // We don't know if we are called from some incomplete state.
+ if (!Q.CxtI || !Q.CxtI->getParent())
+ return nullptr;
+
+ // TODO: This is a poor/cheap way to determine dominance. Should we use the
+ // dominator tree in the SimplifyQuery instead?
+ const BasicBlock *SelectBB = Q.CxtI->getParent();
+ const BasicBlock *PredBB = SelectBB->getSinglePredecessor();
+ if (!PredBB)
+ return nullptr;
+
+ // We need a conditional branch in the predecessor.
+ Value *PredCond;
+ BasicBlock *TrueBB, *FalseBB;
+ if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
+ return nullptr;
+
+ // The branch should get simplified. Don't bother simplifying the select.
+ if (TrueBB == FalseBB)
+ return nullptr;
+
+ assert((TrueBB == SelectBB || FalseBB == SelectBB) &&
+ "Predecessor block does not point to successor?");
+
+ // Is the select condition implied by the predecessor condition?
+ bool CondIsTrue = TrueBB == SelectBB;
+ Optional<bool> Implied = isImpliedCondition(PredCond, Cond, Q.DL, CondIsTrue);
+ if (!Implied)
+ return nullptr;
+ return *Implied ? TV : FV;
+}
+
/// Given operands for a SelectInst, see if we can fold the result.
/// If not, this returns null.
static Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
@@ -3966,6 +4002,9 @@ static Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
if (Value *V = foldSelectWithBinaryOp(Cond, TrueVal, FalseVal))
return V;
+ if (Value *V = foldSelectWithDominatingCond(Cond, TrueVal, FalseVal, Q))
+ return V;
+
return nullptr;
}
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index db75c9383a1..19858ae149a 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -2021,24 +2021,6 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
}
}
- // See if we can determine the result of this select based on a dominating
- // condition.
- BasicBlock *Parent = SI.getParent();
- if (BasicBlock *Dom = Parent->getSinglePredecessor()) {
- auto *PBI = dyn_cast_or_null<BranchInst>(Dom->getTerminator());
- if (PBI && PBI->isConditional() &&
- PBI->getSuccessor(0) != PBI->getSuccessor(1) &&
- (PBI->getSuccessor(0) == Parent || PBI->getSuccessor(1) == Parent)) {
- bool CondIsTrue = PBI->getSuccessor(0) == Parent;
- Optional<bool> Implication = isImpliedCondition(
- PBI->getCondition(), SI.getCondition(), DL, CondIsTrue);
- if (Implication) {
- Value *V = *Implication ? TrueVal : FalseVal;
- return replaceInstUsesWith(SI, V);
- }
- }
- }
-
// If we can compute the condition, there's no need for a select.
// Like the above fold, we are attempting to reduce compile-time cost by
// putting this fold here with limitations rather than in InstSimplify.
OpenPOWER on IntegriCloud