2004-02-23 23:08:11 +00:00
|
|
|
//===-- llvm/CodeGen/VirtRegMap.h - Virtual Register Map -*- C++ -*--------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2004-02-23 23:08:11 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2004-09-30 01:54:45 +00:00
|
|
|
// This file implements a virtual register map. This maps virtual registers to
|
|
|
|
// physical registers and virtual registers to stack slots. It is created and
|
|
|
|
// updated by a register allocator and then used by a machine code rewriter that
|
|
|
|
// adds spill code and rewrites virtual into physical register references.
|
2004-02-23 23:08:11 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CODEGEN_VIRTREGMAP_H
|
|
|
|
#define LLVM_CODEGEN_VIRTREGMAP_H
|
|
|
|
|
2009-03-13 05:55:11 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2008-02-10 18:45:23 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2007-02-01 05:32:05 +00:00
|
|
|
#include "llvm/ADT/IndexedMap.h"
|
2004-02-23 23:08:11 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
2004-09-30 01:54:45 +00:00
|
|
|
class MachineInstr;
|
2007-08-07 16:34:05 +00:00
|
|
|
class MachineFunction;
|
2009-06-14 20:22:55 +00:00
|
|
|
class MachineRegisterInfo;
|
2006-09-05 02:12:02 +00:00
|
|
|
class TargetInstrInfo;
|
2009-07-24 10:36:58 +00:00
|
|
|
class raw_ostream;
|
2011-02-18 22:03:18 +00:00
|
|
|
class SlotIndexes;
|
2004-09-30 01:54:45 +00:00
|
|
|
|
2009-03-13 05:55:11 +00:00
|
|
|
class VirtRegMap : public MachineFunctionPass {
|
2004-09-30 01:54:45 +00:00
|
|
|
public:
|
2007-03-20 08:13:50 +00:00
|
|
|
enum {
|
|
|
|
NO_PHYS_REG = 0,
|
2007-04-04 07:40:01 +00:00
|
|
|
NO_STACK_SLOT = (1L << 30)-1,
|
|
|
|
MAX_STACK_SLOT = (1L << 18)-1
|
2007-03-20 08:13:50 +00:00
|
|
|
};
|
|
|
|
|
2004-09-30 01:54:45 +00:00
|
|
|
private:
|
2009-06-14 20:22:55 +00:00
|
|
|
MachineRegisterInfo *MRI;
|
2009-03-13 05:55:11 +00:00
|
|
|
const TargetInstrInfo *TII;
|
2009-05-04 03:30:11 +00:00
|
|
|
const TargetRegisterInfo *TRI;
|
2009-03-13 05:55:11 +00:00
|
|
|
MachineFunction *MF;
|
2009-05-04 03:30:11 +00:00
|
|
|
|
2004-10-01 00:35:07 +00:00
|
|
|
/// Virt2PhysMap - This is a virtual to physical register
|
|
|
|
/// mapping. Each virtual register is required to have an entry in
|
|
|
|
/// it; even spilled virtual registers (the register mapped to a
|
|
|
|
/// spilled register is the temporary used to load it from the
|
|
|
|
/// stack).
|
2007-02-01 05:32:05 +00:00
|
|
|
IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysMap;
|
Live interval splitting:
When a live interval is being spilled, rather than creating short, non-spillable
intervals for every def / use, split the interval at BB boundaries. That is, for
every BB where the live interval is defined or used, create a new interval that
covers all the defs and uses in the BB.
This is designed to eliminate one common problem: multiple reloads of the same
value in a single basic block. Note, it does *not* decrease the number of spills
since no copies are inserted so the split intervals are *connected* through
spill and reloads (or rematerialization). The newly created intervals can be
spilled again, in that case, since it does not span multiple basic blocks, it's
spilled in the usual manner. However, it can reuse the same stack slot as the
previously split interval.
This is currently controlled by -split-intervals-at-bb.
llvm-svn: 44198
2007-11-17 00:40:40 +00:00
|
|
|
|
2004-10-01 00:35:07 +00:00
|
|
|
/// Virt2StackSlotMap - This is virtual register to stack slot
|
|
|
|
/// mapping. Each spilled virtual register has an entry in it
|
|
|
|
/// which corresponds to the stack slot this register is spilled
|
|
|
|
/// at.
|
2007-02-01 05:32:05 +00:00
|
|
|
IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
|
Live interval splitting:
When a live interval is being spilled, rather than creating short, non-spillable
intervals for every def / use, split the interval at BB boundaries. That is, for
every BB where the live interval is defined or used, create a new interval that
covers all the defs and uses in the BB.
This is designed to eliminate one common problem: multiple reloads of the same
value in a single basic block. Note, it does *not* decrease the number of spills
since no copies are inserted so the split intervals are *connected* through
spill and reloads (or rematerialization). The newly created intervals can be
spilled again, in that case, since it does not span multiple basic blocks, it's
spilled in the usual manner. However, it can reuse the same stack slot as the
previously split interval.
This is currently controlled by -split-intervals-at-bb.
llvm-svn: 44198
2007-11-17 00:40:40 +00:00
|
|
|
|
|
|
|
/// Virt2SplitMap - This is virtual register to splitted virtual register
|
|
|
|
/// mapping.
|
|
|
|
IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
|
|
|
|
|
2010-11-16 00:41:01 +00:00
|
|
|
/// createSpillSlot - Allocate a spill slot for RC from MFI.
|
|
|
|
unsigned createSpillSlot(const TargetRegisterClass *RC);
|
|
|
|
|
2004-09-30 01:54:45 +00:00
|
|
|
VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
|
|
|
|
void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
|
|
|
|
|
|
|
|
public:
|
2009-03-13 05:55:11 +00:00
|
|
|
static char ID;
|
2010-08-06 18:33:48 +00:00
|
|
|
VirtRegMap() : MachineFunctionPass(ID), Virt2PhysMap(NO_PHYS_REG),
|
2011-11-13 01:23:30 +00:00
|
|
|
Virt2StackSlotMap(NO_STACK_SLOT), Virt2SplitMap(0) { }
|
2009-03-13 05:55:11 +00:00
|
|
|
virtual bool runOnMachineFunction(MachineFunction &MF);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2004-09-30 01:54:45 +00:00
|
|
|
|
2010-07-26 23:44:11 +00:00
|
|
|
MachineFunction &getMachineFunction() const {
|
2010-12-10 18:36:02 +00:00
|
|
|
assert(MF && "getMachineFunction called before runOnMachineFunction");
|
2010-07-26 23:44:11 +00:00
|
|
|
return *MF;
|
|
|
|
}
|
|
|
|
|
2010-12-10 18:36:02 +00:00
|
|
|
MachineRegisterInfo &getRegInfo() const { return *MRI; }
|
|
|
|
const TargetRegisterInfo &getTargetRegInfo() const { return *TRI; }
|
|
|
|
|
2004-09-30 01:54:45 +00:00
|
|
|
void grow();
|
|
|
|
|
2004-10-01 00:35:07 +00:00
|
|
|
/// @brief returns true if the specified virtual register is
|
|
|
|
/// mapped to a physical register
|
2004-09-30 01:54:45 +00:00
|
|
|
bool hasPhys(unsigned virtReg) const {
|
|
|
|
return getPhys(virtReg) != NO_PHYS_REG;
|
|
|
|
}
|
|
|
|
|
2004-10-01 00:35:07 +00:00
|
|
|
/// @brief returns the physical register mapped to the specified
|
|
|
|
/// virtual register
|
2004-09-30 01:54:45 +00:00
|
|
|
unsigned getPhys(unsigned virtReg) const {
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(virtReg));
|
2004-09-30 02:15:18 +00:00
|
|
|
return Virt2PhysMap[virtReg];
|
2004-09-30 01:54:45 +00:00
|
|
|
}
|
|
|
|
|
2004-10-01 00:35:07 +00:00
|
|
|
/// @brief creates a mapping for the specified virtual register to
|
|
|
|
/// the specified physical register
|
2004-09-30 01:54:45 +00:00
|
|
|
void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
|
|
|
|
TargetRegisterInfo::isPhysicalRegister(physReg));
|
2004-09-30 02:15:18 +00:00
|
|
|
assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
|
2004-09-30 01:54:45 +00:00
|
|
|
"attempt to assign physical register to already mapped "
|
|
|
|
"virtual register");
|
2004-09-30 02:15:18 +00:00
|
|
|
Virt2PhysMap[virtReg] = physReg;
|
2004-09-30 01:54:45 +00:00
|
|
|
}
|
|
|
|
|
2004-10-01 00:35:07 +00:00
|
|
|
/// @brief clears the specified virtual register's, physical
|
|
|
|
/// register mapping
|
2004-09-30 01:54:45 +00:00
|
|
|
void clearVirt(unsigned virtReg) {
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(virtReg));
|
2004-09-30 02:15:18 +00:00
|
|
|
assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
|
2004-09-30 01:54:45 +00:00
|
|
|
"attempt to clear a not assigned virtual register");
|
2004-09-30 02:15:18 +00:00
|
|
|
Virt2PhysMap[virtReg] = NO_PHYS_REG;
|
2004-09-30 01:54:45 +00:00
|
|
|
}
|
|
|
|
|
2004-10-01 00:35:07 +00:00
|
|
|
/// @brief clears all virtual to physical register mappings
|
2004-09-30 01:54:45 +00:00
|
|
|
void clearAllVirt() {
|
2004-09-30 02:15:18 +00:00
|
|
|
Virt2PhysMap.clear();
|
2004-09-30 01:54:45 +00:00
|
|
|
grow();
|
|
|
|
}
|
|
|
|
|
2009-06-14 20:22:55 +00:00
|
|
|
/// @brief returns the register allocation preference.
|
|
|
|
unsigned getRegAllocPref(unsigned virtReg);
|
|
|
|
|
2011-07-08 20:46:18 +00:00
|
|
|
/// @brief returns true if VirtReg is assigned to its preferred physreg.
|
|
|
|
bool hasPreferredPhys(unsigned VirtReg) {
|
|
|
|
return getPhys(VirtReg) == getRegAllocPref(VirtReg);
|
|
|
|
}
|
|
|
|
|
Live interval splitting:
When a live interval is being spilled, rather than creating short, non-spillable
intervals for every def / use, split the interval at BB boundaries. That is, for
every BB where the live interval is defined or used, create a new interval that
covers all the defs and uses in the BB.
This is designed to eliminate one common problem: multiple reloads of the same
value in a single basic block. Note, it does *not* decrease the number of spills
since no copies are inserted so the split intervals are *connected* through
spill and reloads (or rematerialization). The newly created intervals can be
spilled again, in that case, since it does not span multiple basic blocks, it's
spilled in the usual manner. However, it can reuse the same stack slot as the
previously split interval.
This is currently controlled by -split-intervals-at-bb.
llvm-svn: 44198
2007-11-17 00:40:40 +00:00
|
|
|
/// @brief records virtReg is a split live interval from SReg.
|
|
|
|
void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
|
|
|
|
Virt2SplitMap[virtReg] = SReg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief returns the live interval virtReg is split from.
|
2011-02-18 22:35:20 +00:00
|
|
|
unsigned getPreSplitReg(unsigned virtReg) const {
|
Live interval splitting:
When a live interval is being spilled, rather than creating short, non-spillable
intervals for every def / use, split the interval at BB boundaries. That is, for
every BB where the live interval is defined or used, create a new interval that
covers all the defs and uses in the BB.
This is designed to eliminate one common problem: multiple reloads of the same
value in a single basic block. Note, it does *not* decrease the number of spills
since no copies are inserted so the split intervals are *connected* through
spill and reloads (or rematerialization). The newly created intervals can be
spilled again, in that case, since it does not span multiple basic blocks, it's
spilled in the usual manner. However, it can reuse the same stack slot as the
previously split interval.
This is currently controlled by -split-intervals-at-bb.
llvm-svn: 44198
2007-11-17 00:40:40 +00:00
|
|
|
return Virt2SplitMap[virtReg];
|
|
|
|
}
|
|
|
|
|
2011-02-19 00:38:43 +00:00
|
|
|
/// getOriginal - Return the original virtual register that VirtReg descends
|
|
|
|
/// from through splitting.
|
|
|
|
/// A register that was not created by splitting is its own original.
|
|
|
|
/// This operation is idempotent.
|
|
|
|
unsigned getOriginal(unsigned VirtReg) const {
|
|
|
|
unsigned Orig = getPreSplitReg(VirtReg);
|
|
|
|
return Orig ? Orig : VirtReg;
|
|
|
|
}
|
|
|
|
|
2008-03-12 20:50:04 +00:00
|
|
|
/// @brief returns true if the specified virtual register is not
|
2007-08-13 23:45:17 +00:00
|
|
|
/// mapped to a stack slot or rematerialized.
|
|
|
|
bool isAssignedReg(unsigned virtReg) const {
|
2011-11-13 01:02:04 +00:00
|
|
|
if (getStackSlot(virtReg) == NO_STACK_SLOT)
|
Live interval splitting:
When a live interval is being spilled, rather than creating short, non-spillable
intervals for every def / use, split the interval at BB boundaries. That is, for
every BB where the live interval is defined or used, create a new interval that
covers all the defs and uses in the BB.
This is designed to eliminate one common problem: multiple reloads of the same
value in a single basic block. Note, it does *not* decrease the number of spills
since no copies are inserted so the split intervals are *connected* through
spill and reloads (or rematerialization). The newly created intervals can be
spilled again, in that case, since it does not span multiple basic blocks, it's
spilled in the usual manner. However, it can reuse the same stack slot as the
previously split interval.
This is currently controlled by -split-intervals-at-bb.
llvm-svn: 44198
2007-11-17 00:40:40 +00:00
|
|
|
return true;
|
|
|
|
// Split register can be assigned a physical register as well as a
|
|
|
|
// stack slot or remat id.
|
|
|
|
return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
|
2004-09-30 01:54:45 +00:00
|
|
|
}
|
|
|
|
|
2004-10-01 00:35:07 +00:00
|
|
|
/// @brief returns the stack slot mapped to the specified virtual
|
|
|
|
/// register
|
2004-09-30 01:54:45 +00:00
|
|
|
int getStackSlot(unsigned virtReg) const {
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(virtReg));
|
2004-09-30 02:15:18 +00:00
|
|
|
return Virt2StackSlotMap[virtReg];
|
2004-09-30 01:54:45 +00:00
|
|
|
}
|
|
|
|
|
2004-10-01 00:35:07 +00:00
|
|
|
/// @brief create a mapping for the specifed virtual register to
|
|
|
|
/// the next available stack slot
|
2004-09-30 01:54:45 +00:00
|
|
|
int assignVirt2StackSlot(unsigned virtReg);
|
2004-10-01 00:35:07 +00:00
|
|
|
/// @brief create a mapping for the specified virtual register to
|
|
|
|
/// the specified stack slot
|
2004-09-30 01:54:45 +00:00
|
|
|
void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
|
|
|
|
|
2009-07-24 10:36:58 +00:00
|
|
|
void print(raw_ostream &OS, const Module* M = 0) const;
|
2004-09-30 01:54:45 +00:00
|
|
|
void dump() const;
|
|
|
|
};
|
|
|
|
|
2009-07-24 10:36:58 +00:00
|
|
|
inline raw_ostream &operator<<(raw_ostream &OS, const VirtRegMap &VRM) {
|
|
|
|
VRM.print(OS);
|
|
|
|
return OS;
|
|
|
|
}
|
2004-02-23 23:08:11 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|