Skip to content

Commit be9e3d9

Browse files
committed
[InstCombine] reduce demand-limited bool math to logic, part 2
Follow-on suggested in: D75961
1 parent 586565c commit be9e3d9

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -456,22 +456,39 @@ Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
456456
case Instruction::Add:
457457
if ((DemandedMask & 1) == 0) {
458458
// If we do not need the low bit, try to convert bool math to logic:
459-
// add iN (zext i1 X), (sext i1 Y) --> sext (~X & Y) to iN
460-
// Truth table for inputs and output signbits:
461-
// X:0 | X:1
462-
// ----------
463-
// Y:0 | 0 | 0 |
464-
// Y:1 | -1 | 0 |
465-
// ----------
459+
// add iN (zext i1 X), (sext i1 Y) --> sext (~X & Y) to iN
466460
Value *X, *Y;
467461
if (match(I, m_c_Add(m_OneUse(m_ZExt(m_Value(X))),
468462
m_OneUse(m_SExt(m_Value(Y))))) &&
469463
X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType()) {
464+
// Truth table for inputs and output signbits:
465+
// X:0 | X:1
466+
// ----------
467+
// Y:0 | 0 | 0 |
468+
// Y:1 | -1 | 0 |
469+
// ----------
470470
IRBuilderBase::InsertPointGuard Guard(Builder);
471471
Builder.SetInsertPoint(I);
472472
Value *AndNot = Builder.CreateAnd(Builder.CreateNot(X), Y);
473473
return Builder.CreateSExt(AndNot, VTy);
474474
}
475+
476+
// add iN (sext i1 X), (sext i1 Y) --> sext (X | Y) to iN
477+
// TODO: Relax the one-use checks because we are removing an instruction?
478+
if (match(I, m_Add(m_OneUse(m_SExt(m_Value(X))),
479+
m_OneUse(m_SExt(m_Value(Y))))) &&
480+
X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType()) {
481+
// Truth table for inputs and output signbits:
482+
// X:0 | X:1
483+
// -----------
484+
// Y:0 | -1 | -1 |
485+
// Y:1 | -1 | 0 |
486+
// -----------
487+
IRBuilderBase::InsertPointGuard Guard(Builder);
488+
Builder.SetInsertPoint(I);
489+
Value *Or = Builder.CreateOr(X, Y);
490+
return Builder.CreateSExt(Or, VTy);
491+
}
475492
}
476493
LLVM_FALLTHROUGH;
477494
case Instruction::Sub: {

llvm/test/Transforms/InstCombine/add.ll

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,10 +1184,8 @@ define i32 @lshr_add_use2(i1 %x, i1 %y, i32* %p) {
11841184

11851185
define i32 @lshr_add_sexts(i1 %x, i1 %y) {
11861186
; CHECK-LABEL: @lshr_add_sexts(
1187-
; CHECK-NEXT: [[XS:%.*]] = sext i1 [[X:%.*]] to i32
1188-
; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
1189-
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XS]], [[YS]]
1190-
; CHECK-NEXT: [[R:%.*]] = lshr i32 [[SUB]], 31
1187+
; CHECK-NEXT: [[TMP1:%.*]] = or i1 [[X:%.*]], [[Y:%.*]]
1188+
; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP1]] to i32
11911189
; CHECK-NEXT: ret i32 [[R]]
11921190
;
11931191
%xs = sext i1 %x to i32
@@ -1199,10 +1197,8 @@ define i32 @lshr_add_sexts(i1 %x, i1 %y) {
11991197

12001198
define i5 @and_add_sexts(i1 %x, i1 %y) {
12011199
; CHECK-LABEL: @and_add_sexts(
1202-
; CHECK-NEXT: [[XS:%.*]] = sext i1 [[X:%.*]] to i5
1203-
; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i5
1204-
; CHECK-NEXT: [[SUB:%.*]] = add nsw i5 [[XS]], [[YS]]
1205-
; CHECK-NEXT: [[R:%.*]] = and i5 [[SUB]], -2
1200+
; CHECK-NEXT: [[TMP1:%.*]] = or i1 [[X:%.*]], [[Y:%.*]]
1201+
; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i5 -2, i5 0
12061202
; CHECK-NEXT: ret i5 [[R]]
12071203
;
12081204
%xs = sext i1 %x to i5
@@ -1214,11 +1210,9 @@ define i5 @and_add_sexts(i1 %x, i1 %y) {
12141210

12151211
define <2 x i8> @ashr_add_sexts(<2 x i1> %x, <2 x i1> %y) {
12161212
; CHECK-LABEL: @ashr_add_sexts(
1217-
; CHECK-NEXT: [[XS:%.*]] = sext <2 x i1> [[X:%.*]] to <2 x i8>
1218-
; CHECK-NEXT: [[YS:%.*]] = sext <2 x i1> [[Y:%.*]] to <2 x i8>
1219-
; CHECK-NEXT: [[SUB:%.*]] = add nsw <2 x i8> [[YS]], [[XS]]
1220-
; CHECK-NEXT: [[R:%.*]] = ashr <2 x i8> [[SUB]], <i8 1, i8 1>
1221-
; CHECK-NEXT: ret <2 x i8> [[R]]
1213+
; CHECK-NEXT: [[TMP1:%.*]] = or <2 x i1> [[Y:%.*]], [[X:%.*]]
1214+
; CHECK-NEXT: [[TMP2:%.*]] = sext <2 x i1> [[TMP1]] to <2 x i8>
1215+
; CHECK-NEXT: ret <2 x i8> [[TMP2]]
12221216
;
12231217
%xs = sext <2 x i1> %x to <2 x i8>
12241218
%ys = sext <2 x i1> %y to <2 x i8>
@@ -1229,12 +1223,8 @@ define <2 x i8> @ashr_add_sexts(<2 x i1> %x, <2 x i1> %y) {
12291223

12301224
define i32 @cmp_math_sexts(i32 %x, i32 %y) {
12311225
; CHECK-LABEL: @cmp_math_sexts(
1232-
; CHECK-NEXT: [[GT:%.*]] = icmp ugt i32 [[X:%.*]], [[Y:%.*]]
1233-
; CHECK-NEXT: [[LT:%.*]] = icmp ult i32 [[X]], [[Y]]
1234-
; CHECK-NEXT: [[XZ:%.*]] = sext i1 [[GT]] to i32
1235-
; CHECK-NEXT: [[TMP1:%.*]] = sext i1 [[LT]] to i32
1236-
; CHECK-NEXT: [[S:%.*]] = add nsw i32 [[XZ]], [[TMP1]]
1237-
; CHECK-NEXT: [[R:%.*]] = lshr i32 [[S]], 31
1226+
; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[X:%.*]], [[Y:%.*]]
1227+
; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP1]] to i32
12381228
; CHECK-NEXT: ret i32 [[R]]
12391229
;
12401230
%gt = icmp ugt i32 %x, %y

0 commit comments

Comments
 (0)