[llvm] [CVP] Remove Zero-Index GEP (PR #144831)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 18 23:12:01 PDT 2025


================
@@ -1252,6 +1253,23 @@ static bool processTrunc(TruncInst *TI, LazyValueInfo *LVI) {
   return Changed;
 }
 
+// remove a GEP if all indices are in range [0, 1)
+static bool processGEP(GetElementPtrInst *GEP, LazyValueInfo *LVI) {
+  for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) {
+    ConstantRange Range = LVI->getConstantRangeAtUse(GEP->getOperandUse(i),
+                                                     /*UndefAllowed=*/false);
+    const APInt *C = Range.getSingleElement();
+    if (!C || !C->isZero())
+      return false;
+  }
+
+  // all indices are zero
+  Value *Ptr = GEP->getPointerOperand();
+  GEP->replaceAllUsesWith(Ptr);
+  ++NumGEPs;
----------------
dtcxzyw wrote:

```suggestion
  GEP->eraseFromParent();
  ++NumGEPs;
```

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


More information about the llvm-commits mailing list