[clang] [flang] [llvm] [InstCombine] Canonicalize GEP source element types (PR #180745)

Nikita Popov via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 3 07:42:15 PST 2026


================
@@ -3488,6 +3488,24 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
         BackIndices, GEP.getNoWrapFlags());
   }
 
+  // Canonicalize gep %T to gep [sizeof(%T) x i8]:
+  auto IsCanonicalType = [](Type *Ty) {
+    if (auto *AT = dyn_cast<ArrayType>(Ty))
+      Ty = AT->getElementType();
+    return Ty->isIntegerTy(8);
+  };
+  if (Indices.size() == 1 && !IsCanonicalType(GEPEltType)) {
+    TypeSize Scale = DL.getTypeAllocSize(GEPEltType);
----------------
nikic wrote:

Opened https://github.com/llvm/llvm-project/pull/184364 to fix the incorrect zero index stripping.

As a followup, fully canonicalizing GEPs in one pass instead of the combination of 4 different transforms would allow us to directly emit the correct stride in `[2 x i8]` form, which would allow supporting overaligned vectors again.

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


More information about the cfe-commits mailing list