blob: c7792b0f1266d9d3c6b314116602e1ace365d947 [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"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/ADT/DenseMap.h"
Alkis Evlogimenosb76d2342004-03-01 20:05:10 +000022#include <map>
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000023
24namespace llvm {
Chris Lattnere2b77d52004-09-30 01:54:45 +000025 class MachineInstr;
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000026
Chris Lattnere2b77d52004-09-30 01:54:45 +000027 class VirtRegMap {
28 public:
Chris Lattner39fef8d2004-09-30 02:15:18 +000029 typedef std::multimap<MachineInstr*, unsigned> MI2VirtMapTy;
Alkis Evlogimenosb76d2342004-03-01 20:05:10 +000030
Chris Lattnere2b77d52004-09-30 01:54:45 +000031 private:
Chris Lattner39fef8d2004-09-30 02:15:18 +000032 MachineFunction &MF;
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000033 /// Virt2PhysMap - This is a virtual to physical register
34 /// mapping. Each virtual register is required to have an entry in
35 /// it; even spilled virtual registers (the register mapped to a
36 /// spilled register is the temporary used to load it from the
37 /// stack).
Chris Lattner39fef8d2004-09-30 02:15:18 +000038 DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000039 /// Virt2StackSlotMap - This is virtual register to stack slot
40 /// mapping. Each spilled virtual register has an entry in it
41 /// which corresponds to the stack slot this register is spilled
42 /// at.
Chris Lattner39fef8d2004-09-30 02:15:18 +000043 DenseMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000044 /// MI2VirtMap - This is MachineInstr to virtual register
45 /// mapping. In the case of memory spill code being folded into
46 /// instructions, we need to know which virtual register was
47 /// read/written by this instruction.
Chris Lattner39fef8d2004-09-30 02:15:18 +000048 MI2VirtMapTy MI2VirtMap;
Chris Lattnere2b77d52004-09-30 01:54:45 +000049
50 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
51 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
Alkis Evlogimenosab77b052004-02-23 23:47:10 +000052
Chris Lattnere2b77d52004-09-30 01:54:45 +000053 enum {
54 NO_PHYS_REG = 0,
55 NO_STACK_SLOT = ~0 >> 1
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000056 };
57
Chris Lattnere2b77d52004-09-30 01:54:45 +000058 public:
Chris Lattner39fef8d2004-09-30 02:15:18 +000059 VirtRegMap(MachineFunction &mf)
60 : MF(mf), Virt2PhysMap(NO_PHYS_REG), Virt2StackSlotMap(NO_STACK_SLOT) {
Chris Lattnere2b77d52004-09-30 01:54:45 +000061 grow();
62 }
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000063
Chris Lattnere2b77d52004-09-30 01:54:45 +000064 void grow();
Alkis Evlogimenos31953c72004-03-01 23:18:15 +000065
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000066 /// @brief returns true if the specified virtual register is
67 /// mapped to a physical register
Chris Lattnere2b77d52004-09-30 01:54:45 +000068 bool hasPhys(unsigned virtReg) const {
69 return getPhys(virtReg) != NO_PHYS_REG;
70 }
Alkis Evlogimenos31953c72004-03-01 23:18:15 +000071
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000072 /// @brief returns the physical register mapped to the specified
73 /// virtual register
Chris Lattnere2b77d52004-09-30 01:54:45 +000074 unsigned getPhys(unsigned virtReg) const {
75 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +000076 return Virt2PhysMap[virtReg];
Chris Lattnere2b77d52004-09-30 01:54:45 +000077 }
Alkis Evlogimenos31953c72004-03-01 23:18:15 +000078
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000079 /// @brief creates a mapping for the specified virtual register to
80 /// the specified physical register
Chris Lattnere2b77d52004-09-30 01:54:45 +000081 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
82 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
83 MRegisterInfo::isPhysicalRegister(physReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +000084 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
Chris Lattnere2b77d52004-09-30 01:54:45 +000085 "attempt to assign physical register to already mapped "
86 "virtual register");
Chris Lattner39fef8d2004-09-30 02:15:18 +000087 Virt2PhysMap[virtReg] = physReg;
Chris Lattnere2b77d52004-09-30 01:54:45 +000088 }
89
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000090 /// @brief clears the specified virtual register's, physical
91 /// register mapping
Chris Lattnere2b77d52004-09-30 01:54:45 +000092 void clearVirt(unsigned virtReg) {
93 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +000094 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
Chris Lattnere2b77d52004-09-30 01:54:45 +000095 "attempt to clear a not assigned virtual register");
Chris Lattner39fef8d2004-09-30 02:15:18 +000096 Virt2PhysMap[virtReg] = NO_PHYS_REG;
Chris Lattnere2b77d52004-09-30 01:54:45 +000097 }
98
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +000099 /// @brief clears all virtual to physical register mappings
Chris Lattnere2b77d52004-09-30 01:54:45 +0000100 void clearAllVirt() {
Chris Lattner39fef8d2004-09-30 02:15:18 +0000101 Virt2PhysMap.clear();
Chris Lattnere2b77d52004-09-30 01:54:45 +0000102 grow();
103 }
104
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000105 /// @brief returns true is the specified virtual register is
106 /// mapped to a stack slot
Chris Lattnere2b77d52004-09-30 01:54:45 +0000107 bool hasStackSlot(unsigned virtReg) const {
108 return getStackSlot(virtReg) != NO_STACK_SLOT;
109 }
110
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000111 /// @brief returns the stack slot mapped to the specified virtual
112 /// register
Chris Lattnere2b77d52004-09-30 01:54:45 +0000113 int getStackSlot(unsigned virtReg) const {
114 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner39fef8d2004-09-30 02:15:18 +0000115 return Virt2StackSlotMap[virtReg];
Chris Lattnere2b77d52004-09-30 01:54:45 +0000116 }
117
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000118 /// @brief create a mapping for the specifed virtual register to
119 /// the next available stack slot
Chris Lattnere2b77d52004-09-30 01:54:45 +0000120 int assignVirt2StackSlot(unsigned virtReg);
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000121 /// @brief create a mapping for the specified virtual register to
122 /// the specified stack slot
Chris Lattnere2b77d52004-09-30 01:54:45 +0000123 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
124
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000125 /// @brief updates information about the specified virtual
126 /// register's value folded into newMI machine instruction
Chris Lattnere2b77d52004-09-30 01:54:45 +0000127 void virtFolded(unsigned virtReg, MachineInstr* oldMI,
128 MachineInstr* newMI);
129
Alkis Evlogimenoscc37da12004-10-01 00:35:07 +0000130 /// @brief returns the virtual registers' values folded in memory
131 /// operands of this instruction
Chris Lattner39fef8d2004-09-30 02:15:18 +0000132 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
Chris Lattnere2b77d52004-09-30 01:54:45 +0000133 getFoldedVirts(MachineInstr* MI) const {
Chris Lattner39fef8d2004-09-30 02:15:18 +0000134 return MI2VirtMap.equal_range(MI);
Chris Lattnere2b77d52004-09-30 01:54:45 +0000135 }
136
137 void print(std::ostream &OS) const;
138 void dump() const;
139 };
140
141 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
142 VRM.print(OS);
143 return OS;
144 }
145
146 /// Spiller interface: Implementations of this interface assign spilled
147 /// virtual registers to stack slots, rewriting the code.
148 struct Spiller {
149 virtual ~Spiller();
150 virtual bool runOnMachineFunction(MachineFunction &MF,
151 const VirtRegMap &VRM) = 0;
152 };
153
154 /// createSpiller - Create an return a spiller object, as specified on the
155 /// command line.
156 Spiller* createSpiller();
Alkis Evlogimenos1dd872c2004-02-24 08:58:30 +0000157
Alkis Evlogimenosc794a902004-02-23 23:08:11 +0000158} // End llvm namespace
159
160#endif