25
25
#include " llvm/IR/Constants.h"
26
26
#include " llvm/IR/DataLayout.h"
27
27
#include " llvm/IR/DerivedTypes.h"
28
+ #include " llvm/IR/Dominators.h"
28
29
#include " llvm/IR/Function.h"
29
30
#include " llvm/IR/GlobalAlias.h"
30
31
#include " llvm/IR/GlobalVariable.h"
@@ -590,19 +591,28 @@ Value *llvm::lowerObjectSizeCall(IntrinsicInst *ObjectSize,
590
591
const TargetLibraryInfo *TLI,
591
592
bool MustSucceed) {
592
593
return lowerObjectSizeCall (ObjectSize, DL, TLI, /* AAResults=*/ nullptr ,
593
- MustSucceed);
594
+ /* DT= */ nullptr , MustSucceed);
594
595
}
595
596
596
597
Value *llvm::lowerObjectSizeCall (
597
598
IntrinsicInst *ObjectSize, const DataLayout &DL,
598
599
const TargetLibraryInfo *TLI, AAResults *AA, bool MustSucceed,
599
600
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) {
600
609
assert (ObjectSize->getIntrinsicID () == Intrinsic::objectsize &&
601
610
" ObjectSize must be a call to llvm.objectsize!" );
602
611
603
612
bool MaxVal = cast<ConstantInt>(ObjectSize->getArgOperand (1 ))->isZero ();
604
613
ObjectSizeOpts EvalOptions;
605
614
EvalOptions.AA = AA;
615
+ EvalOptions.DT = DT;
606
616
607
617
// Unless we have to fold this to something, try to be as accurate as
608
618
// possible.
@@ -709,14 +719,46 @@ OffsetSpan ObjectSizeOffsetVisitor::computeImpl(Value *V) {
709
719
// readjust the APInt as we pass it upwards in order for the APInt to match
710
720
// the type the caller passed in.
711
721
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
+
712
754
V = V->stripAndAccumulateConstantOffsets (
713
- DL, Offset, /* AllowNonInbounds */ true , /* AllowInvariantGroup */ true );
755
+ DL, Offset, /* AllowNonInbounds */ true , /* AllowInvariantGroup */ true ,
756
+ /* ExternalAnalysis=*/ OffsetRangeAnalysis);
714
757
715
758
// Later we use the index type size and zero but it will match the type of the
716
759
// value that is passed to computeImpl.
717
760
IntTyBits = DL.getIndexTypeSizeInBits (V->getType ());
718
761
Zero = APInt::getZero (IntTyBits);
719
-
720
762
OffsetSpan ORT = computeValue (V);
721
763
722
764
bool IndexTypeSizeChanged = InitialIntTyBits != IntTyBits;
@@ -794,6 +836,26 @@ OffsetSpan ObjectSizeOffsetVisitor::visitAllocaInst(AllocaInst &I) {
794
836
Size = Size.umul_ov (NumElems, Overflow);
795
837
return Overflow ? ObjectSizeOffsetVisitor::unknown ()
796
838
: 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 ()));
797
859
}
798
860
return ObjectSizeOffsetVisitor::unknown ();
799
861
}
@@ -811,7 +873,32 @@ OffsetSpan ObjectSizeOffsetVisitor::visitArgument(Argument &A) {
811
873
}
812
874
813
875
OffsetSpan 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
+ }))
815
902
return OffsetSpan (Zero, *Size);
816
903
return ObjectSizeOffsetVisitor::unknown ();
817
904
}
0 commit comments