@@ -673,69 +673,6 @@ STATISTIC(ObjectVisitorArgument,
673673STATISTIC (ObjectVisitorLoad,
674674 " Number of load instructions with unsolved size and offset" );
675675
676- static std::optional<APInt>
677- combinePossibleConstantValues (std::optional<APInt> LHS,
678- std::optional<APInt> RHS,
679- ObjectSizeOpts::Mode EvalMode) {
680- if (!LHS || !RHS)
681- return std::nullopt ;
682- if (EvalMode == ObjectSizeOpts::Mode::Max)
683- return LHS->sge (*RHS) ? *LHS : *RHS;
684- else
685- return LHS->sle (*RHS) ? *LHS : *RHS;
686- }
687-
688- static std::optional<APInt> aggregatePossibleConstantValuesImpl (
689- const Value *V, ObjectSizeOpts::Mode EvalMode, unsigned recursionDepth) {
690- constexpr unsigned maxRecursionDepth = 4 ;
691- if (recursionDepth == maxRecursionDepth)
692- return std::nullopt ;
693-
694- if (const auto *CI = dyn_cast<ConstantInt>(V)) {
695- return CI->getValue ();
696- }
697-
698- else if (const auto *SI = dyn_cast<SelectInst>(V)) {
699- return combinePossibleConstantValues (
700- aggregatePossibleConstantValuesImpl (SI->getTrueValue (), EvalMode,
701- recursionDepth + 1 ),
702- aggregatePossibleConstantValuesImpl (SI->getFalseValue (), EvalMode,
703- recursionDepth + 1 ),
704- EvalMode);
705- }
706-
707- else if (const auto *PN = dyn_cast<PHINode>(V)) {
708- unsigned Count = PN->getNumIncomingValues ();
709- if (Count == 0 )
710- return std::nullopt ;
711- auto Acc = aggregatePossibleConstantValuesImpl (
712- PN->getIncomingValue (0 ), EvalMode, recursionDepth + 1 );
713- for (unsigned I = 1 ; Acc && I < Count; ++I) {
714- auto Tmp = aggregatePossibleConstantValuesImpl (
715- PN->getIncomingValue (1 ), EvalMode, recursionDepth + 1 );
716- Acc = combinePossibleConstantValues (Acc, Tmp, EvalMode);
717- }
718- return Acc;
719- }
720-
721- return std::nullopt ;
722- }
723-
724- static std::optional<APInt>
725- aggregatePossibleConstantValues (const Value *V, ObjectSizeOpts::Mode EvalMode) {
726- if (auto *CI = dyn_cast<ConstantInt>(V))
727- return CI->getValue ();
728-
729- if (EvalMode != ObjectSizeOpts::Mode::Min &&
730- EvalMode != ObjectSizeOpts::Mode::Max)
731- return std::nullopt ;
732-
733- // Not using computeConstantRange here because we cannot guarantee it's not
734- // doing optimization based on UB which we want to avoid when expanding
735- // __builtin_object_size.
736- return aggregatePossibleConstantValuesImpl (V, EvalMode, 0u );
737- }
738-
739676// / Align \p Size according to \p Alignment. If \p Size is greater than
740677// / getSignedMaxValue(), set it as unknown as we can only represent signed value
741678// / in OffsetSpan.
@@ -783,36 +720,11 @@ OffsetSpan ObjectSizeOffsetVisitor::computeImpl(Value *V) {
783720 V = V->stripAndAccumulateConstantOffsets (
784721 DL, Offset, /* AllowNonInbounds */ true , /* AllowInvariantGroup */ true );
785722
786- // Give it another try with approximated analysis. We don't start with this
787- // one because stripAndAccumulateConstantOffsets behaves differently wrt.
788- // overflows if we provide an external Analysis.
789- if ((Options.EvalMode == ObjectSizeOpts::Mode::Min ||
790- Options.EvalMode == ObjectSizeOpts::Mode::Max) &&
791- isa<GEPOperator>(V)) {
792- // External Analysis used to compute the Min/Max value of individual Offsets
793- // within a GEP.
794- ObjectSizeOpts::Mode EvalMode =
795- Options.EvalMode == ObjectSizeOpts::Mode::Min
796- ? ObjectSizeOpts::Mode::Max
797- : ObjectSizeOpts::Mode::Min;
798- auto OffsetRangeAnalysis = [EvalMode](Value &VOffset, APInt &Offset) {
799- if (auto PossibleOffset =
800- aggregatePossibleConstantValues (&VOffset, EvalMode)) {
801- Offset = *PossibleOffset;
802- return true ;
803- }
804- return false ;
805- };
806-
807- V = V->stripAndAccumulateConstantOffsets (
808- DL, Offset, /* AllowNonInbounds */ true , /* AllowInvariantGroup */ true ,
809- /* ExternalAnalysis=*/ OffsetRangeAnalysis);
810- }
811-
812723 // Later we use the index type size and zero but it will match the type of the
813724 // value that is passed to computeImpl.
814725 IntTyBits = DL.getIndexTypeSizeInBits (V->getType ());
815726 Zero = APInt::getZero (IntTyBits);
727+
816728 OffsetSpan ORT = computeValue (V);
817729
818730 bool IndexTypeSizeChanged = InitialIntTyBits != IntTyBits;
@@ -900,9 +812,8 @@ OffsetSpan ObjectSizeOffsetVisitor::visitAllocaInst(AllocaInst &I) {
900812 return OffsetSpan (Zero, align (Size, I.getAlign ()));
901813
902814 Value *ArraySize = I.getArraySize ();
903- if (auto PossibleSize =
904- aggregatePossibleConstantValues (ArraySize, Options.EvalMode )) {
905- APInt NumElems = *PossibleSize;
815+ if (const ConstantInt *C = dyn_cast<ConstantInt>(ArraySize)) {
816+ APInt NumElems = C->getValue ();
906817 if (!CheckedZextOrTrunc (NumElems))
907818 return ObjectSizeOffsetVisitor::unknown ();
908819
@@ -928,18 +839,7 @@ OffsetSpan ObjectSizeOffsetVisitor::visitArgument(Argument &A) {
928839}
929840
930841OffsetSpan ObjectSizeOffsetVisitor::visitCallBase (CallBase &CB) {
931- auto Mapper = [this ](const Value *V) -> const Value * {
932- if (!V->getType ()->isIntegerTy ())
933- return V;
934-
935- if (auto PossibleBound =
936- aggregatePossibleConstantValues (V, Options.EvalMode ))
937- return ConstantInt::get (V->getType (), *PossibleBound);
938-
939- return V;
940- };
941-
942- if (std::optional<APInt> Size = getAllocSize (&CB, TLI, Mapper)) {
842+ if (std::optional<APInt> Size = getAllocSize (&CB, TLI)) {
943843 // Very large unsigned value cannot be represented as OffsetSpan.
944844 if (Size->isNegative ())
945845 return ObjectSizeOffsetVisitor::unknown ();
0 commit comments