Skip to content

Commit 2e999b7

Browse files
committed
Allow scalable vectors in ComputeNumSignBits and isKnownNonNull
This is a follow up to D136470 which extends the same scheme used there to ComputeNumSignBits and isKnownNonNull. As a reminder, for scalable vectors we track a single bit which is implicitly broadcast to all lanes. We do not know how many lanes there are statically, and thus have to be conservative along paths which require exact sizes. Differential Revision: https://reviews.llvm.org/D137046
1 parent a715b1b commit 2e999b7

File tree

3 files changed

+13
-34
lines changed

3 files changed

+13
-34
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,6 @@ static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
375375

376376
static unsigned ComputeNumSignBits(const Value *V, unsigned Depth,
377377
const Query &Q) {
378-
// FIXME: We currently have no way to represent the DemandedElts of a scalable
379-
// vector
380-
if (isa<ScalableVectorType>(V->getType()))
381-
return 1;
382-
383378
auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
384379
APInt DemandedElts =
385380
FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
@@ -2449,10 +2444,6 @@ static bool isNonZeroRecurrence(const PHINode *PN) {
24492444
/// Supports values with integer or pointer type and vectors of integers.
24502445
bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
24512446
const Query &Q) {
2452-
// FIXME: We currently have no way to represent the DemandedElts of a scalable
2453-
// vector
2454-
if (isa<ScalableVectorType>(V->getType()))
2455-
return false;
24562447

24572448
#ifndef NDEBUG
24582449
Type *Ty = V->getType();
@@ -2574,14 +2565,18 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
25742565
// Note that we have to take special care to avoid looking through
25752566
// truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
25762567
// as casts that can alter the value, e.g., AddrSpaceCasts.
2577-
if (Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedSize() <=
2568+
if (!isa<ScalableVectorType>(I->getOperand(0)->getType()) &&
2569+
!isa<ScalableVectorType>(I->getType()) &&
2570+
Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedSize() <=
25782571
Q.DL.getTypeSizeInBits(I->getType()).getFixedSize())
25792572
return isKnownNonZero(I->getOperand(0), Depth, Q);
25802573
break;
25812574
case Instruction::PtrToInt:
25822575
// Similar to int2ptr above, we can look through ptr2int here if the cast
25832576
// is a no-op or an extend and not a truncate.
2584-
if (Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedSize() <=
2577+
if (!isa<ScalableVectorType>(I->getOperand(0)->getType()) &&
2578+
!isa<ScalableVectorType>(I->getType()) &&
2579+
Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedSize() <=
25852580
Q.DL.getTypeSizeInBits(I->getType()).getFixedSize())
25862581
return isKnownNonZero(I->getOperand(0), Depth, Q);
25872582
break;
@@ -2740,11 +2735,6 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
27402735
}
27412736

27422737
bool isKnownNonZero(const Value* V, unsigned Depth, const Query& Q) {
2743-
// FIXME: We currently have no way to represent the DemandedElts of a scalable
2744-
// vector
2745-
if (isa<ScalableVectorType>(V->getType()))
2746-
return false;
2747-
27482738
auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
27492739
APInt DemandedElts =
27502740
FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
@@ -3096,12 +3086,6 @@ static unsigned ComputeNumSignBitsImpl(const Value *V,
30963086
const APInt &DemandedElts,
30973087
unsigned Depth, const Query &Q) {
30983088
Type *Ty = V->getType();
3099-
3100-
// FIXME: We currently have no way to represent the DemandedElts of a scalable
3101-
// vector
3102-
if (isa<ScalableVectorType>(Ty))
3103-
return 1;
3104-
31053089
#ifndef NDEBUG
31063090
assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
31073091

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -742,13 +742,11 @@ define <2 x i64> @bitcast_vec_cond(<16 x i1> %cond, <2 x i64> %c, <2 x i64> %d)
742742

743743
define <vscale x 2 x i64> @bitcast_vec_cond_scalable(<vscale x 16 x i1> %cond, <vscale x 2 x i64> %c, <vscale x 2 x i64> %d) {
744744
; CHECK-LABEL: @bitcast_vec_cond_scalable(
745-
; CHECK-NEXT: [[S:%.*]] = sext <vscale x 16 x i1> [[COND:%.*]] to <vscale x 16 x i8>
746-
; CHECK-NEXT: [[T9:%.*]] = bitcast <vscale x 16 x i8> [[S]] to <vscale x 2 x i64>
747-
; CHECK-NEXT: [[NOTT9:%.*]] = xor <vscale x 2 x i64> [[T9]], shufflevector (<vscale x 2 x i64> insertelement (<vscale x 2 x i64> poison, i64 -1, i32 0), <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer)
748-
; CHECK-NEXT: [[T11:%.*]] = and <vscale x 2 x i64> [[NOTT9]], [[C:%.*]]
749-
; CHECK-NEXT: [[T12:%.*]] = and <vscale x 2 x i64> [[T9]], [[D:%.*]]
750-
; CHECK-NEXT: [[R:%.*]] = or <vscale x 2 x i64> [[T11]], [[T12]]
751-
; CHECK-NEXT: ret <vscale x 2 x i64> [[R]]
745+
; CHECK-NEXT: [[TMP1:%.*]] = bitcast <vscale x 2 x i64> [[D:%.*]] to <vscale x 16 x i8>
746+
; CHECK-NEXT: [[TMP2:%.*]] = bitcast <vscale x 2 x i64> [[C:%.*]] to <vscale x 16 x i8>
747+
; CHECK-NEXT: [[TMP3:%.*]] = select <vscale x 16 x i1> [[COND:%.*]], <vscale x 16 x i8> [[TMP1]], <vscale x 16 x i8> [[TMP2]]
748+
; CHECK-NEXT: [[TMP4:%.*]] = bitcast <vscale x 16 x i8> [[TMP3]] to <vscale x 2 x i64>
749+
; CHECK-NEXT: ret <vscale x 2 x i64> [[TMP4]]
752750
;
753751
%s = sext <vscale x 16 x i1> %cond to <vscale x 16 x i8>
754752
%t9 = bitcast <vscale x 16 x i8> %s to <vscale x 2 x i64>

llvm/test/Transforms/InstCombine/select.ll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3409,24 +3409,21 @@ define <vscale x 2 x i32> @and_constant_select_svec(<vscale x 2 x i32> %x, <vsca
34093409
ret <vscale x 2 x i32> %b
34103410
}
34113411

3412-
; TODO: shl should be nsw
34133412
define <vscale x 2 x i32> @scalable_sign_bits(<vscale x 2 x i8> %x) {
34143413
; CHECK-LABEL: @scalable_sign_bits(
34153414
; CHECK-NEXT: [[A:%.*]] = sext <vscale x 2 x i8> [[X:%.*]] to <vscale x 2 x i32>
3416-
; CHECK-NEXT: [[B:%.*]] = shl <vscale x 2 x i32> [[A]], shufflevector (<vscale x 2 x i32> insertelement (<vscale x 2 x i32> poison, i32 16, i32 0), <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer)
3415+
; CHECK-NEXT: [[B:%.*]] = shl nsw <vscale x 2 x i32> [[A]], shufflevector (<vscale x 2 x i32> insertelement (<vscale x 2 x i32> poison, i32 16, i32 0), <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer)
34173416
; CHECK-NEXT: ret <vscale x 2 x i32> [[B]]
34183417
;
34193418
%a = sext <vscale x 2 x i8> %x to <vscale x 2 x i32>
34203419
%b = shl <vscale x 2 x i32> %a, shufflevector (<vscale x 2 x i32> insertelement (<vscale x 2 x i32> poison, i32 16, i32 0), <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer)
34213420
ret <vscale x 2 x i32> %b
34223421
}
34233422

3424-
; TODO: can use ult
34253423
define <vscale x 2 x i1> @scalable_non_zero(<vscale x 2 x i32> %x) {
34263424
; CHECK-LABEL: @scalable_non_zero(
34273425
; CHECK-NEXT: [[A:%.*]] = or <vscale x 2 x i32> [[X:%.*]], shufflevector (<vscale x 2 x i32> insertelement (<vscale x 2 x i32> poison, i32 1, i32 0), <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer)
3428-
; CHECK-NEXT: [[B:%.*]] = add nsw <vscale x 2 x i32> [[A]], shufflevector (<vscale x 2 x i32> insertelement (<vscale x 2 x i32> poison, i32 -1, i32 0), <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer)
3429-
; CHECK-NEXT: [[CMP:%.*]] = icmp ult <vscale x 2 x i32> [[B]], shufflevector (<vscale x 2 x i32> insertelement (<vscale x 2 x i32> poison, i32 56, i32 0), <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer)
3426+
; CHECK-NEXT: [[CMP:%.*]] = icmp ule <vscale x 2 x i32> [[A]], shufflevector (<vscale x 2 x i32> insertelement (<vscale x 2 x i32> poison, i32 56, i32 0), <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer)
34303427
; CHECK-NEXT: ret <vscale x 2 x i1> [[CMP]]
34313428
;
34323429
%a = or <vscale x 2 x i32> %x, shufflevector (<vscale x 2 x i32> insertelement (<vscale x 2 x i32> poison, i32 1, i32 0), <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer)

0 commit comments

Comments
 (0)