blob: 2c7037cfb45a1097857b1c5502a141dbe66e46e8 [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"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng7b88cbc2008-04-11 17:53:36 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner1b989192007-12-31 04:13:23 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/Target/TargetMachine.h"
27#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Support/CommandLine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/Support/Compiler.h"
Evan Chengfc201f32009-02-11 08:24:21 +000030#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/ADT/BitVector.h"
Evan Cheng1376d862008-06-04 09:16:33 +000032#include "llvm/ADT/DenseMap.h"
Evan Chengfc201f32009-02-11 08:24:21 +000033#include "llvm/ADT/DepthFirstIterator.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/ADT/Statistic.h"
35#include "llvm/ADT/STLExtras.h"
36#include "llvm/ADT/SmallSet.h"
37#include <algorithm>
38using namespace llvm;
39
Evan Cheng5ed91b52008-06-13 23:58:02 +000040STATISTIC(NumSpills , "Number of register spills");
Dan Gohman089efff2008-05-13 00:00:25 +000041
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042//===----------------------------------------------------------------------===//
43// VirtRegMap implementation
44//===----------------------------------------------------------------------===//
45
Owen Andersondd56ab72009-03-13 05:55:11 +000046char VirtRegMap::ID = 0;
47
48static RegisterPass<VirtRegMap>
49X("virtregmap", "Virtual Register Map");
50
51bool VirtRegMap::runOnMachineFunction(MachineFunction &mf) {
52 TII = mf.getTarget().getInstrInfo();
53 MF = &mf;
54
55 ReMatId = MAX_STACK_SLOT+1;
56 LowSpillSlot = HighSpillSlot = NO_STACK_SLOT;
57
58 Virt2PhysMap.clear();
59 Virt2StackSlotMap.clear();
60 Virt2ReMatIdMap.clear();
61 Virt2SplitMap.clear();
62 Virt2SplitKillMap.clear();
63 ReMatMap.clear();
64 ImplicitDefed.clear();
65 SpillSlotToUsesMap.clear();
66 MI2VirtMap.clear();
67 SpillPt2VirtMap.clear();
68 RestorePt2VirtMap.clear();
69 EmergencySpillMap.clear();
70 EmergencySpillSlots.clear();
71
Evan Chengda872532008-02-27 03:04:06 +000072 SpillSlotToUsesMap.resize(8);
Owen Andersondd56ab72009-03-13 05:55:11 +000073 ImplicitDefed.resize(MF->getRegInfo().getLastVirtReg()+1-
Evan Cheng7b88cbc2008-04-11 17:53:36 +000074 TargetRegisterInfo::FirstVirtualRegister);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 grow();
Owen Andersondd56ab72009-03-13 05:55:11 +000076
77 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078}
79
80void VirtRegMap::grow() {
Owen Andersondd56ab72009-03-13 05:55:11 +000081 unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg();
Evan Cheng1204d172007-08-13 23:45:17 +000082 Virt2PhysMap.grow(LastVirtReg);
83 Virt2StackSlotMap.grow(LastVirtReg);
84 Virt2ReMatIdMap.grow(LastVirtReg);
Evan Chengcecc8222007-11-17 00:40:40 +000085 Virt2SplitMap.grow(LastVirtReg);
Evan Cheng6f522672007-12-05 09:51:10 +000086 Virt2SplitKillMap.grow(LastVirtReg);
Evan Cheng1204d172007-08-13 23:45:17 +000087 ReMatMap.grow(LastVirtReg);
Evan Cheng7b88cbc2008-04-11 17:53:36 +000088 ImplicitDefed.resize(LastVirtReg-TargetRegisterInfo::FirstVirtualRegister+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089}
90
91int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
Dan Gohman1e57df32008-02-10 18:45:23 +000092 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
94 "attempt to assign stack slot to already spilled register");
Owen Andersondd56ab72009-03-13 05:55:11 +000095 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtReg);
96 int SS = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
Evan Chengda872532008-02-27 03:04:06 +000097 RC->getAlignment());
98 if (LowSpillSlot == NO_STACK_SLOT)
99 LowSpillSlot = SS;
100 if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
101 HighSpillSlot = SS;
102 unsigned Idx = SS-LowSpillSlot;
103 while (Idx >= SpillSlotToUsesMap.size())
104 SpillSlotToUsesMap.resize(SpillSlotToUsesMap.size()*2);
105 Virt2StackSlotMap[virtReg] = SS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 ++NumSpills;
Evan Chengda872532008-02-27 03:04:06 +0000107 return SS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108}
109
Evan Chengda872532008-02-27 03:04:06 +0000110void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int SS) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000111 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
113 "attempt to assign stack slot to already spilled register");
Evan Chengda872532008-02-27 03:04:06 +0000114 assert((SS >= 0 ||
Owen Andersondd56ab72009-03-13 05:55:11 +0000115 (SS >= MF->getFrameInfo()->getObjectIndexBegin())) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 "illegal fixed frame index");
Evan Chengda872532008-02-27 03:04:06 +0000117 Virt2StackSlotMap[virtReg] = SS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118}
119
120int VirtRegMap::assignVirtReMatId(unsigned virtReg) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000121 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng1204d172007-08-13 23:45:17 +0000122 assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 "attempt to assign re-mat id to already spilled register");
Evan Cheng1204d172007-08-13 23:45:17 +0000124 Virt2ReMatIdMap[virtReg] = ReMatId;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 return ReMatId++;
126}
127
Evan Cheng1204d172007-08-13 23:45:17 +0000128void VirtRegMap::assignVirtReMatId(unsigned virtReg, int id) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000129 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng1204d172007-08-13 23:45:17 +0000130 assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
131 "attempt to assign re-mat id to already spilled register");
132 Virt2ReMatIdMap[virtReg] = id;
133}
134
Evan Cheng14cc83f2008-03-11 07:19:34 +0000135int VirtRegMap::getEmergencySpillSlot(const TargetRegisterClass *RC) {
136 std::map<const TargetRegisterClass*, int>::iterator I =
137 EmergencySpillSlots.find(RC);
138 if (I != EmergencySpillSlots.end())
139 return I->second;
Owen Andersondd56ab72009-03-13 05:55:11 +0000140 int SS = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
Evan Cheng14cc83f2008-03-11 07:19:34 +0000141 RC->getAlignment());
142 if (LowSpillSlot == NO_STACK_SLOT)
143 LowSpillSlot = SS;
144 if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
145 HighSpillSlot = SS;
Dan Gohmanad077b82008-10-06 18:00:07 +0000146 EmergencySpillSlots[RC] = SS;
Evan Cheng14cc83f2008-03-11 07:19:34 +0000147 return SS;
148}
149
Evan Chengda872532008-02-27 03:04:06 +0000150void VirtRegMap::addSpillSlotUse(int FI, MachineInstr *MI) {
Owen Andersondd56ab72009-03-13 05:55:11 +0000151 if (!MF->getFrameInfo()->isFixedObjectIndex(FI)) {
David Greene022e2b32008-05-22 21:12:21 +0000152 // If FI < LowSpillSlot, this stack reference was produced by
153 // instruction selection and is not a spill
154 if (FI >= LowSpillSlot) {
155 assert(FI >= 0 && "Spill slot index should not be negative!");
Bill Wendling8c333682008-05-23 01:29:08 +0000156 assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
David Greene022e2b32008-05-22 21:12:21 +0000157 && "Invalid spill slot");
158 SpillSlotToUsesMap[FI-LowSpillSlot].insert(MI);
159 }
Evan Chengda872532008-02-27 03:04:06 +0000160 }
161}
162
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI,
Evan Chengfd0bd3c2007-12-02 08:30:39 +0000164 MachineInstr *NewMI, ModRef MRInfo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 // Move previous memory references folded to new instruction.
166 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI);
167 for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI),
168 E = MI2VirtMap.end(); I != E && I->first == OldMI; ) {
169 MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second));
170 MI2VirtMap.erase(I++);
171 }
172
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 // add new memory reference
174 MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo)));
175}
176
Evan Chengf3255842007-10-13 02:50:24 +0000177void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo) {
178 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(MI);
179 MI2VirtMap.insert(IP, std::make_pair(MI, std::make_pair(VirtReg, MRInfo)));
180}
181
Evan Chengda872532008-02-27 03:04:06 +0000182void VirtRegMap::RemoveMachineInstrFromMaps(MachineInstr *MI) {
183 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
184 MachineOperand &MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000185 if (!MO.isFI())
Evan Chengda872532008-02-27 03:04:06 +0000186 continue;
187 int FI = MO.getIndex();
Owen Andersondd56ab72009-03-13 05:55:11 +0000188 if (MF->getFrameInfo()->isFixedObjectIndex(FI))
Evan Chengda872532008-02-27 03:04:06 +0000189 continue;
David Greene022e2b32008-05-22 21:12:21 +0000190 // This stack reference was produced by instruction selection and
191 // is not a spill
192 if (FI < LowSpillSlot)
193 continue;
Bill Wendling8c333682008-05-23 01:29:08 +0000194 assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
David Greene022e2b32008-05-22 21:12:21 +0000195 && "Invalid spill slot");
Evan Chengda872532008-02-27 03:04:06 +0000196 SpillSlotToUsesMap[FI-LowSpillSlot].erase(MI);
197 }
198 MI2VirtMap.erase(MI);
199 SpillPt2VirtMap.erase(MI);
200 RestorePt2VirtMap.erase(MI);
Evan Cheng14cc83f2008-03-11 07:19:34 +0000201 EmergencySpillMap.erase(MI);
Evan Chengda872532008-02-27 03:04:06 +0000202}
203
Owen Andersondd56ab72009-03-13 05:55:11 +0000204void VirtRegMap::print(std::ostream &OS, const Module* M) const {
205 const TargetRegisterInfo* TRI = MF->getTarget().getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206
207 OS << "********** REGISTER MAP **********\n";
Dan Gohman1e57df32008-02-10 18:45:23 +0000208 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Owen Andersondd56ab72009-03-13 05:55:11 +0000209 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000211 OS << "[reg" << i << " -> " << TRI->getName(Virt2PhysMap[i])
Bill Wendling8eeb9792008-02-26 21:11:01 +0000212 << "]\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 }
214
Dan Gohman1e57df32008-02-10 18:45:23 +0000215 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Owen Andersondd56ab72009-03-13 05:55:11 +0000216 e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT)
218 OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n";
219 OS << '\n';
220}
221
222void VirtRegMap::dump() const {
Dan Gohmanecb9ad52008-03-12 20:52:10 +0000223 print(cerr);
Owen Anderson860d4822009-03-11 22:31:21 +0000224}