blob: 84491a5b5336832797074d0942c23b0934b6e4e4 [file] [log] [blame]
Alkis Evlogimenos71499de2003-12-18 13:06:04 +00001//===-- TwoAddressInstructionPass.cpp - Two-Address instruction pass ------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenos71499de2003-12-18 13:06:04 +00007//
8//===----------------------------------------------------------------------===//
9//
Alkis Evlogimenos50c047d2004-01-04 23:09:24 +000010// This file implements the TwoAddress instruction pass which is used
11// by most register allocators. Two-Address instructions are rewritten
12// from:
13//
14// A = B op C
15//
16// to:
17//
18// A = B
Alkis Evlogimenos14be6402004-02-04 22:17:40 +000019// A op= C
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000020//
Alkis Evlogimenos14be6402004-02-04 22:17:40 +000021// Note that if a register allocator chooses to use this pass, that it
22// has to be capable of handling the non-SSA nature of these rewritten
23// virtual registers.
24//
25// It is also worth noting that the duplicate operand of the two
26// address instruction is removed.
Chris Lattnerbd91c1c2004-01-31 21:07:15 +000027//
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000028//===----------------------------------------------------------------------===//
29
30#define DEBUG_TYPE "twoaddrinstr"
Chris Lattnerbd91c1c2004-01-31 21:07:15 +000031#include "llvm/CodeGen/Passes.h"
Chris Lattner1e313632004-07-21 23:17:57 +000032#include "llvm/Function.h"
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000033#include "llvm/CodeGen/LiveVariables.h"
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000034#include "llvm/CodeGen/MachineFunctionPass.h"
35#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000037#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000038#include "llvm/Target/TargetInstrInfo.h"
39#include "llvm/Target/TargetMachine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000040#include "llvm/Support/Compiler.h"
Evan Cheng875357d2008-03-13 06:37:55 +000041#include "llvm/Support/Debug.h"
Bill Wendling48f7f232008-05-26 05:18:34 +000042#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000043#include "llvm/ADT/Statistic.h"
44#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000045using namespace llvm;
46
Chris Lattnercd3245a2006-12-19 22:41:21 +000047STATISTIC(NumTwoAddressInstrs, "Number of two-address instructions");
48STATISTIC(NumCommuted , "Number of instructions commuted to coalesce");
49STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address");
Evan Cheng875357d2008-03-13 06:37:55 +000050STATISTIC(Num3AddrSunk, "Number of 3-address instructions sunk");
51
52namespace {
Bill Wendling637980e2008-05-10 00:12:52 +000053 class VISIBILITY_HIDDEN TwoAddressInstructionPass
54 : public MachineFunctionPass {
Evan Cheng875357d2008-03-13 06:37:55 +000055 const TargetInstrInfo *TII;
56 const TargetRegisterInfo *TRI;
57 MachineRegisterInfo *MRI;
58 LiveVariables *LV;
59
Bill Wendling637980e2008-05-10 00:12:52 +000060 bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI,
61 unsigned Reg,
62 MachineBasicBlock::iterator OldPos);
Evan Cheng875357d2008-03-13 06:37:55 +000063 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000064 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000065 TwoAddressInstructionPass() : MachineFunctionPass((intptr_t)&ID) {}
66
Bill Wendling637980e2008-05-10 00:12:52 +000067 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
68 AU.addRequired<LiveVariables>();
69 AU.addPreserved<LiveVariables>();
70 AU.addPreservedID(MachineLoopInfoID);
71 AU.addPreservedID(MachineDominatorsID);
72 AU.addPreservedID(PHIEliminationID);
73 MachineFunctionPass::getAnalysisUsage(AU);
74 }
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000075
Bill Wendling637980e2008-05-10 00:12:52 +000076 /// runOnMachineFunction - Pass entry point.
Misha Brukman75fa4e42004-07-22 15:26:23 +000077 bool runOnMachineFunction(MachineFunction&);
78 };
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000079}
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000080
Dan Gohman844731a2008-05-13 00:00:25 +000081char TwoAddressInstructionPass::ID = 0;
82static RegisterPass<TwoAddressInstructionPass>
83X("twoaddressinstruction", "Two-Address instruction pass");
84
Dan Gohman6ddba2b2008-05-13 02:05:11 +000085const PassInfo *const llvm::TwoAddressInstructionPassID = &X;
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000086
Evan Cheng875357d2008-03-13 06:37:55 +000087/// Sink3AddrInstruction - A two-address instruction has been converted to a
88/// three-address instruction to avoid clobbering a register. Try to sink it
Bill Wendling637980e2008-05-10 00:12:52 +000089/// past the instruction that would kill the above mentioned register to reduce
90/// register pressure.
91///
Evan Cheng875357d2008-03-13 06:37:55 +000092bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
93 MachineInstr *MI, unsigned SavedReg,
94 MachineBasicBlock::iterator OldPos) {
95 // Check if it's safe to move this instruction.
96 bool SeenStore = true; // Be conservative.
97 if (!MI->isSafeToMove(TII, SeenStore))
98 return false;
99
100 unsigned DefReg = 0;
101 SmallSet<unsigned, 4> UseRegs;
Bill Wendling637980e2008-05-10 00:12:52 +0000102
Evan Cheng875357d2008-03-13 06:37:55 +0000103 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
104 const MachineOperand &MO = MI->getOperand(i);
105 if (!MO.isRegister())
106 continue;
107 unsigned MOReg = MO.getReg();
108 if (!MOReg)
109 continue;
110 if (MO.isUse() && MOReg != SavedReg)
111 UseRegs.insert(MO.getReg());
112 if (!MO.isDef())
113 continue;
114 if (MO.isImplicit())
115 // Don't try to move it if it implicitly defines a register.
116 return false;
117 if (DefReg)
118 // For now, don't move any instructions that define multiple registers.
119 return false;
120 DefReg = MO.getReg();
121 }
122
123 // Find the instruction that kills SavedReg.
124 MachineInstr *KillMI = NULL;
Bill Wendling637980e2008-05-10 00:12:52 +0000125
Evan Cheng875357d2008-03-13 06:37:55 +0000126 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SavedReg),
127 UE = MRI->use_end(); UI != UE; ++UI) {
128 MachineOperand &UseMO = UI.getOperand();
129 if (!UseMO.isKill())
130 continue;
131 KillMI = UseMO.getParent();
132 break;
133 }
Bill Wendling637980e2008-05-10 00:12:52 +0000134
Evan Cheng875357d2008-03-13 06:37:55 +0000135 if (!KillMI || KillMI->getParent() != MBB)
136 return false;
137
Bill Wendling637980e2008-05-10 00:12:52 +0000138 // If any of the definitions are used by another instruction between the
139 // position and the kill use, then it's not safe to sink it.
140 //
141 // FIXME: This can be sped up if there is an easy way to query whether an
142 // instruction if before or after another instruction. Then we can use
143 // MachineRegisterInfo def / use instead.
Evan Cheng875357d2008-03-13 06:37:55 +0000144 MachineOperand *KillMO = NULL;
145 MachineBasicBlock::iterator KillPos = KillMI;
146 ++KillPos;
Bill Wendling637980e2008-05-10 00:12:52 +0000147
Evan Cheng875357d2008-03-13 06:37:55 +0000148 for (MachineBasicBlock::iterator I = next(OldPos); I != KillPos; ++I) {
149 MachineInstr *OtherMI = I;
Bill Wendling637980e2008-05-10 00:12:52 +0000150
Evan Cheng875357d2008-03-13 06:37:55 +0000151 for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
152 MachineOperand &MO = OtherMI->getOperand(i);
153 if (!MO.isRegister())
154 continue;
155 unsigned MOReg = MO.getReg();
156 if (!MOReg)
157 continue;
158 if (DefReg == MOReg)
159 return false;
Bill Wendling637980e2008-05-10 00:12:52 +0000160
Evan Cheng875357d2008-03-13 06:37:55 +0000161 if (MO.isKill()) {
162 if (OtherMI == KillMI && MOReg == SavedReg)
163 // Save the operand that kills the register. We want unset the kill
164 // marker is we can sink MI past it.
165 KillMO = &MO;
166 else if (UseRegs.count(MOReg))
167 // One of the uses is killed before the destination.
168 return false;
169 }
170 }
171 }
172
Evan Cheng875357d2008-03-13 06:37:55 +0000173 // Update kill and LV information.
174 KillMO->setIsKill(false);
175 KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI);
176 KillMO->setIsKill(true);
177 LiveVariables::VarInfo& VarInfo = LV->getVarInfo(SavedReg);
178 VarInfo.removeKill(KillMI);
179 VarInfo.Kills.push_back(MI);
180
181 // Move instruction to its destination.
182 MBB->remove(MI);
183 MBB->insert(KillPos, MI);
184
185 ++Num3AddrSunk;
186 return true;
187}
188
Bill Wendling637980e2008-05-10 00:12:52 +0000189/// runOnMachineFunction - Reduce two-address instructions to two operands.
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000190///
Chris Lattner163c1e72004-01-31 21:14:04 +0000191bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000192 DOUT << "Machine Function\n";
Misha Brukman75fa4e42004-07-22 15:26:23 +0000193 const TargetMachine &TM = MF.getTarget();
Evan Cheng875357d2008-03-13 06:37:55 +0000194 MRI = &MF.getRegInfo();
195 TII = TM.getInstrInfo();
196 TRI = TM.getRegisterInfo();
197 LV = &getAnalysis<LiveVariables>();
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000198
Misha Brukman75fa4e42004-07-22 15:26:23 +0000199 bool MadeChange = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000200
Bill Wendlinga09362e2006-11-28 22:48:48 +0000201 DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n";
202 DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
Alkis Evlogimenos3a9986f2004-02-18 00:35:06 +0000203
Bill Wendling48f7f232008-05-26 05:18:34 +0000204 SmallPtrSet<MachineInstr*, 8> ReMattedInstrs;
205
Misha Brukman75fa4e42004-07-22 15:26:23 +0000206 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
207 mbbi != mbbe; ++mbbi) {
208 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
Evan Cheng7a963fa2008-03-27 01:27:25 +0000209 mi != me; ) {
210 MachineBasicBlock::iterator nmi = next(mi);
Chris Lattner749c6f62008-01-07 07:27:27 +0000211 const TargetInstrDesc &TID = mi->getDesc();
Evan Cheng360c2dd2006-11-01 23:06:55 +0000212 bool FirstTied = true;
Bill Wendling637980e2008-05-10 00:12:52 +0000213
Chris Lattner749c6f62008-01-07 07:27:27 +0000214 for (unsigned si = 1, e = TID.getNumOperands(); si < e; ++si) {
215 int ti = TID.getOperandConstraint(si, TOI::TIED_TO);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000216 if (ti == -1)
217 continue;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000218
Evan Cheng360c2dd2006-11-01 23:06:55 +0000219 if (FirstTied) {
220 ++NumTwoAddressInstrs;
Bill Wendlingbcd24982006-12-07 20:28:15 +0000221 DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000222 }
Bill Wendling637980e2008-05-10 00:12:52 +0000223
Evan Cheng360c2dd2006-11-01 23:06:55 +0000224 FirstTied = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000225
Evan Cheng360c2dd2006-11-01 23:06:55 +0000226 assert(mi->getOperand(si).isRegister() && mi->getOperand(si).getReg() &&
227 mi->getOperand(si).isUse() && "two address instruction invalid");
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000228
Bill Wendling637980e2008-05-10 00:12:52 +0000229 // If the two operands are the same we just remove the use
Evan Cheng360c2dd2006-11-01 23:06:55 +0000230 // and mark the def as def&use, otherwise we have to insert a copy.
231 if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) {
Bill Wendling637980e2008-05-10 00:12:52 +0000232 // Rewrite:
Evan Cheng360c2dd2006-11-01 23:06:55 +0000233 // a = b op c
234 // to:
235 // a = b
236 // a = a op c
237 unsigned regA = mi->getOperand(ti).getReg();
238 unsigned regB = mi->getOperand(si).getReg();
239
Dan Gohman6f0d0242008-02-10 18:45:23 +0000240 assert(TargetRegisterInfo::isVirtualRegister(regA) &&
241 TargetRegisterInfo::isVirtualRegister(regB) &&
Evan Cheng360c2dd2006-11-01 23:06:55 +0000242 "cannot update physical register live information");
Chris Lattner6b507672004-01-31 21:21:43 +0000243
Chris Lattner1e313632004-07-21 23:17:57 +0000244#ifndef NDEBUG
Evan Cheng360c2dd2006-11-01 23:06:55 +0000245 // First, verify that we don't have a use of a in the instruction (a =
246 // b + a for example) because our transformation will not work. This
247 // should never occur because we are in SSA form.
248 for (unsigned i = 0; i != mi->getNumOperands(); ++i)
249 assert((int)i == ti ||
250 !mi->getOperand(i).isRegister() ||
251 mi->getOperand(i).getReg() != regA);
Chris Lattner1e313632004-07-21 23:17:57 +0000252#endif
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000253
Evan Cheng360c2dd2006-11-01 23:06:55 +0000254 // If this instruction is not the killing user of B, see if we can
255 // rearrange the code to make it so. Making it the killing user will
256 // allow us to coalesce A and B together, eliminating the copy we are
257 // about to insert.
Evan Cheng6130f662008-03-05 00:59:57 +0000258 if (!mi->killsRegister(regB)) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000259 // If this instruction is commutative, check to see if C dies. If
260 // so, swap the B and C operands. This makes the live ranges of A
261 // and C joinable.
262 // FIXME: This code also works for A := B op C instructions.
Chris Lattner749c6f62008-01-07 07:27:27 +0000263 if (TID.isCommutable() && mi->getNumOperands() >= 3) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000264 assert(mi->getOperand(3-si).isRegister() &&
265 "Not a proper commutative instruction!");
266 unsigned regC = mi->getOperand(3-si).getReg();
Bill Wendling637980e2008-05-10 00:12:52 +0000267
Evan Cheng6130f662008-03-05 00:59:57 +0000268 if (mi->killsRegister(regC)) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000269 DOUT << "2addr: COMMUTING : " << *mi;
Evan Cheng875357d2008-03-13 06:37:55 +0000270 MachineInstr *NewMI = TII->commuteInstruction(mi);
Bill Wendling637980e2008-05-10 00:12:52 +0000271
Evan Cheng360c2dd2006-11-01 23:06:55 +0000272 if (NewMI == 0) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000273 DOUT << "2addr: COMMUTING FAILED!\n";
Evan Cheng360c2dd2006-11-01 23:06:55 +0000274 } else {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000275 DOUT << "2addr: COMMUTED TO: " << *NewMI;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000276 // If the instruction changed to commute it, update livevar.
277 if (NewMI != mi) {
Evan Cheng875357d2008-03-13 06:37:55 +0000278 LV->instructionChanged(mi, NewMI); // Update live variables
Evan Cheng360c2dd2006-11-01 23:06:55 +0000279 mbbi->insert(mi, NewMI); // Insert the new inst
280 mbbi->erase(mi); // Nuke the old inst.
281 mi = NewMI;
282 }
283
284 ++NumCommuted;
285 regB = regC;
286 goto InstructionRearranged;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000287 }
Chris Lattnerc71d6942005-01-19 07:08:42 +0000288 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000289 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000290
291 // If this instruction is potentially convertible to a true
292 // three-address instruction,
Chris Lattner749c6f62008-01-07 07:27:27 +0000293 if (TID.isConvertibleTo3Addr()) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000294 // FIXME: This assumes there are no more operands which are tied
295 // to another register.
296#ifndef NDEBUG
Bill Wendling637980e2008-05-10 00:12:52 +0000297 for (unsigned i = si + 1, e = TID.getNumOperands(); i < e; ++i)
Chris Lattner749c6f62008-01-07 07:27:27 +0000298 assert(TID.getOperandConstraint(i, TOI::TIED_TO) == -1);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000299#endif
300
Evan Cheng875357d2008-03-13 06:37:55 +0000301 if (MachineInstr *New=TII->convertToThreeAddress(mbbi, mi, *LV)) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000302 DOUT << "2addr: CONVERTING 2-ADDR: " << *mi;
303 DOUT << "2addr: TO 3-ADDR: " << *New;
Evan Cheng0099ae22008-03-13 07:56:58 +0000304 bool Sunk = false;
Bill Wendling637980e2008-05-10 00:12:52 +0000305
Evan Chenga2248682008-03-13 08:04:35 +0000306 if (New->findRegisterUseOperand(regB, false, TRI))
Evan Cheng0099ae22008-03-13 07:56:58 +0000307 // FIXME: Temporary workaround. If the new instruction doesn't
308 // uses regB, convertToThreeAddress must have created more
309 // then one instruction.
310 Sunk = Sink3AddrInstruction(mbbi, New, regB, mi);
Bill Wendling637980e2008-05-10 00:12:52 +0000311
312 mbbi->erase(mi); // Nuke the old inst.
313
Evan Cheng7a963fa2008-03-27 01:27:25 +0000314 if (!Sunk) {
315 mi = New;
316 nmi = next(mi);
317 }
Bill Wendling637980e2008-05-10 00:12:52 +0000318
Evan Cheng360c2dd2006-11-01 23:06:55 +0000319 ++NumConvertedTo3Addr;
Bill Wendling637980e2008-05-10 00:12:52 +0000320 break; // Done with this instruction.
Evan Cheng360c2dd2006-11-01 23:06:55 +0000321 }
Evan Chengb9d5e7c2007-10-20 04:01:47 +0000322 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000323 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000324
325 InstructionRearranged:
Chris Lattner84bc5422007-12-31 04:13:23 +0000326 const TargetRegisterClass* rc = MF.getRegInfo().getRegClass(regA);
Bill Wendling48f7f232008-05-26 05:18:34 +0000327 MachineInstr *Orig = MRI->getVRegDef(regB);
328
329 if (Orig && TII->isTriviallyReMaterializable(Orig)) {
330 TII->reMaterialize(*mbbi, mi, regA, Orig);
331 ReMattedInstrs.insert(Orig);
332 } else {
333 TII->copyRegToReg(*mbbi, mi, regA, regB, rc, rc);
334 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000335
336 MachineBasicBlock::iterator prevMi = prior(mi);
Bill Wendlingbcd24982006-12-07 20:28:15 +0000337 DOUT << "\t\tprepend:\t"; DEBUG(prevMi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000338
Bill Wendling637980e2008-05-10 00:12:52 +0000339 // Update live variables for regB.
Evan Cheng875357d2008-03-13 06:37:55 +0000340 LiveVariables::VarInfo& varInfoB = LV->getVarInfo(regB);
Bill Wendling637980e2008-05-10 00:12:52 +0000341
Owen Andersona0185402007-11-08 01:20:48 +0000342 // regB is used in this BB.
343 varInfoB.UsedBlocks[mbbi->getNumber()] = true;
Bill Wendling637980e2008-05-10 00:12:52 +0000344
Evan Cheng875357d2008-03-13 06:37:55 +0000345 if (LV->removeVirtualRegisterKilled(regB, mbbi, mi))
346 LV->addVirtualRegisterKilled(regB, prevMi);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000347
Evan Cheng875357d2008-03-13 06:37:55 +0000348 if (LV->removeVirtualRegisterDead(regB, mbbi, mi))
349 LV->addVirtualRegisterDead(regB, prevMi);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000350
Bill Wendling637980e2008-05-10 00:12:52 +0000351 // Replace all occurences of regB with regA.
Evan Cheng360c2dd2006-11-01 23:06:55 +0000352 for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
353 if (mi->getOperand(i).isRegister() &&
354 mi->getOperand(i).getReg() == regB)
355 mi->getOperand(i).setReg(regA);
356 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000357 }
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000358
Evan Cheng360c2dd2006-11-01 23:06:55 +0000359 assert(mi->getOperand(ti).isDef() && mi->getOperand(si).isUse());
360 mi->getOperand(ti).setReg(mi->getOperand(si).getReg());
361 MadeChange = true;
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000362
Bill Wendlingbcd24982006-12-07 20:28:15 +0000363 DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(*cerr.stream(), &TM));
Misha Brukman75fa4e42004-07-22 15:26:23 +0000364 }
Bill Wendling637980e2008-05-10 00:12:52 +0000365
Evan Cheng7a963fa2008-03-27 01:27:25 +0000366 mi = nmi;
Misha Brukman75fa4e42004-07-22 15:26:23 +0000367 }
368 }
369
Bill Wendling48f7f232008-05-26 05:18:34 +0000370 SmallPtrSet<MachineInstr*, 8>::iterator I = ReMattedInstrs.begin();
371 SmallPtrSet<MachineInstr*, 8>::iterator E = ReMattedInstrs.end();
372
373 for (; I != E; ++I) {
374 MachineInstr *MI = *I;
375 bool InstrDead = true;
376
377 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
378 const MachineOperand &MO = MI->getOperand(i);
379 if (!MO.isRegister())
380 continue;
381 unsigned MOReg = MO.getReg();
382 if (!MOReg)
383 continue;
384 if (MO.isDef()) {
385 if (MO.isImplicit())
386 continue;
387
388 if (MRI->use_begin(MOReg) != MRI->use_end()) {
389 InstrDead = false;
390 break;
391 }
392 }
393 }
394
395 if (InstrDead && MI->getNumOperands() > 0)
396 MI->eraseFromParent();
397 }
398
Misha Brukman75fa4e42004-07-22 15:26:23 +0000399 return MadeChange;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000400}