blob: 29637b954f0bde4dc614f5a97f79a5408680e4d4 [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) {
54 TII = mf.getTarget().getInstrInfo();
Evan Cheng5277da72009-05-04 03:30:11 +000055 TRI = mf.getTarget().getRegisterInfo();
Owen Andersondd56ab72009-03-13 05:55:11 +000056 MF = &mf;
57
58 ReMatId = MAX_STACK_SLOT+1;
59 LowSpillSlot = HighSpillSlot = NO_STACK_SLOT;
60
61 Virt2PhysMap.clear();
62 Virt2StackSlotMap.clear();
63 Virt2ReMatIdMap.clear();
64 Virt2SplitMap.clear();
65 Virt2SplitKillMap.clear();
66 ReMatMap.clear();
67 ImplicitDefed.clear();
68 SpillSlotToUsesMap.clear();
69 MI2VirtMap.clear();
70 SpillPt2VirtMap.clear();
71 RestorePt2VirtMap.clear();
72 EmergencySpillMap.clear();
73 EmergencySpillSlots.clear();
74
Evan Chengda872532008-02-27 03:04:06 +000075 SpillSlotToUsesMap.resize(8);
Owen Andersondd56ab72009-03-13 05:55:11 +000076 ImplicitDefed.resize(MF->getRegInfo().getLastVirtReg()+1-
Evan Cheng7b88cbc2008-04-11 17:53:36 +000077 TargetRegisterInfo::FirstVirtualRegister);
Evan Cheng5277da72009-05-04 03:30:11 +000078
79 allocatableRCRegs.clear();
80 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
81 E = TRI->regclass_end(); I != E; ++I)
82 allocatableRCRegs.insert(std::make_pair(*I,
83 TRI->getAllocatableSet(mf, *I)));
84
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 grow();
Owen Andersondd56ab72009-03-13 05:55:11 +000086
87 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088}
89
90void VirtRegMap::grow() {
Owen Andersondd56ab72009-03-13 05:55:11 +000091 unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg();
Evan Cheng1204d172007-08-13 23:45:17 +000092 Virt2PhysMap.grow(LastVirtReg);
93 Virt2StackSlotMap.grow(LastVirtReg);
94 Virt2ReMatIdMap.grow(LastVirtReg);
Evan Chengcecc8222007-11-17 00:40:40 +000095 Virt2SplitMap.grow(LastVirtReg);
Evan Cheng6f522672007-12-05 09:51:10 +000096 Virt2SplitKillMap.grow(LastVirtReg);
Evan Cheng1204d172007-08-13 23:45:17 +000097 ReMatMap.grow(LastVirtReg);
Evan Cheng7b88cbc2008-04-11 17:53:36 +000098 ImplicitDefed.resize(LastVirtReg-TargetRegisterInfo::FirstVirtualRegister+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099}
100
101int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000102 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
104 "attempt to assign stack slot to already spilled register");
Owen Andersondd56ab72009-03-13 05:55:11 +0000105 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtReg);
106 int SS = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
Evan Chengda872532008-02-27 03:04:06 +0000107 RC->getAlignment());
108 if (LowSpillSlot == NO_STACK_SLOT)
109 LowSpillSlot = SS;
110 if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
111 HighSpillSlot = SS;
112 unsigned Idx = SS-LowSpillSlot;
113 while (Idx >= SpillSlotToUsesMap.size())
114 SpillSlotToUsesMap.resize(SpillSlotToUsesMap.size()*2);
115 Virt2StackSlotMap[virtReg] = SS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 ++NumSpills;
Evan Chengda872532008-02-27 03:04:06 +0000117 return SS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118}
119
Evan Chengda872532008-02-27 03:04:06 +0000120void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int SS) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000121 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
123 "attempt to assign stack slot to already spilled register");
Evan Chengda872532008-02-27 03:04:06 +0000124 assert((SS >= 0 ||
Owen Andersondd56ab72009-03-13 05:55:11 +0000125 (SS >= MF->getFrameInfo()->getObjectIndexBegin())) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 "illegal fixed frame index");
Evan Chengda872532008-02-27 03:04:06 +0000127 Virt2StackSlotMap[virtReg] = SS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128}
129
130int VirtRegMap::assignVirtReMatId(unsigned virtReg) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000131 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng1204d172007-08-13 23:45:17 +0000132 assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 "attempt to assign re-mat id to already spilled register");
Evan Cheng1204d172007-08-13 23:45:17 +0000134 Virt2ReMatIdMap[virtReg] = ReMatId;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 return ReMatId++;
136}
137
Evan Cheng1204d172007-08-13 23:45:17 +0000138void VirtRegMap::assignVirtReMatId(unsigned virtReg, int id) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000139 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng1204d172007-08-13 23:45:17 +0000140 assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
141 "attempt to assign re-mat id to already spilled register");
142 Virt2ReMatIdMap[virtReg] = id;
143}
144
Evan Cheng14cc83f2008-03-11 07:19:34 +0000145int VirtRegMap::getEmergencySpillSlot(const TargetRegisterClass *RC) {
146 std::map<const TargetRegisterClass*, int>::iterator I =
147 EmergencySpillSlots.find(RC);
148 if (I != EmergencySpillSlots.end())
149 return I->second;
Owen Andersondd56ab72009-03-13 05:55:11 +0000150 int SS = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
Evan Cheng14cc83f2008-03-11 07:19:34 +0000151 RC->getAlignment());
152 if (LowSpillSlot == NO_STACK_SLOT)
153 LowSpillSlot = SS;
154 if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
155 HighSpillSlot = SS;
Dan Gohmanad077b82008-10-06 18:00:07 +0000156 EmergencySpillSlots[RC] = SS;
Evan Cheng14cc83f2008-03-11 07:19:34 +0000157 return SS;
158}
159
Evan Chengda872532008-02-27 03:04:06 +0000160void VirtRegMap::addSpillSlotUse(int FI, MachineInstr *MI) {
Owen Andersondd56ab72009-03-13 05:55:11 +0000161 if (!MF->getFrameInfo()->isFixedObjectIndex(FI)) {
David Greene022e2b32008-05-22 21:12:21 +0000162 // If FI < LowSpillSlot, this stack reference was produced by
163 // instruction selection and is not a spill
164 if (FI >= LowSpillSlot) {
165 assert(FI >= 0 && "Spill slot index should not be negative!");
Bill Wendling8c333682008-05-23 01:29:08 +0000166 assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
David Greene022e2b32008-05-22 21:12:21 +0000167 && "Invalid spill slot");
168 SpillSlotToUsesMap[FI-LowSpillSlot].insert(MI);
169 }
Evan Chengda872532008-02-27 03:04:06 +0000170 }
171}
172
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI,
Evan Chengfd0bd3c2007-12-02 08:30:39 +0000174 MachineInstr *NewMI, ModRef MRInfo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 // Move previous memory references folded to new instruction.
176 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI);
177 for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI),
178 E = MI2VirtMap.end(); I != E && I->first == OldMI; ) {
179 MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second));
180 MI2VirtMap.erase(I++);
181 }
182
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 // add new memory reference
184 MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo)));
185}
186
Evan Chengf3255842007-10-13 02:50:24 +0000187void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo) {
188 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(MI);
189 MI2VirtMap.insert(IP, std::make_pair(MI, std::make_pair(VirtReg, MRInfo)));
190}
191
Evan Chengda872532008-02-27 03:04:06 +0000192void VirtRegMap::RemoveMachineInstrFromMaps(MachineInstr *MI) {
193 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
194 MachineOperand &MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000195 if (!MO.isFI())
Evan Chengda872532008-02-27 03:04:06 +0000196 continue;
197 int FI = MO.getIndex();
Owen Andersondd56ab72009-03-13 05:55:11 +0000198 if (MF->getFrameInfo()->isFixedObjectIndex(FI))
Evan Chengda872532008-02-27 03:04:06 +0000199 continue;
David Greene022e2b32008-05-22 21:12:21 +0000200 // This stack reference was produced by instruction selection and
Bill Wendlingfbdad532009-03-31 08:41:31 +0000201 // is not a spill
David Greene022e2b32008-05-22 21:12:21 +0000202 if (FI < LowSpillSlot)
203 continue;
Bill Wendling8c333682008-05-23 01:29:08 +0000204 assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
David Greene022e2b32008-05-22 21:12:21 +0000205 && "Invalid spill slot");
Evan Chengda872532008-02-27 03:04:06 +0000206 SpillSlotToUsesMap[FI-LowSpillSlot].erase(MI);
207 }
208 MI2VirtMap.erase(MI);
209 SpillPt2VirtMap.erase(MI);
210 RestorePt2VirtMap.erase(MI);
Evan Cheng14cc83f2008-03-11 07:19:34 +0000211 EmergencySpillMap.erase(MI);
Evan Chengda872532008-02-27 03:04:06 +0000212}
213
Evan Cheng97c5f1f2009-05-03 18:32:42 +0000214/// FindUnusedRegisters - Gather a list of allocatable registers that
215/// have not been allocated to any virtual register.
216bool VirtRegMap::FindUnusedRegisters(const TargetRegisterInfo *TRI,
217 LiveIntervals* LIs) {
218 unsigned NumRegs = TRI->getNumRegs();
219 UnusedRegs.reset();
220 UnusedRegs.resize(NumRegs);
221
222 BitVector Used(NumRegs);
223 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
224 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
225 if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
226 Used.set(Virt2PhysMap[i]);
227
228 BitVector Allocatable = TRI->getAllocatableSet(*MF);
229 bool AnyUnused = false;
230 for (unsigned Reg = 1; Reg < NumRegs; ++Reg) {
231 if (Allocatable[Reg] && !Used[Reg] && !LIs->hasInterval(Reg)) {
232 bool ReallyUnused = true;
233 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
234 if (Used[*AS] || LIs->hasInterval(*AS)) {
235 ReallyUnused = false;
236 break;
237 }
238 }
239 if (ReallyUnused) {
240 AnyUnused = true;
241 UnusedRegs.set(Reg);
242 }
243 }
244 }
245
246 return AnyUnused;
247}
248
Owen Andersondd56ab72009-03-13 05:55:11 +0000249void VirtRegMap::print(std::ostream &OS, const Module* M) const {
250 const TargetRegisterInfo* TRI = MF->getTarget().getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251
252 OS << "********** REGISTER MAP **********\n";
Dan Gohman1e57df32008-02-10 18:45:23 +0000253 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Owen Andersondd56ab72009-03-13 05:55:11 +0000254 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000256 OS << "[reg" << i << " -> " << TRI->getName(Virt2PhysMap[i])
Bill Wendling8eeb9792008-02-26 21:11:01 +0000257 << "]\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 }
259
Dan Gohman1e57df32008-02-10 18:45:23 +0000260 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Owen Andersondd56ab72009-03-13 05:55:11 +0000261 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT)
263 OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n";
264 OS << '\n';
265}
266
267void VirtRegMap::dump() const {
Dan Gohmanecb9ad52008-03-12 20:52:10 +0000268 print(cerr);
Daniel Dunbarc863a612009-03-14 01:53:05 +0000269}