35
35
#include " llvm/IR/Type.h"
36
36
#include " llvm/IR/Value.h"
37
37
#include " llvm/Support/Casting.h"
38
+ #include " llvm/Support/CommandLine.h"
38
39
#include " llvm/Support/Debug.h"
39
40
#include " llvm/Support/MathExtras.h"
40
41
#include " llvm/Support/raw_ostream.h"
@@ -50,6 +51,12 @@ using namespace llvm;
50
51
51
52
#define DEBUG_TYPE " memory-builtins"
52
53
54
+ static cl::opt<unsigned > ObjectSizeOffsetVisitorMaxVisitInstructions (
55
+ " object-size-offset-visitor-max-visit-instructions" ,
56
+ cl::desc (" Maximum number of instructions for ObjectSizeOffsetVisitor to "
57
+ " look at" ),
58
+ cl::init(100 ));
59
+
53
60
enum AllocType : uint8_t {
54
61
OpNewLike = 1 <<0 , // allocates; never returns null
55
62
MallocLike = 1 <<1 , // allocates; may return null
@@ -694,6 +701,11 @@ ObjectSizeOffsetVisitor::ObjectSizeOffsetVisitor(const DataLayout &DL,
694
701
}
695
702
696
703
SizeOffsetType ObjectSizeOffsetVisitor::compute (Value *V) {
704
+ InstructionsVisited = 0 ;
705
+ return computeImpl (V);
706
+ }
707
+
708
+ SizeOffsetType ObjectSizeOffsetVisitor::computeImpl (Value *V) {
697
709
unsigned InitialIntTyBits = DL.getIndexTypeSizeInBits (V->getType ());
698
710
699
711
// Stripping pointer casts can strip address space casts which can change the
@@ -710,14 +722,15 @@ SizeOffsetType ObjectSizeOffsetVisitor::compute(Value *V) {
710
722
IntTyBits = DL.getIndexTypeSizeInBits (V->getType ());
711
723
Zero = APInt::getZero (IntTyBits);
712
724
725
+ SizeOffsetType SOT = computeValue (V);
726
+
713
727
bool IndexTypeSizeChanged = InitialIntTyBits != IntTyBits;
714
728
if (!IndexTypeSizeChanged && Offset.isZero ())
715
- return computeImpl (V) ;
729
+ return SOT ;
716
730
717
731
// We stripped an address space cast that changed the index type size or we
718
732
// accumulated some constant offset (or both). Readjust the bit width to match
719
733
// the argument index type size and apply the offset, as required.
720
- SizeOffsetType SOT = computeImpl (V);
721
734
if (IndexTypeSizeChanged) {
722
735
if (knownSize (SOT) && !::CheckedZextOrTrunc (SOT.first , InitialIntTyBits))
723
736
SOT.first = APInt ();
@@ -729,13 +742,16 @@ SizeOffsetType ObjectSizeOffsetVisitor::compute(Value *V) {
729
742
SOT.second .getBitWidth () > 1 ? SOT.second + Offset : SOT.second };
730
743
}
731
744
732
- SizeOffsetType ObjectSizeOffsetVisitor::computeImpl (Value *V) {
745
+ SizeOffsetType ObjectSizeOffsetVisitor::computeValue (Value *V) {
733
746
if (Instruction *I = dyn_cast<Instruction>(V)) {
734
747
// If we have already seen this instruction, bail out. Cycles can happen in
735
748
// unreachable code after constant propagation.
736
749
auto P = SeenInsts.try_emplace (I, unknown ());
737
750
if (!P.second )
738
751
return P.first ->second ;
752
+ ++InstructionsVisited;
753
+ if (InstructionsVisited > ObjectSizeOffsetVisitorMaxVisitInstructions)
754
+ return unknown ();
739
755
SizeOffsetType Res = visit (*I);
740
756
// Cache the result for later visits. If we happened to visit this during
741
757
// the above recursion, we would consider it unknown until now.
@@ -830,7 +846,7 @@ ObjectSizeOffsetVisitor::visitExtractValueInst(ExtractValueInst&) {
830
846
SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalAlias (GlobalAlias &GA) {
831
847
if (GA.isInterposable ())
832
848
return unknown ();
833
- return compute (GA.getAliasee ());
849
+ return computeImpl (GA.getAliasee ());
834
850
}
835
851
836
852
SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalVariable (GlobalVariable &GV){
@@ -885,7 +901,7 @@ SizeOffsetType ObjectSizeOffsetVisitor::findLoadSizeOffset(
885
901
continue ;
886
902
case AliasResult::MustAlias:
887
903
if (SI->getValueOperand ()->getType ()->isPointerTy ())
888
- return Known (compute (SI->getValueOperand ()));
904
+ return Known (computeImpl (SI->getValueOperand ()));
889
905
else
890
906
return Unknown (); // No handling of non-pointer values by `compute`.
891
907
default :
@@ -998,15 +1014,15 @@ SizeOffsetType ObjectSizeOffsetVisitor::visitPHINode(PHINode &PN) {
998
1014
return unknown ();
999
1015
auto IncomingValues = PN.incoming_values ();
1000
1016
return std::accumulate (IncomingValues.begin () + 1 , IncomingValues.end (),
1001
- compute (*IncomingValues.begin ()),
1017
+ computeImpl (*IncomingValues.begin ()),
1002
1018
[this ](SizeOffsetType LHS, Value *VRHS) {
1003
- return combineSizeOffset (LHS, compute (VRHS));
1019
+ return combineSizeOffset (LHS, computeImpl (VRHS));
1004
1020
});
1005
1021
}
1006
1022
1007
1023
SizeOffsetType ObjectSizeOffsetVisitor::visitSelectInst (SelectInst &I) {
1008
- return combineSizeOffset (compute (I.getTrueValue ()),
1009
- compute (I.getFalseValue ()));
1024
+ return combineSizeOffset (computeImpl (I.getTrueValue ()),
1025
+ computeImpl (I.getFalseValue ()));
1010
1026
}
1011
1027
1012
1028
SizeOffsetType ObjectSizeOffsetVisitor::visitUndefValue (UndefValue&) {
0 commit comments