Skip to content

Commit 19e3c85

Browse files
committed
Cosmetic changes:
- Comment fixes. - Moar whitespace. - Made ivars "private" by default. No functionality change. llvm-svn: 50926
1 parent 14196c0 commit 19e3c85

File tree

1 file changed

+48
-36
lines changed

1 file changed

+48
-36
lines changed

llvm/lib/CodeGen/TwoAddressInstructionPass.cpp

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,31 @@ STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address");
4949
STATISTIC(Num3AddrSunk, "Number of 3-address instructions sunk");
5050

5151
namespace {
52-
struct VISIBILITY_HIDDEN TwoAddressInstructionPass
53-
: public MachineFunctionPass {
52+
class VISIBILITY_HIDDEN TwoAddressInstructionPass
53+
: public MachineFunctionPass {
5454
const TargetInstrInfo *TII;
5555
const TargetRegisterInfo *TRI;
5656
MachineRegisterInfo *MRI;
5757
LiveVariables *LV;
5858

59+
bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI,
60+
unsigned Reg,
61+
MachineBasicBlock::iterator OldPos);
5962
public:
6063
static char ID; // Pass identification, replacement for typeid
6164
TwoAddressInstructionPass() : MachineFunctionPass((intptr_t)&ID) {}
6265

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+
}
6474

65-
/// runOnMachineFunction - pass entry point
75+
/// runOnMachineFunction - Pass entry point.
6676
bool runOnMachineFunction(MachineFunction&);
67-
68-
private:
69-
bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI,
70-
unsigned Reg,
71-
MachineBasicBlock::iterator OldPos);
7277
};
7378

7479
char TwoAddressInstructionPass::ID = 0;
@@ -78,19 +83,11 @@ namespace {
7883

7984
const PassInfo *llvm::TwoAddressInstructionPassID = X.getPassInfo();
8085

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-
9086
/// Sink3AddrInstruction - A two-address instruction has been converted to a
9187
/// 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+
///
9491
bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
9592
MachineInstr *MI, unsigned SavedReg,
9693
MachineBasicBlock::iterator OldPos) {
@@ -101,6 +98,7 @@ bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
10198

10299
unsigned DefReg = 0;
103100
SmallSet<unsigned, 4> UseRegs;
101+
104102
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
105103
const MachineOperand &MO = MI->getOperand(i);
106104
if (!MO.isRegister())
@@ -123,6 +121,7 @@ bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
123121

124122
// Find the instruction that kills SavedReg.
125123
MachineInstr *KillMI = NULL;
124+
126125
for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SavedReg),
127126
UE = MRI->use_end(); UI != UE; ++UI) {
128127
MachineOperand &UseMO = UI.getOperand();
@@ -131,19 +130,23 @@ bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
131130
KillMI = UseMO.getParent();
132131
break;
133132
}
133+
134134
if (!KillMI || KillMI->getParent() != MBB)
135135
return false;
136136

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.
142143
MachineOperand *KillMO = NULL;
143144
MachineBasicBlock::iterator KillPos = KillMI;
144145
++KillPos;
146+
145147
for (MachineBasicBlock::iterator I = next(OldPos); I != KillPos; ++I) {
146148
MachineInstr *OtherMI = I;
149+
147150
for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
148151
MachineOperand &MO = OtherMI->getOperand(i);
149152
if (!MO.isRegister())
@@ -153,6 +156,7 @@ bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
153156
continue;
154157
if (DefReg == MOReg)
155158
return false;
159+
156160
if (MO.isKill()) {
157161
if (OtherMI == KillMI && MOReg == SavedReg)
158162
// Save the operand that kills the register. We want unset the kill
@@ -181,8 +185,7 @@ bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
181185
return true;
182186
}
183187

184-
/// runOnMachineFunction - Reduce two-address instructions to two
185-
/// operands.
188+
/// runOnMachineFunction - Reduce two-address instructions to two operands.
186189
///
187190
bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
188191
DOUT << "Machine Function\n";
@@ -203,8 +206,8 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
203206
mi != me; ) {
204207
MachineBasicBlock::iterator nmi = next(mi);
205208
const TargetInstrDesc &TID = mi->getDesc();
206-
207209
bool FirstTied = true;
210+
208211
for (unsigned si = 1, e = TID.getNumOperands(); si < e; ++si) {
209212
int ti = TID.getOperandConstraint(si, TOI::TIED_TO);
210213
if (ti == -1)
@@ -214,15 +217,16 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
214217
++NumTwoAddressInstrs;
215218
DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM));
216219
}
220+
217221
FirstTied = false;
218222

219223
assert(mi->getOperand(si).isRegister() && mi->getOperand(si).getReg() &&
220224
mi->getOperand(si).isUse() && "two address instruction invalid");
221225

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
223227
// and mark the def as def&use, otherwise we have to insert a copy.
224228
if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) {
225-
// rewrite:
229+
// Rewrite:
226230
// a = b op c
227231
// to:
228232
// a = b
@@ -257,9 +261,11 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
257261
assert(mi->getOperand(3-si).isRegister() &&
258262
"Not a proper commutative instruction!");
259263
unsigned regC = mi->getOperand(3-si).getReg();
264+
260265
if (mi->killsRegister(regC)) {
261266
DOUT << "2addr: COMMUTING : " << *mi;
262267
MachineInstr *NewMI = TII->commuteInstruction(mi);
268+
263269
if (NewMI == 0) {
264270
DOUT << "2addr: COMMUTING FAILED!\n";
265271
} else {
@@ -285,27 +291,30 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
285291
// FIXME: This assumes there are no more operands which are tied
286292
// to another register.
287293
#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)
289295
assert(TID.getOperandConstraint(i, TOI::TIED_TO) == -1);
290296
#endif
291297

292298
if (MachineInstr *New=TII->convertToThreeAddress(mbbi, mi, *LV)) {
293299
DOUT << "2addr: CONVERTING 2-ADDR: " << *mi;
294300
DOUT << "2addr: TO 3-ADDR: " << *New;
295301
bool Sunk = false;
302+
296303
if (New->findRegisterUseOperand(regB, false, TRI))
297304
// FIXME: Temporary workaround. If the new instruction doesn't
298305
// uses regB, convertToThreeAddress must have created more
299306
// then one instruction.
300307
Sunk = Sink3AddrInstruction(mbbi, New, regB, mi);
301-
mbbi->erase(mi); // Nuke the old inst.
308+
309+
mbbi->erase(mi); // Nuke the old inst.
310+
302311
if (!Sunk) {
303312
mi = New;
304313
nmi = next(mi);
305314
}
315+
306316
++NumConvertedTo3Addr;
307-
// Done with this instruction.
308-
break;
317+
break; // Done with this instruction.
309318
}
310319
}
311320
}
@@ -317,17 +326,19 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
317326
MachineBasicBlock::iterator prevMi = prior(mi);
318327
DOUT << "\t\tprepend:\t"; DEBUG(prevMi->print(*cerr.stream(), &TM));
319328

320-
// update live variables for regB
329+
// Update live variables for regB.
321330
LiveVariables::VarInfo& varInfoB = LV->getVarInfo(regB);
331+
322332
// regB is used in this BB.
323333
varInfoB.UsedBlocks[mbbi->getNumber()] = true;
334+
324335
if (LV->removeVirtualRegisterKilled(regB, mbbi, mi))
325336
LV->addVirtualRegisterKilled(regB, prevMi);
326337

327338
if (LV->removeVirtualRegisterDead(regB, mbbi, mi))
328339
LV->addVirtualRegisterDead(regB, prevMi);
329340

330-
// replace all occurences of regB with regA
341+
// Replace all occurences of regB with regA.
331342
for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
332343
if (mi->getOperand(i).isRegister() &&
333344
mi->getOperand(i).getReg() == regB)
@@ -341,6 +352,7 @@ bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
341352

342353
DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(*cerr.stream(), &TM));
343354
}
355+
344356
mi = nmi;
345357
}
346358
}

0 commit comments

Comments
 (0)