- if (GepA && GepB && GepA->getPointerOperand() == GepB->getPointerOperand()) {
- unsigned BW = DL->getPointerSizeInBits(ASA);
- APInt OffsetA(BW, 0) ,OffsetB(BW, 0);
-
- if (GepA->accumulateConstantOffset(*DL, OffsetA) &&
- GepB->accumulateConstantOffset(*DL, OffsetB)) {
- Type *Ty = cast<PointerType>(PtrA->getType())->getElementType();
- int64_t Sz = DL->getTypeStoreSize(Ty);
- return ((OffsetB.getSExtValue() - OffsetA.getSExtValue()) == Sz);
+
+ unsigned BW = DL->getPointerSizeInBits(ASA);
+ Type *Ty = cast<PointerType>(PtrA->getType())->getElementType();
+ int64_t Sz = DL->getTypeStoreSize(Ty);
+
+ // Check if PtrA is the base and PtrB is a constant offset.
+ if (GepB && GepB->getPointerOperand() == PtrA) {
+ APInt Offset(BW, 0);
+ if (GepB->accumulateConstantOffset(*DL, Offset))
+ return Offset.getSExtValue() == Sz;
+ return false;
+ }
+
+ // Check if PtrB is the base and PtrA is a constant offset.
+ if (GepA && GepA->getPointerOperand() == PtrB) {
+ APInt Offset(BW, 0);
+ if (GepA->accumulateConstantOffset(*DL, Offset))
+ return Offset.getSExtValue() == -Sz;
+ return false;
+ }
+
+ // If both pointers are GEPs:
+ if (GepA && GepB) {
+ // Check that they have the same base pointer and number of indices.
+ if (GepA->getPointerOperand() != GepB->getPointerOperand() ||
+ GepA->getNumIndices() != GepB->getNumIndices())
+ return false;
+
+ // Try to strip the geps. This makes SCEV faster.
+ // Make sure that all of the indices except for the last are identical.
+ int LastIdx = GepA->getNumIndices();
+ for (int i = 0; i < LastIdx - 1; i++) {
+ if (GepA->getOperand(i+1) != GepB->getOperand(i+1))
+ return false;