blob: 73803279110730ff4e861ecb8cab449ed14f84bf [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"
Reid Spencer551ccae2004-09-01 22:55:40 +000042#include "llvm/ADT/Statistic.h"
43#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000044using namespace llvm;
45
Chris Lattnercd3245a2006-12-19 22:41:21 +000046STATISTIC(NumTwoAddressInstrs, "Number of two-address instructions");
47STATISTIC(NumCommuted , "Number of instructions commuted to coalesce");
48STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address");
Evan Cheng875357d2008-03-13 06:37:55 +000049STATISTIC(Num3AddrSunk, "Number of 3-address instructions sunk");
50
51namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000052 struct VISIBILITY_HIDDEN TwoAddressInstructionPass
53 : public MachineFunctionPass {
Evan Cheng875357d2008-03-13 06:37:55 +000054 const TargetInstrInfo *TII;
55 const TargetRegisterInfo *TRI;
56 MachineRegisterInfo *MRI;
57 LiveVariables *LV;
58
59 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000060 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000061 TwoAddressInstructionPass() : MachineFunctionPass((intptr_t)&ID) {}
62
Misha Brukman75fa4e42004-07-22 15:26:23 +000063 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000064
Misha Brukman75fa4e42004-07-22 15:26:23 +000065 /// runOnMachineFunction - pass entry point
66 bool runOnMachineFunction(MachineFunction&);
Evan Cheng875357d2008-03-13 06:37:55 +000067
68 private:
69 bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI,
70 unsigned Reg,
71 MachineBasicBlock::iterator OldPos);
Misha Brukman75fa4e42004-07-22 15:26:23 +000072 };
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000073
Devang Patel19974732007-05-03 01:11:54 +000074 char TwoAddressInstructionPass::ID = 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +000075 RegisterPass<TwoAddressInstructionPass>
Misha Brukman75fa4e42004-07-22 15:26:23 +000076 X("twoaddressinstruction", "Two-Address instruction pass");
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000077}
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000078
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000079const PassInfo *llvm::TwoAddressInstructionPassID = X.getPassInfo();
80
Misha Brukman75fa4e42004-07-22 15:26:23 +000081void TwoAddressInstructionPass::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercfa0f2e2005-01-02 02:34:12 +000082 AU.addRequired<LiveVariables>();
Misha Brukman75fa4e42004-07-22 15:26:23 +000083 AU.addPreserved<LiveVariables>();
Bill Wendling67d65bb2008-01-04 20:54:55 +000084 AU.addPreservedID(MachineLoopInfoID);
85 AU.addPreservedID(MachineDominatorsID);
Misha Brukman75fa4e42004-07-22 15:26:23 +000086 AU.addPreservedID(PHIEliminationID);
87 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000088}
89
Evan Cheng875357d2008-03-13 06:37:55 +000090/// Sink3AddrInstruction - A two-address instruction has been converted to a
91/// three-address instruction to avoid clobbering a register. Try to sink it
92/// past the instruction that would kill the above mentioned register to
93/// reduce register pressure.
94bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
95 MachineInstr *MI, unsigned SavedReg,
96 MachineBasicBlock::iterator OldPos) {
97 // Check if it's safe to move this instruction.
98 bool SeenStore = true; // Be conservative.
99 if (!MI->isSafeToMove(TII, SeenStore))
100 return false;
101
102 unsigned DefReg = 0;
103 SmallSet<unsigned, 4> UseRegs;
104 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
105 const MachineOperand &MO = MI->getOperand(i);
106 if (!MO.isRegister())
107 continue;
108 unsigned MOReg = MO.getReg();
109 if (!MOReg)
110 continue;
111 if (MO.isUse() && MOReg != SavedReg)
112 UseRegs.insert(MO.getReg());
113 if (!MO.isDef())
114 continue;
115 if (MO.isImplicit())
116 // Don't try to move it if it implicitly defines a register.
117 return false;
118 if (DefReg)
119 // For now, don't move any instructions that define multiple registers.
120 return false;
121 DefReg = MO.getReg();
122 }
123
124 // Find the instruction that kills SavedReg.
125 MachineInstr *KillMI = NULL;
126 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SavedReg),
127 UE = MRI->use_end(); UI != UE; ++UI) {
128 MachineOperand &UseMO = UI.getOperand();
129 if (!UseMO.isKill())
130 continue;
131 KillMI = UseMO.getParent();
132 break;
133 }
134 if (!KillMI || KillMI->getParent() != MBB)
135 return false;
136
137 // If any of the definitions are used by another instruction between
138 // the position and the kill use, then it's not safe to sink it.
139 // FIXME: This can be sped up if there is an easy way to query whether
140 // an instruction if before or after another instruction. Then we can
141 // use MachineRegisterInfo def / use instead.
142 MachineOperand *KillMO = NULL;
143 MachineBasicBlock::iterator KillPos = KillMI;
144 ++KillPos;
145 for (MachineBasicBlock::iterator I = next(OldPos); I != KillPos; ++I) {
146 MachineInstr *OtherMI = I;
147 for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
148 MachineOperand &MO = OtherMI->getOperand(i);
149 if (!MO.isRegister())
150 continue;
151 unsigned MOReg = MO.getReg();
152 if (!MOReg)
153 continue;
154 if (DefReg == MOReg)
155 return false;
156 if (MO.isKill()) {
157 if (OtherMI == KillMI && MOReg == SavedReg)
158 // Save the operand that kills the register. We want unset the kill
159 // marker is we can sink MI past it.
160 KillMO = &MO;
161 else if (UseRegs.count(MOReg))
162 // One of the uses is killed before the destination.
163 return false;
164 }
165 }
166 }
167
Evan Cheng875357d2008-03-13 06:37:55 +0000168 // Update kill and LV information.
169 KillMO->setIsKill(false);
170 KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI);
171 KillMO->setIsKill(true);
172 LiveVariables::VarInfo& VarInfo = LV->getVarInfo(SavedReg);
173 VarInfo.removeKill(KillMI);
174 VarInfo.Kills.push_back(MI);
175
176 // Move instruction to its destination.
177 MBB->remove(MI);
178 MBB->insert(KillPos, MI);
179
180 ++Num3AddrSunk;
181 return true;
182}
183
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000184/// runOnMachineFunction - Reduce two-address instructions to two
Chris Lattner163c1e72004-01-31 21:14:04 +0000185/// operands.
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000186///
Chris Lattner163c1e72004-01-31 21:14:04 +0000187bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000188 DOUT << "Machine Function\n";
Misha Brukman75fa4e42004-07-22 15:26:23 +0000189 const TargetMachine &TM = MF.getTarget();
Evan Cheng875357d2008-03-13 06:37:55 +0000190 MRI = &MF.getRegInfo();
191 TII = TM.getInstrInfo();
192 TRI = TM.getRegisterInfo();
193 LV = &getAnalysis<LiveVariables>();
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000194
Misha Brukman75fa4e42004-07-22 15:26:23 +0000195 bool MadeChange = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000196
Bill Wendlinga09362e2006-11-28 22:48:48 +0000197 DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n";
198 DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
Alkis Evlogimenos3a9986f2004-02-18 00:35:06 +0000199
Misha Brukman75fa4e42004-07-22 15:26:23 +0000200 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
201 mbbi != mbbe; ++mbbi) {
202 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
203 mi != me; ++mi) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000204 const TargetInstrDesc &TID = mi->getDesc();
Chris Lattner163c1e72004-01-31 21:14:04 +0000205
Evan Cheng360c2dd2006-11-01 23:06:55 +0000206 bool FirstTied = true;
Chris Lattner749c6f62008-01-07 07:27:27 +0000207 for (unsigned si = 1, e = TID.getNumOperands(); si < e; ++si) {
208 int ti = TID.getOperandConstraint(si, TOI::TIED_TO);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000209 if (ti == -1)
210 continue;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000211
Evan Cheng360c2dd2006-11-01 23:06:55 +0000212 if (FirstTied) {
213 ++NumTwoAddressInstrs;
Bill Wendlingbcd24982006-12-07 20:28:15 +0000214 DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000215 }
216 FirstTied = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000217
Evan Cheng360c2dd2006-11-01 23:06:55 +0000218 assert(mi->getOperand(si).isRegister() && mi->getOperand(si).getReg() &&
219 mi->getOperand(si).isUse() && "two address instruction invalid");
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000220
Evan Cheng360c2dd2006-11-01 23:06:55 +0000221 // if the two operands are the same we just remove the use
222 // and mark the def as def&use, otherwise we have to insert a copy.
223 if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) {
224 // rewrite:
225 // a = b op c
226 // to:
227 // a = b
228 // a = a op c
229 unsigned regA = mi->getOperand(ti).getReg();
230 unsigned regB = mi->getOperand(si).getReg();
231
Dan Gohman6f0d0242008-02-10 18:45:23 +0000232 assert(TargetRegisterInfo::isVirtualRegister(regA) &&
233 TargetRegisterInfo::isVirtualRegister(regB) &&
Evan Cheng360c2dd2006-11-01 23:06:55 +0000234 "cannot update physical register live information");
Chris Lattner6b507672004-01-31 21:21:43 +0000235
Chris Lattner1e313632004-07-21 23:17:57 +0000236#ifndef NDEBUG
Evan Cheng360c2dd2006-11-01 23:06:55 +0000237 // First, verify that we don't have a use of a in the instruction (a =
238 // b + a for example) because our transformation will not work. This
239 // should never occur because we are in SSA form.
240 for (unsigned i = 0; i != mi->getNumOperands(); ++i)
241 assert((int)i == ti ||
242 !mi->getOperand(i).isRegister() ||
243 mi->getOperand(i).getReg() != regA);
Chris Lattner1e313632004-07-21 23:17:57 +0000244#endif
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000245
Evan Cheng360c2dd2006-11-01 23:06:55 +0000246 // If this instruction is not the killing user of B, see if we can
247 // rearrange the code to make it so. Making it the killing user will
248 // allow us to coalesce A and B together, eliminating the copy we are
249 // about to insert.
Evan Cheng6130f662008-03-05 00:59:57 +0000250 if (!mi->killsRegister(regB)) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000251 // If this instruction is commutative, check to see if C dies. If
252 // so, swap the B and C operands. This makes the live ranges of A
253 // and C joinable.
254 // FIXME: This code also works for A := B op C instructions.
Chris Lattner749c6f62008-01-07 07:27:27 +0000255 if (TID.isCommutable() && mi->getNumOperands() >= 3) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000256 assert(mi->getOperand(3-si).isRegister() &&
257 "Not a proper commutative instruction!");
258 unsigned regC = mi->getOperand(3-si).getReg();
Evan Cheng6130f662008-03-05 00:59:57 +0000259 if (mi->killsRegister(regC)) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000260 DOUT << "2addr: COMMUTING : " << *mi;
Evan Cheng875357d2008-03-13 06:37:55 +0000261 MachineInstr *NewMI = TII->commuteInstruction(mi);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000262 if (NewMI == 0) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000263 DOUT << "2addr: COMMUTING FAILED!\n";
Evan Cheng360c2dd2006-11-01 23:06:55 +0000264 } else {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000265 DOUT << "2addr: COMMUTED TO: " << *NewMI;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000266 // If the instruction changed to commute it, update livevar.
267 if (NewMI != mi) {
Evan Cheng875357d2008-03-13 06:37:55 +0000268 LV->instructionChanged(mi, NewMI); // Update live variables
Evan Cheng360c2dd2006-11-01 23:06:55 +0000269 mbbi->insert(mi, NewMI); // Insert the new inst
270 mbbi->erase(mi); // Nuke the old inst.
271 mi = NewMI;
272 }
273
274 ++NumCommuted;
275 regB = regC;
276 goto InstructionRearranged;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000277 }
Chris Lattnerc71d6942005-01-19 07:08:42 +0000278 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000279 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000280
281 // If this instruction is potentially convertible to a true
282 // three-address instruction,
Chris Lattner749c6f62008-01-07 07:27:27 +0000283 if (TID.isConvertibleTo3Addr()) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000284 // FIXME: This assumes there are no more operands which are tied
285 // to another register.
286#ifndef NDEBUG
Chris Lattner749c6f62008-01-07 07:27:27 +0000287 for (unsigned i = si+1, e = TID.getNumOperands(); i < e; ++i)
288 assert(TID.getOperandConstraint(i, TOI::TIED_TO) == -1);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000289#endif
290
Evan Cheng875357d2008-03-13 06:37:55 +0000291 if (MachineInstr *New=TII->convertToThreeAddress(mbbi, mi, *LV)) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000292 DOUT << "2addr: CONVERTING 2-ADDR: " << *mi;
293 DOUT << "2addr: TO 3-ADDR: " << *New;
Evan Cheng875357d2008-03-13 06:37:55 +0000294 bool Sunk = Sink3AddrInstruction(mbbi, New, regB, mi);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000295 mbbi->erase(mi); // Nuke the old inst.
Evan Cheng875357d2008-03-13 06:37:55 +0000296 if (!Sunk) mi = New;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000297 ++NumConvertedTo3Addr;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000298 // Done with this instruction.
299 break;
300 }
Evan Chengb9d5e7c2007-10-20 04:01:47 +0000301 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000302 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000303
304 InstructionRearranged:
Chris Lattner84bc5422007-12-31 04:13:23 +0000305 const TargetRegisterClass* rc = MF.getRegInfo().getRegClass(regA);
Evan Cheng875357d2008-03-13 06:37:55 +0000306 TII->copyRegToReg(*mbbi, mi, regA, regB, rc, rc);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000307
308 MachineBasicBlock::iterator prevMi = prior(mi);
Bill Wendlingbcd24982006-12-07 20:28:15 +0000309 DOUT << "\t\tprepend:\t"; DEBUG(prevMi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000310
Owen Andersona0185402007-11-08 01:20:48 +0000311 // update live variables for regB
Evan Cheng875357d2008-03-13 06:37:55 +0000312 LiveVariables::VarInfo& varInfoB = LV->getVarInfo(regB);
Owen Andersona0185402007-11-08 01:20:48 +0000313 // regB is used in this BB.
314 varInfoB.UsedBlocks[mbbi->getNumber()] = true;
Evan Cheng875357d2008-03-13 06:37:55 +0000315 if (LV->removeVirtualRegisterKilled(regB, mbbi, mi))
316 LV->addVirtualRegisterKilled(regB, prevMi);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000317
Evan Cheng875357d2008-03-13 06:37:55 +0000318 if (LV->removeVirtualRegisterDead(regB, mbbi, mi))
319 LV->addVirtualRegisterDead(regB, prevMi);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000320
321 // replace all occurences of regB with regA
322 for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
323 if (mi->getOperand(i).isRegister() &&
324 mi->getOperand(i).getReg() == regB)
325 mi->getOperand(i).setReg(regA);
326 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000327 }
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000328
Evan Cheng360c2dd2006-11-01 23:06:55 +0000329 assert(mi->getOperand(ti).isDef() && mi->getOperand(si).isUse());
330 mi->getOperand(ti).setReg(mi->getOperand(si).getReg());
331 MadeChange = true;
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000332
Bill Wendlingbcd24982006-12-07 20:28:15 +0000333 DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(*cerr.stream(), &TM));
Misha Brukman75fa4e42004-07-22 15:26:23 +0000334 }
Misha Brukman75fa4e42004-07-22 15:26:23 +0000335 }
336 }
337
338 return MadeChange;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000339}