blob: b7cbe51cea3217504b37ebe649f4542619130af0 [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;
Chris Lattner29268692006-09-05 02:12:02 +000028 class TargetInstrInfo;
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000029
Chris Lattner8c4d88d2004-09-30 01:54:45 +000030 class VirtRegMap {
31 public:
Evan Cheng2638e1a2007-03-20 08:13:50 +000032 enum {
33 NO_PHYS_REG = 0,
Evan Cheng91935142007-04-04 07:40:01 +000034 NO_STACK_SLOT = (1L << 30)-1,
35 MAX_STACK_SLOT = (1L << 18)-1
Evan Cheng2638e1a2007-03-20 08:13:50 +000036 };
37
Chris Lattner35f27052006-05-01 21:16:03 +000038 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000039 typedef std::multimap<MachineInstr*,
40 std::pair<unsigned, ModRef> > MI2VirtMapTy;
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000041
Chris Lattner8c4d88d2004-09-30 01:54:45 +000042 private:
Chris Lattner29268692006-09-05 02:12:02 +000043 const TargetInstrInfo &TII;
44
Chris Lattner7f690e62004-09-30 02:15:18 +000045 MachineFunction &MF;
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000046 /// Virt2PhysMap - This is a virtual to physical register
47 /// mapping. Each virtual register is required to have an entry in
48 /// it; even spilled virtual registers (the register mapped to a
49 /// spilled register is the temporary used to load it from the
50 /// stack).
Chris Lattner94c002a2007-02-01 05:32:05 +000051 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000052 /// Virt2StackSlotMap - This is virtual register to stack slot
53 /// mapping. Each spilled virtual register has an entry in it
54 /// which corresponds to the stack slot this register is spilled
55 /// at.
Chris Lattner94c002a2007-02-01 05:32:05 +000056 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000057 /// MI2VirtMap - This is MachineInstr to virtual register
58 /// mapping. In the case of memory spill code being folded into
59 /// instructions, we need to know which virtual register was
60 /// read/written by this instruction.
Chris Lattner7f690e62004-09-30 02:15:18 +000061 MI2VirtMapTy MI2VirtMap;
Misha Brukmanedf128a2005-04-21 22:36:52 +000062
Evan Cheng91935142007-04-04 07:40:01 +000063 /// ReMatMap - This is virtual register to re-materialized instruction
Evan Cheng2638e1a2007-03-20 08:13:50 +000064 /// mapping. Each virtual register whose definition is going to be
65 /// re-materialized has an entry in it.
66 std::map<unsigned, const MachineInstr*> ReMatMap;
67
68 /// ReMatId - Instead of assigning a stack slot to a to be rematerialized
Evan Cheng91935142007-04-04 07:40:01 +000069 /// virtual register, an unique id is being assigned. This keeps track of
Evan Cheng2638e1a2007-03-20 08:13:50 +000070 /// the highest id used so far. Note, this starts at (1<<18) to avoid
71 /// conflicts with stack slot numbers.
72 int ReMatId;
73
Chris Lattner8c4d88d2004-09-30 01:54:45 +000074 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
75 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
Alkis Evlogimenos79742872004-02-23 23:47:10 +000076
Chris Lattner8c4d88d2004-09-30 01:54:45 +000077 public:
Chris Lattner29268692006-09-05 02:12:02 +000078 VirtRegMap(MachineFunction &mf);
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000079
Chris Lattner8c4d88d2004-09-30 01:54:45 +000080 void grow();
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +000081
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000082 /// @brief returns true if the specified virtual register is
83 /// mapped to a physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +000084 bool hasPhys(unsigned virtReg) const {
85 return getPhys(virtReg) != NO_PHYS_REG;
86 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +000087
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000088 /// @brief returns the physical register mapped to the specified
89 /// virtual register
Chris Lattner8c4d88d2004-09-30 01:54:45 +000090 unsigned getPhys(unsigned virtReg) const {
91 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000092 return Virt2PhysMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +000093 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +000094
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000095 /// @brief creates a mapping for the specified virtual register to
96 /// the specified physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +000097 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
98 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
99 MRegisterInfo::isPhysicalRegister(physReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000100 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000101 "attempt to assign physical register to already mapped "
102 "virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +0000103 Virt2PhysMap[virtReg] = physReg;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000104 }
105
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000106 /// @brief clears the specified virtual register's, physical
107 /// register mapping
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000108 void clearVirt(unsigned virtReg) {
109 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000110 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000111 "attempt to clear a not assigned virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +0000112 Virt2PhysMap[virtReg] = NO_PHYS_REG;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000113 }
114
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000115 /// @brief clears all virtual to physical register mappings
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000116 void clearAllVirt() {
Chris Lattner7f690e62004-09-30 02:15:18 +0000117 Virt2PhysMap.clear();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000118 grow();
119 }
120
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000121 /// @brief returns true is the specified virtual register is
122 /// mapped to a stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000123 bool hasStackSlot(unsigned virtReg) const {
124 return getStackSlot(virtReg) != NO_STACK_SLOT;
125 }
126
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000127 /// @brief returns the stack slot mapped to the specified virtual
128 /// register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000129 int getStackSlot(unsigned virtReg) const {
130 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000131 return Virt2StackSlotMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000132 }
133
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000134 /// @brief create a mapping for the specifed virtual register to
135 /// the next available stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000136 int assignVirt2StackSlot(unsigned virtReg);
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000137 /// @brief create a mapping for the specified virtual register to
138 /// the specified stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000139 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
140
Evan Cheng2638e1a2007-03-20 08:13:50 +0000141 /// @brief assign an unique re-materialization id to the specified
142 /// virtual register.
143 int assignVirtReMatId(unsigned virtReg);
144
145 /// @brief returns true if the specified virtual register is being
146 /// re-materialized.
147 bool isReMaterialized(unsigned virtReg) const {
148 return ReMatMap.count(virtReg) != 0;
149 }
150
151 /// @brief returns the original machine instruction being re-issued
152 /// to re-materialize the specified virtual register.
153 const MachineInstr *getReMaterializedMI(unsigned virtReg) {
154 return ReMatMap[virtReg];
155 }
156
157 /// @brief records the specified virtual register will be
158 /// re-materialized and the original instruction which will be re-issed
159 /// for this purpose.
160 void setVirtIsReMaterialized(unsigned virtReg, MachineInstr *def) {
161 ReMatMap[virtReg] = def;
162 }
163
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000164 /// @brief Updates information about the specified virtual register's value
165 /// folded into newMI machine instruction. The OpNum argument indicates the
166 /// operand number of OldMI that is folded.
167 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, unsigned OpNum,
Chris Lattner35f27052006-05-01 21:16:03 +0000168 MachineInstr *NewMI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000169
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000170 /// @brief returns the virtual registers' values folded in memory
171 /// operands of this instruction
Chris Lattner7f690e62004-09-30 02:15:18 +0000172 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000173 getFoldedVirts(MachineInstr* MI) const {
Chris Lattner7f690e62004-09-30 02:15:18 +0000174 return MI2VirtMap.equal_range(MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000175 }
Chris Lattner35f27052006-05-01 21:16:03 +0000176
Chris Lattner229924a2006-05-01 22:03:24 +0000177 /// RemoveFromFoldedVirtMap - If the specified machine instruction is in
178 /// the folded instruction map, remove its entry from the map.
Chris Lattner35f27052006-05-01 21:16:03 +0000179 void RemoveFromFoldedVirtMap(MachineInstr *MI) {
Chris Lattner229924a2006-05-01 22:03:24 +0000180 MI2VirtMap.erase(MI);
Chris Lattner35f27052006-05-01 21:16:03 +0000181 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000182
183 void print(std::ostream &OS) const;
Bill Wendling5c7e3262006-12-17 05:15:13 +0000184 void print(std::ostream *OS) const { if (OS) print(*OS); }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000185 void dump() const;
186 };
187
Bill Wendling5c7e3262006-12-17 05:15:13 +0000188 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
189 VRM.print(OS);
190 return OS;
191 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000192 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
193 VRM.print(OS);
194 return OS;
195 }
196
197 /// Spiller interface: Implementations of this interface assign spilled
198 /// virtual registers to stack slots, rewriting the code.
199 struct Spiller {
200 virtual ~Spiller();
201 virtual bool runOnMachineFunction(MachineFunction &MF,
Chris Lattner35f27052006-05-01 21:16:03 +0000202 VirtRegMap &VRM) = 0;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000203 };
204
205 /// createSpiller - Create an return a spiller object, as specified on the
206 /// command line.
207 Spiller* createSpiller();
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000208
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000209} // End llvm namespace
210
211#endif