[llvm] [InstCombine] Canonicalize `gep T, (gep i8, base, C1), (Index + 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
Wed Apr 24 00:56:36 PDT 2024
- Previous message: [llvm] [InstCombine] Canonicalize `gep T, (gep i8, base, C1), (Index + 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 + C2)` into `gep T, (gep i8, base, C1 + C2 * sizeof(T)), Index` (PR #76177)
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
================
@@ -2317,6 +2317,43 @@ static Instruction *foldSelectGEP(GetElementPtrInst &GEP,
return SelectInst::Create(Cond, NewTrueC, NewFalseC, "", nullptr, Sel);
}
+// Canonicalization:
+// gep T, (gep i8, base, C1), (Index + 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();
+ Value *Base;
+ const APInt *C1;
+ if (!match(Src, m_PtrAdd(m_Value(Base), m_APInt(C1))))
+ return nullptr;
+ Value *VarIndex;
+ const APInt *C2;
+ Type *PtrTy = Src->getType()->getScalarType();
+ unsigned IndexSizeInBits = DL.getIndexTypeSizeInBits(PtrTy);
+ if (!match(GEP.getOperand(1), m_AddLike(m_Value(VarIndex), m_APInt(C2))))
+ return nullptr;
+ if (C1->getBitWidth() != IndexSizeInBits ||
+ C2->getBitWidth() != IndexSizeInBits)
+ return nullptr;
+ Type *BaseType = GEP.getSourceElementType();
+ if (isa<ScalableVectorType>(BaseType))
+ return nullptr;
+ APInt TypeSize(IndexSizeInBits, DL.getTypeAllocSize(BaseType));
+ APInt NewOffset = TypeSize * *C2 + *C1;
+ if (NewOffset.isZero() ||
+ (Src->hasOneUse() && GEP.getOperand(1)->hasOneUse())) {
+ Value *GEPConst = IC.Builder.CreateGEP(IC.Builder.getInt8Ty(), Base,
----------------
nikic wrote:
```suggestion
Value *GEPConst = IC.Builder.CreatePtrAdd(Base,
```
https://github.com/llvm/llvm-project/pull/76177
- Previous message: [llvm] [InstCombine] Canonicalize `gep T, (gep i8, base, C1), (Index + 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 + 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