[llvm] r348088 - [ValueTracking] add helper function for testing implied condition; NFCI
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Sun Dec 2 05:26:03 PST 2018
Author: spatel
Date: Sun Dec 2 05:26:03 2018
New Revision: 348088
URL: http://llvm.org/viewvc/llvm-project?rev=348088&view=rev
Log:
[ValueTracking] add helper function for testing implied condition; NFCI
We were duplicating code around the existing isImpliedCondition() that
checks for a predecessor block/dominating condition, so make that a
wrapper call.
Modified:
llvm/trunk/include/llvm/Analysis/ValueTracking.h
llvm/trunk/lib/Analysis/InstructionSimplify.cpp
llvm/trunk/lib/Analysis/ValueTracking.cpp
llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
Modified: llvm/trunk/include/llvm/Analysis/ValueTracking.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/ValueTracking.h?rev=348088&r1=348087&r2=348088&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/ValueTracking.h (original)
+++ llvm/trunk/include/llvm/Analysis/ValueTracking.h Sun Dec 2 05:26:03 2018
@@ -610,6 +610,12 @@ class Value;
Optional<bool> isImpliedCondition(const Value *LHS, const Value *RHS,
const DataLayout &DL, bool LHSIsTrue = true,
unsigned Depth = 0);
+
+ /// Return the boolean condition value in the context of the given instruction
+ /// if it is known based on dominating conditions.
+ Optional<bool> isImpliedByDomCondition(const Value *Cond,
+ const Instruction *ContextI,
+ const DataLayout &DL);
} // end namespace llvm
#endif // LLVM_ANALYSIS_VALUETRACKING_H
Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=348088&r1=348087&r2=348088&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Sun Dec 2 05:26:03 2018
@@ -3924,42 +3924,6 @@ static Value *simplifySelectWithFCmp(Val
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,
@@ -4002,8 +3966,9 @@ static Value *SimplifySelectInst(Value *
if (Value *V = foldSelectWithBinaryOp(Cond, TrueVal, FalseVal))
return V;
- if (Value *V = foldSelectWithDominatingCond(Cond, TrueVal, FalseVal, Q))
- return V;
+ Optional<bool> Imp = isImpliedByDomCondition(Cond, Q.CxtI, Q.DL);
+ if (Imp)
+ return *Imp ? TrueVal : FalseVal;
return nullptr;
}
Modified: llvm/trunk/lib/Analysis/ValueTracking.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ValueTracking.cpp?rev=348088&r1=348087&r2=348088&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ValueTracking.cpp (original)
+++ llvm/trunk/lib/Analysis/ValueTracking.cpp Sun Dec 2 05:26:03 2018
@@ -5378,3 +5378,35 @@ Optional<bool> llvm::isImpliedCondition(
}
return None;
}
+
+Optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
+ const Instruction *ContextI,
+ const DataLayout &DL) {
+ assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
+ if (!ContextI || !ContextI->getParent())
+ return None;
+
+ // TODO: This is a poor/cheap way to determine dominance. Should we use a
+ // dominator tree (eg, from a SimplifyQuery) instead?
+ const BasicBlock *ContextBB = ContextI->getParent();
+ const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
+ if (!PredBB)
+ return None;
+
+ // 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 None;
+
+ // The branch should get simplified. Don't bother simplifying this condition.
+ if (TrueBB == FalseBB)
+ return None;
+
+ assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
+ "Predecessor block does not point to successor?");
+
+ // Is this condition implied by the predecessor condition?
+ bool CondIsTrue = TrueBB == ContextBB;
+ return isImpliedCondition(PredCond, Cond, DL, CondIsTrue);
+}
Modified: llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp?rev=348088&r1=348087&r2=348088&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp Sun Dec 2 05:26:03 2018
@@ -5854,28 +5854,17 @@ bool SimplifyCFGOpt::SimplifyCondBranch(
if (SimplifyBranchOnICmpChain(BI, Builder, DL))
return true;
- // If this basic block has a single dominating predecessor block and the
- // dominating block's condition implies BI's condition, we know the direction
- // of the BI branch.
- if (BasicBlock *Dom = BB->getSinglePredecessor()) {
- auto *PBI = dyn_cast_or_null<BranchInst>(Dom->getTerminator());
- if (PBI && PBI->isConditional() &&
- PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
- assert(PBI->getSuccessor(0) == BB || PBI->getSuccessor(1) == BB);
- bool CondIsTrue = PBI->getSuccessor(0) == BB;
- Optional<bool> Implication = isImpliedCondition(
- PBI->getCondition(), BI->getCondition(), DL, CondIsTrue);
- if (Implication) {
- // Turn this into a branch on constant.
- auto *OldCond = BI->getCondition();
- ConstantInt *CI = *Implication
- ? ConstantInt::getTrue(BB->getContext())
- : ConstantInt::getFalse(BB->getContext());
- BI->setCondition(CI);
- RecursivelyDeleteTriviallyDeadInstructions(OldCond);
- return requestResimplify();
- }
- }
+ // If this basic block has dominating predecessor blocks and the dominating
+ // blocks' conditions imply BI's condition, we know the direction of BI.
+ Optional<bool> Imp = isImpliedByDomCondition(BI->getCondition(), BI, DL);
+ if (Imp) {
+ // Turn this into a branch on constant.
+ auto *OldCond = BI->getCondition();
+ ConstantInt *TorF = *Imp ? ConstantInt::getTrue(BB->getContext())
+ : ConstantInt::getFalse(BB->getContext());
+ BI->setCondition(TorF);
+ RecursivelyDeleteTriviallyDeadInstructions(OldCond);
+ return requestResimplify();
}
// If this basic block is ONLY a compare and a branch, and if a predecessor
More information about the llvm-commits
mailing list