blob: be19e46168e51d0e9fe0192c121f7993b38a3fd5 [file] [log] [blame]
Alkis Evlogimenos71499de2003-12-18 13:06:04 +00001//===-- TwoAddressInstructionPass.cpp - Two-Address instruction pass ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000036#include "llvm/CodeGen/SSARegMap.h"
37#include "llvm/Target/MRegisterInfo.h"
38#include "llvm/Target/TargetInstrInfo.h"
39#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000040#include "llvm/Support/Debug.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000041#include "llvm/Support/Compiler.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
46namespace {
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000047 static Statistic NumTwoAddressInstrs("twoaddressinstruction",
Misha Brukman75fa4e42004-07-22 15:26:23 +000048 "Number of two-address instructions");
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000049 static Statistic NumCommuted("twoaddressinstruction",
Chris Lattnerc60e6022005-10-26 18:41:41 +000050 "Number of instructions commuted to coalesce");
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000051 static Statistic NumConvertedTo3Addr("twoaddressinstruction",
Chris Lattnercfa0f2e2005-01-02 02:34:12 +000052 "Number of instructions promoted to 3-address");
Chris Lattnerbd91c1c2004-01-31 21:07:15 +000053
Chris Lattnerf8c68f62006-06-28 22:17:39 +000054 struct VISIBILITY_HIDDEN TwoAddressInstructionPass
55 : public MachineFunctionPass {
Misha Brukman75fa4e42004-07-22 15:26:23 +000056 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000057
Misha Brukman75fa4e42004-07-22 15:26:23 +000058 /// runOnMachineFunction - pass entry point
59 bool runOnMachineFunction(MachineFunction&);
60 };
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000061
Misha Brukmanedf128a2005-04-21 22:36:52 +000062 RegisterPass<TwoAddressInstructionPass>
Misha Brukman75fa4e42004-07-22 15:26:23 +000063 X("twoaddressinstruction", "Two-Address instruction pass");
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000064}
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000065
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000066const PassInfo *llvm::TwoAddressInstructionPassID = X.getPassInfo();
67
Misha Brukman75fa4e42004-07-22 15:26:23 +000068void TwoAddressInstructionPass::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercfa0f2e2005-01-02 02:34:12 +000069 AU.addRequired<LiveVariables>();
Misha Brukman75fa4e42004-07-22 15:26:23 +000070 AU.addPreserved<LiveVariables>();
71 AU.addPreservedID(PHIEliminationID);
72 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000073}
74
75/// runOnMachineFunction - Reduce two-address instructions to two
Chris Lattner163c1e72004-01-31 21:14:04 +000076/// operands.
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000077///
Chris Lattner163c1e72004-01-31 21:14:04 +000078bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlinga09362e2006-11-28 22:48:48 +000079 DOUT << "Machine Function\n";
Misha Brukman75fa4e42004-07-22 15:26:23 +000080 const TargetMachine &TM = MF.getTarget();
Misha Brukman75fa4e42004-07-22 15:26:23 +000081 const TargetInstrInfo &TII = *TM.getInstrInfo();
Evan Chengba59a1e2006-12-01 21:52:58 +000082 const MRegisterInfo &MRI = *TM.getRegisterInfo();
Chris Lattnercfa0f2e2005-01-02 02:34:12 +000083 LiveVariables &LV = getAnalysis<LiveVariables>();
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000084
Misha Brukman75fa4e42004-07-22 15:26:23 +000085 bool MadeChange = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000086
Bill Wendlinga09362e2006-11-28 22:48:48 +000087 DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n";
88 DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
Alkis Evlogimenos3a9986f2004-02-18 00:35:06 +000089
Misha Brukman75fa4e42004-07-22 15:26:23 +000090 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
91 mbbi != mbbe; ++mbbi) {
92 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
93 mi != me; ++mi) {
Evan Chengba59a1e2006-12-01 21:52:58 +000094 const TargetInstrDescriptor *TID = mi->getInstrDescriptor();
Chris Lattner163c1e72004-01-31 21:14:04 +000095
Evan Cheng360c2dd2006-11-01 23:06:55 +000096 bool FirstTied = true;
Evan Chengba59a1e2006-12-01 21:52:58 +000097 for (unsigned si = 1, e = TID->numOperands; si < e; ++si) {
98 int ti = TID->getOperandConstraint(si, TOI::TIED_TO);
Evan Cheng360c2dd2006-11-01 23:06:55 +000099 if (ti == -1)
100 continue;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000101
Evan Cheng360c2dd2006-11-01 23:06:55 +0000102 if (FirstTied) {
103 ++NumTwoAddressInstrs;
Bill Wendlingbcd24982006-12-07 20:28:15 +0000104 DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000105 }
106 FirstTied = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000107
Evan Cheng360c2dd2006-11-01 23:06:55 +0000108 assert(mi->getOperand(si).isRegister() && mi->getOperand(si).getReg() &&
109 mi->getOperand(si).isUse() && "two address instruction invalid");
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000110
Evan Cheng360c2dd2006-11-01 23:06:55 +0000111 // if the two operands are the same we just remove the use
112 // and mark the def as def&use, otherwise we have to insert a copy.
113 if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) {
114 // rewrite:
115 // a = b op c
116 // to:
117 // a = b
118 // a = a op c
119 unsigned regA = mi->getOperand(ti).getReg();
120 unsigned regB = mi->getOperand(si).getReg();
121
122 assert(MRegisterInfo::isVirtualRegister(regA) &&
123 MRegisterInfo::isVirtualRegister(regB) &&
124 "cannot update physical register live information");
Chris Lattner6b507672004-01-31 21:21:43 +0000125
Chris Lattner1e313632004-07-21 23:17:57 +0000126#ifndef NDEBUG
Evan Cheng360c2dd2006-11-01 23:06:55 +0000127 // First, verify that we don't have a use of a in the instruction (a =
128 // b + a for example) because our transformation will not work. This
129 // should never occur because we are in SSA form.
130 for (unsigned i = 0; i != mi->getNumOperands(); ++i)
131 assert((int)i == ti ||
132 !mi->getOperand(i).isRegister() ||
133 mi->getOperand(i).getReg() != regA);
Chris Lattner1e313632004-07-21 23:17:57 +0000134#endif
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000135
Evan Cheng360c2dd2006-11-01 23:06:55 +0000136 // If this instruction is not the killing user of B, see if we can
137 // rearrange the code to make it so. Making it the killing user will
138 // allow us to coalesce A and B together, eliminating the copy we are
139 // about to insert.
140 if (!LV.KillsRegister(mi, regB)) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000141 // If this instruction is commutative, check to see if C dies. If
142 // so, swap the B and C operands. This makes the live ranges of A
143 // and C joinable.
144 // FIXME: This code also works for A := B op C instructions.
Evan Chengba59a1e2006-12-01 21:52:58 +0000145 if ((TID->Flags & M_COMMUTABLE) && mi->getNumOperands() == 3) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000146 assert(mi->getOperand(3-si).isRegister() &&
147 "Not a proper commutative instruction!");
148 unsigned regC = mi->getOperand(3-si).getReg();
149 if (LV.KillsRegister(mi, regC)) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000150 DOUT << "2addr: COMMUTING : " << *mi;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000151 MachineInstr *NewMI = TII.commuteInstruction(mi);
152 if (NewMI == 0) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000153 DOUT << "2addr: COMMUTING FAILED!\n";
Evan Cheng360c2dd2006-11-01 23:06:55 +0000154 } else {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000155 DOUT << "2addr: COMMUTED TO: " << *NewMI;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000156 // If the instruction changed to commute it, update livevar.
157 if (NewMI != mi) {
158 LV.instructionChanged(mi, NewMI); // Update live variables
159 mbbi->insert(mi, NewMI); // Insert the new inst
160 mbbi->erase(mi); // Nuke the old inst.
161 mi = NewMI;
162 }
163
164 ++NumCommuted;
165 regB = regC;
166 goto InstructionRearranged;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000167 }
Chris Lattnerc71d6942005-01-19 07:08:42 +0000168 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000169 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000170
171 // If this instruction is potentially convertible to a true
172 // three-address instruction,
Evan Chengba59a1e2006-12-01 21:52:58 +0000173 if (TID->Flags & M_CONVERTIBLE_TO_3_ADDR)
Evan Cheng360c2dd2006-11-01 23:06:55 +0000174 // FIXME: This assumes there are no more operands which are tied
175 // to another register.
176#ifndef NDEBUG
Evan Chengba59a1e2006-12-01 21:52:58 +0000177 for (unsigned i = si+1, e = TID->numOperands; i < e; ++i)
178 assert(TID->getOperandConstraint(i, TOI::TIED_TO) == -1);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000179#endif
180
Evan Chengba59a1e2006-12-01 21:52:58 +0000181 if (MachineInstr *New = TII.convertToThreeAddress(mbbi, mi, LV)) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000182 DOUT << "2addr: CONVERTING 2-ADDR: " << *mi;
183 DOUT << "2addr: TO 3-ADDR: " << *New;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000184 mbbi->erase(mi); // Nuke the old inst.
185 mi = New;
186 ++NumConvertedTo3Addr;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000187 // Done with this instruction.
188 break;
189 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000190 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000191
192 InstructionRearranged:
193 const TargetRegisterClass* rc = MF.getSSARegMap()->getRegClass(regA);
194 MRI.copyRegToReg(*mbbi, mi, regA, regB, rc);
195
196 MachineBasicBlock::iterator prevMi = prior(mi);
Bill Wendlingbcd24982006-12-07 20:28:15 +0000197 DOUT << "\t\tprepend:\t"; DEBUG(prevMi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000198
199 // Update live variables for regA
200 LiveVariables::VarInfo& varInfo = LV.getVarInfo(regA);
201 varInfo.DefInst = prevMi;
202
203 // update live variables for regB
204 if (LV.removeVirtualRegisterKilled(regB, mbbi, mi))
205 LV.addVirtualRegisterKilled(regB, prevMi);
206
207 if (LV.removeVirtualRegisterDead(regB, mbbi, mi))
208 LV.addVirtualRegisterDead(regB, prevMi);
209
210 // replace all occurences of regB with regA
211 for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
212 if (mi->getOperand(i).isRegister() &&
213 mi->getOperand(i).getReg() == regB)
214 mi->getOperand(i).setReg(regA);
215 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000216 }
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000217
Evan Cheng360c2dd2006-11-01 23:06:55 +0000218 assert(mi->getOperand(ti).isDef() && mi->getOperand(si).isUse());
219 mi->getOperand(ti).setReg(mi->getOperand(si).getReg());
220 MadeChange = true;
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000221
Bill Wendlingbcd24982006-12-07 20:28:15 +0000222 DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(*cerr.stream(), &TM));
Misha Brukman75fa4e42004-07-22 15:26:23 +0000223 }
Misha Brukman75fa4e42004-07-22 15:26:23 +0000224 }
225 }
226
227 return MadeChange;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000228}