[llvm] [InstCombine] Canonicalize `gep T, (gep i8, base, C1), (Index +nsw C2)` into `gep T, (gep i8, base, C1 + C2 * sizeof(T)), Index` (PR #76177)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 18 23:33:33 PDT 2024
- Previous message: [llvm] [InstCombine] Canonicalize `gep T, (gep i8, base, C1), (Index +nsw C2)` into `gep T, (gep i8, base, C1 + C2 * sizeof(T)), Index` (PR #76177)
- Next message: [llvm] [InstCombine] Canonicalize `gep T, (gep i8, base, C1), (Index +nsw C2)` into `gep T, (gep i8, base, C1 + C2 * sizeof(T)), Index` (PR #76177)
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
================
@@ -2038,6 +2038,54 @@ static Instruction *foldSelectGEP(GetElementPtrInst &GEP,
return SelectInst::Create(Cond, NewTrueC, NewFalseC, "", nullptr, Sel);
}
+// Canonicalization:
+// gep T, (gep i8, base, C1), (Index +nsw C2) into
+// gep T, (gep i8, base, C1 + C2 * sizeof(T)), Index
+static Instruction *canonicalizeGEPOfConstGEPI8(GetElementPtrInst &GEP,
+ GEPOperator *Src,
+ InstCombinerImpl &IC) {
+ if (GEP.getNumIndices() != 1)
+ return nullptr;
+ auto &DL = IC.getDataLayout();
+ if (!Src->getSourceElementType()->isIntegerTy(8) ||
+ !Src->hasAllConstantIndices())
+ return nullptr;
+ Value *VarIndex;
+ const APInt *C2;
+ Type *PtrTy = Src->getType()->getScalarType();
+ unsigned IndexSizeInBits = DL.getIndexTypeSizeInBits(PtrTy);
+ if (!(GEP.getOperand(1)->getType()->getScalarSizeInBits() >=
+ IndexSizeInBits &&
+ match(GEP.getOperand(1), m_Add(m_Value(VarIndex), m_APInt(C2)))) &&
+ !match(GEP.getOperand(1),
+ m_SExtOrSelf(
+ m_CombineOr(m_NSWAdd(m_Value(VarIndex), m_APInt(C2)),
+ m_DisjointOr(m_Value(VarIndex), m_APInt(C2))))))
+ return nullptr;
+ Type *BaseType = GEP.getSourceElementType();
+ APInt C1(IndexSizeInBits, 0);
+ // Add the offset for Src (which is fully constant).
+ if (!Src->accumulateConstantOffset(DL, C1))
+ return nullptr;
+ APInt TypeSize(IndexSizeInBits, DL.getTypeAllocSize(BaseType));
----------------
nikic wrote:
Can you please add a test without outer scalable GEP? I think it will assert...
https://github.com/llvm/llvm-project/pull/76177
- Previous message: [llvm] [InstCombine] Canonicalize `gep T, (gep i8, base, C1), (Index +nsw C2)` into `gep T, (gep i8, base, C1 + C2 * sizeof(T)), Index` (PR #76177)
- Next message: [llvm] [InstCombine] Canonicalize `gep T, (gep i8, base, C1), (Index +nsw C2)` into `gep T, (gep i8, base, C1 + C2 * sizeof(T)), Index` (PR #76177)
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
More information about the llvm-commits
mailing list