summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChad Rosier <mcrosier@codeaurora.org>2016-04-25 17:23:36 +0000
committerChad Rosier <mcrosier@codeaurora.org>2016-04-25 17:23:36 +0000
commite2cbd13e5634e16301cd9e4b88b5ea08accafe9f (patch)
tree21cf5562f959fc1476cb2b129c884fb939e74267
parent8d728fccb8266004f23485a9b336825d2fa79fd8 (diff)
downloadbcm5719-llvm-e2cbd13e5634e16301cd9e4b88b5ea08accafe9f.tar.gz
bcm5719-llvm-e2cbd13e5634e16301cd9e4b88b5ea08accafe9f.zip
[ValueTracking] Improve isImpliedCondition when the dominating cond is false.
llvm-svn: 267430
-rw-r--r--llvm/include/llvm/Analysis/ValueTracking.h9
-rw-r--r--llvm/lib/Analysis/ValueTracking.cpp9
-rw-r--r--llvm/lib/Transforms/Scalar/JumpThreading.cpp7
-rw-r--r--llvm/lib/Transforms/Utils/SimplifyCFG.cpp8
-rw-r--r--llvm/test/Transforms/JumpThreading/implied-cond.ll50
-rw-r--r--llvm/test/Transforms/SimplifyCFG/implied-cond-matching-false-dest.ll339
6 files changed, 409 insertions, 13 deletions
diff --git a/llvm/include/llvm/Analysis/ValueTracking.h b/llvm/include/llvm/Analysis/ValueTracking.h
index 6e58ee07998..76249b9634f 100644
--- a/llvm/include/llvm/Analysis/ValueTracking.h
+++ b/llvm/include/llvm/Analysis/ValueTracking.h
@@ -463,11 +463,10 @@ template <typename T> class ArrayRef;
/// T | T | F
/// F | T | T
/// (A)
- Optional<bool> isImpliedCondition(Value *LHS, Value *RHS,
- const DataLayout &DL, unsigned Depth = 0,
- AssumptionCache *AC = nullptr,
- const Instruction *CxtI = nullptr,
- const DominatorTree *DT = nullptr);
+ Optional<bool> isImpliedCondition(
+ Value *LHS, Value *RHS, const DataLayout &DL, bool InvertAPred = false,
+ unsigned Depth = 0, AssumptionCache *AC = nullptr,
+ const Instruction *CxtI = nullptr, const DominatorTree *DT = nullptr);
} // end namespace llvm
#endif
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 5ada78a5350..a9b8af1017c 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -3962,8 +3962,8 @@ static Optional<bool> isImpliedCondMatchingOperands(CmpInst::Predicate APred,
}
Optional<bool> llvm::isImpliedCondition(Value *LHS, Value *RHS,
- const DataLayout &DL, unsigned Depth,
- AssumptionCache *AC,
+ const DataLayout &DL, bool InvertAPred,
+ unsigned Depth, AssumptionCache *AC,
const Instruction *CxtI,
const DominatorTree *DT) {
assert(LHS->getType() == RHS->getType() && "mismatched type");
@@ -3971,7 +3971,7 @@ Optional<bool> llvm::isImpliedCondition(Value *LHS, Value *RHS,
assert(OpTy->getScalarType()->isIntegerTy(1));
// LHS ==> RHS by definition
- if (LHS == RHS)
+ if (!InvertAPred && LHS == RHS)
return true;
if (OpTy->isVectorTy())
@@ -3987,6 +3987,9 @@ Optional<bool> llvm::isImpliedCondition(Value *LHS, Value *RHS,
!match(RHS, m_ICmp(BPred, m_Value(BLHS), m_Value(BRHS))))
return None;
+ if (InvertAPred)
+ APred = CmpInst::getInversePredicate(APred);
+
Optional<bool> Implication =
isImpliedCondMatchingOperands(APred, ALHS, ARHS, BPred, BLHS, BRHS);
if (Implication)
diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
index ba406a8e8b3..75c340fd593 100644
--- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -925,11 +925,14 @@ bool JumpThreading::ProcessImpliedCondition(BasicBlock *BB) {
while (CurrentPred && Iter++ < ImplicationSearchThreshold) {
auto *PBI = dyn_cast<BranchInst>(CurrentPred->getTerminator());
- if (!PBI || !PBI->isConditional() || PBI->getSuccessor(0) != CurrentBB)
+ if (!PBI || !PBI->isConditional())
+ return false;
+ if (PBI->getSuccessor(0) != CurrentBB && PBI->getSuccessor(1) != CurrentBB)
return false;
+ bool FalseDest = PBI->getSuccessor(1) == CurrentBB;
Optional<bool> Implication =
- isImpliedCondition(PBI->getCondition(), Cond, DL);
+ isImpliedCondition(PBI->getCondition(), Cond, DL, FalseDest);
if (Implication) {
BI->getSuccessor(*Implication ? 1 : 0)->removePredecessor(BB);
BranchInst::Create(BI->getSuccessor(*Implication ? 0 : 1), BI);
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 53bea2191bc..23199b37b29 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2703,11 +2703,13 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
// If BI is reached from the true path of PBI and PBI's condition implies
// BI's condition, we know the direction of the BI branch.
- if (PBI->getSuccessor(0) == BI->getParent() &&
+ if ((PBI->getSuccessor(0) == BI->getParent() ||
+ PBI->getSuccessor(1) == BI->getParent()) &&
PBI->getSuccessor(0) != PBI->getSuccessor(1) &&
BB->getSinglePredecessor()) {
- Optional<bool> Implication =
- isImpliedCondition(PBI->getCondition(), BI->getCondition(), DL);
+ bool FalseDest = PBI->getSuccessor(1) == BI->getParent();
+ Optional<bool> Implication = isImpliedCondition(
+ PBI->getCondition(), BI->getCondition(), DL, FalseDest);
if (Implication) {
// Turn this into a branch on constant.
auto *OldCond = BI->getCondition();
diff --git a/llvm/test/Transforms/JumpThreading/implied-cond.ll b/llvm/test/Transforms/JumpThreading/implied-cond.ll
index 991f78ff497..6da05791456 100644
--- a/llvm/test/Transforms/JumpThreading/implied-cond.ll
+++ b/llvm/test/Transforms/JumpThreading/implied-cond.ll
@@ -125,3 +125,53 @@ if.end:
if.end3:
ret void
}
+
+declare void @is(i1)
+
+; If A >=s B is false then A <=s B is implied true.
+; CHECK-LABEL: @test_sge_sle
+; CHECK: call void @is(i1 true)
+; CHECK-NOT: call void @is(i1 false)
+define void @test_sge_sle(i32 %a, i32 %b) {
+ %cmp1 = icmp sge i32 %a, %b
+ br i1 %cmp1, label %untaken, label %taken
+
+taken:
+ %cmp2 = icmp sle i32 %a, %b
+ br i1 %cmp2, label %istrue, label %isfalse
+
+istrue:
+ call void @is(i1 true)
+ ret void
+
+isfalse:
+ call void @is(i1 false)
+ ret void
+
+untaken:
+ ret void
+}
+
+; If A <=s B is false then A <=s B is implied false.
+; CHECK-LABEL: @test_sle_sle
+; CHECK-NOT: call void @is(i1 true)
+; CHECK: call void @is(i1 false)
+define void @test_sle_sle(i32 %a, i32 %b) {
+ %cmp1 = icmp sle i32 %a, %b
+ br i1 %cmp1, label %untaken, label %taken
+
+taken:
+ %cmp2 = icmp sle i32 %a, %b
+ br i1 %cmp2, label %istrue, label %isfalse
+
+istrue:
+ call void @is(i1 true)
+ ret void
+
+isfalse:
+ call void @is(i1 false)
+ ret void
+
+untaken:
+ ret void
+}
diff --git a/llvm/test/Transforms/SimplifyCFG/implied-cond-matching-false-dest.ll b/llvm/test/Transforms/SimplifyCFG/implied-cond-matching-false-dest.ll
new file mode 100644
index 00000000000..1d29813ecfa
--- /dev/null
+++ b/llvm/test/Transforms/SimplifyCFG/implied-cond-matching-false-dest.ll
@@ -0,0 +1,339 @@
+; RUN: opt %s -S -simplifycfg | FileCheck %s
+
+declare void @is(i1)
+
+; If A == B is false then A == B is implied false.
+; CHECK-LABEL: @test_eq_eq
+; CHECK-NOT: call void @is(i1 true)
+; CHECK: call void @is(i1 false)
+define void @test_eq_eq(i32 %a, i32 %b) {
+ %cmp1 = icmp eq i32 %a, %b
+ br i1 %cmp1, label %untaken, label %taken
+
+taken:
+ %cmp2 = icmp eq i32 %a, %b
+ br i1 %cmp2, label %istrue, label %isfalse
+
+istrue:
+ call void @is(i1 true)
+ ret void
+
+isfalse:
+ call void @is(i1 false)
+ ret void
+
+untaken:
+ ret void
+}
+
+; If A == B is false then A != B is implied true.
+; CHECK-LABEL: @test_eq_ne
+; CHECK: call void @is(i1 true)
+; CHECK-NOT: call void @is(i1 false)
+define void @test_eq_ne(i32 %a, i32 %b) {
+ %cmp1 = icmp eq i32 %a, %b
+ br i1 %cmp1, label %untaken, label %taken
+
+taken:
+ %cmp2 = icmp ne i32 %a, %b
+ br i1 %cmp2, label %istrue, label %isfalse
+
+istrue:
+ call void @is(i1 true)
+ ret void
+
+isfalse:
+ call void @is(i1 false)
+ ret void
+
+untaken:
+ ret void
+}
+
+; If A != B is false then A != B is implied false.
+; CHECK-LABEL: @test_ne_ne
+; CHECK-NOT: call void @is(i1 true)
+; CHECK: call void @is(i1 false)
+define void @test_ne_ne(i32 %a, i32 %b) {
+ %cmp1 = icmp ne i32 %a, %b
+ br i1 %cmp1, label %untaken, label %taken
+
+taken:
+ %cmp2 = icmp ne i32 %a, %b
+ br i1 %cmp2, label %istrue, label %isfalse
+
+istrue:
+ call void @is(i1 true)
+ ret void
+
+isfalse:
+ call void @is(i1 false)
+ ret void
+
+untaken:
+ ret void
+}
+
+; If A != B is false then A >u B is implied false.
+; CHECK-LABEL: @test_ne_ugt
+; CHECK-NOT: call void @is(i1 true)
+; CHECK: call void @is(i1 false)
+define void @test_ne_ugt(i32 %a, i32 %b) {
+ %cmp1 = icmp ne i32 %a, %b
+ br i1 %cmp1, label %untaken, label %taken
+
+taken:
+ %cmp2 = icmp ugt i32 %a, %b
+ br i1 %cmp2, label %istrue, label %isfalse
+
+istrue:
+ call void @is(i1 true)
+ ret void
+
+isfalse:
+ call void @is(i1 false)
+ ret void
+
+untaken:
+ ret void
+}
+
+; If A != B is false then A >=u B is implied true.
+; CHECK-LABEL: @test_ne_uge
+; CHECK: call void @is(i1 true)
+; CHECK-NOT: call void @is(i1 false)
+define void @test_ne_uge(i32 %a, i32 %b) {
+ %cmp1 = icmp ne i32 %a, %b
+ br i1 %cmp1, label %untaken, label %taken
+
+taken:
+ %cmp2 = icmp uge i32 %a, %b
+ br i1 %cmp2, label %istrue, label %isfalse
+
+istrue:
+ call void @is(i1 true)
+ ret void
+
+isfalse:
+ call void @is(i1 false)
+ ret void
+
+untaken:
+ ret void
+}
+
+; If A != B is false then A <u B is implied false.
+; CHECK-LABEL: @test_ne_ult
+; CHECK-NOT: call void @is(i1 true)
+; CHECK: call void @is(i1 false)
+define void @test_ne_ult(i32 %a, i32 %b) {
+ %cmp1 = icmp ne i32 %a, %b
+ br i1 %cmp1, label %untaken, label %taken
+
+taken:
+ %cmp2 = icmp ult i32 %a, %b
+ br i1 %cmp2, label %istrue, label %isfalse
+
+istrue:
+ call void @is(i1 true)
+ ret void
+
+isfalse:
+ call void @is(i1 false)
+ ret void
+
+untaken:
+ ret void
+}
+
+; If A != B is false then A <=u B is implied true.
+; CHECK-LABEL: @test_ne_ule
+; CHECK: call void @is(i1 true)
+; CHECK-NOT: call void @is(i1 false)
+define void @test_ne_ule(i32 %a, i32 %b) {
+ %cmp1 = icmp ne i32 %a, %b
+ br i1 %cmp1, label %untaken, label %taken
+
+taken:
+ %cmp2 = icmp ule i32 %a, %b
+ br i1 %cmp2, label %istrue, label %isfalse
+
+istrue:
+ call void @is(i1 true)
+ ret void
+
+isfalse:
+ call void @is(i1 false)
+ ret void
+
+untaken:
+ ret void
+}
+
+; If A >u B is false then A >u B is implied false.
+; CHECK-LABEL: @test_ugt_ugt
+; CHECK-NOT: call void @is(i1 true)
+; CHECK: call void @is(i1 false)
+define void @test_ugt_ugt(i32 %a, i32 %b) {
+ %cmp1 = icmp ugt i32 %a, %b
+ br i1 %cmp1, label %untaken, label %taken
+
+taken:
+ %cmp2 = icmp ugt i32 %a, %b
+ br i1 %cmp2, label %istrue, label %isfalse
+
+istrue:
+ call void @is(i1 true)
+ ret void
+
+isfalse:
+ call void @is(i1 false)
+ ret void
+
+untaken:
+ ret void
+}
+
+; If A >u B is false then A <=u B is implied true.
+; CHECK-LABEL: @test_ugt_ule
+; CHECK: call void @is(i1 true)
+; CHECK-NOT: call void @is(i1 false)
+define void @test_ugt_ule(i32 %a, i32 %b) {
+ %cmp1 = icmp ugt i32 %a, %b
+ br i1 %cmp1, label %untaken, label %taken
+
+taken:
+ %cmp2 = icmp ule i32 %a, %b
+ br i1 %cmp2, label %istrue, label %isfalse
+
+istrue:
+ call void @is(i1 true)
+ ret void
+
+isfalse:
+ call void @is(i1 false)
+ ret void
+
+untaken:
+ ret void
+}
+
+; If A >=u B is false then A >=u B is implied false.
+; CHECK-LABEL: @test_uge_uge
+; CHECK-NOT: call void @is(i1 true)
+; CHECK: call void @is(i1 false)
+define void @test_uge_uge(i32 %a, i32 %b) {
+ %cmp1 = icmp uge i32 %a, %b
+ br i1 %cmp1, label %untaken, label %taken
+
+taken:
+ %cmp2 = icmp uge i32 %a, %b
+ br i1 %cmp2, label %istrue, label %isfalse
+
+istrue:
+ call void @is(i1 true)
+ ret void
+
+isfalse:
+ call void @is(i1 false)
+ ret void
+
+untaken:
+ ret void
+}
+
+; If A >=u B is false then A <u B is implied true.
+; CHECK-LABEL: @test_uge_ult
+; CHECK: call void @is(i1 true)
+; CHECK-NOT: call void @is(i1 false)
+define void @test_uge_ult(i32 %a, i32 %b) {
+ %cmp1 = icmp uge i32 %a, %b
+ br i1 %cmp1, label %untaken, label %taken
+
+taken:
+ %cmp2 = icmp ult i32 %a, %b
+ br i1 %cmp2, label %istrue, label %isfalse
+
+istrue:
+ call void @is(i1 true)
+ ret void
+
+isfalse:
+ call void @is(i1 false)
+ ret void
+
+untaken:
+ ret void
+}
+
+; If A >=u B is false then A <=u B is implied true.
+; CHECK-LABEL: @test_uge_ule
+; CHECK: call void @is(i1 true)
+; CHECK-NOT: call void @is(i1 false)
+define void @test_uge_ule(i32 %a, i32 %b) {
+ %cmp1 = icmp uge i32 %a, %b
+ br i1 %cmp1, label %untaken, label %taken
+
+taken:
+ %cmp2 = icmp ule i32 %a, %b
+ br i1 %cmp2, label %istrue, label %isfalse
+
+istrue:
+ call void @is(i1 true)
+ ret void
+
+isfalse:
+ call void @is(i1 false)
+ ret void
+
+untaken:
+ ret void
+}
+
+; If A <u B is false then A <u B is implied false.
+; CHECK-LABEL: @test_ult_ult
+; CHECK-NOT: call void @is(i1 true)
+; CHECK: call void @is(i1 false)
+define void @test_ult_ult(i32 %a, i32 %b) {
+ %cmp1 = icmp ult i32 %a, %b
+ br i1 %cmp1, label %untaken, label %taken
+
+taken:
+ %cmp2 = icmp ult i32 %a, %b
+ br i1 %cmp2, label %istrue, label %isfalse
+
+istrue:
+ call void @is(i1 true)
+ ret void
+
+isfalse:
+ call void @is(i1 false)
+ ret void
+
+untaken:
+ ret void
+}
+
+; If A <=u B is false then A <=u B is implied false.
+; CHECK-LABEL: @test_ule_ule
+; CHECK-NOT: call void @is(i1 true)
+; CHECK: call void @is(i1 false)
+define void @test_ule_ule(i32 %a, i32 %b) {
+ %cmp1 = icmp ule i32 %a, %b
+ br i1 %cmp1, label %untaken, label %taken
+
+taken:
+ %cmp2 = icmp ule i32 %a, %b
+ br i1 %cmp2, label %istrue, label %isfalse
+
+istrue:
+ call void @is(i1 true)
+ ret void
+
+isfalse:
+ call void @is(i1 false)
+ ret void
+
+untaken:
+ ret void
+}
OpenPOWER on IntegriCloud