blob: 0bfbded50c2e1e012ae467831ef2bd2e7f7b3bda [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
46VirtRegMap::VirtRegMap(MachineFunction &mf)
47 : TII(*mf.getTarget().getInstrInfo()), MF(mf),
48 Virt2PhysMap(NO_PHYS_REG), Virt2StackSlotMap(NO_STACK_SLOT),
Evan Chengcecc8222007-11-17 00:40:40 +000049 Virt2ReMatIdMap(NO_STACK_SLOT), Virt2SplitMap(0),
Evan Chengda872532008-02-27 03:04:06 +000050 Virt2SplitKillMap(0), ReMatMap(NULL), ReMatId(MAX_STACK_SLOT+1),
51 LowSpillSlot(NO_STACK_SLOT), HighSpillSlot(NO_STACK_SLOT) {
52 SpillSlotToUsesMap.resize(8);
Evan Cheng7b88cbc2008-04-11 17:53:36 +000053 ImplicitDefed.resize(MF.getRegInfo().getLastVirtReg()+1-
54 TargetRegisterInfo::FirstVirtualRegister);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 grow();
56}
57
58void VirtRegMap::grow() {
Chris Lattner1b989192007-12-31 04:13:23 +000059 unsigned LastVirtReg = MF.getRegInfo().getLastVirtReg();
Evan Cheng1204d172007-08-13 23:45:17 +000060 Virt2PhysMap.grow(LastVirtReg);
61 Virt2StackSlotMap.grow(LastVirtReg);
62 Virt2ReMatIdMap.grow(LastVirtReg);
Evan Chengcecc8222007-11-17 00:40:40 +000063 Virt2SplitMap.grow(LastVirtReg);
Evan Cheng6f522672007-12-05 09:51:10 +000064 Virt2SplitKillMap.grow(LastVirtReg);
Evan Cheng1204d172007-08-13 23:45:17 +000065 ReMatMap.grow(LastVirtReg);
Evan Cheng7b88cbc2008-04-11 17:53:36 +000066 ImplicitDefed.resize(LastVirtReg-TargetRegisterInfo::FirstVirtualRegister+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067}
68
69int VirtRegMap::assignVirt2StackSlot(unsigned virtReg) {
Dan Gohman1e57df32008-02-10 18:45:23 +000070 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
72 "attempt to assign stack slot to already spilled register");
Chris Lattner1b989192007-12-31 04:13:23 +000073 const TargetRegisterClass* RC = MF.getRegInfo().getRegClass(virtReg);
Evan Chengda872532008-02-27 03:04:06 +000074 int SS = MF.getFrameInfo()->CreateStackObject(RC->getSize(),
75 RC->getAlignment());
76 if (LowSpillSlot == NO_STACK_SLOT)
77 LowSpillSlot = SS;
78 if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
79 HighSpillSlot = SS;
80 unsigned Idx = SS-LowSpillSlot;
81 while (Idx >= SpillSlotToUsesMap.size())
82 SpillSlotToUsesMap.resize(SpillSlotToUsesMap.size()*2);
83 Virt2StackSlotMap[virtReg] = SS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 ++NumSpills;
Evan Chengda872532008-02-27 03:04:06 +000085 return SS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086}
87
Evan Chengda872532008-02-27 03:04:06 +000088void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int SS) {
Dan Gohman1e57df32008-02-10 18:45:23 +000089 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
91 "attempt to assign stack slot to already spilled register");
Evan Chengda872532008-02-27 03:04:06 +000092 assert((SS >= 0 ||
93 (SS >= MF.getFrameInfo()->getObjectIndexBegin())) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 "illegal fixed frame index");
Evan Chengda872532008-02-27 03:04:06 +000095 Virt2StackSlotMap[virtReg] = SS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096}
97
98int VirtRegMap::assignVirtReMatId(unsigned virtReg) {
Dan Gohman1e57df32008-02-10 18:45:23 +000099 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng1204d172007-08-13 23:45:17 +0000100 assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 "attempt to assign re-mat id to already spilled register");
Evan Cheng1204d172007-08-13 23:45:17 +0000102 Virt2ReMatIdMap[virtReg] = ReMatId;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 return ReMatId++;
104}
105
Evan Cheng1204d172007-08-13 23:45:17 +0000106void VirtRegMap::assignVirtReMatId(unsigned virtReg, int id) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000107 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Evan Cheng1204d172007-08-13 23:45:17 +0000108 assert(Virt2ReMatIdMap[virtReg] == NO_STACK_SLOT &&
109 "attempt to assign re-mat id to already spilled register");
110 Virt2ReMatIdMap[virtReg] = id;
111}
112
Evan Cheng14cc83f2008-03-11 07:19:34 +0000113int VirtRegMap::getEmergencySpillSlot(const TargetRegisterClass *RC) {
114 std::map<const TargetRegisterClass*, int>::iterator I =
115 EmergencySpillSlots.find(RC);
116 if (I != EmergencySpillSlots.end())
117 return I->second;
118 int SS = MF.getFrameInfo()->CreateStackObject(RC->getSize(),
119 RC->getAlignment());
120 if (LowSpillSlot == NO_STACK_SLOT)
121 LowSpillSlot = SS;
122 if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
123 HighSpillSlot = SS;
Dan Gohmanad077b82008-10-06 18:00:07 +0000124 EmergencySpillSlots[RC] = SS;
Evan Cheng14cc83f2008-03-11 07:19:34 +0000125 return SS;
126}
127
Evan Chengda872532008-02-27 03:04:06 +0000128void VirtRegMap::addSpillSlotUse(int FI, MachineInstr *MI) {
129 if (!MF.getFrameInfo()->isFixedObjectIndex(FI)) {
David Greene022e2b32008-05-22 21:12:21 +0000130 // If FI < LowSpillSlot, this stack reference was produced by
131 // instruction selection and is not a spill
132 if (FI >= LowSpillSlot) {
133 assert(FI >= 0 && "Spill slot index should not be negative!");
Bill Wendling8c333682008-05-23 01:29:08 +0000134 assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
David Greene022e2b32008-05-22 21:12:21 +0000135 && "Invalid spill slot");
136 SpillSlotToUsesMap[FI-LowSpillSlot].insert(MI);
137 }
Evan Chengda872532008-02-27 03:04:06 +0000138 }
139}
140
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *OldMI,
Evan Chengfd0bd3c2007-12-02 08:30:39 +0000142 MachineInstr *NewMI, ModRef MRInfo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 // Move previous memory references folded to new instruction.
144 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(NewMI);
145 for (MI2VirtMapTy::iterator I = MI2VirtMap.lower_bound(OldMI),
146 E = MI2VirtMap.end(); I != E && I->first == OldMI; ) {
147 MI2VirtMap.insert(IP, std::make_pair(NewMI, I->second));
148 MI2VirtMap.erase(I++);
149 }
150
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 // add new memory reference
152 MI2VirtMap.insert(IP, std::make_pair(NewMI, std::make_pair(VirtReg, MRInfo)));
153}
154
Evan Chengf3255842007-10-13 02:50:24 +0000155void VirtRegMap::virtFolded(unsigned VirtReg, MachineInstr *MI, ModRef MRInfo) {
156 MI2VirtMapTy::iterator IP = MI2VirtMap.lower_bound(MI);
157 MI2VirtMap.insert(IP, std::make_pair(MI, std::make_pair(VirtReg, MRInfo)));
158}
159
Evan Chengda872532008-02-27 03:04:06 +0000160void VirtRegMap::RemoveMachineInstrFromMaps(MachineInstr *MI) {
161 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
162 MachineOperand &MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000163 if (!MO.isFI())
Evan Chengda872532008-02-27 03:04:06 +0000164 continue;
165 int FI = MO.getIndex();
166 if (MF.getFrameInfo()->isFixedObjectIndex(FI))
167 continue;
David Greene022e2b32008-05-22 21:12:21 +0000168 // This stack reference was produced by instruction selection and
169 // is not a spill
170 if (FI < LowSpillSlot)
171 continue;
Bill Wendling8c333682008-05-23 01:29:08 +0000172 assert((unsigned)FI-LowSpillSlot < SpillSlotToUsesMap.size()
David Greene022e2b32008-05-22 21:12:21 +0000173 && "Invalid spill slot");
Evan Chengda872532008-02-27 03:04:06 +0000174 SpillSlotToUsesMap[FI-LowSpillSlot].erase(MI);
175 }
176 MI2VirtMap.erase(MI);
177 SpillPt2VirtMap.erase(MI);
178 RestorePt2VirtMap.erase(MI);
Evan Cheng14cc83f2008-03-11 07:19:34 +0000179 EmergencySpillMap.erase(MI);
Evan Chengda872532008-02-27 03:04:06 +0000180}
181
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182void VirtRegMap::print(std::ostream &OS) const {
Dan Gohman1e57df32008-02-10 18:45:23 +0000183 const TargetRegisterInfo* TRI = MF.getTarget().getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184
185 OS << "********** REGISTER MAP **********\n";
Dan Gohman1e57df32008-02-10 18:45:23 +0000186 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Chris Lattner1b989192007-12-31 04:13:23 +0000187 e = MF.getRegInfo().getLastVirtReg(); i <= e; ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 if (Virt2PhysMap[i] != (unsigned)VirtRegMap::NO_PHYS_REG)
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000189 OS << "[reg" << i << " -> " << TRI->getName(Virt2PhysMap[i])
Bill Wendling8eeb9792008-02-26 21:11:01 +0000190 << "]\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 }
192
Dan Gohman1e57df32008-02-10 18:45:23 +0000193 for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
Chris Lattner1b989192007-12-31 04:13:23 +0000194 e = MF.getRegInfo().getLastVirtReg(); i <= e; ++i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 if (Virt2StackSlotMap[i] != VirtRegMap::NO_STACK_SLOT)
196 OS << "[reg" << i << " -> fi#" << Virt2StackSlotMap[i] << "]\n";
197 OS << '\n';
198}
199
200void VirtRegMap::dump() const {
Dan Gohmanecb9ad52008-03-12 20:52:10 +0000201 print(cerr);
Owen Anderson860d4822009-03-11 22:31:21 +0000202}