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

via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 21 09:35:57 PST 2023


================
@@ -559,29 +561,22 @@ static Value *rewriteGEPAsOffset(Type *ElemTy, Value *Start, Value *Base,
 
   // Create all the other instructions.
   for (Value *Val : Explored) {
-
     if (NewInsts.contains(Val))
       continue;
 
     if (auto *GEP = dyn_cast<GEPOperator>(Val)) {
-      Value *Index = NewInsts[GEP->getOperand(1)] ? NewInsts[GEP->getOperand(1)]
-                                                  : GEP->getOperand(1);
-      setInsertionPoint(Builder, GEP);
-      // Indices might need to be sign extended. GEPs will magically do
-      // this, but we need to do it ourselves here.
-      if (Index->getType()->getScalarSizeInBits() !=
-          NewInsts[GEP->getOperand(0)]->getType()->getScalarSizeInBits()) {
-        Index = Builder.CreateSExtOrTrunc(
-            Index, NewInsts[GEP->getOperand(0)]->getType(),
-            GEP->getOperand(0)->getName() + ".sext");
-      }
+      APInt Offset(IndexSize, 0);
+      MapVector<Value *, APInt> VarOffsets;
+      GEP->collectOffset(DL, IndexSize, VarOffsets, Offset);
 
-      auto *Op = NewInsts[GEP->getOperand(0)];
+      setInsertionPoint(Builder, GEP);
+      Value *Op = NewInsts[GEP->getOperand(0)];
+      Value *OffsetV = emitGEPOffset(&Builder, DL, GEP);
----------------
goldsteinn wrote:

Does `OffsetV` no longer need `sext`? Don't see what about your change makes go away.

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


More information about the llvm-commits mailing list