@@ -683,29 +683,30 @@ combinePossibleConstantValues(std::optional<APInt> LHS,
683
683
}
684
684
685
685
static std::optional<APInt> aggregatePossibleConstantValuesImpl (
686
- const Value *V, ObjectSizeOpts::Mode EvalMode, unsigned recursionDepth) {
686
+ const Value *V, ObjectSizeOpts::Mode EvalMode, unsigned BitWidth,
687
+ unsigned recursionDepth) {
687
688
constexpr unsigned maxRecursionDepth = 4 ;
688
689
if (recursionDepth == maxRecursionDepth)
689
690
return std::nullopt;
690
691
691
692
if (const auto *CI = dyn_cast<ConstantInt>(V)) {
692
- return CI->getValue ();
693
+ return CI->getValue (). sextOrTrunc (BitWidth) ;
693
694
} else if (const auto *SI = dyn_cast<SelectInst>(V)) {
694
695
return combinePossibleConstantValues (
695
696
aggregatePossibleConstantValuesImpl (SI->getTrueValue (), EvalMode,
696
- recursionDepth + 1 ),
697
+ BitWidth, recursionDepth + 1 ),
697
698
aggregatePossibleConstantValuesImpl (SI->getFalseValue (), EvalMode,
698
- recursionDepth + 1 ),
699
+ BitWidth, recursionDepth + 1 ),
699
700
EvalMode);
700
701
} else if (const auto *PN = dyn_cast<PHINode>(V)) {
701
702
unsigned Count = PN->getNumIncomingValues ();
702
703
if (Count == 0 )
703
704
return std::nullopt;
704
705
auto Acc = aggregatePossibleConstantValuesImpl (
705
- PN->getIncomingValue (0 ), EvalMode, recursionDepth + 1 );
706
+ PN->getIncomingValue (0 ), EvalMode, BitWidth, recursionDepth + 1 );
706
707
for (unsigned I = 1 ; Acc && I < Count; ++I) {
707
708
auto Tmp = aggregatePossibleConstantValuesImpl (
708
- PN->getIncomingValue (I), EvalMode, recursionDepth + 1 );
709
+ PN->getIncomingValue (I), EvalMode, BitWidth, recursionDepth + 1 );
709
710
Acc = combinePossibleConstantValues (Acc, Tmp, EvalMode);
710
711
}
711
712
return Acc;
@@ -715,9 +716,10 @@ static std::optional<APInt> aggregatePossibleConstantValuesImpl(
715
716
}
716
717
717
718
static std::optional<APInt>
718
- aggregatePossibleConstantValues (const Value *V, ObjectSizeOpts::Mode EvalMode) {
719
+ aggregatePossibleConstantValues (const Value *V, ObjectSizeOpts::Mode EvalMode,
720
+ unsigned BitWidth) {
719
721
if (auto *CI = dyn_cast<ConstantInt>(V))
720
- return CI->getValue ();
722
+ return CI->getValue (). sextOrTrunc (BitWidth) ;
721
723
722
724
if (EvalMode != ObjectSizeOpts::Mode::Min &&
723
725
EvalMode != ObjectSizeOpts::Mode::Max)
@@ -726,7 +728,7 @@ aggregatePossibleConstantValues(const Value *V, ObjectSizeOpts::Mode EvalMode) {
726
728
// Not using computeConstantRange here because we cannot guarantee it's not
727
729
// doing optimization based on UB which we want to avoid when expanding
728
730
// __builtin_object_size.
729
- return aggregatePossibleConstantValuesImpl (V, EvalMode, 0u );
731
+ return aggregatePossibleConstantValuesImpl (V, EvalMode, BitWidth, 0u );
730
732
}
731
733
732
734
// / Align \p Size according to \p Alignment. If \p Size is greater than
@@ -788,9 +790,14 @@ OffsetSpan ObjectSizeOffsetVisitor::computeImpl(Value *V) {
788
790
Options.EvalMode == ObjectSizeOpts::Mode::Min
789
791
? ObjectSizeOpts::Mode::Max
790
792
: ObjectSizeOpts::Mode::Min;
791
- auto OffsetRangeAnalysis = [EvalMode](Value &VOffset, APInt &Offset) {
793
+ // For a GEPOperator the indices are first converted to offsets in the
794
+ // pointer’s index type, so we need to provide the index type to make sure
795
+ // the min/max operations are performed in correct type.
796
+ unsigned IdxTyBits = DL.getIndexTypeSizeInBits (V->getType ());
797
+ auto OffsetRangeAnalysis = [EvalMode, IdxTyBits](Value &VOffset,
798
+ APInt &Offset) {
792
799
if (auto PossibleOffset =
793
- aggregatePossibleConstantValues (&VOffset, EvalMode)) {
800
+ aggregatePossibleConstantValues (&VOffset, EvalMode, IdxTyBits )) {
794
801
Offset = *PossibleOffset;
795
802
return true ;
796
803
}
@@ -900,8 +907,9 @@ OffsetSpan ObjectSizeOffsetVisitor::visitAllocaInst(AllocaInst &I) {
900
907
return OffsetSpan (Zero, align (Size, I.getAlign ()));
901
908
902
909
Value *ArraySize = I.getArraySize ();
903
- if (auto PossibleSize =
904
- aggregatePossibleConstantValues (ArraySize, Options.EvalMode )) {
910
+ if (auto PossibleSize = aggregatePossibleConstantValues (
911
+ ArraySize, Options.EvalMode ,
912
+ ArraySize->getType ()->getScalarSizeInBits ())) {
905
913
APInt NumElems = *PossibleSize;
906
914
if (!CheckedZextOrTrunc (NumElems))
907
915
return ObjectSizeOffsetVisitor::unknown ();
@@ -932,8 +940,8 @@ OffsetSpan ObjectSizeOffsetVisitor::visitCallBase(CallBase &CB) {
932
940
if (!V->getType ()->isIntegerTy ())
933
941
return V;
934
942
935
- if (auto PossibleBound =
936
- aggregatePossibleConstantValues ( V, Options.EvalMode ))
943
+ if (auto PossibleBound = aggregatePossibleConstantValues (
944
+ V, Options.EvalMode , V-> getType ()-> getScalarSizeInBits () ))
937
945
return ConstantInt::get (V->getType (), *PossibleBound);
938
946
939
947
return V;
0 commit comments