[llvm] [InstCombine] Canonicalize more geps with constant gep bases and constant offsets. (PR #110033)
David Green via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 3 11:30:37 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) &&
----------------
davemgreen wrote:
I had an older version that was canonicalizing more generally (in a different way IIRC). It was causing some relatively small perf issues so I was trying to avoid making this over-broad. I think that this case should hopefully be OK though, to do the same thing if the geps indices are all constant.
https://github.com/llvm/llvm-project/pull/110033
More information about the llvm-commits
mailing list