Skip to content

Commit 620cff0

Browse files
committed
[InstCombine] Fold series of instructions into mull for more types
Relax the constraint of wide/vectors types. Address the comment https://reviews.llvm.org/D136015?id=469189#inline-1314520 Reviewed By: spatel, chfast Differential Revision: https://reviews.llvm.org/D136661
1 parent c094b1e commit 620cff0

File tree

3 files changed

+11
-45
lines changed

3 files changed

+11
-45
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,13 +1270,9 @@ static Instruction *factorizeMathWithShlOps(BinaryOperator &I,
12701270
/// Reduce a sequence of masked half-width multiplies to a single multiply.
12711271
/// ((XLow * YHigh) + (YLow * XHigh)) << HalfBits) + (XLow * YLow) --> X * Y
12721272
static Instruction *foldBoxMultiply(BinaryOperator &I) {
1273-
if (!I.getType()->isIntegerTy())
1274-
return nullptr;
1275-
12761273
unsigned BitWidth = I.getType()->getScalarSizeInBits();
1277-
// Skip the odd bitwidth types and large bitwidth types
1278-
// TODO: Relax the constraint of wide/vectors types.
1279-
if ((BitWidth & 0x1) || (BitWidth > 128))
1274+
// Skip the odd bitwidth types.
1275+
if ((BitWidth & 0x1))
12801276
return nullptr;
12811277

12821278
unsigned HalfBits = BitWidth >> 1;

llvm/test/Transforms/InstCombine/mul_fold.ll

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -521,19 +521,10 @@ define i128 @mul128_low(i128 %in0, i128 %in1) {
521521
ret i128 %retLo
522522
}
523523

524-
; TODO: Skip vector type
524+
; Support vector type
525525
define <2 x i8> @mul_v2i8_low(<2 x i8> %in0, <2 x i8> %in1) {
526526
; CHECK-LABEL: @mul_v2i8_low(
527-
; CHECK-NEXT: [[IN0LO:%.*]] = and <2 x i8> [[IN0:%.*]], <i8 15, i8 15>
528-
; CHECK-NEXT: [[IN0HI:%.*]] = lshr <2 x i8> [[IN0]], <i8 4, i8 4>
529-
; CHECK-NEXT: [[IN1LO:%.*]] = and <2 x i8> [[IN1:%.*]], <i8 15, i8 15>
530-
; CHECK-NEXT: [[IN1HI:%.*]] = lshr <2 x i8> [[IN1]], <i8 4, i8 4>
531-
; CHECK-NEXT: [[M10:%.*]] = mul <2 x i8> [[IN1HI]], [[IN0]]
532-
; CHECK-NEXT: [[M01:%.*]] = mul <2 x i8> [[IN0HI]], [[IN1]]
533-
; CHECK-NEXT: [[M00:%.*]] = mul nuw <2 x i8> [[IN1LO]], [[IN0LO]]
534-
; CHECK-NEXT: [[ADDC:%.*]] = add <2 x i8> [[M10]], [[M01]]
535-
; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i8> [[ADDC]], <i8 4, i8 4>
536-
; CHECK-NEXT: [[RETLO:%.*]] = add <2 x i8> [[SHL]], [[M00]]
527+
; CHECK-NEXT: [[RETLO:%.*]] = mul <2 x i8> [[IN0:%.*]], [[IN1:%.*]]
537528
; CHECK-NEXT: ret <2 x i8> [[RETLO]]
538529
;
539530
%In0Lo = and <2 x i8> %in0, <i8 15, i8 15>
@@ -551,17 +542,11 @@ define <2 x i8> @mul_v2i8_low(<2 x i8> %in0, <2 x i8> %in1) {
551542

552543
define <2 x i8> @mul_v2i8_low_one_extra_user(<2 x i8> %in0, <2 x i8> %in1) {
553544
; CHECK-LABEL: @mul_v2i8_low_one_extra_user(
554-
; CHECK-NEXT: [[IN0LO:%.*]] = and <2 x i8> [[IN0:%.*]], <i8 15, i8 15>
555-
; CHECK-NEXT: [[IN0HI:%.*]] = lshr <2 x i8> [[IN0]], <i8 4, i8 4>
545+
; CHECK-NEXT: [[IN0HI:%.*]] = lshr <2 x i8> [[IN0:%.*]], <i8 4, i8 4>
556546
; CHECK-NEXT: [[IN1LO:%.*]] = and <2 x i8> [[IN1:%.*]], <i8 15, i8 15>
557-
; CHECK-NEXT: [[IN1HI:%.*]] = lshr <2 x i8> [[IN1]], <i8 4, i8 4>
558-
; CHECK-NEXT: [[M10:%.*]] = mul <2 x i8> [[IN1HI]], [[IN0]]
559547
; CHECK-NEXT: [[M01:%.*]] = mul nuw <2 x i8> [[IN1LO]], [[IN0HI]]
560548
; CHECK-NEXT: call void @use_v2i8(<2 x i8> [[M01]])
561-
; CHECK-NEXT: [[M00:%.*]] = mul nuw <2 x i8> [[IN1LO]], [[IN0LO]]
562-
; CHECK-NEXT: [[ADDC:%.*]] = add <2 x i8> [[M10]], [[M01]]
563-
; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i8> [[ADDC]], <i8 4, i8 4>
564-
; CHECK-NEXT: [[RETLO:%.*]] = add <2 x i8> [[SHL]], [[M00]]
549+
; CHECK-NEXT: [[RETLO:%.*]] = mul <2 x i8> [[IN0]], [[IN1]]
565550
; CHECK-NEXT: ret <2 x i8> [[RETLO]]
566551
;
567552
%In0Lo = and <2 x i8> %in0, <i8 15, i8 15>
@@ -578,19 +563,10 @@ define <2 x i8> @mul_v2i8_low_one_extra_user(<2 x i8> %in0, <2 x i8> %in1) {
578563
ret <2 x i8> %retLo
579564
}
580565

581-
; TODO: Support wide width
566+
; Support wide width
582567
define i130 @mul130_low(i130 %in0, i130 %in1) {
583568
; CHECK-LABEL: @mul130_low(
584-
; CHECK-NEXT: [[IN0LO:%.*]] = and i130 [[IN0:%.*]], 36893488147419103231
585-
; CHECK-NEXT: [[IN0HI:%.*]] = lshr i130 [[IN0]], 65
586-
; CHECK-NEXT: [[IN1LO:%.*]] = and i130 [[IN1:%.*]], 36893488147419103231
587-
; CHECK-NEXT: [[IN1HI:%.*]] = lshr i130 [[IN1]], 65
588-
; CHECK-NEXT: [[M10:%.*]] = mul i130 [[IN1HI]], [[IN0]]
589-
; CHECK-NEXT: [[M01:%.*]] = mul i130 [[IN0HI]], [[IN1]]
590-
; CHECK-NEXT: [[M00:%.*]] = mul nuw i130 [[IN1LO]], [[IN0LO]]
591-
; CHECK-NEXT: [[ADDC:%.*]] = add i130 [[M10]], [[M01]]
592-
; CHECK-NEXT: [[SHL:%.*]] = shl i130 [[ADDC]], 65
593-
; CHECK-NEXT: [[RETLO:%.*]] = add i130 [[SHL]], [[M00]]
569+
; CHECK-NEXT: [[RETLO:%.*]] = mul i130 [[IN0:%.*]], [[IN1:%.*]]
594570
; CHECK-NEXT: ret i130 [[RETLO]]
595571
;
596572
%In0Lo = and i130 %in0, 36893488147419103231
@@ -609,16 +585,10 @@ define i130 @mul130_low(i130 %in0, i130 %in1) {
609585
define i130 @mul130_low_one_extra_user(i130 %in0, i130 %in1) {
610586
; CHECK-LABEL: @mul130_low_one_extra_user(
611587
; CHECK-NEXT: [[IN0LO:%.*]] = and i130 [[IN0:%.*]], 36893488147419103231
612-
; CHECK-NEXT: [[IN0HI:%.*]] = lshr i130 [[IN0]], 65
613-
; CHECK-NEXT: [[IN1LO:%.*]] = and i130 [[IN1:%.*]], 36893488147419103231
614-
; CHECK-NEXT: [[IN1HI:%.*]] = lshr i130 [[IN1]], 65
588+
; CHECK-NEXT: [[IN1HI:%.*]] = lshr i130 [[IN1:%.*]], 65
615589
; CHECK-NEXT: [[M10:%.*]] = mul nuw i130 [[IN1HI]], [[IN0LO]]
616590
; CHECK-NEXT: call void @use130(i130 [[M10]])
617-
; CHECK-NEXT: [[M01:%.*]] = mul i130 [[IN0HI]], [[IN1]]
618-
; CHECK-NEXT: [[M00:%.*]] = mul nuw i130 [[IN1LO]], [[IN0LO]]
619-
; CHECK-NEXT: [[ADDC:%.*]] = add i130 [[M10]], [[M01]]
620-
; CHECK-NEXT: [[SHL:%.*]] = shl i130 [[ADDC]], 65
621-
; CHECK-NEXT: [[RETLO:%.*]] = add i130 [[SHL]], [[M00]]
591+
; CHECK-NEXT: [[RETLO:%.*]] = mul i130 [[IN0]], [[IN1]]
622592
; CHECK-NEXT: ret i130 [[RETLO]]
623593
;
624594
%In0Lo = and i130 %in0, 36893488147419103231

llvm/test/Transforms/InstCombine/mul_full_64.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ define i64 @umulh_64(i64 %x, i64 %y) {
448448
ret i64 %hi
449449
}
450450

451-
451+
; TODO: https://alive2.llvm.org/ce/z/y26zaW
452452
define i64 @mullo(i64 %x, i64 %y) {
453453
; CHECK-LABEL: @mullo(
454454
; CHECK-NEXT: [[XL:%.*]] = and i64 [[X:%.*]], 4294967295

0 commit comments

Comments
 (0)