79c47ac26f43439bbde8f5d9d11236e1a75b5e2f
[oota-llvm.git] / lib / CodeGen / TwoAddressInstructionPass.cpp
1 //===-- 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 //
10 // 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
19 //     A op= C
20 //
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
23 // virtual registers.
24 //
25 // It is also worth noting that the duplicate operand of the two
26 // address instruction is removed.
27 //
28 //===----------------------------------------------------------------------===//
29
30 #define DEBUG_TYPE "twoaddrinstr"
31 #include "llvm/CodeGen/Passes.h"
32 #include "llvm/CodeGen/LiveVariables.h"
33 #include "llvm/CodeGen/MachineFunctionPass.h"
34 #include "llvm/CodeGen/MachineInstr.h"
35 #include "llvm/CodeGen/SSARegMap.h"
36 #include "llvm/Target/MRegisterInfo.h"
37 #include "llvm/Target/TargetInstrInfo.h"
38 #include "llvm/Target/TargetMachine.h"
39 #include "Support/Debug.h"
40 #include "Support/Statistic.h"
41 #include "Support/STLExtras.h"
42 #include <iostream>
43 using namespace llvm;
44
45 namespace {
46     Statistic<> numTwoAddressInstrs("twoaddressinstruction",
47                                     "Number of two-address instructions");
48     Statistic<> numInstrsAdded("twoaddressinstruction",
49                                "Number of instructions added");
50
51     struct TwoAddressInstructionPass : public MachineFunctionPass
52     {
53         virtual void getAnalysisUsage(AnalysisUsage &AU) const;
54
55         /// runOnMachineFunction - pass entry point
56         bool runOnMachineFunction(MachineFunction&);
57     };
58
59     RegisterPass<TwoAddressInstructionPass> X(
60         "twoaddressinstruction", "Two-Address instruction pass");
61 };
62
63 const PassInfo *llvm::TwoAddressInstructionPassID = X.getPassInfo();
64
65 void TwoAddressInstructionPass::getAnalysisUsage(AnalysisUsage &AU) const
66 {
67     AU.addPreserved<LiveVariables>();
68     AU.addPreservedID(PHIEliminationID);
69     MachineFunctionPass::getAnalysisUsage(AU);
70 }
71
72 /// runOnMachineFunction - Reduce two-address instructions to two
73 /// operands.
74 ///
75 bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
76     DEBUG(std::cerr << "Machine Function\n");
77     const TargetMachine &TM = MF.getTarget();
78     const MRegisterInfo &MRI = *TM.getRegisterInfo();
79     const TargetInstrInfo &TII = TM.getInstrInfo();
80     LiveVariables* LV = getAnalysisToUpdate<LiveVariables>();
81
82     bool MadeChange = false;
83
84     for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
85          mbbi != mbbe; ++mbbi) {
86         for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
87              mi != me; ++mi) {
88             unsigned opcode = mi->getOpcode();
89
90             // ignore if it is not a two-address instruction
91             if (!TII.isTwoAddrInstr(opcode))
92                 continue;
93
94             ++numTwoAddressInstrs;
95
96             DEBUG(std::cerr << "\tinstruction: "; mi->print(std::cerr, TM));
97
98             assert(mi->getOperand(1).isRegister() &&
99                    mi->getOperand(1).getReg() &&
100                    mi->getOperand(1).isUse() &&
101                    "two address instruction invalid");
102
103             // if the two operands are the same we just remove the use
104             // and mark the def as def&use
105             if (mi->getOperand(0).getReg() ==
106                 mi->getOperand(1).getReg()) {
107             }
108             else {
109                 MadeChange = true;
110
111                 // rewrite:
112                 //     a = b op c
113                 // to:
114                 //     a = b
115                 //     a = a op c
116                 unsigned regA = mi->getOperand(0).getReg();
117                 unsigned regB = mi->getOperand(1).getReg();
118
119                 assert(MRegisterInfo::isVirtualRegister(regA) &&
120                        MRegisterInfo::isVirtualRegister(regB) &&
121                        "cannot update physical register live information");
122
123                 // first make sure we do not have a use of a in the
124                 // instruction (a = b + a for example) because our
125                 // transformation will not work. This should never occur
126                 // because we are in SSA form.
127                 for (unsigned i = 1; i != mi->getNumOperands(); ++i)
128                     assert(!mi->getOperand(i).isRegister() ||
129                            mi->getOperand(i).getReg() != regA);
130
131                 const TargetRegisterClass* rc =
132                     MF.getSSARegMap()->getRegClass(regA);
133                 unsigned Added = MRI.copyRegToReg(*mbbi, mi, regA, regB, rc);
134                 numInstrsAdded += Added;
135
136                 MachineBasicBlock::iterator prevMi = prior(mi);
137                 DEBUG(std::cerr << "\t\tadded instruction: ";
138                       prevMi->print(std::cerr, TM));
139
140                 if (LV) {
141                     // update live variables for regA
142                     assert(Added == 1 &&
143                            "Cannot handle multi-instruction copies yet!");
144                     LiveVariables::VarInfo& varInfo = LV->getVarInfo(regA);
145                     varInfo.DefInst = prevMi;
146
147                     // update live variables for regB
148                     if (LV->removeVirtualRegisterKilled(regB, &*mbbi, mi))
149                         LV->addVirtualRegisterKilled(regB, &*mbbi, prevMi);
150
151                     if (LV->removeVirtualRegisterDead(regB, &*mbbi, mi))
152                         LV->addVirtualRegisterDead(regB, &*mbbi, prevMi);
153                 }
154
155                 // replace all occurences of regB with regA
156                 for (unsigned i = 1, e = mi->getNumOperands(); i != e; ++i) {
157                     if (mi->getOperand(i).isRegister() &&
158                         mi->getOperand(i).getReg() == regB)
159                         mi->SetMachineOperandReg(i, regA);
160                 }
161             }
162
163             assert(mi->getOperand(0).isDef());
164             mi->getOperand(0).setUse();
165             mi->RemoveOperand(1);
166
167             DEBUG(std::cerr << "\t\tmodified original to: ";
168                   mi->print(std::cerr, TM));
169         }
170     }
171
172     return MadeChange;
173 }