blob: 794f772101e98839cedaf640e596ab9395239662 [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"
Dan Gohmand68a0762009-01-05 17:59:02 +000045#include "llvm/ADT/SmallSet.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");
Evan Chengd498c8f2009-01-25 03:53:59 +000052STATISTIC(NumAggrCommuted , "Number of instructions aggressively commuted");
Chris Lattnercd3245a2006-12-19 22:41:21 +000053STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address");
Evan Cheng875357d2008-03-13 06:37:55 +000054STATISTIC(Num3AddrSunk, "Number of 3-address instructions sunk");
Evan Cheng7543e582008-06-18 07:49:14 +000055STATISTIC(NumReMats, "Number of instructions re-materialized");
Evan Cheng875357d2008-03-13 06:37:55 +000056
57namespace {
Bill Wendling637980e2008-05-10 00:12:52 +000058 class VISIBILITY_HIDDEN TwoAddressInstructionPass
59 : public MachineFunctionPass {
Evan Cheng875357d2008-03-13 06:37:55 +000060 const TargetInstrInfo *TII;
61 const TargetRegisterInfo *TRI;
62 MachineRegisterInfo *MRI;
63 LiveVariables *LV;
64
Bill Wendling637980e2008-05-10 00:12:52 +000065 bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI,
66 unsigned Reg,
67 MachineBasicBlock::iterator OldPos);
Evan Cheng7543e582008-06-18 07:49:14 +000068
Evan Cheng7543e582008-06-18 07:49:14 +000069 bool isProfitableToReMat(unsigned Reg, const TargetRegisterClass *RC,
Evan Cheng601ca4b2008-06-25 01:16:38 +000070 MachineInstr *MI, MachineInstr *DefMI,
71 MachineBasicBlock *MBB, unsigned Loc,
Evan Cheng7543e582008-06-18 07:49:14 +000072 DenseMap<MachineInstr*, unsigned> &DistanceMap);
Evan Cheng81913712009-01-23 23:27:33 +000073
Evan Chengd498c8f2009-01-25 03:53:59 +000074 bool NoUseAfterLastDef(unsigned Reg, MachineBasicBlock *MBB, unsigned Dist,
75 DenseMap<MachineInstr*, unsigned> &DistanceMap,
76 unsigned &LastDef);
77
78 bool isProfitableToCommute(unsigned regB, unsigned regC,
79 MachineInstr *MI, MachineBasicBlock *MBB,
80 unsigned Dist,
81 DenseMap<MachineInstr*, unsigned> &DistanceMap);
82
Evan Cheng81913712009-01-23 23:27:33 +000083 bool CommuteInstruction(MachineBasicBlock::iterator &mi,
84 MachineFunction::iterator &mbbi,
85 unsigned RegC, unsigned Dist,
86 DenseMap<MachineInstr*, unsigned> &DistanceMap);
Evan Cheng875357d2008-03-13 06:37:55 +000087 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000088 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000089 TwoAddressInstructionPass() : MachineFunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000090
Bill Wendling637980e2008-05-10 00:12:52 +000091 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Bill Wendling637980e2008-05-10 00:12:52 +000092 AU.addPreserved<LiveVariables>();
93 AU.addPreservedID(MachineLoopInfoID);
94 AU.addPreservedID(MachineDominatorsID);
Owen Anderson95dad832008-10-07 20:22:28 +000095 if (StrongPHIElim)
96 AU.addPreservedID(StrongPHIEliminationID);
97 else
98 AU.addPreservedID(PHIEliminationID);
Bill Wendling637980e2008-05-10 00:12:52 +000099 MachineFunctionPass::getAnalysisUsage(AU);
100 }
Alkis Evlogimenos4c080862003-12-18 22:40:24 +0000101
Bill Wendling637980e2008-05-10 00:12:52 +0000102 /// runOnMachineFunction - Pass entry point.
Misha Brukman75fa4e42004-07-22 15:26:23 +0000103 bool runOnMachineFunction(MachineFunction&);
104 };
Chris Lattnerd74ea2b2006-05-24 17:04:05 +0000105}
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000106
Dan Gohman844731a2008-05-13 00:00:25 +0000107char TwoAddressInstructionPass::ID = 0;
108static RegisterPass<TwoAddressInstructionPass>
109X("twoaddressinstruction", "Two-Address instruction pass");
110
Dan Gohman6ddba2b2008-05-13 02:05:11 +0000111const PassInfo *const llvm::TwoAddressInstructionPassID = &X;
Alkis Evlogimenos4c080862003-12-18 22:40:24 +0000112
Evan Cheng875357d2008-03-13 06:37:55 +0000113/// Sink3AddrInstruction - A two-address instruction has been converted to a
114/// three-address instruction to avoid clobbering a register. Try to sink it
Bill Wendling637980e2008-05-10 00:12:52 +0000115/// past the instruction that would kill the above mentioned register to reduce
116/// register pressure.
Evan Cheng875357d2008-03-13 06:37:55 +0000117bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
118 MachineInstr *MI, unsigned SavedReg,
119 MachineBasicBlock::iterator OldPos) {
120 // Check if it's safe to move this instruction.
121 bool SeenStore = true; // Be conservative.
122 if (!MI->isSafeToMove(TII, SeenStore))
123 return false;
124
125 unsigned DefReg = 0;
126 SmallSet<unsigned, 4> UseRegs;
Bill Wendling637980e2008-05-10 00:12:52 +0000127
Evan Cheng875357d2008-03-13 06:37:55 +0000128 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
129 const MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000130 if (!MO.isReg())
Evan Cheng875357d2008-03-13 06:37:55 +0000131 continue;
132 unsigned MOReg = MO.getReg();
133 if (!MOReg)
134 continue;
135 if (MO.isUse() && MOReg != SavedReg)
136 UseRegs.insert(MO.getReg());
137 if (!MO.isDef())
138 continue;
139 if (MO.isImplicit())
140 // Don't try to move it if it implicitly defines a register.
141 return false;
142 if (DefReg)
143 // For now, don't move any instructions that define multiple registers.
144 return false;
145 DefReg = MO.getReg();
146 }
147
148 // Find the instruction that kills SavedReg.
149 MachineInstr *KillMI = NULL;
150 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SavedReg),
151 UE = MRI->use_end(); UI != UE; ++UI) {
152 MachineOperand &UseMO = UI.getOperand();
153 if (!UseMO.isKill())
154 continue;
155 KillMI = UseMO.getParent();
156 break;
157 }
Bill Wendling637980e2008-05-10 00:12:52 +0000158
Evan Cheng875357d2008-03-13 06:37:55 +0000159 if (!KillMI || KillMI->getParent() != MBB)
160 return false;
161
Bill Wendling637980e2008-05-10 00:12:52 +0000162 // If any of the definitions are used by another instruction between the
163 // position and the kill use, then it's not safe to sink it.
164 //
165 // FIXME: This can be sped up if there is an easy way to query whether an
Evan Cheng7543e582008-06-18 07:49:14 +0000166 // instruction is before or after another instruction. Then we can use
Bill Wendling637980e2008-05-10 00:12:52 +0000167 // MachineRegisterInfo def / use instead.
Evan Cheng875357d2008-03-13 06:37:55 +0000168 MachineOperand *KillMO = NULL;
169 MachineBasicBlock::iterator KillPos = KillMI;
170 ++KillPos;
Bill Wendling637980e2008-05-10 00:12:52 +0000171
Evan Cheng7543e582008-06-18 07:49:14 +0000172 unsigned NumVisited = 0;
Evan Cheng875357d2008-03-13 06:37:55 +0000173 for (MachineBasicBlock::iterator I = next(OldPos); I != KillPos; ++I) {
174 MachineInstr *OtherMI = I;
Evan Cheng7543e582008-06-18 07:49:14 +0000175 if (NumVisited > 30) // FIXME: Arbitrary limit to reduce compile time cost.
176 return false;
177 ++NumVisited;
Evan Cheng875357d2008-03-13 06:37:55 +0000178 for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
179 MachineOperand &MO = OtherMI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000180 if (!MO.isReg())
Evan Cheng875357d2008-03-13 06:37:55 +0000181 continue;
182 unsigned MOReg = MO.getReg();
183 if (!MOReg)
184 continue;
185 if (DefReg == MOReg)
186 return false;
Bill Wendling637980e2008-05-10 00:12:52 +0000187
Evan Cheng875357d2008-03-13 06:37:55 +0000188 if (MO.isKill()) {
189 if (OtherMI == KillMI && MOReg == SavedReg)
Evan Cheng7543e582008-06-18 07:49:14 +0000190 // Save the operand that kills the register. We want to unset the kill
191 // marker if we can sink MI past it.
Evan Cheng875357d2008-03-13 06:37:55 +0000192 KillMO = &MO;
193 else if (UseRegs.count(MOReg))
194 // One of the uses is killed before the destination.
195 return false;
196 }
197 }
198 }
199
Evan Cheng875357d2008-03-13 06:37:55 +0000200 // Update kill and LV information.
201 KillMO->setIsKill(false);
202 KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI);
203 KillMO->setIsKill(true);
Owen Anderson802af112008-07-02 21:28:58 +0000204
Evan Cheng9f1c8312008-07-03 09:09:37 +0000205 if (LV)
206 LV->replaceKillInstruction(SavedReg, KillMI, MI);
Evan Cheng875357d2008-03-13 06:37:55 +0000207
208 // Move instruction to its destination.
209 MBB->remove(MI);
210 MBB->insert(KillPos, MI);
211
212 ++Num3AddrSunk;
213 return true;
214}
215
Evan Cheng7543e582008-06-18 07:49:14 +0000216/// isTwoAddrUse - Return true if the specified MI is using the specified
217/// register as a two-address operand.
218static bool isTwoAddrUse(MachineInstr *UseMI, unsigned Reg) {
219 const TargetInstrDesc &TID = UseMI->getDesc();
220 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
221 MachineOperand &MO = UseMI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000222 if (MO.isReg() && MO.getReg() == Reg &&
Evan Cheng7543e582008-06-18 07:49:14 +0000223 (MO.isDef() || TID.getOperandConstraint(i, TOI::TIED_TO) != -1))
224 // Earlier use is a two-address one.
225 return true;
226 }
227 return false;
228}
229
230/// isProfitableToReMat - Return true if the heuristics determines it is likely
231/// to be profitable to re-materialize the definition of Reg rather than copy
232/// the register.
233bool
234TwoAddressInstructionPass::isProfitableToReMat(unsigned Reg,
235 const TargetRegisterClass *RC,
Evan Cheng601ca4b2008-06-25 01:16:38 +0000236 MachineInstr *MI, MachineInstr *DefMI,
237 MachineBasicBlock *MBB, unsigned Loc,
238 DenseMap<MachineInstr*, unsigned> &DistanceMap){
Evan Cheng7543e582008-06-18 07:49:14 +0000239 bool OtherUse = false;
240 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg),
241 UE = MRI->use_end(); UI != UE; ++UI) {
242 MachineOperand &UseMO = UI.getOperand();
Evan Cheng7543e582008-06-18 07:49:14 +0000243 MachineInstr *UseMI = UseMO.getParent();
Evan Cheng601ca4b2008-06-25 01:16:38 +0000244 MachineBasicBlock *UseMBB = UseMI->getParent();
245 if (UseMBB == MBB) {
246 DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI);
247 if (DI != DistanceMap.end() && DI->second == Loc)
248 continue; // Current use.
249 OtherUse = true;
250 // There is at least one other use in the MBB that will clobber the
251 // register.
252 if (isTwoAddrUse(UseMI, Reg))
253 return true;
254 }
Evan Cheng7543e582008-06-18 07:49:14 +0000255 }
Evan Cheng601ca4b2008-06-25 01:16:38 +0000256
257 // If other uses in MBB are not two-address uses, then don't remat.
258 if (OtherUse)
259 return false;
260
261 // No other uses in the same block, remat if it's defined in the same
262 // block so it does not unnecessarily extend the live range.
263 return MBB == DefMI->getParent();
Evan Cheng7543e582008-06-18 07:49:14 +0000264}
265
Evan Chengd498c8f2009-01-25 03:53:59 +0000266/// NoUseAfterLastDef - Return true if there are no intervening uses between the
267/// last instruction in the MBB that defines the specified register and the
268/// two-address instruction which is being processed. It also returns the last
269/// def location by reference
270bool TwoAddressInstructionPass::NoUseAfterLastDef(unsigned Reg,
271 MachineBasicBlock *MBB, unsigned Dist,
272 DenseMap<MachineInstr*, unsigned> &DistanceMap,
273 unsigned &LastDef) {
274 LastDef = 0;
275 unsigned LastUse = Dist;
276 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(Reg),
277 E = MRI->reg_end(); I != E; ++I) {
278 MachineOperand &MO = I.getOperand();
279 MachineInstr *MI = MO.getParent();
280 if (MI->getParent() != MBB)
281 continue;
282 DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI);
283 if (DI == DistanceMap.end())
284 continue;
285 if (MO.isUse() && DI->second < LastUse)
286 LastUse = DI->second;
287 if (MO.isDef() && DI->second > LastDef)
288 LastDef = DI->second;
289 }
290
291 return !(LastUse > LastDef && LastUse < Dist);
292}
293
294/// isProfitableToReMat - Return true if it's potentially profitable to commute
295/// the two-address instruction that's being processed.
296bool
297TwoAddressInstructionPass::isProfitableToCommute(unsigned regB, unsigned regC,
298 MachineInstr *MI, MachineBasicBlock *MBB,
299 unsigned Dist, DenseMap<MachineInstr*, unsigned> &DistanceMap) {
300 // Determine if it's profitable to commute this two address instruction. In
301 // general, we want no uses between this instruction and the definition of
302 // the two-address register.
303 // e.g.
304 // %reg1028<def> = EXTRACT_SUBREG %reg1027<kill>, 1
305 // %reg1029<def> = MOV8rr %reg1028
306 // %reg1029<def> = SHR8ri %reg1029, 7, %EFLAGS<imp-def,dead>
307 // insert => %reg1030<def> = MOV8rr %reg1028
308 // %reg1030<def> = ADD8rr %reg1028<kill>, %reg1029<kill>, %EFLAGS<imp-def,dead>
309 // In this case, it might not be possible to coalesce the second MOV8rr
310 // instruction if the first one is coalesced. So it would be profitable to
311 // commute it:
312 // %reg1028<def> = EXTRACT_SUBREG %reg1027<kill>, 1
313 // %reg1029<def> = MOV8rr %reg1028
314 // %reg1029<def> = SHR8ri %reg1029, 7, %EFLAGS<imp-def,dead>
315 // insert => %reg1030<def> = MOV8rr %reg1029
316 // %reg1030<def> = ADD8rr %reg1029<kill>, %reg1028<kill>, %EFLAGS<imp-def,dead>
317
318 if (!MI->killsRegister(regC))
319 return false;
320
321 // Ok, we have something like:
322 // %reg1030<def> = ADD8rr %reg1028<kill>, %reg1029<kill>, %EFLAGS<imp-def,dead>
323 // let's see if it's worth commuting it.
324
325 // If there is a use of regC between its last def (could be livein) and this
326 // instruction, then bail.
327 unsigned LastDefC = 0;
328 if (!NoUseAfterLastDef(regC, MBB, Dist, DistanceMap, LastDefC))
329 return false;
330
331 // If there is a use of regB between its last def (could be livein) and this
332 // instruction, then go ahead and make this transformation.
333 unsigned LastDefB = 0;
334 if (!NoUseAfterLastDef(regB, MBB, Dist, DistanceMap, LastDefB))
335 return true;
336
337 // Since there are no intervening uses for both registers, then commute
338 // if the def of regC is closer. Its live interval is shorter.
339 return LastDefB && LastDefC && LastDefC > LastDefB;
340}
341
Evan Cheng81913712009-01-23 23:27:33 +0000342/// CommuteInstruction - Commute a two-address instruction and update the basic
343/// block, distance map, and live variables if needed. Return true if it is
344/// successful.
345bool
346TwoAddressInstructionPass::CommuteInstruction(MachineBasicBlock::iterator &mi,
347 MachineFunction::iterator &mbbi,
348 unsigned RegC, unsigned Dist,
349 DenseMap<MachineInstr*, unsigned> &DistanceMap) {
350 MachineInstr *MI = mi;
351 DOUT << "2addr: COMMUTING : " << *MI;
352 MachineInstr *NewMI = TII->commuteInstruction(MI);
353
354 if (NewMI == 0) {
355 DOUT << "2addr: COMMUTING FAILED!\n";
356 return false;
357 }
358
359 DOUT << "2addr: COMMUTED TO: " << *NewMI;
360 // If the instruction changed to commute it, update livevar.
361 if (NewMI != MI) {
362 if (LV)
363 // Update live variables
364 LV->replaceKillInstruction(RegC, MI, NewMI);
365
366 mbbi->insert(mi, NewMI); // Insert the new inst
367 mbbi->erase(mi); // Nuke the old inst.
368 mi = NewMI;
369 DistanceMap.insert(std::make_pair(NewMI, Dist));
370 }
371 return true;
372}
373
Bill Wendling637980e2008-05-10 00:12:52 +0000374/// runOnMachineFunction - Reduce two-address instructions to two operands.
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000375///
Chris Lattner163c1e72004-01-31 21:14:04 +0000376bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000377 DOUT << "Machine Function\n";
Misha Brukman75fa4e42004-07-22 15:26:23 +0000378 const TargetMachine &TM = MF.getTarget();
Evan Cheng875357d2008-03-13 06:37:55 +0000379 MRI = &MF.getRegInfo();
380 TII = TM.getInstrInfo();
381 TRI = TM.getRegisterInfo();
Duncan Sands1465d612009-01-28 13:14:17 +0000382 LV = getAnalysisIfAvailable<LiveVariables>();
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000383
Misha Brukman75fa4e42004-07-22 15:26:23 +0000384 bool MadeChange = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000385
Bill Wendlinga09362e2006-11-28 22:48:48 +0000386 DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n";
387 DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
Alkis Evlogimenos3a9986f2004-02-18 00:35:06 +0000388
Evan Cheng7543e582008-06-18 07:49:14 +0000389 // ReMatRegs - Keep track of the registers whose def's are remat'ed.
390 BitVector ReMatRegs;
391 ReMatRegs.resize(MRI->getLastVirtReg()+1);
392
393 // DistanceMap - Keep track the distance of a MI from the start of the
394 // current basic block.
395 DenseMap<MachineInstr*, unsigned> DistanceMap;
Bill Wendling48f7f232008-05-26 05:18:34 +0000396
Misha Brukman75fa4e42004-07-22 15:26:23 +0000397 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
398 mbbi != mbbe; ++mbbi) {
Evan Cheng7543e582008-06-18 07:49:14 +0000399 unsigned Dist = 0;
400 DistanceMap.clear();
Misha Brukman75fa4e42004-07-22 15:26:23 +0000401 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
Evan Cheng7a963fa2008-03-27 01:27:25 +0000402 mi != me; ) {
403 MachineBasicBlock::iterator nmi = next(mi);
Chris Lattner749c6f62008-01-07 07:27:27 +0000404 const TargetInstrDesc &TID = mi->getDesc();
Evan Cheng360c2dd2006-11-01 23:06:55 +0000405 bool FirstTied = true;
Bill Wendling637980e2008-05-10 00:12:52 +0000406
Evan Cheng7543e582008-06-18 07:49:14 +0000407 DistanceMap.insert(std::make_pair(mi, ++Dist));
Chris Lattner749c6f62008-01-07 07:27:27 +0000408 for (unsigned si = 1, e = TID.getNumOperands(); si < e; ++si) {
409 int ti = TID.getOperandConstraint(si, TOI::TIED_TO);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000410 if (ti == -1)
411 continue;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000412
Evan Cheng360c2dd2006-11-01 23:06:55 +0000413 if (FirstTied) {
414 ++NumTwoAddressInstrs;
Bill Wendlingbcd24982006-12-07 20:28:15 +0000415 DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000416 }
Bill Wendling637980e2008-05-10 00:12:52 +0000417
Evan Cheng360c2dd2006-11-01 23:06:55 +0000418 FirstTied = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000419
Dan Gohmand735b802008-10-03 15:45:36 +0000420 assert(mi->getOperand(si).isReg() && mi->getOperand(si).getReg() &&
Evan Cheng360c2dd2006-11-01 23:06:55 +0000421 mi->getOperand(si).isUse() && "two address instruction invalid");
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000422
Bill Wendling637980e2008-05-10 00:12:52 +0000423 // If the two operands are the same we just remove the use
Evan Cheng360c2dd2006-11-01 23:06:55 +0000424 // and mark the def as def&use, otherwise we have to insert a copy.
425 if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) {
Bill Wendling637980e2008-05-10 00:12:52 +0000426 // Rewrite:
Evan Cheng360c2dd2006-11-01 23:06:55 +0000427 // a = b op c
428 // to:
429 // a = b
430 // a = a op c
431 unsigned regA = mi->getOperand(ti).getReg();
432 unsigned regB = mi->getOperand(si).getReg();
433
Dan Gohman6f0d0242008-02-10 18:45:23 +0000434 assert(TargetRegisterInfo::isVirtualRegister(regA) &&
435 TargetRegisterInfo::isVirtualRegister(regB) &&
Evan Cheng360c2dd2006-11-01 23:06:55 +0000436 "cannot update physical register live information");
Chris Lattner6b507672004-01-31 21:21:43 +0000437
Chris Lattner1e313632004-07-21 23:17:57 +0000438#ifndef NDEBUG
Evan Cheng360c2dd2006-11-01 23:06:55 +0000439 // First, verify that we don't have a use of a in the instruction (a =
440 // b + a for example) because our transformation will not work. This
441 // should never occur because we are in SSA form.
442 for (unsigned i = 0; i != mi->getNumOperands(); ++i)
443 assert((int)i == ti ||
Dan Gohmand735b802008-10-03 15:45:36 +0000444 !mi->getOperand(i).isReg() ||
Evan Cheng360c2dd2006-11-01 23:06:55 +0000445 mi->getOperand(i).getReg() != regA);
Chris Lattner1e313632004-07-21 23:17:57 +0000446#endif
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000447
Evan Cheng360c2dd2006-11-01 23:06:55 +0000448 // If this instruction is not the killing user of B, see if we can
449 // rearrange the code to make it so. Making it the killing user will
450 // allow us to coalesce A and B together, eliminating the copy we are
451 // about to insert.
Evan Cheng6130f662008-03-05 00:59:57 +0000452 if (!mi->killsRegister(regB)) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000453 // If this instruction is commutative, check to see if C dies. If
454 // so, swap the B and C operands. This makes the live ranges of A
455 // and C joinable.
456 // FIXME: This code also works for A := B op C instructions.
Chris Lattner749c6f62008-01-07 07:27:27 +0000457 if (TID.isCommutable() && mi->getNumOperands() >= 3) {
Dan Gohmand735b802008-10-03 15:45:36 +0000458 assert(mi->getOperand(3-si).isReg() &&
Evan Cheng360c2dd2006-11-01 23:06:55 +0000459 "Not a proper commutative instruction!");
460 unsigned regC = mi->getOperand(3-si).getReg();
Evan Cheng6130f662008-03-05 00:59:57 +0000461 if (mi->killsRegister(regC)) {
Evan Cheng81913712009-01-23 23:27:33 +0000462 if (CommuteInstruction(mi, mbbi, regC, Dist, DistanceMap)) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000463 ++NumCommuted;
464 regB = regC;
465 goto InstructionRearranged;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000466 }
Chris Lattnerc71d6942005-01-19 07:08:42 +0000467 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000468 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000469
470 // If this instruction is potentially convertible to a true
471 // three-address instruction,
Chris Lattner749c6f62008-01-07 07:27:27 +0000472 if (TID.isConvertibleTo3Addr()) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000473 // FIXME: This assumes there are no more operands which are tied
474 // to another register.
475#ifndef NDEBUG
Bill Wendling637980e2008-05-10 00:12:52 +0000476 for (unsigned i = si + 1, e = TID.getNumOperands(); i < e; ++i)
Chris Lattner749c6f62008-01-07 07:27:27 +0000477 assert(TID.getOperandConstraint(i, TOI::TIED_TO) == -1);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000478#endif
479
Owen Andersonf660c172008-07-02 23:41:07 +0000480 MachineInstr *NewMI = TII->convertToThreeAddress(mbbi, mi, LV);
Evan Cheng7543e582008-06-18 07:49:14 +0000481 if (NewMI) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000482 DOUT << "2addr: CONVERTING 2-ADDR: " << *mi;
Evan Cheng7543e582008-06-18 07:49:14 +0000483 DOUT << "2addr: TO 3-ADDR: " << *NewMI;
Evan Cheng0099ae22008-03-13 07:56:58 +0000484 bool Sunk = false;
Bill Wendling637980e2008-05-10 00:12:52 +0000485
Evan Cheng7543e582008-06-18 07:49:14 +0000486 if (NewMI->findRegisterUseOperand(regB, false, TRI))
Evan Cheng0099ae22008-03-13 07:56:58 +0000487 // FIXME: Temporary workaround. If the new instruction doesn't
488 // uses regB, convertToThreeAddress must have created more
489 // then one instruction.
Evan Cheng7543e582008-06-18 07:49:14 +0000490 Sunk = Sink3AddrInstruction(mbbi, NewMI, regB, mi);
Bill Wendling637980e2008-05-10 00:12:52 +0000491
492 mbbi->erase(mi); // Nuke the old inst.
493
Evan Cheng7a963fa2008-03-27 01:27:25 +0000494 if (!Sunk) {
Evan Cheng7543e582008-06-18 07:49:14 +0000495 DistanceMap.insert(std::make_pair(NewMI, Dist));
496 mi = NewMI;
Evan Cheng7a963fa2008-03-27 01:27:25 +0000497 nmi = next(mi);
498 }
Bill Wendling637980e2008-05-10 00:12:52 +0000499
Evan Cheng360c2dd2006-11-01 23:06:55 +0000500 ++NumConvertedTo3Addr;
Bill Wendling637980e2008-05-10 00:12:52 +0000501 break; // Done with this instruction.
Evan Cheng360c2dd2006-11-01 23:06:55 +0000502 }
Evan Chengb9d5e7c2007-10-20 04:01:47 +0000503 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000504 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000505
Evan Chengd498c8f2009-01-25 03:53:59 +0000506 // If it's profitable to commute the instruction, do so.
507 if (TID.isCommutable() && mi->getNumOperands() >= 3) {
508 unsigned regC = mi->getOperand(3-si).getReg();
509 if (isProfitableToCommute(regB, regC, mi, mbbi, Dist, DistanceMap))
510 if (CommuteInstruction(mi, mbbi, regC, Dist, DistanceMap)) {
511 ++NumAggrCommuted;
512 ++NumCommuted;
513 regB = regC;
514 }
515 }
516
Evan Cheng360c2dd2006-11-01 23:06:55 +0000517 InstructionRearranged:
Evan Cheng7543e582008-06-18 07:49:14 +0000518 const TargetRegisterClass* rc = MRI->getRegClass(regA);
519 MachineInstr *DefMI = MRI->getVRegDef(regB);
520 // If it's safe and profitable, remat the definition instead of
521 // copying it.
Evan Cheng601ca4b2008-06-25 01:16:38 +0000522 if (DefMI &&
Evan Cheng8763c1c2008-08-27 20:58:54 +0000523 DefMI->getDesc().isAsCheapAsAMove() &&
Evan Chengdf3b9932008-08-27 20:33:50 +0000524 DefMI->isSafeToReMat(TII, regB) &&
Evan Cheng601ca4b2008-06-25 01:16:38 +0000525 isProfitableToReMat(regB, rc, mi, DefMI, mbbi, Dist,DistanceMap)){
Evan Cheng7543e582008-06-18 07:49:14 +0000526 DEBUG(cerr << "2addr: REMATTING : " << *DefMI << "\n");
527 TII->reMaterialize(*mbbi, mi, regA, DefMI);
528 ReMatRegs.set(regB);
529 ++NumReMats;
Bill Wendling48f7f232008-05-26 05:18:34 +0000530 } else {
531 TII->copyRegToReg(*mbbi, mi, regA, regB, rc, rc);
532 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000533
Evan Chengd498c8f2009-01-25 03:53:59 +0000534 MachineBasicBlock::iterator prevMI = prior(mi);
535 // Update DistanceMap.
536 DistanceMap.insert(std::make_pair(prevMI, Dist));
537 DistanceMap[mi] = ++Dist;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000538
Bill Wendling637980e2008-05-10 00:12:52 +0000539 // Update live variables for regB.
Owen Anderson802af112008-07-02 21:28:58 +0000540 if (LV) {
541 LiveVariables::VarInfo& varInfoB = LV->getVarInfo(regB);
Bill Wendling637980e2008-05-10 00:12:52 +0000542
Owen Anderson802af112008-07-02 21:28:58 +0000543 // regB is used in this BB.
544 varInfoB.UsedBlocks[mbbi->getNumber()] = true;
Bill Wendling637980e2008-05-10 00:12:52 +0000545
Evan Cheng9f1c8312008-07-03 09:09:37 +0000546 if (LV->removeVirtualRegisterKilled(regB, mi))
Evan Chengd498c8f2009-01-25 03:53:59 +0000547 LV->addVirtualRegisterKilled(regB, prevMI);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000548
Evan Cheng9f1c8312008-07-03 09:09:37 +0000549 if (LV->removeVirtualRegisterDead(regB, mi))
Evan Chengd498c8f2009-01-25 03:53:59 +0000550 LV->addVirtualRegisterDead(regB, prevMI);
Owen Anderson802af112008-07-02 21:28:58 +0000551 }
Dan Gohman2d9716f2008-11-12 17:15:19 +0000552
Evan Chengd498c8f2009-01-25 03:53:59 +0000553 DOUT << "\t\tprepend:\t"; DEBUG(prevMI->print(*cerr.stream(), &TM));
Owen Anderson802af112008-07-02 21:28:58 +0000554
Bill Wendling637980e2008-05-10 00:12:52 +0000555 // Replace all occurences of regB with regA.
Evan Cheng360c2dd2006-11-01 23:06:55 +0000556 for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
Dan Gohmand735b802008-10-03 15:45:36 +0000557 if (mi->getOperand(i).isReg() &&
Evan Cheng360c2dd2006-11-01 23:06:55 +0000558 mi->getOperand(i).getReg() == regB)
559 mi->getOperand(i).setReg(regA);
560 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000561 }
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000562
Evan Cheng360c2dd2006-11-01 23:06:55 +0000563 assert(mi->getOperand(ti).isDef() && mi->getOperand(si).isUse());
564 mi->getOperand(ti).setReg(mi->getOperand(si).getReg());
565 MadeChange = true;
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000566
Bill Wendlingbcd24982006-12-07 20:28:15 +0000567 DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(*cerr.stream(), &TM));
Misha Brukman75fa4e42004-07-22 15:26:23 +0000568 }
Bill Wendling637980e2008-05-10 00:12:52 +0000569
Evan Cheng7a963fa2008-03-27 01:27:25 +0000570 mi = nmi;
Misha Brukman75fa4e42004-07-22 15:26:23 +0000571 }
572 }
573
Evan Cheng601ca4b2008-06-25 01:16:38 +0000574 // Some remat'ed instructions are dead.
575 int VReg = ReMatRegs.find_first();
576 while (VReg != -1) {
577 if (MRI->use_empty(VReg)) {
578 MachineInstr *DefMI = MRI->getVRegDef(VReg);
579 DefMI->eraseFromParent();
Bill Wendlinga16157a2008-05-26 05:49:49 +0000580 }
Evan Cheng601ca4b2008-06-25 01:16:38 +0000581 VReg = ReMatRegs.find_next(VReg);
Bill Wendling48f7f232008-05-26 05:18:34 +0000582 }
583
Misha Brukman75fa4e42004-07-22 15:26:23 +0000584 return MadeChange;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000585}