[clang] [compiler-rt] [llvm] [ConstantFolding] Canonicalize constexpr GEPs to i8 (PR #89872)
Nikita Popov via cfe-commits
cfe-commits at lists.llvm.org
Sun Apr 28 22:15:27 PDT 2024
================
@@ -944,43 +943,18 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
return ConstantExpr::getIntToPtr(C, ResTy);
}
- // Otherwise form a regular getelementptr. Recompute the indices so that
- // we eliminate over-indexing of the notional static type array bounds.
- // This makes it easy to determine if the getelementptr is "inbounds".
-
- // For GEPs of GlobalValues, use the value type, otherwise use an i8 GEP.
- if (auto *GV = dyn_cast<GlobalValue>(Ptr))
- SrcElemTy = GV->getValueType();
- else
- SrcElemTy = Type::getInt8Ty(Ptr->getContext());
-
- if (!SrcElemTy->isSized())
- return nullptr;
-
- Type *ElemTy = SrcElemTy;
- SmallVector<APInt> Indices = DL.getGEPIndicesForOffset(ElemTy, Offset);
- if (Offset != 0)
- return nullptr;
-
- // Try to add additional zero indices to reach the desired result element
- // type.
- // TODO: Should we avoid extra zero indices if ResElemTy can't be reached and
- // we'll have to insert a bitcast anyway?
- while (ElemTy != ResElemTy) {
- Type *NextTy = GetElementPtrInst::getTypeAtIndex(ElemTy, (uint64_t)0);
- if (!NextTy)
- break;
-
- Indices.push_back(APInt::getZero(isa<StructType>(ElemTy) ? 32 : BitWidth));
- ElemTy = NextTy;
+ // Try to infer inbounds for GEPs of globals.
+ if (!InBounds && Offset.isNonNegative()) {
----------------
nikic wrote:
https://github.com/llvm/llvm-project/blob/main/llvm/test/Transforms/GlobalOpt/large-element-size.ll gets an incorrect inbounds without the check.
https://github.com/llvm/llvm-project/pull/89872
More information about the cfe-commits
mailing list