blob: 426d1cf9b026246e039f3cd0407c363334c139a7 [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"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/DenseMap.h"
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000022#include <map>
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000023
24namespace llvm {
Chris Lattner8c4d88d2004-09-30 01:54:45 +000025 class MachineInstr;
Chris Lattner29268692006-09-05 02:12:02 +000026 class TargetInstrInfo;
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000027
Chris Lattner8c4d88d2004-09-30 01:54:45 +000028 class VirtRegMap {
29 public:
Chris Lattner35f27052006-05-01 21:16:03 +000030 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000031 typedef std::multimap<MachineInstr*,
32 std::pair<unsigned, ModRef> > MI2VirtMapTy;
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000033
Chris Lattner8c4d88d2004-09-30 01:54:45 +000034 private:
Chris Lattner29268692006-09-05 02:12:02 +000035 const TargetInstrInfo &TII;
36
Chris Lattner7f690e62004-09-30 02:15:18 +000037 MachineFunction &MF;
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000038 /// Virt2PhysMap - This is a virtual to physical register
39 /// mapping. Each virtual register is required to have an entry in
40 /// it; even spilled virtual registers (the register mapped to a
41 /// spilled register is the temporary used to load it from the
42 /// stack).
Chris Lattner7f690e62004-09-30 02:15:18 +000043 DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000044 /// Virt2StackSlotMap - This is virtual register to stack slot
45 /// mapping. Each spilled virtual register has an entry in it
46 /// which corresponds to the stack slot this register is spilled
47 /// at.
Chris Lattner7f690e62004-09-30 02:15:18 +000048 DenseMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000049 /// MI2VirtMap - This is MachineInstr to virtual register
50 /// mapping. In the case of memory spill code being folded into
51 /// instructions, we need to know which virtual register was
52 /// read/written by this instruction.
Chris Lattner7f690e62004-09-30 02:15:18 +000053 MI2VirtMapTy MI2VirtMap;
Misha Brukmanedf128a2005-04-21 22:36:52 +000054
Chris Lattner8c4d88d2004-09-30 01:54:45 +000055 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
56 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
Alkis Evlogimenos79742872004-02-23 23:47:10 +000057
Chris Lattner8c4d88d2004-09-30 01:54:45 +000058 enum {
59 NO_PHYS_REG = 0,
60 NO_STACK_SLOT = ~0 >> 1
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000061 };
62
Chris Lattner8c4d88d2004-09-30 01:54:45 +000063 public:
Chris Lattner29268692006-09-05 02:12:02 +000064 VirtRegMap(MachineFunction &mf);
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000065
Chris Lattner8c4d88d2004-09-30 01:54:45 +000066 void grow();
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +000067
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000068 /// @brief returns true if the specified virtual register is
69 /// mapped to a physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +000070 bool hasPhys(unsigned virtReg) const {
71 return getPhys(virtReg) != NO_PHYS_REG;
72 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +000073
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000074 /// @brief returns the physical register mapped to the specified
75 /// virtual register
Chris Lattner8c4d88d2004-09-30 01:54:45 +000076 unsigned getPhys(unsigned virtReg) const {
77 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000078 return Virt2PhysMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +000079 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +000080
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000081 /// @brief creates a mapping for the specified virtual register to
82 /// the specified physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +000083 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
84 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
85 MRegisterInfo::isPhysicalRegister(physReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000086 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +000087 "attempt to assign physical register to already mapped "
88 "virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +000089 Virt2PhysMap[virtReg] = physReg;
Chris Lattner8c4d88d2004-09-30 01:54:45 +000090 }
91
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000092 /// @brief clears the specified virtual register's, physical
93 /// register mapping
Chris Lattner8c4d88d2004-09-30 01:54:45 +000094 void clearVirt(unsigned virtReg) {
95 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +000096 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +000097 "attempt to clear a not assigned virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +000098 Virt2PhysMap[virtReg] = NO_PHYS_REG;
Chris Lattner8c4d88d2004-09-30 01:54:45 +000099 }
100
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000101 /// @brief clears all virtual to physical register mappings
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000102 void clearAllVirt() {
Chris Lattner7f690e62004-09-30 02:15:18 +0000103 Virt2PhysMap.clear();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000104 grow();
105 }
106
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000107 /// @brief returns true is the specified virtual register is
108 /// mapped to a stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000109 bool hasStackSlot(unsigned virtReg) const {
110 return getStackSlot(virtReg) != NO_STACK_SLOT;
111 }
112
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000113 /// @brief returns the stack slot mapped to the specified virtual
114 /// register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000115 int getStackSlot(unsigned virtReg) const {
116 assert(MRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000117 return Virt2StackSlotMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000118 }
119
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000120 /// @brief create a mapping for the specifed virtual register to
121 /// the next available stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000122 int assignVirt2StackSlot(unsigned virtReg);
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000123 /// @brief create a mapping for the specified virtual register to
124 /// the specified stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000125 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
126
Chris Lattnerbec6a9e2004-10-01 23:15:36 +0000127 /// @brief Updates information about the specified virtual register's value
128 /// folded into newMI machine instruction. The OpNum argument indicates the
129 /// operand number of OldMI that is folded.
130 void virtFolded(unsigned VirtReg, MachineInstr *OldMI, unsigned OpNum,
Chris Lattner35f27052006-05-01 21:16:03 +0000131 MachineInstr *NewMI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000132
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000133 /// @brief returns the virtual registers' values folded in memory
134 /// operands of this instruction
Chris Lattner7f690e62004-09-30 02:15:18 +0000135 std::pair<MI2VirtMapTy::const_iterator, MI2VirtMapTy::const_iterator>
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000136 getFoldedVirts(MachineInstr* MI) const {
Chris Lattner7f690e62004-09-30 02:15:18 +0000137 return MI2VirtMap.equal_range(MI);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000138 }
Chris Lattner35f27052006-05-01 21:16:03 +0000139
Chris Lattner229924a2006-05-01 22:03:24 +0000140 /// RemoveFromFoldedVirtMap - If the specified machine instruction is in
141 /// the folded instruction map, remove its entry from the map.
Chris Lattner35f27052006-05-01 21:16:03 +0000142 void RemoveFromFoldedVirtMap(MachineInstr *MI) {
Chris Lattner229924a2006-05-01 22:03:24 +0000143 MI2VirtMap.erase(MI);
Chris Lattner35f27052006-05-01 21:16:03 +0000144 }
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000145
146 void print(std::ostream &OS) const;
147 void dump() const;
148 };
149
150 inline std::ostream &operator<<(std::ostream &OS, const VirtRegMap &VRM) {
151 VRM.print(OS);
152 return OS;
153 }
154
155 /// Spiller interface: Implementations of this interface assign spilled
156 /// virtual registers to stack slots, rewriting the code.
157 struct Spiller {
158 virtual ~Spiller();
159 virtual bool runOnMachineFunction(MachineFunction &MF,
Chris Lattner35f27052006-05-01 21:16:03 +0000160 VirtRegMap &VRM) = 0;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000161 };
162
163 /// createSpiller - Create an return a spiller object, as specified on the
164 /// command line.
165 Spiller* createSpiller();
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000166
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000167} // End llvm namespace
168
169#endif