1 //===-- TwoAddressInstructionPass.cpp - Two-Address instruction pass ------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the TwoAddress instruction pass which is used
11 // by most register allocators. Two-Address instructions are rewritten
21 // 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
25 // It is also worth noting that the duplicate operand of the two
26 // address instruction is removed.
28 //===----------------------------------------------------------------------===//
30 #define DEBUG_TYPE "twoaddrinstr"
31 #include "llvm/CodeGen/Passes.h"
32 #include "llvm/Function.h"
33 #include "llvm/CodeGen/LiveVariables.h"
34 #include "llvm/CodeGen/MachineFunctionPass.h"
35 #include "llvm/CodeGen/MachineInstr.h"
36 #include "llvm/CodeGen/SSARegMap.h"
37 #include "llvm/Target/MRegisterInfo.h"
38 #include "llvm/Target/TargetInstrInfo.h"
39 #include "llvm/Target/TargetMachine.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/Compiler.h"
42 #include "llvm/ADT/Statistic.h"
43 #include "llvm/ADT/STLExtras.h"
48 static Statistic<> NumTwoAddressInstrs("twoaddressinstruction",
49 "Number of two-address instructions");
50 static Statistic<> NumCommuted("twoaddressinstruction",
51 "Number of instructions commuted to coalesce");
52 static Statistic<> NumConvertedTo3Addr("twoaddressinstruction",
53 "Number of instructions promoted to 3-address");
55 struct VISIBILITY_HIDDEN TwoAddressInstructionPass
56 : public MachineFunctionPass {
57 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
59 /// runOnMachineFunction - pass entry point
60 bool runOnMachineFunction(MachineFunction&);
63 RegisterPass<TwoAddressInstructionPass>
64 X("twoaddressinstruction", "Two-Address instruction pass");
67 const PassInfo *llvm::TwoAddressInstructionPassID = X.getPassInfo();
69 void TwoAddressInstructionPass::getAnalysisUsage(AnalysisUsage &AU) const {
70 AU.addRequired<LiveVariables>();
71 AU.addPreserved<LiveVariables>();
72 AU.addPreservedID(PHIEliminationID);
73 MachineFunctionPass::getAnalysisUsage(AU);
76 /// runOnMachineFunction - Reduce two-address instructions to two
79 bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
80 DOUT << "Machine Function\n";
81 const TargetMachine &TM = MF.getTarget();
82 const MRegisterInfo &MRI = *TM.getRegisterInfo();
83 const TargetInstrInfo &TII = *TM.getInstrInfo();
84 LiveVariables &LV = getAnalysis<LiveVariables>();
86 bool MadeChange = false;
88 DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n";
89 DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
91 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
92 mbbi != mbbe; ++mbbi) {
93 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
95 unsigned opcode = mi->getOpcode();
97 bool FirstTied = true;
98 for (unsigned si = 1, e = TII.getNumOperands(opcode); si < e; ++si) {
99 int ti = TII.getOperandConstraint(opcode, si, TargetInstrInfo::TIED_TO);
104 ++NumTwoAddressInstrs;
105 DOUT << '\t'; DEBUG(mi->print(std::cerr, &TM));
109 assert(mi->getOperand(si).isRegister() && mi->getOperand(si).getReg() &&
110 mi->getOperand(si).isUse() && "two address instruction invalid");
112 // 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()) {
120 unsigned regA = mi->getOperand(ti).getReg();
121 unsigned regB = mi->getOperand(si).getReg();
123 assert(MRegisterInfo::isVirtualRegister(regA) &&
124 MRegisterInfo::isVirtualRegister(regB) &&
125 "cannot update physical register live information");
128 // 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);
137 // 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
141 if (!LV.KillsRegister(mi, regB)) {
142 const TargetInstrDescriptor &TID = TII.get(opcode);
144 // If this instruction is commutative, check to see if C dies. If
145 // so, swap the B and C operands. This makes the live ranges of A
147 // FIXME: This code also works for A := B op C instructions.
148 if ((TID.Flags & M_COMMUTABLE) && mi->getNumOperands() == 3) {
149 assert(mi->getOperand(3-si).isRegister() &&
150 "Not a proper commutative instruction!");
151 unsigned regC = mi->getOperand(3-si).getReg();
152 if (LV.KillsRegister(mi, regC)) {
153 DOUT << "2addr: COMMUTING : " << *mi;
154 MachineInstr *NewMI = TII.commuteInstruction(mi);
156 DOUT << "2addr: COMMUTING FAILED!\n";
158 DOUT << "2addr: COMMUTED TO: " << *NewMI;
159 // If the instruction changed to commute it, update livevar.
161 LV.instructionChanged(mi, NewMI); // Update live variables
162 mbbi->insert(mi, NewMI); // Insert the new inst
163 mbbi->erase(mi); // Nuke the old inst.
169 goto InstructionRearranged;
174 // If this instruction is potentially convertible to a true
175 // three-address instruction,
176 if (TID.Flags & M_CONVERTIBLE_TO_3_ADDR)
177 // FIXME: This assumes there are no more operands which are tied
178 // to another register.
180 for (unsigned i = si+1, e = TII.getNumOperands(opcode); i < e; ++i)
181 assert(TII.getOperandConstraint(opcode, i,
182 TargetInstrInfo::TIED_TO) == -1);
185 if (MachineInstr *New = TII.convertToThreeAddress(mi)) {
186 DOUT << "2addr: CONVERTING 2-ADDR: " << *mi;
187 DOUT << "2addr: TO 3-ADDR: " << *New;
188 LV.instructionChanged(mi, New); // Update live variables
189 mbbi->insert(mi, New); // Insert the new inst
190 mbbi->erase(mi); // Nuke the old inst.
192 ++NumConvertedTo3Addr;
193 // Done with this instruction.
198 InstructionRearranged:
199 const TargetRegisterClass* rc = MF.getSSARegMap()->getRegClass(regA);
200 MRI.copyRegToReg(*mbbi, mi, regA, regB, rc);
202 MachineBasicBlock::iterator prevMi = prior(mi);
203 DOUT << "\t\tprepend:\t"; DEBUG(prevMi->print(std::cerr, &TM));
205 // Update live variables for regA
206 LiveVariables::VarInfo& varInfo = LV.getVarInfo(regA);
207 varInfo.DefInst = prevMi;
209 // update live variables for regB
210 if (LV.removeVirtualRegisterKilled(regB, mbbi, mi))
211 LV.addVirtualRegisterKilled(regB, prevMi);
213 if (LV.removeVirtualRegisterDead(regB, mbbi, mi))
214 LV.addVirtualRegisterDead(regB, prevMi);
216 // replace all occurences of regB with regA
217 for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
218 if (mi->getOperand(i).isRegister() &&
219 mi->getOperand(i).getReg() == regB)
220 mi->getOperand(i).setReg(regA);
224 assert(mi->getOperand(ti).isDef() && mi->getOperand(si).isUse());
225 mi->getOperand(ti).setReg(mi->getOperand(si).getReg());
228 DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(std::cerr, &TM));