Skip to content

Commit

Permalink
Make instsimplify's analysis of icmp eq/ne use computeKnownBits to de…
Browse files Browse the repository at this point in the history
…termine whether the icmp is always true or false. Patch by Suyog Sarda!

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211251 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
nlewycky committed Jun 19, 2014
1 parent bdb4aca commit 650b6ea
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
19 changes: 19 additions & 0 deletions lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2241,6 +2241,25 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
}
}

// If a bit is known to be zero for A and known to be one for B,
// then A and B cannot be equal.
if (ICmpInst::isEquality(Pred)) {
if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
uint32_t BitWidth = CI->getBitWidth();
APInt LHSKnownZero(BitWidth, 0);
APInt LHSKnownOne(BitWidth, 0);
computeKnownBits(LHS, LHSKnownZero, LHSKnownOne);
APInt RHSKnownZero(BitWidth, 0);
APInt RHSKnownOne(BitWidth, 0);
computeKnownBits(RHS, RHSKnownZero, RHSKnownOne);
if (((LHSKnownOne & RHSKnownZero) != 0) ||
((LHSKnownZero & RHSKnownOne) != 0))
return (Pred == ICmpInst::ICMP_EQ)
? ConstantInt::getFalse(CI->getContext())
: ConstantInt::getTrue(CI->getContext());
}
}

// Special logic for binary operators.
BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS);
BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS);
Expand Down
2 changes: 1 addition & 1 deletion test/Transforms/InstCombine/align-2d-gep.ll
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ bb1:
store <2 x double><double 0.0, double 0.0>, <2 x double>* %r, align 8

%indvar.next = add i64 %j, 2
%exitcond = icmp eq i64 %indvar.next, 557
%exitcond = icmp eq i64 %indvar.next, 556
br i1 %exitcond, label %bb11, label %bb1

bb11:
Expand Down
19 changes: 19 additions & 0 deletions test/Transforms/InstSimplify/compare.ll
Original file line number Diff line number Diff line change
Expand Up @@ -883,3 +883,22 @@ define i1 @returns_nonnull() {
; CHECK: ret i1 false
}

; If a bit is known to be zero for A and known to be one for B,
; then A and B cannot be equal.
define i1 @icmp_eq_const(i32 %a) nounwind {
%b = mul nsw i32 %a, -2
%c = icmp eq i32 %b, 1
ret i1 %c

; CHECK-LABEL: @icmp_eq_const
; CHECK-NEXT: ret i1 false
}

define i1 @icmp_ne_const(i32 %a) nounwind {
%b = mul nsw i32 %a, -2
%c = icmp ne i32 %b, 1
ret i1 %c

; CHECK-LABEL: @icmp_ne_const
; CHECK-NEXT: ret i1 true
}

0 comments on commit 650b6ea

Please sign in to comment.