[ValueTracking] Improve isImpliedCondition when the dominating cond is false.

llvm-svn: 267430
This commit is contained in:
Chad Rosier 2016-04-25 17:23:36 +00:00
parent a7818daee6
commit 5a0c6f041b
6 changed files with 409 additions and 13 deletions

View File

@ -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

View File

@ -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)

View File

@ -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);

View File

@ -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();

View File

@ -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
}

View File

@ -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
}