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 LiveInterval analysis pass which is used
11 // by the Linear Scan Register allocator. This pass linearizes the
12 // basic blocks of the function in DFS order and uses the
13 // LiveVariables pass to conservatively compute live intervals for
14 // each virtual and physical register.
16 //===----------------------------------------------------------------------===//
18 #define DEBUG_TYPE "twoaddrinstr"
19 #include "llvm/Function.h"
20 #include "llvm/CodeGen/LiveVariables.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineInstr.h"
24 #include "llvm/CodeGen/Passes.h"
25 #include "llvm/CodeGen/SSARegMap.h"
26 #include "llvm/Target/MRegisterInfo.h"
27 #include "llvm/Target/TargetInstrInfo.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Target/TargetRegInfo.h"
30 #include "Support/Debug.h"
31 #include "Support/Statistic.h"
32 #include "Support/STLExtras.h"
38 class TwoAddressInstructionPass : public MachineFunctionPass
42 const TargetMachine* tm_;
43 const MRegisterInfo* mri_;
47 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
50 /// runOnMachineFunction - pass entry point
51 bool runOnMachineFunction(MachineFunction&);
54 RegisterPass<TwoAddressInstructionPass> X(
55 "twoaddressinstruction", "Two-Address instruction pass");
57 Statistic<> numTwoAddressInstrs("twoaddressinstruction",
58 "Number of two-address instructions");
59 Statistic<> numInstrsAdded("twoaddressinstruction",
60 "Number of instructions added");
63 const PassInfo *llvm::TwoAddressInstructionPassID = X.getPassInfo();
65 void TwoAddressInstructionPass::getAnalysisUsage(AnalysisUsage &AU) const
67 AU.addPreserved<LiveVariables>();
68 AU.addRequired<LiveVariables>();
69 AU.addPreservedID(PHIEliminationID);
70 AU.addRequiredID(PHIEliminationID);
71 MachineFunctionPass::getAnalysisUsage(AU);
74 /// runOnMachineFunction - Reduce two-address instructions to two
77 bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &fn) {
78 DEBUG(std::cerr << "Machine Function\n");
80 tm_ = &fn.getTarget();
81 mri_ = tm_->getRegisterInfo();
82 lv_ = &getAnalysis<LiveVariables>();
84 const TargetInstrInfo& tii = tm_->getInstrInfo();
86 for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
87 mbbi != mbbe; ++mbbi) {
88 for (MachineBasicBlock::iterator mii = mbbi->begin();
89 mii != mbbi->end(); ++mii) {
90 MachineInstr* mi = *mii;
92 unsigned opcode = mi->getOpcode();
93 // ignore if it is not a two-address instruction
94 if (!tii.isTwoAddrInstr(opcode))
97 ++numTwoAddressInstrs;
99 DEBUG(std::cerr << "\tinstruction: "; mi->print(std::cerr, *tm_));
101 // we have nothing to do if the two operands are the same
102 if (mi->getOperand(0).getAllocatedRegNum() ==
103 mi->getOperand(1).getAllocatedRegNum())
106 assert(mi->getOperand(1).isRegister() &&
107 mi->getOperand(1).getAllocatedRegNum() &&
108 mi->getOperand(1).isUse() &&
109 "two address instruction invalid");
116 unsigned regA = mi->getOperand(0).getAllocatedRegNum();
117 unsigned regB = mi->getOperand(1).getAllocatedRegNum();
118 bool regAisPhysical = regA < MRegisterInfo::FirstVirtualRegister;
119 bool regBisPhysical = regB < MRegisterInfo::FirstVirtualRegister;
121 const TargetRegisterClass* rc = regAisPhysical ?
122 mri_->getRegClass(regA) :
123 mf_->getSSARegMap()->getRegClass(regA);
125 numInstrsAdded += mri_->copyRegToReg(*mbbi, mii, regA, regB, rc);
127 MachineInstr* prevMi = *(mii - 1);
128 DEBUG(std::cerr << "\t\tadded instruction: ";
129 prevMi->print(std::cerr, *tm_));
131 // update live variables for regA
132 if (regAisPhysical) {
133 lv_->HandlePhysRegDef(regA, prevMi);
136 LiveVariables::VarInfo& varInfo = lv_->getVarInfo(regA);
137 varInfo.DefInst = prevMi;
140 // update live variables for regB
141 if (regBisPhysical) {
142 lv_->HandlePhysRegUse(regB, prevMi);
145 if (lv_->removeVirtualRegisterKilled(regB, &*mbbi, mi))
146 lv_->addVirtualRegisterKilled(regB, &*mbbi, prevMi);
148 if (lv_->removeVirtualRegisterDead(regB, &*mbbi, mi))
149 lv_->addVirtualRegisterDead(regB, &*mbbi, prevMi);
152 // replace all occurences of regB with regA
153 for (unsigned i = 1; i < mi->getNumOperands(); ++i) {
154 if (mi->getOperand(i).isRegister() &&
155 mi->getOperand(i).getReg() == regB)
156 mi->SetMachineOperandReg(i, regA);
158 DEBUG(std::cerr << "\t\tmodified original to: ";
159 mi->print(std::cerr, *tm_));
160 assert(mi->getOperand(0).getAllocatedRegNum() ==
161 mi->getOperand(1).getAllocatedRegNum());
165 return numInstrsAdded != 0;