blob: 8385f3db0499c320ac95c69d037d832ae2af6a74 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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
Chris Lattner8c4d88d2004-09-30 01:54:45 +000020#include "llvm/Target/MRegisterInfo.h"
Evan Cheng2638e1a2007-03-20 08:13:50 +000021#include "llvm/ADT/BitVector.h"
Chris Lattner94c002a2007-02-01 05:32:05 +000022#include "llvm/ADT/IndexedMap.h"
Bill Wendlinge8156192006-12-07 01:30:32 +000023#include "llvm/Support/Streams.h"
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000024#include <map>
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000025
26namespace llvm {
Chris Lattner8c4d88d2004-09-30 01:54:45 +000027 class MachineInstr;
David Greene7e231462007-08-07 16:34:05 +000028 class MachineFunction;
Chris Lattner29268692006-09-05 02:12:02 +000029 class TargetInstrInfo;
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000030
Chris Lattner8c4d88d2004-09-30 01:54:45 +000031 class VirtRegMap {
32 public:
Evan Cheng2638e1a2007-03-20 08:13:50 +000033 enum {
34 NO_PHYS_REG = 0,
Evan Cheng91935142007-04-04 07:40:01 +000035 NO_STACK_SLOT = (1L << 30)-1,
36 MAX_STACK_SLOT = (1L << 18)-1
Evan Cheng2638e1a2007-03-20 08:13:50 +000037 };
38
Chris Lattner35f27052006-05-01 21:16:03 +000039 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000040 typedef std::multimap<MachineInstr*,
41 std::pair<unsigned, ModRef> > MI2VirtMapTy;
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000042
Chris Lattner8c4d88d2004-09-30 01:54:45 +000043 private:
Chris Lattner29268692006-09-05 02:12:02 +000044 const TargetInstrInfo &TII;
45
Chris Lattner7f690e62004-09-30 02:15:18 +000046 MachineFunction &MF;
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000047 /// Virt2PhysMap - This is a virtual to physical register
48 /// mapping. Each virtual register is required to have an entry in
49 /// it; even spilled virtual registers (the register mapped to a
50 /// spilled register is the temporary used to load it from the
51 /// stack).
Chris Lattner94c002a2007-02-01 05:32:05 +000052 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000053 /// Virt2StackSlotMap - This is virtual register to stack slot
54 /// mapping. Each spilled virtual register has an entry in it
55 /// which corresponds to the stack slot this register is spilled
56 /// at.
Chris Lattner94c002a2007-02-01 05:32:05 +000057 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
Evan Cheng549f27d32007-08-13 23:45:17 +000058 IndexedMap<int, VirtReg2IndexFunctor> Virt2ReMatIdMap;
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000059 /// MI2VirtMap - This is MachineInstr to virtual register
60 /// mapping. In the case of memory spill code being folded into
61 /// instructions, we need to know which virtual register was
62 /// read/written by this instruction.
Chris Lattner7f690e62004-09-30 02:15:18 +000063 MI2VirtMapTy MI2VirtMap;
Misha Brukmanedf128a2005-04-21 22:36:52 +000064
Evan Cheng91935142007-04-04 07:40:01 +000065 /// ReMatMap - This is virtual register to re-materialized instruction
Evan Cheng2638e1a2007-03-20 08:13:50 +000066 /// mapping. Each virtual register whose definition is going to be
67 /// re-materialized has an entry in it.
Evan Cheng549f27d32007-08-13 23:45:17 +000068 IndexedMap<MachineInstr*, VirtReg2IndexFunctor> ReMatMap;
Evan Cheng2638e1a2007-03-20 08:13:50 +000069
70 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
Evan Cheng91935142007-04-04 07:40:01 +000071 /// virtual register, an unique id is being assigned. This keeps track of
Evan Cheng2638e1a2007-03-20 08:13:50 +000072 /// the highest id used so far. Note, this starts at (1<<18) to avoid
73 /// conflicts with stack slot numbers.
74 int ReMatId;
75
Chris Lattner8c4d88d2004-09-30 01:54:45 +000076 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
77 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
Alkis Evlogimenos79742872004-02-23 23:47:10 +000078
Chris Lattner8c4d88d2004-09-30 01:54:45 +000079 public:
Dan Gohman61e729e2007-08-02 21:21:54 +000080 explicit VirtRegMap(MachineFunction &mf);
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000081
Chris Lattner8c4d88d2004-09-30 01:54:45 +000082 void grow();
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +000083
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000084 /// @brief returns true if the specified virtual register is
85 /// mapped to a physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +000086 bool hasPhys(unsigned virtReg) const {
87 return getPhys(virtReg) != NO_PHYS_REG;
88 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +000089
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000090 /// @brief returns the physical register mapped to the specified
91 /// virtual register
Chris Lattner8c4d88d2004-09-30 01:54:45 +000092 unsigned getPhys(unsigned virtReg) const {
93 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000094 return Virt2PhysMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +000095 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +000096
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000097 /// @brief creates a mapping for the specified virtual register to
98 /// the specified physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +000099 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
100 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
101 MRegisterInfo::isPhysicalRegister(physReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000102 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000103 "attempt to assign physical register to already mapped "
104 "virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +0000105 Virt2PhysMap[virtReg] = physReg;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000106 }
107
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000108 /// @brief clears the specified virtual register's, physical
109 /// register mapping
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000110 void clearVirt(unsigned virtReg) {
111 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000112 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000113 "attempt to clear a not assigned virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +0000114 Virt2PhysMap[virtReg] = NO_PHYS_REG;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000115 }
116
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000117 /// @brief clears all virtual to physical register mappings
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000118 void clearAllVirt() {
Chris Lattner7f690e62004-09-30 02:15:18 +0000119 Virt2PhysMap.clear();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000120 grow();
121 }
122
Evan Cheng549f27d32007-08-13 23:45:17 +0000123 /// @brief returns true is the specified virtual register is not
124 /// mapped to a stack slot or rematerialized.
125 bool isAssignedReg(unsigned virtReg) const {
126 return getStackSlot(virtReg) == NO_STACK_SLOT &&
127 getReMatId(virtReg) == NO_STACK_SLOT;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000128 }
129
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000130 /// @brief returns the stack slot mapped to the specified virtual
131 /// register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000132 int getStackSlot(unsigned virtReg) const {
133 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000134 return Virt2StackSlotMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000135 }
136
Evan Cheng549f27d32007-08-13 23:45:17 +0000137 /// @brief returns the rematerialization id mapped to the specified virtual
138 /// register
139 int getReMatId(unsigned virtReg) const {
140 assert(MRegisterInfo::isVirtualRegister(virtReg));
141 return Virt2ReMatIdMap[virtReg];
142 }
143
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000144 /// @brief create a mapping for the specifed virtual register to
145 /// the next available stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000146 int assignVirt2StackSlot(unsigned virtReg);
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000147 /// @brief create a mapping for the specified virtual register to
148 /// the specified stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000149 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
150
Evan Cheng2638e1a2007-03-20 08:13:50 +0000151 /// @brief assign an unique re-materialization id to the specified
152 /// virtual register.
153 int assignVirtReMatId(unsigned virtReg);
Evan Cheng549f27d32007-08-13 23:45:17 +0000154 /// @brief assign an unique re-materialization id to the specified
155 /// virtual register.
156 void assignVirtReMatId(unsigned virtReg, int id);
Evan Cheng2638e1a2007-03-20 08:13:50 +0000157
158 /// @brief returns true if the specified virtual register is being
159 /// re-materialized.
160 bool isReMaterialized(unsigned virtReg) const {
Evan Cheng549f27d32007-08-13 23:45:17 +0000161 return ReMatMap[virtReg] != NULL;
Evan Cheng2638e1a2007-03-20 08:13:50 +0000162 }
163
164 /// @brief returns the original machine instruction being re-issued
165 /// to re-materialize the specified virtual register.
Evan Cheng549f27d32007-08-13 23:45:17 +0000166 MachineInstr *getReMaterializedMI(unsigned virtReg) const {
Evan Cheng2638e1a2007-03-20 08:13:50 +0000167 return ReMatMap[virtReg];
168 }
169
170 /// @brief records the specified virtual register will be
171 /// re-materialized and the original instruction which will be re-issed
Evan Cheng549f27d32007-08-13 23:45:17 +0000172 /// for this purpose. If parameter all is true, then all uses of the
173 /// registers are rematerialized and it's safe to delete the definition.
Evan Cheng2638e1a2007-03-20 08:13:50 +0000174 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
175 ReMatMap[virtReg] = def;
176 }
177
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000178 /// @brief Updates information about the specified virtual register's value
179 /// folded into newMI machine instruction. The OpNum argument indicates the
180 /// operand number of OldMI that is folded.
181 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, unsigned OpNum,
Chris Lattner35f27052006-05-01 21:16:03 +0000182 MachineInstr *NewMI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000183
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000184 /// @brief returns the virtual registers' values folded in memory
185 /// operands of this instruction
Chris Lattner7f690e62004-09-30 02:15:18 +0000186 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000187 getFoldedVirts(MachineInstr* MI) const {
Chris Lattner7f690e62004-09-30 02:15:18 +0000188 return MI2VirtMap.equal_range(MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000189 }
Chris Lattner35f27052006-05-01 21:16:03 +0000190
Chris Lattner229924a2006-05-01 22:03:24 +0000191 /// RemoveFromFoldedVirtMap - If the specified machine instruction is in
192 /// the folded instruction map, remove its entry from the map.
Chris Lattner35f27052006-05-01 21:16:03 +0000193 void RemoveFromFoldedVirtMap(MachineInstr *MI) {
Chris Lattner229924a2006-05-01 22:03:24 +0000194 MI2VirtMap.erase(MI);
Chris Lattner35f27052006-05-01 21:16:03 +0000195 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000196
197 void print(std::ostream &OS) const;
Bill Wendling5c7e3262006-12-17 05:15:13 +0000198 void print(std::ostream *OS) const { if (OS) print(*OS); }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000199 void dump() const;
200 };
201
Bill Wendling5c7e3262006-12-17 05:15:13 +0000202 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
203 VRM.print(OS);
204 return OS;
205 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000206 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
207 VRM.print(OS);
208 return OS;
209 }
210
211 /// Spiller interface: Implementations of this interface assign spilled
212 /// virtual registers to stack slots, rewriting the code.
213 struct Spiller {
214 virtual ~Spiller();
215 virtual bool runOnMachineFunction(MachineFunction &MF,
Chris Lattner35f27052006-05-01 21:16:03 +0000216 VirtRegMap &VRM) = 0;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000217 };
218
219 /// createSpiller - Create an return a spiller object, as specified on the
220 /// command line.
221 Spiller* createSpiller();
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000222
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000223} // End llvm namespace
224
225#endif