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