Skip to content

Commit 1fa4f6f

Browse files
[LLVM][InstCombine] not (bitcast (cmp A, B) --> bitcast (!cmp A, B)
1 parent 8eb72b6 commit 1fa4f6f

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5096,9 +5096,17 @@ Instruction *InstCombinerImpl::foldNot(BinaryOperator &I) {
50965096
return &I;
50975097
}
50985098

5099+
// not (bitcast (cmp A, B) --> bitcast (!cmp A, B)
5100+
if (match(NotOp, m_OneUse(m_BitCast(m_Value(X)))) &&
5101+
match(X, m_OneUse(m_Cmp(Pred, m_Value(), m_Value())))) {
5102+
cast<CmpInst>(X)->setPredicate(CmpInst::getInversePredicate(Pred));
5103+
return new BitCastInst(X, Ty);
5104+
}
5105+
50995106
// Move a 'not' ahead of casts of a bool to enable logic reduction:
51005107
// not (bitcast (sext i1 X)) --> bitcast (sext (not i1 X))
5101-
if (match(NotOp, m_OneUse(m_BitCast(m_OneUse(m_SExt(m_Value(X)))))) && X->getType()->isIntOrIntVectorTy(1)) {
5108+
if (match(NotOp, m_OneUse(m_BitCast(m_OneUse(m_SExt(m_Value(X)))))) &&
5109+
X->getType()->isIntOrIntVectorTy(1)) {
51025110
Type *SextTy = cast<BitCastOperator>(NotOp)->getSrcTy();
51035111
Value *NotX = Builder.CreateNot(X);
51045112
Value *Sext = Builder.CreateSExt(NotX, SextTy);

llvm/test/Transforms/InstCombine/not.ll

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,10 +1064,9 @@ if.else:
10641064

10651065
define i8 @invert_bitcasted_icmp(<8 x i32> %a, <8 x i32> %b) {
10661066
; CHECK-LABEL: @invert_bitcasted_icmp(
1067-
; CHECK-NEXT: [[CMP:%.*]] = icmp sle <8 x i32> [[A:%.*]], [[B:%.*]]
1067+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <8 x i32> [[A:%.*]], [[B:%.*]]
10681068
; CHECK-NEXT: [[MASK_AS_INT:%.*]] = bitcast <8 x i1> [[CMP]] to i8
1069-
; CHECK-NEXT: [[NOT:%.*]] = xor i8 [[MASK_AS_INT]], -1
1070-
; CHECK-NEXT: ret i8 [[NOT]]
1069+
; CHECK-NEXT: ret i8 [[MASK_AS_INT]]
10711070
;
10721071
%cmp = icmp sle <8 x i32> %a, %b
10731072
%mask.as.int = bitcast <8 x i1> %cmp to i8
@@ -1107,10 +1106,9 @@ define i8 @invert_bitcasted_icmp_multi_use_2(<8 x i32> %a, <8 x i32> %b) {
11071106

11081107
define i8 @invert_bitcasted_fcmp(<8 x float> %a, <8 x float> %b) {
11091108
; CHECK-LABEL: @invert_bitcasted_fcmp(
1110-
; CHECK-NEXT: [[CMP:%.*]] = fcmp olt <8 x float> [[A:%.*]], [[B:%.*]]
1109+
; CHECK-NEXT: [[CMP:%.*]] = fcmp uge <8 x float> [[A:%.*]], [[B:%.*]]
11111110
; CHECK-NEXT: [[MASK_AS_INT:%.*]] = bitcast <8 x i1> [[CMP]] to i8
1112-
; CHECK-NEXT: [[NOT:%.*]] = xor i8 [[MASK_AS_INT]], -1
1113-
; CHECK-NEXT: ret i8 [[NOT]]
1111+
; CHECK-NEXT: ret i8 [[MASK_AS_INT]]
11141112
;
11151113
%cmp = fcmp olt <8 x float> %a, %b
11161114
%mask.as.int = bitcast <8 x i1> %cmp to i8

0 commit comments

Comments
 (0)