blob: 4d3417fdff514d8ba95d8f58ae8d7ed8684ddef5 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm/CodeGen/VirtRegMap.cpp - Virtual Register Map ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the VirtRegMap class.
11//
12// It also contains implementations of the the Spiller interface, which, given a
13// virtual register map and a machine function, eliminates all virtual
14// references by replacing them with physical register references - adding spill
15// code as necessary.
16//
17//===----------------------------------------------------------------------===//
18
Owen Anderson860d4822009-03-11 22:31:21 +000019#define DEBUG_TYPE "virtregmap"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "VirtRegMap.h"
21#include "llvm/Function.h"
Evan Cheng97c5f1f2009-05-03 18:32:42 +000022#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng7b88cbc2008-04-11 17:53:36 +000025#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner1b989192007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/Target/TargetMachine.h"
28#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng5277da72009-05-04 03:30:11 +000029#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/Support/Compiler.h"
Evan Chengfc201f32009-02-11 08:24:21 +000032#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/ADT/BitVector.h"
Evan Cheng1376d862008-06-04 09:16:33 +000034#include "llvm/ADT/DenseMap.h"
Evan Chengfc201f32009-02-11 08:24:21 +000035#include "llvm/ADT/DepthFirstIterator.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/ADT/Statistic.h"
37#include "llvm/ADT/STLExtras.h"
38#include "llvm/ADT/SmallSet.h"
39#include <algorithm>
40using namespace llvm;
41
Evan Cheng5ed91b52008-06-13 23:58:02 +000042STATISTIC(NumSpills , "Number of register spills");
Dan Gohman089efff2008-05-13 00:00:25 +000043
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044//===----------------------------------------------------------------------===//
45// VirtRegMap implementation
46//===----------------------------------------------------------------------===//
47
Owen Andersondd56ab72009-03-13 05:55:11 +000048char VirtRegMap::ID = 0;
49
50static RegisterPass<VirtRegMap>
51X("virtregmap", "Virtual Register Map");
52
53bool VirtRegMap::runOnMachineFunction(MachineFunction &mf) {
Evan Chengd78907d2009-06-14 20:22:55 +000054 MRI = &mf.getRegInfo();
Owen Andersondd56ab72009-03-13 05:55:11 +000055 TII = mf.getTarget().getInstrInfo();
Evan Cheng5277da72009-05-04 03:30:11 +000056 TRI = mf.getTarget().getRegisterInfo();
Owen Andersondd56ab72009-03-13 05:55:11 +000057 MF = &mf;
58
59 ReMatId = MAX_STACK_SLOT+1;
60 LowSpillSlot = HighSpillSlot = NO_STACK_SLOT;
61
62 Virt2PhysMap.clear();
63 Virt2StackSlotMap.clear();
64 Virt2ReMatIdMap.clear();
65 Virt2SplitMap.clear();
66 Virt2SplitKillMap.clear();
67 ReMatMap.clear();
68 ImplicitDefed.clear();
69 SpillSlotToUsesMap.clear();
70 MI2VirtMap.clear();
71 SpillPt2VirtMap.clear();
72 RestorePt2VirtMap.clear();
73 EmergencySpillMap.clear();
74 EmergencySpillSlots.clear();
75
Evan Chengda872532008-02-27 03:04:06 +000076 SpillSlotToUsesMap.resize(8);
Owen Andersondd56ab72009-03-13 05:55:11 +000077 ImplicitDefed.resize(MF->getRegInfo().getLastVirtReg()+1-
Evan Cheng7b88cbc2008-04-11 17:53:36 +000078 TargetRegisterInfo::FirstVirtualRegister);
Evan Cheng5277da72009-05-04 03:30:11 +000079
80 allocatableRCRegs.clear();
81 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
82 E = TRI->regclass_end(); I != E; ++I)
83 allocatableRCRegs.insert(std::make_pair(*I,
84 TRI->getAllocatableSet(mf, *I)));
85
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 grow();
Owen Andersondd56ab72009-03-13 05:55:11 +000087
88 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089}
90
91void VirtRegMap::grow() {
Owen Andersondd56ab72009-03-13 05:55:11 +000092 unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg();
Evan Cheng1204d172007-08-13 23:45:17 +000093 Virt2PhysMap.grow(LastVirtReg);
94 Virt2StackSlotMap.grow(LastVirtReg);
95 Virt2ReMatIdMap.grow(LastVirtReg);
Evan Chengcecc8222007-11-17 00:40:40 +000096 Virt2SplitMap.grow(LastVirtReg);
Evan Cheng6f522672007-12-05 09:51:10 +000097 Virt2SplitKillMap.grow(LastVirtReg);
Evan Cheng1204d172007-08-13 23:45:17 +000098 ReMatMap.grow(LastVirtReg);
Evan Cheng7b88cbc2008-04-11 17:53:36 +000099 ImplicitDefed.resize(LastVirtReg-TargetRegisterInfo::FirstVirtualRegister+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100}
101
Evan Chengd78907d2009-06-14 20:22:55 +0000102unsigned VirtRegMap::getRegAllocPref(unsigned virtReg) {
Evan Cheng41169552009-06-15 08:28:29 +0000103 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(virtReg);
104 unsigned physReg = Hint.second;
105 if (physReg &&
106 TargetRegisterInfo::isVirtualRegister(physReg) && hasPhys(physReg))
107 physReg = getPhys(physReg);
108 if (Hint.first == 0)
109 return (physReg && TargetRegisterInfo::isPhysicalRegister(physReg))
110 ? physReg : 0;
111 return TRI->ResolveRegAllocHint(Hint.first, physReg, *MF);
Evan Chengd78907d2009-06-14 20:22:55 +0000112}
113
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000115 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
117 "attempt to assign stack slot to already spilled register");
Owen Andersondd56ab72009-03-13 05:55:11 +0000118 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtReg);
119 int SS = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
Evan Chengda872532008-02-27 03:04:06 +0000120 RC->getAlignment());
121 if (LowSpillSlot == NO_STACK_SLOT)
122 LowSpillSlot = SS;
123 if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
124 HighSpillSlot = SS;
125 unsigned Idx = SS-LowSpillSlot;
126 while (Idx >= SpillSlotToUsesMap.size())
127 SpillSlotToUsesMap.resize(SpillSlotToUsesMap.size()*2);
128 Virt2StackSlotMap[virtReg] = SS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 ++NumSpills;
Evan Chengda872532008-02-27 03:04:06 +0000130 return SS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131}
132
Evan Chengda872532008-02-27 03:04:06 +0000133void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int SS) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000134 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
136 "attempt to assign stack slot to already spilled register");
Evan Chengda872532008-02-27 03:04:06 +0000137 assert((SS >= 0 ||
Owen Andersondd56ab72009-03-13 05:55:11 +0000138 (SS >= MF->getFrameInfo()->getObjectIndexBegin())) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 "illegal fixed frame index");
Evan Chengda872532008-02-27 03:04:06 +0000140 Virt2StackSlotMap[virtReg] = SS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141}
142
143int VirtRegMap::assignVirtReMatId(unsigned virtReg) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000144 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng1204d172007-08-13 23:45:17 +0000145 assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 "attempt to assign re-mat id to already spilled register");
Evan Cheng1204d172007-08-13 23:45:17 +0000147 Virt2ReMatIdMap[virtReg] = ReMatId;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 return ReMatId++;
149}
150
Evan Cheng1204d172007-08-13 23:45:17 +0000151void VirtRegMap::assignVirtReMatId(unsigned virtReg, int id) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000152 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng1204d172007-08-13 23:45:17 +0000153 assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
154 "attempt to assign re-mat id to already spilled register");
155 Virt2ReMatIdMap[virtReg] = id;
156}
157
Evan Cheng14cc83f2008-03-11 07:19:34 +0000158int VirtRegMap::getEmergencySpillSlot(const TargetRegisterClass *RC) {
159 std::map<const TargetRegisterClass*, int>::iterator I =
160 EmergencySpillSlots.find(RC);
161 if (I != EmergencySpillSlots.end())
162 return I->second;
Owen Andersondd56ab72009-03-13 05:55:11 +0000163 int SS = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
Evan Cheng14cc83f2008-03-11 07:19:34 +0000164 RC->getAlignment());
165 if (LowSpillSlot == NO_STACK_SLOT)
166 LowSpillSlot = SS;
167 if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
168 HighSpillSlot = SS;
Dan Gohmanad077b82008-10-06 18:00:07 +0000169 EmergencySpillSlots[RC] = SS;
Evan Cheng14cc83f2008-03-11 07:19:34 +0000170 return SS;
171}
172
Evan Chengda872532008-02-27 03:04:06 +0000173void VirtRegMap::addSpillSlotUse(int FI, MachineInstr *MI) {
Owen Andersondd56ab72009-03-13 05:55:11 +0000174 if (!MF->getFrameInfo()->isFixedObjectIndex(FI)) {
David Greene022e2b32008-05-22 21:12:21 +0000175 // If FI < LowSpillSlot, this stack reference was produced by
176 // instruction selection and is not a spill
177 if (FI >= LowSpillSlot) {
178 assert(FI >= 0 && "Spill slot index should not be negative!");
Bill Wendling8c333682008-05-23 01:29:08 +0000179 assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
David Greene022e2b32008-05-22 21:12:21 +0000180 && "Invalid spill slot");
181 SpillSlotToUsesMap[FI-LowSpillSlot].insert(MI);
182 }
Evan Chengda872532008-02-27 03:04:06 +0000183 }
184}
185
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI,
Evan Chengfd0bd3c2007-12-02 08:30:39 +0000187 MachineInstr *NewMI, ModRef MRInfo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 // Move previous memory references folded to new instruction.
189 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI);
190 for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI),
191 E = MI2VirtMap.end(); I != E && I->first == OldMI; ) {
192 MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second));
193 MI2VirtMap.erase(I++);
194 }
195
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 // add new memory reference
197 MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo)));
198}
199
Evan Chengf3255842007-10-13 02:50:24 +0000200void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo) {
201 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(MI);
202 MI2VirtMap.insert(IP, std::make_pair(MI, std::make_pair(VirtReg, MRInfo)));
203}
204
Evan Chengda872532008-02-27 03:04:06 +0000205void VirtRegMap::RemoveMachineInstrFromMaps(MachineInstr *MI) {
206 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
207 MachineOperand &MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000208 if (!MO.isFI())
Evan Chengda872532008-02-27 03:04:06 +0000209 continue;
210 int FI = MO.getIndex();
Owen Andersondd56ab72009-03-13 05:55:11 +0000211 if (MF->getFrameInfo()->isFixedObjectIndex(FI))
Evan Chengda872532008-02-27 03:04:06 +0000212 continue;
David Greene022e2b32008-05-22 21:12:21 +0000213 // This stack reference was produced by instruction selection and
Bill Wendlingfbdad532009-03-31 08:41:31 +0000214 // is not a spill
David Greene022e2b32008-05-22 21:12:21 +0000215 if (FI < LowSpillSlot)
216 continue;
Bill Wendling8c333682008-05-23 01:29:08 +0000217 assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
David Greene022e2b32008-05-22 21:12:21 +0000218 && "Invalid spill slot");
Evan Chengda872532008-02-27 03:04:06 +0000219 SpillSlotToUsesMap[FI-LowSpillSlot].erase(MI);
220 }
221 MI2VirtMap.erase(MI);
222 SpillPt2VirtMap.erase(MI);
223 RestorePt2VirtMap.erase(MI);
Evan Cheng14cc83f2008-03-11 07:19:34 +0000224 EmergencySpillMap.erase(MI);
Evan Chengda872532008-02-27 03:04:06 +0000225}
226
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000227/// FindUnusedRegisters - Gather a list of allocatable registers that
228/// have not been allocated to any virtual register.
Evan Chengd78907d2009-06-14 20:22:55 +0000229bool VirtRegMap::FindUnusedRegisters(LiveIntervals* LIs) {
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000230 unsigned NumRegs = TRI->getNumRegs();
231 UnusedRegs.reset();
232 UnusedRegs.resize(NumRegs);
233
234 BitVector Used(NumRegs);
235 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
236 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
237 if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
238 Used.set(Virt2PhysMap[i]);
239
240 BitVector Allocatable = TRI->getAllocatableSet(*MF);
241 bool AnyUnused = false;
242 for (unsigned Reg = 1; Reg < NumRegs; ++Reg) {
243 if (Allocatable[Reg] && !Used[Reg] && !LIs->hasInterval(Reg)) {
244 bool ReallyUnused = true;
245 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
246 if (Used[*AS] || LIs->hasInterval(*AS)) {
247 ReallyUnused = false;
248 break;
249 }
250 }
251 if (ReallyUnused) {
252 AnyUnused = true;
253 UnusedRegs.set(Reg);
254 }
255 }
256 }
257
258 return AnyUnused;
259}
260
Owen Andersondd56ab72009-03-13 05:55:11 +0000261void VirtRegMap::print(std::ostream &OS, const Module* M) const {
262 const TargetRegisterInfo* TRI = MF->getTarget().getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263
264 OS << "********** REGISTER MAP **********\n";
Dan Gohman1e57df32008-02-10 18:45:23 +0000265 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Owen Andersondd56ab72009-03-13 05:55:11 +0000266 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267 if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000268 OS << "[reg" << i << " -> " << TRI->getName(Virt2PhysMap[i])
Bill Wendling8eeb9792008-02-26 21:11:01 +0000269 << "]\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 }
271
Dan Gohman1e57df32008-02-10 18:45:23 +0000272 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Owen Andersondd56ab72009-03-13 05:55:11 +0000273 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT)
275 OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n";
276 OS << '\n';
277}
278
279void VirtRegMap::dump() const {
Dan Gohmanecb9ad52008-03-12 20:52:10 +0000280 print(cerr);
Daniel Dunbarc863a612009-03-14 01:53:05 +0000281}