2525#include " llvm/IR/Constants.h"
2626#include " llvm/IR/DataLayout.h"
2727#include " llvm/IR/DerivedTypes.h"
28+ #include " llvm/IR/Dominators.h"
2829#include " llvm/IR/Function.h"
2930#include " llvm/IR/GlobalAlias.h"
3031#include " llvm/IR/GlobalVariable.h"
@@ -590,19 +591,28 @@ Value *llvm::lowerObjectSizeCall(IntrinsicInst *ObjectSize,
590591 const TargetLibraryInfo *TLI,
591592 bool MustSucceed) {
592593 return lowerObjectSizeCall (ObjectSize, DL, TLI, /* AAResults=*/ nullptr ,
593- MustSucceed);
594+ /* DT= */ nullptr , MustSucceed);
594595}
595596
596597Value *llvm::lowerObjectSizeCall (
597598 IntrinsicInst *ObjectSize, const DataLayout &DL,
598599 const TargetLibraryInfo *TLI, AAResults *AA, bool MustSucceed,
599600 SmallVectorImpl<Instruction *> *InsertedInstructions) {
601+ return lowerObjectSizeCall (ObjectSize, DL, TLI, AA, /* DT=*/ nullptr ,
602+ MustSucceed, InsertedInstructions);
603+ }
604+
605+ Value *llvm::lowerObjectSizeCall (
606+ IntrinsicInst *ObjectSize, const DataLayout &DL,
607+ const TargetLibraryInfo *TLI, AAResults *AA, DominatorTree *DT,
608+ bool MustSucceed, SmallVectorImpl<Instruction *> *InsertedInstructions) {
600609 assert (ObjectSize->getIntrinsicID () == Intrinsic::objectsize &&
601610 " ObjectSize must be a call to llvm.objectsize!" );
602611
603612 bool MaxVal = cast<ConstantInt>(ObjectSize->getArgOperand (1 ))->isZero ();
604613 ObjectSizeOpts EvalOptions;
605614 EvalOptions.AA = AA;
615+ EvalOptions.DT = DT;
606616
607617 // Unless we have to fold this to something, try to be as accurate as
608618 // possible.
@@ -709,14 +719,46 @@ OffsetSpan ObjectSizeOffsetVisitor::computeImpl(Value *V) {
709719 // readjust the APInt as we pass it upwards in order for the APInt to match
710720 // the type the caller passed in.
711721 APInt Offset (InitialIntTyBits, 0 );
722+
723+ // External Analysis used to compute the Min/Max value of individual Offsets
724+ // within a GEP.
725+ auto OffsetRangeAnalysis = [this , V](Value &VOffset, APInt &Offset) {
726+ if (auto *C = dyn_cast<ConstantInt>(&VOffset)) {
727+ Offset = C->getValue ();
728+ return true ;
729+ }
730+ if (Options.EvalMode != ObjectSizeOpts::Mode::Min &&
731+ Options.EvalMode != ObjectSizeOpts::Mode::Max) {
732+ return false ;
733+ }
734+ ConstantRange CR = computeConstantRange (
735+ &VOffset, /* ForSigned*/ true , /* UseInstrInfo*/ true , /* AC=*/ nullptr ,
736+ /* CtxtI=*/ dyn_cast<Instruction>(V), /* DT=*/ Options.DT );
737+ if (CR.isFullSet ())
738+ return false ;
739+
740+ if (Options.EvalMode == ObjectSizeOpts::Mode::Min) {
741+ Offset = CR.getSignedMax ();
742+ // Upper bound actually unknown.
743+ if (Offset.isMaxSignedValue ())
744+ return false ;
745+ } else {
746+ Offset = CR.getSignedMin ();
747+ // Lower bound actually unknown.
748+ if (Offset.isMinSignedValue ())
749+ return false ;
750+ }
751+ return true ;
752+ };
753+
712754 V = V->stripAndAccumulateConstantOffsets (
713- DL, Offset, /* AllowNonInbounds */ true , /* AllowInvariantGroup */ true );
755+ DL, Offset, /* AllowNonInbounds */ true , /* AllowInvariantGroup */ true ,
756+ /* ExternalAnalysis=*/ OffsetRangeAnalysis);
714757
715758 // Later we use the index type size and zero but it will match the type of the
716759 // value that is passed to computeImpl.
717760 IntTyBits = DL.getIndexTypeSizeInBits (V->getType ());
718761 Zero = APInt::getZero (IntTyBits);
719-
720762 OffsetSpan ORT = computeValue (V);
721763
722764 bool IndexTypeSizeChanged = InitialIntTyBits != IntTyBits;
@@ -794,6 +836,26 @@ OffsetSpan ObjectSizeOffsetVisitor::visitAllocaInst(AllocaInst &I) {
794836 Size = Size.umul_ov (NumElems, Overflow);
795837 return Overflow ? ObjectSizeOffsetVisitor::unknown ()
796838 : OffsetSpan (Zero, align (Size, I.getAlign ()));
839+ } else {
840+ ConstantRange CR =
841+ computeConstantRange (ArraySize, /* ForSigned*/ false ,
842+ /* UseInstrInfo*/ true , /* AC=*/ nullptr ,
843+ /* CtxtI=*/ &I, /* DT=*/ Options.DT );
844+ if (CR.isFullSet ())
845+ return ObjectSizeOffsetVisitor::unknown ();
846+ APInt Bound;
847+ if (Options.EvalMode == ObjectSizeOpts::Mode::Max) {
848+ Bound = CR.getUnsignedMax ();
849+ // Upper bound actually unknown.
850+ if (Bound.isMaxValue ())
851+ return ObjectSizeOffsetVisitor::unknown ();
852+ } else {
853+ Bound = CR.getUnsignedMin ();
854+ // Lower bound actually unknown.
855+ if (Bound.isMinValue ())
856+ return ObjectSizeOffsetVisitor::unknown ();
857+ }
858+ return OffsetSpan (Zero, align (Bound, I.getAlign ()));
797859 }
798860 return ObjectSizeOffsetVisitor::unknown ();
799861}
@@ -811,7 +873,32 @@ OffsetSpan ObjectSizeOffsetVisitor::visitArgument(Argument &A) {
811873}
812874
813875OffsetSpan ObjectSizeOffsetVisitor::visitCallBase (CallBase &CB) {
814- if (std::optional<APInt> Size = getAllocSize (&CB, TLI))
876+ if (std::optional<APInt> Size =
877+ getAllocSize (&CB, TLI, [&CB, this ](const Value *V) -> const Value * {
878+ if (!V->getType ()->isIntegerTy ())
879+ return V;
880+ if (isa<ConstantInt>(V))
881+ return V;
882+ ConstantRange CR = computeConstantRange (
883+ V, /* ForSigned*/ false , /* UseInstrInfo*/ true , /* AC=*/ nullptr ,
884+ /* CtxtI=*/ &CB, /* DT=*/ Options.DT );
885+ if (CR.isFullSet ())
886+ return V;
887+
888+ APInt Bound;
889+ if (Options.EvalMode == ObjectSizeOpts::Mode::Max) {
890+ Bound = CR.getUnsignedMax ();
891+ // Upper bound actually unknown.
892+ if (Bound.isMaxValue ())
893+ return V;
894+ } else {
895+ Bound = CR.getUnsignedMin ();
896+ // Lower bound actually unknown.
897+ if (Bound.isMinValue ())
898+ return V;
899+ }
900+ return ConstantInt::get (V->getType (), Bound);
901+ }))
815902 return OffsetSpan (Zero, *Size);
816903 return ObjectSizeOffsetVisitor::unknown ();
817904}
0 commit comments