blob: 81864b7c0e4e81c6de067aae0d996d79db0d5beb [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"
Bill Wendlinga16157a2008-05-26 05:49:49 +000040#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000041#include "llvm/Support/Compiler.h"
Evan Cheng875357d2008-03-13 06:37:55 +000042#include "llvm/Support/Debug.h"
Bill Wendling48f7f232008-05-26 05:18:34 +000043#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000044#include "llvm/ADT/Statistic.h"
45#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000046using namespace llvm;
47
Chris Lattnercd3245a2006-12-19 22:41:21 +000048STATISTIC(NumTwoAddressInstrs, "Number of two-address instructions");
49STATISTIC(NumCommuted , "Number of instructions commuted to coalesce");
50STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address");
Evan Cheng875357d2008-03-13 06:37:55 +000051STATISTIC(Num3AddrSunk, "Number of 3-address instructions sunk");
52
Bill Wendlinga16157a2008-05-26 05:49:49 +000053static cl::opt<bool>
54EnableReMat("2-addr-remat", cl::init(false), cl::Hidden,
55 cl::desc("Two-addr conversion should remat when possible."));
56
Evan Cheng875357d2008-03-13 06:37:55 +000057namespace {
Bill Wendling637980e2008-05-10 00:12:52 +000058 class VISIBILITY_HIDDEN TwoAddressInstructionPass
59 : public MachineFunctionPass {
Evan Cheng875357d2008-03-13 06:37:55 +000060 const TargetInstrInfo *TII;
61 const TargetRegisterInfo *TRI;
62 MachineRegisterInfo *MRI;
63 LiveVariables *LV;
64
Bill Wendling637980e2008-05-10 00:12:52 +000065 bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI,
66 unsigned Reg,
67 MachineBasicBlock::iterator OldPos);
Evan Cheng875357d2008-03-13 06:37:55 +000068 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000069 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000070 TwoAddressInstructionPass() : MachineFunctionPass((intptr_t)&ID) {}
71
Bill Wendling637980e2008-05-10 00:12:52 +000072 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
73 AU.addRequired<LiveVariables>();
74 AU.addPreserved<LiveVariables>();
75 AU.addPreservedID(MachineLoopInfoID);
76 AU.addPreservedID(MachineDominatorsID);
77 AU.addPreservedID(PHIEliminationID);
78 MachineFunctionPass::getAnalysisUsage(AU);
79 }
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000080
Bill Wendling637980e2008-05-10 00:12:52 +000081 /// runOnMachineFunction - Pass entry point.
Misha Brukman75fa4e42004-07-22 15:26:23 +000082 bool runOnMachineFunction(MachineFunction&);
83 };
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000084}
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000085
Dan Gohman844731a2008-05-13 00:00:25 +000086char TwoAddressInstructionPass::ID = 0;
87static RegisterPass<TwoAddressInstructionPass>
88X("twoaddressinstruction", "Two-Address instruction pass");
89
Dan Gohman6ddba2b2008-05-13 02:05:11 +000090const PassInfo *const llvm::TwoAddressInstructionPassID = &X;
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000091
Evan Cheng875357d2008-03-13 06:37:55 +000092/// Sink3AddrInstruction - A two-address instruction has been converted to a
93/// three-address instruction to avoid clobbering a register. Try to sink it
Bill Wendling637980e2008-05-10 00:12:52 +000094/// past the instruction that would kill the above mentioned register to reduce
95/// register pressure.
96///
Evan Cheng875357d2008-03-13 06:37:55 +000097bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
98 MachineInstr *MI, unsigned SavedReg,
99 MachineBasicBlock::iterator OldPos) {
100 // Check if it's safe to move this instruction.
101 bool SeenStore = true; // Be conservative.
102 if (!MI->isSafeToMove(TII, SeenStore))
103 return false;
104
105 unsigned DefReg = 0;
106 SmallSet<unsigned, 4> UseRegs;
Bill Wendling637980e2008-05-10 00:12:52 +0000107
Evan Cheng875357d2008-03-13 06:37:55 +0000108 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
109 const MachineOperand &MO = MI->getOperand(i);
110 if (!MO.isRegister())
111 continue;
112 unsigned MOReg = MO.getReg();
113 if (!MOReg)
114 continue;
115 if (MO.isUse() && MOReg != SavedReg)
116 UseRegs.insert(MO.getReg());
117 if (!MO.isDef())
118 continue;
119 if (MO.isImplicit())
120 // Don't try to move it if it implicitly defines a register.
121 return false;
122 if (DefReg)
123 // For now, don't move any instructions that define multiple registers.
124 return false;
125 DefReg = MO.getReg();
126 }
127
128 // Find the instruction that kills SavedReg.
129 MachineInstr *KillMI = NULL;
Bill Wendling637980e2008-05-10 00:12:52 +0000130
Evan Cheng875357d2008-03-13 06:37:55 +0000131 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SavedReg),
132 UE = MRI->use_end(); UI != UE; ++UI) {
133 MachineOperand &UseMO = UI.getOperand();
134 if (!UseMO.isKill())
135 continue;
136 KillMI = UseMO.getParent();
137 break;
138 }
Bill Wendling637980e2008-05-10 00:12:52 +0000139
Evan Cheng875357d2008-03-13 06:37:55 +0000140 if (!KillMI || KillMI->getParent() != MBB)
141 return false;
142
Bill Wendling637980e2008-05-10 00:12:52 +0000143 // If any of the definitions are used by another instruction between the
144 // position and the kill use, then it's not safe to sink it.
145 //
146 // FIXME: This can be sped up if there is an easy way to query whether an
147 // instruction if before or after another instruction. Then we can use
148 // MachineRegisterInfo def / use instead.
Evan Cheng875357d2008-03-13 06:37:55 +0000149 MachineOperand *KillMO = NULL;
150 MachineBasicBlock::iterator KillPos = KillMI;
151 ++KillPos;
Bill Wendling637980e2008-05-10 00:12:52 +0000152
Evan Cheng875357d2008-03-13 06:37:55 +0000153 for (MachineBasicBlock::iterator I = next(OldPos); I != KillPos; ++I) {
154 MachineInstr *OtherMI = I;
Bill Wendling637980e2008-05-10 00:12:52 +0000155
Evan Cheng875357d2008-03-13 06:37:55 +0000156 for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
157 MachineOperand &MO = OtherMI->getOperand(i);
158 if (!MO.isRegister())
159 continue;
160 unsigned MOReg = MO.getReg();
161 if (!MOReg)
162 continue;
163 if (DefReg == MOReg)
164 return false;
Bill Wendling637980e2008-05-10 00:12:52 +0000165
Evan Cheng875357d2008-03-13 06:37:55 +0000166 if (MO.isKill()) {
167 if (OtherMI == KillMI && MOReg == SavedReg)
168 // Save the operand that kills the register. We want unset the kill
169 // marker is we can sink MI past it.
170 KillMO = &MO;
171 else if (UseRegs.count(MOReg))
172 // One of the uses is killed before the destination.
173 return false;
174 }
175 }
176 }
177
Evan Cheng875357d2008-03-13 06:37:55 +0000178 // Update kill and LV information.
179 KillMO->setIsKill(false);
180 KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI);
181 KillMO->setIsKill(true);
182 LiveVariables::VarInfo& VarInfo = LV->getVarInfo(SavedReg);
183 VarInfo.removeKill(KillMI);
184 VarInfo.Kills.push_back(MI);
185
186 // Move instruction to its destination.
187 MBB->remove(MI);
188 MBB->insert(KillPos, MI);
189
190 ++Num3AddrSunk;
191 return true;
192}
193
Bill Wendling637980e2008-05-10 00:12:52 +0000194/// runOnMachineFunction - Reduce two-address instructions to two operands.
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000195///
Chris Lattner163c1e72004-01-31 21:14:04 +0000196bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000197 DOUT << "Machine Function\n";
Misha Brukman75fa4e42004-07-22 15:26:23 +0000198 const TargetMachine &TM = MF.getTarget();
Evan Cheng875357d2008-03-13 06:37:55 +0000199 MRI = &MF.getRegInfo();
200 TII = TM.getInstrInfo();
201 TRI = TM.getRegisterInfo();
202 LV = &getAnalysis<LiveVariables>();
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000203
Misha Brukman75fa4e42004-07-22 15:26:23 +0000204 bool MadeChange = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000205
Bill Wendlinga09362e2006-11-28 22:48:48 +0000206 DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n";
207 DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
Alkis Evlogimenos3a9986f2004-02-18 00:35:06 +0000208
Bill Wendling48f7f232008-05-26 05:18:34 +0000209 SmallPtrSet<MachineInstr*, 8> ReMattedInstrs;
210
Misha Brukman75fa4e42004-07-22 15:26:23 +0000211 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
212 mbbi != mbbe; ++mbbi) {
213 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
Evan Cheng7a963fa2008-03-27 01:27:25 +0000214 mi != me; ) {
215 MachineBasicBlock::iterator nmi = next(mi);
Chris Lattner749c6f62008-01-07 07:27:27 +0000216 const TargetInstrDesc &TID = mi->getDesc();
Evan Cheng360c2dd2006-11-01 23:06:55 +0000217 bool FirstTied = true;
Bill Wendling637980e2008-05-10 00:12:52 +0000218
Chris Lattner749c6f62008-01-07 07:27:27 +0000219 for (unsigned si = 1, e = TID.getNumOperands(); si < e; ++si) {
220 int ti = TID.getOperandConstraint(si, TOI::TIED_TO);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000221 if (ti == -1)
222 continue;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000223
Evan Cheng360c2dd2006-11-01 23:06:55 +0000224 if (FirstTied) {
225 ++NumTwoAddressInstrs;
Bill Wendlingbcd24982006-12-07 20:28:15 +0000226 DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000227 }
Bill Wendling637980e2008-05-10 00:12:52 +0000228
Evan Cheng360c2dd2006-11-01 23:06:55 +0000229 FirstTied = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000230
Evan Cheng360c2dd2006-11-01 23:06:55 +0000231 assert(mi->getOperand(si).isRegister() && mi->getOperand(si).getReg() &&
232 mi->getOperand(si).isUse() && "two address instruction invalid");
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000233
Bill Wendling637980e2008-05-10 00:12:52 +0000234 // If the two operands are the same we just remove the use
Evan Cheng360c2dd2006-11-01 23:06:55 +0000235 // and mark the def as def&use, otherwise we have to insert a copy.
236 if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) {
Bill Wendling637980e2008-05-10 00:12:52 +0000237 // Rewrite:
Evan Cheng360c2dd2006-11-01 23:06:55 +0000238 // a = b op c
239 // to:
240 // a = b
241 // a = a op c
242 unsigned regA = mi->getOperand(ti).getReg();
243 unsigned regB = mi->getOperand(si).getReg();
244
Dan Gohman6f0d0242008-02-10 18:45:23 +0000245 assert(TargetRegisterInfo::isVirtualRegister(regA) &&
246 TargetRegisterInfo::isVirtualRegister(regB) &&
Evan Cheng360c2dd2006-11-01 23:06:55 +0000247 "cannot update physical register live information");
Chris Lattner6b507672004-01-31 21:21:43 +0000248
Chris Lattner1e313632004-07-21 23:17:57 +0000249#ifndef NDEBUG
Evan Cheng360c2dd2006-11-01 23:06:55 +0000250 // First, verify that we don't have a use of a in the instruction (a =
251 // b + a for example) because our transformation will not work. This
252 // should never occur because we are in SSA form.
253 for (unsigned i = 0; i != mi->getNumOperands(); ++i)
254 assert((int)i == ti ||
255 !mi->getOperand(i).isRegister() ||
256 mi->getOperand(i).getReg() != regA);
Chris Lattner1e313632004-07-21 23:17:57 +0000257#endif
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000258
Evan Cheng360c2dd2006-11-01 23:06:55 +0000259 // If this instruction is not the killing user of B, see if we can
260 // rearrange the code to make it so. Making it the killing user will
261 // allow us to coalesce A and B together, eliminating the copy we are
262 // about to insert.
Evan Cheng6130f662008-03-05 00:59:57 +0000263 if (!mi->killsRegister(regB)) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000264 // If this instruction is commutative, check to see if C dies. If
265 // so, swap the B and C operands. This makes the live ranges of A
266 // and C joinable.
267 // FIXME: This code also works for A := B op C instructions.
Chris Lattner749c6f62008-01-07 07:27:27 +0000268 if (TID.isCommutable() && mi->getNumOperands() >= 3) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000269 assert(mi->getOperand(3-si).isRegister() &&
270 "Not a proper commutative instruction!");
271 unsigned regC = mi->getOperand(3-si).getReg();
Bill Wendling637980e2008-05-10 00:12:52 +0000272
Evan Cheng6130f662008-03-05 00:59:57 +0000273 if (mi->killsRegister(regC)) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000274 DOUT << "2addr: COMMUTING : " << *mi;
Evan Cheng875357d2008-03-13 06:37:55 +0000275 MachineInstr *NewMI = TII->commuteInstruction(mi);
Bill Wendling637980e2008-05-10 00:12:52 +0000276
Evan Cheng360c2dd2006-11-01 23:06:55 +0000277 if (NewMI == 0) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000278 DOUT << "2addr: COMMUTING FAILED!\n";
Evan Cheng360c2dd2006-11-01 23:06:55 +0000279 } else {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000280 DOUT << "2addr: COMMUTED TO: " << *NewMI;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000281 // If the instruction changed to commute it, update livevar.
282 if (NewMI != mi) {
Evan Cheng875357d2008-03-13 06:37:55 +0000283 LV->instructionChanged(mi, NewMI); // Update live variables
Evan Cheng360c2dd2006-11-01 23:06:55 +0000284 mbbi->insert(mi, NewMI); // Insert the new inst
285 mbbi->erase(mi); // Nuke the old inst.
286 mi = NewMI;
287 }
288
289 ++NumCommuted;
290 regB = regC;
291 goto InstructionRearranged;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000292 }
Chris Lattnerc71d6942005-01-19 07:08:42 +0000293 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000294 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000295
296 // If this instruction is potentially convertible to a true
297 // three-address instruction,
Chris Lattner749c6f62008-01-07 07:27:27 +0000298 if (TID.isConvertibleTo3Addr()) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000299 // FIXME: This assumes there are no more operands which are tied
300 // to another register.
301#ifndef NDEBUG
Bill Wendling637980e2008-05-10 00:12:52 +0000302 for (unsigned i = si + 1, e = TID.getNumOperands(); i < e; ++i)
Chris Lattner749c6f62008-01-07 07:27:27 +0000303 assert(TID.getOperandConstraint(i, TOI::TIED_TO) == -1);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000304#endif
305
Evan Cheng875357d2008-03-13 06:37:55 +0000306 if (MachineInstr *New=TII->convertToThreeAddress(mbbi, mi, *LV)) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000307 DOUT << "2addr: CONVERTING 2-ADDR: " << *mi;
308 DOUT << "2addr: TO 3-ADDR: " << *New;
Evan Cheng0099ae22008-03-13 07:56:58 +0000309 bool Sunk = false;
Bill Wendling637980e2008-05-10 00:12:52 +0000310
Evan Chenga2248682008-03-13 08:04:35 +0000311 if (New->findRegisterUseOperand(regB, false, TRI))
Evan Cheng0099ae22008-03-13 07:56:58 +0000312 // FIXME: Temporary workaround. If the new instruction doesn't
313 // uses regB, convertToThreeAddress must have created more
314 // then one instruction.
315 Sunk = Sink3AddrInstruction(mbbi, New, regB, mi);
Bill Wendling637980e2008-05-10 00:12:52 +0000316
317 mbbi->erase(mi); // Nuke the old inst.
318
Evan Cheng7a963fa2008-03-27 01:27:25 +0000319 if (!Sunk) {
320 mi = New;
321 nmi = next(mi);
322 }
Bill Wendling637980e2008-05-10 00:12:52 +0000323
Evan Cheng360c2dd2006-11-01 23:06:55 +0000324 ++NumConvertedTo3Addr;
Bill Wendling637980e2008-05-10 00:12:52 +0000325 break; // Done with this instruction.
Evan Cheng360c2dd2006-11-01 23:06:55 +0000326 }
Evan Chengb9d5e7c2007-10-20 04:01:47 +0000327 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000328 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000329
330 InstructionRearranged:
Chris Lattner84bc5422007-12-31 04:13:23 +0000331 const TargetRegisterClass* rc = MF.getRegInfo().getRegClass(regA);
Bill Wendling48f7f232008-05-26 05:18:34 +0000332 MachineInstr *Orig = MRI->getVRegDef(regB);
Bill Wendlingbd0879d2008-05-29 01:02:09 +0000333 const TargetInstrDesc &OrigTID = Orig->getDesc();
Bill Wendlinga8db1472008-05-28 22:52:47 +0000334 bool SawStore = false;
Bill Wendling48f7f232008-05-26 05:18:34 +0000335
Bill Wendlinga8db1472008-05-28 22:52:47 +0000336 if (EnableReMat && Orig && Orig->isSafeToMove(TII, SawStore) &&
Bill Wendlingbd0879d2008-05-29 01:02:09 +0000337 OrigTID.isAsCheapAsAMove() && !OrigTID.mayLoad() &&
338 !OrigTID.isSimpleLoad()) {
339 DEBUG(cerr << "2addr: REMATTING : " << *Orig << "\n");
Bill Wendling48f7f232008-05-26 05:18:34 +0000340 TII->reMaterialize(*mbbi, mi, regA, Orig);
341 ReMattedInstrs.insert(Orig);
342 } else {
343 TII->copyRegToReg(*mbbi, mi, regA, regB, rc, rc);
344 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000345
346 MachineBasicBlock::iterator prevMi = prior(mi);
Bill Wendlingbcd24982006-12-07 20:28:15 +0000347 DOUT << "\t\tprepend:\t"; DEBUG(prevMi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000348
Bill Wendling637980e2008-05-10 00:12:52 +0000349 // Update live variables for regB.
Evan Cheng875357d2008-03-13 06:37:55 +0000350 LiveVariables::VarInfo& varInfoB = LV->getVarInfo(regB);
Bill Wendling637980e2008-05-10 00:12:52 +0000351
Owen Andersona0185402007-11-08 01:20:48 +0000352 // regB is used in this BB.
353 varInfoB.UsedBlocks[mbbi->getNumber()] = true;
Bill Wendling637980e2008-05-10 00:12:52 +0000354
Evan Cheng875357d2008-03-13 06:37:55 +0000355 if (LV->removeVirtualRegisterKilled(regB, mbbi, mi))
356 LV->addVirtualRegisterKilled(regB, prevMi);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000357
Evan Cheng875357d2008-03-13 06:37:55 +0000358 if (LV->removeVirtualRegisterDead(regB, mbbi, mi))
359 LV->addVirtualRegisterDead(regB, prevMi);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000360
Bill Wendling637980e2008-05-10 00:12:52 +0000361 // Replace all occurences of regB with regA.
Evan Cheng360c2dd2006-11-01 23:06:55 +0000362 for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
363 if (mi->getOperand(i).isRegister() &&
364 mi->getOperand(i).getReg() == regB)
365 mi->getOperand(i).setReg(regA);
366 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000367 }
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000368
Evan Cheng360c2dd2006-11-01 23:06:55 +0000369 assert(mi->getOperand(ti).isDef() && mi->getOperand(si).isUse());
370 mi->getOperand(ti).setReg(mi->getOperand(si).getReg());
371 MadeChange = true;
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000372
Bill Wendlingbcd24982006-12-07 20:28:15 +0000373 DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(*cerr.stream(), &TM));
Misha Brukman75fa4e42004-07-22 15:26:23 +0000374 }
Bill Wendling637980e2008-05-10 00:12:52 +0000375
Evan Cheng7a963fa2008-03-27 01:27:25 +0000376 mi = nmi;
Misha Brukman75fa4e42004-07-22 15:26:23 +0000377 }
378 }
379
Bill Wendlinga16157a2008-05-26 05:49:49 +0000380 if (EnableReMat) {
Bill Wendlingb0f65e12008-05-27 20:40:52 +0000381 // Check to see if the instructions that we rematerialized are now dead. If
382 // they are, expunge them here.
Bill Wendlinga16157a2008-05-26 05:49:49 +0000383 SmallPtrSet<MachineInstr*, 8>::iterator I = ReMattedInstrs.begin();
384 SmallPtrSet<MachineInstr*, 8>::iterator E = ReMattedInstrs.end();
Bill Wendling48f7f232008-05-26 05:18:34 +0000385
Bill Wendlinga16157a2008-05-26 05:49:49 +0000386 for (; I != E; ++I) {
387 MachineInstr *MI = *I;
388 bool InstrDead = true;
389
390 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
391 const MachineOperand &MO = MI->getOperand(i);
392 if (!MO.isRegister())
Bill Wendling48f7f232008-05-26 05:18:34 +0000393 continue;
Bill Wendlinga16157a2008-05-26 05:49:49 +0000394 unsigned MOReg = MO.getReg();
Bill Wendling48f7f232008-05-26 05:18:34 +0000395
Bill Wendlingb0f65e12008-05-27 20:40:52 +0000396 if (!MOReg || !MO.isDef() || (MO.isImplicit() && MO.isDead()))
397 continue;
398
399 if (MRI->use_begin(MOReg) != MRI->use_end()) {
400 InstrDead = false;
401 break;
Bill Wendling48f7f232008-05-26 05:18:34 +0000402 }
403 }
Bill Wendling48f7f232008-05-26 05:18:34 +0000404
Bill Wendlingb0f65e12008-05-27 20:40:52 +0000405 if (InstrDead)
Bill Wendlinga16157a2008-05-26 05:49:49 +0000406 MI->eraseFromParent();
407 }
Bill Wendling48f7f232008-05-26 05:18:34 +0000408 }
409
Misha Brukman75fa4e42004-07-22 15:26:23 +0000410 return MadeChange;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000411}