@@ -303,15 +303,21 @@ bool llvm::isKnownNegative(const Value *V, const SimplifyQuery &SQ,
303
303
return computeKnownBits (V, Depth, SQ).isNegative ();
304
304
}
305
305
306
- static bool isKnownNonEqual (const Value *V1, const Value *V2, unsigned Depth,
306
+ static bool isKnownNonEqual (const Value *V1, const Value *V2,
307
+ const APInt &DemandedElts, unsigned Depth,
307
308
const SimplifyQuery &Q);
308
309
309
310
bool llvm::isKnownNonEqual (const Value *V1, const Value *V2,
310
311
const DataLayout &DL, AssumptionCache *AC,
311
312
const Instruction *CxtI, const DominatorTree *DT,
312
313
bool UseInstrInfo) {
314
+ assert (V1->getType () == V2->getType () &&
315
+ " Testing equality of non-equal types!" );
316
+ auto *FVTy = dyn_cast<FixedVectorType>(V1->getType ());
317
+ APInt DemandedElts =
318
+ FVTy ? APInt::getAllOnes (FVTy->getNumElements ()) : APInt (1 , 1 );
313
319
return ::isKnownNonEqual (
314
- V1, V2, 0 ,
320
+ V1, V2, DemandedElts, 0 ,
315
321
SimplifyQuery (DL, DT, AC, safeCxtI (V2, V1, CxtI), UseInstrInfo));
316
322
}
317
323
@@ -2654,7 +2660,7 @@ static bool isNonZeroSub(const APInt &DemandedElts, unsigned Depth,
2654
2660
if (C->isNullValue () && isKnownNonZero (Y, DemandedElts, Q, Depth))
2655
2661
return true ;
2656
2662
2657
- return ::isKnownNonEqual (X, Y, Depth, Q);
2663
+ return ::isKnownNonEqual (X, Y, DemandedElts, Depth, Q);
2658
2664
}
2659
2665
2660
2666
static bool isNonZeroMul (const APInt &DemandedElts, unsigned Depth,
@@ -2778,8 +2784,11 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
2778
2784
// This all implies the 2 i16 elements are non-zero.
2779
2785
Type *FromTy = I->getOperand (0 )->getType ();
2780
2786
if ((FromTy->isIntOrIntVectorTy () || FromTy->isPtrOrPtrVectorTy ()) &&
2781
- (BitWidth % getBitWidth (FromTy->getScalarType (), Q.DL )) == 0 )
2787
+ (BitWidth % getBitWidth (FromTy->getScalarType (), Q.DL )) == 0 ) {
2788
+ if (match (I, m_ElementWiseBitCast (m_Value ())))
2789
+ return isKnownNonZero (I->getOperand (0 ), DemandedElts, Q, Depth);
2782
2790
return isKnownNonZero (I->getOperand (0 ), Q, Depth);
2791
+ }
2783
2792
} break ;
2784
2793
case Instruction::IntToPtr:
2785
2794
// Note that we have to take special care to avoid looking through
@@ -2788,21 +2797,21 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
2788
2797
if (!isa<ScalableVectorType>(I->getType ()) &&
2789
2798
Q.DL .getTypeSizeInBits (I->getOperand (0 )->getType ()).getFixedValue () <=
2790
2799
Q.DL .getTypeSizeInBits (I->getType ()).getFixedValue ())
2791
- return isKnownNonZero (I->getOperand (0 ), Q, Depth);
2800
+ return isKnownNonZero (I->getOperand (0 ), DemandedElts, Q, Depth);
2792
2801
break ;
2793
2802
case Instruction::PtrToInt:
2794
2803
// Similar to int2ptr above, we can look through ptr2int here if the cast
2795
2804
// is a no-op or an extend and not a truncate.
2796
2805
if (!isa<ScalableVectorType>(I->getType ()) &&
2797
2806
Q.DL .getTypeSizeInBits (I->getOperand (0 )->getType ()).getFixedValue () <=
2798
2807
Q.DL .getTypeSizeInBits (I->getType ()).getFixedValue ())
2799
- return isKnownNonZero (I->getOperand (0 ), Q, Depth);
2808
+ return isKnownNonZero (I->getOperand (0 ), DemandedElts, Q, Depth);
2800
2809
break ;
2801
2810
case Instruction::Trunc:
2802
2811
// nuw/nsw trunc preserves zero/non-zero status of input.
2803
2812
if (auto *TI = dyn_cast<TruncInst>(I))
2804
2813
if (TI->hasNoSignedWrap () || TI->hasNoUnsignedWrap ())
2805
- return isKnownNonZero (TI->getOperand (0 ), Q, Depth);
2814
+ return isKnownNonZero (TI->getOperand (0 ), DemandedElts, Q, Depth);
2806
2815
break ;
2807
2816
2808
2817
case Instruction::Sub:
@@ -2823,13 +2832,13 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
2823
2832
case Instruction::SExt:
2824
2833
case Instruction::ZExt:
2825
2834
// ext X != 0 if X != 0.
2826
- return isKnownNonZero (I->getOperand (0 ), Q, Depth);
2835
+ return isKnownNonZero (I->getOperand (0 ), DemandedElts, Q, Depth);
2827
2836
2828
2837
case Instruction::Shl: {
2829
2838
// shl nsw/nuw can't remove any non-zero bits.
2830
2839
const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);
2831
2840
if (Q.IIQ .hasNoUnsignedWrap (BO) || Q.IIQ .hasNoSignedWrap (BO))
2832
- return isKnownNonZero (I->getOperand (0 ), Q, Depth);
2841
+ return isKnownNonZero (I->getOperand (0 ), DemandedElts, Q, Depth);
2833
2842
2834
2843
// shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
2835
2844
// if the lowest bit is shifted off the end.
@@ -2845,7 +2854,7 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
2845
2854
// shr exact can only shift out zero bits.
2846
2855
const PossiblyExactOperator *BO = cast<PossiblyExactOperator>(I);
2847
2856
if (BO->isExact ())
2848
- return isKnownNonZero (I->getOperand (0 ), Q, Depth);
2857
+ return isKnownNonZero (I->getOperand (0 ), DemandedElts, Q, Depth);
2849
2858
2850
2859
// shr X, Y != 0 if X is negative. Note that the value of the shift is not
2851
2860
// defined if the sign bit is shifted off the end.
@@ -3100,6 +3109,8 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
3100
3109
/* NSW=*/ true , /* NUW=*/ false );
3101
3110
// Vec reverse preserves zero/non-zero status from input vec.
3102
3111
case Intrinsic::vector_reverse:
3112
+ return isKnownNonZero (II->getArgOperand (0 ), DemandedElts.reverseBits (),
3113
+ Q, Depth);
3103
3114
// umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3104
3115
case Intrinsic::vector_reduce_or:
3105
3116
case Intrinsic::vector_reduce_umax:
@@ -3424,7 +3435,8 @@ getInvertibleOperands(const Operator *Op1,
3424
3435
// / Only handle a small subset of binops where (binop V2, X) with non-zero X
3425
3436
// / implies V2 != V1.
3426
3437
static bool isModifyingBinopOfNonZero (const Value *V1, const Value *V2,
3427
- unsigned Depth, const SimplifyQuery &Q) {
3438
+ const APInt &DemandedElts, unsigned Depth,
3439
+ const SimplifyQuery &Q) {
3428
3440
const BinaryOperator *BO = dyn_cast<BinaryOperator>(V1);
3429
3441
if (!BO)
3430
3442
return false ;
@@ -3444,39 +3456,43 @@ static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3444
3456
Op = BO->getOperand (0 );
3445
3457
else
3446
3458
return false ;
3447
- return isKnownNonZero (Op, Q, Depth + 1 );
3459
+ return isKnownNonZero (Op, DemandedElts, Q, Depth + 1 );
3448
3460
}
3449
3461
return false ;
3450
3462
}
3451
3463
3452
3464
// / Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3453
3465
// / the multiplication is nuw or nsw.
3454
- static bool isNonEqualMul (const Value *V1, const Value *V2, unsigned Depth,
3466
+ static bool isNonEqualMul (const Value *V1, const Value *V2,
3467
+ const APInt &DemandedElts, unsigned Depth,
3455
3468
const SimplifyQuery &Q) {
3456
3469
if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3457
3470
const APInt *C;
3458
3471
return match (OBO, m_Mul (m_Specific (V1), m_APInt (C))) &&
3459
3472
(OBO->hasNoUnsignedWrap () || OBO->hasNoSignedWrap ()) &&
3460
- !C->isZero () && !C->isOne () && isKnownNonZero (V1, Q, Depth + 1 );
3473
+ !C->isZero () && !C->isOne () &&
3474
+ isKnownNonZero (V1, DemandedElts, Q, Depth + 1 );
3461
3475
}
3462
3476
return false ;
3463
3477
}
3464
3478
3465
3479
// / Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3466
3480
// / the shift is nuw or nsw.
3467
- static bool isNonEqualShl (const Value *V1, const Value *V2, unsigned Depth,
3481
+ static bool isNonEqualShl (const Value *V1, const Value *V2,
3482
+ const APInt &DemandedElts, unsigned Depth,
3468
3483
const SimplifyQuery &Q) {
3469
3484
if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3470
3485
const APInt *C;
3471
3486
return match (OBO, m_Shl (m_Specific (V1), m_APInt (C))) &&
3472
3487
(OBO->hasNoUnsignedWrap () || OBO->hasNoSignedWrap ()) &&
3473
- !C->isZero () && isKnownNonZero (V1, Q, Depth + 1 );
3488
+ !C->isZero () && isKnownNonZero (V1, DemandedElts, Q, Depth + 1 );
3474
3489
}
3475
3490
return false ;
3476
3491
}
3477
3492
3478
3493
static bool isNonEqualPHIs (const PHINode *PN1, const PHINode *PN2,
3479
- unsigned Depth, const SimplifyQuery &Q) {
3494
+ const APInt &DemandedElts, unsigned Depth,
3495
+ const SimplifyQuery &Q) {
3480
3496
// Check two PHIs are in same block.
3481
3497
if (PN1->getParent () != PN2->getParent ())
3482
3498
return false ;
@@ -3498,14 +3514,15 @@ static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3498
3514
3499
3515
SimplifyQuery RecQ = Q;
3500
3516
RecQ.CxtI = IncomBB->getTerminator ();
3501
- if (!isKnownNonEqual (IV1, IV2, Depth + 1 , RecQ))
3517
+ if (!isKnownNonEqual (IV1, IV2, DemandedElts, Depth + 1 , RecQ))
3502
3518
return false ;
3503
3519
UsedFullRecursion = true ;
3504
3520
}
3505
3521
return true ;
3506
3522
}
3507
3523
3508
- static bool isNonEqualSelect (const Value *V1, const Value *V2, unsigned Depth,
3524
+ static bool isNonEqualSelect (const Value *V1, const Value *V2,
3525
+ const APInt &DemandedElts, unsigned Depth,
3509
3526
const SimplifyQuery &Q) {
3510
3527
const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
3511
3528
if (!SI1)
@@ -3516,12 +3533,12 @@ static bool isNonEqualSelect(const Value *V1, const Value *V2, unsigned Depth,
3516
3533
const Value *Cond2 = SI2->getCondition ();
3517
3534
if (Cond1 == Cond2)
3518
3535
return isKnownNonEqual (SI1->getTrueValue (), SI2->getTrueValue (),
3519
- Depth + 1 , Q) &&
3536
+ DemandedElts, Depth + 1 , Q) &&
3520
3537
isKnownNonEqual (SI1->getFalseValue (), SI2->getFalseValue (),
3521
- Depth + 1 , Q);
3538
+ DemandedElts, Depth + 1 , Q);
3522
3539
}
3523
- return isKnownNonEqual (SI1->getTrueValue (), V2, Depth + 1 , Q) &&
3524
- isKnownNonEqual (SI1->getFalseValue (), V2, Depth + 1 , Q);
3540
+ return isKnownNonEqual (SI1->getTrueValue (), V2, DemandedElts, Depth + 1 , Q) &&
3541
+ isKnownNonEqual (SI1->getFalseValue (), V2, DemandedElts, Depth + 1 , Q);
3525
3542
}
3526
3543
3527
3544
// Check to see if A is both a GEP and is the incoming value for a PHI in the
@@ -3577,7 +3594,8 @@ static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B,
3577
3594
}
3578
3595
3579
3596
// / Return true if it is known that V1 != V2.
3580
- static bool isKnownNonEqual (const Value *V1, const Value *V2, unsigned Depth,
3597
+ static bool isKnownNonEqual (const Value *V1, const Value *V2,
3598
+ const APInt &DemandedElts, unsigned Depth,
3581
3599
const SimplifyQuery &Q) {
3582
3600
if (V1 == V2)
3583
3601
return false ;
@@ -3595,40 +3613,44 @@ static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth,
3595
3613
auto *O2 = dyn_cast<Operator>(V2);
3596
3614
if (O1 && O2 && O1->getOpcode () == O2->getOpcode ()) {
3597
3615
if (auto Values = getInvertibleOperands (O1, O2))
3598
- return isKnownNonEqual (Values->first , Values->second , Depth + 1 , Q);
3616
+ return isKnownNonEqual (Values->first , Values->second , DemandedElts,
3617
+ Depth + 1 , Q);
3599
3618
3600
3619
if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
3601
3620
const PHINode *PN2 = cast<PHINode>(V2);
3602
3621
// FIXME: This is missing a generalization to handle the case where one is
3603
3622
// a PHI and another one isn't.
3604
- if (isNonEqualPHIs (PN1, PN2, Depth, Q))
3623
+ if (isNonEqualPHIs (PN1, PN2, DemandedElts, Depth, Q))
3605
3624
return true ;
3606
3625
};
3607
3626
}
3608
3627
3609
- if (isModifyingBinopOfNonZero (V1, V2, Depth, Q) ||
3610
- isModifyingBinopOfNonZero (V2, V1, Depth, Q))
3628
+ if (isModifyingBinopOfNonZero (V1, V2, DemandedElts, Depth, Q) ||
3629
+ isModifyingBinopOfNonZero (V2, V1, DemandedElts, Depth, Q))
3611
3630
return true ;
3612
3631
3613
- if (isNonEqualMul (V1, V2, Depth, Q) || isNonEqualMul (V2, V1, Depth, Q))
3632
+ if (isNonEqualMul (V1, V2, DemandedElts, Depth, Q) ||
3633
+ isNonEqualMul (V2, V1, DemandedElts, Depth, Q))
3614
3634
return true ;
3615
3635
3616
- if (isNonEqualShl (V1, V2, Depth, Q) || isNonEqualShl (V2, V1, Depth, Q))
3636
+ if (isNonEqualShl (V1, V2, DemandedElts, Depth, Q) ||
3637
+ isNonEqualShl (V2, V1, DemandedElts, Depth, Q))
3617
3638
return true ;
3618
3639
3619
3640
if (V1->getType ()->isIntOrIntVectorTy ()) {
3620
3641
// Are any known bits in V1 contradictory to known bits in V2? If V1
3621
3642
// has a known zero where V2 has a known one, they must not be equal.
3622
- KnownBits Known1 = computeKnownBits (V1, Depth, Q);
3643
+ KnownBits Known1 = computeKnownBits (V1, DemandedElts, Depth, Q);
3623
3644
if (!Known1.isUnknown ()) {
3624
- KnownBits Known2 = computeKnownBits (V2, Depth, Q);
3645
+ KnownBits Known2 = computeKnownBits (V2, DemandedElts, Depth, Q);
3625
3646
if (Known1.Zero .intersects (Known2.One ) ||
3626
3647
Known2.Zero .intersects (Known1.One ))
3627
3648
return true ;
3628
3649
}
3629
3650
}
3630
3651
3631
- if (isNonEqualSelect (V1, V2, Depth, Q) || isNonEqualSelect (V2, V1, Depth, Q))
3652
+ if (isNonEqualSelect (V1, V2, DemandedElts, Depth, Q) ||
3653
+ isNonEqualSelect (V2, V1, DemandedElts, Depth, Q))
3632
3654
return true ;
3633
3655
3634
3656
if (isNonEqualPointersWithRecursiveGEP (V1, V2, Q) ||
@@ -3640,7 +3662,7 @@ static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth,
3640
3662
// Check PtrToInt type matches the pointer size.
3641
3663
if (match (V1, m_PtrToIntSameSize (Q.DL , m_Value (A))) &&
3642
3664
match (V2, m_PtrToIntSameSize (Q.DL , m_Value (B))))
3643
- return isKnownNonEqual (A, B, Depth + 1 , Q);
3665
+ return isKnownNonEqual (A, B, DemandedElts, Depth + 1 , Q);
3644
3666
3645
3667
return false ;
3646
3668
}
0 commit comments