blob: dbfd770b733c052bc9b58d4e6f235ef711153fc1 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- TwoAddressInstructionPass.cpp - Two-Address instruction pass ------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// 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
19// A op= C
20//
21// 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.
27//
28//===----------------------------------------------------------------------===//
29
30#define DEBUG_TYPE "twoaddrinstr"
31#include "llvm/CodeGen/Passes.h"
32#include "llvm/Function.h"
33#include "llvm/CodeGen/LiveVariables.h"
34#include "llvm/CodeGen/MachineFunctionPass.h"
35#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner1b989192007-12-31 04:13:23 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000037#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Target/TargetInstrInfo.h"
39#include "llvm/Target/TargetMachine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040#include "llvm/Support/Compiler.h"
Evan Cheng0c85fe62008-03-13 06:37:55 +000041#include "llvm/Support/Debug.h"
Evan Cheng990dd2b2008-06-18 07:49:14 +000042#include "llvm/ADT/BitVector.h"
43#include "llvm/ADT/DenseMap.h"
Bill Wendling3334b272008-05-26 05:18:34 +000044#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045#include "llvm/ADT/Statistic.h"
46#include "llvm/ADT/STLExtras.h"
47using namespace llvm;
48
49STATISTIC(NumTwoAddressInstrs, "Number of two-address instructions");
50STATISTIC(NumCommuted , "Number of instructions commuted to coalesce");
51STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address");
Evan Cheng0c85fe62008-03-13 06:37:55 +000052STATISTIC(Num3AddrSunk, "Number of 3-address instructions sunk");
Evan Cheng990dd2b2008-06-18 07:49:14 +000053STATISTIC(NumReMats, "Number of instructions re-materialized");
Evan Cheng0c85fe62008-03-13 06:37:55 +000054
55namespace {
Bill Wendling06289272008-05-10 00:12:52 +000056 class VISIBILITY_HIDDEN TwoAddressInstructionPass
57 : public MachineFunctionPass {
Evan Cheng0c85fe62008-03-13 06:37:55 +000058 const TargetInstrInfo *TII;
59 const TargetRegisterInfo *TRI;
60 MachineRegisterInfo *MRI;
61 LiveVariables *LV;
62
Bill Wendling06289272008-05-10 00:12:52 +000063 bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI,
64 unsigned Reg,
65 MachineBasicBlock::iterator OldPos);
Evan Cheng990dd2b2008-06-18 07:49:14 +000066
Evan Cheng990dd2b2008-06-18 07:49:14 +000067 bool isProfitableToReMat(unsigned Reg, const TargetRegisterClass *RC,
Evan Chengd2b9d302008-06-25 01:16:38 +000068 MachineInstr *MI, MachineInstr *DefMI,
69 MachineBasicBlock *MBB, unsigned Loc,
Evan Cheng990dd2b2008-06-18 07:49:14 +000070 DenseMap<MachineInstr*, unsigned> &DistanceMap);
Evan Cheng0c85fe62008-03-13 06:37:55 +000071 public:
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000073 TwoAddressInstructionPass() : MachineFunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074
Bill Wendling06289272008-05-10 00:12:52 +000075 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Bill Wendling06289272008-05-10 00:12:52 +000076 AU.addPreserved<LiveVariables>();
77 AU.addPreservedID(MachineLoopInfoID);
78 AU.addPreservedID(MachineDominatorsID);
79 AU.addPreservedID(PHIEliminationID);
80 MachineFunctionPass::getAnalysisUsage(AU);
81 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082
Bill Wendling06289272008-05-10 00:12:52 +000083 /// runOnMachineFunction - Pass entry point.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 bool runOnMachineFunction(MachineFunction&);
85 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086}
87
Dan Gohman089efff2008-05-13 00:00:25 +000088char TwoAddressInstructionPass::ID = 0;
89static RegisterPass<TwoAddressInstructionPass>
90X("twoaddressinstruction", "Two-Address instruction pass");
91
Dan Gohman66a636e2008-05-13 02:05:11 +000092const PassInfo *const llvm::TwoAddressInstructionPassID = &X;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093
Evan Cheng0c85fe62008-03-13 06:37:55 +000094/// Sink3AddrInstruction - A two-address instruction has been converted to a
95/// three-address instruction to avoid clobbering a register. Try to sink it
Bill Wendling06289272008-05-10 00:12:52 +000096/// past the instruction that would kill the above mentioned register to reduce
97/// register pressure.
Evan Cheng0c85fe62008-03-13 06:37:55 +000098bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
99 MachineInstr *MI, unsigned SavedReg,
100 MachineBasicBlock::iterator OldPos) {
101 // Check if it's safe to move this instruction.
102 bool SeenStore = true; // Be conservative.
103 if (!MI->isSafeToMove(TII, SeenStore))
104 return false;
105
106 unsigned DefReg = 0;
107 SmallSet<unsigned, 4> UseRegs;
Bill Wendling06289272008-05-10 00:12:52 +0000108
Evan Cheng0c85fe62008-03-13 06:37:55 +0000109 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
110 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000111 if (!MO.isReg())
Evan Cheng0c85fe62008-03-13 06:37:55 +0000112 continue;
113 unsigned MOReg = MO.getReg();
114 if (!MOReg)
115 continue;
116 if (MO.isUse() && MOReg != SavedReg)
117 UseRegs.insert(MO.getReg());
118 if (!MO.isDef())
119 continue;
120 if (MO.isImplicit())
121 // Don't try to move it if it implicitly defines a register.
122 return false;
123 if (DefReg)
124 // For now, don't move any instructions that define multiple registers.
125 return false;
126 DefReg = MO.getReg();
127 }
128
129 // Find the instruction that kills SavedReg.
130 MachineInstr *KillMI = NULL;
131 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 Wendling06289272008-05-10 00:12:52 +0000139
Evan Cheng0c85fe62008-03-13 06:37:55 +0000140 if (!KillMI || KillMI->getParent() != MBB)
141 return false;
142
Bill Wendling06289272008-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
Evan Cheng990dd2b2008-06-18 07:49:14 +0000147 // instruction is before or after another instruction. Then we can use
Bill Wendling06289272008-05-10 00:12:52 +0000148 // MachineRegisterInfo def / use instead.
Evan Cheng0c85fe62008-03-13 06:37:55 +0000149 MachineOperand *KillMO = NULL;
150 MachineBasicBlock::iterator KillPos = KillMI;
151 ++KillPos;
Bill Wendling06289272008-05-10 00:12:52 +0000152
Evan Cheng990dd2b2008-06-18 07:49:14 +0000153 unsigned NumVisited = 0;
Evan Cheng0c85fe62008-03-13 06:37:55 +0000154 for (MachineBasicBlock::iterator I = next(OldPos); I != KillPos; ++I) {
155 MachineInstr *OtherMI = I;
Evan Cheng990dd2b2008-06-18 07:49:14 +0000156 if (NumVisited > 30) // FIXME: Arbitrary limit to reduce compile time cost.
157 return false;
158 ++NumVisited;
Evan Cheng0c85fe62008-03-13 06:37:55 +0000159 for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
160 MachineOperand &MO = OtherMI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000161 if (!MO.isReg())
Evan Cheng0c85fe62008-03-13 06:37:55 +0000162 continue;
163 unsigned MOReg = MO.getReg();
164 if (!MOReg)
165 continue;
166 if (DefReg == MOReg)
167 return false;
Bill Wendling06289272008-05-10 00:12:52 +0000168
Evan Cheng0c85fe62008-03-13 06:37:55 +0000169 if (MO.isKill()) {
170 if (OtherMI == KillMI && MOReg == SavedReg)
Evan Cheng990dd2b2008-06-18 07:49:14 +0000171 // Save the operand that kills the register. We want to unset the kill
172 // marker if we can sink MI past it.
Evan Cheng0c85fe62008-03-13 06:37:55 +0000173 KillMO = &MO;
174 else if (UseRegs.count(MOReg))
175 // One of the uses is killed before the destination.
176 return false;
177 }
178 }
179 }
180
Evan Cheng0c85fe62008-03-13 06:37:55 +0000181 // Update kill and LV information.
182 KillMO->setIsKill(false);
183 KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI);
184 KillMO->setIsKill(true);
Owen Andersonb9232172008-07-02 21:28:58 +0000185
Evan Chenge52c1912008-07-03 09:09:37 +0000186 if (LV)
187 LV->replaceKillInstruction(SavedReg, KillMI, MI);
Evan Cheng0c85fe62008-03-13 06:37:55 +0000188
189 // Move instruction to its destination.
190 MBB->remove(MI);
191 MBB->insert(KillPos, MI);
192
193 ++Num3AddrSunk;
194 return true;
195}
196
Evan Cheng990dd2b2008-06-18 07:49:14 +0000197/// isTwoAddrUse - Return true if the specified MI is using the specified
198/// register as a two-address operand.
199static bool isTwoAddrUse(MachineInstr *UseMI, unsigned Reg) {
200 const TargetInstrDesc &TID = UseMI->getDesc();
201 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
202 MachineOperand &MO = UseMI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000203 if (MO.isReg() && MO.getReg() == Reg &&
Evan Cheng990dd2b2008-06-18 07:49:14 +0000204 (MO.isDef() || TID.getOperandConstraint(i, TOI::TIED_TO) != -1))
205 // Earlier use is a two-address one.
206 return true;
207 }
208 return false;
209}
210
211/// isProfitableToReMat - Return true if the heuristics determines it is likely
212/// to be profitable to re-materialize the definition of Reg rather than copy
213/// the register.
214bool
215TwoAddressInstructionPass::isProfitableToReMat(unsigned Reg,
216 const TargetRegisterClass *RC,
Evan Chengd2b9d302008-06-25 01:16:38 +0000217 MachineInstr *MI, MachineInstr *DefMI,
218 MachineBasicBlock *MBB, unsigned Loc,
219 DenseMap<MachineInstr*, unsigned> &DistanceMap){
Evan Cheng990dd2b2008-06-18 07:49:14 +0000220 bool OtherUse = false;
221 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg),
222 UE = MRI->use_end(); UI != UE; ++UI) {
223 MachineOperand &UseMO = UI.getOperand();
224 if (!UseMO.isUse())
225 continue;
226 MachineInstr *UseMI = UseMO.getParent();
Evan Chengd2b9d302008-06-25 01:16:38 +0000227 MachineBasicBlock *UseMBB = UseMI->getParent();
228 if (UseMBB == MBB) {
229 DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI);
230 if (DI != DistanceMap.end() && DI->second == Loc)
231 continue; // Current use.
232 OtherUse = true;
233 // There is at least one other use in the MBB that will clobber the
234 // register.
235 if (isTwoAddrUse(UseMI, Reg))
236 return true;
237 }
Evan Cheng990dd2b2008-06-18 07:49:14 +0000238 }
Evan Chengd2b9d302008-06-25 01:16:38 +0000239
240 // If other uses in MBB are not two-address uses, then don't remat.
241 if (OtherUse)
242 return false;
243
244 // No other uses in the same block, remat if it's defined in the same
245 // block so it does not unnecessarily extend the live range.
246 return MBB == DefMI->getParent();
Evan Cheng990dd2b2008-06-18 07:49:14 +0000247}
248
Bill Wendling06289272008-05-10 00:12:52 +0000249/// runOnMachineFunction - Reduce two-address instructions to two operands.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250///
251bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
252 DOUT << "Machine Function\n";
253 const TargetMachine &TM = MF.getTarget();
Evan Cheng0c85fe62008-03-13 06:37:55 +0000254 MRI = &MF.getRegInfo();
255 TII = TM.getInstrInfo();
256 TRI = TM.getRegisterInfo();
Owen Andersonb9232172008-07-02 21:28:58 +0000257 LV = getAnalysisToUpdate<LiveVariables>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258
259 bool MadeChange = false;
260
261 DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n";
262 DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
263
Evan Cheng990dd2b2008-06-18 07:49:14 +0000264 // ReMatRegs - Keep track of the registers whose def's are remat'ed.
265 BitVector ReMatRegs;
266 ReMatRegs.resize(MRI->getLastVirtReg()+1);
267
268 // DistanceMap - Keep track the distance of a MI from the start of the
269 // current basic block.
270 DenseMap<MachineInstr*, unsigned> DistanceMap;
Bill Wendling3334b272008-05-26 05:18:34 +0000271
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
273 mbbi != mbbe; ++mbbi) {
Evan Cheng990dd2b2008-06-18 07:49:14 +0000274 unsigned Dist = 0;
275 DistanceMap.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
Evan Cheng478568d2008-03-27 01:27:25 +0000277 mi != me; ) {
278 MachineBasicBlock::iterator nmi = next(mi);
Chris Lattner5b930372008-01-07 07:27:27 +0000279 const TargetInstrDesc &TID = mi->getDesc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 bool FirstTied = true;
Bill Wendling06289272008-05-10 00:12:52 +0000281
Evan Cheng990dd2b2008-06-18 07:49:14 +0000282 DistanceMap.insert(std::make_pair(mi, ++Dist));
Chris Lattner5b930372008-01-07 07:27:27 +0000283 for (unsigned si = 1, e = TID.getNumOperands(); si < e; ++si) {
284 int ti = TID.getOperandConstraint(si, TOI::TIED_TO);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 if (ti == -1)
286 continue;
287
288 if (FirstTied) {
289 ++NumTwoAddressInstrs;
290 DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM));
291 }
Bill Wendling06289272008-05-10 00:12:52 +0000292
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 FirstTied = false;
294
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000295 assert(mi->getOperand(si).isReg() && mi->getOperand(si).getReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 mi->getOperand(si).isUse() && "two address instruction invalid");
297
Bill Wendling06289272008-05-10 00:12:52 +0000298 // If the two operands are the same we just remove the use
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 // and mark the def as def&use, otherwise we have to insert a copy.
300 if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) {
Bill Wendling06289272008-05-10 00:12:52 +0000301 // Rewrite:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 // a = b op c
303 // to:
304 // a = b
305 // a = a op c
306 unsigned regA = mi->getOperand(ti).getReg();
307 unsigned regB = mi->getOperand(si).getReg();
308
Dan Gohman1e57df32008-02-10 18:45:23 +0000309 assert(TargetRegisterInfo::isVirtualRegister(regA) &&
310 TargetRegisterInfo::isVirtualRegister(regB) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 "cannot update physical register live information");
312
313#ifndef NDEBUG
314 // First, verify that we don't have a use of a in the instruction (a =
315 // b + a for example) because our transformation will not work. This
316 // should never occur because we are in SSA form.
317 for (unsigned i = 0; i != mi->getNumOperands(); ++i)
318 assert((int)i == ti ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000319 !mi->getOperand(i).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320 mi->getOperand(i).getReg() != regA);
321#endif
322
323 // If this instruction is not the killing user of B, see if we can
324 // rearrange the code to make it so. Making it the killing user will
325 // allow us to coalesce A and B together, eliminating the copy we are
326 // about to insert.
Evan Chengc7daf1f2008-03-05 00:59:57 +0000327 if (!mi->killsRegister(regB)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 // If this instruction is commutative, check to see if C dies. If
329 // so, swap the B and C operands. This makes the live ranges of A
330 // and C joinable.
331 // FIXME: This code also works for A := B op C instructions.
Chris Lattner5b930372008-01-07 07:27:27 +0000332 if (TID.isCommutable() && mi->getNumOperands() >= 3) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000333 assert(mi->getOperand(3-si).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 "Not a proper commutative instruction!");
335 unsigned regC = mi->getOperand(3-si).getReg();
Bill Wendling06289272008-05-10 00:12:52 +0000336
Evan Chengc7daf1f2008-03-05 00:59:57 +0000337 if (mi->killsRegister(regC)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338 DOUT << "2addr: COMMUTING : " << *mi;
Evan Cheng0c85fe62008-03-13 06:37:55 +0000339 MachineInstr *NewMI = TII->commuteInstruction(mi);
Bill Wendling06289272008-05-10 00:12:52 +0000340
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 if (NewMI == 0) {
342 DOUT << "2addr: COMMUTING FAILED!\n";
343 } else {
344 DOUT << "2addr: COMMUTED TO: " << *NewMI;
345 // If the instruction changed to commute it, update livevar.
346 if (NewMI != mi) {
Evan Chengd1c7e8f2008-07-03 00:07:19 +0000347 if (LV)
Owen Andersonb9232172008-07-02 21:28:58 +0000348 // Update live variables
Evan Chengd1c7e8f2008-07-03 00:07:19 +0000349 LV->replaceKillInstruction(regC, mi, NewMI);
Owen Andersonb9232172008-07-02 21:28:58 +0000350
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 mbbi->insert(mi, NewMI); // Insert the new inst
352 mbbi->erase(mi); // Nuke the old inst.
353 mi = NewMI;
Evan Cheng990dd2b2008-06-18 07:49:14 +0000354 DistanceMap.insert(std::make_pair(NewMI, Dist));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 }
356
357 ++NumCommuted;
358 regB = regC;
359 goto InstructionRearranged;
360 }
361 }
362 }
363
364 // If this instruction is potentially convertible to a true
365 // three-address instruction,
Chris Lattner5b930372008-01-07 07:27:27 +0000366 if (TID.isConvertibleTo3Addr()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 // FIXME: This assumes there are no more operands which are tied
368 // to another register.
369#ifndef NDEBUG
Bill Wendling06289272008-05-10 00:12:52 +0000370 for (unsigned i = si + 1, e = TID.getNumOperands(); i < e; ++i)
Chris Lattner5b930372008-01-07 07:27:27 +0000371 assert(TID.getOperandConstraint(i, TOI::TIED_TO) == -1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372#endif
373
Owen Andersonc6959722008-07-02 23:41:07 +0000374 MachineInstr *NewMI = TII->convertToThreeAddress(mbbi, mi, LV);
Evan Cheng990dd2b2008-06-18 07:49:14 +0000375 if (NewMI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 DOUT << "2addr: CONVERTING 2-ADDR: " << *mi;
Evan Cheng990dd2b2008-06-18 07:49:14 +0000377 DOUT << "2addr: TO 3-ADDR: " << *NewMI;
Evan Chengfdde77b2008-03-13 07:56:58 +0000378 bool Sunk = false;
Bill Wendling06289272008-05-10 00:12:52 +0000379
Evan Cheng990dd2b2008-06-18 07:49:14 +0000380 if (NewMI->findRegisterUseOperand(regB, false, TRI))
Evan Chengfdde77b2008-03-13 07:56:58 +0000381 // FIXME: Temporary workaround. If the new instruction doesn't
382 // uses regB, convertToThreeAddress must have created more
383 // then one instruction.
Evan Cheng990dd2b2008-06-18 07:49:14 +0000384 Sunk = Sink3AddrInstruction(mbbi, NewMI, regB, mi);
Bill Wendling06289272008-05-10 00:12:52 +0000385
386 mbbi->erase(mi); // Nuke the old inst.
387
Evan Cheng478568d2008-03-27 01:27:25 +0000388 if (!Sunk) {
Evan Cheng990dd2b2008-06-18 07:49:14 +0000389 DistanceMap.insert(std::make_pair(NewMI, Dist));
390 mi = NewMI;
Evan Cheng478568d2008-03-27 01:27:25 +0000391 nmi = next(mi);
392 }
Bill Wendling06289272008-05-10 00:12:52 +0000393
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 ++NumConvertedTo3Addr;
Bill Wendling06289272008-05-10 00:12:52 +0000395 break; // Done with this instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396 }
Evan Cheng8ba2af52007-10-20 04:01:47 +0000397 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 }
399
400 InstructionRearranged:
Evan Cheng990dd2b2008-06-18 07:49:14 +0000401 const TargetRegisterClass* rc = MRI->getRegClass(regA);
402 MachineInstr *DefMI = MRI->getVRegDef(regB);
403 // If it's safe and profitable, remat the definition instead of
404 // copying it.
Evan Chengd2b9d302008-06-25 01:16:38 +0000405 if (DefMI &&
Evan Chenga02c6692008-08-27 20:58:54 +0000406 DefMI->getDesc().isAsCheapAsAMove() &&
Evan Cheng75e2cee2008-08-27 20:33:50 +0000407 DefMI->isSafeToReMat(TII, regB) &&
Evan Chengd2b9d302008-06-25 01:16:38 +0000408 isProfitableToReMat(regB, rc, mi, DefMI, mbbi, Dist,DistanceMap)){
Evan Cheng990dd2b2008-06-18 07:49:14 +0000409 DEBUG(cerr << "2addr: REMATTING : " << *DefMI << "\n");
410 TII->reMaterialize(*mbbi, mi, regA, DefMI);
411 ReMatRegs.set(regB);
412 ++NumReMats;
Bill Wendling3334b272008-05-26 05:18:34 +0000413 } else {
414 TII->copyRegToReg(*mbbi, mi, regA, regB, rc, rc);
415 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416
417 MachineBasicBlock::iterator prevMi = prior(mi);
418 DOUT << "\t\tprepend:\t"; DEBUG(prevMi->print(*cerr.stream(), &TM));
419
Bill Wendling06289272008-05-10 00:12:52 +0000420 // Update live variables for regB.
Owen Andersonb9232172008-07-02 21:28:58 +0000421 if (LV) {
422 LiveVariables::VarInfo& varInfoB = LV->getVarInfo(regB);
Bill Wendling06289272008-05-10 00:12:52 +0000423
Owen Andersonb9232172008-07-02 21:28:58 +0000424 // regB is used in this BB.
425 varInfoB.UsedBlocks[mbbi->getNumber()] = true;
Bill Wendling06289272008-05-10 00:12:52 +0000426
Evan Chenge52c1912008-07-03 09:09:37 +0000427 if (LV->removeVirtualRegisterKilled(regB, mi))
Owen Andersonb9232172008-07-02 21:28:58 +0000428 LV->addVirtualRegisterKilled(regB, prevMi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000429
Evan Chenge52c1912008-07-03 09:09:37 +0000430 if (LV->removeVirtualRegisterDead(regB, mi))
Owen Andersonb9232172008-07-02 21:28:58 +0000431 LV->addVirtualRegisterDead(regB, prevMi);
Owen Andersonb9232172008-07-02 21:28:58 +0000432 }
433
Bill Wendling06289272008-05-10 00:12:52 +0000434 // Replace all occurences of regB with regA.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435 for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000436 if (mi->getOperand(i).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 mi->getOperand(i).getReg() == regB)
438 mi->getOperand(i).setReg(regA);
439 }
440 }
441
442 assert(mi->getOperand(ti).isDef() && mi->getOperand(si).isUse());
443 mi->getOperand(ti).setReg(mi->getOperand(si).getReg());
444 MadeChange = true;
445
446 DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(*cerr.stream(), &TM));
447 }
Bill Wendling06289272008-05-10 00:12:52 +0000448
Evan Cheng478568d2008-03-27 01:27:25 +0000449 mi = nmi;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 }
451 }
452
Evan Chengd2b9d302008-06-25 01:16:38 +0000453 // Some remat'ed instructions are dead.
454 int VReg = ReMatRegs.find_first();
455 while (VReg != -1) {
456 if (MRI->use_empty(VReg)) {
457 MachineInstr *DefMI = MRI->getVRegDef(VReg);
458 DefMI->eraseFromParent();
Bill Wendlingc3852fc2008-05-26 05:49:49 +0000459 }
Evan Chengd2b9d302008-06-25 01:16:38 +0000460 VReg = ReMatRegs.find_next(VReg);
Bill Wendling3334b272008-05-26 05:18:34 +0000461 }
462
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 return MadeChange;
464}