blob: 977a8ff54ad7b9a72c2d3ab4b058966d2e59edae [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"
Evan Cheng875357d2008-03-13 06:37:55 +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"
Reid Spencer551ccae2004-09-01 22:55:40 +000043#include "llvm/ADT/Statistic.h"
44#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000045using namespace llvm;
46
Chris Lattnercd3245a2006-12-19 22:41:21 +000047STATISTIC(NumTwoAddressInstrs, "Number of two-address instructions");
48STATISTIC(NumCommuted , "Number of instructions commuted to coalesce");
49STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address");
Evan Cheng875357d2008-03-13 06:37:55 +000050STATISTIC(Num3AddrSunk, "Number of 3-address instructions sunk");
51
52namespace {
53 static cl::opt<int>
54 SinkLimit("two-addr-sink-limit", cl::init(-1), cl::Hidden);
55}
Chris Lattnerbd91c1c2004-01-31 21:07:15 +000056
Chris Lattnercd3245a2006-12-19 22:41:21 +000057namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000058 struct 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
65 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000066 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000067 TwoAddressInstructionPass() : MachineFunctionPass((intptr_t)&ID) {}
68
Misha Brukman75fa4e42004-07-22 15:26:23 +000069 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000070
Misha Brukman75fa4e42004-07-22 15:26:23 +000071 /// runOnMachineFunction - pass entry point
72 bool runOnMachineFunction(MachineFunction&);
Evan Cheng875357d2008-03-13 06:37:55 +000073
74 private:
75 bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI,
76 unsigned Reg,
77 MachineBasicBlock::iterator OldPos);
Misha Brukman75fa4e42004-07-22 15:26:23 +000078 };
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000079
Devang Patel19974732007-05-03 01:11:54 +000080 char TwoAddressInstructionPass::ID = 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +000081 RegisterPass<TwoAddressInstructionPass>
Misha Brukman75fa4e42004-07-22 15:26:23 +000082 X("twoaddressinstruction", "Two-Address instruction pass");
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000083}
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000084
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000085const PassInfo *llvm::TwoAddressInstructionPassID = X.getPassInfo();
86
Misha Brukman75fa4e42004-07-22 15:26:23 +000087void TwoAddressInstructionPass::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercfa0f2e2005-01-02 02:34:12 +000088 AU.addRequired<LiveVariables>();
Misha Brukman75fa4e42004-07-22 15:26:23 +000089 AU.addPreserved<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000090 AU.addPreservedID(MachineLoopInfoID);
91 AU.addPreservedID(MachineDominatorsID);
Misha Brukman75fa4e42004-07-22 15:26:23 +000092 AU.addPreservedID(PHIEliminationID);
93 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000094}
95
Evan Cheng875357d2008-03-13 06:37:55 +000096/// Sink3AddrInstruction - A two-address instruction has been converted to a
97/// three-address instruction to avoid clobbering a register. Try to sink it
98/// past the instruction that would kill the above mentioned register to
99/// reduce register pressure.
100bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
101 MachineInstr *MI, unsigned SavedReg,
102 MachineBasicBlock::iterator OldPos) {
103 // Check if it's safe to move this instruction.
104 bool SeenStore = true; // Be conservative.
105 if (!MI->isSafeToMove(TII, SeenStore))
106 return false;
107
108 unsigned DefReg = 0;
109 SmallSet<unsigned, 4> UseRegs;
110 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 }
140 if (!KillMI || KillMI->getParent() != MBB)
141 return false;
142
143 // If any of the definitions are used by another instruction between
144 // the position and the kill use, then it's not safe to sink it.
145 // FIXME: This can be sped up if there is an easy way to query whether
146 // an instruction if before or after another instruction. Then we can
147 // use MachineRegisterInfo def / use instead.
148 MachineOperand *KillMO = NULL;
149 MachineBasicBlock::iterator KillPos = KillMI;
150 ++KillPos;
151 for (MachineBasicBlock::iterator I = next(OldPos); I != KillPos; ++I) {
152 MachineInstr *OtherMI = I;
153 for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
154 MachineOperand &MO = OtherMI->getOperand(i);
155 if (!MO.isRegister())
156 continue;
157 unsigned MOReg = MO.getReg();
158 if (!MOReg)
159 continue;
160 if (DefReg == MOReg)
161 return false;
162 if (MO.isKill()) {
163 if (OtherMI == KillMI && MOReg == SavedReg)
164 // Save the operand that kills the register. We want unset the kill
165 // marker is we can sink MI past it.
166 KillMO = &MO;
167 else if (UseRegs.count(MOReg))
168 // One of the uses is killed before the destination.
169 return false;
170 }
171 }
172 }
173
174 if (SinkLimit != -1 && Num3AddrSunk == (unsigned)SinkLimit)
175 return false;
176
177 // Update kill and LV information.
178 KillMO->setIsKill(false);
179 KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI);
180 KillMO->setIsKill(true);
181 LiveVariables::VarInfo& VarInfo = LV->getVarInfo(SavedReg);
182 VarInfo.removeKill(KillMI);
183 VarInfo.Kills.push_back(MI);
184
185 // Move instruction to its destination.
186 MBB->remove(MI);
187 MBB->insert(KillPos, MI);
188
189 ++Num3AddrSunk;
190 return true;
191}
192
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000193/// runOnMachineFunction - Reduce two-address instructions to two
Chris Lattner163c1e72004-01-31 21:14:04 +0000194/// operands.
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000195///
Chris Lattner163c1e72004-01-31 21:14:04 +0000196bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000197 DOUT << "Machine Function\n";
Misha Brukman75fa4e42004-07-22 15:26:23 +0000198 const TargetMachine &TM = MF.getTarget();
Evan Cheng875357d2008-03-13 06:37:55 +0000199 MRI = &MF.getRegInfo();
200 TII = TM.getInstrInfo();
201 TRI = TM.getRegisterInfo();
202 LV = &getAnalysis<LiveVariables>();
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000203
Misha Brukman75fa4e42004-07-22 15:26:23 +0000204 bool MadeChange = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000205
Bill Wendlinga09362e2006-11-28 22:48:48 +0000206 DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n";
207 DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
Alkis Evlogimenos3a9986f2004-02-18 00:35:06 +0000208
Misha Brukman75fa4e42004-07-22 15:26:23 +0000209 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
210 mbbi != mbbe; ++mbbi) {
211 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
212 mi != me; ++mi) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000213 const TargetInstrDesc &TID = mi->getDesc();
Chris Lattner163c1e72004-01-31 21:14:04 +0000214
Evan Cheng360c2dd2006-11-01 23:06:55 +0000215 bool FirstTied = true;
Chris Lattner749c6f62008-01-07 07:27:27 +0000216 for (unsigned si = 1, e = TID.getNumOperands(); si < e; ++si) {
217 int ti = TID.getOperandConstraint(si, TOI::TIED_TO);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000218 if (ti == -1)
219 continue;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000220
Evan Cheng360c2dd2006-11-01 23:06:55 +0000221 if (FirstTied) {
222 ++NumTwoAddressInstrs;
Bill Wendlingbcd24982006-12-07 20:28:15 +0000223 DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000224 }
225 FirstTied = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000226
Evan Cheng360c2dd2006-11-01 23:06:55 +0000227 assert(mi->getOperand(si).isRegister() && mi->getOperand(si).getReg() &&
228 mi->getOperand(si).isUse() && "two address instruction invalid");
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000229
Evan Cheng360c2dd2006-11-01 23:06:55 +0000230 // if the two operands are the same we just remove the use
231 // and mark the def as def&use, otherwise we have to insert a copy.
232 if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) {
233 // rewrite:
234 // a = b op c
235 // to:
236 // a = b
237 // a = a op c
238 unsigned regA = mi->getOperand(ti).getReg();
239 unsigned regB = mi->getOperand(si).getReg();
240
Dan Gohman6f0d0242008-02-10 18:45:23 +0000241 assert(TargetRegisterInfo::isVirtualRegister(regA) &&
242 TargetRegisterInfo::isVirtualRegister(regB) &&
Evan Cheng360c2dd2006-11-01 23:06:55 +0000243 "cannot update physical register live information");
Chris Lattner6b507672004-01-31 21:21:43 +0000244
Chris Lattner1e313632004-07-21 23:17:57 +0000245#ifndef NDEBUG
Evan Cheng360c2dd2006-11-01 23:06:55 +0000246 // First, verify that we don't have a use of a in the instruction (a =
247 // b + a for example) because our transformation will not work. This
248 // should never occur because we are in SSA form.
249 for (unsigned i = 0; i != mi->getNumOperands(); ++i)
250 assert((int)i == ti ||
251 !mi->getOperand(i).isRegister() ||
252 mi->getOperand(i).getReg() != regA);
Chris Lattner1e313632004-07-21 23:17:57 +0000253#endif
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000254
Evan Cheng360c2dd2006-11-01 23:06:55 +0000255 // If this instruction is not the killing user of B, see if we can
256 // rearrange the code to make it so. Making it the killing user will
257 // allow us to coalesce A and B together, eliminating the copy we are
258 // about to insert.
Evan Cheng6130f662008-03-05 00:59:57 +0000259 if (!mi->killsRegister(regB)) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000260 // If this instruction is commutative, check to see if C dies. If
261 // so, swap the B and C operands. This makes the live ranges of A
262 // and C joinable.
263 // FIXME: This code also works for A := B op C instructions.
Chris Lattner749c6f62008-01-07 07:27:27 +0000264 if (TID.isCommutable() && mi->getNumOperands() >= 3) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000265 assert(mi->getOperand(3-si).isRegister() &&
266 "Not a proper commutative instruction!");
267 unsigned regC = mi->getOperand(3-si).getReg();
Evan Cheng6130f662008-03-05 00:59:57 +0000268 if (mi->killsRegister(regC)) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000269 DOUT << "2addr: COMMUTING : " << *mi;
Evan Cheng875357d2008-03-13 06:37:55 +0000270 MachineInstr *NewMI = TII->commuteInstruction(mi);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000271 if (NewMI == 0) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000272 DOUT << "2addr: COMMUTING FAILED!\n";
Evan Cheng360c2dd2006-11-01 23:06:55 +0000273 } else {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000274 DOUT << "2addr: COMMUTED TO: " << *NewMI;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000275 // If the instruction changed to commute it, update livevar.
276 if (NewMI != mi) {
Evan Cheng875357d2008-03-13 06:37:55 +0000277 LV->instructionChanged(mi, NewMI); // Update live variables
Evan Cheng360c2dd2006-11-01 23:06:55 +0000278 mbbi->insert(mi, NewMI); // Insert the new inst
279 mbbi->erase(mi); // Nuke the old inst.
280 mi = NewMI;
281 }
282
283 ++NumCommuted;
284 regB = regC;
285 goto InstructionRearranged;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000286 }
Chris Lattnerc71d6942005-01-19 07:08:42 +0000287 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000288 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000289
290 // If this instruction is potentially convertible to a true
291 // three-address instruction,
Chris Lattner749c6f62008-01-07 07:27:27 +0000292 if (TID.isConvertibleTo3Addr()) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000293 // FIXME: This assumes there are no more operands which are tied
294 // to another register.
295#ifndef NDEBUG
Chris Lattner749c6f62008-01-07 07:27:27 +0000296 for (unsigned i = si+1, e = TID.getNumOperands(); i < e; ++i)
297 assert(TID.getOperandConstraint(i, TOI::TIED_TO) == -1);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000298#endif
299
Evan Cheng875357d2008-03-13 06:37:55 +0000300 if (MachineInstr *New=TII->convertToThreeAddress(mbbi, mi, *LV)) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000301 DOUT << "2addr: CONVERTING 2-ADDR: " << *mi;
302 DOUT << "2addr: TO 3-ADDR: " << *New;
Evan Cheng875357d2008-03-13 06:37:55 +0000303 bool Sunk = Sink3AddrInstruction(mbbi, New, regB, mi);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000304 mbbi->erase(mi); // Nuke the old inst.
Evan Cheng875357d2008-03-13 06:37:55 +0000305 if (!Sunk) mi = New;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000306 ++NumConvertedTo3Addr;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000307 // Done with this instruction.
308 break;
309 }
Evan Chengb9d5e7c2007-10-20 04:01:47 +0000310 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000311 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000312
313 InstructionRearranged:
Chris Lattner84bc5422007-12-31 04:13:23 +0000314 const TargetRegisterClass* rc = MF.getRegInfo().getRegClass(regA);
Evan Cheng875357d2008-03-13 06:37:55 +0000315 TII->copyRegToReg(*mbbi, mi, regA, regB, rc, rc);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000316
317 MachineBasicBlock::iterator prevMi = prior(mi);
Bill Wendlingbcd24982006-12-07 20:28:15 +0000318 DOUT << "\t\tprepend:\t"; DEBUG(prevMi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000319
Owen Andersona0185402007-11-08 01:20:48 +0000320 // update live variables for regB
Evan Cheng875357d2008-03-13 06:37:55 +0000321 LiveVariables::VarInfo& varInfoB = LV->getVarInfo(regB);
Owen Andersona0185402007-11-08 01:20:48 +0000322 // regB is used in this BB.
323 varInfoB.UsedBlocks[mbbi->getNumber()] = true;
Evan Cheng875357d2008-03-13 06:37:55 +0000324 if (LV->removeVirtualRegisterKilled(regB, mbbi, mi))
325 LV->addVirtualRegisterKilled(regB, prevMi);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000326
Evan Cheng875357d2008-03-13 06:37:55 +0000327 if (LV->removeVirtualRegisterDead(regB, mbbi, mi))
328 LV->addVirtualRegisterDead(regB, prevMi);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000329
330 // replace all occurences of regB with regA
331 for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
332 if (mi->getOperand(i).isRegister() &&
333 mi->getOperand(i).getReg() == regB)
334 mi->getOperand(i).setReg(regA);
335 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000336 }
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000337
Evan Cheng360c2dd2006-11-01 23:06:55 +0000338 assert(mi->getOperand(ti).isDef() && mi->getOperand(si).isUse());
339 mi->getOperand(ti).setReg(mi->getOperand(si).getReg());
340 MadeChange = true;
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000341
Bill Wendlingbcd24982006-12-07 20:28:15 +0000342 DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(*cerr.stream(), &TM));
Misha Brukman75fa4e42004-07-22 15:26:23 +0000343 }
Misha Brukman75fa4e42004-07-22 15:26:23 +0000344 }
345 }
346
347 return MadeChange;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000348}