Skip to content

Commit 3a12613

Browse files
committed
[InstCombine] remove casts from splat-a-bit pattern
https://alive2.llvm.org/ce/z/_AivbM This case seems clear since we can reduce instruction count and avoid an intermediate type change, but we might want to use mask-and-compare for other sequences. Currently, we can generate more instructions on some related patterns by trying to use bit-hacks instead of mask+cmp, so something is not behaving as expected.
1 parent b78c85a commit 3a12613

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,18 @@ Instruction *InstCombinerImpl::visitSExt(SExtInst &CI) {
15911591
return BinaryOperator::CreateAShr(A, NewShAmt);
15921592
}
15931593

1594+
// Splatting a bit of constant-index across a value:
1595+
// sext (ashr (trunc iN X to iM), M-1) to iN --> ashr (shl X, N-M), N-1
1596+
// TODO: If the dest type is different, use a cast (adjust use check).
1597+
if (match(Src, m_OneUse(m_AShr(m_Trunc(m_Value(X)),
1598+
m_SpecificInt(SrcBitSize - 1)))) &&
1599+
X->getType() == DestTy) {
1600+
Constant *ShlAmtC = ConstantInt::get(DestTy, DestBitSize - SrcBitSize);
1601+
Constant *AshrAmtC = ConstantInt::get(DestTy, DestBitSize - 1);
1602+
Value *Shl = Builder.CreateShl(X, ShlAmtC);
1603+
return BinaryOperator::CreateAShr(Shl, AshrAmtC);
1604+
}
1605+
15941606
if (match(Src, m_VScale(DL))) {
15951607
if (CI.getFunction()->hasFnAttribute(Attribute::VScaleRange)) {
15961608
unsigned MaxVScale = CI.getFunction()

llvm/test/Transforms/InstCombine/sext.ll

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,8 @@ define i10 @test19(i10 %i) {
323323

324324
define i32 @smear_set_bit(i32 %x) {
325325
; CHECK-LABEL: @smear_set_bit(
326-
; CHECK-NEXT: [[T:%.*]] = trunc i32 [[X:%.*]] to i8
327-
; CHECK-NEXT: [[A:%.*]] = ashr i8 [[T]], 7
328-
; CHECK-NEXT: [[S:%.*]] = sext i8 [[A]] to i32
326+
; CHECK-NEXT: [[TMP1:%.*]] = shl i32 [[X:%.*]], 24
327+
; CHECK-NEXT: [[S:%.*]] = ashr i32 [[TMP1]], 31
329328
; CHECK-NEXT: ret i32 [[S]]
330329
;
331330
%t = trunc i32 %x to i8
@@ -334,12 +333,14 @@ define i32 @smear_set_bit(i32 %x) {
334333
ret i32 %s
335334
}
336335

336+
; extra use of trunc is ok because we still shorten the use chain
337+
337338
define <2 x i32> @smear_set_bit_vec_use1(<2 x i32> %x) {
338339
; CHECK-LABEL: @smear_set_bit_vec_use1(
339340
; CHECK-NEXT: [[T:%.*]] = trunc <2 x i32> [[X:%.*]] to <2 x i5>
340341
; CHECK-NEXT: call void @use_vec(<2 x i5> [[T]])
341-
; CHECK-NEXT: [[A:%.*]] = ashr <2 x i5> [[T]], <i5 4, i5 4>
342-
; CHECK-NEXT: [[S:%.*]] = sext <2 x i5> [[A]] to <2 x i32>
342+
; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i32> [[X]], <i32 27, i32 27>
343+
; CHECK-NEXT: [[S:%.*]] = ashr <2 x i32> [[TMP1]], <i32 31, i32 31>
343344
; CHECK-NEXT: ret <2 x i32> [[S]]
344345
;
345346
%t = trunc <2 x i32> %x to <2 x i5>
@@ -349,6 +350,8 @@ define <2 x i32> @smear_set_bit_vec_use1(<2 x i32> %x) {
349350
ret <2 x i32> %s
350351
}
351352

353+
; negative test - extra use
354+
352355
define i32 @smear_set_bit_use2(i32 %x) {
353356
; CHECK-LABEL: @smear_set_bit_use2(
354357
; CHECK-NEXT: [[T:%.*]] = trunc i32 [[X:%.*]] to i8
@@ -364,6 +367,8 @@ define i32 @smear_set_bit_use2(i32 %x) {
364367
ret i32 %s
365368
}
366369

370+
; negative test - must shift all the way across
371+
367372
define i32 @smear_set_bit_wrong_shift_amount(i32 %x) {
368373
; CHECK-LABEL: @smear_set_bit_wrong_shift_amount(
369374
; CHECK-NEXT: [[T:%.*]] = trunc i32 [[X:%.*]] to i8
@@ -377,6 +382,8 @@ define i32 @smear_set_bit_wrong_shift_amount(i32 %x) {
377382
ret i32 %s
378383
}
379384

385+
; TODO: this could be mask+compare+sext or shifts+trunc
386+
380387
define i16 @smear_set_bit_different_dest_type(i32 %x) {
381388
; CHECK-LABEL: @smear_set_bit_different_dest_type(
382389
; CHECK-NEXT: [[T:%.*]] = trunc i32 [[X:%.*]] to i8

0 commit comments

Comments
 (0)