blob: 9a6e5bc3f0761977d9d013fe80a56471140f78fb [file] [log] [blame]
Alkis Evlogimenos725021c2003-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 Evlogimenos5e0e6712004-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 Evlogimenos32742642004-02-04 22:17:40 +000019// A op= C
Alkis Evlogimenos725021c2003-12-18 13:06:04 +000020//
Alkis Evlogimenos32742642004-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 Lattnerd835aa62004-01-31 21:07:15 +000027//
Alkis Evlogimenos725021c2003-12-18 13:06:04 +000028//===----------------------------------------------------------------------===//
29
30#define DEBUG_TYPE "twoaddrinstr"
Chris Lattnerd835aa62004-01-31 21:07:15 +000031#include "llvm/CodeGen/Passes.h"
Chris Lattneradbbc622004-07-21 23:17:57 +000032#include "llvm/Function.h"
Alkis Evlogimenos725021c2003-12-18 13:06:04 +000033#include "llvm/CodeGen/LiveVariables.h"
Alkis Evlogimenos725021c2003-12-18 13:06:04 +000034#include "llvm/CodeGen/MachineFunctionPass.h"
35#include "llvm/CodeGen/MachineInstr.h"
Alkis Evlogimenos725021c2003-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"
Alkis Evlogimenos725021c2003-12-18 13:06:04 +000040#include "Support/Debug.h"
41#include "Support/Statistic.h"
Alkis Evlogimenos5a922402004-02-14 01:18:34 +000042#include "Support/STLExtras.h"
Alkis Evlogimenos725021c2003-12-18 13:06:04 +000043using namespace llvm;
44
45namespace {
Chris Lattnerd835aa62004-01-31 21:07:15 +000046 Statistic<> numTwoAddressInstrs("twoaddressinstruction",
47 "Number of two-address instructions");
48 Statistic<> numInstrsAdded("twoaddressinstruction",
49 "Number of instructions added");
50
Chris Lattner626f4312004-01-31 21:14:04 +000051 struct TwoAddressInstructionPass : public MachineFunctionPass
Alkis Evlogimenos71390902003-12-18 22:40:24 +000052 {
Alkis Evlogimenos71390902003-12-18 22:40:24 +000053 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
54
Alkis Evlogimenos71390902003-12-18 22:40:24 +000055 /// runOnMachineFunction - pass entry point
56 bool runOnMachineFunction(MachineFunction&);
57 };
58
59 RegisterPass<TwoAddressInstructionPass> X(
Alkis Evlogimenos725021c2003-12-18 13:06:04 +000060 "twoaddressinstruction", "Two-Address instruction pass");
Alkis Evlogimenos725021c2003-12-18 13:06:04 +000061};
62
Alkis Evlogimenos71390902003-12-18 22:40:24 +000063const PassInfo *llvm::TwoAddressInstructionPassID = X.getPassInfo();
64
Alkis Evlogimenos725021c2003-12-18 13:06:04 +000065void TwoAddressInstructionPass::getAnalysisUsage(AnalysisUsage &AU) const
66{
67 AU.addPreserved<LiveVariables>();
Alkis Evlogimenos725021c2003-12-18 13:06:04 +000068 AU.addPreservedID(PHIEliminationID);
Alkis Evlogimenos725021c2003-12-18 13:06:04 +000069 MachineFunctionPass::getAnalysisUsage(AU);
70}
71
72/// runOnMachineFunction - Reduce two-address instructions to two
Chris Lattner626f4312004-01-31 21:14:04 +000073/// operands.
Alkis Evlogimenos725021c2003-12-18 13:06:04 +000074///
Chris Lattner626f4312004-01-31 21:14:04 +000075bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
Alkis Evlogimenos725021c2003-12-18 13:06:04 +000076 DEBUG(std::cerr << "Machine Function\n");
Chris Lattner626f4312004-01-31 21:14:04 +000077 const TargetMachine &TM = MF.getTarget();
78 const MRegisterInfo &MRI = *TM.getRegisterInfo();
Chris Lattner21505422004-06-02 05:57:12 +000079 const TargetInstrInfo &TII = *TM.getInstrInfo();
Alkis Evlogimenose27f33f2004-02-15 21:50:32 +000080 LiveVariables* LV = getAnalysisToUpdate<LiveVariables>();
Alkis Evlogimenos725021c2003-12-18 13:06:04 +000081
Chris Lattner626f4312004-01-31 21:14:04 +000082 bool MadeChange = false;
Alkis Evlogimenos725021c2003-12-18 13:06:04 +000083
Alkis Evlogimenos26583db2004-02-18 00:35:06 +000084 DEBUG(std::cerr << "********** REWRITING TWO-ADDR INSTRS **********\n");
85 DEBUG(std::cerr << "********** Function: "
86 << MF.getFunction()->getName() << '\n');
87
Chris Lattner626f4312004-01-31 21:14:04 +000088 for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
Alkis Evlogimenos725021c2003-12-18 13:06:04 +000089 mbbi != mbbe; ++mbbi) {
Alkis Evlogimenos80da8652004-02-12 02:27:10 +000090 for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
91 mi != me; ++mi) {
Alkis Evlogimenos725021c2003-12-18 13:06:04 +000092 unsigned opcode = mi->getOpcode();
Chris Lattner626f4312004-01-31 21:14:04 +000093
Alkis Evlogimenos725021c2003-12-18 13:06:04 +000094 // ignore if it is not a two-address instruction
Chris Lattner626f4312004-01-31 21:14:04 +000095 if (!TII.isTwoAddrInstr(opcode))
Alkis Evlogimenos725021c2003-12-18 13:06:04 +000096 continue;
97
98 ++numTwoAddressInstrs;
99
Tanya Lattner23dbc812004-06-25 00:13:11 +0000100 DEBUG(std::cerr << '\t'; mi->print(std::cerr, &TM));
Alkis Evlogimenos725021c2003-12-18 13:06:04 +0000101
Chris Lattnera2ef2962004-01-31 21:21:43 +0000102 assert(mi->getOperand(1).isRegister() &&
Alkis Evlogimenos8cdd0212004-02-13 21:01:20 +0000103 mi->getOperand(1).getReg() &&
Chris Lattnera2ef2962004-01-31 21:21:43 +0000104 mi->getOperand(1).isUse() &&
105 "two address instruction invalid");
106
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000107 // if the two operands are the same we just remove the use
Chris Lattneradbbc622004-07-21 23:17:57 +0000108 // and mark the def as def&use, otherwise we have to insert a copy.
109 if (mi->getOperand(0).getReg() != mi->getOperand(1).getReg()) {
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000110 // rewrite:
111 // a = b op c
112 // to:
113 // a = b
114 // a = a op c
Alkis Evlogimenos8cdd0212004-02-13 21:01:20 +0000115 unsigned regA = mi->getOperand(0).getReg();
116 unsigned regB = mi->getOperand(1).getReg();
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000117
118 assert(MRegisterInfo::isVirtualRegister(regA) &&
119 MRegisterInfo::isVirtualRegister(regB) &&
120 "cannot update physical register live information");
121
122 // first make sure we do not have a use of a in the
123 // instruction (a = b + a for example) because our
124 // transformation will not work. This should never occur
125 // because we are in SSA form.
Chris Lattneradbbc622004-07-21 23:17:57 +0000126#ifndef NDEBUG
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000127 for (unsigned i = 1; i != mi->getNumOperands(); ++i)
128 assert(!mi->getOperand(i).isRegister() ||
Alkis Evlogimenos8cdd0212004-02-13 21:01:20 +0000129 mi->getOperand(i).getReg() != regA);
Chris Lattneradbbc622004-07-21 23:17:57 +0000130#endif
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000131
132 const TargetRegisterClass* rc =
133 MF.getSSARegMap()->getRegClass(regA);
Alkis Evlogimenos80da8652004-02-12 02:27:10 +0000134 unsigned Added = MRI.copyRegToReg(*mbbi, mi, regA, regB, rc);
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000135 numInstrsAdded += Added;
136
Alkis Evlogimenos5a922402004-02-14 01:18:34 +0000137 MachineBasicBlock::iterator prevMi = prior(mi);
Alkis Evlogimenos26583db2004-02-18 00:35:06 +0000138 DEBUG(std::cerr << "\t\tprepend:\t";
Tanya Lattner23dbc812004-06-25 00:13:11 +0000139 prevMi->print(std::cerr, &TM));
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000140
Alkis Evlogimenose27f33f2004-02-15 21:50:32 +0000141 if (LV) {
142 // update live variables for regA
143 assert(Added == 1 &&
144 "Cannot handle multi-instruction copies yet!");
145 LiveVariables::VarInfo& varInfo = LV->getVarInfo(regA);
146 varInfo.DefInst = prevMi;
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000147
Alkis Evlogimenose27f33f2004-02-15 21:50:32 +0000148 // update live variables for regB
Chris Lattnerf2b976b2004-07-22 05:51:56 +0000149 if (LV->removeVirtualRegisterKilled(regB, mbbi, mi))
Chris Lattnera74cf5a2004-07-19 06:55:21 +0000150 LV->addVirtualRegisterKilled(regB, prevMi);
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000151
Chris Lattnerf2b976b2004-07-22 05:51:56 +0000152 if (LV->removeVirtualRegisterDead(regB, mbbi, mi))
Chris Lattnera74cf5a2004-07-19 06:55:21 +0000153 LV->addVirtualRegisterDead(regB, prevMi);
Alkis Evlogimenose27f33f2004-02-15 21:50:32 +0000154 }
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000155
156 // replace all occurences of regB with regA
157 for (unsigned i = 1, e = mi->getNumOperands(); i != e; ++i) {
158 if (mi->getOperand(i).isRegister() &&
159 mi->getOperand(i).getReg() == regB)
160 mi->SetMachineOperandReg(i, regA);
161 }
162 }
163
164 assert(mi->getOperand(0).isDef());
165 mi->getOperand(0).setUse();
166 mi->RemoveOperand(1);
Chris Lattneradbbc622004-07-21 23:17:57 +0000167 MadeChange = true;
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000168
Alkis Evlogimenos26583db2004-02-18 00:35:06 +0000169 DEBUG(std::cerr << "\t\trewrite to:\t";
Tanya Lattner23dbc812004-06-25 00:13:11 +0000170 mi->print(std::cerr, &TM));
Alkis Evlogimenos725021c2003-12-18 13:06:04 +0000171 }
172 }
173
Chris Lattner626f4312004-01-31 21:14:04 +0000174 return MadeChange;
Alkis Evlogimenos725021c2003-12-18 13:06:04 +0000175}