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

via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 13 11:44:40 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))
----------------
goldsteinn wrote:

Personally would either 1) add comment that you are explicitly summing into offset or move the `GEP->accumulateConstantOffset(DL, Offset)` to inside the loop. Otherwise very easy to miss the point of this loop due to the by-reference output variable.

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


More information about the llvm-commits mailing list