blob: 52a54eb5bd86876a418700207816dfb11edfa874 [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"
Owen Anderson95dad832008-10-07 20:22:28 +000040#include "llvm/Target/TargetOptions.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
56namespace {
Bill Wendling637980e2008-05-10 00:12:52 +000057 class VISIBILITY_HIDDEN TwoAddressInstructionPass
58 : public MachineFunctionPass {
Evan Cheng875357d2008-03-13 06:37:55 +000059 const TargetInstrInfo *TII;
60 const TargetRegisterInfo *TRI;
61 MachineRegisterInfo *MRI;
62 LiveVariables *LV;
63
Bill Wendling637980e2008-05-10 00:12:52 +000064 bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI,
65 unsigned Reg,
66 MachineBasicBlock::iterator OldPos);
Evan Cheng7543e582008-06-18 07:49:14 +000067
Evan Cheng7543e582008-06-18 07:49:14 +000068 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
Dan Gohmanae73dc12008-09-04 17:05:41 +000074 TwoAddressInstructionPass() : MachineFunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000075
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);
Owen Anderson95dad832008-10-07 20:22:28 +000080 if (StrongPHIElim)
81 AU.addPreservedID(StrongPHIEliminationID);
82 else
83 AU.addPreservedID(PHIEliminationID);
Bill Wendling637980e2008-05-10 00:12:52 +000084 MachineFunctionPass::getAnalysisUsage(AU);
85 }
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000086
Bill Wendling637980e2008-05-10 00:12:52 +000087 /// runOnMachineFunction - Pass entry point.
Misha Brukman75fa4e42004-07-22 15:26:23 +000088 bool runOnMachineFunction(MachineFunction&);
89 };
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000090}
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000091
Dan Gohman844731a2008-05-13 00:00:25 +000092char TwoAddressInstructionPass::ID = 0;
93static RegisterPass<TwoAddressInstructionPass>
94X("twoaddressinstruction", "Two-Address instruction pass");
95
Dan Gohman6ddba2b2008-05-13 02:05:11 +000096const PassInfo *const llvm::TwoAddressInstructionPassID = &X;
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000097
Evan Cheng875357d2008-03-13 06:37:55 +000098/// Sink3AddrInstruction - A two-address instruction has been converted to a
99/// three-address instruction to avoid clobbering a register. Try to sink it
Bill Wendling637980e2008-05-10 00:12:52 +0000100/// past the instruction that would kill the above mentioned register to reduce
101/// register pressure.
Evan Cheng875357d2008-03-13 06:37:55 +0000102bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
103 MachineInstr *MI, unsigned SavedReg,
104 MachineBasicBlock::iterator OldPos) {
105 // Check if it's safe to move this instruction.
106 bool SeenStore = true; // Be conservative.
107 if (!MI->isSafeToMove(TII, SeenStore))
108 return false;
109
110 unsigned DefReg = 0;
111 SmallSet<unsigned, 4> UseRegs;
Bill Wendling637980e2008-05-10 00:12:52 +0000112
Evan Cheng875357d2008-03-13 06:37:55 +0000113 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
114 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000115 if (!MO.isReg())
Evan Cheng875357d2008-03-13 06:37:55 +0000116 continue;
117 unsigned MOReg = MO.getReg();
118 if (!MOReg)
119 continue;
120 if (MO.isUse() && MOReg != SavedReg)
121 UseRegs.insert(MO.getReg());
122 if (!MO.isDef())
123 continue;
124 if (MO.isImplicit())
125 // Don't try to move it if it implicitly defines a register.
126 return false;
127 if (DefReg)
128 // For now, don't move any instructions that define multiple registers.
129 return false;
130 DefReg = MO.getReg();
131 }
132
133 // Find the instruction that kills SavedReg.
134 MachineInstr *KillMI = NULL;
135 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SavedReg),
136 UE = MRI->use_end(); UI != UE; ++UI) {
137 MachineOperand &UseMO = UI.getOperand();
138 if (!UseMO.isKill())
139 continue;
140 KillMI = UseMO.getParent();
141 break;
142 }
Bill Wendling637980e2008-05-10 00:12:52 +0000143
Evan Cheng875357d2008-03-13 06:37:55 +0000144 if (!KillMI || KillMI->getParent() != MBB)
145 return false;
146
Bill Wendling637980e2008-05-10 00:12:52 +0000147 // If any of the definitions are used by another instruction between the
148 // position and the kill use, then it's not safe to sink it.
149 //
150 // FIXME: This can be sped up if there is an easy way to query whether an
Evan Cheng7543e582008-06-18 07:49:14 +0000151 // instruction is before or after another instruction. Then we can use
Bill Wendling637980e2008-05-10 00:12:52 +0000152 // MachineRegisterInfo def / use instead.
Evan Cheng875357d2008-03-13 06:37:55 +0000153 MachineOperand *KillMO = NULL;
154 MachineBasicBlock::iterator KillPos = KillMI;
155 ++KillPos;
Bill Wendling637980e2008-05-10 00:12:52 +0000156
Evan Cheng7543e582008-06-18 07:49:14 +0000157 unsigned NumVisited = 0;
Evan Cheng875357d2008-03-13 06:37:55 +0000158 for (MachineBasicBlock::iterator I = next(OldPos); I != KillPos; ++I) {
159 MachineInstr *OtherMI = I;
Evan Cheng7543e582008-06-18 07:49:14 +0000160 if (NumVisited > 30) // FIXME: Arbitrary limit to reduce compile time cost.
161 return false;
162 ++NumVisited;
Evan Cheng875357d2008-03-13 06:37:55 +0000163 for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
164 MachineOperand &MO = OtherMI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000165 if (!MO.isReg())
Evan Cheng875357d2008-03-13 06:37:55 +0000166 continue;
167 unsigned MOReg = MO.getReg();
168 if (!MOReg)
169 continue;
170 if (DefReg == MOReg)
171 return false;
Bill Wendling637980e2008-05-10 00:12:52 +0000172
Evan Cheng875357d2008-03-13 06:37:55 +0000173 if (MO.isKill()) {
174 if (OtherMI == KillMI && MOReg == SavedReg)
Evan Cheng7543e582008-06-18 07:49:14 +0000175 // Save the operand that kills the register. We want to unset the kill
176 // marker if we can sink MI past it.
Evan Cheng875357d2008-03-13 06:37:55 +0000177 KillMO = &MO;
178 else if (UseRegs.count(MOReg))
179 // One of the uses is killed before the destination.
180 return false;
181 }
182 }
183 }
184
Evan Cheng875357d2008-03-13 06:37:55 +0000185 // Update kill and LV information.
186 KillMO->setIsKill(false);
187 KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI);
188 KillMO->setIsKill(true);
Owen Anderson802af112008-07-02 21:28:58 +0000189
Evan Cheng9f1c8312008-07-03 09:09:37 +0000190 if (LV)
191 LV->replaceKillInstruction(SavedReg, KillMI, MI);
Evan Cheng875357d2008-03-13 06:37:55 +0000192
193 // Move instruction to its destination.
194 MBB->remove(MI);
195 MBB->insert(KillPos, MI);
196
197 ++Num3AddrSunk;
198 return true;
199}
200
Evan Cheng7543e582008-06-18 07:49:14 +0000201/// isTwoAddrUse - Return true if the specified MI is using the specified
202/// register as a two-address operand.
203static bool isTwoAddrUse(MachineInstr *UseMI, unsigned Reg) {
204 const TargetInstrDesc &TID = UseMI->getDesc();
205 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
206 MachineOperand &MO = UseMI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000207 if (MO.isReg() && MO.getReg() == Reg &&
Evan Cheng7543e582008-06-18 07:49:14 +0000208 (MO.isDef() || TID.getOperandConstraint(i, TOI::TIED_TO) != -1))
209 // Earlier use is a two-address one.
210 return true;
211 }
212 return false;
213}
214
215/// isProfitableToReMat - Return true if the heuristics determines it is likely
216/// to be profitable to re-materialize the definition of Reg rather than copy
217/// the register.
218bool
219TwoAddressInstructionPass::isProfitableToReMat(unsigned Reg,
220 const TargetRegisterClass *RC,
Evan Cheng601ca4b2008-06-25 01:16:38 +0000221 MachineInstr *MI, MachineInstr *DefMI,
222 MachineBasicBlock *MBB, unsigned Loc,
223 DenseMap<MachineInstr*, unsigned> &DistanceMap){
Evan Cheng7543e582008-06-18 07:49:14 +0000224 bool OtherUse = false;
225 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg),
226 UE = MRI->use_end(); UI != UE; ++UI) {
227 MachineOperand &UseMO = UI.getOperand();
228 if (!UseMO.isUse())
229 continue;
230 MachineInstr *UseMI = UseMO.getParent();
Evan Cheng601ca4b2008-06-25 01:16:38 +0000231 MachineBasicBlock *UseMBB = UseMI->getParent();
232 if (UseMBB == MBB) {
233 DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI);
234 if (DI != DistanceMap.end() && DI->second == Loc)
235 continue; // Current use.
236 OtherUse = true;
237 // There is at least one other use in the MBB that will clobber the
238 // register.
239 if (isTwoAddrUse(UseMI, Reg))
240 return true;
241 }
Evan Cheng7543e582008-06-18 07:49:14 +0000242 }
Evan Cheng601ca4b2008-06-25 01:16:38 +0000243
244 // If other uses in MBB are not two-address uses, then don't remat.
245 if (OtherUse)
246 return false;
247
248 // No other uses in the same block, remat if it's defined in the same
249 // block so it does not unnecessarily extend the live range.
250 return MBB == DefMI->getParent();
Evan Cheng7543e582008-06-18 07:49:14 +0000251}
252
Bill Wendling637980e2008-05-10 00:12:52 +0000253/// runOnMachineFunction - Reduce two-address instructions to two operands.
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000254///
Chris Lattner163c1e72004-01-31 21:14:04 +0000255bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000256 DOUT << "Machine Function\n";
Misha Brukman75fa4e42004-07-22 15:26:23 +0000257 const TargetMachine &TM = MF.getTarget();
Evan Cheng875357d2008-03-13 06:37:55 +0000258 MRI = &MF.getRegInfo();
259 TII = TM.getInstrInfo();
260 TRI = TM.getRegisterInfo();
Owen Anderson802af112008-07-02 21:28:58 +0000261 LV = getAnalysisToUpdate<LiveVariables>();
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000262
Misha Brukman75fa4e42004-07-22 15:26:23 +0000263 bool MadeChange = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000264
Bill Wendlinga09362e2006-11-28 22:48:48 +0000265 DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n";
266 DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
Alkis Evlogimenos3a9986f2004-02-18 00:35:06 +0000267
Evan Cheng7543e582008-06-18 07:49:14 +0000268 // ReMatRegs - Keep track of the registers whose def's are remat'ed.
269 BitVector ReMatRegs;
270 ReMatRegs.resize(MRI->getLastVirtReg()+1);
271
272 // DistanceMap - Keep track the distance of a MI from the start of the
273 // current basic block.
274 DenseMap<MachineInstr*, unsigned> DistanceMap;
Bill Wendling48f7f232008-05-26 05:18:34 +0000275
Misha Brukman75fa4e42004-07-22 15:26:23 +0000276 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
277 mbbi != mbbe; ++mbbi) {
Evan Cheng7543e582008-06-18 07:49:14 +0000278 unsigned Dist = 0;
279 DistanceMap.clear();
Misha Brukman75fa4e42004-07-22 15:26:23 +0000280 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
Evan Cheng7a963fa2008-03-27 01:27:25 +0000281 mi != me; ) {
282 MachineBasicBlock::iterator nmi = next(mi);
Chris Lattner749c6f62008-01-07 07:27:27 +0000283 const TargetInstrDesc &TID = mi->getDesc();
Evan Cheng360c2dd2006-11-01 23:06:55 +0000284 bool FirstTied = true;
Bill Wendling637980e2008-05-10 00:12:52 +0000285
Evan Cheng7543e582008-06-18 07:49:14 +0000286 DistanceMap.insert(std::make_pair(mi, ++Dist));
Chris Lattner749c6f62008-01-07 07:27:27 +0000287 for (unsigned si = 1, e = TID.getNumOperands(); si < e; ++si) {
288 int ti = TID.getOperandConstraint(si, TOI::TIED_TO);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000289 if (ti == -1)
290 continue;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000291
Evan Cheng360c2dd2006-11-01 23:06:55 +0000292 if (FirstTied) {
293 ++NumTwoAddressInstrs;
Bill Wendlingbcd24982006-12-07 20:28:15 +0000294 DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000295 }
Bill Wendling637980e2008-05-10 00:12:52 +0000296
Evan Cheng360c2dd2006-11-01 23:06:55 +0000297 FirstTied = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000298
Dan Gohmand735b802008-10-03 15:45:36 +0000299 assert(mi->getOperand(si).isReg() && mi->getOperand(si).getReg() &&
Evan Cheng360c2dd2006-11-01 23:06:55 +0000300 mi->getOperand(si).isUse() && "two address instruction invalid");
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000301
Bill Wendling637980e2008-05-10 00:12:52 +0000302 // If the two operands are the same we just remove the use
Evan Cheng360c2dd2006-11-01 23:06:55 +0000303 // and mark the def as def&use, otherwise we have to insert a copy.
304 if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) {
Bill Wendling637980e2008-05-10 00:12:52 +0000305 // Rewrite:
Evan Cheng360c2dd2006-11-01 23:06:55 +0000306 // a = b op c
307 // to:
308 // a = b
309 // a = a op c
310 unsigned regA = mi->getOperand(ti).getReg();
311 unsigned regB = mi->getOperand(si).getReg();
312
Dan Gohman6f0d0242008-02-10 18:45:23 +0000313 assert(TargetRegisterInfo::isVirtualRegister(regA) &&
314 TargetRegisterInfo::isVirtualRegister(regB) &&
Evan Cheng360c2dd2006-11-01 23:06:55 +0000315 "cannot update physical register live information");
Chris Lattner6b507672004-01-31 21:21:43 +0000316
Chris Lattner1e313632004-07-21 23:17:57 +0000317#ifndef NDEBUG
Evan Cheng360c2dd2006-11-01 23:06:55 +0000318 // First, verify that we don't have a use of a in the instruction (a =
319 // b + a for example) because our transformation will not work. This
320 // should never occur because we are in SSA form.
321 for (unsigned i = 0; i != mi->getNumOperands(); ++i)
322 assert((int)i == ti ||
Dan Gohmand735b802008-10-03 15:45:36 +0000323 !mi->getOperand(i).isReg() ||
Evan Cheng360c2dd2006-11-01 23:06:55 +0000324 mi->getOperand(i).getReg() != regA);
Chris Lattner1e313632004-07-21 23:17:57 +0000325#endif
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000326
Evan Cheng360c2dd2006-11-01 23:06:55 +0000327 // If this instruction is not the killing user of B, see if we can
328 // rearrange the code to make it so. Making it the killing user will
329 // allow us to coalesce A and B together, eliminating the copy we are
330 // about to insert.
Evan Cheng6130f662008-03-05 00:59:57 +0000331 if (!mi->killsRegister(regB)) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000332 // If this instruction is commutative, check to see if C dies. If
333 // so, swap the B and C operands. This makes the live ranges of A
334 // and C joinable.
335 // FIXME: This code also works for A := B op C instructions.
Chris Lattner749c6f62008-01-07 07:27:27 +0000336 if (TID.isCommutable() && mi->getNumOperands() >= 3) {
Dan Gohmand735b802008-10-03 15:45:36 +0000337 assert(mi->getOperand(3-si).isReg() &&
Evan Cheng360c2dd2006-11-01 23:06:55 +0000338 "Not a proper commutative instruction!");
339 unsigned regC = mi->getOperand(3-si).getReg();
Bill Wendling637980e2008-05-10 00:12:52 +0000340
Evan Cheng6130f662008-03-05 00:59:57 +0000341 if (mi->killsRegister(regC)) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000342 DOUT << "2addr: COMMUTING : " << *mi;
Evan Cheng875357d2008-03-13 06:37:55 +0000343 MachineInstr *NewMI = TII->commuteInstruction(mi);
Bill Wendling637980e2008-05-10 00:12:52 +0000344
Evan Cheng360c2dd2006-11-01 23:06:55 +0000345 if (NewMI == 0) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000346 DOUT << "2addr: COMMUTING FAILED!\n";
Evan Cheng360c2dd2006-11-01 23:06:55 +0000347 } else {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000348 DOUT << "2addr: COMMUTED TO: " << *NewMI;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000349 // If the instruction changed to commute it, update livevar.
350 if (NewMI != mi) {
Evan Chengbe04dc12008-07-03 00:07:19 +0000351 if (LV)
Owen Anderson802af112008-07-02 21:28:58 +0000352 // Update live variables
Evan Chengbe04dc12008-07-03 00:07:19 +0000353 LV->replaceKillInstruction(regC, mi, NewMI);
Owen Anderson802af112008-07-02 21:28:58 +0000354
Evan Cheng360c2dd2006-11-01 23:06:55 +0000355 mbbi->insert(mi, NewMI); // Insert the new inst
356 mbbi->erase(mi); // Nuke the old inst.
357 mi = NewMI;
Evan Cheng7543e582008-06-18 07:49:14 +0000358 DistanceMap.insert(std::make_pair(NewMI, Dist));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000359 }
360
361 ++NumCommuted;
362 regB = regC;
363 goto InstructionRearranged;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000364 }
Chris Lattnerc71d6942005-01-19 07:08:42 +0000365 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000366 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000367
368 // If this instruction is potentially convertible to a true
369 // three-address instruction,
Chris Lattner749c6f62008-01-07 07:27:27 +0000370 if (TID.isConvertibleTo3Addr()) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000371 // FIXME: This assumes there are no more operands which are tied
372 // to another register.
373#ifndef NDEBUG
Bill Wendling637980e2008-05-10 00:12:52 +0000374 for (unsigned i = si + 1, e = TID.getNumOperands(); i < e; ++i)
Chris Lattner749c6f62008-01-07 07:27:27 +0000375 assert(TID.getOperandConstraint(i, TOI::TIED_TO) == -1);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000376#endif
377
Owen Andersonf660c172008-07-02 23:41:07 +0000378 MachineInstr *NewMI = TII->convertToThreeAddress(mbbi, mi, LV);
Evan Cheng7543e582008-06-18 07:49:14 +0000379 if (NewMI) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000380 DOUT << "2addr: CONVERTING 2-ADDR: " << *mi;
Evan Cheng7543e582008-06-18 07:49:14 +0000381 DOUT << "2addr: TO 3-ADDR: " << *NewMI;
Evan Cheng0099ae22008-03-13 07:56:58 +0000382 bool Sunk = false;
Bill Wendling637980e2008-05-10 00:12:52 +0000383
Evan Cheng7543e582008-06-18 07:49:14 +0000384 if (NewMI->findRegisterUseOperand(regB, false, TRI))
Evan Cheng0099ae22008-03-13 07:56:58 +0000385 // FIXME: Temporary workaround. If the new instruction doesn't
386 // uses regB, convertToThreeAddress must have created more
387 // then one instruction.
Evan Cheng7543e582008-06-18 07:49:14 +0000388 Sunk = Sink3AddrInstruction(mbbi, NewMI, regB, mi);
Bill Wendling637980e2008-05-10 00:12:52 +0000389
390 mbbi->erase(mi); // Nuke the old inst.
391
Evan Cheng7a963fa2008-03-27 01:27:25 +0000392 if (!Sunk) {
Evan Cheng7543e582008-06-18 07:49:14 +0000393 DistanceMap.insert(std::make_pair(NewMI, Dist));
394 mi = NewMI;
Evan Cheng7a963fa2008-03-27 01:27:25 +0000395 nmi = next(mi);
396 }
Bill Wendling637980e2008-05-10 00:12:52 +0000397
Evan Cheng360c2dd2006-11-01 23:06:55 +0000398 ++NumConvertedTo3Addr;
Bill Wendling637980e2008-05-10 00:12:52 +0000399 break; // Done with this instruction.
Evan Cheng360c2dd2006-11-01 23:06:55 +0000400 }
Evan Chengb9d5e7c2007-10-20 04:01:47 +0000401 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000402 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000403
404 InstructionRearranged:
Evan Cheng7543e582008-06-18 07:49:14 +0000405 const TargetRegisterClass* rc = MRI->getRegClass(regA);
406 MachineInstr *DefMI = MRI->getVRegDef(regB);
407 // If it's safe and profitable, remat the definition instead of
408 // copying it.
Evan Cheng601ca4b2008-06-25 01:16:38 +0000409 if (DefMI &&
Evan Cheng8763c1c2008-08-27 20:58:54 +0000410 DefMI->getDesc().isAsCheapAsAMove() &&
Evan Chengdf3b9932008-08-27 20:33:50 +0000411 DefMI->isSafeToReMat(TII, regB) &&
Evan Cheng601ca4b2008-06-25 01:16:38 +0000412 isProfitableToReMat(regB, rc, mi, DefMI, mbbi, Dist,DistanceMap)){
Evan Cheng7543e582008-06-18 07:49:14 +0000413 DEBUG(cerr << "2addr: REMATTING : " << *DefMI << "\n");
414 TII->reMaterialize(*mbbi, mi, regA, DefMI);
415 ReMatRegs.set(regB);
416 ++NumReMats;
Bill Wendling48f7f232008-05-26 05:18:34 +0000417 } else {
418 TII->copyRegToReg(*mbbi, mi, regA, regB, rc, rc);
419 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000420
421 MachineBasicBlock::iterator prevMi = prior(mi);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000422
Bill Wendling637980e2008-05-10 00:12:52 +0000423 // Update live variables for regB.
Owen Anderson802af112008-07-02 21:28:58 +0000424 if (LV) {
425 LiveVariables::VarInfo& varInfoB = LV->getVarInfo(regB);
Bill Wendling637980e2008-05-10 00:12:52 +0000426
Owen Anderson802af112008-07-02 21:28:58 +0000427 // regB is used in this BB.
428 varInfoB.UsedBlocks[mbbi->getNumber()] = true;
Bill Wendling637980e2008-05-10 00:12:52 +0000429
Evan Cheng9f1c8312008-07-03 09:09:37 +0000430 if (LV->removeVirtualRegisterKilled(regB, mi))
Owen Anderson802af112008-07-02 21:28:58 +0000431 LV->addVirtualRegisterKilled(regB, prevMi);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000432
Evan Cheng9f1c8312008-07-03 09:09:37 +0000433 if (LV->removeVirtualRegisterDead(regB, mi))
Owen Anderson802af112008-07-02 21:28:58 +0000434 LV->addVirtualRegisterDead(regB, prevMi);
Owen Anderson802af112008-07-02 21:28:58 +0000435 }
Dan Gohman2d9716f2008-11-12 17:15:19 +0000436
437 DOUT << "\t\tprepend:\t"; DEBUG(prevMi->print(*cerr.stream(), &TM));
Owen Anderson802af112008-07-02 21:28:58 +0000438
Bill Wendling637980e2008-05-10 00:12:52 +0000439 // Replace all occurences of regB with regA.
Evan Cheng360c2dd2006-11-01 23:06:55 +0000440 for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000441 if (mi->getOperand(i).isReg() &&
Evan Cheng360c2dd2006-11-01 23:06:55 +0000442 mi->getOperand(i).getReg() == regB)
443 mi->getOperand(i).setReg(regA);
444 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000445 }
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000446
Evan Cheng360c2dd2006-11-01 23:06:55 +0000447 assert(mi->getOperand(ti).isDef() && mi->getOperand(si).isUse());
448 mi->getOperand(ti).setReg(mi->getOperand(si).getReg());
449 MadeChange = true;
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000450
Bill Wendlingbcd24982006-12-07 20:28:15 +0000451 DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(*cerr.stream(), &TM));
Misha Brukman75fa4e42004-07-22 15:26:23 +0000452 }
Bill Wendling637980e2008-05-10 00:12:52 +0000453
Evan Cheng7a963fa2008-03-27 01:27:25 +0000454 mi = nmi;
Misha Brukman75fa4e42004-07-22 15:26:23 +0000455 }
456 }
457
Evan Cheng601ca4b2008-06-25 01:16:38 +0000458 // Some remat'ed instructions are dead.
459 int VReg = ReMatRegs.find_first();
460 while (VReg != -1) {
461 if (MRI->use_empty(VReg)) {
462 MachineInstr *DefMI = MRI->getVRegDef(VReg);
463 DefMI->eraseFromParent();
Bill Wendlinga16157a2008-05-26 05:49:49 +0000464 }
Evan Cheng601ca4b2008-06-25 01:16:38 +0000465 VReg = ReMatRegs.find_next(VReg);
Bill Wendling48f7f232008-05-26 05:18:34 +0000466 }
467
Misha Brukman75fa4e42004-07-22 15:26:23 +0000468 return MadeChange;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000469}