Skip to content

Commit 8291461

Browse files
committed
[InstCombine] change 'not' match for bitwise select
The tests diffs are logically equivalent, and so this is generally NFC, but this makes the code match the code comment. It should also be more efficient. If we choose the 'not' operand (rather than the 'not' instruction) as the select condition, then we don't have to invert the select condition/operands as a subsequent transform.
1 parent e178b56 commit 8291461

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,18 +2292,18 @@ static bool areInverseVectorBitmasks(Constant *C1, Constant *C2) {
22922292
/// vector composed of all-zeros or all-ones values and is the bitwise 'not' of
22932293
/// B, it can be used as the condition operand of a select instruction.
22942294
Value *InstCombinerImpl::getSelectCondition(Value *A, Value *B) {
2295-
// Step 1: We may have peeked through bitcasts in the caller.
2295+
// We may have peeked through bitcasts in the caller.
22962296
// Exit immediately if we don't have (vector) integer types.
22972297
Type *Ty = A->getType();
22982298
if (!Ty->isIntOrIntVectorTy() || !B->getType()->isIntOrIntVectorTy())
22992299
return nullptr;
23002300

2301-
// Step 2: We need 0 or all-1's bitmasks.
2301+
// We need 0 or all-1's bitmasks.
23022302
if (ComputeNumSignBits(A) != Ty->getScalarSizeInBits())
23032303
return nullptr;
23042304

2305-
// Step 3: If B is the 'not' value of A, we have our answer.
2306-
if (match(A, m_Not(m_Specific(B)))) {
2305+
// If B is the 'not' value of A, we have our answer.
2306+
if (match(B, m_Not(m_Specific(A)))) {
23072307
// If these are scalars or vectors of i1, A can be used directly.
23082308
if (Ty->isIntOrIntVectorTy(1))
23092309
return A;

llvm/test/Transforms/InstCombine/logical-select-inseltpoison.ll

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ define i32 @foo(i32 %a, i32 %b, i32 %c, i32 %d) {
1919

2020
define i32 @bar(i32 %a, i32 %b, i32 %c, i32 %d) {
2121
; CHECK-LABEL: @bar(
22-
; CHECK-NEXT: [[E_NOT:%.*]] = icmp slt i32 [[A:%.*]], [[B:%.*]]
23-
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[E_NOT]], i32 [[C:%.*]], i32 [[D:%.*]]
22+
; CHECK-NEXT: [[E:%.*]] = icmp slt i32 [[A:%.*]], [[B:%.*]]
23+
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[E]], i32 [[C:%.*]], i32 [[D:%.*]]
2424
; CHECK-NEXT: ret i32 [[TMP1]]
2525
;
2626
%e = icmp slt i32 %a, %b
@@ -472,9 +472,9 @@ define <4 x i1> @vec_of_bools(<4 x i1> %a, <4 x i1> %b, <4 x i1> %c) {
472472

473473
define i4 @vec_of_casted_bools(i4 %a, i4 %b, <4 x i1> %c) {
474474
; CHECK-LABEL: @vec_of_casted_bools(
475-
; CHECK-NEXT: [[TMP1:%.*]] = bitcast i4 [[A:%.*]] to <4 x i1>
476-
; CHECK-NEXT: [[TMP2:%.*]] = bitcast i4 [[B:%.*]] to <4 x i1>
477-
; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[C:%.*]], <4 x i1> [[TMP2]], <4 x i1> [[TMP1]]
475+
; CHECK-NEXT: [[TMP1:%.*]] = bitcast i4 [[B:%.*]] to <4 x i1>
476+
; CHECK-NEXT: [[TMP2:%.*]] = bitcast i4 [[A:%.*]] to <4 x i1>
477+
; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[C:%.*]], <4 x i1> [[TMP1]], <4 x i1> [[TMP2]]
478478
; CHECK-NEXT: [[TMP4:%.*]] = bitcast <4 x i1> [[TMP3]] to i4
479479
; CHECK-NEXT: ret i4 [[TMP4]]
480480
;
@@ -582,8 +582,8 @@ define <4 x i32> @vec_sel_xor_multi_use(<4 x i32> %a, <4 x i32> %b, <4 x i1> %c)
582582

583583
define i32 @allSignBits(i32 %cond, i32 %tval, i32 %fval) {
584584
; CHECK-LABEL: @allSignBits(
585-
; CHECK-NEXT: [[DOTNOT:%.*]] = icmp slt i32 [[COND:%.*]], 0
586-
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[DOTNOT]], i32 [[TVAL:%.*]], i32 [[FVAL:%.*]]
585+
; CHECK-NEXT: [[DOTNOT:%.*]] = icmp sgt i32 [[COND:%.*]], -1
586+
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[DOTNOT]], i32 [[FVAL:%.*]], i32 [[TVAL:%.*]]
587587
; CHECK-NEXT: ret i32 [[TMP1]]
588588
;
589589
%bitmask = ashr i32 %cond, 31

llvm/test/Transforms/InstCombine/logical-select.ll

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ define i32 @foo(i32 %a, i32 %b, i32 %c, i32 %d) {
1919

2020
define i32 @bar(i32 %a, i32 %b, i32 %c, i32 %d) {
2121
; CHECK-LABEL: @bar(
22-
; CHECK-NEXT: [[E_NOT:%.*]] = icmp slt i32 [[A:%.*]], [[B:%.*]]
23-
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[E_NOT]], i32 [[C:%.*]], i32 [[D:%.*]]
22+
; CHECK-NEXT: [[E:%.*]] = icmp slt i32 [[A:%.*]], [[B:%.*]]
23+
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[E]], i32 [[C:%.*]], i32 [[D:%.*]]
2424
; CHECK-NEXT: ret i32 [[TMP1]]
2525
;
2626
%e = icmp slt i32 %a, %b
@@ -472,9 +472,9 @@ define <4 x i1> @vec_of_bools(<4 x i1> %a, <4 x i1> %b, <4 x i1> %c) {
472472

473473
define i4 @vec_of_casted_bools(i4 %a, i4 %b, <4 x i1> %c) {
474474
; CHECK-LABEL: @vec_of_casted_bools(
475-
; CHECK-NEXT: [[TMP1:%.*]] = bitcast i4 [[A:%.*]] to <4 x i1>
476-
; CHECK-NEXT: [[TMP2:%.*]] = bitcast i4 [[B:%.*]] to <4 x i1>
477-
; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[C:%.*]], <4 x i1> [[TMP2]], <4 x i1> [[TMP1]]
475+
; CHECK-NEXT: [[TMP1:%.*]] = bitcast i4 [[B:%.*]] to <4 x i1>
476+
; CHECK-NEXT: [[TMP2:%.*]] = bitcast i4 [[A:%.*]] to <4 x i1>
477+
; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[C:%.*]], <4 x i1> [[TMP1]], <4 x i1> [[TMP2]]
478478
; CHECK-NEXT: [[TMP4:%.*]] = bitcast <4 x i1> [[TMP3]] to i4
479479
; CHECK-NEXT: ret i4 [[TMP4]]
480480
;
@@ -582,8 +582,8 @@ define <4 x i32> @vec_sel_xor_multi_use(<4 x i32> %a, <4 x i32> %b, <4 x i1> %c)
582582

583583
define i32 @allSignBits(i32 %cond, i32 %tval, i32 %fval) {
584584
; CHECK-LABEL: @allSignBits(
585-
; CHECK-NEXT: [[DOTNOT:%.*]] = icmp slt i32 [[COND:%.*]], 0
586-
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[DOTNOT]], i32 [[TVAL:%.*]], i32 [[FVAL:%.*]]
585+
; CHECK-NEXT: [[DOTNOT:%.*]] = icmp sgt i32 [[COND:%.*]], -1
586+
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[DOTNOT]], i32 [[FVAL:%.*]], i32 [[TVAL:%.*]]
587587
; CHECK-NEXT: ret i32 [[TMP1]]
588588
;
589589
%bitmask = ashr i32 %cond, 31

0 commit comments

Comments
 (0)