@@ -99,33 +99,6 @@ class CopyTracker {
99
99
}
100
100
}
101
101
102
- // / Remove any entry in the tracker's copy maps that is marked clobbered in \p
103
- // / RegMask. The map will typically have a lot fewer entries than the regmask
104
- // / clobbers, so this is more efficient than iterating the clobbered registers
105
- // / and calling ClobberRegister() on them.
106
- void removeClobberedRegs (const MachineOperand &RegMask,
107
- const TargetRegisterInfo &TRI) {
108
- auto RemoveFromMap = [&RegMask](Reg2MIMap &Map) {
109
- for (Reg2MIMap::iterator I = Map.begin (), E = Map.end (), Next; I != E;
110
- I = Next) {
111
- Next = std::next (I);
112
- if (RegMask.clobbersPhysReg (I->first ))
113
- Map.erase (I);
114
- }
115
- };
116
- RemoveFromMap (AvailCopyMap);
117
- RemoveFromMap (CopyMap);
118
-
119
- for (SourceMap::iterator I = SrcMap.begin (), E = SrcMap.end (), Next; I != E;
120
- I = Next) {
121
- Next = std::next (I);
122
- if (RegMask.clobbersPhysReg (I->first )) {
123
- markRegsUnavailable (I->second , TRI);
124
- SrcMap.erase (I);
125
- }
126
- }
127
- }
128
-
129
102
// / Clobber a single register, removing it from the tracker's copy maps.
130
103
void clobberRegister (unsigned Reg, const TargetRegisterInfo &TRI) {
131
104
for (MCRegAliasIterator AI (Reg, &TRI, true ); AI.isValid (); ++AI) {
@@ -163,11 +136,24 @@ class CopyTracker {
163
136
164
137
bool hasAvailableCopies () { return !AvailCopyMap.empty (); }
165
138
166
- MachineInstr *findAvailCopy (unsigned Reg) {
139
+ MachineInstr *findAvailCopy (MachineInstr &DestCopy, unsigned Reg) {
167
140
auto CI = AvailCopyMap.find (Reg);
168
- if (CI != AvailCopyMap.end ())
169
- return CI->second ;
170
- return nullptr ;
141
+ if (CI == AvailCopyMap.end ())
142
+ return nullptr ;
143
+ MachineInstr &AvailCopy = *CI->second ;
144
+
145
+ // Check that the available copy isn't clobbered by any regmasks between
146
+ // itself and the destination.
147
+ unsigned AvailSrc = AvailCopy.getOperand (1 ).getReg ();
148
+ unsigned AvailDef = AvailCopy.getOperand (0 ).getReg ();
149
+ for (const MachineInstr &MI :
150
+ make_range (AvailCopy.getIterator (), DestCopy.getIterator ()))
151
+ for (const MachineOperand &MO : MI.operands ())
152
+ if (MO.isRegMask ())
153
+ if (MO.clobbersPhysReg (AvailSrc) || MO.clobbersPhysReg (AvailDef))
154
+ return nullptr ;
155
+
156
+ return &AvailCopy;
171
157
}
172
158
173
159
MachineInstr *findCopy (unsigned Reg) {
@@ -277,7 +263,7 @@ bool MachineCopyPropagation::eraseIfRedundant(MachineInstr &Copy, unsigned Src,
277
263
return false ;
278
264
279
265
// Search for an existing copy.
280
- MachineInstr *PrevCopy = Tracker.findAvailCopy (Def);
266
+ MachineInstr *PrevCopy = Tracker.findAvailCopy (Copy, Def);
281
267
if (!PrevCopy)
282
268
return false ;
283
269
@@ -398,7 +384,7 @@ void MachineCopyPropagation::forwardUses(MachineInstr &MI) {
398
384
if (!MOUse.isRenamable ())
399
385
continue ;
400
386
401
- MachineInstr *Copy = Tracker.findAvailCopy (MOUse.getReg ());
387
+ MachineInstr *Copy = Tracker.findAvailCopy (MI, MOUse.getReg ());
402
388
if (!Copy)
403
389
continue ;
404
390
@@ -586,15 +572,17 @@ void MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
586
572
LLVM_DEBUG (dbgs () << " MCP: Removing copy due to regmask clobbering: " ;
587
573
MaybeDead->dump ());
588
574
575
+ // Make sure we invalidate any entries in the copy maps before erasing
576
+ // the instruction.
577
+ Tracker.clobberRegister (Reg, *TRI);
578
+
589
579
// erase() will return the next valid iterator pointing to the next
590
580
// element after the erased one.
591
581
DI = MaybeDeadCopies.erase (DI);
592
582
MaybeDead->eraseFromParent ();
593
583
Changed = true ;
594
584
++NumDeletes;
595
585
}
596
-
597
- Tracker.removeClobberedRegs (*RegMask, *TRI);
598
586
}
599
587
600
588
// Any previous copy definition or reading the Defs is no longer available.
0 commit comments