================ @@ -1482,6 +1482,20 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C, switch (Opcode) { default: llvm_unreachable("Missing case"); + case Instruction::PtrToAddr: + if (auto *GEP = dyn_cast<GEPOperator>(C)) { + // For now just handle the basic case of GEPs on NULL for ptrtoaddr. + // (ptrtoaddr (gep null, x)) -> x + // (ptrtoaddr (gep (gep null, x), y) -> x + y, etc. + unsigned BitWidth = DL.getIndexTypeSizeInBits(GEP->getType()); + APInt BaseOffset(BitWidth, 0); + auto *Base = cast<Constant>(GEP->stripAndAccumulateConstantOffsets( + DL, BaseOffset, /*AllowNonInbounds=*/true)); + if (Base->isNullValue()) { + return ConstantInt::get(C->getContext(), BaseOffset); + } + } ---------------- nikic wrote:
I think this code is untested? Probably best to leave it for a followup. https://github.com/llvm/llvm-project/pull/139357 _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits