Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions llvm/lib/Analysis/CmpInstAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ llvm::decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred,
break;
}

// Try to convert (trunc X) eq/ne C into (X & Mask) eq/ne C
if (LookThroughTrunc && isa<TruncInst>(LHS)) {
Result.Pred = Pred;
Result.Mask = APInt::getAllOnes(C.getBitWidth());
Result.C = C;
break;
}

return std::nullopt;
}
}
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ static unsigned conjugateICmpMask(unsigned Mask) {
// Adapts the external decomposeBitTest for local use.
static bool decomposeBitTest(Value *Cond, CmpInst::Predicate &Pred, Value *&X,
Value *&Y, Value *&Z) {
auto Res = llvm::decomposeBitTest(Cond, /*LookThroughTrunc=*/true,
/*AllowNonZeroC=*/true);
auto Res =
llvm::decomposeBitTest(Cond, /*LookThroughTrunc=*/true,
/*AllowNonZeroC=*/true, /*DecomposeAnd=*/true);
if (!Res)
return false;

Expand Down
13 changes: 13 additions & 0 deletions llvm/test/Transforms/InstCombine/and-or-icmps.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3721,3 +3721,16 @@ define i1 @merge_range_check_or(i8 %a) {
%and = or i1 %cmp1, %cmp2
ret i1 %and
}

; Just a very complicated way of checking if v1 == 0.
define i1 @complicated_zero_equality_test(i64 %v1) {
; CHECK-LABEL: @complicated_zero_equality_test(
Comment thread
wermos marked this conversation as resolved.
; CHECK-NEXT: [[V5:%.*]] = icmp eq i64 [[V1:%.*]], 0
; CHECK-NEXT: ret i1 [[V5]]
;
%v2 = trunc i64 %v1 to i32
%v3 = icmp eq i32 %v2, 0
%v4 = icmp ult i64 %v1, 4294967296 ; 2 ^ 32
%v5 = and i1 %v4, %v3
ret i1 %v5
}
4 changes: 1 addition & 3 deletions llvm/test/Transforms/InstCombine/icmp-logical.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1793,9 +1793,7 @@ define <2 x i1> @masked_icmps_bmask_notmixed_or_vec(<2 x i8> %A) {
define <2 x i1> @masked_icmps_bmask_notmixed_or_vec_poison1(<2 x i8> %A) {
; CHECK-LABEL: @masked_icmps_bmask_notmixed_or_vec_poison1(
; CHECK-NEXT: [[MASK1:%.*]] = and <2 x i8> [[A:%.*]], splat (i8 15)
; CHECK-NEXT: [[TST1:%.*]] = icmp eq <2 x i8> [[MASK1]], <i8 3, i8 poison>
; CHECK-NEXT: [[TST2:%.*]] = icmp eq <2 x i8> [[A]], splat (i8 -13)
; CHECK-NEXT: [[RES:%.*]] = or <2 x i1> [[TST1]], [[TST2]]
; CHECK-NEXT: [[RES:%.*]] = icmp eq <2 x i8> [[MASK1]], splat (i8 3)
; CHECK-NEXT: ret <2 x i1> [[RES]]
;
%mask1 = and <2 x i8> %A, <i8 15, i8 15> ; 0x0f
Expand Down
18 changes: 6 additions & 12 deletions llvm/test/Transforms/InstCombine/merge-icmp.ll
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,8 @@ define i1 @or_extra_use1(i16 %load) {
; CHECK-NEXT: [[TRUNC:%.*]] = trunc i16 [[LOAD:%.*]] to i8
; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i8 [[TRUNC]], 127
; CHECK-NEXT: call void @use.i1(i1 [[CMP1]])
; CHECK-NEXT: [[AND:%.*]] = and i16 [[LOAD]], -4096
; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i16 [[AND]], 20480
; CHECK-NEXT: [[OR:%.*]] = or i1 [[CMP1]], [[CMP2]]
; CHECK-NEXT: [[TMP1:%.*]] = and i16 [[LOAD]], -3841
; CHECK-NEXT: [[OR:%.*]] = icmp ne i16 [[TMP1]], 20607
; CHECK-NEXT: ret i1 [[OR]]
;
%trunc = trunc i16 %load to i8
Expand All @@ -183,12 +182,11 @@ define i1 @or_extra_use1(i16 %load) {

define i1 @or_extra_use2(i16 %load) {
; CHECK-LABEL: @or_extra_use2(
; CHECK-NEXT: [[TRUNC:%.*]] = trunc i16 [[LOAD:%.*]] to i8
; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i8 [[TRUNC]], 127
; CHECK-NEXT: [[AND:%.*]] = and i16 [[LOAD]], -4096
; CHECK-NEXT: [[AND:%.*]] = and i16 [[LOAD:%.*]], -4096
; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i16 [[AND]], 20480
; CHECK-NEXT: call void @use.i1(i1 [[CMP2]])
; CHECK-NEXT: [[OR:%.*]] = or i1 [[CMP1]], [[CMP2]]
; CHECK-NEXT: [[TMP1:%.*]] = and i16 [[LOAD]], -3841
; CHECK-NEXT: [[OR:%.*]] = icmp ne i16 [[TMP1]], 20607
; CHECK-NEXT: ret i1 [[OR]]
;
%trunc = trunc i16 %load to i8
Expand Down Expand Up @@ -316,11 +314,7 @@ define i1 @or_wrong_const1(i16 %load) {

define i1 @or_wrong_const2(i16 %load) {
; CHECK-LABEL: @or_wrong_const2(
; CHECK-NEXT: [[TRUNC:%.*]] = trunc i16 [[LOAD:%.*]] to i8
; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i8 [[TRUNC]], 127
; CHECK-NEXT: [[AND:%.*]] = and i16 [[LOAD]], -255
; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i16 [[AND]], 17665
; CHECK-NEXT: [[OR:%.*]] = or i1 [[CMP1]], [[CMP2]]
; CHECK-NEXT: [[OR:%.*]] = icmp ne i16 [[LOAD:%.*]], 17791
; CHECK-NEXT: ret i1 [[OR]]
;
%trunc = trunc i16 %load to i8
Expand Down