2004-02-24 00:08:11 +01:00
|
|
|
//===-- llvm/CodeGen/VirtRegMap.h - Virtual Register Map -*- C++ -*--------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2004-02-24 00:08:11 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2004-09-30 03:54:45 +02: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-24 00:08:11 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CODEGEN_VIRTREGMAP_H
|
|
|
|
#define LLVM_CODEGEN_VIRTREGMAP_H
|
|
|
|
|
2009-03-13 06:55:11 +01:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2008-02-10 19:45:23 +01:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2007-02-01 06:32:05 +01:00
|
|
|
#include "llvm/ADT/IndexedMap.h"
|
2004-02-24 00:08:11 +01:00
|
|
|
|
|
|
|
namespace llvm {
|
2004-09-30 03:54:45 +02:00
|
|
|
class MachineInstr;
|
2007-08-07 18:34:05 +02:00
|
|
|
class MachineFunction;
|
2009-06-14 22:22:55 +02:00
|
|
|
class MachineRegisterInfo;
|
2006-09-05 04:12:02 +02:00
|
|
|
class TargetInstrInfo;
|
2009-07-24 12:36:58 +02:00
|
|
|
class raw_ostream;
|
2011-02-18 23:03:18 +01:00
|
|
|
class SlotIndexes;
|
2004-09-30 03:54:45 +02:00
|
|
|
|
2009-03-13 06:55:11 +01:00
|
|
|
class VirtRegMap : public MachineFunctionPass {
|
2004-09-30 03:54:45 +02:00
|
|
|
public:
|
2007-03-20 09:13:50 +01:00
|
|
|
enum {
|
|
|
|
NO_PHYS_REG = 0,
|
2007-04-04 09:40:01 +02:00
|
|
|
NO_STACK_SLOT = (1L << 30)-1,
|
|
|
|
MAX_STACK_SLOT = (1L << 18)-1
|
2007-03-20 09:13:50 +01:00
|
|
|
};
|
|
|
|
|
2004-09-30 03:54:45 +02:00
|
|
|
private:
|
2009-06-14 22:22:55 +02:00
|
|
|
MachineRegisterInfo *MRI;
|
2009-03-13 06:55:11 +01:00
|
|
|
const TargetInstrInfo *TII;
|
2009-05-04 05:30:11 +02:00
|
|
|
const TargetRegisterInfo *TRI;
|
2009-03-13 06:55:11 +01:00
|
|
|
MachineFunction *MF;
|
2009-05-04 05:30:11 +02:00
|
|
|
|
2004-10-01 02:35:07 +02: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 06:32:05 +01: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 01:40:40 +01:00
|
|
|
|
2004-10-01 02:35:07 +02: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 06:32:05 +01: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 01:40:40 +01:00
|
|
|
|
|
|
|
/// Virt2SplitMap - This is virtual register to splitted virtual register
|
|
|
|
/// mapping.
|
|
|
|
IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2SplitMap;
|
|
|
|
|
2010-11-16 01:41:01 +01:00
|
|
|
/// createSpillSlot - Allocate a spill slot for RC from MFI.
|
|
|
|
unsigned createSpillSlot(const TargetRegisterClass *RC);
|
|
|
|
|
2004-09-30 03:54:45 +02:00
|
|
|
VirtRegMap(const VirtRegMap&); // DO NOT IMPLEMENT
|
|
|
|
void operator=(const VirtRegMap&); // DO NOT IMPLEMENT
|
|
|
|
|
|
|
|
public:
|
2009-03-13 06:55:11 +01:00
|
|
|
static char ID;
|
2010-08-06 20:33:48 +02:00
|
|
|
VirtRegMap() : MachineFunctionPass(ID), Virt2PhysMap(NO_PHYS_REG),
|
2011-11-13 02:23:30 +01:00
|
|
|
Virt2StackSlotMap(NO_STACK_SLOT), Virt2SplitMap(0) { }
|
2009-03-13 06:55:11 +01:00
|
|
|
virtual bool runOnMachineFunction(MachineFunction &MF);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2004-09-30 03:54:45 +02:00
|
|
|
|
2010-07-27 01:44:11 +02:00
|
|
|
MachineFunction &getMachineFunction() const {
|
2010-12-10 19:36:02 +01:00
|
|
|
assert(MF && "getMachineFunction called before runOnMachineFunction");
|
2010-07-27 01:44:11 +02:00
|
|
|
return *MF;
|
|
|
|
}
|
|
|
|
|
2010-12-10 19:36:02 +01:00
|
|
|
MachineRegisterInfo &getRegInfo() const { return *MRI; }
|
|
|
|
const TargetRegisterInfo &getTargetRegInfo() const { return *TRI; }
|
|
|
|
|
2004-09-30 03:54:45 +02:00
|
|
|
void grow();
|
|
|
|
|
2004-10-01 02:35:07 +02:00
|
|
|
/// @brief returns true if the specified virtual register is
|
|
|
|
/// mapped to a physical register
|
2004-09-30 03:54:45 +02:00
|
|
|
bool hasPhys(unsigned virtReg) const {
|
|
|
|
return getPhys(virtReg) != NO_PHYS_REG;
|
|
|
|
}
|
|
|
|
|
2004-10-01 02:35:07 +02:00
|
|
|
/// @brief returns the physical register mapped to the specified
|
|
|
|
/// virtual register
|
2004-09-30 03:54:45 +02:00
|
|
|
unsigned getPhys(unsigned virtReg) const {
|
2008-02-10 19:45:23 +01:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(virtReg));
|
2004-09-30 04:15:18 +02:00
|
|
|
return Virt2PhysMap[virtReg];
|
2004-09-30 03:54:45 +02:00
|
|
|
}
|
|
|
|
|
2004-10-01 02:35:07 +02:00
|
|
|
/// @brief creates a mapping for the specified virtual register to
|
|
|
|
/// the specified physical register
|
2004-09-30 03:54:45 +02:00
|
|
|
void assignVirt2Phys(unsigned virtReg, unsigned physReg) {
|
2008-02-10 19:45:23 +01:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(virtReg) &&
|
|
|
|
TargetRegisterInfo::isPhysicalRegister(physReg));
|
2004-09-30 04:15:18 +02:00
|
|
|
assert(Virt2PhysMap[virtReg] == NO_PHYS_REG &&
|
2004-09-30 03:54:45 +02:00
|
|
|
"attempt to assign physical register to already mapped "
|
|
|
|
"virtual register");
|
2004-09-30 04:15:18 +02:00
|
|
|
Virt2PhysMap[virtReg] = physReg;
|
2004-09-30 03:54:45 +02:00
|
|
|
}
|
|
|
|
|
2004-10-01 02:35:07 +02:00
|
|
|
/// @brief clears the specified virtual register's, physical
|
|
|
|
/// register mapping
|
2004-09-30 03:54:45 +02:00
|
|
|
void clearVirt(unsigned virtReg) {
|
2008-02-10 19:45:23 +01:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(virtReg));
|
2004-09-30 04:15:18 +02:00
|
|
|
assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
|
2004-09-30 03:54:45 +02:00
|
|
|
"attempt to clear a not assigned virtual register");
|
2004-09-30 04:15:18 +02:00
|
|
|
Virt2PhysMap[virtReg] = NO_PHYS_REG;
|
2004-09-30 03:54:45 +02:00
|
|
|
}
|
|
|
|
|
2004-10-01 02:35:07 +02:00
|
|
|
/// @brief clears all virtual to physical register mappings
|
2004-09-30 03:54:45 +02:00
|
|
|
void clearAllVirt() {
|
2004-09-30 04:15:18 +02:00
|
|
|
Virt2PhysMap.clear();
|
2004-09-30 03:54:45 +02:00
|
|
|
grow();
|
|
|
|
}
|
|
|
|
|
2009-06-14 22:22:55 +02:00
|
|
|
/// @brief returns the register allocation preference.
|
|
|
|
unsigned getRegAllocPref(unsigned virtReg);
|
|
|
|
|
2011-07-08 22:46:18 +02: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 01:40:40 +01: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 23:35:20 +01: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 01:40:40 +01:00
|
|
|
return Virt2SplitMap[virtReg];
|
|
|
|
}
|
|
|
|
|
2011-02-19 01:38:43 +01: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 21:50:04 +01:00
|
|
|
/// @brief returns true if the specified virtual register is not
|
2007-08-14 01:45:17 +02:00
|
|
|
/// mapped to a stack slot or rematerialized.
|
|
|
|
bool isAssignedReg(unsigned virtReg) const {
|
2011-11-13 02:02:04 +01: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 01:40:40 +01: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 03:54:45 +02:00
|
|
|
}
|
|
|
|
|
2004-10-01 02:35:07 +02:00
|
|
|
/// @brief returns the stack slot mapped to the specified virtual
|
|
|
|
/// register
|
2004-09-30 03:54:45 +02:00
|
|
|
int getStackSlot(unsigned virtReg) const {
|
2008-02-10 19:45:23 +01:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(virtReg));
|
2004-09-30 04:15:18 +02:00
|
|
|
return Virt2StackSlotMap[virtReg];
|
2004-09-30 03:54:45 +02:00
|
|
|
}
|
|
|
|
|
2004-10-01 02:35:07 +02:00
|
|
|
/// @brief create a mapping for the specifed virtual register to
|
|
|
|
/// the next available stack slot
|
2004-09-30 03:54:45 +02:00
|
|
|
int assignVirt2StackSlot(unsigned virtReg);
|
2004-10-01 02:35:07 +02:00
|
|
|
/// @brief create a mapping for the specified virtual register to
|
|
|
|
/// the specified stack slot
|
2004-09-30 03:54:45 +02:00
|
|
|
void assignVirt2StackSlot(unsigned virtReg, int frameIndex);
|
|
|
|
|
2011-02-18 23:03:18 +01:00
|
|
|
/// rewrite - Rewrite all instructions in MF to use only physical registers
|
|
|
|
/// by mapping all virtual register operands to their assigned physical
|
|
|
|
/// registers.
|
|
|
|
///
|
|
|
|
/// @param Indexes Optionally remove deleted instructions from indexes.
|
|
|
|
void rewrite(SlotIndexes *Indexes);
|
|
|
|
|
2009-07-24 12:36:58 +02:00
|
|
|
void print(raw_ostream &OS, const Module* M = 0) const;
|
2004-09-30 03:54:45 +02:00
|
|
|
void dump() const;
|
|
|
|
};
|
|
|
|
|
2009-07-24 12:36:58 +02:00
|
|
|
inline raw_ostream &operator<<(raw_ostream &OS, const VirtRegMap &VRM) {
|
|
|
|
VRM.print(OS);
|
|
|
|
return OS;
|
|
|
|
}
|
2004-02-24 00:08:11 +01:00
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|