Index: include/llvm/IR/Instructions.h =================================================================== --- include/llvm/IR/Instructions.h +++ include/llvm/IR/Instructions.h @@ -292,6 +292,12 @@ return getPointerOperand()->getType()->getPointerAddressSpace(); } + /// \brief Returns the explicit aligment, if present, or the ABI one. + unsigned getActualAlignment() const; + + /// \brief Returns the size in bytes of the loaded value. + uint64_t getLoadedSize() const; + // Methods for support type inquiry through isa, cast, and dyn_cast: static inline bool classof(const Instruction *I) { return I->getOpcode() == Instruction::Load; Index: lib/Analysis/MemDerefPrinter.cpp =================================================================== --- lib/Analysis/MemDerefPrinter.cpp +++ lib/Analysis/MemDerefPrinter.cpp @@ -56,10 +56,8 @@ for (auto &I: instructions(F)) { if (LoadInst *LI = dyn_cast(&I)) { Value *PO = LI->getPointerOperand(); - uint64_t Size = DL.getTypeStoreSize(LI->getType()); - unsigned Align = LI->getAlignment(); - if (Align == 0) - Align = DL.getABITypeAlignment(LI->getType()); + uint64_t Size = LI->getLoadedSize(); + unsigned Align = LI->getActualAlignment(); if (isDereferenceablePointer(PO, Size, DL)) Deref.push_back(PO); if (isDereferenceableAndAlignedPointer(PO, Size, Align, DL)) Index: lib/Analysis/ValueTracking.cpp =================================================================== --- lib/Analysis/ValueTracking.cpp +++ lib/Analysis/ValueTracking.cpp @@ -3372,12 +3372,9 @@ Attribute::SanitizeAddress)) return false; const DataLayout &DL = LI->getModule()->getDataLayout(); - uint64_t Size = DL.getTypeStoreSize(LI->getType()); - unsigned Align = LI->getAlignment(); - if (Align == 0) - Align = DL.getABITypeAlignment(LI->getType()); return isDereferenceableAndAlignedPointer( - LI->getPointerOperand(), Size, Align, DL, CtxI, DT, TLI); + LI->getPointerOperand(), LI->getLoadedSize(), + LI->getActualAlignment(), DL, CtxI, DT, TLI); } case Instruction::Call: { if (const IntrinsicInst *II = dyn_cast(Inst)) { Index: lib/IR/Instructions.cpp =================================================================== --- lib/IR/Instructions.cpp +++ lib/IR/Instructions.cpp @@ -1293,6 +1293,20 @@ assert(getAlignment() == Align && "Alignment representation error!"); } +unsigned LoadInst::getActualAlignment() const { + unsigned Align = getAlignment(); + if (Align == 0) { + const DataLayout &DL = getModule()->getDataLayout(); + return DL.getABITypeAlignment(getType()); + } + return Align; +} + +uint64_t LoadInst::getLoadedSize() const { + const DataLayout &DL = getModule()->getDataLayout(); + return DL.getTypeStoreSize(getType()); +} + //===----------------------------------------------------------------------===// // StoreInst Implementation //===----------------------------------------------------------------------===//