blob: 5b06c01bfdf7c99ba0c7716b603b48a3ea2853d3 [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"
Chris Lattner1003dc72007-02-01 05:32:05 +000021#include "llvm/ADT/IndexedMap.h"
Bill Wendlingf3baad32006-12-07 01:30:32 +000022#include "llvm/Support/Streams.h"
Alkis Evlogimenosb76d2342004-03-01 20:05:10 +000023#include <map>
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000024
25namespace llvm {
Chris Lattnere2b77d52004-09-30 01:54:45 +000026 class MachineInstr;
Chris Lattner13a5dcd2006-09-05 02:12:02 +000027 class TargetInstrInfo;
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000028
Chris Lattnere2b77d52004-09-30 01:54:45 +000029 class VirtRegMap {
30 public:
Chris Lattner4dee67c2006-05-01 21:16:03 +000031 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
Chris Lattner1905ae62004-10-01 23:15:36 +000032 typedef std::multimap<MachineInstr*,
33 std::pair<unsigned, ModRef> > MI2VirtMapTy;
Alkis Evlogimenosb76d2342004-03-01 20:05:10 +000034
Chris Lattnere2b77d52004-09-30 01:54:45 +000035 private:
Chris Lattner13a5dcd2006-09-05 02:12:02 +000036 const TargetInstrInfo &TII;
37
Chris Lattner39fef8d2004-09-30 02:15:18 +000038 MachineFunction &MF;
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000039 /// Virt2PhysMap - This is a virtual to physical register
40 /// mapping. Each virtual register is required to have an entry in
41 /// it; even spilled virtual registers (the register mapped to a
42 /// spilled register is the temporary used to load it from the
43 /// stack).
Chris Lattner1003dc72007-02-01 05:32:05 +000044 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000045 /// Virt2StackSlotMap - This is virtual register to stack slot
46 /// mapping. Each spilled virtual register has an entry in it
47 /// which corresponds to the stack slot this register is spilled
48 /// at.
Chris Lattner1003dc72007-02-01 05:32:05 +000049 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000050 /// MI2VirtMap - This is MachineInstr to virtual register
51 /// mapping. In the case of memory spill code being folded into
52 /// instructions, we need to know which virtual register was
53 /// read/written by this instruction.
Chris Lattner39fef8d2004-09-30 02:15:18 +000054 MI2VirtMapTy MI2VirtMap;
Misha Brukman835702a2005-04-21 22:36:52 +000055
Chris Lattnere2b77d52004-09-30 01:54:45 +000056 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
57 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
Alkis Evlogimenosab77b052004-02-23 23:47:10 +000058
Chris Lattnere2b77d52004-09-30 01:54:45 +000059 enum {
60 NO_PHYS_REG = 0,
61 NO_STACK_SLOT = ~0 >> 1
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000062 };
63
Chris Lattnere2b77d52004-09-30 01:54:45 +000064 public:
Chris Lattner13a5dcd2006-09-05 02:12:02 +000065 VirtRegMap(MachineFunction &mf);
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000066
Chris Lattnere2b77d52004-09-30 01:54:45 +000067 void grow();
Alkis Evlogimenos31953c72004-03-01 23:18:15 +000068
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000069 /// @brief returns true if the specified virtual register is
70 /// mapped to a physical register
Chris Lattnere2b77d52004-09-30 01:54:45 +000071 bool hasPhys(unsigned virtReg) const {
72 return getPhys(virtReg) != NO_PHYS_REG;
73 }
Alkis Evlogimenos31953c72004-03-01 23:18:15 +000074
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000075 /// @brief returns the physical register mapped to the specified
76 /// virtual register
Chris Lattnere2b77d52004-09-30 01:54:45 +000077 unsigned getPhys(unsigned virtReg) const {
78 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +000079 return Virt2PhysMap[virtReg];
Chris Lattnere2b77d52004-09-30 01:54:45 +000080 }
Alkis Evlogimenos31953c72004-03-01 23:18:15 +000081
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000082 /// @brief creates a mapping for the specified virtual register to
83 /// the specified physical register
Chris Lattnere2b77d52004-09-30 01:54:45 +000084 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
85 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
86 MRegisterInfo::isPhysicalRegister(physReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +000087 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
Chris Lattnere2b77d52004-09-30 01:54:45 +000088 "attempt to assign physical register to already mapped "
89 "virtual register");
Chris Lattner39fef8d2004-09-30 02:15:18 +000090 Virt2PhysMap[virtReg] = physReg;
Chris Lattnere2b77d52004-09-30 01:54:45 +000091 }
92
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000093 /// @brief clears the specified virtual register's, physical
94 /// register mapping
Chris Lattnere2b77d52004-09-30 01:54:45 +000095 void clearVirt(unsigned virtReg) {
96 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +000097 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
Chris Lattnere2b77d52004-09-30 01:54:45 +000098 "attempt to clear a not assigned virtual register");
Chris Lattner39fef8d2004-09-30 02:15:18 +000099 Virt2PhysMap[virtReg] = NO_PHYS_REG;
Chris Lattnere2b77d52004-09-30 01:54:45 +0000100 }
101
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000102 /// @brief clears all virtual to physical register mappings
Chris Lattnere2b77d52004-09-30 01:54:45 +0000103 void clearAllVirt() {
Chris Lattner39fef8d2004-09-30 02:15:18 +0000104 Virt2PhysMap.clear();
Chris Lattnere2b77d52004-09-30 01:54:45 +0000105 grow();
106 }
107
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000108 /// @brief returns true is the specified virtual register is
109 /// mapped to a stack slot
Chris Lattnere2b77d52004-09-30 01:54:45 +0000110 bool hasStackSlot(unsigned virtReg) const {
111 return getStackSlot(virtReg) != NO_STACK_SLOT;
112 }
113
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000114 /// @brief returns the stack slot mapped to the specified virtual
115 /// register
Chris Lattnere2b77d52004-09-30 01:54:45 +0000116 int getStackSlot(unsigned virtReg) const {
117 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +0000118 return Virt2StackSlotMap[virtReg];
Chris Lattnere2b77d52004-09-30 01:54:45 +0000119 }
120
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000121 /// @brief create a mapping for the specifed virtual register to
122 /// the next available stack slot
Chris Lattnere2b77d52004-09-30 01:54:45 +0000123 int assignVirt2StackSlot(unsigned virtReg);
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000124 /// @brief create a mapping for the specified virtual register to
125 /// the specified stack slot
Chris Lattnere2b77d52004-09-30 01:54:45 +0000126 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
127
Chris Lattner1905ae62004-10-01 23:15:36 +0000128 /// @brief Updates information about the specified virtual register's value
129 /// folded into newMI machine instruction. The OpNum argument indicates the
130 /// operand number of OldMI that is folded.
131 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, unsigned OpNum,
Chris Lattner4dee67c2006-05-01 21:16:03 +0000132 MachineInstr *NewMI);
Chris Lattnere2b77d52004-09-30 01:54:45 +0000133
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000134 /// @brief returns the virtual registers' values folded in memory
135 /// operands of this instruction
Chris Lattner39fef8d2004-09-30 02:15:18 +0000136 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
Chris Lattnere2b77d52004-09-30 01:54:45 +0000137 getFoldedVirts(MachineInstr* MI) const {
Chris Lattner39fef8d2004-09-30 02:15:18 +0000138 return MI2VirtMap.equal_range(MI);
Chris Lattnere2b77d52004-09-30 01:54:45 +0000139 }
Chris Lattner4dee67c2006-05-01 21:16:03 +0000140
Chris Lattnerfd0a5472006-05-01 22:03:24 +0000141 /// RemoveFromFoldedVirtMap - If the specified machine instruction is in
142 /// the folded instruction map, remove its entry from the map.
Chris Lattner4dee67c2006-05-01 21:16:03 +0000143 void RemoveFromFoldedVirtMap(MachineInstr *MI) {
Chris Lattnerfd0a5472006-05-01 22:03:24 +0000144 MI2VirtMap.erase(MI);
Chris Lattner4dee67c2006-05-01 21:16:03 +0000145 }
Chris Lattnere2b77d52004-09-30 01:54:45 +0000146
147 void print(std::ostream &OS) const;
Bill Wendlinga77f1422006-12-17 05:15:13 +0000148 void print(std::ostream *OS) const { if (OS) print(*OS); }
Chris Lattnere2b77d52004-09-30 01:54:45 +0000149 void dump() const;
150 };
151
Bill Wendlinga77f1422006-12-17 05:15:13 +0000152 inline std::ostream *operator<<(std::ostream *OS, const VirtRegMap &VRM) {
153 VRM.print(OS);
154 return OS;
155 }
Chris Lattnere2b77d52004-09-30 01:54:45 +0000156 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
157 VRM.print(OS);
158 return OS;
159 }
160
161 /// Spiller interface: Implementations of this interface assign spilled
162 /// virtual registers to stack slots, rewriting the code.
163 struct Spiller {
164 virtual ~Spiller();
165 virtual bool runOnMachineFunction(MachineFunction &MF,
Chris Lattner4dee67c2006-05-01 21:16:03 +0000166 VirtRegMap &VRM) = 0;
Chris Lattnere2b77d52004-09-30 01:54:45 +0000167 };
168
169 /// createSpiller - Create an return a spiller object, as specified on the
170 /// command line.
171 Spiller* createSpiller();
Alkis Evlogimenos1dd872c2004-02-24 08:58:30 +0000172
Alkis Evlogimenosc794a902004-02-23 23:08:11 +0000173} // End llvm namespace
174
175#endif