blob: 08f576c956fc84ca8bf5c6d9686085787c8c4fa9 [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"
Evan Cheng7543e582008-06-18 07:49:14 +000042#include "llvm/ADT/BitVector.h"
43#include "llvm/ADT/DenseMap.h"
Bill Wendling48f7f232008-05-26 05:18:34 +000044#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000045#include "llvm/ADT/Statistic.h"
46#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000047using namespace llvm;
48
Chris Lattnercd3245a2006-12-19 22:41:21 +000049STATISTIC(NumTwoAddressInstrs, "Number of two-address instructions");
50STATISTIC(NumCommuted , "Number of instructions commuted to coalesce");
51STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address");
Evan Cheng875357d2008-03-13 06:37:55 +000052STATISTIC(Num3AddrSunk, "Number of 3-address instructions sunk");
Evan Cheng7543e582008-06-18 07:49:14 +000053STATISTIC(NumReMats, "Number of instructions re-materialized");
Evan Cheng875357d2008-03-13 06:37:55 +000054
55namespace {
Bill Wendling637980e2008-05-10 00:12:52 +000056 class VISIBILITY_HIDDEN TwoAddressInstructionPass
57 : public MachineFunctionPass {
Evan Cheng875357d2008-03-13 06:37:55 +000058 const TargetInstrInfo *TII;
59 const TargetRegisterInfo *TRI;
60 MachineRegisterInfo *MRI;
61 LiveVariables *LV;
62
Bill Wendling637980e2008-05-10 00:12:52 +000063 bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI,
64 unsigned Reg,
65 MachineBasicBlock::iterator OldPos);
Evan Cheng7543e582008-06-18 07:49:14 +000066
67 bool isSafeToReMat(unsigned DstReg, MachineInstr *MI);
68 bool isProfitableToReMat(unsigned Reg, const TargetRegisterClass *RC,
Evan Cheng601ca4b2008-06-25 01:16:38 +000069 MachineInstr *MI, MachineInstr *DefMI,
70 MachineBasicBlock *MBB, unsigned Loc,
Evan Cheng7543e582008-06-18 07:49:14 +000071 DenseMap<MachineInstr*, unsigned> &DistanceMap);
Evan Cheng875357d2008-03-13 06:37:55 +000072 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000073 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000074 TwoAddressInstructionPass() : MachineFunctionPass((intptr_t)&ID) {}
75
Bill Wendling637980e2008-05-10 00:12:52 +000076 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Bill Wendling637980e2008-05-10 00:12:52 +000077 AU.addPreserved<LiveVariables>();
78 AU.addPreservedID(MachineLoopInfoID);
79 AU.addPreservedID(MachineDominatorsID);
80 AU.addPreservedID(PHIEliminationID);
81 MachineFunctionPass::getAnalysisUsage(AU);
82 }
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000083
Bill Wendling637980e2008-05-10 00:12:52 +000084 /// runOnMachineFunction - Pass entry point.
Misha Brukman75fa4e42004-07-22 15:26:23 +000085 bool runOnMachineFunction(MachineFunction&);
86 };
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000087}
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000088
Dan Gohman844731a2008-05-13 00:00:25 +000089char TwoAddressInstructionPass::ID = 0;
90static RegisterPass<TwoAddressInstructionPass>
91X("twoaddressinstruction", "Two-Address instruction pass");
92
Dan Gohman6ddba2b2008-05-13 02:05:11 +000093const PassInfo *const llvm::TwoAddressInstructionPassID = &X;
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000094
Evan Cheng875357d2008-03-13 06:37:55 +000095/// Sink3AddrInstruction - A two-address instruction has been converted to a
96/// three-address instruction to avoid clobbering a register. Try to sink it
Bill Wendling637980e2008-05-10 00:12:52 +000097/// past the instruction that would kill the above mentioned register to reduce
98/// register pressure.
Evan Cheng875357d2008-03-13 06:37:55 +000099bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
100 MachineInstr *MI, unsigned SavedReg,
101 MachineBasicBlock::iterator OldPos) {
102 // Check if it's safe to move this instruction.
103 bool SeenStore = true; // Be conservative.
104 if (!MI->isSafeToMove(TII, SeenStore))
105 return false;
106
107 unsigned DefReg = 0;
108 SmallSet<unsigned, 4> UseRegs;
Bill Wendling637980e2008-05-10 00:12:52 +0000109
Evan Cheng875357d2008-03-13 06:37:55 +0000110 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
111 const MachineOperand &MO = MI->getOperand(i);
112 if (!MO.isRegister())
113 continue;
114 unsigned MOReg = MO.getReg();
115 if (!MOReg)
116 continue;
117 if (MO.isUse() && MOReg != SavedReg)
118 UseRegs.insert(MO.getReg());
119 if (!MO.isDef())
120 continue;
121 if (MO.isImplicit())
122 // Don't try to move it if it implicitly defines a register.
123 return false;
124 if (DefReg)
125 // For now, don't move any instructions that define multiple registers.
126 return false;
127 DefReg = MO.getReg();
128 }
129
130 // Find the instruction that kills SavedReg.
131 MachineInstr *KillMI = NULL;
132 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SavedReg),
133 UE = MRI->use_end(); UI != UE; ++UI) {
134 MachineOperand &UseMO = UI.getOperand();
135 if (!UseMO.isKill())
136 continue;
137 KillMI = UseMO.getParent();
138 break;
139 }
Bill Wendling637980e2008-05-10 00:12:52 +0000140
Evan Cheng875357d2008-03-13 06:37:55 +0000141 if (!KillMI || KillMI->getParent() != MBB)
142 return false;
143
Bill Wendling637980e2008-05-10 00:12:52 +0000144 // If any of the definitions are used by another instruction between the
145 // position and the kill use, then it's not safe to sink it.
146 //
147 // FIXME: This can be sped up if there is an easy way to query whether an
Evan Cheng7543e582008-06-18 07:49:14 +0000148 // instruction is before or after another instruction. Then we can use
Bill Wendling637980e2008-05-10 00:12:52 +0000149 // MachineRegisterInfo def / use instead.
Evan Cheng875357d2008-03-13 06:37:55 +0000150 MachineOperand *KillMO = NULL;
151 MachineBasicBlock::iterator KillPos = KillMI;
152 ++KillPos;
Bill Wendling637980e2008-05-10 00:12:52 +0000153
Evan Cheng7543e582008-06-18 07:49:14 +0000154 unsigned NumVisited = 0;
Evan Cheng875357d2008-03-13 06:37:55 +0000155 for (MachineBasicBlock::iterator I = next(OldPos); I != KillPos; ++I) {
156 MachineInstr *OtherMI = I;
Evan Cheng7543e582008-06-18 07:49:14 +0000157 if (NumVisited > 30) // FIXME: Arbitrary limit to reduce compile time cost.
158 return false;
159 ++NumVisited;
Evan Cheng875357d2008-03-13 06:37:55 +0000160 for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
161 MachineOperand &MO = OtherMI->getOperand(i);
162 if (!MO.isRegister())
163 continue;
164 unsigned MOReg = MO.getReg();
165 if (!MOReg)
166 continue;
167 if (DefReg == MOReg)
168 return false;
Bill Wendling637980e2008-05-10 00:12:52 +0000169
Evan Cheng875357d2008-03-13 06:37:55 +0000170 if (MO.isKill()) {
171 if (OtherMI == KillMI && MOReg == SavedReg)
Evan Cheng7543e582008-06-18 07:49:14 +0000172 // Save the operand that kills the register. We want to unset the kill
173 // marker if we can sink MI past it.
Evan Cheng875357d2008-03-13 06:37:55 +0000174 KillMO = &MO;
175 else if (UseRegs.count(MOReg))
176 // One of the uses is killed before the destination.
177 return false;
178 }
179 }
180 }
181
Evan Cheng875357d2008-03-13 06:37:55 +0000182 // Update kill and LV information.
183 KillMO->setIsKill(false);
184 KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI);
185 KillMO->setIsKill(true);
Owen Anderson802af112008-07-02 21:28:58 +0000186
Evan Cheng9f1c8312008-07-03 09:09:37 +0000187 if (LV)
188 LV->replaceKillInstruction(SavedReg, KillMI, MI);
Evan Cheng875357d2008-03-13 06:37:55 +0000189
190 // Move instruction to its destination.
191 MBB->remove(MI);
192 MBB->insert(KillPos, MI);
193
194 ++Num3AddrSunk;
195 return true;
196}
197
Evan Cheng7543e582008-06-18 07:49:14 +0000198/// isSafeToReMat - Return true if it's safe to rematerialize the specified
199/// instruction which defined the specified register instead of copying it.
200bool
201TwoAddressInstructionPass::isSafeToReMat(unsigned DstReg, MachineInstr *MI) {
202 const TargetInstrDesc &TID = MI->getDesc();
203 if (!TID.isAsCheapAsAMove())
204 return false;
205 bool SawStore = false;
206 if (!MI->isSafeToMove(TII, SawStore))
207 return false;
208 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
209 MachineOperand &MO = MI->getOperand(i);
210 if (!MO.isRegister())
211 continue;
212 // FIXME: For now, do not remat any instruction with register operands.
213 // Later on, we can loosen the restriction is the register operands have
214 // not been modified between the def and use. Note, this is different from
215 // MachineSink because the code in no longer in two-address form (at least
216 // partially).
217 if (MO.isUse())
218 return false;
219 else if (!MO.isDead() && MO.getReg() != DstReg)
220 return false;
221 }
222 return true;
223}
224
225/// isTwoAddrUse - Return true if the specified MI is using the specified
226/// register as a two-address operand.
227static bool isTwoAddrUse(MachineInstr *UseMI, unsigned Reg) {
228 const TargetInstrDesc &TID = UseMI->getDesc();
229 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
230 MachineOperand &MO = UseMI->getOperand(i);
Evan Cheng32a3ac72008-06-19 06:17:19 +0000231 if (MO.isRegister() && MO.getReg() == Reg &&
Evan Cheng7543e582008-06-18 07:49:14 +0000232 (MO.isDef() || TID.getOperandConstraint(i, TOI::TIED_TO) != -1))
233 // Earlier use is a two-address one.
234 return true;
235 }
236 return false;
237}
238
239/// isProfitableToReMat - Return true if the heuristics determines it is likely
240/// to be profitable to re-materialize the definition of Reg rather than copy
241/// the register.
242bool
243TwoAddressInstructionPass::isProfitableToReMat(unsigned Reg,
244 const TargetRegisterClass *RC,
Evan Cheng601ca4b2008-06-25 01:16:38 +0000245 MachineInstr *MI, MachineInstr *DefMI,
246 MachineBasicBlock *MBB, unsigned Loc,
247 DenseMap<MachineInstr*, unsigned> &DistanceMap){
Evan Cheng7543e582008-06-18 07:49:14 +0000248 bool OtherUse = false;
249 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg),
250 UE = MRI->use_end(); UI != UE; ++UI) {
251 MachineOperand &UseMO = UI.getOperand();
252 if (!UseMO.isUse())
253 continue;
254 MachineInstr *UseMI = UseMO.getParent();
Evan Cheng601ca4b2008-06-25 01:16:38 +0000255 MachineBasicBlock *UseMBB = UseMI->getParent();
256 if (UseMBB == MBB) {
257 DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI);
258 if (DI != DistanceMap.end() && DI->second == Loc)
259 continue; // Current use.
260 OtherUse = true;
261 // There is at least one other use in the MBB that will clobber the
262 // register.
263 if (isTwoAddrUse(UseMI, Reg))
264 return true;
265 }
Evan Cheng7543e582008-06-18 07:49:14 +0000266 }
Evan Cheng601ca4b2008-06-25 01:16:38 +0000267
268 // If other uses in MBB are not two-address uses, then don't remat.
269 if (OtherUse)
270 return false;
271
272 // No other uses in the same block, remat if it's defined in the same
273 // block so it does not unnecessarily extend the live range.
274 return MBB == DefMI->getParent();
Evan Cheng7543e582008-06-18 07:49:14 +0000275}
276
Bill Wendling637980e2008-05-10 00:12:52 +0000277/// runOnMachineFunction - Reduce two-address instructions to two operands.
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000278///
Chris Lattner163c1e72004-01-31 21:14:04 +0000279bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000280 DOUT << "Machine Function\n";
Misha Brukman75fa4e42004-07-22 15:26:23 +0000281 const TargetMachine &TM = MF.getTarget();
Evan Cheng875357d2008-03-13 06:37:55 +0000282 MRI = &MF.getRegInfo();
283 TII = TM.getInstrInfo();
284 TRI = TM.getRegisterInfo();
Owen Anderson802af112008-07-02 21:28:58 +0000285 LV = getAnalysisToUpdate<LiveVariables>();
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000286
Misha Brukman75fa4e42004-07-22 15:26:23 +0000287 bool MadeChange = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000288
Bill Wendlinga09362e2006-11-28 22:48:48 +0000289 DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n";
290 DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
Alkis Evlogimenos3a9986f2004-02-18 00:35:06 +0000291
Evan Cheng7543e582008-06-18 07:49:14 +0000292 // ReMatRegs - Keep track of the registers whose def's are remat'ed.
293 BitVector ReMatRegs;
294 ReMatRegs.resize(MRI->getLastVirtReg()+1);
295
296 // DistanceMap - Keep track the distance of a MI from the start of the
297 // current basic block.
298 DenseMap<MachineInstr*, unsigned> DistanceMap;
Bill Wendling48f7f232008-05-26 05:18:34 +0000299
Misha Brukman75fa4e42004-07-22 15:26:23 +0000300 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
301 mbbi != mbbe; ++mbbi) {
Evan Cheng7543e582008-06-18 07:49:14 +0000302 unsigned Dist = 0;
303 DistanceMap.clear();
Misha Brukman75fa4e42004-07-22 15:26:23 +0000304 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
Evan Cheng7a963fa2008-03-27 01:27:25 +0000305 mi != me; ) {
306 MachineBasicBlock::iterator nmi = next(mi);
Chris Lattner749c6f62008-01-07 07:27:27 +0000307 const TargetInstrDesc &TID = mi->getDesc();
Evan Cheng360c2dd2006-11-01 23:06:55 +0000308 bool FirstTied = true;
Bill Wendling637980e2008-05-10 00:12:52 +0000309
Evan Cheng7543e582008-06-18 07:49:14 +0000310 DistanceMap.insert(std::make_pair(mi, ++Dist));
Chris Lattner749c6f62008-01-07 07:27:27 +0000311 for (unsigned si = 1, e = TID.getNumOperands(); si < e; ++si) {
312 int ti = TID.getOperandConstraint(si, TOI::TIED_TO);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000313 if (ti == -1)
314 continue;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000315
Evan Cheng360c2dd2006-11-01 23:06:55 +0000316 if (FirstTied) {
317 ++NumTwoAddressInstrs;
Bill Wendlingbcd24982006-12-07 20:28:15 +0000318 DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000319 }
Bill Wendling637980e2008-05-10 00:12:52 +0000320
Evan Cheng360c2dd2006-11-01 23:06:55 +0000321 FirstTied = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000322
Evan Cheng360c2dd2006-11-01 23:06:55 +0000323 assert(mi->getOperand(si).isRegister() && mi->getOperand(si).getReg() &&
324 mi->getOperand(si).isUse() && "two address instruction invalid");
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000325
Bill Wendling637980e2008-05-10 00:12:52 +0000326 // If the two operands are the same we just remove the use
Evan Cheng360c2dd2006-11-01 23:06:55 +0000327 // and mark the def as def&use, otherwise we have to insert a copy.
328 if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) {
Bill Wendling637980e2008-05-10 00:12:52 +0000329 // Rewrite:
Evan Cheng360c2dd2006-11-01 23:06:55 +0000330 // a = b op c
331 // to:
332 // a = b
333 // a = a op c
334 unsigned regA = mi->getOperand(ti).getReg();
335 unsigned regB = mi->getOperand(si).getReg();
336
Dan Gohman6f0d0242008-02-10 18:45:23 +0000337 assert(TargetRegisterInfo::isVirtualRegister(regA) &&
338 TargetRegisterInfo::isVirtualRegister(regB) &&
Evan Cheng360c2dd2006-11-01 23:06:55 +0000339 "cannot update physical register live information");
Chris Lattner6b507672004-01-31 21:21:43 +0000340
Chris Lattner1e313632004-07-21 23:17:57 +0000341#ifndef NDEBUG
Evan Cheng360c2dd2006-11-01 23:06:55 +0000342 // First, verify that we don't have a use of a in the instruction (a =
343 // b + a for example) because our transformation will not work. This
344 // should never occur because we are in SSA form.
345 for (unsigned i = 0; i != mi->getNumOperands(); ++i)
346 assert((int)i == ti ||
347 !mi->getOperand(i).isRegister() ||
348 mi->getOperand(i).getReg() != regA);
Chris Lattner1e313632004-07-21 23:17:57 +0000349#endif
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000350
Evan Cheng360c2dd2006-11-01 23:06:55 +0000351 // If this instruction is not the killing user of B, see if we can
352 // rearrange the code to make it so. Making it the killing user will
353 // allow us to coalesce A and B together, eliminating the copy we are
354 // about to insert.
Evan Cheng6130f662008-03-05 00:59:57 +0000355 if (!mi->killsRegister(regB)) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000356 // If this instruction is commutative, check to see if C dies. If
357 // so, swap the B and C operands. This makes the live ranges of A
358 // and C joinable.
359 // FIXME: This code also works for A := B op C instructions.
Chris Lattner749c6f62008-01-07 07:27:27 +0000360 if (TID.isCommutable() && mi->getNumOperands() >= 3) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000361 assert(mi->getOperand(3-si).isRegister() &&
362 "Not a proper commutative instruction!");
363 unsigned regC = mi->getOperand(3-si).getReg();
Bill Wendling637980e2008-05-10 00:12:52 +0000364
Evan Cheng6130f662008-03-05 00:59:57 +0000365 if (mi->killsRegister(regC)) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000366 DOUT << "2addr: COMMUTING : " << *mi;
Evan Cheng875357d2008-03-13 06:37:55 +0000367 MachineInstr *NewMI = TII->commuteInstruction(mi);
Bill Wendling637980e2008-05-10 00:12:52 +0000368
Evan Cheng360c2dd2006-11-01 23:06:55 +0000369 if (NewMI == 0) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000370 DOUT << "2addr: COMMUTING FAILED!\n";
Evan Cheng360c2dd2006-11-01 23:06:55 +0000371 } else {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000372 DOUT << "2addr: COMMUTED TO: " << *NewMI;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000373 // If the instruction changed to commute it, update livevar.
374 if (NewMI != mi) {
Evan Chengbe04dc12008-07-03 00:07:19 +0000375 if (LV)
Owen Anderson802af112008-07-02 21:28:58 +0000376 // Update live variables
Evan Chengbe04dc12008-07-03 00:07:19 +0000377 LV->replaceKillInstruction(regC, mi, NewMI);
Owen Anderson802af112008-07-02 21:28:58 +0000378
Evan Cheng360c2dd2006-11-01 23:06:55 +0000379 mbbi->insert(mi, NewMI); // Insert the new inst
380 mbbi->erase(mi); // Nuke the old inst.
381 mi = NewMI;
Evan Cheng7543e582008-06-18 07:49:14 +0000382 DistanceMap.insert(std::make_pair(NewMI, Dist));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000383 }
384
385 ++NumCommuted;
386 regB = regC;
387 goto InstructionRearranged;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000388 }
Chris Lattnerc71d6942005-01-19 07:08:42 +0000389 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000390 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000391
392 // If this instruction is potentially convertible to a true
393 // three-address instruction,
Chris Lattner749c6f62008-01-07 07:27:27 +0000394 if (TID.isConvertibleTo3Addr()) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000395 // FIXME: This assumes there are no more operands which are tied
396 // to another register.
397#ifndef NDEBUG
Bill Wendling637980e2008-05-10 00:12:52 +0000398 for (unsigned i = si + 1, e = TID.getNumOperands(); i < e; ++i)
Chris Lattner749c6f62008-01-07 07:27:27 +0000399 assert(TID.getOperandConstraint(i, TOI::TIED_TO) == -1);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000400#endif
401
Owen Andersonf660c172008-07-02 23:41:07 +0000402 MachineInstr *NewMI = TII->convertToThreeAddress(mbbi, mi, LV);
Evan Cheng7543e582008-06-18 07:49:14 +0000403 if (NewMI) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000404 DOUT << "2addr: CONVERTING 2-ADDR: " << *mi;
Evan Cheng7543e582008-06-18 07:49:14 +0000405 DOUT << "2addr: TO 3-ADDR: " << *NewMI;
Evan Cheng0099ae22008-03-13 07:56:58 +0000406 bool Sunk = false;
Bill Wendling637980e2008-05-10 00:12:52 +0000407
Evan Cheng7543e582008-06-18 07:49:14 +0000408 if (NewMI->findRegisterUseOperand(regB, false, TRI))
Evan Cheng0099ae22008-03-13 07:56:58 +0000409 // FIXME: Temporary workaround. If the new instruction doesn't
410 // uses regB, convertToThreeAddress must have created more
411 // then one instruction.
Evan Cheng7543e582008-06-18 07:49:14 +0000412 Sunk = Sink3AddrInstruction(mbbi, NewMI, regB, mi);
Bill Wendling637980e2008-05-10 00:12:52 +0000413
414 mbbi->erase(mi); // Nuke the old inst.
415
Evan Cheng7a963fa2008-03-27 01:27:25 +0000416 if (!Sunk) {
Evan Cheng7543e582008-06-18 07:49:14 +0000417 DistanceMap.insert(std::make_pair(NewMI, Dist));
418 mi = NewMI;
Evan Cheng7a963fa2008-03-27 01:27:25 +0000419 nmi = next(mi);
420 }
Bill Wendling637980e2008-05-10 00:12:52 +0000421
Evan Cheng360c2dd2006-11-01 23:06:55 +0000422 ++NumConvertedTo3Addr;
Bill Wendling637980e2008-05-10 00:12:52 +0000423 break; // Done with this instruction.
Evan Cheng360c2dd2006-11-01 23:06:55 +0000424 }
Evan Chengb9d5e7c2007-10-20 04:01:47 +0000425 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000426 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000427
428 InstructionRearranged:
Evan Cheng7543e582008-06-18 07:49:14 +0000429 const TargetRegisterClass* rc = MRI->getRegClass(regA);
430 MachineInstr *DefMI = MRI->getVRegDef(regB);
431 // If it's safe and profitable, remat the definition instead of
432 // copying it.
Evan Cheng601ca4b2008-06-25 01:16:38 +0000433 if (DefMI &&
Evan Cheng7543e582008-06-18 07:49:14 +0000434 isSafeToReMat(regB, DefMI) &&
Evan Cheng601ca4b2008-06-25 01:16:38 +0000435 isProfitableToReMat(regB, rc, mi, DefMI, mbbi, Dist,DistanceMap)){
Evan Cheng7543e582008-06-18 07:49:14 +0000436 DEBUG(cerr << "2addr: REMATTING : " << *DefMI << "\n");
437 TII->reMaterialize(*mbbi, mi, regA, DefMI);
438 ReMatRegs.set(regB);
439 ++NumReMats;
Bill Wendling48f7f232008-05-26 05:18:34 +0000440 } else {
441 TII->copyRegToReg(*mbbi, mi, regA, regB, rc, rc);
442 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000443
444 MachineBasicBlock::iterator prevMi = prior(mi);
Bill Wendlingbcd24982006-12-07 20:28:15 +0000445 DOUT << "\t\tprepend:\t"; DEBUG(prevMi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000446
Bill Wendling637980e2008-05-10 00:12:52 +0000447 // Update live variables for regB.
Owen Anderson802af112008-07-02 21:28:58 +0000448 if (LV) {
449 LiveVariables::VarInfo& varInfoB = LV->getVarInfo(regB);
Bill Wendling637980e2008-05-10 00:12:52 +0000450
Owen Anderson802af112008-07-02 21:28:58 +0000451 // regB is used in this BB.
452 varInfoB.UsedBlocks[mbbi->getNumber()] = true;
Bill Wendling637980e2008-05-10 00:12:52 +0000453
Evan Cheng9f1c8312008-07-03 09:09:37 +0000454 if (LV->removeVirtualRegisterKilled(regB, mi))
Owen Anderson802af112008-07-02 21:28:58 +0000455 LV->addVirtualRegisterKilled(regB, prevMi);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000456
Evan Cheng9f1c8312008-07-03 09:09:37 +0000457 if (LV->removeVirtualRegisterDead(regB, mi))
Owen Anderson802af112008-07-02 21:28:58 +0000458 LV->addVirtualRegisterDead(regB, prevMi);
Owen Anderson802af112008-07-02 21:28:58 +0000459 }
460
Bill Wendling637980e2008-05-10 00:12:52 +0000461 // Replace all occurences of regB with regA.
Evan Cheng360c2dd2006-11-01 23:06:55 +0000462 for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
463 if (mi->getOperand(i).isRegister() &&
464 mi->getOperand(i).getReg() == regB)
465 mi->getOperand(i).setReg(regA);
466 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000467 }
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000468
Evan Cheng360c2dd2006-11-01 23:06:55 +0000469 assert(mi->getOperand(ti).isDef() && mi->getOperand(si).isUse());
470 mi->getOperand(ti).setReg(mi->getOperand(si).getReg());
471 MadeChange = true;
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000472
Bill Wendlingbcd24982006-12-07 20:28:15 +0000473 DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(*cerr.stream(), &TM));
Misha Brukman75fa4e42004-07-22 15:26:23 +0000474 }
Bill Wendling637980e2008-05-10 00:12:52 +0000475
Evan Cheng7a963fa2008-03-27 01:27:25 +0000476 mi = nmi;
Misha Brukman75fa4e42004-07-22 15:26:23 +0000477 }
478 }
479
Evan Cheng601ca4b2008-06-25 01:16:38 +0000480 // Some remat'ed instructions are dead.
481 int VReg = ReMatRegs.find_first();
482 while (VReg != -1) {
483 if (MRI->use_empty(VReg)) {
484 MachineInstr *DefMI = MRI->getVRegDef(VReg);
485 DefMI->eraseFromParent();
Bill Wendlinga16157a2008-05-26 05:49:49 +0000486 }
Evan Cheng601ca4b2008-06-25 01:16:38 +0000487 VReg = ReMatRegs.find_next(VReg);
Bill Wendling48f7f232008-05-26 05:18:34 +0000488 }
489
Misha Brukman75fa4e42004-07-22 15:26:23 +0000490 return MadeChange;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000491}