Skip to content

Commit a2b4bb7

Browse files
committed
Use stripAndAccumulateConstantOffsets()
1 parent 3748dfe commit a2b4bb7

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -621,22 +621,6 @@ static Value *rewriteGEPAsOffset(Value *Start, Value *Base,
621621
return NewInsts[Start];
622622
}
623623

624-
/// Looks through GEPs in order to express the input Value as a constant
625-
/// indexed GEP. Returns a pair containing the GEPs Pointer and Index.
626-
static std::pair<Value *, APInt>
627-
getAsConstantIndexedAddress(Value *V, const DataLayout &DL) {
628-
APInt Offset = APInt(DL.getIndexTypeSizeInBits(V->getType()), 0);
629-
while (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
630-
// We accept only inbouds GEPs here to exclude the possibility of
631-
// overflow.
632-
if (!GEP->isInBounds() || !GEP->accumulateConstantOffset(DL, Offset))
633-
break;
634-
635-
V = GEP->getPointerOperand();
636-
}
637-
return {V, Offset};
638-
}
639-
640624
/// Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.
641625
/// We can look through PHIs, GEPs and casts in order to determine a common base
642626
/// between GEPLHS and RHS.
@@ -651,9 +635,14 @@ static Instruction *transformToIndexedCompare(GEPOperator *GEPLHS, Value *RHS,
651635
if (!GEPLHS->hasAllConstantIndices())
652636
return nullptr;
653637

654-
Value *PtrBase;
655-
APInt Offset;
656-
std::tie(PtrBase, Offset) = getAsConstantIndexedAddress(GEPLHS, DL);
638+
APInt Offset(DL.getIndexTypeSizeInBits(GEPLHS->getType()), 0);
639+
Value *PtrBase =
640+
GEPLHS->stripAndAccumulateConstantOffsets(DL, Offset,
641+
/*AllowNonInbounds*/ false);
642+
643+
// Bail if we looked through addrspacecast.
644+
if (PtrBase->getType() != GEPLHS->getType())
645+
return nullptr;
657646

658647
// The set of nodes that will take part in this transformation.
659648
SetVector<Value *> Nodes;

0 commit comments

Comments
 (0)