@@ -49,26 +49,31 @@ STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address");
49
49
STATISTIC (Num3AddrSunk, " Number of 3-address instructions sunk" );
50
50
51
51
namespace {
52
- struct VISIBILITY_HIDDEN TwoAddressInstructionPass
53
- : public MachineFunctionPass {
52
+ class VISIBILITY_HIDDEN TwoAddressInstructionPass
53
+ : public MachineFunctionPass {
54
54
const TargetInstrInfo *TII;
55
55
const TargetRegisterInfo *TRI;
56
56
MachineRegisterInfo *MRI;
57
57
LiveVariables *LV;
58
58
59
+ bool Sink3AddrInstruction (MachineBasicBlock *MBB, MachineInstr *MI,
60
+ unsigned Reg,
61
+ MachineBasicBlock::iterator OldPos);
59
62
public:
60
63
static char ID; // Pass identification, replacement for typeid
61
64
TwoAddressInstructionPass () : MachineFunctionPass((intptr_t )&ID) {}
62
65
63
- virtual void getAnalysisUsage (AnalysisUsage &AU) const ;
66
+ virtual void getAnalysisUsage (AnalysisUsage &AU) const {
67
+ AU.addRequired <LiveVariables>();
68
+ AU.addPreserved <LiveVariables>();
69
+ AU.addPreservedID (MachineLoopInfoID);
70
+ AU.addPreservedID (MachineDominatorsID);
71
+ AU.addPreservedID (PHIEliminationID);
72
+ MachineFunctionPass::getAnalysisUsage (AU);
73
+ }
64
74
65
- // / runOnMachineFunction - pass entry point
75
+ // / runOnMachineFunction - Pass entry point.
66
76
bool runOnMachineFunction (MachineFunction&);
67
-
68
- private:
69
- bool Sink3AddrInstruction (MachineBasicBlock *MBB, MachineInstr *MI,
70
- unsigned Reg,
71
- MachineBasicBlock::iterator OldPos);
72
77
};
73
78
74
79
char TwoAddressInstructionPass::ID = 0 ;
@@ -78,19 +83,11 @@ namespace {
78
83
79
84
const PassInfo *llvm::TwoAddressInstructionPassID = X.getPassInfo();
80
85
81
- void TwoAddressInstructionPass::getAnalysisUsage (AnalysisUsage &AU) const {
82
- AU.addRequired <LiveVariables>();
83
- AU.addPreserved <LiveVariables>();
84
- AU.addPreservedID (MachineLoopInfoID);
85
- AU.addPreservedID (MachineDominatorsID);
86
- AU.addPreservedID (PHIEliminationID);
87
- MachineFunctionPass::getAnalysisUsage (AU);
88
- }
89
-
90
86
// / Sink3AddrInstruction - A two-address instruction has been converted to a
91
87
// / three-address instruction to avoid clobbering a register. Try to sink it
92
- // / past the instruction that would kill the above mentioned register to
93
- // / reduce register pressure.
88
+ // / past the instruction that would kill the above mentioned register to reduce
89
+ // / register pressure.
90
+ // /
94
91
bool TwoAddressInstructionPass::Sink3AddrInstruction (MachineBasicBlock *MBB,
95
92
MachineInstr *MI, unsigned SavedReg,
96
93
MachineBasicBlock::iterator OldPos) {
@@ -101,6 +98,7 @@ bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
101
98
102
99
unsigned DefReg = 0 ;
103
100
SmallSet<unsigned , 4 > UseRegs;
101
+
104
102
for (unsigned i = 0 , e = MI->getNumOperands (); i != e; ++i) {
105
103
const MachineOperand &MO = MI->getOperand (i);
106
104
if (!MO.isRegister ())
@@ -123,6 +121,7 @@ bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
123
121
124
122
// Find the instruction that kills SavedReg.
125
123
MachineInstr *KillMI = NULL ;
124
+
126
125
for (MachineRegisterInfo::use_iterator UI = MRI->use_begin (SavedReg),
127
126
UE = MRI->use_end (); UI != UE; ++UI) {
128
127
MachineOperand &UseMO = UI.getOperand ();
@@ -131,19 +130,23 @@ bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
131
130
KillMI = UseMO.getParent ();
132
131
break ;
133
132
}
133
+
134
134
if (!KillMI || KillMI->getParent () != MBB)
135
135
return false ;
136
136
137
- // If any of the definitions are used by another instruction between
138
- // the position and the kill use, then it's not safe to sink it.
139
- // FIXME: This can be sped up if there is an easy way to query whether
140
- // an instruction if before or after another instruction. Then we can
141
- // use MachineRegisterInfo def / use instead.
137
+ // If any of the definitions are used by another instruction between the
138
+ // position and the kill use, then it's not safe to sink it.
139
+ //
140
+ // FIXME: This can be sped up if there is an easy way to query whether an
141
+ // instruction if before or after another instruction. Then we can use
142
+ // MachineRegisterInfo def / use instead.
142
143
MachineOperand *KillMO = NULL ;
143
144
MachineBasicBlock::iterator KillPos = KillMI;
144
145
++KillPos;
146
+
145
147
for (MachineBasicBlock::iterator I = next (OldPos); I != KillPos; ++I) {
146
148
MachineInstr *OtherMI = I;
149
+
147
150
for (unsigned i = 0 , e = OtherMI->getNumOperands (); i != e; ++i) {
148
151
MachineOperand &MO = OtherMI->getOperand (i);
149
152
if (!MO.isRegister ())
@@ -153,6 +156,7 @@ bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
153
156
continue ;
154
157
if (DefReg == MOReg)
155
158
return false ;
159
+
156
160
if (MO.isKill ()) {
157
161
if (OtherMI == KillMI && MOReg == SavedReg)
158
162
// Save the operand that kills the register. We want unset the kill
@@ -181,8 +185,7 @@ bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
181
185
return true ;
182
186
}
183
187
184
- // / runOnMachineFunction - Reduce two-address instructions to two
185
- // / operands.
188
+ // / runOnMachineFunction - Reduce two-address instructions to two operands.
186
189
// /
187
190
bool TwoAddressInstructionPass::runOnMachineFunction (MachineFunction &MF) {
188
191
DOUT << " Machine Function\n " ;
@@ -203,8 +206,8 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
203
206
mi != me; ) {
204
207
MachineBasicBlock::iterator nmi = next (mi);
205
208
const TargetInstrDesc &TID = mi->getDesc ();
206
-
207
209
bool FirstTied = true ;
210
+
208
211
for (unsigned si = 1 , e = TID.getNumOperands (); si < e; ++si) {
209
212
int ti = TID.getOperandConstraint (si, TOI::TIED_TO);
210
213
if (ti == -1 )
@@ -214,15 +217,16 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
214
217
++NumTwoAddressInstrs;
215
218
DOUT << ' \t ' ; DEBUG (mi->print (*cerr.stream (), &TM));
216
219
}
220
+
217
221
FirstTied = false ;
218
222
219
223
assert (mi->getOperand (si).isRegister () && mi->getOperand (si).getReg () &&
220
224
mi->getOperand (si).isUse () && " two address instruction invalid" );
221
225
222
- // if the two operands are the same we just remove the use
226
+ // If the two operands are the same we just remove the use
223
227
// and mark the def as def&use, otherwise we have to insert a copy.
224
228
if (mi->getOperand (ti).getReg () != mi->getOperand (si).getReg ()) {
225
- // rewrite :
229
+ // Rewrite :
226
230
// a = b op c
227
231
// to:
228
232
// a = b
@@ -257,9 +261,11 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
257
261
assert (mi->getOperand (3 -si).isRegister () &&
258
262
" Not a proper commutative instruction!" );
259
263
unsigned regC = mi->getOperand (3 -si).getReg ();
264
+
260
265
if (mi->killsRegister (regC)) {
261
266
DOUT << " 2addr: COMMUTING : " << *mi;
262
267
MachineInstr *NewMI = TII->commuteInstruction (mi);
268
+
263
269
if (NewMI == 0 ) {
264
270
DOUT << " 2addr: COMMUTING FAILED!\n " ;
265
271
} else {
@@ -285,27 +291,30 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
285
291
// FIXME: This assumes there are no more operands which are tied
286
292
// to another register.
287
293
#ifndef NDEBUG
288
- for (unsigned i = si+ 1 , e = TID.getNumOperands (); i < e; ++i)
294
+ for (unsigned i = si + 1 , e = TID.getNumOperands (); i < e; ++i)
289
295
assert (TID.getOperandConstraint (i, TOI::TIED_TO) == -1 );
290
296
#endif
291
297
292
298
if (MachineInstr *New=TII->convertToThreeAddress (mbbi, mi, *LV)) {
293
299
DOUT << " 2addr: CONVERTING 2-ADDR: " << *mi;
294
300
DOUT << " 2addr: TO 3-ADDR: " << *New;
295
301
bool Sunk = false ;
302
+
296
303
if (New->findRegisterUseOperand (regB, false , TRI))
297
304
// FIXME: Temporary workaround. If the new instruction doesn't
298
305
// uses regB, convertToThreeAddress must have created more
299
306
// then one instruction.
300
307
Sunk = Sink3AddrInstruction (mbbi, New, regB, mi);
301
- mbbi->erase (mi); // Nuke the old inst.
308
+
309
+ mbbi->erase (mi); // Nuke the old inst.
310
+
302
311
if (!Sunk) {
303
312
mi = New;
304
313
nmi = next (mi);
305
314
}
315
+
306
316
++NumConvertedTo3Addr;
307
- // Done with this instruction.
308
- break ;
317
+ break ; // Done with this instruction.
309
318
}
310
319
}
311
320
}
@@ -317,17 +326,19 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
317
326
MachineBasicBlock::iterator prevMi = prior (mi);
318
327
DOUT << " \t\t prepend:\t " ; DEBUG (prevMi->print (*cerr.stream (), &TM));
319
328
320
- // update live variables for regB
329
+ // Update live variables for regB.
321
330
LiveVariables::VarInfo& varInfoB = LV->getVarInfo (regB);
331
+
322
332
// regB is used in this BB.
323
333
varInfoB.UsedBlocks [mbbi->getNumber ()] = true ;
334
+
324
335
if (LV->removeVirtualRegisterKilled (regB, mbbi, mi))
325
336
LV->addVirtualRegisterKilled (regB, prevMi);
326
337
327
338
if (LV->removeVirtualRegisterDead (regB, mbbi, mi))
328
339
LV->addVirtualRegisterDead (regB, prevMi);
329
340
330
- // replace all occurences of regB with regA
341
+ // Replace all occurences of regB with regA.
331
342
for (unsigned i = 0 , e = mi->getNumOperands (); i != e; ++i) {
332
343
if (mi->getOperand (i).isRegister () &&
333
344
mi->getOperand (i).getReg () == regB)
@@ -341,6 +352,7 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
341
352
342
353
DOUT << " \t\t rewrite to:\t " ; DEBUG (mi->print (*cerr.stream (), &TM));
343
354
}
355
+
344
356
mi = nmi;
345
357
}
346
358
}
0 commit comments