blob: f343ff40d812d93ea095814397055c7b6c5a30e7 [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 {
Bill Wendling637980e2008-05-10 00:12:52 +000052 class 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
Bill Wendling637980e2008-05-10 00:12:52 +000059 bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI,
60 unsigned Reg,
61 MachineBasicBlock::iterator OldPos);
Evan Cheng875357d2008-03-13 06:37:55 +000062 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000063 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000064 TwoAddressInstructionPass() : MachineFunctionPass((intptr_t)&ID) {}
65
Bill Wendling637980e2008-05-10 00:12:52 +000066 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
67 AU.addRequired<LiveVariables>();
68 AU.addPreserved<LiveVariables>();
69 AU.addPreservedID(MachineLoopInfoID);
70 AU.addPreservedID(MachineDominatorsID);
71 AU.addPreservedID(PHIEliminationID);
72 MachineFunctionPass::getAnalysisUsage(AU);
73 }
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000074
Bill Wendling637980e2008-05-10 00:12:52 +000075 /// runOnMachineFunction - Pass entry point.
Misha Brukman75fa4e42004-07-22 15:26:23 +000076 bool runOnMachineFunction(MachineFunction&);
77 };
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000078}
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000079
Dan Gohman844731a2008-05-13 00:00:25 +000080char TwoAddressInstructionPass::ID = 0;
81static RegisterPass<TwoAddressInstructionPass>
82X("twoaddressinstruction", "Two-Address instruction pass");
83
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000084const PassInfo *llvm::TwoAddressInstructionPassID = X.getPassInfo();
85
Evan Cheng875357d2008-03-13 06:37:55 +000086/// Sink3AddrInstruction - A two-address instruction has been converted to a
87/// three-address instruction to avoid clobbering a register. Try to sink it
Bill Wendling637980e2008-05-10 00:12:52 +000088/// past the instruction that would kill the above mentioned register to reduce
89/// register pressure.
90///
Evan Cheng875357d2008-03-13 06:37:55 +000091bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
92 MachineInstr *MI, unsigned SavedReg,
93 MachineBasicBlock::iterator OldPos) {
94 // Check if it's safe to move this instruction.
95 bool SeenStore = true; // Be conservative.
96 if (!MI->isSafeToMove(TII, SeenStore))
97 return false;
98
99 unsigned DefReg = 0;
100 SmallSet<unsigned, 4> UseRegs;
Bill Wendling637980e2008-05-10 00:12:52 +0000101
Evan Cheng875357d2008-03-13 06:37:55 +0000102 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
103 const MachineOperand &MO = MI->getOperand(i);
104 if (!MO.isRegister())
105 continue;
106 unsigned MOReg = MO.getReg();
107 if (!MOReg)
108 continue;
109 if (MO.isUse() && MOReg != SavedReg)
110 UseRegs.insert(MO.getReg());
111 if (!MO.isDef())
112 continue;
113 if (MO.isImplicit())
114 // Don't try to move it if it implicitly defines a register.
115 return false;
116 if (DefReg)
117 // For now, don't move any instructions that define multiple registers.
118 return false;
119 DefReg = MO.getReg();
120 }
121
122 // Find the instruction that kills SavedReg.
123 MachineInstr *KillMI = NULL;
Bill Wendling637980e2008-05-10 00:12:52 +0000124
Evan Cheng875357d2008-03-13 06:37:55 +0000125 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SavedReg),
126 UE = MRI->use_end(); UI != UE; ++UI) {
127 MachineOperand &UseMO = UI.getOperand();
128 if (!UseMO.isKill())
129 continue;
130 KillMI = UseMO.getParent();
131 break;
132 }
Bill Wendling637980e2008-05-10 00:12:52 +0000133
Evan Cheng875357d2008-03-13 06:37:55 +0000134 if (!KillMI || KillMI->getParent() != MBB)
135 return false;
136
Bill Wendling637980e2008-05-10 00:12:52 +0000137 // If any of the definitions are used by another instruction between the
138 // position and the kill use, then it's not safe to sink it.
139 //
140 // FIXME: This can be sped up if there is an easy way to query whether an
141 // instruction if before or after another instruction. Then we can use
142 // MachineRegisterInfo def / use instead.
Evan Cheng875357d2008-03-13 06:37:55 +0000143 MachineOperand *KillMO = NULL;
144 MachineBasicBlock::iterator KillPos = KillMI;
145 ++KillPos;
Bill Wendling637980e2008-05-10 00:12:52 +0000146
Evan Cheng875357d2008-03-13 06:37:55 +0000147 for (MachineBasicBlock::iterator I = next(OldPos); I != KillPos; ++I) {
148 MachineInstr *OtherMI = I;
Bill Wendling637980e2008-05-10 00:12:52 +0000149
Evan Cheng875357d2008-03-13 06:37:55 +0000150 for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
151 MachineOperand &MO = OtherMI->getOperand(i);
152 if (!MO.isRegister())
153 continue;
154 unsigned MOReg = MO.getReg();
155 if (!MOReg)
156 continue;
157 if (DefReg == MOReg)
158 return false;
Bill Wendling637980e2008-05-10 00:12:52 +0000159
Evan Cheng875357d2008-03-13 06:37:55 +0000160 if (MO.isKill()) {
161 if (OtherMI == KillMI && MOReg == SavedReg)
162 // Save the operand that kills the register. We want unset the kill
163 // marker is we can sink MI past it.
164 KillMO = &MO;
165 else if (UseRegs.count(MOReg))
166 // One of the uses is killed before the destination.
167 return false;
168 }
169 }
170 }
171
Evan Cheng875357d2008-03-13 06:37:55 +0000172 // Update kill and LV information.
173 KillMO->setIsKill(false);
174 KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI);
175 KillMO->setIsKill(true);
176 LiveVariables::VarInfo& VarInfo = LV->getVarInfo(SavedReg);
177 VarInfo.removeKill(KillMI);
178 VarInfo.Kills.push_back(MI);
179
180 // Move instruction to its destination.
181 MBB->remove(MI);
182 MBB->insert(KillPos, MI);
183
184 ++Num3AddrSunk;
185 return true;
186}
187
Bill Wendling637980e2008-05-10 00:12:52 +0000188/// runOnMachineFunction - Reduce two-address instructions to two operands.
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000189///
Chris Lattner163c1e72004-01-31 21:14:04 +0000190bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000191 DOUT << "Machine Function\n";
Misha Brukman75fa4e42004-07-22 15:26:23 +0000192 const TargetMachine &TM = MF.getTarget();
Evan Cheng875357d2008-03-13 06:37:55 +0000193 MRI = &MF.getRegInfo();
194 TII = TM.getInstrInfo();
195 TRI = TM.getRegisterInfo();
196 LV = &getAnalysis<LiveVariables>();
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000197
Misha Brukman75fa4e42004-07-22 15:26:23 +0000198 bool MadeChange = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000199
Bill Wendlinga09362e2006-11-28 22:48:48 +0000200 DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n";
201 DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
Alkis Evlogimenos3a9986f2004-02-18 00:35:06 +0000202
Misha Brukman75fa4e42004-07-22 15:26:23 +0000203 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
204 mbbi != mbbe; ++mbbi) {
205 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
Evan Cheng7a963fa2008-03-27 01:27:25 +0000206 mi != me; ) {
207 MachineBasicBlock::iterator nmi = next(mi);
Chris Lattner749c6f62008-01-07 07:27:27 +0000208 const TargetInstrDesc &TID = mi->getDesc();
Evan Cheng360c2dd2006-11-01 23:06:55 +0000209 bool FirstTied = true;
Bill Wendling637980e2008-05-10 00:12:52 +0000210
Chris Lattner749c6f62008-01-07 07:27:27 +0000211 for (unsigned si = 1, e = TID.getNumOperands(); si < e; ++si) {
212 int ti = TID.getOperandConstraint(si, TOI::TIED_TO);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000213 if (ti == -1)
214 continue;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000215
Evan Cheng360c2dd2006-11-01 23:06:55 +0000216 if (FirstTied) {
217 ++NumTwoAddressInstrs;
Bill Wendlingbcd24982006-12-07 20:28:15 +0000218 DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000219 }
Bill Wendling637980e2008-05-10 00:12:52 +0000220
Evan Cheng360c2dd2006-11-01 23:06:55 +0000221 FirstTied = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000222
Evan Cheng360c2dd2006-11-01 23:06:55 +0000223 assert(mi->getOperand(si).isRegister() && mi->getOperand(si).getReg() &&
224 mi->getOperand(si).isUse() && "two address instruction invalid");
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000225
Bill Wendling637980e2008-05-10 00:12:52 +0000226 // If the two operands are the same we just remove the use
Evan Cheng360c2dd2006-11-01 23:06:55 +0000227 // and mark the def as def&use, otherwise we have to insert a copy.
228 if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) {
Bill Wendling637980e2008-05-10 00:12:52 +0000229 // Rewrite:
Evan Cheng360c2dd2006-11-01 23:06:55 +0000230 // a = b op c
231 // to:
232 // a = b
233 // a = a op c
234 unsigned regA = mi->getOperand(ti).getReg();
235 unsigned regB = mi->getOperand(si).getReg();
236
Dan Gohman6f0d0242008-02-10 18:45:23 +0000237 assert(TargetRegisterInfo::isVirtualRegister(regA) &&
238 TargetRegisterInfo::isVirtualRegister(regB) &&
Evan Cheng360c2dd2006-11-01 23:06:55 +0000239 "cannot update physical register live information");
Chris Lattner6b507672004-01-31 21:21:43 +0000240
Chris Lattner1e313632004-07-21 23:17:57 +0000241#ifndef NDEBUG
Evan Cheng360c2dd2006-11-01 23:06:55 +0000242 // First, verify that we don't have a use of a in the instruction (a =
243 // b + a for example) because our transformation will not work. This
244 // should never occur because we are in SSA form.
245 for (unsigned i = 0; i != mi->getNumOperands(); ++i)
246 assert((int)i == ti ||
247 !mi->getOperand(i).isRegister() ||
248 mi->getOperand(i).getReg() != regA);
Chris Lattner1e313632004-07-21 23:17:57 +0000249#endif
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000250
Evan Cheng360c2dd2006-11-01 23:06:55 +0000251 // If this instruction is not the killing user of B, see if we can
252 // rearrange the code to make it so. Making it the killing user will
253 // allow us to coalesce A and B together, eliminating the copy we are
254 // about to insert.
Evan Cheng6130f662008-03-05 00:59:57 +0000255 if (!mi->killsRegister(regB)) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000256 // If this instruction is commutative, check to see if C dies. If
257 // so, swap the B and C operands. This makes the live ranges of A
258 // and C joinable.
259 // FIXME: This code also works for A := B op C instructions.
Chris Lattner749c6f62008-01-07 07:27:27 +0000260 if (TID.isCommutable() && mi->getNumOperands() >= 3) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000261 assert(mi->getOperand(3-si).isRegister() &&
262 "Not a proper commutative instruction!");
263 unsigned regC = mi->getOperand(3-si).getReg();
Bill Wendling637980e2008-05-10 00:12:52 +0000264
Evan Cheng6130f662008-03-05 00:59:57 +0000265 if (mi->killsRegister(regC)) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000266 DOUT << "2addr: COMMUTING : " << *mi;
Evan Cheng875357d2008-03-13 06:37:55 +0000267 MachineInstr *NewMI = TII->commuteInstruction(mi);
Bill Wendling637980e2008-05-10 00:12:52 +0000268
Evan Cheng360c2dd2006-11-01 23:06:55 +0000269 if (NewMI == 0) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000270 DOUT << "2addr: COMMUTING FAILED!\n";
Evan Cheng360c2dd2006-11-01 23:06:55 +0000271 } else {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000272 DOUT << "2addr: COMMUTED TO: " << *NewMI;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000273 // If the instruction changed to commute it, update livevar.
274 if (NewMI != mi) {
Evan Cheng875357d2008-03-13 06:37:55 +0000275 LV->instructionChanged(mi, NewMI); // Update live variables
Evan Cheng360c2dd2006-11-01 23:06:55 +0000276 mbbi->insert(mi, NewMI); // Insert the new inst
277 mbbi->erase(mi); // Nuke the old inst.
278 mi = NewMI;
279 }
280
281 ++NumCommuted;
282 regB = regC;
283 goto InstructionRearranged;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000284 }
Chris Lattnerc71d6942005-01-19 07:08:42 +0000285 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000286 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000287
288 // If this instruction is potentially convertible to a true
289 // three-address instruction,
Chris Lattner749c6f62008-01-07 07:27:27 +0000290 if (TID.isConvertibleTo3Addr()) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000291 // FIXME: This assumes there are no more operands which are tied
292 // to another register.
293#ifndef NDEBUG
Bill Wendling637980e2008-05-10 00:12:52 +0000294 for (unsigned i = si + 1, e = TID.getNumOperands(); i < e; ++i)
Chris Lattner749c6f62008-01-07 07:27:27 +0000295 assert(TID.getOperandConstraint(i, TOI::TIED_TO) == -1);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000296#endif
297
Evan Cheng875357d2008-03-13 06:37:55 +0000298 if (MachineInstr *New=TII->convertToThreeAddress(mbbi, mi, *LV)) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000299 DOUT << "2addr: CONVERTING 2-ADDR: " << *mi;
300 DOUT << "2addr: TO 3-ADDR: " << *New;
Evan Cheng0099ae22008-03-13 07:56:58 +0000301 bool Sunk = false;
Bill Wendling637980e2008-05-10 00:12:52 +0000302
Evan Chenga2248682008-03-13 08:04:35 +0000303 if (New->findRegisterUseOperand(regB, false, TRI))
Evan Cheng0099ae22008-03-13 07:56:58 +0000304 // FIXME: Temporary workaround. If the new instruction doesn't
305 // uses regB, convertToThreeAddress must have created more
306 // then one instruction.
307 Sunk = Sink3AddrInstruction(mbbi, New, regB, mi);
Bill Wendling637980e2008-05-10 00:12:52 +0000308
309 mbbi->erase(mi); // Nuke the old inst.
310
Evan Cheng7a963fa2008-03-27 01:27:25 +0000311 if (!Sunk) {
312 mi = New;
313 nmi = next(mi);
314 }
Bill Wendling637980e2008-05-10 00:12:52 +0000315
Evan Cheng360c2dd2006-11-01 23:06:55 +0000316 ++NumConvertedTo3Addr;
Bill Wendling637980e2008-05-10 00:12:52 +0000317 break; // Done with this instruction.
Evan Cheng360c2dd2006-11-01 23:06:55 +0000318 }
Evan Chengb9d5e7c2007-10-20 04:01:47 +0000319 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000320 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000321
322 InstructionRearranged:
Chris Lattner84bc5422007-12-31 04:13:23 +0000323 const TargetRegisterClass* rc = MF.getRegInfo().getRegClass(regA);
Evan Cheng875357d2008-03-13 06:37:55 +0000324 TII->copyRegToReg(*mbbi, mi, regA, regB, rc, rc);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000325
326 MachineBasicBlock::iterator prevMi = prior(mi);
Bill Wendlingbcd24982006-12-07 20:28:15 +0000327 DOUT << "\t\tprepend:\t"; DEBUG(prevMi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000328
Bill Wendling637980e2008-05-10 00:12:52 +0000329 // Update live variables for regB.
Evan Cheng875357d2008-03-13 06:37:55 +0000330 LiveVariables::VarInfo& varInfoB = LV->getVarInfo(regB);
Bill Wendling637980e2008-05-10 00:12:52 +0000331
Owen Andersona0185402007-11-08 01:20:48 +0000332 // regB is used in this BB.
333 varInfoB.UsedBlocks[mbbi->getNumber()] = true;
Bill Wendling637980e2008-05-10 00:12:52 +0000334
Evan Cheng875357d2008-03-13 06:37:55 +0000335 if (LV->removeVirtualRegisterKilled(regB, mbbi, mi))
336 LV->addVirtualRegisterKilled(regB, prevMi);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000337
Evan Cheng875357d2008-03-13 06:37:55 +0000338 if (LV->removeVirtualRegisterDead(regB, mbbi, mi))
339 LV->addVirtualRegisterDead(regB, prevMi);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000340
Bill Wendling637980e2008-05-10 00:12:52 +0000341 // Replace all occurences of regB with regA.
Evan Cheng360c2dd2006-11-01 23:06:55 +0000342 for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
343 if (mi->getOperand(i).isRegister() &&
344 mi->getOperand(i).getReg() == regB)
345 mi->getOperand(i).setReg(regA);
346 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000347 }
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000348
Evan Cheng360c2dd2006-11-01 23:06:55 +0000349 assert(mi->getOperand(ti).isDef() && mi->getOperand(si).isUse());
350 mi->getOperand(ti).setReg(mi->getOperand(si).getReg());
351 MadeChange = true;
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000352
Bill Wendlingbcd24982006-12-07 20:28:15 +0000353 DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(*cerr.stream(), &TM));
Misha Brukman75fa4e42004-07-22 15:26:23 +0000354 }
Bill Wendling637980e2008-05-10 00:12:52 +0000355
Evan Cheng7a963fa2008-03-27 01:27:25 +0000356 mi = nmi;
Misha Brukman75fa4e42004-07-22 15:26:23 +0000357 }
358 }
359
360 return MadeChange;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000361}