blob: da20d4ae114b6a25c2cb1f49faa8b3453268a45b [file] [log] [blame]
Alkis Evlogimenosc794a902004-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 Lattnere2b77d52004-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 Evlogimenosc794a902004-02-23 23:08:11 +000014//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_CODEGEN_VIRTREGMAP_H
18#define LLVM_CODEGEN_VIRTREGMAP_H
19
Chris Lattnere2b77d52004-09-30 01:54:45 +000020#include "llvm/Target/MRegisterInfo.h"
Evan Cheng0e3278e2007-03-20 08:13:50 +000021#include "llvm/ADT/BitVector.h"
Chris Lattner1003dc72007-02-01 05:32:05 +000022#include "llvm/ADT/IndexedMap.h"
Bill Wendlingf3baad32006-12-07 01:30:32 +000023#include "llvm/Support/Streams.h"
Alkis Evlogimenosb76d2342004-03-01 20:05:10 +000024#include <map>
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000025
26namespace llvm {
Chris Lattnere2b77d52004-09-30 01:54:45 +000027 class MachineInstr;
David Greene99905f12007-08-07 16:34:05 +000028 class MachineFunction;
Chris Lattner13a5dcd2006-09-05 02:12:02 +000029 class TargetInstrInfo;
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000030
Chris Lattnere2b77d52004-09-30 01:54:45 +000031 class VirtRegMap {
32 public:
Evan Cheng0e3278e2007-03-20 08:13:50 +000033 enum {
34 NO_PHYS_REG = 0,
Evan Cheng8be98c12007-04-04 07:40:01 +000035 NO_STACK_SLOT = (1L << 30)-1,
36 MAX_STACK_SLOT = (1L << 18)-1
Evan Cheng0e3278e2007-03-20 08:13:50 +000037 };
38
Chris Lattner4dee67c2006-05-01 21:16:03 +000039 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
Chris Lattner1905ae62004-10-01 23:15:36 +000040 typedef std::multimap<MachineInstr*,
41 std::pair<unsigned, ModRef> > MI2VirtMapTy;
Alkis Evlogimenosb76d2342004-03-01 20:05:10 +000042
Chris Lattnere2b77d52004-09-30 01:54:45 +000043 private:
Chris Lattner13a5dcd2006-09-05 02:12:02 +000044 const TargetInstrInfo &TII;
45
Chris Lattner39fef8d2004-09-30 02:15:18 +000046 MachineFunction &MF;
Alkis Evlogimenoscc37da12004-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 Lattner1003dc72007-02-01 05:32:05 +000052 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
Alkis Evlogimenoscc37da12004-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 Lattner1003dc72007-02-01 05:32:05 +000057 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000058 /// MI2VirtMap - This is MachineInstr to virtual register
59 /// mapping. In the case of memory spill code being folded into
60 /// instructions, we need to know which virtual register was
61 /// read/written by this instruction.
Chris Lattner39fef8d2004-09-30 02:15:18 +000062 MI2VirtMapTy MI2VirtMap;
Misha Brukman835702a2005-04-21 22:36:52 +000063
Evan Cheng8be98c12007-04-04 07:40:01 +000064 /// ReMatMap - This is virtual register to re-materialized instruction
Evan Cheng0e3278e2007-03-20 08:13:50 +000065 /// mapping. Each virtual register whose definition is going to be
66 /// re-materialized has an entry in it.
67 std::map<unsigned, const MachineInstr*> ReMatMap;
68
69 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
Evan Cheng8be98c12007-04-04 07:40:01 +000070 /// virtual register, an unique id is being assigned. This keeps track of
Evan Cheng0e3278e2007-03-20 08:13:50 +000071 /// the highest id used so far. Note, this starts at (1<<18) to avoid
72 /// conflicts with stack slot numbers.
73 int ReMatId;
74
Chris Lattnere2b77d52004-09-30 01:54:45 +000075 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
76 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
Alkis Evlogimenosab77b052004-02-23 23:47:10 +000077
Chris Lattnere2b77d52004-09-30 01:54:45 +000078 public:
Dan Gohman5f6a9da52007-08-02 21:21:54 +000079 explicit VirtRegMap(MachineFunction &mf);
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000080
Chris Lattnere2b77d52004-09-30 01:54:45 +000081 void grow();
Alkis Evlogimenos31953c72004-03-01 23:18:15 +000082
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000083 /// @brief returns true if the specified virtual register is
84 /// mapped to a physical register
Chris Lattnere2b77d52004-09-30 01:54:45 +000085 bool hasPhys(unsigned virtReg) const {
86 return getPhys(virtReg) != NO_PHYS_REG;
87 }
Alkis Evlogimenos31953c72004-03-01 23:18:15 +000088
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000089 /// @brief returns the physical register mapped to the specified
90 /// virtual register
Chris Lattnere2b77d52004-09-30 01:54:45 +000091 unsigned getPhys(unsigned virtReg) const {
92 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +000093 return Virt2PhysMap[virtReg];
Chris Lattnere2b77d52004-09-30 01:54:45 +000094 }
Alkis Evlogimenos31953c72004-03-01 23:18:15 +000095
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000096 /// @brief creates a mapping for the specified virtual register to
97 /// the specified physical register
Chris Lattnere2b77d52004-09-30 01:54:45 +000098 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
99 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
100 MRegisterInfo::isPhysicalRegister(physReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +0000101 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
Chris Lattnere2b77d52004-09-30 01:54:45 +0000102 "attempt to assign physical register to already mapped "
103 "virtual register");
Chris Lattner39fef8d2004-09-30 02:15:18 +0000104 Virt2PhysMap[virtReg] = physReg;
Chris Lattnere2b77d52004-09-30 01:54:45 +0000105 }
106
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000107 /// @brief clears the specified virtual register's, physical
108 /// register mapping
Chris Lattnere2b77d52004-09-30 01:54:45 +0000109 void clearVirt(unsigned virtReg) {
110 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +0000111 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
Chris Lattnere2b77d52004-09-30 01:54:45 +0000112 "attempt to clear a not assigned virtual register");
Chris Lattner39fef8d2004-09-30 02:15:18 +0000113 Virt2PhysMap[virtReg] = NO_PHYS_REG;
Chris Lattnere2b77d52004-09-30 01:54:45 +0000114 }
115
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000116 /// @brief clears all virtual to physical register mappings
Chris Lattnere2b77d52004-09-30 01:54:45 +0000117 void clearAllVirt() {
Chris Lattner39fef8d2004-09-30 02:15:18 +0000118 Virt2PhysMap.clear();
Chris Lattnere2b77d52004-09-30 01:54:45 +0000119 grow();
120 }
121
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000122 /// @brief returns true is the specified virtual register is
123 /// mapped to a stack slot
Chris Lattnere2b77d52004-09-30 01:54:45 +0000124 bool hasStackSlot(unsigned virtReg) const {
125 return getStackSlot(virtReg) != NO_STACK_SLOT;
126 }
127
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000128 /// @brief returns the stack slot mapped to the specified virtual
129 /// register
Chris Lattnere2b77d52004-09-30 01:54:45 +0000130 int getStackSlot(unsigned virtReg) const {
131 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +0000132 return Virt2StackSlotMap[virtReg];
Chris Lattnere2b77d52004-09-30 01:54:45 +0000133 }
134
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000135 /// @brief create a mapping for the specifed virtual register to
136 /// the next available stack slot
Chris Lattnere2b77d52004-09-30 01:54:45 +0000137 int assignVirt2StackSlot(unsigned virtReg);
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000138 /// @brief create a mapping for the specified virtual register to
139 /// the specified stack slot
Chris Lattnere2b77d52004-09-30 01:54:45 +0000140 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
141
Evan Cheng0e3278e2007-03-20 08:13:50 +0000142 /// @brief assign an unique re-materialization id to the specified
143 /// virtual register.
144 int assignVirtReMatId(unsigned virtReg);
145
146 /// @brief returns true if the specified virtual register is being
147 /// re-materialized.
148 bool isReMaterialized(unsigned virtReg) const {
149 return ReMatMap.count(virtReg) != 0;
150 }
151
152 /// @brief returns the original machine instruction being re-issued
153 /// to re-materialize the specified virtual register.
154 const MachineInstr *getReMaterializedMI(unsigned virtReg) {
155 return ReMatMap[virtReg];
156 }
157
158 /// @brief records the specified virtual register will be
159 /// re-materialized and the original instruction which will be re-issed
160 /// for this purpose.
161 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
162 ReMatMap[virtReg] = def;
163 }
164
Chris Lattner1905ae62004-10-01 23:15:36 +0000165 /// @brief Updates information about the specified virtual register's value
166 /// folded into newMI machine instruction. The OpNum argument indicates the
167 /// operand number of OldMI that is folded.
168 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, unsigned OpNum,
Chris Lattner4dee67c2006-05-01 21:16:03 +0000169 MachineInstr *NewMI);
Chris Lattnere2b77d52004-09-30 01:54:45 +0000170
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000171 /// @brief returns the virtual registers' values folded in memory
172 /// operands of this instruction
Chris Lattner39fef8d2004-09-30 02:15:18 +0000173 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
Chris Lattnere2b77d52004-09-30 01:54:45 +0000174 getFoldedVirts(MachineInstr* MI) const {
Chris Lattner39fef8d2004-09-30 02:15:18 +0000175 return MI2VirtMap.equal_range(MI);
Chris Lattnere2b77d52004-09-30 01:54:45 +0000176 }
Chris Lattner4dee67c2006-05-01 21:16:03 +0000177
Chris Lattnerfd0a5472006-05-01 22:03:24 +0000178 /// RemoveFromFoldedVirtMap - If the specified machine instruction is in
179 /// the folded instruction map, remove its entry from the map.
Chris Lattner4dee67c2006-05-01 21:16:03 +0000180 void RemoveFromFoldedVirtMap(MachineInstr *MI) {
Chris Lattnerfd0a5472006-05-01 22:03:24 +0000181 MI2VirtMap.erase(MI);
Chris Lattner4dee67c2006-05-01 21:16:03 +0000182 }
Chris Lattnere2b77d52004-09-30 01:54:45 +0000183
184 void print(std::ostream &OS) const;
Bill Wendlinga77f1422006-12-17 05:15:13 +0000185 void print(std::ostream *OS) const { if (OS) print(*OS); }
Chris Lattnere2b77d52004-09-30 01:54:45 +0000186 void dump() const;
187 };
188
Bill Wendlinga77f1422006-12-17 05:15:13 +0000189 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
190 VRM.print(OS);
191 return OS;
192 }
Chris Lattnere2b77d52004-09-30 01:54:45 +0000193 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
194 VRM.print(OS);
195 return OS;
196 }
197
198 /// Spiller interface: Implementations of this interface assign spilled
199 /// virtual registers to stack slots, rewriting the code.
200 struct Spiller {
201 virtual ~Spiller();
202 virtual bool runOnMachineFunction(MachineFunction &MF,
Chris Lattner4dee67c2006-05-01 21:16:03 +0000203 VirtRegMap &VRM) = 0;
Chris Lattnere2b77d52004-09-30 01:54:45 +0000204 };
205
206 /// createSpiller - Create an return a spiller object, as specified on the
207 /// command line.
208 Spiller* createSpiller();
Alkis Evlogimenos1dd872c2004-02-24 08:58:30 +0000209
Alkis Evlogimenosc794a902004-02-23 23:08:11 +0000210} // End llvm namespace
211
212#endif