blob: e3a38f6f5b58264e2af4f0e3321d153853535a6d [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 {
Devang Patel19974732007-05-03 01:11:54 +000053 static char ID; // Pass identifcation, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000054 TwoAddressInstructionPass() : MachineFunctionPass((intptr_t)&ID) {}
55
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
Devang Patel19974732007-05-03 01:11:54 +000062 char TwoAddressInstructionPass::ID = 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +000063 RegisterPass<TwoAddressInstructionPass>
Misha Brukman75fa4e42004-07-22 15:26:23 +000064 X("twoaddressinstruction", "Two-Address instruction pass");
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000065}
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000066
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000067const PassInfo *llvm::TwoAddressInstructionPassID = X.getPassInfo();
68
Misha Brukman75fa4e42004-07-22 15:26:23 +000069void TwoAddressInstructionPass::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercfa0f2e2005-01-02 02:34:12 +000070 AU.addRequired<LiveVariables>();
Misha Brukman75fa4e42004-07-22 15:26:23 +000071 AU.addPreserved<LiveVariables>();
72 AU.addPreservedID(PHIEliminationID);
73 MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000074}
75
76/// runOnMachineFunction - Reduce two-address instructions to two
Chris Lattner163c1e72004-01-31 21:14:04 +000077/// operands.
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000078///
Chris Lattner163c1e72004-01-31 21:14:04 +000079bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlinga09362e2006-11-28 22:48:48 +000080 DOUT << "Machine Function\n";
Misha Brukman75fa4e42004-07-22 15:26:23 +000081 const TargetMachine &TM = MF.getTarget();
Misha Brukman75fa4e42004-07-22 15:26:23 +000082 const TargetInstrInfo &TII = *TM.getInstrInfo();
Evan Chengba59a1e2006-12-01 21:52:58 +000083 const MRegisterInfo &MRI = *TM.getRegisterInfo();
Chris Lattnercfa0f2e2005-01-02 02:34:12 +000084 LiveVariables &LV = getAnalysis<LiveVariables>();
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000085
Misha Brukman75fa4e42004-07-22 15:26:23 +000086 bool MadeChange = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000087
Bill Wendlinga09362e2006-11-28 22:48:48 +000088 DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n";
89 DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
Alkis Evlogimenos3a9986f2004-02-18 00:35:06 +000090
Misha Brukman75fa4e42004-07-22 15:26:23 +000091 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
92 mbbi != mbbe; ++mbbi) {
93 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
94 mi != me; ++mi) {
Evan Chengba59a1e2006-12-01 21:52:58 +000095 const TargetInstrDescriptor *TID = mi->getInstrDescriptor();
Chris Lattner163c1e72004-01-31 21:14:04 +000096
Evan Cheng360c2dd2006-11-01 23:06:55 +000097 bool FirstTied = true;
Evan Chengba59a1e2006-12-01 21:52:58 +000098 for (unsigned si = 1, e = TID->numOperands; si < e; ++si) {
99 int ti = TID->getOperandConstraint(si, TOI::TIED_TO);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000100 if (ti == -1)
101 continue;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000102
Evan Cheng360c2dd2006-11-01 23:06:55 +0000103 if (FirstTied) {
104 ++NumTwoAddressInstrs;
Bill Wendlingbcd24982006-12-07 20:28:15 +0000105 DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000106 }
107 FirstTied = false;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000108
Evan Cheng360c2dd2006-11-01 23:06:55 +0000109 assert(mi->getOperand(si).isRegister() && mi->getOperand(si).getReg() &&
110 mi->getOperand(si).isUse() && "two address instruction invalid");
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000111
Evan Cheng360c2dd2006-11-01 23:06:55 +0000112 // if the two operands are the same we just remove the use
113 // and mark the def as def&use, otherwise we have to insert a copy.
114 if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) {
115 // rewrite:
116 // a = b op c
117 // to:
118 // a = b
119 // a = a op c
120 unsigned regA = mi->getOperand(ti).getReg();
121 unsigned regB = mi->getOperand(si).getReg();
122
123 assert(MRegisterInfo::isVirtualRegister(regA) &&
124 MRegisterInfo::isVirtualRegister(regB) &&
125 "cannot update physical register live information");
Chris Lattner6b507672004-01-31 21:21:43 +0000126
Chris Lattner1e313632004-07-21 23:17:57 +0000127#ifndef NDEBUG
Evan Cheng360c2dd2006-11-01 23:06:55 +0000128 // First, verify that we don't have a use of a in the instruction (a =
129 // b + a for example) because our transformation will not work. This
130 // should never occur because we are in SSA form.
131 for (unsigned i = 0; i != mi->getNumOperands(); ++i)
132 assert((int)i == ti ||
133 !mi->getOperand(i).isRegister() ||
134 mi->getOperand(i).getReg() != regA);
Chris Lattner1e313632004-07-21 23:17:57 +0000135#endif
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000136
Evan Cheng360c2dd2006-11-01 23:06:55 +0000137 // If this instruction is not the killing user of B, see if we can
138 // rearrange the code to make it so. Making it the killing user will
139 // allow us to coalesce A and B together, eliminating the copy we are
140 // about to insert.
141 if (!LV.KillsRegister(mi, regB)) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000142 // If this instruction is commutative, check to see if C dies. If
143 // so, swap the B and C operands. This makes the live ranges of A
144 // and C joinable.
145 // FIXME: This code also works for A := B op C instructions.
Evan Chengba59a1e2006-12-01 21:52:58 +0000146 if ((TID->Flags & M_COMMUTABLE) && mi->getNumOperands() == 3) {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000147 assert(mi->getOperand(3-si).isRegister() &&
148 "Not a proper commutative instruction!");
149 unsigned regC = mi->getOperand(3-si).getReg();
150 if (LV.KillsRegister(mi, regC)) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000151 DOUT << "2addr: COMMUTING : " << *mi;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000152 MachineInstr *NewMI = TII.commuteInstruction(mi);
153 if (NewMI == 0) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000154 DOUT << "2addr: COMMUTING FAILED!\n";
Evan Cheng360c2dd2006-11-01 23:06:55 +0000155 } else {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000156 DOUT << "2addr: COMMUTED TO: " << *NewMI;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000157 // If the instruction changed to commute it, update livevar.
158 if (NewMI != mi) {
159 LV.instructionChanged(mi, NewMI); // Update live variables
160 mbbi->insert(mi, NewMI); // Insert the new inst
161 mbbi->erase(mi); // Nuke the old inst.
162 mi = NewMI;
163 }
164
165 ++NumCommuted;
166 regB = regC;
167 goto InstructionRearranged;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000168 }
Chris Lattnerc71d6942005-01-19 07:08:42 +0000169 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000170 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000171
172 // If this instruction is potentially convertible to a true
173 // three-address instruction,
Evan Chengba59a1e2006-12-01 21:52:58 +0000174 if (TID->Flags & M_CONVERTIBLE_TO_3_ADDR)
Evan Cheng360c2dd2006-11-01 23:06:55 +0000175 // FIXME: This assumes there are no more operands which are tied
176 // to another register.
177#ifndef NDEBUG
Evan Chengba59a1e2006-12-01 21:52:58 +0000178 for (unsigned i = si+1, e = TID->numOperands; i < e; ++i)
179 assert(TID->getOperandConstraint(i, TOI::TIED_TO) == -1);
Evan Cheng360c2dd2006-11-01 23:06:55 +0000180#endif
181
Evan Chengba59a1e2006-12-01 21:52:58 +0000182 if (MachineInstr *New = TII.convertToThreeAddress(mbbi, mi, LV)) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000183 DOUT << "2addr: CONVERTING 2-ADDR: " << *mi;
184 DOUT << "2addr: TO 3-ADDR: " << *New;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000185 mbbi->erase(mi); // Nuke the old inst.
186 mi = New;
187 ++NumConvertedTo3Addr;
Evan Cheng360c2dd2006-11-01 23:06:55 +0000188 // Done with this instruction.
189 break;
190 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000191 }
Evan Cheng360c2dd2006-11-01 23:06:55 +0000192
193 InstructionRearranged:
194 const TargetRegisterClass* rc = MF.getSSARegMap()->getRegClass(regA);
195 MRI.copyRegToReg(*mbbi, mi, regA, regB, rc);
196
197 MachineBasicBlock::iterator prevMi = prior(mi);
Bill Wendlingbcd24982006-12-07 20:28:15 +0000198 DOUT << "\t\tprepend:\t"; DEBUG(prevMi->print(*cerr.stream(), &TM));
Evan Cheng360c2dd2006-11-01 23:06:55 +0000199
200 // Update live variables for regA
201 LiveVariables::VarInfo& varInfo = LV.getVarInfo(regA);
202 varInfo.DefInst = prevMi;
203
Evan Cheng360c2dd2006-11-01 23:06:55 +0000204 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}