Skip to content

Commit 0108630

Browse files
frasercrmcktstellar
authored andcommitted
[InstCombine] Fix scalable-vector bitwise select matching
D113035 enhanced the matching of bitwise selects from vector types. This change unfortunately introduced crashes as it tries to cast scalable vector types to integers. Reviewed By: spatel Differential Revision: https://reviews.llvm.org/D124997 (cherry picked from commit bafab9c)
1 parent 39e9097 commit 0108630

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2488,8 +2488,12 @@ Value *InstCombinerImpl::matchSelectFromAndOr(Value *A, Value *C, Value *B,
24882488
// not create unnecessary casts if the types already match.
24892489
Type *SelTy = A->getType();
24902490
if (auto *VecTy = dyn_cast<VectorType>(Cond->getType())) {
2491+
// For a fixed or scalable vector get N from <{vscale x} N x iM>
24912492
unsigned Elts = VecTy->getElementCount().getKnownMinValue();
2492-
Type *EltTy = Builder.getIntNTy(SelTy->getPrimitiveSizeInBits() / Elts);
2493+
// For a fixed or scalable vector, get the size in bits of N x iM; for a
2494+
// scalar this is just M.
2495+
unsigned SelEltSize = SelTy->getPrimitiveSizeInBits().getKnownMinSize();
2496+
Type *EltTy = Builder.getIntNTy(SelEltSize / Elts);
24932497
SelTy = VectorType::get(EltTy, VecTy->getElementCount());
24942498
}
24952499
Value *BitcastC = Builder.CreateBitCast(C, SelTy);

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,18 @@ define <4 x i1> @vec_of_bools(<4 x i1> %a, <4 x i1> %b, <4 x i1> %c) {
471471
ret <4 x i1> %or
472472
}
473473

474+
define <vscale x 1 x i1> @vec_of_bools_scalable(<vscale x 1 x i1> %a, <vscale x 1 x i1> %c, <vscale x 1 x i1> %d) {
475+
; CHECK-LABEL: @vec_of_bools_scalable(
476+
; CHECK-NEXT: [[TMP1:%.*]] = select <vscale x 1 x i1> [[A:%.*]], <vscale x 1 x i1> [[C:%.*]], <vscale x 1 x i1> [[D:%.*]]
477+
; CHECK-NEXT: ret <vscale x 1 x i1> [[TMP1]]
478+
;
479+
%b = xor <vscale x 1 x i1> %a, shufflevector (<vscale x 1 x i1> insertelement (<vscale x 1 x i1> poison, i1 true, i32 0), <vscale x 1 x i1> poison, <vscale x 1 x i32> zeroinitializer)
480+
%t11 = and <vscale x 1 x i1> %a, %c
481+
%t12 = and <vscale x 1 x i1> %b, %d
482+
%r = or <vscale x 1 x i1> %t11, %t12
483+
ret <vscale x 1 x i1> %r
484+
}
485+
474486
define i4 @vec_of_casted_bools(i4 %a, i4 %b, <4 x i1> %c) {
475487
; CHECK-LABEL: @vec_of_casted_bools(
476488
; CHECK-NEXT: [[TMP1:%.*]] = bitcast i4 [[B:%.*]] to <4 x i1>
@@ -488,6 +500,25 @@ define i4 @vec_of_casted_bools(i4 %a, i4 %b, <4 x i1> %c) {
488500
ret i4 %or
489501
}
490502

503+
define <vscale x 1 x i64> @vec_of_casted_bools_scalable(<vscale x 1 x i64> %a, <vscale x 1 x i64> %b, <vscale x 8 x i1> %cond) {
504+
; CHECK-LABEL: @vec_of_casted_bools_scalable(
505+
; CHECK-NEXT: [[TMP1:%.*]] = bitcast <vscale x 1 x i64> [[A:%.*]] to <vscale x 8 x i8>
506+
; CHECK-NEXT: [[TMP2:%.*]] = bitcast <vscale x 1 x i64> [[B:%.*]] to <vscale x 8 x i8>
507+
; CHECK-NEXT: [[TMP3:%.*]] = select <vscale x 8 x i1> [[COND:%.*]], <vscale x 8 x i8> [[TMP1]], <vscale x 8 x i8> [[TMP2]]
508+
; CHECK-NEXT: [[TMP4:%.*]] = bitcast <vscale x 8 x i8> [[TMP3]] to <vscale x 1 x i64>
509+
; CHECK-NEXT: ret <vscale x 1 x i64> [[TMP4]]
510+
;
511+
%scond = sext <vscale x 8 x i1> %cond to <vscale x 8 x i8>
512+
%notcond = xor <vscale x 8 x i1> %cond, shufflevector (<vscale x 8 x i1> insertelement (<vscale x 8 x i1> poison, i1 true, i32 0), <vscale x 8 x i1> poison, <vscale x 8 x i32> zeroinitializer)
513+
%snotcond = sext <vscale x 8 x i1> %notcond to <vscale x 8 x i8>
514+
%bc1 = bitcast <vscale x 8 x i8> %scond to <vscale x 1 x i64>
515+
%bc2 = bitcast <vscale x 8 x i8> %snotcond to <vscale x 1 x i64>
516+
%and1 = and <vscale x 1 x i64> %a, %bc1
517+
%and2 = and <vscale x 1 x i64> %bc2, %b
518+
%or = or <vscale x 1 x i64> %and1, %and2
519+
ret <vscale x 1 x i64> %or
520+
}
521+
491522
; Inverted 'and' constants mean this is a select which is canonicalized to a shuffle.
492523

493524
define <4 x i32> @vec_sel_consts(<4 x i32> %a, <4 x i32> %b) {

0 commit comments

Comments
 (0)