2002-12-16 16:15:28 +00:00
|
|
|
//===-- RegAllocLocal.cpp - A BasicBlock generic register allocator -------===//
|
2004-02-13 18:20:47 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2004-02-13 18:20:47 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-16 16:15:28 +00:00
|
|
|
//
|
|
|
|
// This register allocator allocates registers to a basic block at a time,
|
|
|
|
// attempting to keep values in registers and reusing registers as appropriate.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-08-03 21:47:31 +00:00
|
|
|
#define DEBUG_TYPE "regalloc"
|
2003-01-13 00:25:40 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2002-12-28 20:40:43 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2002-12-16 16:15:28 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2002-12-25 05:05:46 +00:00
|
|
|
#include "llvm/CodeGen/SSARegMap.h"
|
2002-12-28 21:08:26 +00:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2003-01-13 00:25:40 +00:00
|
|
|
#include "llvm/CodeGen/LiveVariables.h"
|
2003-01-14 22:00:31 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2002-12-16 16:15:28 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2004-10-26 15:35:58 +00:00
|
|
|
#include <algorithm>
|
2006-01-22 23:41:00 +00:00
|
|
|
#include <iostream>
|
2004-01-31 21:27:19 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-12-16 16:15:28 +00:00
|
|
|
namespace {
|
2004-02-19 06:19:09 +00:00
|
|
|
Statistic<> NumStores("ra-local", "Number of stores added");
|
|
|
|
Statistic<> NumLoads ("ra-local", "Number of loads added");
|
2004-02-21 18:07:33 +00:00
|
|
|
Statistic<> NumFolded("ra-local", "Number of loads/stores folded into "
|
|
|
|
"instructions");
|
2002-12-28 20:40:43 +00:00
|
|
|
class RA : public MachineFunctionPass {
|
|
|
|
const TargetMachine *TM;
|
2002-12-16 16:15:28 +00:00
|
|
|
MachineFunction *MF;
|
2002-12-28 20:40:43 +00:00
|
|
|
const MRegisterInfo *RegInfo;
|
2003-01-13 00:25:40 +00:00
|
|
|
LiveVariables *LV;
|
2005-01-23 22:51:56 +00:00
|
|
|
bool *PhysRegsEverUsed;
|
2002-12-25 05:05:46 +00:00
|
|
|
|
2003-08-04 23:36:39 +00:00
|
|
|
// StackSlotForVirtReg - Maps virtual regs to the frame index where these
|
|
|
|
// values are spilled.
|
2002-12-28 20:40:43 +00:00
|
|
|
std::map<unsigned, int> StackSlotForVirtReg;
|
2002-12-16 16:15:28 +00:00
|
|
|
|
|
|
|
// Virt2PhysRegMap - This map contains entries for each virtual register
|
2004-02-25 21:55:45 +00:00
|
|
|
// that is currently available in a physical register.
|
|
|
|
DenseMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
|
2004-02-09 02:12:04 +00:00
|
|
|
|
|
|
|
unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
|
2004-02-25 21:55:45 +00:00
|
|
|
return Virt2PhysRegMap[VirtReg];
|
2004-02-09 02:12:04 +00:00
|
|
|
}
|
2004-02-13 18:20:47 +00:00
|
|
|
|
2004-02-09 01:26:13 +00:00
|
|
|
// PhysRegsUsed - This array is effectively a map, containing entries for
|
|
|
|
// each physical register that currently has a value (ie, it is in
|
|
|
|
// Virt2PhysRegMap). The value mapped to is the virtual register
|
|
|
|
// corresponding to the physical register (the inverse of the
|
|
|
|
// Virt2PhysRegMap), or 0. The value is set to 0 if this register is pinned
|
|
|
|
// because it is used by a future instruction. If the entry for a physical
|
|
|
|
// register is -1, then the physical register is "not in the map".
|
2002-12-16 16:15:28 +00:00
|
|
|
//
|
2004-02-13 18:20:47 +00:00
|
|
|
std::vector<int> PhysRegsUsed;
|
2002-12-16 16:15:28 +00:00
|
|
|
|
|
|
|
// PhysRegsUseOrder - This contains a list of the physical registers that
|
|
|
|
// currently have a virtual register value in them. This list provides an
|
|
|
|
// ordering of registers, imposing a reallocation order. This list is only
|
|
|
|
// used if all registers are allocated and we have to spill one, in which
|
|
|
|
// case we spill the least recently used register. Entries at the front of
|
|
|
|
// the list are the least recently used registers, entries at the back are
|
|
|
|
// the most recently used.
|
|
|
|
//
|
|
|
|
std::vector<unsigned> PhysRegsUseOrder;
|
|
|
|
|
2003-01-13 00:25:40 +00:00
|
|
|
// VirtRegModified - This bitset contains information about which virtual
|
|
|
|
// registers need to be spilled back to memory when their registers are
|
|
|
|
// scavenged. If a virtual register has simply been rematerialized, there
|
|
|
|
// is no reason to spill it to memory when we need the register back.
|
2002-12-18 08:14:26 +00:00
|
|
|
//
|
2003-01-13 00:25:40 +00:00
|
|
|
std::vector<bool> VirtRegModified;
|
|
|
|
|
|
|
|
void markVirtRegModified(unsigned Reg, bool Val = true) {
|
2004-01-31 21:27:19 +00:00
|
|
|
assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
|
2003-01-13 00:25:40 +00:00
|
|
|
Reg -= MRegisterInfo::FirstVirtualRegister;
|
|
|
|
if (VirtRegModified.size() <= Reg) VirtRegModified.resize(Reg+1);
|
|
|
|
VirtRegModified[Reg] = Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVirtRegModified(unsigned Reg) const {
|
2004-01-31 21:27:19 +00:00
|
|
|
assert(MRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
|
2003-01-13 00:25:40 +00:00
|
|
|
assert(Reg - MRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
|
2004-02-13 18:20:47 +00:00
|
|
|
&& "Illegal virtual register!");
|
2003-01-13 00:25:40 +00:00
|
|
|
return VirtRegModified[Reg - MRegisterInfo::FirstVirtualRegister];
|
|
|
|
}
|
2002-12-18 08:14:26 +00:00
|
|
|
|
2002-12-16 16:15:28 +00:00
|
|
|
void MarkPhysRegRecentlyUsed(unsigned Reg) {
|
2004-06-16 06:57:29 +00:00
|
|
|
if(PhysRegsUseOrder.empty() ||
|
|
|
|
PhysRegsUseOrder.back() == Reg) return; // Already most recently used
|
2002-12-24 00:04:55 +00:00
|
|
|
|
|
|
|
for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
|
2004-02-13 18:20:47 +00:00
|
|
|
if (areRegsEqual(Reg, PhysRegsUseOrder[i-1])) {
|
|
|
|
unsigned RegMatch = PhysRegsUseOrder[i-1]; // remove from middle
|
|
|
|
PhysRegsUseOrder.erase(PhysRegsUseOrder.begin()+i-1);
|
|
|
|
// Add it to the end of the list
|
|
|
|
PhysRegsUseOrder.push_back(RegMatch);
|
|
|
|
if (RegMatch == Reg)
|
|
|
|
return; // Found an exact match, exit early
|
|
|
|
}
|
2002-12-16 16:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "Local Register Allocator";
|
|
|
|
}
|
|
|
|
|
2003-01-13 00:25:40 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2004-02-17 17:49:10 +00:00
|
|
|
AU.addRequired<LiveVariables>();
|
2003-01-13 00:25:40 +00:00
|
|
|
AU.addRequiredID(PHIEliminationID);
|
2003-12-18 22:40:24 +00:00
|
|
|
AU.addRequiredID(TwoAddressInstructionPassID);
|
2003-01-13 00:25:40 +00:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2002-12-16 16:15:28 +00:00
|
|
|
private:
|
|
|
|
/// runOnMachineFunction - Register allocate the whole function
|
|
|
|
bool runOnMachineFunction(MachineFunction &Fn);
|
|
|
|
|
|
|
|
/// AllocateBasicBlock - Register allocate the specified basic block.
|
|
|
|
void AllocateBasicBlock(MachineBasicBlock &MBB);
|
|
|
|
|
2002-12-18 08:14:26 +00:00
|
|
|
|
|
|
|
/// areRegsEqual - This method returns true if the specified registers are
|
|
|
|
/// related to each other. To do this, it checks to see if they are equal
|
|
|
|
/// or if the first register is in the alias set of the second register.
|
|
|
|
///
|
|
|
|
bool areRegsEqual(unsigned R1, unsigned R2) const {
|
|
|
|
if (R1 == R2) return true;
|
2003-10-08 05:20:08 +00:00
|
|
|
for (const unsigned *AliasSet = RegInfo->getAliasSet(R2);
|
|
|
|
*AliasSet; ++AliasSet) {
|
|
|
|
if (*AliasSet == R1) return true;
|
|
|
|
}
|
2002-12-18 08:14:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-12-28 20:40:43 +00:00
|
|
|
/// getStackSpaceFor - This returns the frame index of the specified virtual
|
2003-08-04 23:36:39 +00:00
|
|
|
/// register on the stack, allocating space if necessary.
|
2002-12-28 20:40:43 +00:00
|
|
|
int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
|
2002-12-16 16:15:28 +00:00
|
|
|
|
2003-08-04 23:36:39 +00:00
|
|
|
/// removePhysReg - This method marks the specified physical register as no
|
|
|
|
/// longer being in use.
|
|
|
|
///
|
2002-12-18 08:14:26 +00:00
|
|
|
void removePhysReg(unsigned PhysReg);
|
2002-12-16 16:15:28 +00:00
|
|
|
|
|
|
|
/// spillVirtReg - This method spills the value specified by PhysReg into
|
|
|
|
/// the virtual register slot specified by VirtReg. It then updates the RA
|
|
|
|
/// data structures to indicate the fact that PhysReg is now available.
|
|
|
|
///
|
2004-02-22 19:08:15 +00:00
|
|
|
void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
|
2002-12-16 16:15:28 +00:00
|
|
|
unsigned VirtReg, unsigned PhysReg);
|
|
|
|
|
2002-12-16 17:44:42 +00:00
|
|
|
/// spillPhysReg - This method spills the specified physical register into
|
2003-08-17 18:01:15 +00:00
|
|
|
/// the virtual register slot associated with it. If OnlyVirtRegs is set to
|
|
|
|
/// true, then the request is ignored if the physical register does not
|
|
|
|
/// contain a virtual register.
|
2003-01-13 00:25:40 +00:00
|
|
|
///
|
2004-02-17 03:57:19 +00:00
|
|
|
void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
|
2003-08-17 18:01:15 +00:00
|
|
|
unsigned PhysReg, bool OnlyVirtRegs = false);
|
2002-12-16 17:44:42 +00:00
|
|
|
|
2003-01-13 00:25:40 +00:00
|
|
|
/// assignVirtToPhysReg - This method updates local state so that we know
|
|
|
|
/// that PhysReg is the proper container for VirtReg now. The physical
|
|
|
|
/// register must not be used for anything else when this is called.
|
|
|
|
///
|
|
|
|
void assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg);
|
|
|
|
|
|
|
|
/// liberatePhysReg - Make sure the specified physical register is available
|
|
|
|
/// for use. If there is currently a value in it, it is either moved out of
|
|
|
|
/// the way or spilled to memory.
|
|
|
|
///
|
|
|
|
void liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
|
2004-02-13 18:20:47 +00:00
|
|
|
unsigned PhysReg);
|
2002-12-16 16:15:28 +00:00
|
|
|
|
2002-12-17 02:50:10 +00:00
|
|
|
/// isPhysRegAvailable - Return true if the specified physical register is
|
|
|
|
/// free and available for use. This also includes checking to see if
|
|
|
|
/// aliased registers are all free...
|
|
|
|
///
|
2002-12-18 08:14:26 +00:00
|
|
|
bool isPhysRegAvailable(unsigned PhysReg) const;
|
2003-01-13 00:25:40 +00:00
|
|
|
|
|
|
|
/// getFreeReg - Look to see if there is a free register available in the
|
|
|
|
/// specified register class. If not, return 0.
|
|
|
|
///
|
|
|
|
unsigned getFreeReg(const TargetRegisterClass *RC);
|
2004-02-13 18:20:47 +00:00
|
|
|
|
2003-01-13 00:25:40 +00:00
|
|
|
/// getReg - Find a physical register to hold the specified virtual
|
2002-12-16 16:15:28 +00:00
|
|
|
/// register. If all compatible physical registers are used, this method
|
|
|
|
/// spills the last used virtual register to the stack, and uses that
|
|
|
|
/// register.
|
|
|
|
///
|
2004-02-17 03:57:19 +00:00
|
|
|
unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
|
2004-02-13 18:20:47 +00:00
|
|
|
unsigned VirtReg);
|
2002-12-16 16:15:28 +00:00
|
|
|
|
2004-02-17 03:57:19 +00:00
|
|
|
/// reloadVirtReg - This method transforms the specified specified virtual
|
|
|
|
/// register use to refer to a physical register. This method may do this
|
|
|
|
/// in one of several ways: if the register is available in a physical
|
|
|
|
/// register already, it uses that physical register. If the value is not
|
|
|
|
/// in a physical register, and if there are physical registers available,
|
|
|
|
/// it loads it into a register. If register pressure is high, and it is
|
|
|
|
/// possible, it tries to fold the load of the virtual register into the
|
|
|
|
/// instruction itself. It avoids doing this if register pressure is low to
|
|
|
|
/// improve the chance that subsequent instructions can use the reloaded
|
|
|
|
/// value. This method returns the modified instruction.
|
2002-12-16 16:15:28 +00:00
|
|
|
///
|
2004-02-17 03:57:19 +00:00
|
|
|
MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
|
|
|
|
unsigned OpNum);
|
2005-04-21 22:36:52 +00:00
|
|
|
|
2003-08-04 23:36:39 +00:00
|
|
|
|
|
|
|
void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
|
|
|
|
unsigned PhysReg);
|
2002-12-16 16:15:28 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2003-08-04 23:36:39 +00:00
|
|
|
/// getStackSpaceFor - This allocates space for the specified virtual register
|
|
|
|
/// to be held on the stack.
|
|
|
|
int RA::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
|
|
|
|
// Find the location Reg would belong...
|
|
|
|
std::map<unsigned, int>::iterator I =StackSlotForVirtReg.lower_bound(VirtReg);
|
2002-12-16 16:15:28 +00:00
|
|
|
|
2002-12-28 20:40:43 +00:00
|
|
|
if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
|
2002-12-16 16:15:28 +00:00
|
|
|
return I->second; // Already has space allocated?
|
|
|
|
|
2002-12-28 20:40:43 +00:00
|
|
|
// Allocate a new stack object for this spill location...
|
2004-08-15 22:02:22 +00:00
|
|
|
int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
|
|
|
|
RC->getAlignment());
|
2002-12-16 16:15:28 +00:00
|
|
|
|
|
|
|
// Assign the slot...
|
2002-12-28 20:40:43 +00:00
|
|
|
StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
|
|
|
|
return FrameIdx;
|
2002-12-16 16:15:28 +00:00
|
|
|
}
|
|
|
|
|
2002-12-17 02:50:10 +00:00
|
|
|
|
2004-02-13 18:20:47 +00:00
|
|
|
/// removePhysReg - This method marks the specified physical register as no
|
2002-12-18 08:14:26 +00:00
|
|
|
/// longer being in use.
|
|
|
|
///
|
|
|
|
void RA::removePhysReg(unsigned PhysReg) {
|
2004-02-09 01:26:13 +00:00
|
|
|
PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
|
2002-12-18 08:14:26 +00:00
|
|
|
|
|
|
|
std::vector<unsigned>::iterator It =
|
|
|
|
std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
|
2004-01-13 06:24:30 +00:00
|
|
|
if (It != PhysRegsUseOrder.end())
|
|
|
|
PhysRegsUseOrder.erase(It);
|
2002-12-18 08:14:26 +00:00
|
|
|
}
|
|
|
|
|
2003-01-13 00:25:40 +00:00
|
|
|
|
2002-12-16 16:15:28 +00:00
|
|
|
/// spillVirtReg - This method spills the value specified by PhysReg into the
|
|
|
|
/// virtual register slot specified by VirtReg. It then updates the RA data
|
|
|
|
/// structures to indicate the fact that PhysReg is now available.
|
|
|
|
///
|
2004-02-22 19:08:15 +00:00
|
|
|
void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
|
2002-12-16 16:15:28 +00:00
|
|
|
unsigned VirtReg, unsigned PhysReg) {
|
2003-08-05 04:13:58 +00:00
|
|
|
assert(VirtReg && "Spilling a physical register is illegal!"
|
2003-08-05 00:49:09 +00:00
|
|
|
" Must not have appropriate kill for the register or use exists beyond"
|
|
|
|
" the intended one.");
|
|
|
|
DEBUG(std::cerr << " Spilling register " << RegInfo->getName(PhysReg);
|
|
|
|
std::cerr << " containing %reg" << VirtReg;
|
|
|
|
if (!isVirtRegModified(VirtReg))
|
|
|
|
std::cerr << " which has not been modified, so no store necessary!");
|
|
|
|
|
|
|
|
// Otherwise, there is a virtual register corresponding to this physical
|
|
|
|
// register. We only need to spill it into its stack slot if it has been
|
|
|
|
// modified.
|
|
|
|
if (isVirtRegModified(VirtReg)) {
|
|
|
|
const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
|
|
|
|
int FrameIndex = getStackSpaceFor(VirtReg, RC);
|
|
|
|
DEBUG(std::cerr << " to stack slot #" << FrameIndex);
|
2005-09-30 01:29:00 +00:00
|
|
|
RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
|
2004-02-19 06:19:09 +00:00
|
|
|
++NumStores; // Update statistics
|
2002-12-16 16:15:28 +00:00
|
|
|
}
|
2004-02-09 02:12:04 +00:00
|
|
|
|
|
|
|
getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
|
2002-12-16 16:15:28 +00:00
|
|
|
|
2003-08-04 23:36:39 +00:00
|
|
|
DEBUG(std::cerr << "\n");
|
2002-12-18 08:14:26 +00:00
|
|
|
removePhysReg(PhysReg);
|
2002-12-16 16:15:28 +00:00
|
|
|
}
|
|
|
|
|
2002-12-17 02:50:10 +00:00
|
|
|
|
2003-01-13 00:25:40 +00:00
|
|
|
/// spillPhysReg - This method spills the specified physical register into the
|
2003-08-17 18:01:15 +00:00
|
|
|
/// virtual register slot associated with it. If OnlyVirtRegs is set to true,
|
|
|
|
/// then the request is ignored if the physical register does not contain a
|
|
|
|
/// virtual register.
|
2003-01-13 00:25:40 +00:00
|
|
|
///
|
2004-02-17 03:57:19 +00:00
|
|
|
void RA::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
|
2003-08-17 18:01:15 +00:00
|
|
|
unsigned PhysReg, bool OnlyVirtRegs) {
|
2004-02-09 01:26:13 +00:00
|
|
|
if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
|
|
|
|
if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
|
|
|
|
spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
|
2003-10-08 05:20:08 +00:00
|
|
|
} else {
|
2003-01-13 00:25:40 +00:00
|
|
|
// If the selected register aliases any other registers, we must make
|
|
|
|
// sure that one of the aliases isn't alive...
|
2003-10-08 05:20:08 +00:00
|
|
|
for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
|
2004-02-09 01:26:13 +00:00
|
|
|
*AliasSet; ++AliasSet)
|
|
|
|
if (PhysRegsUsed[*AliasSet] != -1) // Spill aliased register...
|
|
|
|
if (PhysRegsUsed[*AliasSet] || !OnlyVirtRegs)
|
|
|
|
spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
|
2003-01-13 00:25:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// assignVirtToPhysReg - This method updates local state so that we know
|
|
|
|
/// that PhysReg is the proper container for VirtReg now. The physical
|
|
|
|
/// register must not be used for anything else when this is called.
|
|
|
|
///
|
|
|
|
void RA::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
|
2004-02-09 01:26:13 +00:00
|
|
|
assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
|
2003-01-13 00:25:40 +00:00
|
|
|
// Update information to note the fact that this register was just used, and
|
|
|
|
// it holds VirtReg.
|
|
|
|
PhysRegsUsed[PhysReg] = VirtReg;
|
2004-02-13 18:20:47 +00:00
|
|
|
getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
|
2003-01-13 00:25:40 +00:00
|
|
|
PhysRegsUseOrder.push_back(PhysReg); // New use of PhysReg
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-17 02:50:10 +00:00
|
|
|
/// isPhysRegAvailable - Return true if the specified physical register is free
|
|
|
|
/// and available for use. This also includes checking to see if aliased
|
|
|
|
/// registers are all free...
|
|
|
|
///
|
|
|
|
bool RA::isPhysRegAvailable(unsigned PhysReg) const {
|
2004-02-09 01:26:13 +00:00
|
|
|
if (PhysRegsUsed[PhysReg] != -1) return false;
|
2002-12-17 02:50:10 +00:00
|
|
|
|
|
|
|
// If the selected register aliases any other allocated registers, it is
|
|
|
|
// not free!
|
2003-10-08 05:20:08 +00:00
|
|
|
for (const unsigned *AliasSet = RegInfo->getAliasSet(PhysReg);
|
|
|
|
*AliasSet; ++AliasSet)
|
2004-02-09 01:26:13 +00:00
|
|
|
if (PhysRegsUsed[*AliasSet] != -1) // Aliased register in use?
|
2003-10-08 05:20:08 +00:00
|
|
|
return false; // Can't use this reg then.
|
2002-12-17 02:50:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-13 00:25:40 +00:00
|
|
|
/// getFreeReg - Look to see if there is a free register available in the
|
|
|
|
/// specified register class. If not, return 0.
|
2002-12-16 16:15:28 +00:00
|
|
|
///
|
2003-01-13 00:25:40 +00:00
|
|
|
unsigned RA::getFreeReg(const TargetRegisterClass *RC) {
|
2002-12-28 20:40:43 +00:00
|
|
|
// Get iterators defining the range of registers that are valid to allocate in
|
|
|
|
// this class, which also specifies the preferred allocation order.
|
|
|
|
TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
|
|
|
|
TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
|
2002-12-17 02:50:10 +00:00
|
|
|
|
2003-01-13 00:25:40 +00:00
|
|
|
for (; RI != RE; ++RI)
|
|
|
|
if (isPhysRegAvailable(*RI)) { // Is reg unused?
|
|
|
|
assert(*RI != 0 && "Cannot use register!");
|
|
|
|
return *RI; // Found an unused register!
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// liberatePhysReg - Make sure the specified physical register is available for
|
|
|
|
/// use. If there is currently a value in it, it is either moved out of the way
|
|
|
|
/// or spilled to memory.
|
|
|
|
///
|
|
|
|
void RA::liberatePhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
|
2004-02-13 18:20:47 +00:00
|
|
|
unsigned PhysReg) {
|
2003-01-13 00:25:40 +00:00
|
|
|
spillPhysReg(MBB, I, PhysReg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// getReg - Find a physical register to hold the specified virtual
|
|
|
|
/// register. If all compatible physical registers are used, this method spills
|
|
|
|
/// the last used virtual register to the stack, and uses that register.
|
|
|
|
///
|
2004-02-17 03:57:19 +00:00
|
|
|
unsigned RA::getReg(MachineBasicBlock &MBB, MachineInstr *I,
|
2004-02-13 18:20:47 +00:00
|
|
|
unsigned VirtReg) {
|
2003-01-13 00:25:40 +00:00
|
|
|
const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
|
|
|
|
|
|
|
|
// First check to see if we have a free register of the requested type...
|
|
|
|
unsigned PhysReg = getFreeReg(RC);
|
2002-12-16 16:15:28 +00:00
|
|
|
|
2002-12-17 02:50:10 +00:00
|
|
|
// If we didn't find an unused register, scavenge one now!
|
2002-12-16 16:15:28 +00:00
|
|
|
if (PhysReg == 0) {
|
2002-12-16 17:44:42 +00:00
|
|
|
assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
|
2002-12-17 02:50:10 +00:00
|
|
|
|
|
|
|
// Loop over all of the preallocated registers from the least recently used
|
|
|
|
// to the most recently used. When we find one that is capable of holding
|
|
|
|
// our register, use it.
|
|
|
|
for (unsigned i = 0; PhysReg == 0; ++i) {
|
2002-12-16 16:15:28 +00:00
|
|
|
assert(i != PhysRegsUseOrder.size() &&
|
|
|
|
"Couldn't find a register of the appropriate class!");
|
2004-02-13 18:20:47 +00:00
|
|
|
|
2002-12-17 02:50:10 +00:00
|
|
|
unsigned R = PhysRegsUseOrder[i];
|
2003-08-23 23:49:42 +00:00
|
|
|
|
|
|
|
// We can only use this register if it holds a virtual register (ie, it
|
|
|
|
// can be spilled). Do not use it if it is an explicitly allocated
|
|
|
|
// physical register!
|
2004-02-09 01:26:13 +00:00
|
|
|
assert(PhysRegsUsed[R] != -1 &&
|
2003-08-23 23:49:42 +00:00
|
|
|
"PhysReg in PhysRegsUseOrder, but is not allocated?");
|
|
|
|
if (PhysRegsUsed[R]) {
|
|
|
|
// If the current register is compatible, use it.
|
2004-08-15 22:23:09 +00:00
|
|
|
if (RC->contains(R)) {
|
2003-08-23 23:49:42 +00:00
|
|
|
PhysReg = R;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// If one of the registers aliased to the current register is
|
|
|
|
// compatible, use it.
|
2003-10-08 05:20:08 +00:00
|
|
|
for (const unsigned *AliasSet = RegInfo->getAliasSet(R);
|
|
|
|
*AliasSet; ++AliasSet) {
|
2004-08-15 22:23:09 +00:00
|
|
|
if (RC->contains(*AliasSet)) {
|
2003-10-08 05:20:08 +00:00
|
|
|
PhysReg = *AliasSet; // Take an aliased register
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-08-23 23:49:42 +00:00
|
|
|
}
|
2002-12-17 02:50:10 +00:00
|
|
|
}
|
2002-12-16 16:15:28 +00:00
|
|
|
}
|
|
|
|
|
2002-12-17 02:50:10 +00:00
|
|
|
assert(PhysReg && "Physical register not assigned!?!?");
|
|
|
|
|
2002-12-16 16:15:28 +00:00
|
|
|
// At this point PhysRegsUseOrder[i] is the least recently used register of
|
|
|
|
// compatible register class. Spill it to memory and reap its remains.
|
2002-12-16 17:44:42 +00:00
|
|
|
spillPhysReg(MBB, I, PhysReg);
|
2002-12-17 02:50:10 +00:00
|
|
|
}
|
2002-12-16 17:44:42 +00:00
|
|
|
|
2002-12-16 16:15:28 +00:00
|
|
|
// Now that we know which register we need to assign this to, do it now!
|
2003-01-13 00:25:40 +00:00
|
|
|
assignVirtToPhysReg(VirtReg, PhysReg);
|
2002-12-16 16:15:28 +00:00
|
|
|
return PhysReg;
|
|
|
|
}
|
|
|
|
|
2002-12-17 02:50:10 +00:00
|
|
|
|
2004-02-17 03:57:19 +00:00
|
|
|
/// reloadVirtReg - This method transforms the specified specified virtual
|
|
|
|
/// register use to refer to a physical register. This method may do this in
|
|
|
|
/// one of several ways: if the register is available in a physical register
|
|
|
|
/// already, it uses that physical register. If the value is not in a physical
|
|
|
|
/// register, and if there are physical registers available, it loads it into a
|
|
|
|
/// register. If register pressure is high, and it is possible, it tries to
|
|
|
|
/// fold the load of the virtual register into the instruction itself. It
|
|
|
|
/// avoids doing this if register pressure is low to improve the chance that
|
|
|
|
/// subsequent instructions can use the reloaded value. This method returns the
|
|
|
|
/// modified instruction.
|
2002-12-16 16:15:28 +00:00
|
|
|
///
|
2004-02-17 03:57:19 +00:00
|
|
|
MachineInstr *RA::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
|
|
|
|
unsigned OpNum) {
|
|
|
|
unsigned VirtReg = MI->getOperand(OpNum).getReg();
|
|
|
|
|
|
|
|
// If the virtual register is already available, just update the instruction
|
|
|
|
// and return.
|
2004-02-13 18:20:47 +00:00
|
|
|
if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
|
2004-02-17 03:57:19 +00:00
|
|
|
MarkPhysRegRecentlyUsed(PR); // Already have this value available!
|
2006-05-04 17:52:23 +00:00
|
|
|
MI->getOperand(OpNum).setReg(PR); // Assign the input register
|
2004-02-17 03:57:19 +00:00
|
|
|
return MI;
|
2002-12-16 16:15:28 +00:00
|
|
|
}
|
|
|
|
|
2004-02-17 04:08:37 +00:00
|
|
|
// Otherwise, we need to fold it into the current instruction, or reload it.
|
|
|
|
// If we have registers available to hold the value, use them.
|
2002-12-25 05:05:46 +00:00
|
|
|
const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
|
2004-02-17 04:08:37 +00:00
|
|
|
unsigned PhysReg = getFreeReg(RC);
|
2004-02-17 08:09:40 +00:00
|
|
|
int FrameIndex = getStackSpaceFor(VirtReg, RC);
|
2004-02-17 04:08:37 +00:00
|
|
|
|
2004-02-17 08:09:40 +00:00
|
|
|
if (PhysReg) { // Register is available, allocate it!
|
|
|
|
assignVirtToPhysReg(VirtReg, PhysReg);
|
|
|
|
} else { // No registers available.
|
|
|
|
// If we can fold this spill into this instruction, do so now.
|
2004-03-14 07:19:51 +00:00
|
|
|
if (MachineInstr* FMI = RegInfo->foldMemoryOperand(MI, OpNum, FrameIndex)){
|
2004-02-21 18:07:33 +00:00
|
|
|
++NumFolded;
|
2004-02-19 18:34:02 +00:00
|
|
|
// Since we changed the address of MI, make sure to update live variables
|
|
|
|
// to know that the new instruction has the properties of the old one.
|
2004-03-14 07:19:51 +00:00
|
|
|
LV->instructionChanged(MI, FMI);
|
|
|
|
return MBB.insert(MBB.erase(MI), FMI);
|
2004-02-17 04:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// It looks like we can't fold this virtual register load into this
|
|
|
|
// instruction. Force some poor hapless value out of the register file to
|
|
|
|
// make room for the new register, and reload it.
|
|
|
|
PhysReg = getReg(MBB, MI, VirtReg);
|
|
|
|
}
|
|
|
|
|
2003-01-13 00:25:40 +00:00
|
|
|
markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
|
|
|
|
|
2003-08-04 23:36:39 +00:00
|
|
|
DEBUG(std::cerr << " Reloading %reg" << VirtReg << " into "
|
|
|
|
<< RegInfo->getName(PhysReg) << "\n");
|
|
|
|
|
2002-12-16 16:15:28 +00:00
|
|
|
// Add move instruction(s)
|
2005-09-30 01:29:00 +00:00
|
|
|
RegInfo->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
|
2004-02-19 06:19:09 +00:00
|
|
|
++NumLoads; // Update statistics
|
2004-02-17 03:57:19 +00:00
|
|
|
|
2005-01-23 22:51:56 +00:00
|
|
|
PhysRegsEverUsed[PhysReg] = true;
|
2006-05-04 17:52:23 +00:00
|
|
|
MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
|
2004-02-17 03:57:19 +00:00
|
|
|
return MI;
|
2002-12-16 16:15:28 +00:00
|
|
|
}
|
|
|
|
|
2003-08-04 23:36:39 +00:00
|
|
|
|
|
|
|
|
2003-01-13 00:25:40 +00:00
|
|
|
void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
|
|
|
|
// loop over each instruction
|
2005-11-09 18:22:42 +00:00
|
|
|
MachineBasicBlock::iterator MII = MBB.begin();
|
|
|
|
const TargetInstrInfo &TII = *TM->getInstrInfo();
|
|
|
|
while (MII != MBB.end()) {
|
|
|
|
MachineInstr *MI = MII++;
|
|
|
|
const TargetInstrDescriptor &TID = TII.get(MI->getOpcode());
|
2003-08-04 23:36:39 +00:00
|
|
|
DEBUG(std::cerr << "\nStarting RegAlloc of: " << *MI;
|
|
|
|
std::cerr << " Regs have values: ";
|
2004-02-09 01:26:13 +00:00
|
|
|
for (unsigned i = 0; i != RegInfo->getNumRegs(); ++i)
|
|
|
|
if (PhysRegsUsed[i] != -1)
|
|
|
|
std::cerr << "[" << RegInfo->getName(i)
|
|
|
|
<< ",%reg" << PhysRegsUsed[i] << "] ";
|
2003-08-04 23:36:39 +00:00
|
|
|
std::cerr << "\n");
|
2002-12-16 16:15:28 +00:00
|
|
|
|
2003-01-13 00:25:40 +00:00
|
|
|
// Loop over the implicit uses, making sure that they are at the head of the
|
|
|
|
// use order list, so they don't get reallocated.
|
2003-10-08 05:20:08 +00:00
|
|
|
for (const unsigned *ImplicitUses = TID.ImplicitUses;
|
|
|
|
*ImplicitUses; ++ImplicitUses)
|
2004-02-09 02:12:04 +00:00
|
|
|
MarkPhysRegRecentlyUsed(*ImplicitUses);
|
2002-12-16 16:15:28 +00:00
|
|
|
|
2003-08-15 21:19:25 +00:00
|
|
|
// Get the used operands into registers. This has the potential to spill
|
2003-08-04 23:36:39 +00:00
|
|
|
// incoming values if we are out of registers. Note that we completely
|
|
|
|
// ignore physical register uses here. We assume that if an explicit
|
|
|
|
// physical register is referenced by the instruction, that it is guaranteed
|
|
|
|
// to be live-in, or the input is badly hosed.
|
2003-01-13 00:25:40 +00:00
|
|
|
//
|
2004-02-26 22:00:20 +00:00
|
|
|
for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
|
|
|
|
MachineOperand& MO = MI->getOperand(i);
|
|
|
|
// here we are looking for only used operands (never def&use)
|
|
|
|
if (!MO.isDef() && MO.isRegister() && MO.getReg() &&
|
|
|
|
MRegisterInfo::isVirtualRegister(MO.getReg()))
|
2004-02-17 03:57:19 +00:00
|
|
|
MI = reloadVirtReg(MBB, MI, i);
|
2004-02-26 22:00:20 +00:00
|
|
|
}
|
2004-02-13 18:20:47 +00:00
|
|
|
|
2004-02-17 17:49:10 +00:00
|
|
|
// If this instruction is the last user of anything in registers, kill the
|
|
|
|
// value, freeing the register being used, so it doesn't need to be
|
|
|
|
// spilled to memory.
|
|
|
|
//
|
|
|
|
for (LiveVariables::killed_iterator KI = LV->killed_begin(MI),
|
|
|
|
KE = LV->killed_end(MI); KI != KE; ++KI) {
|
2005-08-23 23:42:17 +00:00
|
|
|
unsigned VirtReg = *KI;
|
2004-02-17 17:49:10 +00:00
|
|
|
unsigned PhysReg = VirtReg;
|
|
|
|
if (MRegisterInfo::isVirtualRegister(VirtReg)) {
|
|
|
|
// If the virtual register was never materialized into a register, it
|
|
|
|
// might not be in the map, but it won't hurt to zero it out anyway.
|
|
|
|
unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
|
|
|
|
PhysReg = PhysRegSlot;
|
|
|
|
PhysRegSlot = 0;
|
|
|
|
}
|
2002-12-16 16:15:28 +00:00
|
|
|
|
2004-02-17 17:49:10 +00:00
|
|
|
if (PhysReg) {
|
|
|
|
DEBUG(std::cerr << " Last use of " << RegInfo->getName(PhysReg)
|
|
|
|
<< "[%reg" << VirtReg <<"], removing it from live set\n");
|
|
|
|
removePhysReg(PhysReg);
|
2002-12-16 16:15:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop over all of the operands of the instruction, spilling registers that
|
|
|
|
// are defined, and marking explicit destinations in the PhysRegsUsed map.
|
2004-02-26 22:00:20 +00:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand& MO = MI->getOperand(i);
|
|
|
|
if (MO.isDef() && MO.isRegister() && MO.getReg() &&
|
|
|
|
MRegisterInfo::isPhysicalRegister(MO.getReg())) {
|
|
|
|
unsigned Reg = MO.getReg();
|
2005-01-23 22:51:56 +00:00
|
|
|
PhysRegsEverUsed[Reg] = true;
|
2004-02-12 02:27:10 +00:00
|
|
|
spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in the reg
|
2002-12-24 00:04:55 +00:00
|
|
|
PhysRegsUsed[Reg] = 0; // It is free and reserved now
|
2002-12-16 16:15:28 +00:00
|
|
|
PhysRegsUseOrder.push_back(Reg);
|
2004-01-13 06:24:30 +00:00
|
|
|
for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
|
|
|
|
*AliasSet; ++AliasSet) {
|
2004-02-09 02:12:04 +00:00
|
|
|
PhysRegsUseOrder.push_back(*AliasSet);
|
|
|
|
PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
|
2005-01-23 22:51:56 +00:00
|
|
|
PhysRegsEverUsed[*AliasSet] = true;
|
2004-01-13 06:24:30 +00:00
|
|
|
}
|
2002-12-16 16:15:28 +00:00
|
|
|
}
|
2004-02-26 22:00:20 +00:00
|
|
|
}
|
2002-12-16 16:15:28 +00:00
|
|
|
|
2003-01-13 00:25:40 +00:00
|
|
|
// Loop over the implicit defs, spilling them as well.
|
2003-12-13 01:20:58 +00:00
|
|
|
for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
|
|
|
|
*ImplicitDefs; ++ImplicitDefs) {
|
|
|
|
unsigned Reg = *ImplicitDefs;
|
2004-02-17 08:09:40 +00:00
|
|
|
spillPhysReg(MBB, MI, Reg, true);
|
2003-12-13 01:20:58 +00:00
|
|
|
PhysRegsUseOrder.push_back(Reg);
|
|
|
|
PhysRegsUsed[Reg] = 0; // It is free and reserved now
|
2005-01-23 22:51:56 +00:00
|
|
|
PhysRegsEverUsed[Reg] = true;
|
|
|
|
|
2004-01-13 06:24:30 +00:00
|
|
|
for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
|
|
|
|
*AliasSet; ++AliasSet) {
|
2004-02-09 02:12:04 +00:00
|
|
|
PhysRegsUseOrder.push_back(*AliasSet);
|
|
|
|
PhysRegsUsed[*AliasSet] = 0; // It is free and reserved now
|
2005-01-23 22:51:56 +00:00
|
|
|
PhysRegsEverUsed[*AliasSet] = true;
|
2004-01-13 06:24:30 +00:00
|
|
|
}
|
2003-12-13 01:20:58 +00:00
|
|
|
}
|
2002-12-16 16:15:28 +00:00
|
|
|
|
|
|
|
// Okay, we have allocated all of the source operands and spilled any values
|
|
|
|
// that would be destroyed by defs of this instruction. Loop over the
|
2005-01-23 22:51:56 +00:00
|
|
|
// explicit defs and assign them to a register, spilling incoming values if
|
2003-01-13 00:25:40 +00:00
|
|
|
// we need to scavenge a register.
|
2002-12-18 08:14:26 +00:00
|
|
|
//
|
2004-02-26 22:00:20 +00:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand& MO = MI->getOperand(i);
|
|
|
|
if (MO.isDef() && MO.isRegister() && MO.getReg() &&
|
|
|
|
MRegisterInfo::isVirtualRegister(MO.getReg())) {
|
|
|
|
unsigned DestVirtReg = MO.getReg();
|
2002-12-16 16:15:28 +00:00
|
|
|
unsigned DestPhysReg;
|
|
|
|
|
2003-12-18 13:08:52 +00:00
|
|
|
// If DestVirtReg already has a value, use it.
|
2004-02-13 18:20:47 +00:00
|
|
|
if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
|
2004-02-12 02:27:10 +00:00
|
|
|
DestPhysReg = getReg(MBB, MI, DestVirtReg);
|
2005-01-23 22:51:56 +00:00
|
|
|
PhysRegsEverUsed[DestPhysReg] = true;
|
2003-05-12 03:54:14 +00:00
|
|
|
markVirtRegModified(DestVirtReg);
|
2006-05-04 17:52:23 +00:00
|
|
|
MI->getOperand(i).setReg(DestPhysReg); // Assign the output register
|
2002-12-16 16:15:28 +00:00
|
|
|
}
|
2004-02-26 22:00:20 +00:00
|
|
|
}
|
2002-12-18 08:14:26 +00:00
|
|
|
|
2004-02-17 17:49:10 +00:00
|
|
|
// If this instruction defines any registers that are immediately dead,
|
|
|
|
// kill them now.
|
|
|
|
//
|
|
|
|
for (LiveVariables::killed_iterator KI = LV->dead_begin(MI),
|
|
|
|
KE = LV->dead_end(MI); KI != KE; ++KI) {
|
2005-08-23 23:42:17 +00:00
|
|
|
unsigned VirtReg = *KI;
|
2004-02-17 17:49:10 +00:00
|
|
|
unsigned PhysReg = VirtReg;
|
|
|
|
if (MRegisterInfo::isVirtualRegister(VirtReg)) {
|
|
|
|
unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
|
|
|
|
PhysReg = PhysRegSlot;
|
|
|
|
assert(PhysReg != 0);
|
|
|
|
PhysRegSlot = 0;
|
|
|
|
}
|
2003-01-13 00:25:40 +00:00
|
|
|
|
2004-02-17 17:49:10 +00:00
|
|
|
if (PhysReg) {
|
|
|
|
DEBUG(std::cerr << " Register " << RegInfo->getName(PhysReg)
|
|
|
|
<< " [%reg" << VirtReg
|
|
|
|
<< "] is never used, removing it frame live list\n");
|
|
|
|
removePhysReg(PhysReg);
|
2002-12-18 08:14:26 +00:00
|
|
|
}
|
|
|
|
}
|
2005-11-09 18:22:42 +00:00
|
|
|
|
|
|
|
// Finally, if this is a noop copy instruction, zap it.
|
|
|
|
unsigned SrcReg, DstReg;
|
|
|
|
if (TII.isMoveInstr(*MI, SrcReg, DstReg) && SrcReg == DstReg)
|
|
|
|
MBB.erase(MI);
|
2002-12-16 16:15:28 +00:00
|
|
|
}
|
|
|
|
|
2005-11-09 18:22:42 +00:00
|
|
|
MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
|
2002-12-16 16:15:28 +00:00
|
|
|
|
|
|
|
// Spill all physical registers holding virtual registers now.
|
2004-02-09 01:26:13 +00:00
|
|
|
for (unsigned i = 0, e = RegInfo->getNumRegs(); i != e; ++i)
|
|
|
|
if (PhysRegsUsed[i] != -1)
|
|
|
|
if (unsigned VirtReg = PhysRegsUsed[i])
|
2004-02-12 02:27:10 +00:00
|
|
|
spillVirtReg(MBB, MI, VirtReg, i);
|
2004-02-09 01:26:13 +00:00
|
|
|
else
|
|
|
|
removePhysReg(i);
|
2002-12-16 16:15:28 +00:00
|
|
|
|
2005-11-09 05:28:45 +00:00
|
|
|
#if 0
|
|
|
|
// This checking code is very expensive.
|
2004-02-09 02:12:04 +00:00
|
|
|
bool AllOk = true;
|
2004-02-25 21:55:45 +00:00
|
|
|
for (unsigned i = MRegisterInfo::FirstVirtualRegister,
|
|
|
|
e = MF->getSSARegMap()->getLastVirtReg(); i <= e; ++i)
|
2004-02-09 02:12:04 +00:00
|
|
|
if (unsigned PR = Virt2PhysRegMap[i]) {
|
|
|
|
std::cerr << "Register still mapped: " << i << " -> " << PR << "\n";
|
|
|
|
AllOk = false;
|
|
|
|
}
|
|
|
|
assert(AllOk && "Virtual registers still in phys regs?");
|
|
|
|
#endif
|
2004-02-13 18:20:47 +00:00
|
|
|
|
2003-08-17 18:01:15 +00:00
|
|
|
// Clear any physical register which appear live at the end of the basic
|
|
|
|
// block, but which do not hold any virtual registers. e.g., the stack
|
|
|
|
// pointer.
|
|
|
|
PhysRegsUseOrder.clear();
|
2002-12-16 16:15:28 +00:00
|
|
|
}
|
|
|
|
|
2002-12-17 03:16:10 +00:00
|
|
|
|
2002-12-16 16:15:28 +00:00
|
|
|
/// runOnMachineFunction - Register allocate the whole function
|
|
|
|
///
|
|
|
|
bool RA::runOnMachineFunction(MachineFunction &Fn) {
|
|
|
|
DEBUG(std::cerr << "Machine Function " << "\n");
|
|
|
|
MF = &Fn;
|
2002-12-28 20:40:43 +00:00
|
|
|
TM = &Fn.getTarget();
|
|
|
|
RegInfo = TM->getRegisterInfo();
|
2004-02-17 17:49:10 +00:00
|
|
|
LV = &getAnalysis<LiveVariables>();
|
2002-12-16 16:15:28 +00:00
|
|
|
|
2005-01-23 22:51:56 +00:00
|
|
|
PhysRegsEverUsed = new bool[RegInfo->getNumRegs()];
|
|
|
|
std::fill(PhysRegsEverUsed, PhysRegsEverUsed+RegInfo->getNumRegs(), false);
|
|
|
|
Fn.setUsedPhysRegs(PhysRegsEverUsed);
|
|
|
|
|
2004-02-13 18:20:47 +00:00
|
|
|
PhysRegsUsed.assign(RegInfo->getNumRegs(), -1);
|
2004-02-09 01:26:13 +00:00
|
|
|
|
2004-02-13 18:20:47 +00:00
|
|
|
// initialize the virtual->physical register map to have a 'null'
|
|
|
|
// mapping for all virtual registers
|
2004-02-25 21:55:45 +00:00
|
|
|
Virt2PhysRegMap.grow(MF->getSSARegMap()->getLastVirtReg());
|
2004-02-09 02:12:04 +00:00
|
|
|
|
2002-12-16 16:15:28 +00:00
|
|
|
// Loop over all of the basic blocks, eliminating virtual register references
|
|
|
|
for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
|
|
|
|
MBB != MBBe; ++MBB)
|
|
|
|
AllocateBasicBlock(*MBB);
|
|
|
|
|
2002-12-28 20:40:43 +00:00
|
|
|
StackSlotForVirtReg.clear();
|
2004-02-13 18:20:47 +00:00
|
|
|
PhysRegsUsed.clear();
|
2003-01-13 00:25:40 +00:00
|
|
|
VirtRegModified.clear();
|
2004-02-09 02:12:04 +00:00
|
|
|
Virt2PhysRegMap.clear();
|
2002-12-16 16:15:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-01-31 21:27:19 +00:00
|
|
|
FunctionPass *llvm::createLocalRegisterAllocator() {
|
2002-12-28 20:40:43 +00:00
|
|
|
return new RA();
|
2002-12-16 16:15:28 +00:00
|
|
|
}
|