blob: f2f6ab02beffb3cc2073656cce3e7f5b77d8b351 [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"
29#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/Support/Compiler.h"
Evan Chengfc201f32009-02-11 08:24:21 +000031#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/ADT/BitVector.h"
Evan Cheng1376d862008-06-04 09:16:33 +000033#include "llvm/ADT/DenseMap.h"
Evan Chengfc201f32009-02-11 08:24:21 +000034#include "llvm/ADT/DepthFirstIterator.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/ADT/Statistic.h"
36#include "llvm/ADT/STLExtras.h"
37#include "llvm/ADT/SmallSet.h"
38#include <algorithm>
39using namespace llvm;
40
Evan Cheng5ed91b52008-06-13 23:58:02 +000041STATISTIC(NumSpills , "Number of register spills");
Dan Gohman089efff2008-05-13 00:00:25 +000042
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043//===----------------------------------------------------------------------===//
44// VirtRegMap implementation
45//===----------------------------------------------------------------------===//
46
Owen Andersondd56ab72009-03-13 05:55:11 +000047char VirtRegMap::ID = 0;
48
49static RegisterPass<VirtRegMap>
50X("virtregmap", "Virtual Register Map");
51
52bool VirtRegMap::runOnMachineFunction(MachineFunction &mf) {
53 TII = mf.getTarget().getInstrInfo();
54 MF = &mf;
55
56 ReMatId = MAX_STACK_SLOT+1;
57 LowSpillSlot = HighSpillSlot = NO_STACK_SLOT;
58
59 Virt2PhysMap.clear();
60 Virt2StackSlotMap.clear();
61 Virt2ReMatIdMap.clear();
62 Virt2SplitMap.clear();
63 Virt2SplitKillMap.clear();
64 ReMatMap.clear();
65 ImplicitDefed.clear();
66 SpillSlotToUsesMap.clear();
67 MI2VirtMap.clear();
68 SpillPt2VirtMap.clear();
69 RestorePt2VirtMap.clear();
70 EmergencySpillMap.clear();
71 EmergencySpillSlots.clear();
72
Evan Chengda872532008-02-27 03:04:06 +000073 SpillSlotToUsesMap.resize(8);
Owen Andersondd56ab72009-03-13 05:55:11 +000074 ImplicitDefed.resize(MF->getRegInfo().getLastVirtReg()+1-
Evan Cheng7b88cbc2008-04-11 17:53:36 +000075 TargetRegisterInfo::FirstVirtualRegister);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 grow();
Owen Andersondd56ab72009-03-13 05:55:11 +000077
78 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079}
80
81void VirtRegMap::grow() {
Owen Andersondd56ab72009-03-13 05:55:11 +000082 unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg();
Evan Cheng1204d172007-08-13 23:45:17 +000083 Virt2PhysMap.grow(LastVirtReg);
84 Virt2StackSlotMap.grow(LastVirtReg);
85 Virt2ReMatIdMap.grow(LastVirtReg);
Evan Chengcecc8222007-11-17 00:40:40 +000086 Virt2SplitMap.grow(LastVirtReg);
Evan Cheng6f522672007-12-05 09:51:10 +000087 Virt2SplitKillMap.grow(LastVirtReg);
Evan Cheng1204d172007-08-13 23:45:17 +000088 ReMatMap.grow(LastVirtReg);
Evan Cheng7b88cbc2008-04-11 17:53:36 +000089 ImplicitDefed.resize(LastVirtReg-TargetRegisterInfo::FirstVirtualRegister+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090}
91
92int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
Dan Gohman1e57df32008-02-10 18:45:23 +000093 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
95 "attempt to assign stack slot to already spilled register");
Owen Andersondd56ab72009-03-13 05:55:11 +000096 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtReg);
97 int SS = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
Evan Chengda872532008-02-27 03:04:06 +000098 RC->getAlignment());
99 if (LowSpillSlot == NO_STACK_SLOT)
100 LowSpillSlot = SS;
101 if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
102 HighSpillSlot = SS;
103 unsigned Idx = SS-LowSpillSlot;
104 while (Idx >= SpillSlotToUsesMap.size())
105 SpillSlotToUsesMap.resize(SpillSlotToUsesMap.size()*2);
106 Virt2StackSlotMap[virtReg] = SS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 ++NumSpills;
Evan Chengda872532008-02-27 03:04:06 +0000108 return SS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109}
110
Evan Chengda872532008-02-27 03:04:06 +0000111void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int SS) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000112 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
114 "attempt to assign stack slot to already spilled register");
Evan Chengda872532008-02-27 03:04:06 +0000115 assert((SS >= 0 ||
Owen Andersondd56ab72009-03-13 05:55:11 +0000116 (SS >= MF->getFrameInfo()->getObjectIndexBegin())) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 "illegal fixed frame index");
Evan Chengda872532008-02-27 03:04:06 +0000118 Virt2StackSlotMap[virtReg] = SS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119}
120
121int VirtRegMap::assignVirtReMatId(unsigned virtReg) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000122 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng1204d172007-08-13 23:45:17 +0000123 assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 "attempt to assign re-mat id to already spilled register");
Evan Cheng1204d172007-08-13 23:45:17 +0000125 Virt2ReMatIdMap[virtReg] = ReMatId;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 return ReMatId++;
127}
128
Evan Cheng1204d172007-08-13 23:45:17 +0000129void VirtRegMap::assignVirtReMatId(unsigned virtReg, int id) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000130 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng1204d172007-08-13 23:45:17 +0000131 assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
132 "attempt to assign re-mat id to already spilled register");
133 Virt2ReMatIdMap[virtReg] = id;
134}
135
Evan Cheng14cc83f2008-03-11 07:19:34 +0000136int VirtRegMap::getEmergencySpillSlot(const TargetRegisterClass *RC) {
137 std::map<const TargetRegisterClass*, int>::iterator I =
138 EmergencySpillSlots.find(RC);
139 if (I != EmergencySpillSlots.end())
140 return I->second;
Owen Andersondd56ab72009-03-13 05:55:11 +0000141 int SS = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
Evan Cheng14cc83f2008-03-11 07:19:34 +0000142 RC->getAlignment());
143 if (LowSpillSlot == NO_STACK_SLOT)
144 LowSpillSlot = SS;
145 if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
146 HighSpillSlot = SS;
Dan Gohmanad077b82008-10-06 18:00:07 +0000147 EmergencySpillSlots[RC] = SS;
Evan Cheng14cc83f2008-03-11 07:19:34 +0000148 return SS;
149}
150
Evan Chengda872532008-02-27 03:04:06 +0000151void VirtRegMap::addSpillSlotUse(int FI, MachineInstr *MI) {
Owen Andersondd56ab72009-03-13 05:55:11 +0000152 if (!MF->getFrameInfo()->isFixedObjectIndex(FI)) {
David Greene022e2b32008-05-22 21:12:21 +0000153 // If FI < LowSpillSlot, this stack reference was produced by
154 // instruction selection and is not a spill
155 if (FI >= LowSpillSlot) {
156 assert(FI >= 0 && "Spill slot index should not be negative!");
Bill Wendling8c333682008-05-23 01:29:08 +0000157 assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
David Greene022e2b32008-05-22 21:12:21 +0000158 && "Invalid spill slot");
159 SpillSlotToUsesMap[FI-LowSpillSlot].insert(MI);
160 }
Evan Chengda872532008-02-27 03:04:06 +0000161 }
162}
163
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI,
Evan Chengfd0bd3c2007-12-02 08:30:39 +0000165 MachineInstr *NewMI, ModRef MRInfo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 // Move previous memory references folded to new instruction.
167 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI);
168 for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI),
169 E = MI2VirtMap.end(); I != E && I->first == OldMI; ) {
170 MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second));
171 MI2VirtMap.erase(I++);
172 }
173
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 // add new memory reference
175 MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo)));
176}
177
Evan Chengf3255842007-10-13 02:50:24 +0000178void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo) {
179 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(MI);
180 MI2VirtMap.insert(IP, std::make_pair(MI, std::make_pair(VirtReg, MRInfo)));
181}
182
Evan Chengda872532008-02-27 03:04:06 +0000183void VirtRegMap::RemoveMachineInstrFromMaps(MachineInstr *MI) {
184 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
185 MachineOperand &MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000186 if (!MO.isFI())
Evan Chengda872532008-02-27 03:04:06 +0000187 continue;
188 int FI = MO.getIndex();
Owen Andersondd56ab72009-03-13 05:55:11 +0000189 if (MF->getFrameInfo()->isFixedObjectIndex(FI))
Evan Chengda872532008-02-27 03:04:06 +0000190 continue;
David Greene022e2b32008-05-22 21:12:21 +0000191 // This stack reference was produced by instruction selection and
Bill Wendlingfbdad532009-03-31 08:41:31 +0000192 // is not a spill
David Greene022e2b32008-05-22 21:12:21 +0000193 if (FI < LowSpillSlot)
194 continue;
Bill Wendling8c333682008-05-23 01:29:08 +0000195 assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
David Greene022e2b32008-05-22 21:12:21 +0000196 && "Invalid spill slot");
Evan Chengda872532008-02-27 03:04:06 +0000197 SpillSlotToUsesMap[FI-LowSpillSlot].erase(MI);
198 }
199 MI2VirtMap.erase(MI);
200 SpillPt2VirtMap.erase(MI);
201 RestorePt2VirtMap.erase(MI);
Evan Cheng14cc83f2008-03-11 07:19:34 +0000202 EmergencySpillMap.erase(MI);
Evan Chengda872532008-02-27 03:04:06 +0000203}
204
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000205/// FindUnusedRegisters - Gather a list of allocatable registers that
206/// have not been allocated to any virtual register.
207bool VirtRegMap::FindUnusedRegisters(const TargetRegisterInfo *TRI,
208 LiveIntervals* LIs) {
209 unsigned NumRegs = TRI->getNumRegs();
210 UnusedRegs.reset();
211 UnusedRegs.resize(NumRegs);
212
213 BitVector Used(NumRegs);
214 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
215 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
216 if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
217 Used.set(Virt2PhysMap[i]);
218
219 BitVector Allocatable = TRI->getAllocatableSet(*MF);
220 bool AnyUnused = false;
221 for (unsigned Reg = 1; Reg < NumRegs; ++Reg) {
222 if (Allocatable[Reg] && !Used[Reg] && !LIs->hasInterval(Reg)) {
223 bool ReallyUnused = true;
224 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
225 if (Used[*AS] || LIs->hasInterval(*AS)) {
226 ReallyUnused = false;
227 break;
228 }
229 }
230 if (ReallyUnused) {
231 AnyUnused = true;
232 UnusedRegs.set(Reg);
233 }
234 }
235 }
236
237 return AnyUnused;
238}
239
Owen Andersondd56ab72009-03-13 05:55:11 +0000240void VirtRegMap::print(std::ostream &OS, const Module* M) const {
241 const TargetRegisterInfo* TRI = MF->getTarget().getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242
243 OS << "********** REGISTER MAP **********\n";
Dan Gohman1e57df32008-02-10 18:45:23 +0000244 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Owen Andersondd56ab72009-03-13 05:55:11 +0000245 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000247 OS << "[reg" << i << " -> " << TRI->getName(Virt2PhysMap[i])
Bill Wendling8eeb9792008-02-26 21:11:01 +0000248 << "]\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 }
250
Dan Gohman1e57df32008-02-10 18:45:23 +0000251 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Owen Andersondd56ab72009-03-13 05:55:11 +0000252 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT)
254 OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n";
255 OS << '\n';
256}
257
258void VirtRegMap::dump() const {
Dan Gohmanecb9ad52008-03-12 20:52:10 +0000259 print(cerr);
Daniel Dunbarc863a612009-03-14 01:53:05 +0000260}