blob: 7213088bc3c6418f0c905c41b862d25f21275a02 [file] [log] [blame]
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +00001//===-- llvm/CodeGen/VirtRegMap.h - Virtual Register Map -*- C++ -*--------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner8c4d88d2004-09-30 01:54:45 +000010// This file implements a virtual register map. This maps virtual registers to
11// physical registers and virtual registers to stack slots. It is created and
12// updated by a register allocator and then used by a machine code rewriter that
13// adds spill code and rewrites virtual into physical register references.
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000014//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_CODEGEN_VIRTREGMAP_H
18#define LLVM_CODEGEN_VIRTREGMAP_H
19
Owen Anderson49c8aa02009-03-13 05:55:11 +000020#include "llvm/CodeGen/MachineFunctionPass.h"
Lang Hames86511252009-09-04 20:41:11 +000021#include "llvm/CodeGen/LiveInterval.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000022#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng4cce6b42008-04-11 17:53:36 +000023#include "llvm/ADT/BitVector.h"
Evan Chengc781a242009-05-03 18:32:42 +000024#include "llvm/ADT/DenseMap.h"
Chris Lattner94c002a2007-02-01 05:32:05 +000025#include "llvm/ADT/IndexedMap.h"
Evan Chengd3653122008-02-27 03:04:06 +000026#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmand68a0762009-01-05 17:59:02 +000027#include "llvm/ADT/SmallVector.h"
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000028#include <map>
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000029
30namespace llvm {
Evan Chengc781a242009-05-03 18:32:42 +000031 class LiveIntervals;
Chris Lattner8c4d88d2004-09-30 01:54:45 +000032 class MachineInstr;
David Greene7e231462007-08-07 16:34:05 +000033 class MachineFunction;
Evan Cheng90f95f82009-06-14 20:22:55 +000034 class MachineRegisterInfo;
Chris Lattner29268692006-09-05 02:12:02 +000035 class TargetInstrInfo;
Mike Stumpfe095f32009-05-04 18:40:41 +000036 class TargetRegisterInfo;
Daniel Dunbar1cd1d982009-07-24 10:36:58 +000037 class raw_ostream;
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +000038 class SlotIndexes;
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000039
Owen Anderson49c8aa02009-03-13 05:55:11 +000040 class VirtRegMap : public MachineFunctionPass {
Chris Lattner8c4d88d2004-09-30 01:54:45 +000041 public:
Evan Cheng2638e1a2007-03-20 08:13:50 +000042 enum {
43 NO_PHYS_REG = 0,
Evan Cheng91935142007-04-04 07:40:01 +000044 NO_STACK_SLOT = (1L << 30)-1,
45 MAX_STACK_SLOT = (1L << 18)-1
Evan Cheng2638e1a2007-03-20 08:13:50 +000046 };
47
Chris Lattner35f27052006-05-01 21:16:03 +000048 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000049 typedef std::multimap<MachineInstr*,
50 std::pair<unsigned, ModRef> > MI2VirtMapTy;
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000051
Chris Lattner8c4d88d2004-09-30 01:54:45 +000052 private:
Evan Cheng90f95f82009-06-14 20:22:55 +000053 MachineRegisterInfo *MRI;
Owen Anderson49c8aa02009-03-13 05:55:11 +000054 const TargetInstrInfo *TII;
Mike Stumpfe095f32009-05-04 18:40:41 +000055 const TargetRegisterInfo *TRI;
Owen Anderson49c8aa02009-03-13 05:55:11 +000056 MachineFunction *MF;
Mike Stumpfe095f32009-05-04 18:40:41 +000057
58 DenseMap<const TargetRegisterClass*, BitVector> allocatableRCRegs;
59
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000060 /// Virt2PhysMap - This is a virtual to physical register
61 /// mapping. Each virtual register is required to have an entry in
62 /// it; even spilled virtual registers (the register mapped to a
63 /// spilled register is the temporary used to load it from the
64 /// stack).
Chris Lattner94c002a2007-02-01 05:32:05 +000065 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
Evan Cheng81a03822007-11-17 00:40:40 +000066
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000067 /// Virt2StackSlotMap - This is virtual register to stack slot
68 /// mapping. Each spilled virtual register has an entry in it
69 /// which corresponds to the stack slot this register is spilled
70 /// at.
Chris Lattner94c002a2007-02-01 05:32:05 +000071 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
Evan Cheng81a03822007-11-17 00:40:40 +000072
Evan Cheng81a03822007-11-17 00:40:40 +000073 /// Virt2SplitMap - This is virtual register to splitted virtual register
74 /// mapping.
75 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
76
Evan Chengd3653122008-02-27 03:04:06 +000077 /// LowSpillSlot, HighSpillSlot - Lowest and highest spill slot indexes.
78 int LowSpillSlot, HighSpillSlot;
79
80 /// SpillSlotToUsesMap - Records uses for each register spill slot.
81 SmallVector<SmallPtrSet<MachineInstr*, 4>, 8> SpillSlotToUsesMap;
82
Jakob Stoklund Olesenb55e91e2010-11-16 00:41:01 +000083 /// createSpillSlot - Allocate a spill slot for RC from MFI.
84 unsigned createSpillSlot(const TargetRegisterClass *RC);
85
Chris Lattner8c4d88d2004-09-30 01:54:45 +000086 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
87 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
Alkis Evlogimenos79742872004-02-23 23:47:10 +000088
Chris Lattner8c4d88d2004-09-30 01:54:45 +000089 public:
Owen Anderson49c8aa02009-03-13 05:55:11 +000090 static char ID;
Owen Anderson90c579d2010-08-06 18:33:48 +000091 VirtRegMap() : MachineFunctionPass(ID), Virt2PhysMap(NO_PHYS_REG),
Jakob Stoklund Olesen3cb0b0e2011-11-13 01:02:04 +000092 Virt2StackSlotMap(NO_STACK_SLOT), Virt2SplitMap(0),
Owen Anderson49c8aa02009-03-13 05:55:11 +000093 LowSpillSlot(NO_STACK_SLOT), HighSpillSlot(NO_STACK_SLOT) { }
94 virtual bool runOnMachineFunction(MachineFunction &MF);
95
96 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
97 AU.setPreservesAll();
98 MachineFunctionPass::getAnalysisUsage(AU);
99 }
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000100
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000101 MachineFunction &getMachineFunction() const {
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +0000102 assert(MF && "getMachineFunction called before runOnMachineFunction");
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +0000103 return *MF;
104 }
105
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +0000106 MachineRegisterInfo &getRegInfo() const { return *MRI; }
107 const TargetRegisterInfo &getTargetRegInfo() const { return *TRI; }
108
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000109 void grow();
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000110
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000111 /// @brief returns true if the specified virtual register is
112 /// mapped to a physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000113 bool hasPhys(unsigned virtReg) const {
114 return getPhys(virtReg) != NO_PHYS_REG;
115 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000116
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000117 /// @brief returns the physical register mapped to the specified
118 /// virtual register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000119 unsigned getPhys(unsigned virtReg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000120 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000121 return Virt2PhysMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000122 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000123
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000124 /// @brief creates a mapping for the specified virtual register to
125 /// the specified physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000126 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000127 assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
128 TargetRegisterInfo::isPhysicalRegister(physReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000129 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000130 "attempt to assign physical register to already mapped "
131 "virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +0000132 Virt2PhysMap[virtReg] = physReg;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000133 }
134
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000135 /// @brief clears the specified virtual register's, physical
136 /// register mapping
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000137 void clearVirt(unsigned virtReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000138 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000139 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000140 "attempt to clear a not assigned virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +0000141 Virt2PhysMap[virtReg] = NO_PHYS_REG;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000142 }
143
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000144 /// @brief clears all virtual to physical register mappings
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000145 void clearAllVirt() {
Chris Lattner7f690e62004-09-30 02:15:18 +0000146 Virt2PhysMap.clear();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000147 grow();
148 }
149
Evan Cheng90f95f82009-06-14 20:22:55 +0000150 /// @brief returns the register allocation preference.
151 unsigned getRegAllocPref(unsigned virtReg);
152
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000153 /// @brief returns true if VirtReg is assigned to its preferred physreg.
154 bool hasPreferredPhys(unsigned VirtReg) {
155 return getPhys(VirtReg) == getRegAllocPref(VirtReg);
156 }
157
Evan Cheng81a03822007-11-17 00:40:40 +0000158 /// @brief records virtReg is a split live interval from SReg.
159 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
160 Virt2SplitMap[virtReg] = SReg;
161 }
162
163 /// @brief returns the live interval virtReg is split from.
Jakob Stoklund Olesene324f6e2011-02-18 22:35:20 +0000164 unsigned getPreSplitReg(unsigned virtReg) const {
Evan Cheng81a03822007-11-17 00:40:40 +0000165 return Virt2SplitMap[virtReg];
166 }
167
Jakob Stoklund Olesenfd389172011-02-19 00:38:43 +0000168 /// getOriginal - Return the original virtual register that VirtReg descends
169 /// from through splitting.
170 /// A register that was not created by splitting is its own original.
171 /// This operation is idempotent.
172 unsigned getOriginal(unsigned VirtReg) const {
173 unsigned Orig = getPreSplitReg(VirtReg);
174 return Orig ? Orig : VirtReg;
175 }
176
Dan Gohman39e33ac2008-03-12 20:50:04 +0000177 /// @brief returns true if the specified virtual register is not
Evan Cheng549f27d32007-08-13 23:45:17 +0000178 /// mapped to a stack slot or rematerialized.
179 bool isAssignedReg(unsigned virtReg) const {
Jakob Stoklund Olesen3cb0b0e2011-11-13 01:02:04 +0000180 if (getStackSlot(virtReg) == NO_STACK_SLOT)
Evan Cheng81a03822007-11-17 00:40:40 +0000181 return true;
182 // Split register can be assigned a physical register as well as a
183 // stack slot or remat id.
184 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000185 }
186
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000187 /// @brief returns the stack slot mapped to the specified virtual
188 /// register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000189 int getStackSlot(unsigned virtReg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000190 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000191 return Virt2StackSlotMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000192 }
193
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000194 /// @brief create a mapping for the specifed virtual register to
195 /// the next available stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000196 int assignVirt2StackSlot(unsigned virtReg);
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000197 /// @brief create a mapping for the specified virtual register to
198 /// the specified stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000199 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
200
Evan Chengd3653122008-02-27 03:04:06 +0000201 /// @brief Records a spill slot use.
202 void addSpillSlotUse(int FrameIndex, MachineInstr *MI);
203
204 /// @brief Returns true if spill slot has been used.
205 bool isSpillSlotUsed(int FrameIndex) const {
206 assert(FrameIndex >= 0 && "Spill slot index should not be negative!");
207 return !SpillSlotToUsesMap[FrameIndex-LowSpillSlot].empty();
208 }
209
Evan Chengcada2452007-11-28 01:28:46 +0000210 /// RemoveMachineInstrFromMaps - MI is being erased, remove it from the
211 /// the folded instruction map and spill point map.
Evan Chengd3653122008-02-27 03:04:06 +0000212 void RemoveMachineInstrFromMaps(MachineInstr *MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000213
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +0000214 /// rewrite - Rewrite all instructions in MF to use only physical registers
215 /// by mapping all virtual register operands to their assigned physical
216 /// registers.
217 ///
218 /// @param Indexes Optionally remove deleted instructions from indexes.
219 void rewrite(SlotIndexes *Indexes);
220
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000221 void print(raw_ostream &OS, const Module* M = 0) const;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000222 void dump() const;
223 };
224
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000225 inline raw_ostream &operator<<(raw_ostream &OS, const VirtRegMap &VRM) {
226 VRM.print(OS);
227 return OS;
228 }
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000229} // End llvm namespace
230
231#endif