@@ -1489,7 +1489,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
14891489 }
14901490 case Instruction::PHI: {
14911491 const PHINode *P = cast<PHINode>(I);
1492- BinaryOperator *BO = nullptr ;
1492+ Instruction *BO = nullptr ;
14931493 Value *R = nullptr , *L = nullptr ;
14941494 if (matchSimpleRecurrence (P, BO, R, L)) {
14951495 // Handle the case of a simple two-predecessor recurrence PHI.
@@ -1553,6 +1553,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
15531553 case Instruction::Sub:
15541554 case Instruction::And:
15551555 case Instruction::Or:
1556+ case Instruction::GetElementPtr:
15561557 case Instruction::Mul: {
15571558 // Change the context instruction to the "edge" that flows into the
15581559 // phi. This is important because that is where the value is actually
@@ -1571,12 +1572,21 @@ static void computeKnownBitsFromOperator(const Operator *I,
15711572
15721573 // We need to take the minimum number of known bits
15731574 KnownBits Known3 (BitWidth);
1575+ if (BitWidth != getBitWidth (L->getType (), Q.DL )) {
1576+ assert (isa < GetElementPtrInst>(BO) &&
1577+ " Bitwidth should only be different for GEPs." );
1578+ break ;
1579+ }
15741580 RecQ.CxtI = LInst;
15751581 computeKnownBits (L, DemandedElts, Known3, Depth + 1 , RecQ);
15761582
15771583 Known.Zero .setLowBits (std::min (Known2.countMinTrailingZeros (),
15781584 Known3.countMinTrailingZeros ()));
15791585
1586+ // Don't apply logic below for GEPs.
1587+ if (isa<GetElementPtrInst>(BO))
1588+ break ;
1589+
15801590 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
15811591 if (!OverflowOp || !Q.IIQ .hasNoSignedWrap (OverflowOp))
15821592 break ;
@@ -1737,6 +1747,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
17371747 Known.resetAll ();
17381748 }
17391749 }
1750+
17401751 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
17411752 switch (II->getIntrinsicID ()) {
17421753 default :
@@ -2270,7 +2281,7 @@ void computeKnownBits(const Value *V, const APInt &DemandedElts,
22702281// / always a power of two (or zero).
22712282static bool isPowerOfTwoRecurrence (const PHINode *PN, bool OrZero,
22722283 unsigned Depth, SimplifyQuery &Q) {
2273- BinaryOperator *BO = nullptr ;
2284+ Instruction *BO = nullptr ;
22742285 Value *Start = nullptr , *Step = nullptr ;
22752286 if (!matchSimpleRecurrence (PN, BO, Start, Step))
22762287 return false ;
@@ -2308,7 +2319,7 @@ static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
23082319 // Divisor must be a power of two.
23092320 // If OrZero is false, cannot guarantee induction variable is non-zero after
23102321 // division, same for Shr, unless it is exact division.
2311- return (OrZero || Q.IIQ .isExact (BO )) &&
2322+ return (OrZero || Q.IIQ .isExact (cast<BinaryOperator>(BO) )) &&
23122323 isKnownToBeAPowerOfTwo (Step, false , Depth, Q);
23132324 case Instruction::Shl:
23142325 return OrZero || Q.IIQ .hasNoUnsignedWrap (BO) || Q.IIQ .hasNoSignedWrap (BO);
@@ -2317,7 +2328,7 @@ static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
23172328 return false ;
23182329 [[fallthrough]];
23192330 case Instruction::LShr:
2320- return OrZero || Q.IIQ .isExact (BO );
2331+ return OrZero || Q.IIQ .isExact (cast<BinaryOperator>(BO) );
23212332 default :
23222333 return false ;
23232334 }
@@ -2727,7 +2738,7 @@ static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value)
27272738// / Try to detect a recurrence that monotonically increases/decreases from a
27282739// / non-zero starting value. These are common as induction variables.
27292740static bool isNonZeroRecurrence (const PHINode *PN) {
2730- BinaryOperator *BO = nullptr ;
2741+ Instruction *BO = nullptr ;
27312742 Value *Start = nullptr , *Step = nullptr ;
27322743 const APInt *StartC, *StepC;
27332744 if (!matchSimpleRecurrence (PN, BO, Start, Step) ||
@@ -3560,9 +3571,9 @@ getInvertibleOperands(const Operator *Op1,
35603571 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
35613572 // are a single invertible function of the start values? Note that repeated
35623573 // application of an invertible function is also invertible
3563- BinaryOperator *BO1 = nullptr ;
3574+ Instruction *BO1 = nullptr ;
35643575 Value *Start1 = nullptr , *Step1 = nullptr ;
3565- BinaryOperator *BO2 = nullptr ;
3576+ Instruction *BO2 = nullptr ;
35663577 Value *Start2 = nullptr , *Step2 = nullptr ;
35673578 if (PN1->getParent () != PN2->getParent () ||
35683579 !matchSimpleRecurrence (PN1, BO1, Start1, Step1) ||
@@ -9199,6 +9210,17 @@ llvm::canConvertToMinOrMaxIntrinsic(ArrayRef<Value *> VL) {
91999210
92009211bool llvm::matchSimpleRecurrence (const PHINode *P, BinaryOperator *&BO,
92019212 Value *&Start, Value *&Step) {
9213+ Instruction *I;
9214+ if (matchSimpleRecurrence (P, I, Start, Step)) {
9215+ BO = dyn_cast<BinaryOperator>(I);
9216+ if (BO)
9217+ return true ;
9218+ }
9219+ return false ;
9220+ }
9221+
9222+ bool llvm::matchSimpleRecurrence (const PHINode *P, Instruction *&BO,
9223+ Value *&Start, Value *&Step) {
92029224 // Handle the case of a simple two-predecessor recurrence PHI.
92039225 // There's a lot more that could theoretically be done here, but
92049226 // this is sufficient to catch some interesting cases.
@@ -9208,7 +9230,7 @@ bool llvm::matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO,
92089230 for (unsigned i = 0 ; i != 2 ; ++i) {
92099231 Value *L = P->getIncomingValue (i);
92109232 Value *R = P->getIncomingValue (!i);
9211- auto *LU = dyn_cast<BinaryOperator >(L);
9233+ auto *LU = dyn_cast<Instruction >(L);
92129234 if (!LU)
92139235 continue ;
92149236 unsigned Opcode = LU->getOpcode ();
@@ -9240,6 +9262,21 @@ bool llvm::matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO,
92409262
92419263 break ; // Match!
92429264 }
9265+ case Instruction::GetElementPtr: {
9266+ if (LU->getNumOperands () != 2 ||
9267+ !cast<GetElementPtrInst>(L)->getSourceElementType ()->isIntegerTy (8 ))
9268+ continue ;
9269+
9270+ Value *LL = LU->getOperand (0 );
9271+ Value *LR = LU->getOperand (1 );
9272+ // Find a recurrence.
9273+ if (LL == P) {
9274+ // Found a match
9275+ L = LR;
9276+ break ;
9277+ }
9278+ continue ;
9279+ }
92439280 };
92449281
92459282 // We have matched a recurrence of the form:
@@ -9256,9 +9293,9 @@ bool llvm::matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO,
92569293 return false ;
92579294}
92589295
9259- bool llvm::matchSimpleRecurrence (const BinaryOperator *I, PHINode *&P,
9296+ bool llvm::matchSimpleRecurrence (const Instruction *I, PHINode *&P,
92609297 Value *&Start, Value *&Step) {
9261- BinaryOperator *BO = nullptr ;
9298+ Instruction *BO = nullptr ;
92629299 P = dyn_cast<PHINode>(I->getOperand (0 ));
92639300 if (!P)
92649301 P = dyn_cast<PHINode>(I->getOperand (1 ));
0 commit comments