================ @@ -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()) { + bool CanBeNull, CanBeFreed; + uint64_t DerefBytes = + Ptr->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed); + InBounds = DerefBytes != 0 && !CanBeNull && Offset.sle(DerefBytes); ---------------- nikic wrote:
I don't think we'd want to remove it at this point as it's still used in some key places like initial IR construction in clang -- longer term, I'd like to get rid of the two separate constant folding implementations altogether. https://github.com/llvm/llvm-project/pull/89872 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits