[llvm] [InstCombine] Canonicalize non-i8 gep of mul to i8 (PR #96606)

David Green via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 26 06:16:04 PDT 2024


================
@@ -2787,9 +2787,16 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
                                     GEP.getNoWrapFlags()));
   }
 
-  // Canonicalize scalable GEPs to an explicit offset using the llvm.vscale
-  // intrinsic. This has better support in BasicAA.
-  if (GEPEltType->isScalableTy()) {
+  // Canonicalize
+  //  - scalable GEPs to an explicit offset using the llvm.vscale intrinsic.
+  //    This has better support in BasicAA.
+  //  - gep i32 p, mul(O, C) -> gep i8, p, mul(O, C*4) to fold the two
+  //    multiplies together.
+  if (GEPEltType->isScalableTy() ||
+      (!GEPEltType->isIntegerTy(8) && GEP.getNumIndices() == 1 &&
+       match(GEP.getOperand(1),
+             m_OneUse(m_CombineOr(m_Mul(m_Value(), m_ConstantInt()),
+                                  m_Shl(m_Value(), m_ConstantInt())))))) {
----------------
davemgreen wrote:

Because it can fold into the mul? Thanks - I can take a look.

But I would prefer to do it in a separate patch if that is OK. If I rebase I can check if it comes up in practice. (Or I could just implement it anyway, let me know if you think that is better).

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


More information about the llvm-commits mailing list