diff --git a/llvm/lib/Analysis/Loads.cpp b/llvm/lib/Analysis/Loads.cpp --- a/llvm/lib/Analysis/Loads.cpp +++ b/llvm/lib/Analysis/Loads.cpp @@ -85,6 +85,15 @@ .isMinValue()) return false; + // A pointer to a global value is dereferenceable if all dereferenced bits + // are within the global's size. + if (auto *GV = dyn_cast(Base)) { + if (((Offset + Size.sextOrTrunc(Offset.getBitWidth())) * 8) + .ule( + DL.getTypeSizeInBits(GV->getType()->getPointerElementType()))) + return true; + } + // If the base pointer is dereferenceable for Offset+Size bytes, then the // GEP (== Base + Offset) is dereferenceable for Size bytes. If the base // pointer is aligned to Align bytes, and the Offset is divisible by Align