blob: fa3ceff75979e57f47312a95e5070eeb31730a9c [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 Lattnerf8c68f62006-06-28 22:17:39 +000041#include "llvm/Support/Visibility.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000042#include "llvm/ADT/Statistic.h"
43#include "llvm/ADT/STLExtras.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000044#include <iostream>
Alkis Evlogimenos71499de2003-12-18 13:06:04 +000045using namespace llvm;
46
47namespace {
Chris Lattnercfa0f2e2005-01-02 02:34:12 +000048 Statistic<> NumTwoAddressInstrs("twoaddressinstruction",
Misha Brukman75fa4e42004-07-22 15:26:23 +000049 "Number of two-address instructions");
Chris Lattnercfa0f2e2005-01-02 02:34:12 +000050 Statistic<> NumCommuted("twoaddressinstruction",
Chris Lattnerc60e6022005-10-26 18:41:41 +000051 "Number of instructions commuted to coalesce");
Chris Lattnercfa0f2e2005-01-02 02:34:12 +000052 Statistic<> NumConvertedTo3Addr("twoaddressinstruction",
53 "Number of instructions promoted to 3-address");
Chris Lattnerbd91c1c2004-01-31 21:07:15 +000054
Chris Lattnerf8c68f62006-06-28 22:17:39 +000055 struct VISIBILITY_HIDDEN TwoAddressInstructionPass
56 : public MachineFunctionPass {
Misha Brukman75fa4e42004-07-22 15:26:23 +000057 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000058
Misha Brukman75fa4e42004-07-22 15:26:23 +000059 /// runOnMachineFunction - pass entry point
60 bool runOnMachineFunction(MachineFunction&);
61 };
Alkis Evlogimenos4c080862003-12-18 22:40:24 +000062
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) {
Misha Brukman75fa4e42004-07-22 15:26:23 +000080 DEBUG(std::cerr << "Machine Function\n");
81 const TargetMachine &TM = MF.getTarget();
82 const MRegisterInfo &MRI = *TM.getRegisterInfo();
83 const TargetInstrInfo &TII = *TM.getInstrInfo();
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
Misha Brukman75fa4e42004-07-22 15:26:23 +000088 DEBUG(std::cerr << "********** REWRITING TWO-ADDR INSTRS **********\n");
89 DEBUG(std::cerr << "********** Function: "
90 << MF.getFunction()->getName() << '\n');
Alkis Evlogimenos3a9986f2004-02-18 00:35:06 +000091
Misha Brukman75fa4e42004-07-22 15:26:23 +000092 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
93 mbbi != mbbe; ++mbbi) {
94 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
95 mi != me; ++mi) {
96 unsigned opcode = mi->getOpcode();
Chris Lattner163c1e72004-01-31 21:14:04 +000097
Misha Brukman75fa4e42004-07-22 15:26:23 +000098 // ignore if it is not a two-address instruction
99 if (!TII.isTwoAddrInstr(opcode))
100 continue;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000101
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000102 ++NumTwoAddressInstrs;
Misha Brukman75fa4e42004-07-22 15:26:23 +0000103 DEBUG(std::cerr << '\t'; mi->print(std::cerr, &TM));
104 assert(mi->getOperand(1).isRegister() && mi->getOperand(1).getReg() &&
105 mi->getOperand(1).isUse() && "two address instruction invalid");
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000106
Misha Brukman75fa4e42004-07-22 15:26:23 +0000107 // if the two operands are the same we just remove the use
108 // and mark the def as def&use, otherwise we have to insert a copy.
109 if (mi->getOperand(0).getReg() != mi->getOperand(1).getReg()) {
110 // rewrite:
111 // a = b op c
112 // to:
113 // a = b
114 // a = a op c
115 unsigned regA = mi->getOperand(0).getReg();
116 unsigned regB = mi->getOperand(1).getReg();
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000117
Misha Brukman75fa4e42004-07-22 15:26:23 +0000118 assert(MRegisterInfo::isVirtualRegister(regA) &&
119 MRegisterInfo::isVirtualRegister(regB) &&
120 "cannot update physical register live information");
Chris Lattner6b507672004-01-31 21:21:43 +0000121
Chris Lattner1e313632004-07-21 23:17:57 +0000122#ifndef NDEBUG
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000123 // First, verify that we do not have a use of a in the instruction (a =
124 // b + a for example) because our transformation will not work. This
125 // should never occur because we are in SSA form.
Misha Brukman75fa4e42004-07-22 15:26:23 +0000126 for (unsigned i = 1; i != mi->getNumOperands(); ++i)
127 assert(!mi->getOperand(i).isRegister() ||
128 mi->getOperand(i).getReg() != regA);
Chris Lattner1e313632004-07-21 23:17:57 +0000129#endif
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000130
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000131 // If this instruction is not the killing user of B, see if we can
132 // rearrange the code to make it so. Making it the killing user will
Chris Lattnerc60e6022005-10-26 18:41:41 +0000133 // allow us to coalesce A and B together, eliminating the copy we are
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000134 // about to insert.
135 if (!LV.KillsRegister(mi, regB)) {
136 const TargetInstrDescriptor &TID = TII.get(opcode);
137
138 // If this instruction is commutative, check to see if C dies. If so,
139 // swap the B and C operands. This makes the live ranges of A and C
140 // joinable.
141 if (TID.Flags & M_COMMUTABLE) {
142 assert(mi->getOperand(2).isRegister() &&
143 "Not a proper commutative instruction!");
144 unsigned regC = mi->getOperand(2).getReg();
145 if (LV.KillsRegister(mi, regC)) {
146 DEBUG(std::cerr << "2addr: COMMUTING : " << *mi);
Chris Lattnerc71d6942005-01-19 07:08:42 +0000147 MachineInstr *NewMI = TII.commuteInstruction(mi);
148 if (NewMI == 0) {
149 DEBUG(std::cerr << "2addr: COMMUTING FAILED!\n");
150 } else {
151 DEBUG(std::cerr << "2addr: COMMUTED TO: " << *NewMI);
152 // If the instruction changed to commute it, update livevar.
153 if (NewMI != mi) {
154 LV.instructionChanged(mi, NewMI); // Update live variables
155 mbbi->insert(mi, NewMI); // Insert the new inst
156 mbbi->erase(mi); // Nuke the old inst.
157 mi = NewMI;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000158 }
Chris Lattnerc71d6942005-01-19 07:08:42 +0000159
160 ++NumCommuted;
161 regB = regC;
162 goto InstructionRearranged;
163 }
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000164 }
165 }
166 // If this instruction is potentially convertible to a true
Misha Brukmanedf128a2005-04-21 22:36:52 +0000167 // three-address instruction,
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000168 if (TID.Flags & M_CONVERTIBLE_TO_3_ADDR)
169 if (MachineInstr *New = TII.convertToThreeAddress(mi)) {
170 DEBUG(std::cerr << "2addr: CONVERTING 2-ADDR: " << *mi);
171 DEBUG(std::cerr << "2addr: TO 3-ADDR: " << *New);
172 LV.instructionChanged(mi, New); // Update live variables
173 mbbi->insert(mi, New); // Insert the new inst
174 mbbi->erase(mi); // Nuke the old inst.
175 mi = New;
176 ++NumConvertedTo3Addr;
177 assert(!TII.isTwoAddrInstr(New->getOpcode()) &&
178 "convertToThreeAddress returned a 2-addr instruction??");
179 // Done with this instruction.
180 continue;
181 }
182 }
183 InstructionRearranged:
Misha Brukman75fa4e42004-07-22 15:26:23 +0000184 const TargetRegisterClass* rc = MF.getSSARegMap()->getRegClass(regA);
Chris Lattner078fee32004-08-15 22:14:31 +0000185 MRI.copyRegToReg(*mbbi, mi, regA, regB, rc);
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000186
Misha Brukman75fa4e42004-07-22 15:26:23 +0000187 MachineBasicBlock::iterator prevMi = prior(mi);
188 DEBUG(std::cerr << "\t\tprepend:\t"; prevMi->print(std::cerr, &TM));
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000189
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000190 // Update live variables for regA
191 LiveVariables::VarInfo& varInfo = LV.getVarInfo(regA);
192 varInfo.DefInst = prevMi;
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000193
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000194 // update live variables for regB
195 if (LV.removeVirtualRegisterKilled(regB, mbbi, mi))
196 LV.addVirtualRegisterKilled(regB, prevMi);
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000197
Chris Lattnercfa0f2e2005-01-02 02:34:12 +0000198 if (LV.removeVirtualRegisterDead(regB, mbbi, mi))
199 LV.addVirtualRegisterDead(regB, prevMi);
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000200
Misha Brukman75fa4e42004-07-22 15:26:23 +0000201 // replace all occurences of regB with regA
202 for (unsigned i = 1, e = mi->getNumOperands(); i != e; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000203 if (mi->getOperand(i).isRegister() &&
Misha Brukman75fa4e42004-07-22 15:26:23 +0000204 mi->getOperand(i).getReg() == regB)
Chris Lattnere53f4a02006-05-04 17:52:23 +0000205 mi->getOperand(i).setReg(regA);
Misha Brukman75fa4e42004-07-22 15:26:23 +0000206 }
207 }
208
209 assert(mi->getOperand(0).isDef());
210 mi->getOperand(0).setUse();
211 mi->RemoveOperand(1);
212 MadeChange = true;
213
214 DEBUG(std::cerr << "\t\trewrite to:\t"; mi->print(std::cerr, &TM));
215 }
216 }
217
218 return MadeChange;
Alkis Evlogimenos71499de2003-12-18 13:06:04 +0000219}