Skip to content

Commit

Permalink
[LVI][CVP] Handle (x | y) < C style conditions
Browse files Browse the repository at this point in the history
InstCombine may convert conditions like (x < C) && (y < C) into
(x | y) < C (for some C). This patch teaches LVI to recognize that
in this case, it can infer either x < C or y < C along the edge.

This fixes the issue reported at
rust-lang/rust#73827.

Differential Revision: https://reviews.llvm.org/D82715
  • Loading branch information
nikic committed Jul 1, 2020
1 parent 05d7929 commit 91836fd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
17 changes: 14 additions & 3 deletions llvm/lib/Analysis/LazyValueInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,8 @@ Optional<ValueLatticeElement> LazyValueInfoImpl::solveBlockValueExtractValue(
return ValueLatticeElement::getOverdefined();
}

static bool matchICmpOperand(const APInt *&Offset, Value *LHS, Value *Val) {
static bool matchICmpOperand(const APInt *&Offset, Value *LHS, Value *Val,
ICmpInst::Predicate Pred) {
if (LHS == Val)
return true;

Expand All @@ -1102,6 +1103,16 @@ static bool matchICmpOperand(const APInt *&Offset, Value *LHS, Value *Val) {
if (match(LHS, m_Add(m_Specific(Val), m_APInt(Offset))))
return true;

// If (x | y) < C, then (x < C) && (y < C).
if (match(LHS, m_c_Or(m_Specific(Val), m_Value())) &&
(Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE))
return true;

// If (x & y) > C, then (x > C) && (y > C).
if (match(LHS, m_c_And(m_Specific(Val), m_Value())) &&
(Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE))
return true;

return false;
}

Expand All @@ -1127,10 +1138,10 @@ static ValueLatticeElement getValueFromICmpCondition(Value *Val, ICmpInst *ICI,
return ValueLatticeElement::getOverdefined();

const APInt *Offset = nullptr;
if (!matchICmpOperand(Offset, LHS, Val)) {
if (!matchICmpOperand(Offset, LHS, Val, EdgePred)) {
std::swap(LHS, RHS);
EdgePred = CmpInst::getSwappedPredicate(EdgePred);
if (!matchICmpOperand(Offset, LHS, Val))
if (!matchICmpOperand(Offset, LHS, Val, EdgePred))
return ValueLatticeElement::getOverdefined();
}

Expand Down
42 changes: 14 additions & 28 deletions llvm/test/Transforms/CorrelatedValuePropagation/icmp.ll
Original file line number Diff line number Diff line change
Expand Up @@ -659,10 +659,8 @@ define void @test_icmp_or_ult(i32 %a, i32 %b) {
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[OR]], 42
; CHECK-NEXT: br i1 [[CMP]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
; CHECK: if.true:
; CHECK-NEXT: [[CMP2:%.*]] = icmp ult i32 [[A]], 42
; CHECK-NEXT: call void @check1(i1 [[CMP2]])
; CHECK-NEXT: [[CMP3:%.*]] = icmp ult i32 [[B]], 42
; CHECK-NEXT: call void @check1(i1 [[CMP3]])
; CHECK-NEXT: call void @check1(i1 true)
; CHECK-NEXT: call void @check1(i1 true)
; CHECK-NEXT: ret void
; CHECK: if.false:
; CHECK-NEXT: [[CMP4:%.*]] = icmp uge i32 [[A]], 42
Expand Down Expand Up @@ -698,10 +696,8 @@ define void @test_icmp_or_ule(i32 %a, i32 %b) {
; CHECK-NEXT: [[CMP:%.*]] = icmp ule i32 [[OR]], 42
; CHECK-NEXT: br i1 [[CMP]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
; CHECK: if.true:
; CHECK-NEXT: [[CMP2:%.*]] = icmp ule i32 [[A]], 42
; CHECK-NEXT: call void @check1(i1 [[CMP2]])
; CHECK-NEXT: [[CMP3:%.*]] = icmp ule i32 [[B]], 42
; CHECK-NEXT: call void @check1(i1 [[CMP3]])
; CHECK-NEXT: call void @check1(i1 true)
; CHECK-NEXT: call void @check1(i1 true)
; CHECK-NEXT: ret void
; CHECK: if.false:
; CHECK-NEXT: [[CMP4:%.*]] = icmp ugt i32 [[A]], 42
Expand Down Expand Up @@ -743,10 +739,8 @@ define void @test_icmp_or_ugt(i32 %a, i32 %b) {
; CHECK-NEXT: call void @check1(i1 [[CMP3]])
; CHECK-NEXT: ret void
; CHECK: if.false:
; CHECK-NEXT: [[CMP4:%.*]] = icmp ule i32 [[A]], 42
; CHECK-NEXT: call void @check1(i1 [[CMP4]])
; CHECK-NEXT: [[CMP5:%.*]] = icmp ule i32 [[B]], 42
; CHECK-NEXT: call void @check1(i1 [[CMP5]])
; CHECK-NEXT: call void @check1(i1 true)
; CHECK-NEXT: call void @check1(i1 true)
; CHECK-NEXT: ret void
;
entry:
Expand Down Expand Up @@ -782,10 +776,8 @@ define void @test_icmp_or_uge(i32 %a, i32 %b) {
; CHECK-NEXT: call void @check1(i1 [[CMP3]])
; CHECK-NEXT: ret void
; CHECK: if.false:
; CHECK-NEXT: [[CMP4:%.*]] = icmp ult i32 [[A]], 42
; CHECK-NEXT: call void @check1(i1 [[CMP4]])
; CHECK-NEXT: [[CMP5:%.*]] = icmp ult i32 [[B]], 42
; CHECK-NEXT: call void @check1(i1 [[CMP5]])
; CHECK-NEXT: call void @check1(i1 true)
; CHECK-NEXT: call void @check1(i1 true)
; CHECK-NEXT: ret void
;
entry:
Expand Down Expand Up @@ -854,10 +846,8 @@ define void @test_icmp_and_ugt(i32 %a, i32 %b) {
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[AND]], 42
; CHECK-NEXT: br i1 [[CMP]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
; CHECK: if.true:
; CHECK-NEXT: [[CMP2:%.*]] = icmp ugt i32 [[A]], 42
; CHECK-NEXT: call void @check1(i1 [[CMP2]])
; CHECK-NEXT: [[CMP3:%.*]] = icmp ugt i32 [[B]], 42
; CHECK-NEXT: call void @check1(i1 [[CMP3]])
; CHECK-NEXT: call void @check1(i1 true)
; CHECK-NEXT: call void @check1(i1 true)
; CHECK-NEXT: ret void
; CHECK: if.false:
; CHECK-NEXT: [[CMP4:%.*]] = icmp ule i32 [[A]], 42
Expand Down Expand Up @@ -893,10 +883,8 @@ define void @test_icmp_and_uge(i32 %a, i32 %b) {
; CHECK-NEXT: [[CMP:%.*]] = icmp uge i32 [[AND]], 42
; CHECK-NEXT: br i1 [[CMP]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
; CHECK: if.true:
; CHECK-NEXT: [[CMP2:%.*]] = icmp uge i32 [[A]], 42
; CHECK-NEXT: call void @check1(i1 [[CMP2]])
; CHECK-NEXT: [[CMP3:%.*]] = icmp uge i32 [[B]], 42
; CHECK-NEXT: call void @check1(i1 [[CMP3]])
; CHECK-NEXT: call void @check1(i1 true)
; CHECK-NEXT: call void @check1(i1 true)
; CHECK-NEXT: ret void
; CHECK: if.false:
; CHECK-NEXT: [[CMP4:%.*]] = icmp ult i32 [[A]], 42
Expand Down Expand Up @@ -938,10 +926,8 @@ define void @test_icmp_and_ult(i32 %a, i32 %b) {
; CHECK-NEXT: call void @check1(i1 [[CMP3]])
; CHECK-NEXT: ret void
; CHECK: if.false:
; CHECK-NEXT: [[CMP4:%.*]] = icmp uge i32 [[A]], 42
; CHECK-NEXT: call void @check1(i1 [[CMP4]])
; CHECK-NEXT: [[CMP5:%.*]] = icmp uge i32 [[B]], 42
; CHECK-NEXT: call void @check1(i1 [[CMP5]])
; CHECK-NEXT: call void @check1(i1 true)
; CHECK-NEXT: call void @check1(i1 true)
; CHECK-NEXT: ret void
;
entry:
Expand Down

0 comments on commit 91836fd

Please sign in to comment.