blob: d4fb2e4d884217cde71a7489c00cef2a2ba4af98 [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"
Daniel Dunbarf55f61f2009-07-24 10:36:58 +000033#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/ADT/BitVector.h"
Evan Cheng1376d862008-06-04 09:16:33 +000035#include "llvm/ADT/DenseMap.h"
Evan Chengfc201f32009-02-11 08:24:21 +000036#include "llvm/ADT/DepthFirstIterator.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/ADT/Statistic.h"
38#include "llvm/ADT/STLExtras.h"
39#include "llvm/ADT/SmallSet.h"
40#include <algorithm>
41using namespace llvm;
42
Evan Cheng5ed91b52008-06-13 23:58:02 +000043STATISTIC(NumSpills , "Number of register spills");
Dan Gohman089efff2008-05-13 00:00:25 +000044
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045//===----------------------------------------------------------------------===//
46// VirtRegMap implementation
47//===----------------------------------------------------------------------===//
48
Owen Andersondd56ab72009-03-13 05:55:11 +000049char VirtRegMap::ID = 0;
50
51static RegisterPass<VirtRegMap>
52X("virtregmap", "Virtual Register Map");
53
54bool VirtRegMap::runOnMachineFunction(MachineFunction &mf) {
Evan Chengd78907d2009-06-14 20:22:55 +000055 MRI = &mf.getRegInfo();
Owen Andersondd56ab72009-03-13 05:55:11 +000056 TII = mf.getTarget().getInstrInfo();
Evan Cheng5277da72009-05-04 03:30:11 +000057 TRI = mf.getTarget().getRegisterInfo();
Owen Andersondd56ab72009-03-13 05:55:11 +000058 MF = &mf;
Lang Hamesd6a717c2009-11-03 23:52:08 +000059
Owen Andersondd56ab72009-03-13 05:55:11 +000060 ReMatId = MAX_STACK_SLOT+1;
61 LowSpillSlot = HighSpillSlot = NO_STACK_SLOT;
62
63 Virt2PhysMap.clear();
64 Virt2StackSlotMap.clear();
65 Virt2ReMatIdMap.clear();
66 Virt2SplitMap.clear();
67 Virt2SplitKillMap.clear();
68 ReMatMap.clear();
69 ImplicitDefed.clear();
70 SpillSlotToUsesMap.clear();
71 MI2VirtMap.clear();
72 SpillPt2VirtMap.clear();
73 RestorePt2VirtMap.clear();
74 EmergencySpillMap.clear();
75 EmergencySpillSlots.clear();
76
Evan Chengda872532008-02-27 03:04:06 +000077 SpillSlotToUsesMap.resize(8);
Owen Andersondd56ab72009-03-13 05:55:11 +000078 ImplicitDefed.resize(MF->getRegInfo().getLastVirtReg()+1-
Evan Cheng7b88cbc2008-04-11 17:53:36 +000079 TargetRegisterInfo::FirstVirtualRegister);
Evan Cheng5277da72009-05-04 03:30:11 +000080
81 allocatableRCRegs.clear();
82 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
83 E = TRI->regclass_end(); I != E; ++I)
84 allocatableRCRegs.insert(std::make_pair(*I,
85 TRI->getAllocatableSet(mf, *I)));
86
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 grow();
Owen Andersondd56ab72009-03-13 05:55:11 +000088
89 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090}
91
92void VirtRegMap::grow() {
Owen Andersondd56ab72009-03-13 05:55:11 +000093 unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg();
Evan Cheng1204d172007-08-13 23:45:17 +000094 Virt2PhysMap.grow(LastVirtReg);
95 Virt2StackSlotMap.grow(LastVirtReg);
96 Virt2ReMatIdMap.grow(LastVirtReg);
Evan Chengcecc8222007-11-17 00:40:40 +000097 Virt2SplitMap.grow(LastVirtReg);
Evan Cheng6f522672007-12-05 09:51:10 +000098 Virt2SplitKillMap.grow(LastVirtReg);
Evan Cheng1204d172007-08-13 23:45:17 +000099 ReMatMap.grow(LastVirtReg);
Evan Cheng7b88cbc2008-04-11 17:53:36 +0000100 ImplicitDefed.resize(LastVirtReg-TargetRegisterInfo::FirstVirtualRegister+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101}
102
Evan Chengd78907d2009-06-14 20:22:55 +0000103unsigned VirtRegMap::getRegAllocPref(unsigned virtReg) {
Evan Cheng41169552009-06-15 08:28:29 +0000104 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(virtReg);
105 unsigned physReg = Hint.second;
106 if (physReg &&
107 TargetRegisterInfo::isVirtualRegister(physReg) && hasPhys(physReg))
108 physReg = getPhys(physReg);
109 if (Hint.first == 0)
110 return (physReg && TargetRegisterInfo::isPhysicalRegister(physReg))
111 ? physReg : 0;
112 return TRI->ResolveRegAllocHint(Hint.first, physReg, *MF);
Evan Chengd78907d2009-06-14 20:22:55 +0000113}
114
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000116 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
118 "attempt to assign stack slot to already spilled register");
Owen Andersondd56ab72009-03-13 05:55:11 +0000119 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtReg);
David Greene6424ab92009-11-12 20:49:22 +0000120 int SS = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
121 RC->getAlignment());
Evan Chengda872532008-02-27 03:04:06 +0000122 if (LowSpillSlot == NO_STACK_SLOT)
123 LowSpillSlot = SS;
124 if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
125 HighSpillSlot = SS;
126 unsigned Idx = SS-LowSpillSlot;
127 while (Idx >= SpillSlotToUsesMap.size())
128 SpillSlotToUsesMap.resize(SpillSlotToUsesMap.size()*2);
129 Virt2StackSlotMap[virtReg] = SS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 ++NumSpills;
Evan Chengda872532008-02-27 03:04:06 +0000131 return SS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132}
133
Evan Chengda872532008-02-27 03:04:06 +0000134void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int SS) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000135 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
137 "attempt to assign stack slot to already spilled register");
Evan Chengda872532008-02-27 03:04:06 +0000138 assert((SS >= 0 ||
Owen Andersondd56ab72009-03-13 05:55:11 +0000139 (SS >= MF->getFrameInfo()->getObjectIndexBegin())) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 "illegal fixed frame index");
Evan Chengda872532008-02-27 03:04:06 +0000141 Virt2StackSlotMap[virtReg] = SS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142}
143
144int VirtRegMap::assignVirtReMatId(unsigned virtReg) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000145 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng1204d172007-08-13 23:45:17 +0000146 assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 "attempt to assign re-mat id to already spilled register");
Evan Cheng1204d172007-08-13 23:45:17 +0000148 Virt2ReMatIdMap[virtReg] = ReMatId;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 return ReMatId++;
150}
151
Evan Cheng1204d172007-08-13 23:45:17 +0000152void VirtRegMap::assignVirtReMatId(unsigned virtReg, int id) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000153 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng1204d172007-08-13 23:45:17 +0000154 assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
155 "attempt to assign re-mat id to already spilled register");
156 Virt2ReMatIdMap[virtReg] = id;
157}
158
Evan Cheng14cc83f2008-03-11 07:19:34 +0000159int VirtRegMap::getEmergencySpillSlot(const TargetRegisterClass *RC) {
160 std::map<const TargetRegisterClass*, int>::iterator I =
161 EmergencySpillSlots.find(RC);
162 if (I != EmergencySpillSlots.end())
163 return I->second;
David Greene6424ab92009-11-12 20:49:22 +0000164 int SS = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
165 RC->getAlignment());
Evan Cheng14cc83f2008-03-11 07:19:34 +0000166 if (LowSpillSlot == NO_STACK_SLOT)
167 LowSpillSlot = SS;
168 if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
169 HighSpillSlot = SS;
Dan Gohmanad077b82008-10-06 18:00:07 +0000170 EmergencySpillSlots[RC] = SS;
Evan Cheng14cc83f2008-03-11 07:19:34 +0000171 return SS;
172}
173
Evan Chengda872532008-02-27 03:04:06 +0000174void VirtRegMap::addSpillSlotUse(int FI, MachineInstr *MI) {
Owen Andersondd56ab72009-03-13 05:55:11 +0000175 if (!MF->getFrameInfo()->isFixedObjectIndex(FI)) {
David Greene022e2b32008-05-22 21:12:21 +0000176 // If FI < LowSpillSlot, this stack reference was produced by
177 // instruction selection and is not a spill
178 if (FI >= LowSpillSlot) {
179 assert(FI >= 0 && "Spill slot index should not be negative!");
Bill Wendling8c333682008-05-23 01:29:08 +0000180 assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
David Greene022e2b32008-05-22 21:12:21 +0000181 && "Invalid spill slot");
182 SpillSlotToUsesMap[FI-LowSpillSlot].insert(MI);
183 }
Evan Chengda872532008-02-27 03:04:06 +0000184 }
185}
186
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI,
Evan Chengfd0bd3c2007-12-02 08:30:39 +0000188 MachineInstr *NewMI, ModRef MRInfo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 // Move previous memory references folded to new instruction.
190 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI);
191 for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI),
192 E = MI2VirtMap.end(); I != E && I->first == OldMI; ) {
193 MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second));
194 MI2VirtMap.erase(I++);
195 }
196
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 // add new memory reference
198 MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo)));
199}
200
Evan Chengf3255842007-10-13 02:50:24 +0000201void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo) {
202 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(MI);
203 MI2VirtMap.insert(IP, std::make_pair(MI, std::make_pair(VirtReg, MRInfo)));
204}
205
Evan Chengda872532008-02-27 03:04:06 +0000206void VirtRegMap::RemoveMachineInstrFromMaps(MachineInstr *MI) {
207 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
208 MachineOperand &MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000209 if (!MO.isFI())
Evan Chengda872532008-02-27 03:04:06 +0000210 continue;
211 int FI = MO.getIndex();
Owen Andersondd56ab72009-03-13 05:55:11 +0000212 if (MF->getFrameInfo()->isFixedObjectIndex(FI))
Evan Chengda872532008-02-27 03:04:06 +0000213 continue;
David Greene022e2b32008-05-22 21:12:21 +0000214 // This stack reference was produced by instruction selection and
Bill Wendlingfbdad532009-03-31 08:41:31 +0000215 // is not a spill
David Greene022e2b32008-05-22 21:12:21 +0000216 if (FI < LowSpillSlot)
217 continue;
Bill Wendling8c333682008-05-23 01:29:08 +0000218 assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
David Greene022e2b32008-05-22 21:12:21 +0000219 && "Invalid spill slot");
Evan Chengda872532008-02-27 03:04:06 +0000220 SpillSlotToUsesMap[FI-LowSpillSlot].erase(MI);
221 }
222 MI2VirtMap.erase(MI);
223 SpillPt2VirtMap.erase(MI);
224 RestorePt2VirtMap.erase(MI);
Evan Cheng14cc83f2008-03-11 07:19:34 +0000225 EmergencySpillMap.erase(MI);
Evan Chengda872532008-02-27 03:04:06 +0000226}
227
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000228/// FindUnusedRegisters - Gather a list of allocatable registers that
229/// have not been allocated to any virtual register.
Evan Chengd78907d2009-06-14 20:22:55 +0000230bool VirtRegMap::FindUnusedRegisters(LiveIntervals* LIs) {
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000231 unsigned NumRegs = TRI->getNumRegs();
232 UnusedRegs.reset();
233 UnusedRegs.resize(NumRegs);
234
235 BitVector Used(NumRegs);
236 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
237 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
238 if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
239 Used.set(Virt2PhysMap[i]);
240
241 BitVector Allocatable = TRI->getAllocatableSet(*MF);
242 bool AnyUnused = false;
243 for (unsigned Reg = 1; Reg < NumRegs; ++Reg) {
244 if (Allocatable[Reg] && !Used[Reg] && !LIs->hasInterval(Reg)) {
245 bool ReallyUnused = true;
246 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
247 if (Used[*AS] || LIs->hasInterval(*AS)) {
248 ReallyUnused = false;
249 break;
250 }
251 }
252 if (ReallyUnused) {
253 AnyUnused = true;
254 UnusedRegs.set(Reg);
255 }
256 }
257 }
258
259 return AnyUnused;
260}
261
Daniel Dunbarf55f61f2009-07-24 10:36:58 +0000262void VirtRegMap::print(raw_ostream &OS, const Module* M) const {
Owen Andersondd56ab72009-03-13 05:55:11 +0000263 const TargetRegisterInfo* TRI = MF->getTarget().getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264
265 OS << "********** REGISTER MAP **********\n";
Dan Gohman1e57df32008-02-10 18:45:23 +0000266 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Owen Andersondd56ab72009-03-13 05:55:11 +0000267 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000269 OS << "[reg" << i << " -> " << TRI->getName(Virt2PhysMap[i])
Bill Wendling8eeb9792008-02-26 21:11:01 +0000270 << "]\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 }
272
Dan Gohman1e57df32008-02-10 18:45:23 +0000273 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Owen Andersondd56ab72009-03-13 05:55:11 +0000274 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT)
276 OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n";
277 OS << '\n';
278}
279
280void VirtRegMap::dump() const {
David Greenef3a06042010-01-05 01:25:45 +0000281 print(dbgs());
Daniel Dunbarc863a612009-03-14 01:53:05 +0000282}