[llvm] [InstCombine] Canonicalize more geps with constant gep bases and constant offsets. (PR #110033)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 27 00:36:12 PDT 2024


================
@@ -2736,6 +2736,35 @@ Value *InstCombiner::getFreelyInvertedImpl(Value *V, bool WillInvertAllUses,
   return nullptr;
 }
 
+// Return true if we should canonicalize the gep to a i8 ptradd.
+static bool shouldCanonicalizeGEPToPtrAdd(GetElementPtrInst &GEP) {
+  Value *PtrOp = GEP.getOperand(0);
+  Type *GEPEltType = GEP.getSourceElementType();
+  if (GEPEltType->isIntegerTy(8))
+    return false;
+
+  // Canonicalize scalable GEPs to an explicit offset using the llvm.vscale
+  // intrinsic. This has better support in BasicAA.
+  if (GEPEltType->isScalableTy())
+    return true;
+
+  // gep i32 p, mul(O, C) -> gep i8, p, mul(O, C*4) to fold the two multiplies
+  // together.
+  if (GEP.getNumIndices() == 1 &&
+      match(GEP.getOperand(1),
+            m_OneUse(m_CombineOr(m_Mul(m_Value(), m_ConstantInt()),
+                                 m_Shl(m_Value(), m_ConstantInt())))))
+    return true;
+
+  // gep (gep @global, C1), %x, C2 is expanded so the two constants can
+  // possibly be merged together.
+  return isa<GEPOperator>(PtrOp) && isa<ConstantExpr>(PtrOp) &&
----------------
nikic wrote:

Hm, should this one be checking for a constant expression, or that the inner GEPOperator hasAllConstantIndices()? Is there a reason why we want to apply this only to GEPs of globals in particular, rather than all constant GEPs?

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


More information about the llvm-commits mailing list