@@ -242,7 +242,7 @@ static void printInstsShort(raw_ostream &OS,
242
242
}
243
243
}
244
244
245
- raw_ostream &operator <<(raw_ostream &OS, const SrcState &S) {
245
+ static raw_ostream &operator <<(raw_ostream &OS, const SrcState &S) {
246
246
OS << " src-state<" ;
247
247
if (S.empty ()) {
248
248
OS << " empty" ;
@@ -432,8 +432,7 @@ class SrcSafetyAnalysis {
432
432
SrcStatePrinter P (BC);
433
433
LLVM_DEBUG ({
434
434
dbgs () << " SrcSafetyAnalysis::ComputeNext(" ;
435
- BC.InstPrinter ->printInst (&const_cast <MCInst &>(Point ), 0 , " " , *BC.STI ,
436
- dbgs ());
435
+ BC.InstPrinter ->printInst (&Point , 0 , " " , *BC.STI , dbgs ());
437
436
dbgs () << " , " ;
438
437
P.print (dbgs (), Cur);
439
438
dbgs () << " )\n " ;
@@ -611,6 +610,42 @@ class DataflowSrcSafetyAnalysis
611
610
StringRef getAnnotationName () const { return " DataflowSrcSafetyAnalysis" ; }
612
611
};
613
612
613
+ // / A helper base class for implementing a simplified counterpart of a dataflow
614
+ // / analysis for functions without CFG information.
615
+ template <typename StateTy> class CFGUnawareAnalysis {
616
+ BinaryContext &BC;
617
+ BinaryFunction &BF;
618
+ MCPlusBuilder::AllocatorIdTy AllocId;
619
+ unsigned StateAnnotationIndex;
620
+
621
+ void cleanStateAnnotations () {
622
+ for (auto &I : BF.instrs ())
623
+ BC.MIB ->removeAnnotation (I.second , StateAnnotationIndex);
624
+ }
625
+
626
+ protected:
627
+ CFGUnawareAnalysis (BinaryFunction &BF, MCPlusBuilder::AllocatorIdTy AllocId,
628
+ StringRef AnnotationName)
629
+ : BC(BF.getBinaryContext()), BF(BF), AllocId(AllocId) {
630
+ StateAnnotationIndex = BC.MIB ->getOrCreateAnnotationIndex (AnnotationName);
631
+ }
632
+
633
+ void setState (MCInst &Inst, const StateTy &S) {
634
+ // Check if we need to remove an old annotation (this is the case if
635
+ // this is the second, detailed run of the analysis).
636
+ if (BC.MIB ->hasAnnotation (Inst, StateAnnotationIndex))
637
+ BC.MIB ->removeAnnotation (Inst, StateAnnotationIndex);
638
+ // Attach the state.
639
+ BC.MIB ->addAnnotation (Inst, StateAnnotationIndex, S, AllocId);
640
+ }
641
+
642
+ const StateTy &getState (const MCInst &Inst) const {
643
+ return BC.MIB ->getAnnotationAs <StateTy>(Inst, StateAnnotationIndex);
644
+ }
645
+
646
+ ~CFGUnawareAnalysis () { cleanStateAnnotations (); }
647
+ };
648
+
614
649
// A simplified implementation of DataflowSrcSafetyAnalysis for functions
615
650
// lacking CFG information.
616
651
//
@@ -645,15 +680,10 @@ class DataflowSrcSafetyAnalysis
645
680
// of instructions without labels in between. These sequences can be processed
646
681
// the same way basic blocks are processed by data-flow analysis, assuming
647
682
// pessimistically that all registers are unsafe at the start of each sequence.
648
- class CFGUnawareSrcSafetyAnalysis : public SrcSafetyAnalysis {
683
+ class CFGUnawareSrcSafetyAnalysis : public SrcSafetyAnalysis ,
684
+ public CFGUnawareAnalysis<SrcState> {
685
+ using SrcSafetyAnalysis::BC;
649
686
BinaryFunction &BF;
650
- MCPlusBuilder::AllocatorIdTy AllocId;
651
- unsigned StateAnnotationIndex;
652
-
653
- void cleanStateAnnotations () {
654
- for (auto &I : BF.instrs ())
655
- BC.MIB ->removeAnnotation (I.second , StateAnnotationIndex);
656
- }
657
687
658
688
// / Creates a state with all registers marked unsafe (not to be confused
659
689
// / with empty state).
@@ -665,9 +695,8 @@ class CFGUnawareSrcSafetyAnalysis : public SrcSafetyAnalysis {
665
695
CFGUnawareSrcSafetyAnalysis (BinaryFunction &BF,
666
696
MCPlusBuilder::AllocatorIdTy AllocId,
667
697
ArrayRef<MCPhysReg> RegsToTrackInstsFor)
668
- : SrcSafetyAnalysis(BF, RegsToTrackInstsFor), BF(BF), AllocId(AllocId) {
669
- StateAnnotationIndex =
670
- BC.MIB ->getOrCreateAnnotationIndex (" CFGUnawareSrcSafetyAnalysis" );
698
+ : SrcSafetyAnalysis(BF, RegsToTrackInstsFor),
699
+ CFGUnawareAnalysis (BF, AllocId, " CFGUnawareSrcSafetyAnalysis" ), BF(BF) {
671
700
}
672
701
673
702
void run () override {
@@ -686,23 +715,17 @@ class CFGUnawareSrcSafetyAnalysis : public SrcSafetyAnalysis {
686
715
S = createUnsafeState ();
687
716
}
688
717
689
- // Check if we need to remove an old annotation (this is the case if
690
- // this is the second, detailed, run of the analysis).
691
- if (BC.MIB ->hasAnnotation (Inst, StateAnnotationIndex))
692
- BC.MIB ->removeAnnotation (Inst, StateAnnotationIndex);
693
718
// Attach the state *before* this instruction executes.
694
- BC. MIB -> addAnnotation (Inst, StateAnnotationIndex, S, AllocId );
719
+ setState (Inst, S );
695
720
696
721
// Compute the state after this instruction executes.
697
722
S = computeNext (Inst, S);
698
723
}
699
724
}
700
725
701
726
const SrcState &getStateBefore (const MCInst &Inst) const override {
702
- return BC. MIB -> getAnnotationAs <SrcState> (Inst, StateAnnotationIndex );
727
+ return getState (Inst);
703
728
}
704
-
705
- ~CFGUnawareSrcSafetyAnalysis () { cleanStateAnnotations (); }
706
729
};
707
730
708
731
std::shared_ptr<SrcSafetyAnalysis>
@@ -786,7 +809,7 @@ struct DstState {
786
809
bool operator !=(const DstState &RHS) const { return !((*this ) == RHS); }
787
810
};
788
811
789
- raw_ostream &operator <<(raw_ostream &OS, const DstState &S) {
812
+ static raw_ostream &operator <<(raw_ostream &OS, const DstState &S) {
790
813
OS << " dst-state<" ;
791
814
if (S.empty ()) {
792
815
OS << " empty" ;
@@ -962,8 +985,7 @@ class DstSafetyAnalysis {
962
985
DstStatePrinter P (BC);
963
986
LLVM_DEBUG ({
964
987
dbgs () << " DstSafetyAnalysis::ComputeNext(" ;
965
- BC.InstPrinter ->printInst (&const_cast <MCInst &>(Point ), 0 , " " , *BC.STI ,
966
- dbgs ());
988
+ BC.InstPrinter ->printInst (&Point , 0 , " " , *BC.STI , dbgs ());
967
989
dbgs () << " , " ;
968
990
P.print (dbgs (), Cur);
969
991
dbgs () << " )\n " ;
@@ -1108,15 +1130,10 @@ class DataflowDstSafetyAnalysis
1108
1130
StringRef getAnnotationName () const { return " DataflowDstSafetyAnalysis" ; }
1109
1131
};
1110
1132
1111
- class CFGUnawareDstSafetyAnalysis : public DstSafetyAnalysis {
1133
+ class CFGUnawareDstSafetyAnalysis : public DstSafetyAnalysis ,
1134
+ public CFGUnawareAnalysis<DstState> {
1135
+ using DstSafetyAnalysis::BC;
1112
1136
BinaryFunction &BF;
1113
- MCPlusBuilder::AllocatorIdTy AllocId;
1114
- unsigned StateAnnotationIndex;
1115
-
1116
- void cleanStateAnnotations () {
1117
- for (auto &I : BF.instrs ())
1118
- BC.MIB ->removeAnnotation (I.second , StateAnnotationIndex);
1119
- }
1120
1137
1121
1138
DstState createUnsafeState () const {
1122
1139
return DstState (NumRegs, RegsToTrackInstsFor.getNumTrackedRegisters ());
@@ -1126,9 +1143,8 @@ class CFGUnawareDstSafetyAnalysis : public DstSafetyAnalysis {
1126
1143
CFGUnawareDstSafetyAnalysis (BinaryFunction &BF,
1127
1144
MCPlusBuilder::AllocatorIdTy AllocId,
1128
1145
ArrayRef<MCPhysReg> RegsToTrackInstsFor)
1129
- : DstSafetyAnalysis(BF, RegsToTrackInstsFor), BF(BF), AllocId(AllocId) {
1130
- StateAnnotationIndex =
1131
- BC.MIB ->getOrCreateAnnotationIndex (" CFGUnawareDstSafetyAnalysis" );
1146
+ : DstSafetyAnalysis(BF, RegsToTrackInstsFor),
1147
+ CFGUnawareAnalysis (BF, AllocId, " CFGUnawareDstSafetyAnalysis" ), BF(BF) {
1132
1148
}
1133
1149
1134
1150
void run () override {
@@ -1146,23 +1162,17 @@ class CFGUnawareDstSafetyAnalysis : public DstSafetyAnalysis {
1146
1162
S = createUnsafeState ();
1147
1163
}
1148
1164
1149
- // Check if we need to remove an old annotation (this is the case if
1150
- // this is the second, detailed, run of the analysis).
1151
- if (BC.MIB ->hasAnnotation (Inst, StateAnnotationIndex))
1152
- BC.MIB ->removeAnnotation (Inst, StateAnnotationIndex);
1153
1165
// Attach the state *after* this instruction executes.
1154
- BC. MIB -> addAnnotation (Inst, StateAnnotationIndex, S, AllocId );
1166
+ setState (Inst, S );
1155
1167
1156
1168
// Compute the next state.
1157
1169
S = computeNext (Inst, S);
1158
1170
}
1159
1171
}
1160
1172
1161
1173
const DstState &getStateAfter (const MCInst &Inst) const override {
1162
- return BC. MIB -> getAnnotationAs <DstState> (Inst, StateAnnotationIndex );
1174
+ return getState (Inst);
1163
1175
}
1164
-
1165
- ~CFGUnawareDstSafetyAnalysis () { cleanStateAnnotations (); }
1166
1176
};
1167
1177
1168
1178
std::shared_ptr<DstSafetyAnalysis>
0 commit comments