10
10
//
11
11
// ===----------------------------------------------------------------------===//
12
12
13
+ #include " llvm/ADT/DenseSet.h"
13
14
#include " llvm/ADT/Statistic.h"
15
+ #include " llvm/CodeGen/LiveIntervals.h"
14
16
#include " llvm/CodeGen/MachineFunctionPass.h"
15
17
#include " llvm/CodeGen/MachineRegisterInfo.h"
16
18
#include " llvm/CodeGen/Passes.h"
@@ -32,6 +34,7 @@ namespace {
32
34
const TargetRegisterInfo *TRI;
33
35
const MachineRegisterInfo *MRI;
34
36
const TargetInstrInfo *TII;
37
+ LiveIntervals *LIS;
35
38
BitVector LivePhysRegs;
36
39
37
40
public:
@@ -41,7 +44,7 @@ namespace {
41
44
}
42
45
43
46
void getAnalysisUsage (AnalysisUsage &AU) const override {
44
- AU.setPreservesCFG ();
47
+ AU.setPreservesAll ();
45
48
MachineFunctionPass::getAnalysisUsage (AU);
46
49
}
47
50
@@ -78,9 +81,15 @@ bool DeadMachineInstructionElim::isDead(const MachineInstr *MI) const {
78
81
unsigned Reg = MO.getReg ();
79
82
if (TargetRegisterInfo::isPhysicalRegister (Reg)) {
80
83
// Don't delete live physreg defs, or any reserved register defs.
81
- if (LivePhysRegs.test (Reg) || MRI->isReserved (Reg))
84
+ // Do not remove physreg defs if we have LIS as we may be unable
85
+ // to accurately recompute its liveness.
86
+ if (LivePhysRegs.test (Reg) || MRI->isReserved (Reg) || LIS)
82
87
return false ;
83
88
} else {
89
+ // An instruction can also use its def in case if it is a tied operand.
90
+ // TODO: Technically we can also remove it if def dominates the use.
91
+ // This can happen when two instructions define different subregs
92
+ // of the same register.
84
93
for (const MachineInstr &Use : MRI->use_nodbg_instructions (Reg)) {
85
94
if (&Use != MI)
86
95
// This def has a non-debug use. Don't delete the instruction!
@@ -102,6 +111,8 @@ bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
102
111
MRI = &MF.getRegInfo ();
103
112
TRI = MF.getSubtarget ().getRegisterInfo ();
104
113
TII = MF.getSubtarget ().getInstrInfo ();
114
+ LIS = getAnalysisIfAvailable<LiveIntervals>();
115
+ DenseSet<unsigned > RecalcRegs;
105
116
106
117
// Loop over all instructions in all blocks, from bottom to top, so that it's
107
118
// more likely that chains of dependent but ultimately dead instructions will
@@ -127,6 +138,14 @@ bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
127
138
// If the instruction is dead, delete it!
128
139
if (isDead (MI)) {
129
140
LLVM_DEBUG (dbgs () << " DeadMachineInstructionElim: DELETING: " << *MI);
141
+ if (LIS) {
142
+ for (const MachineOperand &MO : MI->operands ()) {
143
+ if (MO.isReg () && TRI->isVirtualRegister (MO.getReg ()))
144
+ RecalcRegs.insert (MO.getReg ());
145
+ }
146
+ LIS->RemoveMachineInstrFromMaps (*MI);
147
+ }
148
+
130
149
// It is possible that some DBG_VALUE instructions refer to this
131
150
// instruction. They get marked as undef and will be deleted
132
151
// in the live debug variable analysis.
@@ -170,5 +189,12 @@ bool DeadMachineInstructionElim::runOnMachineFunction(MachineFunction &MF) {
170
189
}
171
190
172
191
LivePhysRegs.clear ();
192
+
193
+ for (auto Reg : RecalcRegs) {
194
+ LIS->removeInterval (Reg);
195
+ if (!MRI->reg_empty (Reg))
196
+ LIS->createAndComputeVirtRegInterval (Reg);
197
+ }
198
+
173
199
return AnyChanges;
174
200
}
0 commit comments