blob: d680a98a7fd254e70717d47c477158edbd40622f [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"
Evan Cheng7543e582008-06-18 07:49:14 +000043#include "llvm/ADT/BitVector.h"
44#include "llvm/ADT/DenseMap.h"
Bill Wendling48f7f232008-05-26 05:18:34 +000045#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000046#include "llvm/ADT/Statistic.h"
47#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000048using namespace llvm;
49
Chris Lattnercd3245a2006-12-19 22:41:21 +000050STATISTIC(NumTwoAddressInstrs, "Number of two-address instructions");
51STATISTIC(NumCommuted , "Number of instructions commuted to coalesce");
52STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address");
Evan Cheng875357d2008-03-13 06:37:55 +000053STATISTIC(Num3AddrSunk, "Number of 3-address instructions sunk");
Evan Cheng7543e582008-06-18 07:49:14 +000054STATISTIC(NumReMats, "Number of instructions re-materialized");
Evan Cheng875357d2008-03-13 06:37:55 +000055
Bill Wendlinga16157a2008-05-26 05:49:49 +000056static cl::opt<bool>
Evan Cheng7543e582008-06-18 07:49:14 +000057EnableReMat("two-addr-remat", cl::init(false), cl::Hidden,
Bill Wendlinga16157a2008-05-26 05:49:49 +000058 cl::desc("Two-addr conversion should remat when possible."));
59
Evan Cheng875357d2008-03-13 06:37:55 +000060namespace {
Bill Wendling637980e2008-05-10 00:12:52 +000061 class VISIBILITY_HIDDEN TwoAddressInstructionPass
62 : public MachineFunctionPass {
Evan Cheng875357d2008-03-13 06:37:55 +000063 const TargetInstrInfo *TII;
64 const TargetRegisterInfo *TRI;
65 MachineRegisterInfo *MRI;
66 LiveVariables *LV;
67
Bill Wendling637980e2008-05-10 00:12:52 +000068 bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI,
69 unsigned Reg,
70 MachineBasicBlock::iterator OldPos);
Evan Cheng7543e582008-06-18 07:49:14 +000071
72 bool isSafeToReMat(unsigned DstReg, MachineInstr *MI);
73 bool isProfitableToReMat(unsigned Reg, const TargetRegisterClass *RC,
74 MachineInstr *MI, unsigned Loc,
75 MachineInstr *DefMI, MachineBasicBlock *MBB,
76 DenseMap<MachineInstr*, unsigned> &DistanceMap);
Evan Cheng875357d2008-03-13 06:37:55 +000077 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000078 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000079 TwoAddressInstructionPass() : MachineFunctionPass((intptr_t)&ID) {}
80
Bill Wendling637980e2008-05-10 00:12:52 +000081 virtual void 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 }
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000089
Bill Wendling637980e2008-05-10 00:12:52 +000090 /// runOnMachineFunction - Pass entry point.
Misha Brukman75fa4e42004-07-22 15:26:23 +000091 bool runOnMachineFunction(MachineFunction&);
92 };
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000093}
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000094
Dan Gohman844731a2008-05-13 00:00:25 +000095char TwoAddressInstructionPass::ID = 0;
96static RegisterPass<TwoAddressInstructionPass>
97X("twoaddressinstruction", "Two-Address instruction pass");
98
Dan Gohman6ddba2b2008-05-13 02:05:11 +000099const PassInfo *const llvm::TwoAddressInstructionPassID = &X;
Alkis Evlogimenos4c080862003-12-18 22:40:24 +0000100
Evan Cheng875357d2008-03-13 06:37:55 +0000101/// Sink3AddrInstruction - A two-address instruction has been converted to a
102/// three-address instruction to avoid clobbering a register. Try to sink it
Bill Wendling637980e2008-05-10 00:12:52 +0000103/// past the instruction that would kill the above mentioned register to reduce
104/// register pressure.
Evan Cheng875357d2008-03-13 06:37:55 +0000105bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
106 MachineInstr *MI, unsigned SavedReg,
107 MachineBasicBlock::iterator OldPos) {
108 // Check if it's safe to move this instruction.
109 bool SeenStore = true; // Be conservative.
110 if (!MI->isSafeToMove(TII, SeenStore))
111 return false;
112
113 unsigned DefReg = 0;
114 SmallSet<unsigned, 4> UseRegs;
Bill Wendling637980e2008-05-10 00:12:52 +0000115
Evan Cheng875357d2008-03-13 06:37:55 +0000116 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
117 const MachineOperand &MO = MI->getOperand(i);
118 if (!MO.isRegister())
119 continue;
120 unsigned MOReg = MO.getReg();
121 if (!MOReg)
122 continue;
123 if (MO.isUse() && MOReg != SavedReg)
124 UseRegs.insert(MO.getReg());
125 if (!MO.isDef())
126 continue;
127 if (MO.isImplicit())
128 // Don't try to move it if it implicitly defines a register.
129 return false;
130 if (DefReg)
131 // For now, don't move any instructions that define multiple registers.
132 return false;
133 DefReg = MO.getReg();
134 }
135
136 // Find the instruction that kills SavedReg.
137 MachineInstr *KillMI = NULL;
138 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SavedReg),
139 UE = MRI->use_end(); UI != UE; ++UI) {
140 MachineOperand &UseMO = UI.getOperand();
141 if (!UseMO.isKill())
142 continue;
143 KillMI = UseMO.getParent();
144 break;
145 }
Bill Wendling637980e2008-05-10 00:12:52 +0000146
Evan Cheng875357d2008-03-13 06:37:55 +0000147 if (!KillMI || KillMI->getParent() != MBB)
148 return false;
149
Bill Wendling637980e2008-05-10 00:12:52 +0000150 // If any of the definitions are used by another instruction between the
151 // position and the kill use, then it's not safe to sink it.
152 //
153 // FIXME: This can be sped up if there is an easy way to query whether an
Evan Cheng7543e582008-06-18 07:49:14 +0000154 // instruction is before or after another instruction. Then we can use
Bill Wendling637980e2008-05-10 00:12:52 +0000155 // MachineRegisterInfo def / use instead.
Evan Cheng875357d2008-03-13 06:37:55 +0000156 MachineOperand *KillMO = NULL;
157 MachineBasicBlock::iterator KillPos = KillMI;
158 ++KillPos;
Bill Wendling637980e2008-05-10 00:12:52 +0000159
Evan Cheng7543e582008-06-18 07:49:14 +0000160 unsigned NumVisited = 0;
Evan Cheng875357d2008-03-13 06:37:55 +0000161 for (MachineBasicBlock::iterator I = next(OldPos); I != KillPos; ++I) {
162 MachineInstr *OtherMI = I;
Evan Cheng7543e582008-06-18 07:49:14 +0000163 if (NumVisited > 30) // FIXME: Arbitrary limit to reduce compile time cost.
164 return false;
165 ++NumVisited;
Evan Cheng875357d2008-03-13 06:37:55 +0000166 for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
167 MachineOperand &MO = OtherMI->getOperand(i);
168 if (!MO.isRegister())
169 continue;
170 unsigned MOReg = MO.getReg();
171 if (!MOReg)
172 continue;
173 if (DefReg == MOReg)
174 return false;
Bill Wendling637980e2008-05-10 00:12:52 +0000175
Evan Cheng875357d2008-03-13 06:37:55 +0000176 if (MO.isKill()) {
177 if (OtherMI == KillMI && MOReg == SavedReg)
Evan Cheng7543e582008-06-18 07:49:14 +0000178 // Save the operand that kills the register. We want to unset the kill
179 // marker if we can sink MI past it.
Evan Cheng875357d2008-03-13 06:37:55 +0000180 KillMO = &MO;
181 else if (UseRegs.count(MOReg))
182 // One of the uses is killed before the destination.
183 return false;
184 }
185 }
186 }
187
Evan Cheng875357d2008-03-13 06:37:55 +0000188 // Update kill and LV information.
189 KillMO->setIsKill(false);
190 KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI);
191 KillMO->setIsKill(true);
192 LiveVariables::VarInfo& VarInfo = LV->getVarInfo(SavedReg);
193 VarInfo.removeKill(KillMI);
194 VarInfo.Kills.push_back(MI);
195
196 // Move instruction to its destination.
197 MBB->remove(MI);
198 MBB->insert(KillPos, MI);
199
200 ++Num3AddrSunk;
201 return true;
202}
203
Evan Cheng7543e582008-06-18 07:49:14 +0000204/// isSafeToReMat - Return true if it's safe to rematerialize the specified
205/// instruction which defined the specified register instead of copying it.
206bool
207TwoAddressInstructionPass::isSafeToReMat(unsigned DstReg, MachineInstr *MI) {
208 const TargetInstrDesc &TID = MI->getDesc();
209 if (!TID.isAsCheapAsAMove())
210 return false;
211 bool SawStore = false;
212 if (!MI->isSafeToMove(TII, SawStore))
213 return false;
214 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
215 MachineOperand &MO = MI->getOperand(i);
216 if (!MO.isRegister())
217 continue;
218 // FIXME: For now, do not remat any instruction with register operands.
219 // Later on, we can loosen the restriction is the register operands have
220 // not been modified between the def and use. Note, this is different from
221 // MachineSink because the code in no longer in two-address form (at least
222 // partially).
223 if (MO.isUse())
224 return false;
225 else if (!MO.isDead() && MO.getReg() != DstReg)
226 return false;
227 }
228 return true;
229}
230
231/// isTwoAddrUse - Return true if the specified MI is using the specified
232/// register as a two-address operand.
233static bool isTwoAddrUse(MachineInstr *UseMI, unsigned Reg) {
234 const TargetInstrDesc &TID = UseMI->getDesc();
235 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
236 MachineOperand &MO = UseMI->getOperand(i);
237 if (MO.getReg() == Reg &&
238 (MO.isDef() || TID.getOperandConstraint(i, TOI::TIED_TO) != -1))
239 // Earlier use is a two-address one.
240 return true;
241 }
242 return false;
243}
244
245/// isProfitableToReMat - Return true if the heuristics determines it is likely
246/// to be profitable to re-materialize the definition of Reg rather than copy
247/// the register.
248bool
249TwoAddressInstructionPass::isProfitableToReMat(unsigned Reg,
250 const TargetRegisterClass *RC,
251 MachineInstr *MI, unsigned Loc,
252 MachineInstr *DefMI, MachineBasicBlock *MBB,
253 DenseMap<MachineInstr*, unsigned> &DistanceMap) {
254 if (DefMI->getParent() != MBB)
255 return true;
256 // If earlier uses in MBB are not two-address uses, then don't remat.
257 bool OtherUse = false;
258 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg),
259 UE = MRI->use_end(); UI != UE; ++UI) {
260 MachineOperand &UseMO = UI.getOperand();
261 if (!UseMO.isUse())
262 continue;
263 MachineInstr *UseMI = UseMO.getParent();
264 if (UseMI->getParent() != MBB)
265 continue;
266 DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI);
267 if (DI != DistanceMap.end() && DI->second == Loc)
268 continue; // Current use.
269 OtherUse = true;
270 // There is at least one other use in the MBB that will clobber the
271 // register.
272 if (isTwoAddrUse(UseMI, Reg))
273 return true;
274 }
275 return !OtherUse;
276}
277
Bill Wendling637980e2008-05-10 00:12:52 +0000278/// runOnMachineFunction - Reduce two-address instructions to two operands.
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000279///
Chris Lattner163c1e72004-01-31 21:14:04 +0000280bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000281 DOUT << "Machine Function\n";
Misha Brukman75fa4e42004-07-22 15:26:23 +0000282 const TargetMachine &TM = MF.getTarget();
Evan Cheng875357d2008-03-13 06:37:55 +0000283 MRI = &MF.getRegInfo();
284 TII = TM.getInstrInfo();
285 TRI = TM.getRegisterInfo();
286 LV = &getAnalysis<LiveVariables>();
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000287
Misha Brukman75fa4e42004-07-22 15:26:23 +0000288 bool MadeChange = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000289
Bill Wendlinga09362e2006-11-28 22:48:48 +0000290 DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n";
291 DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
Alkis Evlogimenos3a9986f2004-02-18 00:35:06 +0000292
Evan Cheng7543e582008-06-18 07:49:14 +0000293 // ReMatRegs - Keep track of the registers whose def's are remat'ed.
294 BitVector ReMatRegs;
295 ReMatRegs.resize(MRI->getLastVirtReg()+1);
296
297 // DistanceMap - Keep track the distance of a MI from the start of the
298 // current basic block.
299 DenseMap<MachineInstr*, unsigned> DistanceMap;
Bill Wendling48f7f232008-05-26 05:18:34 +0000300
Misha Brukman75fa4e42004-07-22 15:26:23 +0000301 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
302 mbbi != mbbe; ++mbbi) {
Evan Cheng7543e582008-06-18 07:49:14 +0000303 unsigned Dist = 0;
304 DistanceMap.clear();
Misha Brukman75fa4e42004-07-22 15:26:23 +0000305 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
Evan Cheng7a963fa2008-03-27 01:27:25 +0000306 mi != me; ) {
307 MachineBasicBlock::iterator nmi = next(mi);
Chris Lattner749c6f62008-01-07 07:27:27 +0000308 const TargetInstrDesc &TID = mi->getDesc();
Evan Cheng360c2dd2006-11-01 23:06:55 +0000309 bool FirstTied = true;
Bill Wendling637980e2008-05-10 00:12:52 +0000310
Evan Cheng7543e582008-06-18 07:49:14 +0000311 DistanceMap.insert(std::make_pair(mi, ++Dist));
Chris Lattner749c6f62008-01-07 07:27:27 +0000312 for (unsigned si = 1, e = TID.getNumOperands(); si < e; ++si) {
313 int ti = TID.getOperandConstraint(si, TOI::TIED_TO);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000314 if (ti == -1)
315 continue;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000316
Evan Cheng360c2dd2006-11-01 23:06:55 +0000317 if (FirstTied) {
318 ++NumTwoAddressInstrs;
Bill Wendlingbcd24982006-12-07 20:28:15 +0000319 DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000320 }
Bill Wendling637980e2008-05-10 00:12:52 +0000321
Evan Cheng360c2dd2006-11-01 23:06:55 +0000322 FirstTied = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000323
Evan Cheng360c2dd2006-11-01 23:06:55 +0000324 assert(mi->getOperand(si).isRegister() && mi->getOperand(si).getReg() &&
325 mi->getOperand(si).isUse() && "two address instruction invalid");
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000326
Bill Wendling637980e2008-05-10 00:12:52 +0000327 // If the two operands are the same we just remove the use
Evan Cheng360c2dd2006-11-01 23:06:55 +0000328 // and mark the def as def&use, otherwise we have to insert a copy.
329 if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) {
Bill Wendling637980e2008-05-10 00:12:52 +0000330 // Rewrite:
Evan Cheng360c2dd2006-11-01 23:06:55 +0000331 // a = b op c
332 // to:
333 // a = b
334 // a = a op c
335 unsigned regA = mi->getOperand(ti).getReg();
336 unsigned regB = mi->getOperand(si).getReg();
337
Dan Gohman6f0d0242008-02-10 18:45:23 +0000338 assert(TargetRegisterInfo::isVirtualRegister(regA) &&
339 TargetRegisterInfo::isVirtualRegister(regB) &&
Evan Cheng360c2dd2006-11-01 23:06:55 +0000340 "cannot update physical register live information");
Chris Lattner6b507672004-01-31 21:21:43 +0000341
Chris Lattner1e313632004-07-21 23:17:57 +0000342#ifndef NDEBUG
Evan Cheng360c2dd2006-11-01 23:06:55 +0000343 // First, verify that we don't have a use of a in the instruction (a =
344 // b + a for example) because our transformation will not work. This
345 // should never occur because we are in SSA form.
346 for (unsigned i = 0; i != mi->getNumOperands(); ++i)
347 assert((int)i == ti ||
348 !mi->getOperand(i).isRegister() ||
349 mi->getOperand(i).getReg() != regA);
Chris Lattner1e313632004-07-21 23:17:57 +0000350#endif
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000351
Evan Cheng360c2dd2006-11-01 23:06:55 +0000352 // If this instruction is not the killing user of B, see if we can
353 // rearrange the code to make it so. Making it the killing user will
354 // allow us to coalesce A and B together, eliminating the copy we are
355 // about to insert.
Evan Cheng6130f662008-03-05 00:59:57 +0000356 if (!mi->killsRegister(regB)) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000357 // If this instruction is commutative, check to see if C dies. If
358 // so, swap the B and C operands. This makes the live ranges of A
359 // and C joinable.
360 // FIXME: This code also works for A := B op C instructions.
Chris Lattner749c6f62008-01-07 07:27:27 +0000361 if (TID.isCommutable() && mi->getNumOperands() >= 3) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000362 assert(mi->getOperand(3-si).isRegister() &&
363 "Not a proper commutative instruction!");
364 unsigned regC = mi->getOperand(3-si).getReg();
Bill Wendling637980e2008-05-10 00:12:52 +0000365
Evan Cheng6130f662008-03-05 00:59:57 +0000366 if (mi->killsRegister(regC)) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000367 DOUT << "2addr: COMMUTING : " << *mi;
Evan Cheng875357d2008-03-13 06:37:55 +0000368 MachineInstr *NewMI = TII->commuteInstruction(mi);
Bill Wendling637980e2008-05-10 00:12:52 +0000369
Evan Cheng360c2dd2006-11-01 23:06:55 +0000370 if (NewMI == 0) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000371 DOUT << "2addr: COMMUTING FAILED!\n";
Evan Cheng360c2dd2006-11-01 23:06:55 +0000372 } else {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000373 DOUT << "2addr: COMMUTED TO: " << *NewMI;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000374 // If the instruction changed to commute it, update livevar.
375 if (NewMI != mi) {
Evan Cheng875357d2008-03-13 06:37:55 +0000376 LV->instructionChanged(mi, NewMI); // Update live variables
Evan Cheng360c2dd2006-11-01 23:06:55 +0000377 mbbi->insert(mi, NewMI); // Insert the new inst
378 mbbi->erase(mi); // Nuke the old inst.
379 mi = NewMI;
Evan Cheng7543e582008-06-18 07:49:14 +0000380 DistanceMap.insert(std::make_pair(NewMI, Dist));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000381 }
382
383 ++NumCommuted;
384 regB = regC;
385 goto InstructionRearranged;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000386 }
Chris Lattnerc71d6942005-01-19 07:08:42 +0000387 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000388 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000389
390 // If this instruction is potentially convertible to a true
391 // three-address instruction,
Chris Lattner749c6f62008-01-07 07:27:27 +0000392 if (TID.isConvertibleTo3Addr()) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000393 // FIXME: This assumes there are no more operands which are tied
394 // to another register.
395#ifndef NDEBUG
Bill Wendling637980e2008-05-10 00:12:52 +0000396 for (unsigned i = si + 1, e = TID.getNumOperands(); i < e; ++i)
Chris Lattner749c6f62008-01-07 07:27:27 +0000397 assert(TID.getOperandConstraint(i, TOI::TIED_TO) == -1);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000398#endif
399
Evan Cheng7543e582008-06-18 07:49:14 +0000400 MachineInstr *NewMI = TII->convertToThreeAddress(mbbi, mi, *LV);
401 if (NewMI) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000402 DOUT << "2addr: CONVERTING 2-ADDR: " << *mi;
Evan Cheng7543e582008-06-18 07:49:14 +0000403 DOUT << "2addr: TO 3-ADDR: " << *NewMI;
Evan Cheng0099ae22008-03-13 07:56:58 +0000404 bool Sunk = false;
Bill Wendling637980e2008-05-10 00:12:52 +0000405
Evan Cheng7543e582008-06-18 07:49:14 +0000406 if (NewMI->findRegisterUseOperand(regB, false, TRI))
Evan Cheng0099ae22008-03-13 07:56:58 +0000407 // FIXME: Temporary workaround. If the new instruction doesn't
408 // uses regB, convertToThreeAddress must have created more
409 // then one instruction.
Evan Cheng7543e582008-06-18 07:49:14 +0000410 Sunk = Sink3AddrInstruction(mbbi, NewMI, regB, mi);
Bill Wendling637980e2008-05-10 00:12:52 +0000411
412 mbbi->erase(mi); // Nuke the old inst.
413
Evan Cheng7a963fa2008-03-27 01:27:25 +0000414 if (!Sunk) {
Evan Cheng7543e582008-06-18 07:49:14 +0000415 DistanceMap.insert(std::make_pair(NewMI, Dist));
416 mi = NewMI;
Evan Cheng7a963fa2008-03-27 01:27:25 +0000417 nmi = next(mi);
418 }
Bill Wendling637980e2008-05-10 00:12:52 +0000419
Evan Cheng360c2dd2006-11-01 23:06:55 +0000420 ++NumConvertedTo3Addr;
Bill Wendling637980e2008-05-10 00:12:52 +0000421 break; // Done with this instruction.
Evan Cheng360c2dd2006-11-01 23:06:55 +0000422 }
Evan Chengb9d5e7c2007-10-20 04:01:47 +0000423 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000424 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000425
426 InstructionRearranged:
Evan Cheng7543e582008-06-18 07:49:14 +0000427 const TargetRegisterClass* rc = MRI->getRegClass(regA);
428 MachineInstr *DefMI = MRI->getVRegDef(regB);
429 // If it's safe and profitable, remat the definition instead of
430 // copying it.
431 if (EnableReMat && DefMI &&
432 isSafeToReMat(regB, DefMI) &&
433 isProfitableToReMat(regB, rc, mi, Dist, DefMI, mbbi,DistanceMap)){
434 DEBUG(cerr << "2addr: REMATTING : " << *DefMI << "\n");
435 TII->reMaterialize(*mbbi, mi, regA, DefMI);
436 ReMatRegs.set(regB);
437 ++NumReMats;
Bill Wendling48f7f232008-05-26 05:18:34 +0000438 } else {
439 TII->copyRegToReg(*mbbi, mi, regA, regB, rc, rc);
440 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000441
442 MachineBasicBlock::iterator prevMi = prior(mi);
Bill Wendlingbcd24982006-12-07 20:28:15 +0000443 DOUT << "\t\tprepend:\t"; DEBUG(prevMi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000444
Bill Wendling637980e2008-05-10 00:12:52 +0000445 // Update live variables for regB.
Evan Cheng875357d2008-03-13 06:37:55 +0000446 LiveVariables::VarInfo& varInfoB = LV->getVarInfo(regB);
Bill Wendling637980e2008-05-10 00:12:52 +0000447
Owen Andersona0185402007-11-08 01:20:48 +0000448 // regB is used in this BB.
449 varInfoB.UsedBlocks[mbbi->getNumber()] = true;
Bill Wendling637980e2008-05-10 00:12:52 +0000450
Evan Cheng875357d2008-03-13 06:37:55 +0000451 if (LV->removeVirtualRegisterKilled(regB, mbbi, mi))
452 LV->addVirtualRegisterKilled(regB, prevMi);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000453
Evan Cheng875357d2008-03-13 06:37:55 +0000454 if (LV->removeVirtualRegisterDead(regB, mbbi, mi))
455 LV->addVirtualRegisterDead(regB, prevMi);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000456
Bill Wendling637980e2008-05-10 00:12:52 +0000457 // Replace all occurences of regB with regA.
Evan Cheng360c2dd2006-11-01 23:06:55 +0000458 for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
459 if (mi->getOperand(i).isRegister() &&
460 mi->getOperand(i).getReg() == regB)
461 mi->getOperand(i).setReg(regA);
462 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000463 }
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000464
Evan Cheng360c2dd2006-11-01 23:06:55 +0000465 assert(mi->getOperand(ti).isDef() && mi->getOperand(si).isUse());
466 mi->getOperand(ti).setReg(mi->getOperand(si).getReg());
467 MadeChange = true;
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000468
Bill Wendlingbcd24982006-12-07 20:28:15 +0000469 DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(*cerr.stream(), &TM));
Misha Brukman75fa4e42004-07-22 15:26:23 +0000470 }
Bill Wendling637980e2008-05-10 00:12:52 +0000471
Evan Cheng7a963fa2008-03-27 01:27:25 +0000472 mi = nmi;
Misha Brukman75fa4e42004-07-22 15:26:23 +0000473 }
474 }
475
Bill Wendlinga16157a2008-05-26 05:49:49 +0000476 if (EnableReMat) {
Evan Cheng7543e582008-06-18 07:49:14 +0000477 // Some remat'ed instructions are dead.
478 int VReg = ReMatRegs.find_first();
479 while (VReg != -1) {
480 if (MRI->use_empty(VReg)) {
481 MachineInstr *DefMI = MRI->getVRegDef(VReg);
482 DefMI->eraseFromParent();
Bill Wendling48f7f232008-05-26 05:18:34 +0000483 }
Evan Cheng7543e582008-06-18 07:49:14 +0000484 VReg = ReMatRegs.find_next(VReg);
Bill Wendlinga16157a2008-05-26 05:49:49 +0000485 }
Evan Cheng7543e582008-06-18 07:49:14 +0000486
Bill Wendling48f7f232008-05-26 05:18:34 +0000487 }
488
Misha Brukman75fa4e42004-07-22 15:26:23 +0000489 return MadeChange;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000490}