@@ -1489,7 +1489,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
1489
1489
}
1490
1490
case Instruction::PHI: {
1491
1491
const PHINode *P = cast<PHINode>(I);
1492
- BinaryOperator *BO = nullptr ;
1492
+ Instruction *BO = nullptr ;
1493
1493
Value *R = nullptr , *L = nullptr ;
1494
1494
if (matchSimpleRecurrence (P, BO, R, L)) {
1495
1495
// Handle the case of a simple two-predecessor recurrence PHI.
@@ -1553,6 +1553,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
1553
1553
case Instruction::Sub:
1554
1554
case Instruction::And:
1555
1555
case Instruction::Or:
1556
+ case Instruction::GetElementPtr:
1556
1557
case Instruction::Mul: {
1557
1558
// Change the context instruction to the "edge" that flows into the
1558
1559
// phi. This is important because that is where the value is actually
@@ -1571,12 +1572,21 @@ static void computeKnownBitsFromOperator(const Operator *I,
1571
1572
1572
1573
// We need to take the minimum number of known bits
1573
1574
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
+ }
1574
1580
RecQ.CxtI = LInst;
1575
1581
computeKnownBits (L, DemandedElts, Known3, Depth + 1 , RecQ);
1576
1582
1577
1583
Known.Zero .setLowBits (std::min (Known2.countMinTrailingZeros (),
1578
1584
Known3.countMinTrailingZeros ()));
1579
1585
1586
+ // Don't apply logic below for GEPs.
1587
+ if (isa<GetElementPtrInst>(BO))
1588
+ break ;
1589
+
1580
1590
auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1581
1591
if (!OverflowOp || !Q.IIQ .hasNoSignedWrap (OverflowOp))
1582
1592
break ;
@@ -1737,6 +1747,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
1737
1747
Known.resetAll ();
1738
1748
}
1739
1749
}
1750
+
1740
1751
if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1741
1752
switch (II->getIntrinsicID ()) {
1742
1753
default :
@@ -2270,7 +2281,7 @@ void computeKnownBits(const Value *V, const APInt &DemandedElts,
2270
2281
// / always a power of two (or zero).
2271
2282
static bool isPowerOfTwoRecurrence (const PHINode *PN, bool OrZero,
2272
2283
unsigned Depth, SimplifyQuery &Q) {
2273
- BinaryOperator *BO = nullptr ;
2284
+ Instruction *BO = nullptr ;
2274
2285
Value *Start = nullptr , *Step = nullptr ;
2275
2286
if (!matchSimpleRecurrence (PN, BO, Start, Step))
2276
2287
return false ;
@@ -2308,7 +2319,7 @@ static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2308
2319
// Divisor must be a power of two.
2309
2320
// If OrZero is false, cannot guarantee induction variable is non-zero after
2310
2321
// 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) )) &&
2312
2323
isKnownToBeAPowerOfTwo (Step, false , Depth, Q);
2313
2324
case Instruction::Shl:
2314
2325
return OrZero || Q.IIQ .hasNoUnsignedWrap (BO) || Q.IIQ .hasNoSignedWrap (BO);
@@ -2317,7 +2328,7 @@ static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2317
2328
return false ;
2318
2329
[[fallthrough]];
2319
2330
case Instruction::LShr:
2320
- return OrZero || Q.IIQ .isExact (BO );
2331
+ return OrZero || Q.IIQ .isExact (cast<BinaryOperator>(BO) );
2321
2332
default :
2322
2333
return false ;
2323
2334
}
@@ -2727,7 +2738,7 @@ static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value)
2727
2738
// / Try to detect a recurrence that monotonically increases/decreases from a
2728
2739
// / non-zero starting value. These are common as induction variables.
2729
2740
static bool isNonZeroRecurrence (const PHINode *PN) {
2730
- BinaryOperator *BO = nullptr ;
2741
+ Instruction *BO = nullptr ;
2731
2742
Value *Start = nullptr , *Step = nullptr ;
2732
2743
const APInt *StartC, *StepC;
2733
2744
if (!matchSimpleRecurrence (PN, BO, Start, Step) ||
@@ -3560,9 +3571,9 @@ getInvertibleOperands(const Operator *Op1,
3560
3571
// If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3561
3572
// are a single invertible function of the start values? Note that repeated
3562
3573
// application of an invertible function is also invertible
3563
- BinaryOperator *BO1 = nullptr ;
3574
+ Instruction *BO1 = nullptr ;
3564
3575
Value *Start1 = nullptr , *Step1 = nullptr ;
3565
- BinaryOperator *BO2 = nullptr ;
3576
+ Instruction *BO2 = nullptr ;
3566
3577
Value *Start2 = nullptr , *Step2 = nullptr ;
3567
3578
if (PN1->getParent () != PN2->getParent () ||
3568
3579
!matchSimpleRecurrence (PN1, BO1, Start1, Step1) ||
@@ -9199,6 +9210,17 @@ llvm::canConvertToMinOrMaxIntrinsic(ArrayRef<Value *> VL) {
9199
9210
9200
9211
bool llvm::matchSimpleRecurrence (const PHINode *P, BinaryOperator *&BO,
9201
9212
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) {
9202
9224
// Handle the case of a simple two-predecessor recurrence PHI.
9203
9225
// There's a lot more that could theoretically be done here, but
9204
9226
// this is sufficient to catch some interesting cases.
@@ -9208,7 +9230,7 @@ bool llvm::matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO,
9208
9230
for (unsigned i = 0 ; i != 2 ; ++i) {
9209
9231
Value *L = P->getIncomingValue (i);
9210
9232
Value *R = P->getIncomingValue (!i);
9211
- auto *LU = dyn_cast<BinaryOperator >(L);
9233
+ auto *LU = dyn_cast<Instruction >(L);
9212
9234
if (!LU)
9213
9235
continue ;
9214
9236
unsigned Opcode = LU->getOpcode ();
@@ -9240,6 +9262,21 @@ bool llvm::matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO,
9240
9262
9241
9263
break ; // Match!
9242
9264
}
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
+ }
9243
9280
};
9244
9281
9245
9282
// We have matched a recurrence of the form:
@@ -9256,9 +9293,9 @@ bool llvm::matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO,
9256
9293
return false ;
9257
9294
}
9258
9295
9259
- bool llvm::matchSimpleRecurrence (const BinaryOperator *I, PHINode *&P,
9296
+ bool llvm::matchSimpleRecurrence (const Instruction *I, PHINode *&P,
9260
9297
Value *&Start, Value *&Step) {
9261
- BinaryOperator *BO = nullptr ;
9298
+ Instruction *BO = nullptr ;
9262
9299
P = dyn_cast<PHINode>(I->getOperand (0 ));
9263
9300
if (!P)
9264
9301
P = dyn_cast<PHINode>(I->getOperand (1 ));
0 commit comments