@@ -2568,98 +2568,104 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
25682568 if (isKnownNonNullFromDominatingCondition (V, Q.CxtI , Q.DT ))
25692569 return true ;
25702570
2571- // Check for recursive pointer simplifications.
2572- if (V->getType ()->isPointerTy ()) {
2573- // Look through bitcast operations, GEPs, and int2ptr instructions as they
2574- // do not alter the value, or at least not the nullness property of the
2575- // value, e.g., int2ptr is allowed to zero/sign extend the value.
2576- //
2571+ const Operator *I = dyn_cast<Operator>(V);
2572+ if (!I)
2573+ return false ;
2574+
2575+ unsigned BitWidth = getBitWidth (V->getType ()->getScalarType (), Q.DL );
2576+ switch (I->getOpcode ()) {
2577+ case Instruction::GetElementPtr:
2578+ if (I->getType ()->isPointerTy ())
2579+ return isGEPKnownNonNull (cast<GEPOperator>(I), Depth, Q);
2580+ break ;
2581+ case Instruction::BitCast:
2582+ if (I->getType ()->isPointerTy ())
2583+ return isKnownNonZero (I->getOperand (0 ), Depth, Q);
2584+ break ;
2585+ case Instruction::IntToPtr:
25772586 // Note that we have to take special care to avoid looking through
25782587 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
25792588 // as casts that can alter the value, e.g., AddrSpaceCasts.
2580- if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V))
2581- return isGEPKnownNonNull (GEP, Depth, Q);
2582-
2583- if (auto *BCO = dyn_cast<BitCastOperator>(V))
2584- return isKnownNonZero (BCO->getOperand (0 ), Depth, Q);
2585-
2586- if (auto *I2P = dyn_cast<IntToPtrInst>(V))
2587- if (Q.DL .getTypeSizeInBits (I2P->getSrcTy ()).getFixedSize () <=
2588- Q.DL .getTypeSizeInBits (I2P->getDestTy ()).getFixedSize ())
2589- return isKnownNonZero (I2P->getOperand (0 ), Depth, Q);
2590- }
2591-
2592- // Similar to int2ptr above, we can look through ptr2int here if the cast
2593- // is a no-op or an extend and not a truncate.
2594- if (auto *P2I = dyn_cast<PtrToIntInst>(V))
2595- if (Q.DL .getTypeSizeInBits (P2I->getSrcTy ()).getFixedSize () <=
2596- Q.DL .getTypeSizeInBits (P2I->getDestTy ()).getFixedSize ())
2597- return isKnownNonZero (P2I->getOperand (0 ), Depth, Q);
2598-
2599- unsigned BitWidth = getBitWidth (V->getType ()->getScalarType (), Q.DL );
2600-
2601- // X | Y != 0 if X != 0 or Y != 0.
2602- Value *X = nullptr , *Y = nullptr ;
2603- if (match (V, m_Or (m_Value (X), m_Value (Y))))
2604- return isKnownNonZero (X, DemandedElts, Depth, Q) ||
2605- isKnownNonZero (Y, DemandedElts, Depth, Q);
2606-
2607- // ext X != 0 if X != 0.
2608- if (isa<SExtInst>(V) || isa<ZExtInst>(V))
2609- return isKnownNonZero (cast<Instruction>(V)->getOperand (0 ), Depth, Q);
2589+ if (Q.DL .getTypeSizeInBits (I->getOperand (0 )->getType ()).getFixedSize () <=
2590+ Q.DL .getTypeSizeInBits (I->getType ()).getFixedSize ())
2591+ return isKnownNonZero (I->getOperand (0 ), Depth, Q);
2592+ break ;
2593+ case Instruction::PtrToInt:
2594+ // Similar to int2ptr above, we can look through ptr2int here if the cast
2595+ // is a no-op or an extend and not a truncate.
2596+ if (Q.DL .getTypeSizeInBits (I->getOperand (0 )->getType ()).getFixedSize () <=
2597+ Q.DL .getTypeSizeInBits (I->getType ()).getFixedSize ())
2598+ return isKnownNonZero (I->getOperand (0 ), Depth, Q);
2599+ break ;
2600+ case Instruction::Or:
2601+ // X | Y != 0 if X != 0 or Y != 0.
2602+ return isKnownNonZero (I->getOperand (0 ), DemandedElts, Depth, Q) ||
2603+ isKnownNonZero (I->getOperand (1 ), DemandedElts, Depth, Q);
2604+ case Instruction::SExt:
2605+ case Instruction::ZExt:
2606+ // ext X != 0 if X != 0.
2607+ return isKnownNonZero (I->getOperand (0 ), Depth, Q);
26102608
2611- // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
2612- // if the lowest bit is shifted off the end.
2613- if (match (V, m_Shl (m_Value (X), m_Value (Y)))) {
2609+ case Instruction::Shl: {
26142610 // shl nuw can't remove any non-zero bits.
26152611 const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
26162612 if (Q.IIQ .hasNoUnsignedWrap (BO))
2617- return isKnownNonZero (X , Depth, Q);
2613+ return isKnownNonZero (I-> getOperand ( 0 ) , Depth, Q);
26182614
2615+ // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
2616+ // if the lowest bit is shifted off the end.
26192617 KnownBits Known (BitWidth);
2620- computeKnownBits (X , DemandedElts, Known, Depth, Q);
2618+ computeKnownBits (I-> getOperand ( 0 ) , DemandedElts, Known, Depth, Q);
26212619 if (Known.One [0 ])
26222620 return true ;
2621+ break ;
26232622 }
2624- // shr X, Y != 0 if X is negative. Note that the value of the shift is not
2625- // defined if the sign bit is shifted off the end.
2626- else if (match (V, m_Shr (m_Value (X), m_Value (Y)))) {
2623+ case Instruction::LShr:
2624+ case Instruction::AShr: {
26272625 // shr exact can only shift out zero bits.
26282626 const PossiblyExactOperator *BO = cast<PossiblyExactOperator>(V);
26292627 if (BO->isExact ())
2630- return isKnownNonZero (X , Depth, Q);
2628+ return isKnownNonZero (I-> getOperand ( 0 ) , Depth, Q);
26312629
2632- KnownBits Known = computeKnownBits (X, DemandedElts, Depth, Q);
2630+ // shr X, Y != 0 if X is negative. Note that the value of the shift is not
2631+ // defined if the sign bit is shifted off the end.
2632+ KnownBits Known =
2633+ computeKnownBits (I->getOperand (0 ), DemandedElts, Depth, Q);
26332634 if (Known.isNegative ())
26342635 return true ;
26352636
26362637 // If the shifter operand is a constant, and all of the bits shifted
26372638 // out are known to be zero, and X is known non-zero then at least one
26382639 // non-zero bit must remain.
2639- if (ConstantInt *Shift = dyn_cast<ConstantInt>(Y )) {
2640+ if (ConstantInt *Shift = dyn_cast<ConstantInt>(I-> getOperand ( 1 ) )) {
26402641 auto ShiftVal = Shift->getLimitedValue (BitWidth - 1 );
26412642 // Is there a known one in the portion not shifted out?
26422643 if (Known.countMaxLeadingZeros () < BitWidth - ShiftVal)
26432644 return true ;
26442645 // Are all the bits to be shifted out known zero?
26452646 if (Known.countMinTrailingZeros () >= ShiftVal)
2646- return isKnownNonZero (X , DemandedElts, Depth, Q);
2647+ return isKnownNonZero (I-> getOperand ( 0 ) , DemandedElts, Depth, Q);
26472648 }
2649+ break ;
26482650 }
2649- // div exact can only produce a zero if the dividend is zero.
2650- else if (match (V, m_Exact (m_IDiv (m_Value (X), m_Value ())))) {
2651- return isKnownNonZero (X, DemandedElts, Depth, Q);
2652- }
2653- // X + Y.
2654- else if (match (V, m_Add (m_Value (X), m_Value (Y)))) {
2655- KnownBits XKnown = computeKnownBits (X, DemandedElts, Depth, Q);
2656- KnownBits YKnown = computeKnownBits (Y, DemandedElts, Depth, Q);
2651+ case Instruction::UDiv:
2652+ case Instruction::SDiv:
2653+ // div exact can only produce a zero if the dividend is zero.
2654+ if (cast<PossiblyExactOperator>(I)->isExact ())
2655+ return isKnownNonZero (I->getOperand (0 ), DemandedElts, Depth, Q);
2656+ break ;
2657+ case Instruction::Add: {
2658+ // X + Y.
2659+ KnownBits XKnown =
2660+ computeKnownBits (I->getOperand (0 ), DemandedElts, Depth, Q);
2661+ KnownBits YKnown =
2662+ computeKnownBits (I->getOperand (1 ), DemandedElts, Depth, Q);
26572663
26582664 // If X and Y are both non-negative (as signed values) then their sum is not
26592665 // zero unless both X and Y are zero.
26602666 if (XKnown.isNonNegative () && YKnown.isNonNegative ())
2661- if (isKnownNonZero (X , DemandedElts, Depth, Q) ||
2662- isKnownNonZero (Y , DemandedElts, Depth, Q))
2667+ if (isKnownNonZero (I-> getOperand ( 0 ) , DemandedElts, Depth, Q) ||
2668+ isKnownNonZero (I-> getOperand ( 1 ) , DemandedElts, Depth, Q))
26632669 return true ;
26642670
26652671 // If X and Y are both negative (as signed values) then their sum is not
@@ -2678,30 +2684,31 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
26782684
26792685 // The sum of a non-negative number and a power of two is not zero.
26802686 if (XKnown.isNonNegative () &&
2681- isKnownToBeAPowerOfTwo (Y , /* OrZero*/ false , Depth, Q))
2687+ isKnownToBeAPowerOfTwo (I-> getOperand ( 1 ) , /* OrZero*/ false , Depth, Q))
26822688 return true ;
26832689 if (YKnown.isNonNegative () &&
2684- isKnownToBeAPowerOfTwo (X , /* OrZero*/ false , Depth, Q))
2690+ isKnownToBeAPowerOfTwo (I-> getOperand ( 0 ) , /* OrZero*/ false , Depth, Q))
26852691 return true ;
2692+ break ;
26862693 }
2687- // X * Y.
2688- else if (match (V, m_Mul (m_Value (X), m_Value (Y)))) {
2689- const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
2694+ case Instruction::Mul: {
26902695 // If X and Y are non-zero then so is X * Y as long as the multiplication
26912696 // does not overflow.
2697+ const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
26922698 if ((Q.IIQ .hasNoSignedWrap (BO) || Q.IIQ .hasNoUnsignedWrap (BO)) &&
2693- isKnownNonZero (X , DemandedElts, Depth, Q) &&
2694- isKnownNonZero (Y , DemandedElts, Depth, Q))
2699+ isKnownNonZero (I-> getOperand ( 0 ) , DemandedElts, Depth, Q) &&
2700+ isKnownNonZero (I-> getOperand ( 1 ) , DemandedElts, Depth, Q))
26952701 return true ;
2702+ break ;
26962703 }
2697- // (C ? X : Y) != 0 if X != 0 and Y != 0.
2698- else if ( const SelectInst *SI = dyn_cast<SelectInst>(V)) {
2699- if (isKnownNonZero (SI-> getTrueValue ( ), DemandedElts, Depth, Q) &&
2700- isKnownNonZero (SI-> getFalseValue ( ), DemandedElts, Depth, Q))
2704+ case Instruction::Select:
2705+ // (C ? X : Y) != 0 if X != 0 and Y != 0.
2706+ if (isKnownNonZero (I-> getOperand ( 1 ), DemandedElts, Depth, Q) &&
2707+ isKnownNonZero (I-> getOperand ( 2 ), DemandedElts, Depth, Q))
27012708 return true ;
2702- }
2703- // PHI
2704- else if ( const PHINode *PN = dyn_cast <PHINode>(V)) {
2709+ break ;
2710+ case Instruction:: PHI: {
2711+ auto *PN = cast <PHINode>(I);
27052712 if (Q.IIQ .UseInstrInfo && isNonZeroRecurrence (PN))
27062713 return true ;
27072714
@@ -2715,28 +2722,29 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
27152722 return isKnownNonZero (U.get (), DemandedElts, NewDepth, RecQ);
27162723 });
27172724 }
2718- // ExtractElement
2719- else if (const auto *EEI = dyn_cast<ExtractElementInst>(V)) {
2720- const Value *Vec = EEI->getVectorOperand ();
2721- const Value *Idx = EEI->getIndexOperand ();
2722- auto *CIdx = dyn_cast<ConstantInt>(Idx);
2723- if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType ())) {
2724- unsigned NumElts = VecTy->getNumElements ();
2725- APInt DemandedVecElts = APInt::getAllOnes (NumElts);
2726- if (CIdx && CIdx->getValue ().ult (NumElts))
2727- DemandedVecElts = APInt::getOneBitSet (NumElts, CIdx->getZExtValue ());
2728- return isKnownNonZero (Vec, DemandedVecElts, Depth, Q);
2725+ case Instruction::ExtractElement:
2726+ if (const auto *EEI = dyn_cast<ExtractElementInst>(V)) {
2727+ const Value *Vec = EEI->getVectorOperand ();
2728+ const Value *Idx = EEI->getIndexOperand ();
2729+ auto *CIdx = dyn_cast<ConstantInt>(Idx);
2730+ if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType ())) {
2731+ unsigned NumElts = VecTy->getNumElements ();
2732+ APInt DemandedVecElts = APInt::getAllOnes (NumElts);
2733+ if (CIdx && CIdx->getValue ().ult (NumElts))
2734+ DemandedVecElts = APInt::getOneBitSet (NumElts, CIdx->getZExtValue ());
2735+ return isKnownNonZero (Vec, DemandedVecElts, Depth, Q);
2736+ }
27292737 }
2730- }
2731- // Freeze
2732- else if (const FreezeInst *FI = dyn_cast<FreezeInst>(V)) {
2733- auto *Op = FI->getOperand (0 );
2734- if (isKnownNonZero (Op, Depth, Q) &&
2735- isGuaranteedNotToBePoison (Op, Q.AC , Q.CxtI , Q.DT , Depth))
2738+ break ;
2739+ case Instruction::Freeze:
2740+ if (isKnownNonZero (I->getOperand (0 ), Depth, Q) &&
2741+ isGuaranteedNotToBePoison (I->getOperand (0 ), Q.AC , Q.CxtI , Q.DT , Depth))
27362742 return true ;
2737- } else if (const auto *II = dyn_cast<IntrinsicInst>(V)) {
2738- if (II->getIntrinsicID () == Intrinsic::vscale)
2743+ break ;
2744+ case Instruction::Call:
2745+ if (cast<CallInst>(I)->getIntrinsicID () == Intrinsic::vscale)
27392746 return true ;
2747+ break ;
27402748 }
27412749
27422750 KnownBits Known (BitWidth);
0 commit comments