blob: 68d817b45873533de235f9c07f7b3275f2dcb43a [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +00007//
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
Owen Anderson49c8aa02009-03-13 05:55:11 +000020#include "llvm/CodeGen/MachineFunctionPass.h"
Lang Hames86511252009-09-04 20:41:11 +000021#include "llvm/CodeGen/LiveInterval.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000022#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng4cce6b42008-04-11 17:53:36 +000023#include "llvm/ADT/BitVector.h"
Evan Chengc781a242009-05-03 18:32:42 +000024#include "llvm/ADT/DenseMap.h"
Chris Lattner94c002a2007-02-01 05:32:05 +000025#include "llvm/ADT/IndexedMap.h"
Evan Chengd3653122008-02-27 03:04:06 +000026#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmand68a0762009-01-05 17:59:02 +000027#include "llvm/ADT/SmallVector.h"
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000028#include <map>
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000029
30namespace llvm {
Evan Chengc781a242009-05-03 18:32:42 +000031 class LiveIntervals;
Chris Lattner8c4d88d2004-09-30 01:54:45 +000032 class MachineInstr;
David Greene7e231462007-08-07 16:34:05 +000033 class MachineFunction;
Evan Cheng90f95f82009-06-14 20:22:55 +000034 class MachineRegisterInfo;
Chris Lattner29268692006-09-05 02:12:02 +000035 class TargetInstrInfo;
Mike Stumpfe095f32009-05-04 18:40:41 +000036 class TargetRegisterInfo;
Daniel Dunbar1cd1d982009-07-24 10:36:58 +000037 class raw_ostream;
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +000038 class SlotIndexes;
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000039
Owen Anderson49c8aa02009-03-13 05:55:11 +000040 class VirtRegMap : public MachineFunctionPass {
Chris Lattner8c4d88d2004-09-30 01:54:45 +000041 public:
Evan Cheng2638e1a2007-03-20 08:13:50 +000042 enum {
43 NO_PHYS_REG = 0,
Evan Cheng91935142007-04-04 07:40:01 +000044 NO_STACK_SLOT = (1L << 30)-1,
45 MAX_STACK_SLOT = (1L << 18)-1
Evan Cheng2638e1a2007-03-20 08:13:50 +000046 };
47
Chris Lattner35f27052006-05-01 21:16:03 +000048 enum ModRef { isRef = 1, isMod = 2, isModRef = 3 };
Chris Lattnerbec6a9e2004-10-01 23:15:36 +000049 typedef std::multimap<MachineInstr*,
50 std::pair<unsigned, ModRef> > MI2VirtMapTy;
Alkis Evlogimenos5f375022004-03-01 20:05:10 +000051
Chris Lattner8c4d88d2004-09-30 01:54:45 +000052 private:
Evan Cheng90f95f82009-06-14 20:22:55 +000053 MachineRegisterInfo *MRI;
Owen Anderson49c8aa02009-03-13 05:55:11 +000054 const TargetInstrInfo *TII;
Mike Stumpfe095f32009-05-04 18:40:41 +000055 const TargetRegisterInfo *TRI;
Owen Anderson49c8aa02009-03-13 05:55:11 +000056 MachineFunction *MF;
Mike Stumpfe095f32009-05-04 18:40:41 +000057
58 DenseMap<const TargetRegisterClass*, BitVector> allocatableRCRegs;
59
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000060 /// Virt2PhysMap - This is a virtual to physical register
61 /// mapping. Each virtual register is required to have an entry in
62 /// it; even spilled virtual registers (the register mapped to a
63 /// spilled register is the temporary used to load it from the
64 /// stack).
Chris Lattner94c002a2007-02-01 05:32:05 +000065 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
Evan Cheng81a03822007-11-17 00:40:40 +000066
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +000067 /// Virt2StackSlotMap - This is virtual register to stack slot
68 /// mapping. Each spilled virtual register has an entry in it
69 /// which corresponds to the stack slot this register is spilled
70 /// at.
Chris Lattner94c002a2007-02-01 05:32:05 +000071 IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
Evan Cheng81a03822007-11-17 00:40:40 +000072
Evan Cheng81a03822007-11-17 00:40:40 +000073 /// Virt2SplitMap - This is virtual register to splitted virtual register
74 /// mapping.
75 IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
76
Jakob Stoklund Olesenb55e91e2010-11-16 00:41:01 +000077 /// createSpillSlot - Allocate a spill slot for RC from MFI.
78 unsigned createSpillSlot(const TargetRegisterClass *RC);
79
Chris Lattner8c4d88d2004-09-30 01:54:45 +000080 VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
81 void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
Alkis Evlogimenos79742872004-02-23 23:47:10 +000082
Chris Lattner8c4d88d2004-09-30 01:54:45 +000083 public:
Owen Anderson49c8aa02009-03-13 05:55:11 +000084 static char ID;
Owen Anderson90c579d2010-08-06 18:33:48 +000085 VirtRegMap() : MachineFunctionPass(ID), Virt2PhysMap(NO_PHYS_REG),
Jakob Stoklund Olesencb390642011-11-13 01:23:30 +000086 Virt2StackSlotMap(NO_STACK_SLOT), Virt2SplitMap(0) { }
Owen Anderson49c8aa02009-03-13 05:55:11 +000087 virtual bool runOnMachineFunction(MachineFunction &MF);
88
89 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
90 AU.setPreservesAll();
91 MachineFunctionPass::getAnalysisUsage(AU);
92 }
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000093
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000094 MachineFunction &getMachineFunction() const {
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000095 assert(MF && "getMachineFunction called before runOnMachineFunction");
Jakob Stoklund Olesenf0179002010-07-26 23:44:11 +000096 return *MF;
97 }
98
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000099 MachineRegisterInfo &getRegInfo() const { return *MRI; }
100 const TargetRegisterInfo &getTargetRegInfo() const { return *TRI; }
101
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000102 void grow();
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000103
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000104 /// @brief returns true if the specified virtual register is
105 /// mapped to a physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000106 bool hasPhys(unsigned virtReg) const {
107 return getPhys(virtReg) != NO_PHYS_REG;
108 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000109
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000110 /// @brief returns the physical register mapped to the specified
111 /// virtual register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000112 unsigned getPhys(unsigned virtReg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000113 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000114 return Virt2PhysMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000115 }
Alkis Evlogimenosdd420e02004-03-01 23:18:15 +0000116
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000117 /// @brief creates a mapping for the specified virtual register to
118 /// the specified physical register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000119 void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000120 assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
121 TargetRegisterInfo::isPhysicalRegister(physReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000122 assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000123 "attempt to assign physical register to already mapped "
124 "virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +0000125 Virt2PhysMap[virtReg] = physReg;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000126 }
127
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000128 /// @brief clears the specified virtual register's, physical
129 /// register mapping
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000130 void clearVirt(unsigned virtReg) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000131 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000132 assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000133 "attempt to clear a not assigned virtual register");
Chris Lattner7f690e62004-09-30 02:15:18 +0000134 Virt2PhysMap[virtReg] = NO_PHYS_REG;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000135 }
136
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000137 /// @brief clears all virtual to physical register mappings
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000138 void clearAllVirt() {
Chris Lattner7f690e62004-09-30 02:15:18 +0000139 Virt2PhysMap.clear();
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000140 grow();
141 }
142
Evan Cheng90f95f82009-06-14 20:22:55 +0000143 /// @brief returns the register allocation preference.
144 unsigned getRegAllocPref(unsigned virtReg);
145
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000146 /// @brief returns true if VirtReg is assigned to its preferred physreg.
147 bool hasPreferredPhys(unsigned VirtReg) {
148 return getPhys(VirtReg) == getRegAllocPref(VirtReg);
149 }
150
Evan Cheng81a03822007-11-17 00:40:40 +0000151 /// @brief records virtReg is a split live interval from SReg.
152 void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
153 Virt2SplitMap[virtReg] = SReg;
154 }
155
156 /// @brief returns the live interval virtReg is split from.
Jakob Stoklund Olesene324f6e2011-02-18 22:35:20 +0000157 unsigned getPreSplitReg(unsigned virtReg) const {
Evan Cheng81a03822007-11-17 00:40:40 +0000158 return Virt2SplitMap[virtReg];
159 }
160
Jakob Stoklund Olesenfd389172011-02-19 00:38:43 +0000161 /// getOriginal - Return the original virtual register that VirtReg descends
162 /// from through splitting.
163 /// A register that was not created by splitting is its own original.
164 /// This operation is idempotent.
165 unsigned getOriginal(unsigned VirtReg) const {
166 unsigned Orig = getPreSplitReg(VirtReg);
167 return Orig ? Orig : VirtReg;
168 }
169
Dan Gohman39e33ac2008-03-12 20:50:04 +0000170 /// @brief returns true if the specified virtual register is not
Evan Cheng549f27d32007-08-13 23:45:17 +0000171 /// mapped to a stack slot or rematerialized.
172 bool isAssignedReg(unsigned virtReg) const {
Jakob Stoklund Olesen3cb0b0e2011-11-13 01:02:04 +0000173 if (getStackSlot(virtReg) == NO_STACK_SLOT)
Evan Cheng81a03822007-11-17 00:40:40 +0000174 return true;
175 // Split register can be assigned a physical register as well as a
176 // stack slot or remat id.
177 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000178 }
179
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000180 /// @brief returns the stack slot mapped to the specified virtual
181 /// register
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000182 int getStackSlot(unsigned virtReg) const {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000183 assert(TargetRegisterInfo::isVirtualRegister(virtReg));
Chris Lattner7f690e62004-09-30 02:15:18 +0000184 return Virt2StackSlotMap[virtReg];
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000185 }
186
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000187 /// @brief create a mapping for the specifed virtual register to
188 /// the next available stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000189 int assignVirt2StackSlot(unsigned virtReg);
Alkis Evlogimenosc736b3a2004-10-01 00:35:07 +0000190 /// @brief create a mapping for the specified virtual register to
191 /// the specified stack slot
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000192 void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
193
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +0000194 /// rewrite - Rewrite all instructions in MF to use only physical registers
195 /// by mapping all virtual register operands to their assigned physical
196 /// registers.
197 ///
198 /// @param Indexes Optionally remove deleted instructions from indexes.
199 void rewrite(SlotIndexes *Indexes);
200
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000201 void print(raw_ostream &OS, const Module* M = 0) const;
Chris Lattner8c4d88d2004-09-30 01:54:45 +0000202 void dump() const;
203 };
204
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000205 inline raw_ostream &operator<<(raw_ostream &OS, const VirtRegMap &VRM) {
206 VRM.print(OS);
207 return OS;
208 }
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +0000209} // End llvm namespace
210
211#endif