[llvm] [InstCombine] Make indexed compare fold GEP source type independent (PR #71663)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 14 06:10:01 PST 2023


================
@@ -637,28 +623,18 @@ static Value *rewriteGEPAsOffset(Type *ElemTy, Value *Start, Value *Base,
 
 /// Looks through GEPs in order to express the input Value as a constant
 /// indexed GEP. Returns a pair containing the GEPs Pointer and Index.
-static std::pair<Value *, Value *>
-getAsConstantIndexedAddress(Type *ElemTy, Value *V, const DataLayout &DL) {
-  Type *IndexType = IntegerType::get(V->getContext(),
-                                     DL.getIndexTypeSizeInBits(V->getType()));
-
-  Constant *Index = ConstantInt::getNullValue(IndexType);
+static std::pair<Value *, APInt>
+getAsConstantIndexedAddress(Value *V, const DataLayout &DL) {
+  APInt Offset = APInt(DL.getIndexTypeSizeInBits(V->getType()), 0);
   while (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
     // We accept only inbouds GEPs here to exclude the possibility of
     // overflow.
-    if (!GEP->isInBounds())
+    if (!GEP->isInBounds() || !GEP->accumulateConstantOffset(DL, Offset))
----------------
nikic wrote:

I've switch to using stripAndAccumulateConstantOffsets() + an address space check instead. Makes the code a bit cleaner, I think.

https://github.com/llvm/llvm-project/pull/71663


More information about the llvm-commits mailing list