blob: f6a1cecd67bfba91992a69cb60602a59d5b2e2c0 [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//
10// This file implements a virtual register map. This maps virtual
11// registers to physical registers and virtual registers to stack
12// slots. It is created and updated by a register allocator and then
13// used by a machine code rewriter that adds spill code and rewrites
14// virtual into physical register references.
15//
16//===----------------------------------------------------------------------===//
17
18#ifndef LLVM_CODEGEN_VIRTREGMAP_H
19#define LLVM_CODEGEN_VIRTREGMAP_H
20
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/SSARegMap.h"
23#include <climits>
24
25namespace llvm {
26
27 class VirtRegMap {
28 public:
29 typedef std::vector<unsigned> Virt2PhysMap;
30 typedef std::vector<int> Virt2StackSlotMap;
Alkis Evlogimenos79742872004-02-23 23:47:10 +000031
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000032 private:
33 MachineFunction* mf_;
34 Virt2PhysMap v2pMap_;
35 Virt2StackSlotMap v2ssMap_;
36
37 // do not implement
38 VirtRegMap(const VirtRegMap& rhs);
39 const VirtRegMap& operator=(const VirtRegMap& rhs);
40
41 static unsigned toIndex(unsigned virtReg) {
42 return virtReg - MRegisterInfo::FirstVirtualRegister;
43 }
44 static unsigned fromIndex(unsigned index) {
45 return index + MRegisterInfo::FirstVirtualRegister;
46 }
47
Alkis Evlogimenose8124b92004-02-23 23:49:40 +000048 enum {
49 NO_PHYS_REG = 0,
50 NO_STACK_SLOT = INT_MAX
51 };
52
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000053 public:
54 VirtRegMap(MachineFunction& mf)
55 : mf_(&mf),
56 v2pMap_(mf.getSSARegMap()->getNumVirtualRegs(), NO_PHYS_REG),
57 v2ssMap_(mf.getSSARegMap()->getNumVirtualRegs(), NO_STACK_SLOT) {
58 }
59
Alkis Evlogimenosec8b8bb2004-02-24 06:30:36 +000060 bool hasPhys(unsigned virtReg) const {
61 return getPhys(virtReg) != NO_PHYS_REG;
62 }
63
Alkis Evlogimenos79742872004-02-23 23:47:10 +000064 unsigned getPhys(unsigned virtReg) const {
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000065 assert(MRegisterInfo::isVirtualRegister(virtReg));
66 return v2pMap_[toIndex(virtReg)];
67 }
68
69 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
70 assert(MRegisterInfo::isVirtualRegister(virtReg) &&
71 MRegisterInfo::isPhysicalRegister(physReg));
72 assert(v2pMap_[toIndex(virtReg)] == NO_PHYS_REG &&
73 "attempt to assign physical register to already mapped "
74 "virtual register");
75 v2pMap_[toIndex(virtReg)] = physReg;
76 }
77
78 void clearVirtReg(unsigned virtReg) {
79 assert(MRegisterInfo::isVirtualRegister(virtReg));
80 assert(v2pMap_[toIndex(virtReg)] != NO_PHYS_REG &&
81 "attempt to clear a not assigned virtual register");
82 v2pMap_[toIndex(virtReg)] = NO_PHYS_REG;
83 }
84
Alkis Evlogimenosec8b8bb2004-02-24 06:30:36 +000085 bool hasStackSlot(unsigned virtReg) const {
86 return getStackSlot(virtReg) != NO_STACK_SLOT;
87 }
88
Alkis Evlogimenos79742872004-02-23 23:47:10 +000089 int getStackSlot(unsigned virtReg) const {
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000090 assert(MRegisterInfo::isVirtualRegister(virtReg));
91 return v2ssMap_[toIndex(virtReg)];
92 }
93
94 int assignVirt2StackSlot(unsigned virtReg);
95
96 friend std::ostream& operator<<(std::ostream& os, const VirtRegMap& li);
97 };
98
99 std::ostream& operator<<(std::ostream& os, const VirtRegMap& li);
100
101} // End llvm namespace
102
103#endif