@@ -91,14 +91,14 @@ class TrackedRegisters {
91
91
const std::vector<MCPhysReg> Registers;
92
92
std::vector<uint16_t > RegToIndexMapping;
93
93
94
- static size_t getMappingSize (const std::vector <MCPhysReg> & RegsToTrack) {
94
+ static size_t getMappingSize (const ArrayRef <MCPhysReg> RegsToTrack) {
95
95
if (RegsToTrack.empty ())
96
96
return 0 ;
97
97
return 1 + *llvm::max_element (RegsToTrack);
98
98
}
99
99
100
100
public:
101
- TrackedRegisters (const std::vector <MCPhysReg> & RegsToTrack)
101
+ TrackedRegisters (const ArrayRef <MCPhysReg> RegsToTrack)
102
102
: Registers(RegsToTrack),
103
103
RegToIndexMapping (getMappingSize(RegsToTrack), NoIndex) {
104
104
for (unsigned I = 0 ; I < RegsToTrack.size (); ++I)
@@ -234,7 +234,7 @@ struct SrcState {
234
234
235
235
static void printLastInsts (
236
236
raw_ostream &OS,
237
- const std::vector <SmallPtrSet<const MCInst *, 4 >> & LastInstWritingReg) {
237
+ const ArrayRef <SmallPtrSet<const MCInst *, 4 >> LastInstWritingReg) {
238
238
OS << " Insts: " ;
239
239
for (unsigned I = 0 ; I < LastInstWritingReg.size (); ++I) {
240
240
auto &Set = LastInstWritingReg[I];
@@ -295,19 +295,18 @@ void SrcStatePrinter::print(raw_ostream &OS, const SrcState &S) const {
295
295
class SrcSafetyAnalysis {
296
296
public:
297
297
SrcSafetyAnalysis (BinaryFunction &BF,
298
- const std::vector <MCPhysReg> & RegsToTrackInstsFor)
298
+ const ArrayRef <MCPhysReg> RegsToTrackInstsFor)
299
299
: BC(BF.getBinaryContext()), NumRegs(BC.MRI->getNumRegs ()),
300
300
RegsToTrackInstsFor(RegsToTrackInstsFor) {}
301
301
302
302
virtual ~SrcSafetyAnalysis () {}
303
303
304
304
static std::shared_ptr<SrcSafetyAnalysis>
305
305
create (BinaryFunction &BF, MCPlusBuilder::AllocatorIdTy AllocId,
306
- const std::vector <MCPhysReg> & RegsToTrackInstsFor);
306
+ const ArrayRef <MCPhysReg> RegsToTrackInstsFor);
307
307
308
308
virtual void run () = 0;
309
- virtual ErrorOr<const SrcState &>
310
- getStateBefore (const MCInst &Inst) const = 0 ;
309
+ virtual const SrcState &getStateBefore (const MCInst &Inst) const = 0;
311
310
312
311
protected:
313
312
BinaryContext &BC;
@@ -348,7 +347,7 @@ class SrcSafetyAnalysis {
348
347
}
349
348
350
349
BitVector getClobberedRegs (const MCInst &Point ) const {
351
- BitVector Clobbered (NumRegs, false );
350
+ BitVector Clobbered (NumRegs);
352
351
// Assume a call can clobber all registers, including callee-saved
353
352
// registers. There's a good chance that callee-saved registers will be
354
353
// saved on the stack at some point during execution of the callee.
@@ -409,8 +408,7 @@ class SrcSafetyAnalysis {
409
408
410
409
// FirstCheckerInst should belong to the same basic block, meaning
411
410
// it was deterministically processed a few steps before this instruction.
412
- const SrcState &StateBeforeChecker =
413
- getStateBefore (*FirstCheckerInst).get ();
411
+ const SrcState &StateBeforeChecker = getStateBefore (*FirstCheckerInst);
414
412
if (StateBeforeChecker.SafeToDerefRegs [CheckedReg])
415
413
Regs.push_back (CheckedReg);
416
414
}
@@ -523,10 +521,7 @@ class SrcSafetyAnalysis {
523
521
const ArrayRef<MCPhysReg> UsedDirtyRegs) const {
524
522
if (RegsToTrackInstsFor.empty ())
525
523
return {};
526
- auto MaybeState = getStateBefore (Inst);
527
- if (!MaybeState)
528
- llvm_unreachable (" Expected state to be present" );
529
- const SrcState &S = *MaybeState;
524
+ const SrcState &S = getStateBefore (Inst);
530
525
// Due to aliasing registers, multiple registers may have been tracked.
531
526
std::set<const MCInst *> LastWritingInsts;
532
527
for (MCPhysReg TrackedReg : UsedDirtyRegs) {
@@ -537,7 +532,7 @@ class SrcSafetyAnalysis {
537
532
for (const MCInst *Inst : LastWritingInsts) {
538
533
MCInstReference Ref = MCInstReference::get (Inst, BF);
539
534
assert (Ref && " Expected Inst to be found" );
540
- Result.push_back (MCInstReference ( Ref) );
535
+ Result.push_back (Ref);
541
536
}
542
537
return Result;
543
538
}
@@ -557,11 +552,11 @@ class DataflowSrcSafetyAnalysis
557
552
public:
558
553
DataflowSrcSafetyAnalysis (BinaryFunction &BF,
559
554
MCPlusBuilder::AllocatorIdTy AllocId,
560
- const std::vector <MCPhysReg> & RegsToTrackInstsFor)
555
+ const ArrayRef <MCPhysReg> RegsToTrackInstsFor)
561
556
: SrcSafetyAnalysis(BF, RegsToTrackInstsFor), DFParent(BF, AllocId) {}
562
557
563
- ErrorOr< const SrcState &> getStateBefore (const MCInst &Inst) const override {
564
- return DFParent::getStateBefore (Inst);
558
+ const SrcState &getStateBefore (const MCInst &Inst) const override {
559
+ return DFParent::getStateBefore (Inst). get () ;
565
560
}
566
561
567
562
void run () override {
@@ -670,7 +665,7 @@ class CFGUnawareSrcSafetyAnalysis : public SrcSafetyAnalysis {
670
665
public:
671
666
CFGUnawareSrcSafetyAnalysis (BinaryFunction &BF,
672
667
MCPlusBuilder::AllocatorIdTy AllocId,
673
- const std::vector <MCPhysReg> & RegsToTrackInstsFor)
668
+ const ArrayRef <MCPhysReg> RegsToTrackInstsFor)
674
669
: SrcSafetyAnalysis(BF, RegsToTrackInstsFor), BF(BF), AllocId(AllocId) {
675
670
StateAnnotationIndex =
676
671
BC.MIB ->getOrCreateAnnotationIndex (" CFGUnawareSrcSafetyAnalysis" );
@@ -704,7 +699,7 @@ class CFGUnawareSrcSafetyAnalysis : public SrcSafetyAnalysis {
704
699
}
705
700
}
706
701
707
- ErrorOr< const SrcState &> getStateBefore (const MCInst &Inst) const override {
702
+ const SrcState &getStateBefore (const MCInst &Inst) const override {
708
703
return BC.MIB ->getAnnotationAs <SrcState>(Inst, StateAnnotationIndex);
709
704
}
710
705
@@ -714,7 +709,7 @@ class CFGUnawareSrcSafetyAnalysis : public SrcSafetyAnalysis {
714
709
std::shared_ptr<SrcSafetyAnalysis>
715
710
SrcSafetyAnalysis::create (BinaryFunction &BF,
716
711
MCPlusBuilder::AllocatorIdTy AllocId,
717
- const std::vector <MCPhysReg> & RegsToTrackInstsFor) {
712
+ const ArrayRef <MCPhysReg> RegsToTrackInstsFor) {
718
713
if (BF.hasCFG ())
719
714
return std::make_shared<DataflowSrcSafetyAnalysis>(BF, AllocId,
720
715
RegsToTrackInstsFor);
@@ -821,7 +816,7 @@ Analysis::findGadgets(BinaryFunction &BF,
821
816
822
817
BinaryContext &BC = BF.getBinaryContext ();
823
818
iterateOverInstrs (BF, [&](MCInstReference Inst) {
824
- const SrcState &S = * Analysis->getStateBefore (Inst);
819
+ const SrcState &S = Analysis->getStateBefore (Inst);
825
820
826
821
// If non-empty state was never propagated from the entry basic block
827
822
// to Inst, assume it to be unreachable and report a warning.
0 commit comments