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
|
|
|
|
//
|
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-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"
|
2006-11-15 20:55:15 +00:00
|
|
|
#include "llvm/BasicBlock.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-28 21:08:26 +00:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2007-12-31 04:13:23 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2008-02-06 08:00:32 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2006-08-02 12:30:23 +00:00
|
|
|
#include "llvm/CodeGen/RegAllocRegistry.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"
|
2006-08-27 12:54:02 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-07-11 13:10:19 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-07-10 01:56:35 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2007-02-01 05:32:05 +00:00
|
|
|
#include "llvm/ADT/IndexedMap.h"
|
2009-01-29 02:20:59 +00:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2006-11-15 20:55:15 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2008-02-06 19:16:53 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2004-10-26 15:35:58 +00:00
|
|
|
#include <algorithm>
|
2004-01-31 21:27:19 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2006-12-19 22:41:21 +00:00
|
|
|
STATISTIC(NumStores, "Number of stores added");
|
|
|
|
STATISTIC(NumLoads , "Number of loads added");
|
2006-08-01 14:21:23 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
static RegisterRegAlloc
|
2008-10-14 20:25:08 +00:00
|
|
|
localRegAlloc("local", "local register allocator",
|
2008-05-13 00:00:25 +00:00
|
|
|
createLocalRegisterAllocator);
|
2006-08-01 14:21:23 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
namespace {
|
2007-05-08 19:02:46 +00:00
|
|
|
class VISIBILITY_HIDDEN RALocal : public MachineFunctionPass {
|
2007-05-01 21:15:47 +00:00
|
|
|
public:
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID;
|
2008-09-04 17:05:41 +00:00
|
|
|
RALocal() : MachineFunctionPass(&ID), StackSlotForVirtReg(-1) {}
|
2007-05-01 21:15:47 +00:00
|
|
|
private:
|
2002-12-28 20:40:43 +00:00
|
|
|
const TargetMachine *TM;
|
2002-12-16 16:15:28 +00:00
|
|
|
MachineFunction *MF;
|
2008-02-10 18:45:23 +00:00
|
|
|
const TargetRegisterInfo *TRI;
|
2008-01-07 01:35:56 +00:00
|
|
|
const TargetInstrInfo *TII;
|
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.
|
2008-07-10 18:23:23 +00:00
|
|
|
IndexedMap<int, VirtReg2IndexFunctor> 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.
|
2007-02-01 05:32:05 +00:00
|
|
|
IndexedMap<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
|
2006-09-08 19:03:30 +00:00
|
|
|
// because it is used by a future instruction, and to -2 if it is not
|
|
|
|
// allocatable. 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;
|
|
|
|
|
2008-01-17 02:08:17 +00:00
|
|
|
// Virt2LastUseMap - This maps each virtual register to its last use
|
|
|
|
// (MachineInstr*, operand index pair).
|
|
|
|
IndexedMap<std::pair<MachineInstr*, unsigned>, VirtReg2IndexFunctor>
|
|
|
|
Virt2LastUseMap;
|
|
|
|
|
|
|
|
std::pair<MachineInstr*,unsigned>& getVirtRegLastUse(unsigned Reg) {
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
|
2008-01-17 02:08:17 +00:00
|
|
|
return Virt2LastUseMap[Reg];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
//
|
2008-01-17 00:35:26 +00:00
|
|
|
BitVector VirtRegModified;
|
2008-07-08 22:24:50 +00:00
|
|
|
|
|
|
|
// UsedInMultipleBlocks - Tracks whether a particular register is used in
|
|
|
|
// more than one block.
|
|
|
|
BitVector UsedInMultipleBlocks;
|
2003-01-13 00:25:40 +00:00
|
|
|
|
|
|
|
void markVirtRegModified(unsigned Reg, bool Val = true) {
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
|
|
|
|
Reg -= TargetRegisterInfo::FirstVirtualRegister;
|
2008-01-17 00:35:26 +00:00
|
|
|
if (Val)
|
|
|
|
VirtRegModified.set(Reg);
|
|
|
|
else
|
|
|
|
VirtRegModified.reset(Reg);
|
2003-01-13 00:25:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isVirtRegModified(unsigned Reg) const {
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
|
|
|
|
assert(Reg - TargetRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
|
2004-02-13 18:20:47 +00:00
|
|
|
&& "Illegal virtual register!");
|
2008-02-10 18:45:23 +00:00
|
|
|
return VirtRegModified[Reg - TargetRegisterInfo::FirstVirtualRegister];
|
2003-01-13 00:25:40 +00:00
|
|
|
}
|
2002-12-18 08:14:26 +00:00
|
|
|
|
2007-06-26 21:05:13 +00:00
|
|
|
void AddToPhysRegsUseOrder(unsigned Reg) {
|
|
|
|
std::vector<unsigned>::iterator It =
|
|
|
|
std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), Reg);
|
|
|
|
if (It != PhysRegsUseOrder.end())
|
|
|
|
PhysRegsUseOrder.erase(It);
|
|
|
|
PhysRegsUseOrder.push_back(Reg);
|
|
|
|
}
|
|
|
|
|
2002-12-16 16:15:28 +00:00
|
|
|
void MarkPhysRegRecentlyUsed(unsigned Reg) {
|
2006-09-03 07:15:37 +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 {
|
2009-07-31 23:37:33 +00:00
|
|
|
AU.setPreservesCFG();
|
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;
|
2008-02-10 18:45:23 +00:00
|
|
|
for (const unsigned *AliasSet = TRI->getAliasSet(R2);
|
2003-10-08 05:20:08 +00:00
|
|
|
*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);
|
|
|
|
|
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
|
2009-01-29 01:13:00 +00:00
|
|
|
/// register. If NoFree is true, that means the caller knows there isn't
|
|
|
|
/// a free register, do not call getFreeReg().
|
2004-02-17 03:57:19 +00:00
|
|
|
unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
|
2009-01-29 01:13:00 +00:00
|
|
|
unsigned VirtReg, bool NoFree = false);
|
2002-12-16 16:15:28 +00:00
|
|
|
|
2009-05-07 21:19:45 +00:00
|
|
|
/// reloadVirtReg - This method transforms the specified virtual
|
2004-02-17 03:57:19 +00:00
|
|
|
/// 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,
|
2009-01-29 02:20:59 +00:00
|
|
|
unsigned OpNum, SmallSet<unsigned, 4> &RRegs);
|
2005-04-21 22:36:52 +00:00
|
|
|
|
2008-07-09 20:14:53 +00:00
|
|
|
/// ComputeLocalLiveness - Computes liveness of registers within a basic
|
|
|
|
/// block, setting the killed/dead flags as appropriate.
|
|
|
|
void ComputeLocalLiveness(MachineBasicBlock& MBB);
|
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
|
|
|
};
|
2007-05-08 19:02:46 +00:00
|
|
|
char RALocal::ID = 0;
|
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.
|
2007-05-08 19:02:46 +00:00
|
|
|
int RALocal::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
|
2003-08-04 23:36:39 +00:00
|
|
|
// Find the location Reg would belong...
|
2008-07-10 18:23:23 +00:00
|
|
|
int SS = StackSlotForVirtReg[VirtReg];
|
|
|
|
if (SS != -1)
|
|
|
|
return SS; // Already has space allocated?
|
2002-12-16 16:15:28 +00:00
|
|
|
|
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...
|
2008-07-10 18:23:23 +00:00
|
|
|
StackSlotForVirtReg[VirtReg] = FrameIdx;
|
2002-12-28 20:40:43 +00:00
|
|
|
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.
|
|
|
|
///
|
2007-05-08 19:02:46 +00:00
|
|
|
void RALocal::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.
|
|
|
|
///
|
2007-05-08 19:02:46 +00:00
|
|
|
void RALocal::spillVirtReg(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I,
|
|
|
|
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.");
|
2008-02-26 21:47:57 +00:00
|
|
|
DOUT << " Spilling register " << TRI->getName(PhysReg)
|
2006-11-17 02:09:07 +00:00
|
|
|
<< " containing %reg" << VirtReg;
|
2008-01-01 21:11:32 +00:00
|
|
|
|
2008-01-17 02:08:17 +00:00
|
|
|
if (!isVirtRegModified(VirtReg)) {
|
2006-11-17 02:09:07 +00:00
|
|
|
DOUT << " which has not been modified, so no store necessary!";
|
2008-01-17 02:08:17 +00:00
|
|
|
std::pair<MachineInstr*, unsigned> &LastUse = getVirtRegLastUse(VirtReg);
|
|
|
|
if (LastUse.first)
|
|
|
|
LastUse.first->getOperand(LastUse.second).setIsKill();
|
2008-02-06 19:16:53 +00:00
|
|
|
} else {
|
|
|
|
// 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.
|
2007-12-31 04:13:23 +00:00
|
|
|
const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
|
2003-08-05 00:49:09 +00:00
|
|
|
int FrameIndex = getStackSpaceFor(VirtReg, RC);
|
2006-11-17 02:09:07 +00:00
|
|
|
DOUT << " to stack slot #" << FrameIndex;
|
2008-02-06 19:16:53 +00:00
|
|
|
// If the instruction reads the register that's spilled, (e.g. this can
|
|
|
|
// happen if it is a move to a physical register), then the spill
|
|
|
|
// instruction is not a kill.
|
2008-03-05 00:59:57 +00:00
|
|
|
bool isKill = !(I != MBB.end() && I->readsRegister(PhysReg));
|
2008-02-11 08:30:52 +00:00
|
|
|
TII->storeRegToStackSlot(MBB, I, PhysReg, isKill, 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
|
|
|
|
2006-11-17 02:09:07 +00:00
|
|
|
DOUT << "\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
|
|
|
///
|
2007-05-08 19:02:46 +00:00
|
|
|
void RALocal::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
|
|
|
|
unsigned PhysReg, bool OnlyVirtRegs) {
|
2004-02-09 01:26:13 +00:00
|
|
|
if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
|
2006-09-08 19:03:30 +00:00
|
|
|
assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!");
|
2004-02-09 01:26:13 +00:00
|
|
|
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
|
2006-09-08 19:03:30 +00:00
|
|
|
// sure that one of the aliases isn't alive.
|
2008-02-10 18:45:23 +00:00
|
|
|
for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
|
2004-02-09 01:26:13 +00:00
|
|
|
*AliasSet; ++AliasSet)
|
2006-09-08 19:03:30 +00:00
|
|
|
if (PhysRegsUsed[*AliasSet] != -1 && // Spill aliased register.
|
|
|
|
PhysRegsUsed[*AliasSet] != -2) // If allocatable.
|
2007-06-26 21:05:13 +00:00
|
|
|
if (PhysRegsUsed[*AliasSet])
|
|
|
|
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.
|
|
|
|
///
|
2007-05-08 19:02:46 +00:00
|
|
|
void RALocal::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;
|
2007-06-26 21:05:13 +00:00
|
|
|
AddToPhysRegsUseOrder(PhysReg); // New use of PhysReg
|
2003-01-13 00:25:40 +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...
|
|
|
|
///
|
2007-05-08 19:02:46 +00:00
|
|
|
bool RALocal::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!
|
2008-02-10 18:45:23 +00:00
|
|
|
for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
|
2003-10-08 05:20:08 +00:00
|
|
|
*AliasSet; ++AliasSet)
|
2008-02-22 20:30:53 +00:00
|
|
|
if (PhysRegsUsed[*AliasSet] >= 0) // 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
|
|
|
///
|
2007-05-08 19:02:46 +00:00
|
|
|
unsigned RALocal::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
///
|
2007-05-08 19:02:46 +00:00
|
|
|
unsigned RALocal::getReg(MachineBasicBlock &MBB, MachineInstr *I,
|
2009-01-29 01:13:00 +00:00
|
|
|
unsigned VirtReg, bool NoFree) {
|
2007-12-31 04:13:23 +00:00
|
|
|
const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
|
2003-01-13 00:25:40 +00:00
|
|
|
|
|
|
|
// First check to see if we have a free register of the requested type...
|
2009-01-29 01:13:00 +00:00
|
|
|
unsigned PhysReg = NoFree ? 0 : 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?");
|
2006-09-08 19:03:30 +00:00
|
|
|
if (PhysRegsUsed[R] && PhysRegsUsed[R] != -2) {
|
2003-08-23 23:49:42 +00:00
|
|
|
// 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.
|
2008-02-10 18:45:23 +00:00
|
|
|
for (const unsigned *AliasIt = TRI->getAliasSet(R);
|
2006-09-03 07:15:37 +00:00
|
|
|
*AliasIt; ++AliasIt) {
|
|
|
|
if (RC->contains(*AliasIt) &&
|
|
|
|
// If this is pinned down for some reason, don't use it. For
|
|
|
|
// example, if CL is pinned, and we run across CH, don't use
|
|
|
|
// CH as justification for using scavenging ECX (which will
|
|
|
|
// fail).
|
2006-09-08 19:03:30 +00:00
|
|
|
PhysRegsUsed[*AliasIt] != 0 &&
|
|
|
|
|
|
|
|
// Make sure the register is allocatable. Don't allocate SIL on
|
|
|
|
// x86-32.
|
|
|
|
PhysRegsUsed[*AliasIt] != -2) {
|
2006-09-03 07:15:37 +00:00
|
|
|
PhysReg = *AliasIt; // Take an aliased register
|
2003-10-08 05:20:08 +00:00
|
|
|
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
|
|
|
|
2009-05-07 21:20:42 +00:00
|
|
|
/// reloadVirtReg - This method transforms the specified virtual
|
2004-02-17 03:57:19 +00:00
|
|
|
/// 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
|
|
|
///
|
2007-05-08 19:02:46 +00:00
|
|
|
MachineInstr *RALocal::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
|
2009-01-29 02:20:59 +00:00
|
|
|
unsigned OpNum,
|
|
|
|
SmallSet<unsigned, 4> &ReloadedRegs) {
|
2004-02-17 03:57:19 +00:00
|
|
|
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)) {
|
2008-02-29 18:52:01 +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
|
2008-02-29 18:52:01 +00:00
|
|
|
getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
|
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.
|
2007-12-31 04:13:23 +00:00
|
|
|
const TargetRegisterClass *RC = MF->getRegInfo().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.
|
2008-02-07 19:46:55 +00:00
|
|
|
// Force some poor hapless value out of the register file to
|
2004-02-17 04:08:37 +00:00
|
|
|
// make room for the new register, and reload it.
|
2009-01-29 01:13:00 +00:00
|
|
|
PhysReg = getReg(MBB, MI, VirtReg, true);
|
2004-02-17 04:08:37 +00:00
|
|
|
}
|
|
|
|
|
2003-01-13 00:25:40 +00:00
|
|
|
markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
|
|
|
|
|
2006-11-17 02:09:07 +00:00
|
|
|
DOUT << " Reloading %reg" << VirtReg << " into "
|
2008-02-26 21:47:57 +00:00
|
|
|
<< TRI->getName(PhysReg) << "\n";
|
2003-08-04 23:36:39 +00:00
|
|
|
|
2002-12-16 16:15:28 +00:00
|
|
|
// Add move instruction(s)
|
2008-01-01 21:11:32 +00:00
|
|
|
TII->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
|
|
|
|
2007-12-31 04:13:23 +00:00
|
|
|
MF->getRegInfo().setPhysRegUsed(PhysReg);
|
2006-05-04 17:52:23 +00:00
|
|
|
MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
|
2008-01-17 02:08:17 +00:00
|
|
|
getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
|
2009-01-29 02:20:59 +00:00
|
|
|
|
|
|
|
if (!ReloadedRegs.insert(PhysReg)) {
|
2009-07-11 13:10:19 +00:00
|
|
|
std::string msg;
|
|
|
|
raw_string_ostream Msg(msg);
|
|
|
|
Msg << "Ran out of registers during register allocation!";
|
2009-01-29 02:20:59 +00:00
|
|
|
if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
|
2009-07-11 13:10:19 +00:00
|
|
|
Msg << "\nPlease check your inline asm statement for invalid "
|
2009-01-29 02:20:59 +00:00
|
|
|
<< "constraints:\n";
|
2009-07-11 13:10:19 +00:00
|
|
|
MI->print(Msg, TM);
|
2009-01-29 02:20:59 +00:00
|
|
|
}
|
2009-07-11 13:10:19 +00:00
|
|
|
llvm_report_error(Msg.str());
|
2009-01-29 02:20:59 +00:00
|
|
|
}
|
|
|
|
for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
|
|
|
|
*SubRegs; ++SubRegs) {
|
|
|
|
if (!ReloadedRegs.insert(*SubRegs)) {
|
2009-07-11 13:10:19 +00:00
|
|
|
std::string msg;
|
|
|
|
raw_string_ostream Msg(msg);
|
|
|
|
Msg << "Ran out of registers during register allocation!";
|
2009-01-29 02:20:59 +00:00
|
|
|
if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
|
2009-07-11 13:10:19 +00:00
|
|
|
Msg << "\nPlease check your inline asm statement for invalid "
|
2009-01-29 02:20:59 +00:00
|
|
|
<< "constraints:\n";
|
2009-07-11 13:10:19 +00:00
|
|
|
MI->print(Msg, TM);
|
2009-01-29 02:20:59 +00:00
|
|
|
}
|
2009-07-11 13:10:19 +00:00
|
|
|
llvm_report_error(Msg.str());
|
2009-01-29 02:20:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-17 03:57:19 +00:00
|
|
|
return MI;
|
2002-12-16 16:15:28 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 21:05:13 +00:00
|
|
|
/// isReadModWriteImplicitKill - True if this is an implicit kill for a
|
|
|
|
/// read/mod/write register, i.e. update partial register.
|
|
|
|
static bool isReadModWriteImplicitKill(MachineInstr *MI, unsigned Reg) {
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand& MO = MI->getOperand(i);
|
2008-10-03 15:45:36 +00:00
|
|
|
if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
|
2007-06-26 21:05:13 +00:00
|
|
|
MO.isDef() && !MO.isDead())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2003-08-04 23:36:39 +00:00
|
|
|
|
2007-06-26 21:05:13 +00:00
|
|
|
/// isReadModWriteImplicitDef - True if this is an implicit def for a
|
|
|
|
/// read/mod/write register, i.e. update partial register.
|
|
|
|
static bool isReadModWriteImplicitDef(MachineInstr *MI, unsigned Reg) {
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand& MO = MI->getOperand(i);
|
2008-10-03 15:45:36 +00:00
|
|
|
if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
|
2007-06-26 21:05:13 +00:00
|
|
|
!MO.isDef() && MO.isKill())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2003-08-04 23:36:39 +00:00
|
|
|
|
2008-07-08 22:24:50 +00:00
|
|
|
// precedes - Helper function to determine with MachineInstr A
|
|
|
|
// precedes MachineInstr B within the same MBB.
|
|
|
|
static bool precedes(MachineBasicBlock::iterator A,
|
|
|
|
MachineBasicBlock::iterator B) {
|
|
|
|
if (A == B)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator I = A->getParent()->begin();
|
|
|
|
while (I != A->getParent()->end()) {
|
|
|
|
if (I == A)
|
|
|
|
return true;
|
|
|
|
else if (I == B)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-07-09 20:14:53 +00:00
|
|
|
/// ComputeLocalLiveness - Computes liveness of registers within a basic
|
|
|
|
/// block, setting the killed/dead flags as appropriate.
|
|
|
|
void RALocal::ComputeLocalLiveness(MachineBasicBlock& MBB) {
|
2008-07-08 22:24:50 +00:00
|
|
|
MachineRegisterInfo& MRI = MBB.getParent()->getRegInfo();
|
|
|
|
// Keep track of the most recently seen previous use or def of each reg,
|
|
|
|
// so that we can update them with dead/kill markers.
|
2008-07-10 01:56:35 +00:00
|
|
|
DenseMap<unsigned, std::pair<MachineInstr*, unsigned> > LastUseDef;
|
2008-07-08 22:24:50 +00:00
|
|
|
for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand& MO = I->getOperand(i);
|
|
|
|
// Uses don't trigger any flags, but we need to save
|
|
|
|
// them for later. Also, we have to process these
|
|
|
|
// _before_ processing the defs, since an instr
|
|
|
|
// uses regs before it defs them.
|
2008-10-08 04:30:51 +00:00
|
|
|
if (MO.isReg() && MO.getReg() && MO.isUse()) {
|
2008-07-08 22:24:50 +00:00
|
|
|
LastUseDef[MO.getReg()] = std::make_pair(I, i);
|
2008-10-08 04:30:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) continue;
|
|
|
|
|
2009-01-29 02:20:59 +00:00
|
|
|
const unsigned* Aliases = TRI->getAliasSet(MO.getReg());
|
|
|
|
if (Aliases) {
|
|
|
|
while (*Aliases) {
|
2008-10-08 04:30:51 +00:00
|
|
|
DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
|
2009-01-29 02:20:59 +00:00
|
|
|
alias = LastUseDef.find(*Aliases);
|
2008-10-08 04:30:51 +00:00
|
|
|
|
2009-01-29 02:20:59 +00:00
|
|
|
if (alias != LastUseDef.end() && alias->second.first != I)
|
|
|
|
LastUseDef[*Aliases] = std::make_pair(I, i);
|
2008-10-08 04:30:51 +00:00
|
|
|
|
2009-01-29 02:20:59 +00:00
|
|
|
++Aliases;
|
2008-10-08 04:30:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-08 22:24:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand& MO = I->getOperand(i);
|
|
|
|
// Defs others than 2-addr redefs _do_ trigger flag changes:
|
|
|
|
// - A def followed by a def is dead
|
|
|
|
// - A use followed by a def is a kill
|
2008-10-03 15:45:36 +00:00
|
|
|
if (MO.isReg() && MO.getReg() && MO.isDef()) {
|
2008-07-10 01:56:35 +00:00
|
|
|
DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
|
2008-07-08 22:24:50 +00:00
|
|
|
last = LastUseDef.find(MO.getReg());
|
|
|
|
if (last != LastUseDef.end()) {
|
2008-07-10 01:53:01 +00:00
|
|
|
// Check if this is a two address instruction. If so, then
|
|
|
|
// the def does not kill the use.
|
2008-07-10 07:35:43 +00:00
|
|
|
if (last->second.first == I &&
|
2009-04-09 17:16:43 +00:00
|
|
|
I->isRegTiedToUseOperand(i))
|
2008-07-10 07:35:43 +00:00
|
|
|
continue;
|
2008-07-09 21:15:10 +00:00
|
|
|
|
2008-07-08 22:24:50 +00:00
|
|
|
MachineOperand& lastUD =
|
|
|
|
last->second.first->getOperand(last->second.second);
|
|
|
|
if (lastUD.isDef())
|
|
|
|
lastUD.setIsDead(true);
|
2008-07-10 07:35:43 +00:00
|
|
|
else
|
2008-07-08 22:24:50 +00:00
|
|
|
lastUD.setIsKill(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
LastUseDef[MO.getReg()] = std::make_pair(I, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Live-out (of the function) registers contain return values of the function,
|
|
|
|
// so we need to make sure they are alive at return time.
|
|
|
|
if (!MBB.empty() && MBB.back().getDesc().isReturn()) {
|
|
|
|
MachineInstr* Ret = &MBB.back();
|
|
|
|
for (MachineRegisterInfo::liveout_iterator
|
|
|
|
I = MF->getRegInfo().liveout_begin(),
|
|
|
|
E = MF->getRegInfo().liveout_end(); I != E; ++I)
|
|
|
|
if (!Ret->readsRegister(*I)) {
|
|
|
|
Ret->addOperand(MachineOperand::CreateReg(*I, false, true));
|
|
|
|
LastUseDef[*I] = std::make_pair(Ret, Ret->getNumOperands()-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, loop over the final use/def of each reg
|
|
|
|
// in the block and determine if it is dead.
|
2008-07-10 01:56:35 +00:00
|
|
|
for (DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
|
2008-07-08 22:24:50 +00:00
|
|
|
I = LastUseDef.begin(), E = LastUseDef.end(); I != E; ++I) {
|
|
|
|
MachineInstr* MI = I->second.first;
|
|
|
|
unsigned idx = I->second.second;
|
|
|
|
MachineOperand& MO = MI->getOperand(idx);
|
|
|
|
|
|
|
|
bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(MO.getReg());
|
|
|
|
|
|
|
|
// A crude approximation of "live-out" calculation
|
|
|
|
bool usedOutsideBlock = isPhysReg ? false :
|
|
|
|
UsedInMultipleBlocks.test(MO.getReg() -
|
|
|
|
TargetRegisterInfo::FirstVirtualRegister);
|
|
|
|
if (!isPhysReg && !usedOutsideBlock)
|
|
|
|
for (MachineRegisterInfo::reg_iterator UI = MRI.reg_begin(MO.getReg()),
|
|
|
|
UE = MRI.reg_end(); UI != UE; ++UI)
|
|
|
|
// Two cases:
|
|
|
|
// - used in another block
|
|
|
|
// - used in the same block before it is defined (loop)
|
|
|
|
if (UI->getParent() != &MBB ||
|
2008-07-08 23:36:37 +00:00
|
|
|
(MO.isDef() && UI.getOperand().isUse() && precedes(&*UI, MI))) {
|
2008-07-08 22:24:50 +00:00
|
|
|
UsedInMultipleBlocks.set(MO.getReg() -
|
|
|
|
TargetRegisterInfo::FirstVirtualRegister);
|
|
|
|
usedOutsideBlock = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Physical registers and those that are not live-out of the block
|
|
|
|
// are killed/dead at their last use/def within this block.
|
|
|
|
if (isPhysReg || !usedOutsideBlock) {
|
2008-10-04 00:31:14 +00:00
|
|
|
if (MO.isUse()) {
|
|
|
|
// Don't mark uses that are tied to defs as kills.
|
2009-03-19 20:30:06 +00:00
|
|
|
if (!MI->isRegTiedToDefOperand(idx))
|
2008-10-04 00:31:14 +00:00
|
|
|
MO.setIsKill(true);
|
|
|
|
} else
|
2008-07-08 22:24:50 +00:00
|
|
|
MO.setIsDead(true);
|
|
|
|
}
|
|
|
|
}
|
2008-07-09 20:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RALocal::AllocateBasicBlock(MachineBasicBlock &MBB) {
|
|
|
|
// loop over each instruction
|
|
|
|
MachineBasicBlock::iterator MII = MBB.begin();
|
|
|
|
|
|
|
|
DEBUG(const BasicBlock *LBB = MBB.getBasicBlock();
|
2009-07-25 00:23:56 +00:00
|
|
|
if (LBB) errs() << "\nStarting RegAlloc of BB: " << LBB->getName());
|
2008-07-09 20:14:53 +00:00
|
|
|
|
2009-01-29 18:37:30 +00:00
|
|
|
// Add live-in registers as active.
|
|
|
|
for (MachineBasicBlock::livein_iterator I = MBB.livein_begin(),
|
2008-07-09 20:14:53 +00:00
|
|
|
E = MBB.livein_end(); I != E; ++I) {
|
2009-01-29 18:37:30 +00:00
|
|
|
unsigned Reg = *I;
|
|
|
|
MF->getRegInfo().setPhysRegUsed(Reg);
|
|
|
|
PhysRegsUsed[Reg] = 0; // It is free and reserved now
|
|
|
|
AddToPhysRegsUseOrder(Reg);
|
|
|
|
for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
|
|
|
|
*SubRegs; ++SubRegs) {
|
|
|
|
if (PhysRegsUsed[*SubRegs] != -2) {
|
|
|
|
AddToPhysRegsUseOrder(*SubRegs);
|
|
|
|
PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
|
|
|
|
MF->getRegInfo().setPhysRegUsed(*SubRegs);
|
2008-07-09 20:14:53 +00:00
|
|
|
}
|
2009-01-29 18:37:30 +00:00
|
|
|
}
|
2008-07-09 20:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ComputeLocalLiveness(MBB);
|
2008-07-08 22:24:50 +00:00
|
|
|
|
2006-06-15 22:21:53 +00:00
|
|
|
// Otherwise, sequentially allocate each instruction in the MBB.
|
2005-11-09 18:22:42 +00:00
|
|
|
while (MII != MBB.end()) {
|
|
|
|
MachineInstr *MI = MII++;
|
2008-01-07 07:27:27 +00:00
|
|
|
const TargetInstrDesc &TID = MI->getDesc();
|
2006-11-17 02:09:07 +00:00
|
|
|
DEBUG(DOUT << "\nStarting RegAlloc of: " << *MI;
|
|
|
|
DOUT << " Regs have values: ";
|
2008-02-10 18:45:23 +00:00
|
|
|
for (unsigned i = 0; i != TRI->getNumRegs(); ++i)
|
2006-09-08 19:03:30 +00:00
|
|
|
if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
|
2008-02-26 21:47:57 +00:00
|
|
|
DOUT << "[" << TRI->getName(i)
|
2006-11-17 02:09:07 +00:00
|
|
|
<< ",%reg" << PhysRegsUsed[i] << "] ";
|
|
|
|
DOUT << "\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.
|
2006-07-21 21:15:20 +00:00
|
|
|
if (TID.ImplicitUses) {
|
|
|
|
for (const unsigned *ImplicitUses = TID.ImplicitUses;
|
|
|
|
*ImplicitUses; ++ImplicitUses)
|
|
|
|
MarkPhysRegRecentlyUsed(*ImplicitUses);
|
|
|
|
}
|
2002-12-16 16:15:28 +00:00
|
|
|
|
2006-11-15 20:55:15 +00:00
|
|
|
SmallVector<unsigned, 8> Kills;
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand& MO = MI->getOperand(i);
|
2008-10-03 15:45:36 +00:00
|
|
|
if (MO.isReg() && MO.isKill()) {
|
2007-06-26 21:05:13 +00:00
|
|
|
if (!MO.isImplicit())
|
|
|
|
Kills.push_back(MO.getReg());
|
|
|
|
else if (!isReadModWriteImplicitKill(MI, MO.getReg()))
|
|
|
|
// These are extra physical register kills when a sub-register
|
|
|
|
// is defined (def of a sub-register is a read/mod/write of the
|
|
|
|
// larger registers). Ignore.
|
|
|
|
Kills.push_back(MO.getReg());
|
|
|
|
}
|
2006-11-15 20:55:15 +00:00
|
|
|
}
|
|
|
|
|
2008-09-24 23:13:09 +00:00
|
|
|
// If any physical regs are earlyclobber, spill any value they might
|
|
|
|
// have in them, then mark them unallocatable.
|
|
|
|
// If any virtual regs are earlyclobber, allocate them now (before
|
|
|
|
// freeing inputs that are killed).
|
|
|
|
if (MI->getOpcode()==TargetInstrInfo::INLINEASM) {
|
|
|
|
for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
|
|
|
|
MachineOperand& MO = MI->getOperand(i);
|
2008-10-03 15:45:36 +00:00
|
|
|
if (MO.isReg() && MO.isDef() && MO.isEarlyClobber() &&
|
2008-09-24 23:13:09 +00:00
|
|
|
MO.getReg()) {
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
|
|
|
|
unsigned DestVirtReg = MO.getReg();
|
|
|
|
unsigned DestPhysReg;
|
|
|
|
|
|
|
|
// If DestVirtReg already has a value, use it.
|
|
|
|
if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
|
|
|
|
DestPhysReg = getReg(MBB, MI, DestVirtReg);
|
|
|
|
MF->getRegInfo().setPhysRegUsed(DestPhysReg);
|
|
|
|
markVirtRegModified(DestVirtReg);
|
|
|
|
getVirtRegLastUse(DestVirtReg) =
|
|
|
|
std::make_pair((MachineInstr*)0, 0);
|
|
|
|
DOUT << " Assigning " << TRI->getName(DestPhysReg)
|
|
|
|
<< " to %reg" << DestVirtReg << "\n";
|
|
|
|
MO.setReg(DestPhysReg); // Assign the earlyclobber register
|
|
|
|
} else {
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
|
|
|
|
// These are extra physical register defs when a sub-register
|
|
|
|
// is defined (def of a sub-register is a read/mod/write of the
|
|
|
|
// larger registers). Ignore.
|
|
|
|
if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
|
|
|
|
|
|
|
|
MF->getRegInfo().setPhysRegUsed(Reg);
|
|
|
|
spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
|
|
|
|
PhysRegsUsed[Reg] = 0; // It is free and reserved now
|
|
|
|
AddToPhysRegsUseOrder(Reg);
|
|
|
|
|
2009-01-29 02:20:59 +00:00
|
|
|
for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
|
|
|
|
*SubRegs; ++SubRegs) {
|
|
|
|
if (PhysRegsUsed[*SubRegs] != -2) {
|
|
|
|
MF->getRegInfo().setPhysRegUsed(*SubRegs);
|
|
|
|
PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
|
|
|
|
AddToPhysRegsUseOrder(*SubRegs);
|
2008-09-24 23:13:09 +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
|
|
|
//
|
2009-01-29 02:20:59 +00:00
|
|
|
SmallSet<unsigned, 4> ReloadedRegs;
|
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)
|
2008-10-03 15:45:36 +00:00
|
|
|
if (MO.isReg() && !MO.isDef() && MO.getReg() && !MO.isImplicit() &&
|
2008-02-10 18:45:23 +00:00
|
|
|
TargetRegisterInfo::isVirtualRegister(MO.getReg()))
|
2009-01-29 02:20:59 +00:00
|
|
|
MI = reloadVirtReg(MBB, MI, i, ReloadedRegs);
|
2004-02-26 22:00:20 +00:00
|
|
|
}
|
2004-02-13 18:20:47 +00:00
|
|
|
|
2006-11-15 20:55:15 +00:00
|
|
|
// If this instruction is the last user of this register, kill the
|
2004-02-17 17:49:10 +00:00
|
|
|
// value, freeing the register being used, so it doesn't need to be
|
|
|
|
// spilled to memory.
|
|
|
|
//
|
2006-11-15 20:55:15 +00:00
|
|
|
for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
|
|
|
|
unsigned VirtReg = Kills[i];
|
2004-02-17 17:49:10 +00:00
|
|
|
unsigned PhysReg = VirtReg;
|
2008-02-10 18:45:23 +00:00
|
|
|
if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
|
2004-02-17 17:49:10 +00:00
|
|
|
// 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;
|
2006-09-08 20:21:31 +00:00
|
|
|
} else if (PhysRegsUsed[PhysReg] == -2) {
|
|
|
|
// Unallocatable register dead, ignore.
|
|
|
|
continue;
|
2007-06-26 21:05:13 +00:00
|
|
|
} else {
|
2007-10-22 19:42:28 +00:00
|
|
|
assert((!PhysRegsUsed[PhysReg] || PhysRegsUsed[PhysReg] == -1) &&
|
2007-06-26 21:05:13 +00:00
|
|
|
"Silently clearing a virtual register?");
|
2004-02-17 17:49:10 +00:00
|
|
|
}
|
2002-12-16 16:15:28 +00:00
|
|
|
|
2004-02-17 17:49:10 +00:00
|
|
|
if (PhysReg) {
|
2008-02-26 21:47:57 +00:00
|
|
|
DOUT << " Last use of " << TRI->getName(PhysReg)
|
2006-11-17 02:09:07 +00:00
|
|
|
<< "[%reg" << VirtReg <<"], removing it from live set\n";
|
2004-02-17 17:49:10 +00:00
|
|
|
removePhysReg(PhysReg);
|
2009-01-29 02:20:59 +00:00
|
|
|
for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
|
|
|
|
*SubRegs; ++SubRegs) {
|
|
|
|
if (PhysRegsUsed[*SubRegs] != -2) {
|
2006-11-17 02:09:07 +00:00
|
|
|
DOUT << " Last use of "
|
2009-01-29 02:20:59 +00:00
|
|
|
<< TRI->getName(*SubRegs)
|
2006-11-17 02:09:07 +00:00
|
|
|
<< "[%reg" << VirtReg <<"], removing it from live set\n";
|
2009-01-29 02:20:59 +00:00
|
|
|
removePhysReg(*SubRegs);
|
2006-11-15 20:55:15 +00:00
|
|
|
}
|
|
|
|
}
|
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);
|
2008-10-03 15:45:36 +00:00
|
|
|
if (MO.isReg() && MO.isDef() && !MO.isImplicit() && MO.getReg() &&
|
2008-09-24 23:13:09 +00:00
|
|
|
!MO.isEarlyClobber() &&
|
2008-02-10 18:45:23 +00:00
|
|
|
TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
|
2004-02-26 22:00:20 +00:00
|
|
|
unsigned Reg = MO.getReg();
|
2006-09-08 19:11:11 +00:00
|
|
|
if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
|
2007-06-26 21:05:13 +00:00
|
|
|
// These are extra physical register defs when a sub-register
|
|
|
|
// is defined (def of a sub-register is a read/mod/write of the
|
|
|
|
// larger registers). Ignore.
|
|
|
|
if (isReadModWriteImplicitDef(MI, MO.getReg())) continue;
|
|
|
|
|
2007-12-31 04:13:23 +00:00
|
|
|
MF->getRegInfo().setPhysRegUsed(Reg);
|
2006-11-15 20:55:15 +00:00
|
|
|
spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
|
2002-12-24 00:04:55 +00:00
|
|
|
PhysRegsUsed[Reg] = 0; // It is free and reserved now
|
2007-06-26 21:05:13 +00:00
|
|
|
AddToPhysRegsUseOrder(Reg);
|
|
|
|
|
2009-01-29 02:20:59 +00:00
|
|
|
for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
|
|
|
|
*SubRegs; ++SubRegs) {
|
|
|
|
if (PhysRegsUsed[*SubRegs] != -2) {
|
|
|
|
MF->getRegInfo().setPhysRegUsed(*SubRegs);
|
|
|
|
PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
|
|
|
|
AddToPhysRegsUseOrder(*SubRegs);
|
2006-09-08 19:03:30 +00:00
|
|
|
}
|
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.
|
2006-07-21 21:15:20 +00:00
|
|
|
if (TID.ImplicitDefs) {
|
|
|
|
for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
|
|
|
|
*ImplicitDefs; ++ImplicitDefs) {
|
|
|
|
unsigned Reg = *ImplicitDefs;
|
2007-06-26 21:05:13 +00:00
|
|
|
if (PhysRegsUsed[Reg] != -2) {
|
2006-09-19 18:02:01 +00:00
|
|
|
spillPhysReg(MBB, MI, Reg, true);
|
2007-06-26 21:05:13 +00:00
|
|
|
AddToPhysRegsUseOrder(Reg);
|
2006-09-19 18:02:01 +00:00
|
|
|
PhysRegsUsed[Reg] = 0; // It is free and reserved now
|
|
|
|
}
|
2007-12-31 04:13:23 +00:00
|
|
|
MF->getRegInfo().setPhysRegUsed(Reg);
|
2009-01-29 02:20:59 +00:00
|
|
|
for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
|
|
|
|
*SubRegs; ++SubRegs) {
|
|
|
|
if (PhysRegsUsed[*SubRegs] != -2) {
|
|
|
|
AddToPhysRegsUseOrder(*SubRegs);
|
|
|
|
PhysRegsUsed[*SubRegs] = 0; // It is free and reserved now
|
|
|
|
MF->getRegInfo().setPhysRegUsed(*SubRegs);
|
2006-09-08 19:03:30 +00:00
|
|
|
}
|
2006-07-21 21:15:20 +00:00
|
|
|
}
|
2004-01-13 06:24:30 +00:00
|
|
|
}
|
2003-12-13 01:20:58 +00:00
|
|
|
}
|
2002-12-16 16:15:28 +00:00
|
|
|
|
2006-11-15 20:55:15 +00:00
|
|
|
SmallVector<unsigned, 8> DeadDefs;
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand& MO = MI->getOperand(i);
|
2008-10-03 15:45:36 +00:00
|
|
|
if (MO.isReg() && MO.isDead())
|
2006-11-15 20:55:15 +00:00
|
|
|
DeadDefs.push_back(MO.getReg());
|
|
|
|
}
|
|
|
|
|
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);
|
2008-10-03 15:45:36 +00:00
|
|
|
if (MO.isReg() && MO.isDef() && MO.getReg() &&
|
2008-09-24 23:13:09 +00:00
|
|
|
!MO.isEarlyClobber() &&
|
2008-02-10 18:45:23 +00:00
|
|
|
TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
|
2004-02-26 22:00:20 +00:00
|
|
|
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);
|
2007-12-31 04:13:23 +00:00
|
|
|
MF->getRegInfo().setPhysRegUsed(DestPhysReg);
|
2003-05-12 03:54:14 +00:00
|
|
|
markVirtRegModified(DestVirtReg);
|
2008-01-17 02:08:17 +00:00
|
|
|
getVirtRegLastUse(DestVirtReg) = std::make_pair((MachineInstr*)0, 0);
|
2008-02-26 21:47:57 +00:00
|
|
|
DOUT << " Assigning " << TRI->getName(DestPhysReg)
|
2008-02-22 19:57:06 +00:00
|
|
|
<< " to %reg" << DestVirtReg << "\n";
|
2008-07-09 20:12:26 +00:00
|
|
|
MO.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.
|
|
|
|
//
|
2006-11-15 20:55:15 +00:00
|
|
|
for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) {
|
|
|
|
unsigned VirtReg = DeadDefs[i];
|
2004-02-17 17:49:10 +00:00
|
|
|
unsigned PhysReg = VirtReg;
|
2008-02-10 18:45:23 +00:00
|
|
|
if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
|
2004-02-17 17:49:10 +00:00
|
|
|
unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
|
|
|
|
PhysReg = PhysRegSlot;
|
|
|
|
assert(PhysReg != 0);
|
|
|
|
PhysRegSlot = 0;
|
2006-09-08 20:21:31 +00:00
|
|
|
} else if (PhysRegsUsed[PhysReg] == -2) {
|
|
|
|
// Unallocatable register dead, ignore.
|
|
|
|
continue;
|
2004-02-17 17:49:10 +00:00
|
|
|
}
|
2003-01-13 00:25:40 +00:00
|
|
|
|
2004-02-17 17:49:10 +00:00
|
|
|
if (PhysReg) {
|
2008-02-26 21:47:57 +00:00
|
|
|
DOUT << " Register " << TRI->getName(PhysReg)
|
2004-02-17 17:49:10 +00:00
|
|
|
<< " [%reg" << VirtReg
|
2008-11-24 16:01:21 +00:00
|
|
|
<< "] is never used, removing it from live set\n";
|
2004-02-17 17:49:10 +00:00
|
|
|
removePhysReg(PhysReg);
|
2008-02-10 18:45:23 +00:00
|
|
|
for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
|
2006-11-15 20:55:15 +00:00
|
|
|
*AliasSet; ++AliasSet) {
|
|
|
|
if (PhysRegsUsed[*AliasSet] != -2) {
|
2008-02-26 21:47:57 +00:00
|
|
|
DOUT << " Register " << TRI->getName(*AliasSet)
|
2006-11-15 20:55:15 +00:00
|
|
|
<< " [%reg" << *AliasSet
|
2008-11-24 16:01:21 +00:00
|
|
|
<< "] is never used, removing it from live set\n";
|
2006-11-15 20:55:15 +00:00
|
|
|
removePhysReg(*AliasSet);
|
|
|
|
}
|
|
|
|
}
|
2002-12-18 08:14:26 +00:00
|
|
|
}
|
|
|
|
}
|
2005-11-09 18:22:42 +00:00
|
|
|
|
Fix pr4100. Do not remove no-op copies when they are dead. The register
scavenger gets confused about register liveness if it doesn't see them.
I'm not thrilled with this solution, but it only comes up when there are dead
copies in the code, which is something that hopefully doesn't happen much.
Here is what happens in pr4100: As shown in the following excerpt from the
debug output of llc, the source of a move gets reloaded from the stack,
inserting a new load instruction before the move. Since that source operand
is a kill, the physical register is free to be reused for the destination
of the move. The move ends up being a no-op, copying R3 to R3, so it is
deleted. But, it leaves behind the load to reload %reg1028 into R3, and
that load is not updated to show that it's destination operand (R3) is dead.
The scavenger gets confused by that load because it thinks that R3 is live.
Starting RegAlloc of: %reg1025<def,dead> = MOVr %reg1028<kill>, 14, %reg0, %reg0
Regs have values:
Reloading %reg1028 into R3
Last use of R3[%reg1028], removing it from live set
Assigning R3 to %reg1025
Register R3 [%reg1025] is never used, removing it from live set
Alternative solutions might be either marking the load as dead, or zapping
the load along with the no-op copy. I couldn't see an easy way to do
either of those, though.
llvm-svn: 71196
2009-05-07 23:47:03 +00:00
|
|
|
// Finally, if this is a noop copy instruction, zap it. (Except that if
|
|
|
|
// the copy is dead, it must be kept to avoid messing up liveness info for
|
|
|
|
// the register scavenger. See pr4100.)
|
2009-01-20 19:12:24 +00:00
|
|
|
unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
|
|
|
|
if (TII->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
|
Fix pr4100. Do not remove no-op copies when they are dead. The register
scavenger gets confused about register liveness if it doesn't see them.
I'm not thrilled with this solution, but it only comes up when there are dead
copies in the code, which is something that hopefully doesn't happen much.
Here is what happens in pr4100: As shown in the following excerpt from the
debug output of llc, the source of a move gets reloaded from the stack,
inserting a new load instruction before the move. Since that source operand
is a kill, the physical register is free to be reused for the destination
of the move. The move ends up being a no-op, copying R3 to R3, so it is
deleted. But, it leaves behind the load to reload %reg1028 into R3, and
that load is not updated to show that it's destination operand (R3) is dead.
The scavenger gets confused by that load because it thinks that R3 is live.
Starting RegAlloc of: %reg1025<def,dead> = MOVr %reg1028<kill>, 14, %reg0, %reg0
Regs have values:
Reloading %reg1028 into R3
Last use of R3[%reg1028], removing it from live set
Assigning R3 to %reg1025
Register R3 [%reg1025] is never used, removing it from live set
Alternative solutions might be either marking the load as dead, or zapping
the load along with the no-op copy. I couldn't see an easy way to do
either of those, though.
llvm-svn: 71196
2009-05-07 23:47:03 +00:00
|
|
|
SrcReg == DstReg && DeadDefs.empty())
|
2005-11-09 18:22:42 +00:00
|
|
|
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.
|
2008-02-10 18:45:23 +00:00
|
|
|
for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
|
2008-02-20 12:07:57 +00:00
|
|
|
if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2) {
|
2004-02-09 01:26:13 +00:00
|
|
|
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);
|
2008-02-20 12:07:57 +00:00
|
|
|
}
|
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;
|
2008-02-10 18:45:23 +00:00
|
|
|
for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
|
2007-12-31 04:13:23 +00:00
|
|
|
e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
|
2004-02-09 02:12:04 +00:00
|
|
|
if (unsigned PR = Virt2PhysRegMap[i]) {
|
2006-12-07 20:04:42 +00:00
|
|
|
cerr << "Register still mapped: " << i << " -> " << PR << "\n";
|
2004-02-09 02:12:04 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/// runOnMachineFunction - Register allocate the whole function
|
|
|
|
///
|
2007-05-08 19:02:46 +00:00
|
|
|
bool RALocal::runOnMachineFunction(MachineFunction &Fn) {
|
2006-11-17 02:09:07 +00:00
|
|
|
DOUT << "Machine Function " << "\n";
|
2002-12-16 16:15:28 +00:00
|
|
|
MF = &Fn;
|
2002-12-28 20:40:43 +00:00
|
|
|
TM = &Fn.getTarget();
|
2008-02-10 18:45:23 +00:00
|
|
|
TRI = TM->getRegisterInfo();
|
2008-01-07 01:35:56 +00:00
|
|
|
TII = TM->getInstrInfo();
|
2002-12-16 16:15:28 +00:00
|
|
|
|
2008-02-10 18:45:23 +00:00
|
|
|
PhysRegsUsed.assign(TRI->getNumRegs(), -1);
|
2006-09-08 19:03:30 +00:00
|
|
|
|
|
|
|
// At various places we want to efficiently check to see whether a register
|
|
|
|
// is allocatable. To handle this, we mark all unallocatable registers as
|
|
|
|
// being pinned down, permanently.
|
|
|
|
{
|
2008-02-10 18:45:23 +00:00
|
|
|
BitVector Allocable = TRI->getAllocatableSet(Fn);
|
2006-09-08 19:03:30 +00:00
|
|
|
for (unsigned i = 0, e = Allocable.size(); i != e; ++i)
|
|
|
|
if (!Allocable[i])
|
|
|
|
PhysRegsUsed[i] = -2; // Mark the reg unallocable.
|
|
|
|
}
|
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
|
2008-01-17 00:35:26 +00:00
|
|
|
unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg();
|
2008-07-10 18:23:23 +00:00
|
|
|
StackSlotForVirtReg.grow(LastVirtReg);
|
2008-01-17 00:35:26 +00:00
|
|
|
Virt2PhysRegMap.grow(LastVirtReg);
|
2008-01-17 02:08:17 +00:00
|
|
|
Virt2LastUseMap.grow(LastVirtReg);
|
2008-02-10 18:45:23 +00:00
|
|
|
VirtRegModified.resize(LastVirtReg+1-TargetRegisterInfo::FirstVirtualRegister);
|
2008-07-08 22:24:50 +00:00
|
|
|
UsedInMultipleBlocks.resize(LastVirtReg+1-TargetRegisterInfo::FirstVirtualRegister);
|
|
|
|
|
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();
|
2008-07-08 22:24:50 +00:00
|
|
|
UsedInMultipleBlocks.clear();
|
2004-02-09 02:12:04 +00:00
|
|
|
Virt2PhysRegMap.clear();
|
2008-01-17 02:08:17 +00:00
|
|
|
Virt2LastUseMap.clear();
|
2002-12-16 16:15:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-01-31 21:27:19 +00:00
|
|
|
FunctionPass *llvm::createLocalRegisterAllocator() {
|
2007-05-08 19:02:46 +00:00
|
|
|
return new RALocal();
|
2002-12-16 16:15:28 +00:00
|
|
|
}
|