blob: b14a95e406c5eba273111bdca144446799d0137a [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
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");
Chris Lattnerbd91c1c2004-01-31 21:07:15 +000049
Chris Lattnercd3245a2006-12-19 22:41:21 +000050namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000051 struct VISIBILITY_HIDDEN TwoAddressInstructionPass
52 : public MachineFunctionPass {
Misha Brukman75fa4e42004-07-22 15:26:23 +000053 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000054
Misha Brukman75fa4e42004-07-22 15:26:23 +000055 /// runOnMachineFunction - pass entry point
56 bool runOnMachineFunction(MachineFunction&);
57 };
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000058
Misha Brukmanedf128a2005-04-21 22:36:52 +000059 RegisterPass<TwoAddressInstructionPass>
Misha Brukman75fa4e42004-07-22 15:26:23 +000060 X("twoaddressinstruction", "Two-Address instruction pass");
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000061}
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000062
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000063const PassInfo *llvm::TwoAddressInstructionPassID = X.getPassInfo();
64
Misha Brukman75fa4e42004-07-22 15:26:23 +000065void TwoAddressInstructionPass::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercfa0f2e2005-01-02 02:34:12 +000066 AU.addRequired<LiveVariables>();
Misha Brukman75fa4e42004-07-22 15:26:23 +000067 AU.addPreserved<LiveVariables>();
68 AU.addPreservedID(PHIEliminationID);
69 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000070}
71
72/// runOnMachineFunction - Reduce two-address instructions to two
Chris Lattner163c1e72004-01-31 21:14:04 +000073/// operands.
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000074///
Chris Lattner163c1e72004-01-31 21:14:04 +000075bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlinga09362e2006-11-28 22:48:48 +000076 DOUT << "Machine Function\n";
Misha Brukman75fa4e42004-07-22 15:26:23 +000077 const TargetMachine &TM = MF.getTarget();
Misha Brukman75fa4e42004-07-22 15:26:23 +000078 const TargetInstrInfo &TII = *TM.getInstrInfo();
Evan Chengba59a1e2006-12-01 21:52:58 +000079 const MRegisterInfo &MRI = *TM.getRegisterInfo();
Chris Lattnercfa0f2e2005-01-02 02:34:12 +000080 LiveVariables &LV = getAnalysis<LiveVariables>();
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000081
Misha Brukman75fa4e42004-07-22 15:26:23 +000082 bool MadeChange = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000083
Bill Wendlinga09362e2006-11-28 22:48:48 +000084 DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n";
85 DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
Alkis Evlogimenos3a9986f2004-02-18 00:35:06 +000086
Misha Brukman75fa4e42004-07-22 15:26:23 +000087 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
88 mbbi != mbbe; ++mbbi) {
89 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
90 mi != me; ++mi) {
Evan Chengba59a1e2006-12-01 21:52:58 +000091 const TargetInstrDescriptor *TID = mi->getInstrDescriptor();
Chris Lattner163c1e72004-01-31 21:14:04 +000092
Evan Cheng360c2dd2006-11-01 23:06:55 +000093 bool FirstTied = true;
Evan Chengba59a1e2006-12-01 21:52:58 +000094 for (unsigned si = 1, e = TID->numOperands; si < e; ++si) {
95 int ti = TID->getOperandConstraint(si, TOI::TIED_TO);
Evan Cheng360c2dd2006-11-01 23:06:55 +000096 if (ti == -1)
97 continue;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000098
Evan Cheng360c2dd2006-11-01 23:06:55 +000099 if (FirstTied) {
100 ++NumTwoAddressInstrs;
Bill Wendlingbcd24982006-12-07 20:28:15 +0000101 DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000102 }
103 FirstTied = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000104
Evan Cheng360c2dd2006-11-01 23:06:55 +0000105 assert(mi->getOperand(si).isRegister() && mi->getOperand(si).getReg() &&
106 mi->getOperand(si).isUse() && "two address instruction invalid");
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000107
Evan Cheng360c2dd2006-11-01 23:06:55 +0000108 // if the two operands are the same we just remove the use
109 // and mark the def as def&use, otherwise we have to insert a copy.
110 if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) {
111 // rewrite:
112 // a = b op c
113 // to:
114 // a = b
115 // a = a op c
116 unsigned regA = mi->getOperand(ti).getReg();
117 unsigned regB = mi->getOperand(si).getReg();
118
119 assert(MRegisterInfo::isVirtualRegister(regA) &&
120 MRegisterInfo::isVirtualRegister(regB) &&
121 "cannot update physical register live information");
Chris Lattner6b507672004-01-31 21:21:43 +0000122
Chris Lattner1e313632004-07-21 23:17:57 +0000123#ifndef NDEBUG
Evan Cheng360c2dd2006-11-01 23:06:55 +0000124 // First, verify that we don't have a use of a in the instruction (a =
125 // b + a for example) because our transformation will not work. This
126 // should never occur because we are in SSA form.
127 for (unsigned i = 0; i != mi->getNumOperands(); ++i)
128 assert((int)i == ti ||
129 !mi->getOperand(i).isRegister() ||
130 mi->getOperand(i).getReg() != regA);
Chris Lattner1e313632004-07-21 23:17:57 +0000131#endif
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000132
Evan Cheng360c2dd2006-11-01 23:06:55 +0000133 // If this instruction is not the killing user of B, see if we can
134 // rearrange the code to make it so. Making it the killing user will
135 // allow us to coalesce A and B together, eliminating the copy we are
136 // about to insert.
137 if (!LV.KillsRegister(mi, regB)) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000138 // If this instruction is commutative, check to see if C dies. If
139 // so, swap the B and C operands. This makes the live ranges of A
140 // and C joinable.
141 // FIXME: This code also works for A := B op C instructions.
Evan Chengba59a1e2006-12-01 21:52:58 +0000142 if ((TID->Flags & M_COMMUTABLE) && mi->getNumOperands() == 3) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000143 assert(mi->getOperand(3-si).isRegister() &&
144 "Not a proper commutative instruction!");
145 unsigned regC = mi->getOperand(3-si).getReg();
146 if (LV.KillsRegister(mi, regC)) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000147 DOUT << "2addr: COMMUTING : " << *mi;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000148 MachineInstr *NewMI = TII.commuteInstruction(mi);
149 if (NewMI == 0) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000150 DOUT << "2addr: COMMUTING FAILED!\n";
Evan Cheng360c2dd2006-11-01 23:06:55 +0000151 } else {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000152 DOUT << "2addr: COMMUTED TO: " << *NewMI;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000153 // If the instruction changed to commute it, update livevar.
154 if (NewMI != mi) {
155 LV.instructionChanged(mi, NewMI); // Update live variables
156 mbbi->insert(mi, NewMI); // Insert the new inst
157 mbbi->erase(mi); // Nuke the old inst.
158 mi = NewMI;
159 }
160
161 ++NumCommuted;
162 regB = regC;
163 goto InstructionRearranged;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000164 }
Chris Lattnerc71d6942005-01-19 07:08:42 +0000165 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000166 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000167
168 // If this instruction is potentially convertible to a true
169 // three-address instruction,
Evan Chengba59a1e2006-12-01 21:52:58 +0000170 if (TID->Flags & M_CONVERTIBLE_TO_3_ADDR)
Evan Cheng360c2dd2006-11-01 23:06:55 +0000171 // FIXME: This assumes there are no more operands which are tied
172 // to another register.
173#ifndef NDEBUG
Evan Chengba59a1e2006-12-01 21:52:58 +0000174 for (unsigned i = si+1, e = TID->numOperands; i < e; ++i)
175 assert(TID->getOperandConstraint(i, TOI::TIED_TO) == -1);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000176#endif
177
Evan Chengba59a1e2006-12-01 21:52:58 +0000178 if (MachineInstr *New = TII.convertToThreeAddress(mbbi, mi, LV)) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000179 DOUT << "2addr: CONVERTING 2-ADDR: " << *mi;
180 DOUT << "2addr: TO 3-ADDR: " << *New;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000181 mbbi->erase(mi); // Nuke the old inst.
182 mi = New;
183 ++NumConvertedTo3Addr;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000184 // Done with this instruction.
185 break;
186 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000187 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000188
189 InstructionRearranged:
190 const TargetRegisterClass* rc = MF.getSSARegMap()->getRegClass(regA);
191 MRI.copyRegToReg(*mbbi, mi, regA, regB, rc);
192
193 MachineBasicBlock::iterator prevMi = prior(mi);
Bill Wendlingbcd24982006-12-07 20:28:15 +0000194 DOUT << "\t\tprepend:\t"; DEBUG(prevMi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000195
196 // Update live variables for regA
197 LiveVariables::VarInfo& varInfo = LV.getVarInfo(regA);
198 varInfo.DefInst = prevMi;
199
Evan Cheng360c2dd2006-11-01 23:06:55 +0000200 if (LV.removeVirtualRegisterKilled(regB, mbbi, mi))
201 LV.addVirtualRegisterKilled(regB, prevMi);
202
203 if (LV.removeVirtualRegisterDead(regB, mbbi, mi))
204 LV.addVirtualRegisterDead(regB, prevMi);
205
206 // replace all occurences of regB with regA
207 for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
208 if (mi->getOperand(i).isRegister() &&
209 mi->getOperand(i).getReg() == regB)
210 mi->getOperand(i).setReg(regA);
211 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000212 }
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000213
Evan Cheng360c2dd2006-11-01 23:06:55 +0000214 assert(mi->getOperand(ti).isDef() && mi->getOperand(si).isUse());
215 mi->getOperand(ti).setReg(mi->getOperand(si).getReg());
216 MadeChange = true;
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000217
Bill Wendlingbcd24982006-12-07 20:28:15 +0000218 DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(*cerr.stream(), &TM));
Misha Brukman75fa4e42004-07-22 15:26:23 +0000219 }
Misha Brukman75fa4e42004-07-22 15:26:23 +0000220 }
221 }
222
223 return MadeChange;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000224}