blob: d7953896be155a4a12cd197fa4d5a6b8192ac6d3 [file] [log] [blame]
Chris Lattner62ed6b92008-01-01 01:12:31 +00001//===-- lib/Codegen/MachineRegisterInfo.cpp -------------------------------===//
Chris Lattner84bc5422007-12-31 04:13:23 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Implementation of the MachineRegisterInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengf48023b2010-04-26 19:16:00 +000015#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohman98708262010-04-14 16:51:49 +000016#include "llvm/Target/TargetInstrInfo.h"
Jakob Stoklund Olesen6d1fd0b2011-08-09 16:46:27 +000017#include "llvm/Target/TargetMachine.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000018using namespace llvm;
19
Jakob Stoklund Olesen73e7dce2011-07-29 22:51:22 +000020MachineRegisterInfo::MachineRegisterInfo(const TargetRegisterInfo &TRI)
21 : IsSSA(true) {
Chris Lattner84bc5422007-12-31 04:13:23 +000022 VRegInfo.reserve(256);
Evan Cheng90f95f82009-06-14 20:22:55 +000023 RegAllocHints.reserve(256);
Dan Gohman6f0d0242008-02-10 18:45:23 +000024 UsedPhysRegs.resize(TRI.getNumRegs());
Chris Lattner62ed6b92008-01-01 01:12:31 +000025
26 // Create the physreg use/def lists.
Dan Gohman6f0d0242008-02-10 18:45:23 +000027 PhysRegUseDefLists = new MachineOperand*[TRI.getNumRegs()];
28 memset(PhysRegUseDefLists, 0, sizeof(MachineOperand*)*TRI.getNumRegs());
Chris Lattner62ed6b92008-01-01 01:12:31 +000029}
30
31MachineRegisterInfo::~MachineRegisterInfo() {
32#ifndef NDEBUG
Jakob Stoklund Olesen994c7272011-01-09 03:05:46 +000033 for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i)
34 assert(VRegInfo[TargetRegisterInfo::index2VirtReg(i)].second == 0 &&
35 "Vreg use list non-empty still?");
Dan Gohman03bafaf2008-07-07 19:55:35 +000036 for (unsigned i = 0, e = UsedPhysRegs.size(); i != e; ++i)
37 assert(!PhysRegUseDefLists[i] &&
38 "PhysRegUseDefLists has entries after all instructions are deleted");
Chris Lattner62ed6b92008-01-01 01:12:31 +000039#endif
40 delete [] PhysRegUseDefLists;
41}
42
Dan Gohman33f1c682009-04-15 01:19:35 +000043/// setRegClass - Set the register class of the specified virtual register.
44///
45void
46MachineRegisterInfo::setRegClass(unsigned Reg, const TargetRegisterClass *RC) {
Dan Gohman33f1c682009-04-15 01:19:35 +000047 VRegInfo[Reg].first = RC;
Dan Gohman33f1c682009-04-15 01:19:35 +000048}
49
Jakob Stoklund Olesenbf4699c2010-10-06 23:54:39 +000050const TargetRegisterClass *
51MachineRegisterInfo::constrainRegClass(unsigned Reg,
52 const TargetRegisterClass *RC) {
53 const TargetRegisterClass *OldRC = getRegClass(Reg);
54 if (OldRC == RC)
55 return RC;
56 const TargetRegisterClass *NewRC = getCommonSubClass(OldRC, RC);
57 if (!NewRC)
58 return 0;
59 if (NewRC != OldRC)
60 setRegClass(Reg, NewRC);
61 return NewRC;
62}
63
Jakob Stoklund Olesen6d1fd0b2011-08-09 16:46:27 +000064bool
65MachineRegisterInfo::recomputeRegClass(unsigned Reg, const TargetMachine &TM) {
66 const TargetInstrInfo *TII = TM.getInstrInfo();
67 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
68 const TargetRegisterClass *OldRC = getRegClass(Reg);
69 const TargetRegisterClass *NewRC = TRI->getLargestLegalSuperClass(OldRC);
70
71 // Stop early if there is no room to grow.
72 if (NewRC == OldRC)
73 return false;
74
75 // Accumulate constraints from all uses.
76 for (reg_nodbg_iterator I = reg_nodbg_begin(Reg), E = reg_nodbg_end(); I != E;
77 ++I) {
78 // TRI doesn't have accurate enough information to model this yet.
79 if (I.getOperand().getSubReg())
80 return false;
81 // Inline asm instuctions don't remember their constraints.
82 if (I->isInlineAsm())
83 return false;
84 const TargetRegisterClass *OpRC =
85 TII->getRegClass(I->getDesc(), I.getOperandNo(), TRI);
86 if (OpRC)
87 NewRC = getCommonSubClass(NewRC, OpRC);
88 if (!NewRC || NewRC == OldRC)
89 return false;
90 }
91 setRegClass(Reg, NewRC);
92 return true;
93}
94
Dan Gohman2e3e5bf2008-12-08 04:54:11 +000095/// createVirtualRegister - Create and return a new virtual register in the
96/// function with the specified register class.
97///
98unsigned
99MachineRegisterInfo::createVirtualRegister(const TargetRegisterClass *RegClass){
100 assert(RegClass && "Cannot create register without RegClass!");
Jakob Stoklund Olesenf462e3f2011-06-02 23:07:20 +0000101 assert(RegClass->isAllocatable() &&
102 "Virtual register RegClass must be allocatable.");
Dan Gohman2e3e5bf2008-12-08 04:54:11 +0000103
Jakob Stoklund Olesen994c7272011-01-09 03:05:46 +0000104 // New virtual register number.
105 unsigned Reg = TargetRegisterInfo::index2VirtReg(getNumVirtRegs());
106
107 // Add a reg, but keep track of whether the vector reallocated or not.
108 const unsigned FirstVirtReg = TargetRegisterInfo::index2VirtReg(0);
109 void *ArrayBase = getNumVirtRegs() == 0 ? 0 : &VRegInfo[FirstVirtReg];
110 VRegInfo.grow(Reg);
111 VRegInfo[Reg].first = RegClass;
112 RegAllocHints.grow(Reg);
113
114 if (ArrayBase && &VRegInfo[FirstVirtReg] != ArrayBase)
Dan Gohman2e3e5bf2008-12-08 04:54:11 +0000115 // The vector reallocated, handle this now.
116 HandleVRegListReallocation();
Jakob Stoklund Olesen994c7272011-01-09 03:05:46 +0000117 return Reg;
Dan Gohman2e3e5bf2008-12-08 04:54:11 +0000118}
119
Chris Lattner62ed6b92008-01-01 01:12:31 +0000120/// HandleVRegListReallocation - We just added a virtual register to the
121/// VRegInfo info list and it reallocated. Update the use/def lists info
122/// pointers.
123void MachineRegisterInfo::HandleVRegListReallocation() {
124 // The back pointers for the vreg lists point into the previous vector.
125 // Update them to point to their correct slots.
Jakob Stoklund Olesen994c7272011-01-09 03:05:46 +0000126 for (unsigned i = 0, e = getNumVirtRegs(); i != e; ++i) {
127 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
128 MachineOperand *List = VRegInfo[Reg].second;
Chris Lattner62ed6b92008-01-01 01:12:31 +0000129 if (!List) continue;
130 // Update the back-pointer to be accurate once more.
Jakob Stoklund Olesen994c7272011-01-09 03:05:46 +0000131 List->Contents.Reg.Prev = &VRegInfo[Reg].second;
Chris Lattner62ed6b92008-01-01 01:12:31 +0000132 }
Chris Lattner84bc5422007-12-31 04:13:23 +0000133}
Chris Lattnera91a7d52008-01-01 03:07:29 +0000134
Chris Lattnere138b3d2008-01-01 20:36:19 +0000135/// replaceRegWith - Replace all instances of FromReg with ToReg in the
136/// machine function. This is like llvm-level X->replaceAllUsesWith(Y),
137/// except that it also changes any definitions of the register as well.
138void MachineRegisterInfo::replaceRegWith(unsigned FromReg, unsigned ToReg) {
139 assert(FromReg != ToReg && "Cannot replace a reg with itself");
140
141 // TODO: This could be more efficient by bulk changing the operands.
142 for (reg_iterator I = reg_begin(FromReg), E = reg_end(); I != E; ) {
143 MachineOperand &O = I.getOperand();
144 ++I;
145 O.setReg(ToReg);
146 }
147}
148
Chris Lattnera91a7d52008-01-01 03:07:29 +0000149
150/// getVRegDef - Return the machine instr that defines the specified virtual
151/// register or null if none is found. This assumes that the code is in SSA
152/// form, so there should only be one definition.
153MachineInstr *MachineRegisterInfo::getVRegDef(unsigned Reg) const {
Dan Gohman2bf06492009-09-25 22:26:13 +0000154 // Since we are in SSA form, we can use the first definition.
155 if (!def_empty(Reg))
156 return &*def_begin(Reg);
Chris Lattnera91a7d52008-01-01 03:07:29 +0000157 return 0;
158}
Evan Cheng1eb5cf92008-02-13 02:45:38 +0000159
Evan Cheng1423c702010-03-03 21:18:38 +0000160bool MachineRegisterInfo::hasOneUse(unsigned RegNo) const {
161 use_iterator UI = use_begin(RegNo);
162 if (UI == use_end())
163 return false;
164 return ++UI == use_end();
165}
166
167bool MachineRegisterInfo::hasOneNonDBGUse(unsigned RegNo) const {
168 use_nodbg_iterator UI = use_nodbg_begin(RegNo);
169 if (UI == use_nodbg_end())
170 return false;
171 return ++UI == use_nodbg_end();
172}
Evan Cheng1eb5cf92008-02-13 02:45:38 +0000173
Dan Gohman49b45892010-05-13 19:24:00 +0000174/// clearKillFlags - Iterate over all the uses of the given register and
175/// clear the kill flag from the MachineOperand. This function is used by
176/// optimization passes which extend register lifetimes and need only
177/// preserve conservative kill flag information.
178void MachineRegisterInfo::clearKillFlags(unsigned Reg) const {
179 for (use_iterator UI = use_begin(Reg), UE = use_end(); UI != UE; ++UI)
180 UI.getOperand().setIsKill(false);
181}
182
Dan Gohman13e73f42010-04-13 16:55:37 +0000183bool MachineRegisterInfo::isLiveIn(unsigned Reg) const {
184 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
185 if (I->first == Reg || I->second == Reg)
186 return true;
187 return false;
188}
189
190bool MachineRegisterInfo::isLiveOut(unsigned Reg) const {
191 for (liveout_iterator I = liveout_begin(), E = liveout_end(); I != E; ++I)
192 if (*I == Reg)
193 return true;
194 return false;
195}
196
Evan Cheng2ad0fcf2010-04-28 23:08:54 +0000197/// getLiveInPhysReg - If VReg is a live-in virtual register, return the
198/// corresponding live-in physical register.
199unsigned MachineRegisterInfo::getLiveInPhysReg(unsigned VReg) const {
200 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
201 if (I->second == VReg)
202 return I->first;
203 return 0;
204}
205
Evan Cheng39460432010-05-24 21:33:37 +0000206/// getLiveInVirtReg - If PReg is a live-in physical register, return the
207/// corresponding live-in physical register.
208unsigned MachineRegisterInfo::getLiveInVirtReg(unsigned PReg) const {
209 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
210 if (I->first == PReg)
211 return I->second;
212 return 0;
213}
214
Dan Gohman98708262010-04-14 16:51:49 +0000215/// EmitLiveInCopies - Emit copies to initialize livein virtual registers
216/// into the given entry block.
217void
218MachineRegisterInfo::EmitLiveInCopies(MachineBasicBlock *EntryMBB,
219 const TargetRegisterInfo &TRI,
220 const TargetInstrInfo &TII) {
Evan Cheng701d4d32010-05-29 02:23:39 +0000221 // Emit the copies into the top of the block.
Dan Gohmanfe5e4da2010-06-24 22:23:02 +0000222 for (unsigned i = 0, e = LiveIns.size(); i != e; ++i)
223 if (LiveIns[i].second) {
224 if (use_empty(LiveIns[i].second)) {
225 // The livein has no uses. Drop it.
226 //
227 // It would be preferable to have isel avoid creating live-in
228 // records for unused arguments in the first place, but it's
229 // complicated by the debug info code for arguments.
230 LiveIns.erase(LiveIns.begin() + i);
231 --i; --e;
232 } else {
233 // Emit a copy.
Devang Patel68e6bee2011-02-21 23:21:26 +0000234 BuildMI(*EntryMBB, EntryMBB->begin(), DebugLoc(),
Jakob Stoklund Olesen1e1098c2010-07-10 22:42:59 +0000235 TII.get(TargetOpcode::COPY), LiveIns[i].second)
236 .addReg(LiveIns[i].first);
Dan Gohmanb13033f2010-04-14 17:05:00 +0000237
Dan Gohmanfe5e4da2010-06-24 22:23:02 +0000238 // Add the register to the entry block live-in set.
239 EntryMBB->addLiveIn(LiveIns[i].first);
240 }
241 } else {
242 // Add the register to the entry block live-in set.
243 EntryMBB->addLiveIn(LiveIns[i].first);
244 }
Dan Gohman98708262010-04-14 16:51:49 +0000245}
246
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000247void MachineRegisterInfo::closePhysRegsUsed(const TargetRegisterInfo &TRI) {
248 for (int i = UsedPhysRegs.find_first(); i >= 0;
249 i = UsedPhysRegs.find_next(i))
250 for (const unsigned *SS = TRI.getSubRegisters(i);
251 unsigned SubReg = *SS; ++SS)
Jakob Stoklund Olesen8e8b3cb2010-05-11 20:51:04 +0000252 if (SubReg > unsigned(i))
Jakob Stoklund Olesen82b07dc2010-05-11 20:30:28 +0000253 UsedPhysRegs.set(SubReg);
254}
255
Evan Cheng1eb5cf92008-02-13 02:45:38 +0000256#ifndef NDEBUG
257void MachineRegisterInfo::dumpUses(unsigned Reg) const {
258 for (use_iterator I = use_begin(Reg), E = use_end(); I != E; ++I)
259 I.getOperand().getParent()->dump();
260}
261#endif