2002-12-17 00:15:28 +08:00
|
|
|
//===-- RegAllocLocal.cpp - A BasicBlock generic register allocator -------===//
|
2004-02-14 02:20:47 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2004-02-14 02:20:47 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-17 00:15:28 +08: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-04 05:47:31 +08:00
|
|
|
#define DEBUG_TYPE "regalloc"
|
2006-11-16 04:55:15 +08:00
|
|
|
#include "llvm/BasicBlock.h"
|
2002-12-29 04:40:43 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2002-12-17 00:15:28 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2002-12-29 05:08:26 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2007-12-31 12:13:23 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2008-02-06 16:00:32 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2006-08-02 20:30:23 +08:00
|
|
|
#include "llvm/CodeGen/RegAllocRegistry.h"
|
2003-01-15 06:00:31 +08:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2002-12-17 00:15:28 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 21:10:19 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-07-10 09:56:35 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2007-02-01 13:32:05 +08:00
|
|
|
#include "llvm/ADT/IndexedMap.h"
|
2009-01-29 10:20:59 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2006-11-16 04:55:15 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2008-02-07 03:16:53 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2004-10-26 23:35:58 +08:00
|
|
|
#include <algorithm>
|
2004-02-01 05:27:19 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2006-12-20 06:41:21 +08:00
|
|
|
STATISTIC(NumStores, "Number of stores added");
|
|
|
|
STATISTIC(NumLoads , "Number of loads added");
|
2006-08-01 22:21:23 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
static RegisterRegAlloc
|
2008-10-15 04:25:08 +08:00
|
|
|
localRegAlloc("local", "local register allocator",
|
2008-05-13 08:00:25 +08:00
|
|
|
createLocalRegisterAllocator);
|
2006-08-01 22:21:23 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
namespace {
|
2009-10-25 14:33:48 +08:00
|
|
|
class RALocal : public MachineFunctionPass {
|
2007-05-02 05:15:47 +08:00
|
|
|
public:
|
2007-05-03 09:11:54 +08:00
|
|
|
static char ID;
|
2008-09-05 01:05:41 +08:00
|
|
|
RALocal() : MachineFunctionPass(&ID), StackSlotForVirtReg(-1) {}
|
2007-05-02 05:15:47 +08:00
|
|
|
private:
|
2002-12-29 04:40:43 +08:00
|
|
|
const TargetMachine *TM;
|
2002-12-17 00:15:28 +08:00
|
|
|
MachineFunction *MF;
|
2008-02-11 02:45:23 +08:00
|
|
|
const TargetRegisterInfo *TRI;
|
2008-01-07 09:35:56 +08:00
|
|
|
const TargetInstrInfo *TII;
|
2002-12-25 13:05:46 +08:00
|
|
|
|
2003-08-05 07:36:39 +08:00
|
|
|
// StackSlotForVirtReg - Maps virtual regs to the frame index where these
|
|
|
|
// values are spilled.
|
2008-07-11 02:23:23 +08:00
|
|
|
IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
|
2002-12-17 00:15:28 +08:00
|
|
|
|
|
|
|
// Virt2PhysRegMap - This map contains entries for each virtual register
|
2004-02-26 05:55:45 +08:00
|
|
|
// that is currently available in a physical register.
|
2007-02-01 13:32:05 +08:00
|
|
|
IndexedMap<unsigned, VirtReg2IndexFunctor> Virt2PhysRegMap;
|
2004-02-09 10:12:04 +08:00
|
|
|
|
|
|
|
unsigned &getVirt2PhysRegMapSlot(unsigned VirtReg) {
|
2004-02-26 05:55:45 +08:00
|
|
|
return Virt2PhysRegMap[VirtReg];
|
2004-02-09 10:12:04 +08:00
|
|
|
}
|
2004-02-14 02:20:47 +08:00
|
|
|
|
2004-02-09 09:26:13 +08: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-09 03:03:30 +08: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-17 00:15:28 +08:00
|
|
|
//
|
2004-02-14 02:20:47 +08:00
|
|
|
std::vector<int> PhysRegsUsed;
|
2002-12-17 00:15:28 +08: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 10:08:17 +08: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-11 02:45:23 +08:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
|
2008-01-17 10:08:17 +08:00
|
|
|
return Virt2LastUseMap[Reg];
|
|
|
|
}
|
|
|
|
|
2003-01-13 08:25:40 +08: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 16:14:26 +08:00
|
|
|
//
|
2008-01-17 08:35:26 +08:00
|
|
|
BitVector VirtRegModified;
|
2008-07-09 06:24:50 +08:00
|
|
|
|
|
|
|
// UsedInMultipleBlocks - Tracks whether a particular register is used in
|
|
|
|
// more than one block.
|
|
|
|
BitVector UsedInMultipleBlocks;
|
2003-01-13 08:25:40 +08:00
|
|
|
|
|
|
|
void markVirtRegModified(unsigned Reg, bool Val = true) {
|
2008-02-11 02:45:23 +08:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
|
|
|
|
Reg -= TargetRegisterInfo::FirstVirtualRegister;
|
2008-01-17 08:35:26 +08:00
|
|
|
if (Val)
|
|
|
|
VirtRegModified.set(Reg);
|
|
|
|
else
|
|
|
|
VirtRegModified.reset(Reg);
|
2003-01-13 08:25:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isVirtRegModified(unsigned Reg) const {
|
2008-02-11 02:45:23 +08:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(Reg) && "Illegal VirtReg!");
|
|
|
|
assert(Reg - TargetRegisterInfo::FirstVirtualRegister < VirtRegModified.size()
|
2004-02-14 02:20:47 +08:00
|
|
|
&& "Illegal virtual register!");
|
2008-02-11 02:45:23 +08:00
|
|
|
return VirtRegModified[Reg - TargetRegisterInfo::FirstVirtualRegister];
|
2003-01-13 08:25:40 +08:00
|
|
|
}
|
2002-12-18 16:14:26 +08:00
|
|
|
|
2007-06-27 05:05:13 +08: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-17 00:15:28 +08:00
|
|
|
void MarkPhysRegRecentlyUsed(unsigned Reg) {
|
2006-09-03 15:15:37 +08:00
|
|
|
if (PhysRegsUseOrder.empty() ||
|
|
|
|
PhysRegsUseOrder.back() == Reg) return; // Already most recently used
|
2002-12-24 08:04:55 +08:00
|
|
|
|
|
|
|
for (unsigned i = PhysRegsUseOrder.size(); i != 0; --i)
|
2004-02-14 02:20:47 +08: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-17 00:15:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "Local Register Allocator";
|
|
|
|
}
|
|
|
|
|
2003-01-13 08:25:40 +08:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2009-08-01 07:37:33 +08:00
|
|
|
AU.setPreservesCFG();
|
2003-01-13 08:25:40 +08:00
|
|
|
AU.addRequiredID(PHIEliminationID);
|
2003-12-19 06:40:24 +08:00
|
|
|
AU.addRequiredID(TwoAddressInstructionPassID);
|
2003-01-13 08:25:40 +08:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2002-12-17 00:15:28 +08: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 16:14:26 +08: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-11 02:45:23 +08:00
|
|
|
for (const unsigned *AliasSet = TRI->getAliasSet(R2);
|
2003-10-08 13:20:08 +08:00
|
|
|
*AliasSet; ++AliasSet) {
|
|
|
|
if (*AliasSet == R1) return true;
|
|
|
|
}
|
2002-12-18 16:14:26 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-12-29 04:40:43 +08:00
|
|
|
/// getStackSpaceFor - This returns the frame index of the specified virtual
|
2003-08-05 07:36:39 +08:00
|
|
|
/// register on the stack, allocating space if necessary.
|
2002-12-29 04:40:43 +08:00
|
|
|
int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
|
2002-12-17 00:15:28 +08:00
|
|
|
|
2003-08-05 07:36:39 +08:00
|
|
|
/// removePhysReg - This method marks the specified physical register as no
|
|
|
|
/// longer being in use.
|
|
|
|
///
|
2002-12-18 16:14:26 +08:00
|
|
|
void removePhysReg(unsigned PhysReg);
|
2002-12-17 00:15:28 +08: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-23 03:08:15 +08:00
|
|
|
void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
|
2002-12-17 00:15:28 +08:00
|
|
|
unsigned VirtReg, unsigned PhysReg);
|
|
|
|
|
2002-12-17 01:44:42 +08:00
|
|
|
/// spillPhysReg - This method spills the specified physical register into
|
2003-08-18 02:01:15 +08: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 08:25:40 +08:00
|
|
|
///
|
2004-02-17 11:57:19 +08:00
|
|
|
void spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
|
2003-08-18 02:01:15 +08:00
|
|
|
unsigned PhysReg, bool OnlyVirtRegs = false);
|
2002-12-17 01:44:42 +08:00
|
|
|
|
2003-01-13 08:25:40 +08: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 10:50:10 +08: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 16:14:26 +08:00
|
|
|
bool isPhysRegAvailable(unsigned PhysReg) const;
|
2003-01-13 08:25:40 +08: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-14 02:20:47 +08:00
|
|
|
|
2003-01-13 08:25:40 +08:00
|
|
|
/// getReg - Find a physical register to hold the specified virtual
|
2002-12-17 00:15:28 +08: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 09:13:00 +08:00
|
|
|
/// register. If NoFree is true, that means the caller knows there isn't
|
|
|
|
/// a free register, do not call getFreeReg().
|
2004-02-17 11:57:19 +08:00
|
|
|
unsigned getReg(MachineBasicBlock &MBB, MachineInstr *MI,
|
2009-01-29 09:13:00 +08:00
|
|
|
unsigned VirtReg, bool NoFree = false);
|
2002-12-17 00:15:28 +08:00
|
|
|
|
2009-05-08 05:19:45 +08:00
|
|
|
/// reloadVirtReg - This method transforms the specified virtual
|
2004-02-17 11:57:19 +08: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-17 00:15:28 +08:00
|
|
|
///
|
2004-02-17 11:57:19 +08:00
|
|
|
MachineInstr *reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
|
2009-01-29 10:20:59 +08:00
|
|
|
unsigned OpNum, SmallSet<unsigned, 4> &RRegs);
|
2005-04-22 06:36:52 +08:00
|
|
|
|
2008-07-10 04:14:53 +08:00
|
|
|
/// ComputeLocalLiveness - Computes liveness of registers within a basic
|
|
|
|
/// block, setting the killed/dead flags as appropriate.
|
|
|
|
void ComputeLocalLiveness(MachineBasicBlock& MBB);
|
2003-08-05 07:36:39 +08:00
|
|
|
|
|
|
|
void reloadPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
|
|
|
|
unsigned PhysReg);
|
2002-12-17 00:15:28 +08:00
|
|
|
};
|
2007-05-09 03:02:46 +08:00
|
|
|
char RALocal::ID = 0;
|
2002-12-17 00:15:28 +08:00
|
|
|
}
|
|
|
|
|
2003-08-05 07:36:39 +08:00
|
|
|
/// getStackSpaceFor - This allocates space for the specified virtual register
|
|
|
|
/// to be held on the stack.
|
2007-05-09 03:02:46 +08:00
|
|
|
int RALocal::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
|
2003-08-05 07:36:39 +08:00
|
|
|
// Find the location Reg would belong...
|
2008-07-11 02:23:23 +08:00
|
|
|
int SS = StackSlotForVirtReg[VirtReg];
|
|
|
|
if (SS != -1)
|
|
|
|
return SS; // Already has space allocated?
|
2002-12-17 00:15:28 +08:00
|
|
|
|
2002-12-29 04:40:43 +08:00
|
|
|
// Allocate a new stack object for this spill location...
|
2004-08-16 06:02:22 +08:00
|
|
|
int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
|
2009-10-17 17:20:14 +08:00
|
|
|
RC->getAlignment(),true);
|
2002-12-17 00:15:28 +08:00
|
|
|
|
|
|
|
// Assign the slot...
|
2008-07-11 02:23:23 +08:00
|
|
|
StackSlotForVirtReg[VirtReg] = FrameIdx;
|
2002-12-29 04:40:43 +08:00
|
|
|
return FrameIdx;
|
2002-12-17 00:15:28 +08:00
|
|
|
}
|
|
|
|
|
2002-12-17 10:50:10 +08:00
|
|
|
|
2004-02-14 02:20:47 +08:00
|
|
|
/// removePhysReg - This method marks the specified physical register as no
|
2002-12-18 16:14:26 +08:00
|
|
|
/// longer being in use.
|
|
|
|
///
|
2007-05-09 03:02:46 +08:00
|
|
|
void RALocal::removePhysReg(unsigned PhysReg) {
|
2004-02-09 09:26:13 +08:00
|
|
|
PhysRegsUsed[PhysReg] = -1; // PhyReg no longer used
|
2002-12-18 16:14:26 +08:00
|
|
|
|
|
|
|
std::vector<unsigned>::iterator It =
|
|
|
|
std::find(PhysRegsUseOrder.begin(), PhysRegsUseOrder.end(), PhysReg);
|
2004-01-13 14:24:30 +08:00
|
|
|
if (It != PhysRegsUseOrder.end())
|
|
|
|
PhysRegsUseOrder.erase(It);
|
2002-12-18 16:14:26 +08:00
|
|
|
}
|
|
|
|
|
2003-01-13 08:25:40 +08:00
|
|
|
|
2002-12-17 00:15:28 +08: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-09 03:02:46 +08:00
|
|
|
void RALocal::spillVirtReg(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I,
|
|
|
|
unsigned VirtReg, unsigned PhysReg) {
|
2003-08-05 12:13:58 +08:00
|
|
|
assert(VirtReg && "Spilling a physical register is illegal!"
|
2003-08-05 08:49:09 +08:00
|
|
|
" Must not have appropriate kill for the register or use exists beyond"
|
|
|
|
" the intended one.");
|
2009-08-23 04:38:09 +08:00
|
|
|
DEBUG(errs() << " Spilling register " << TRI->getName(PhysReg)
|
|
|
|
<< " containing %reg" << VirtReg);
|
2008-01-02 05:11:32 +08:00
|
|
|
|
2008-01-17 10:08:17 +08:00
|
|
|
if (!isVirtRegModified(VirtReg)) {
|
2009-08-23 04:38:09 +08:00
|
|
|
DEBUG(errs() << " which has not been modified, so no store necessary!");
|
2008-01-17 10:08:17 +08:00
|
|
|
std::pair<MachineInstr*, unsigned> &LastUse = getVirtRegLastUse(VirtReg);
|
|
|
|
if (LastUse.first)
|
|
|
|
LastUse.first->getOperand(LastUse.second).setIsKill();
|
2008-02-07 03:16:53 +08: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 12:13:23 +08:00
|
|
|
const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
|
2003-08-05 08:49:09 +08:00
|
|
|
int FrameIndex = getStackSpaceFor(VirtReg, RC);
|
2009-08-23 04:38:09 +08:00
|
|
|
DEBUG(errs() << " to stack slot #" << FrameIndex);
|
2008-02-07 03:16:53 +08: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 08:59:57 +08:00
|
|
|
bool isKill = !(I != MBB.end() && I->readsRegister(PhysReg));
|
2008-02-11 16:30:52 +08:00
|
|
|
TII->storeRegToStackSlot(MBB, I, PhysReg, isKill, FrameIndex, RC);
|
2004-02-19 14:19:09 +08:00
|
|
|
++NumStores; // Update statistics
|
2002-12-17 00:15:28 +08:00
|
|
|
}
|
2004-02-09 10:12:04 +08:00
|
|
|
|
|
|
|
getVirt2PhysRegMapSlot(VirtReg) = 0; // VirtReg no longer available
|
2002-12-17 00:15:28 +08:00
|
|
|
|
2009-08-23 04:38:09 +08:00
|
|
|
DEBUG(errs() << '\n');
|
2002-12-18 16:14:26 +08:00
|
|
|
removePhysReg(PhysReg);
|
2002-12-17 00:15:28 +08:00
|
|
|
}
|
|
|
|
|
2002-12-17 10:50:10 +08:00
|
|
|
|
2003-01-13 08:25:40 +08:00
|
|
|
/// spillPhysReg - This method spills the specified physical register into the
|
2003-08-18 02:01:15 +08: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 08:25:40 +08:00
|
|
|
///
|
2007-05-09 03:02:46 +08:00
|
|
|
void RALocal::spillPhysReg(MachineBasicBlock &MBB, MachineInstr *I,
|
|
|
|
unsigned PhysReg, bool OnlyVirtRegs) {
|
2004-02-09 09:26:13 +08:00
|
|
|
if (PhysRegsUsed[PhysReg] != -1) { // Only spill it if it's used!
|
2006-09-09 03:03:30 +08:00
|
|
|
assert(PhysRegsUsed[PhysReg] != -2 && "Non allocable reg used!");
|
2004-02-09 09:26:13 +08:00
|
|
|
if (PhysRegsUsed[PhysReg] || !OnlyVirtRegs)
|
|
|
|
spillVirtReg(MBB, I, PhysRegsUsed[PhysReg], PhysReg);
|
2003-10-08 13:20:08 +08:00
|
|
|
} else {
|
2003-01-13 08:25:40 +08:00
|
|
|
// If the selected register aliases any other registers, we must make
|
2006-09-09 03:03:30 +08:00
|
|
|
// sure that one of the aliases isn't alive.
|
2008-02-11 02:45:23 +08:00
|
|
|
for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
|
2004-02-09 09:26:13 +08:00
|
|
|
*AliasSet; ++AliasSet)
|
2006-09-09 03:03:30 +08:00
|
|
|
if (PhysRegsUsed[*AliasSet] != -1 && // Spill aliased register.
|
|
|
|
PhysRegsUsed[*AliasSet] != -2) // If allocatable.
|
2007-06-27 05:05:13 +08:00
|
|
|
if (PhysRegsUsed[*AliasSet])
|
|
|
|
spillVirtReg(MBB, I, PhysRegsUsed[*AliasSet], *AliasSet);
|
2003-01-13 08:25:40 +08: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-09 03:02:46 +08:00
|
|
|
void RALocal::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
|
2004-02-09 09:26:13 +08:00
|
|
|
assert(PhysRegsUsed[PhysReg] == -1 && "Phys reg already assigned!");
|
2003-01-13 08:25:40 +08:00
|
|
|
// Update information to note the fact that this register was just used, and
|
|
|
|
// it holds VirtReg.
|
|
|
|
PhysRegsUsed[PhysReg] = VirtReg;
|
2004-02-14 02:20:47 +08:00
|
|
|
getVirt2PhysRegMapSlot(VirtReg) = PhysReg;
|
2007-06-27 05:05:13 +08:00
|
|
|
AddToPhysRegsUseOrder(PhysReg); // New use of PhysReg
|
2003-01-13 08:25:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-17 10:50:10 +08: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-09 03:02:46 +08:00
|
|
|
bool RALocal::isPhysRegAvailable(unsigned PhysReg) const {
|
2004-02-09 09:26:13 +08:00
|
|
|
if (PhysRegsUsed[PhysReg] != -1) return false;
|
2002-12-17 10:50:10 +08:00
|
|
|
|
|
|
|
// If the selected register aliases any other allocated registers, it is
|
|
|
|
// not free!
|
2008-02-11 02:45:23 +08:00
|
|
|
for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
|
2003-10-08 13:20:08 +08:00
|
|
|
*AliasSet; ++AliasSet)
|
2008-02-23 04:30:53 +08:00
|
|
|
if (PhysRegsUsed[*AliasSet] >= 0) // Aliased register in use?
|
2003-10-08 13:20:08 +08:00
|
|
|
return false; // Can't use this reg then.
|
2002-12-17 10:50:10 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-13 08:25:40 +08:00
|
|
|
/// getFreeReg - Look to see if there is a free register available in the
|
|
|
|
/// specified register class. If not, return 0.
|
2002-12-17 00:15:28 +08:00
|
|
|
///
|
2007-05-09 03:02:46 +08:00
|
|
|
unsigned RALocal::getFreeReg(const TargetRegisterClass *RC) {
|
2002-12-29 04:40:43 +08: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 10:50:10 +08:00
|
|
|
|
2003-01-13 08:25:40 +08: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-09 03:02:46 +08:00
|
|
|
unsigned RALocal::getReg(MachineBasicBlock &MBB, MachineInstr *I,
|
2009-01-29 09:13:00 +08:00
|
|
|
unsigned VirtReg, bool NoFree) {
|
2007-12-31 12:13:23 +08:00
|
|
|
const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
|
2003-01-13 08:25:40 +08:00
|
|
|
|
|
|
|
// First check to see if we have a free register of the requested type...
|
2009-01-29 09:13:00 +08:00
|
|
|
unsigned PhysReg = NoFree ? 0 : getFreeReg(RC);
|
2002-12-17 00:15:28 +08:00
|
|
|
|
2002-12-17 10:50:10 +08:00
|
|
|
// If we didn't find an unused register, scavenge one now!
|
2002-12-17 00:15:28 +08:00
|
|
|
if (PhysReg == 0) {
|
2002-12-17 01:44:42 +08:00
|
|
|
assert(!PhysRegsUseOrder.empty() && "No allocated registers??");
|
2002-12-17 10:50:10 +08: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-17 00:15:28 +08:00
|
|
|
assert(i != PhysRegsUseOrder.size() &&
|
|
|
|
"Couldn't find a register of the appropriate class!");
|
2004-02-14 02:20:47 +08:00
|
|
|
|
2002-12-17 10:50:10 +08:00
|
|
|
unsigned R = PhysRegsUseOrder[i];
|
2003-08-24 07:49:42 +08: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 09:26:13 +08:00
|
|
|
assert(PhysRegsUsed[R] != -1 &&
|
2003-08-24 07:49:42 +08:00
|
|
|
"PhysReg in PhysRegsUseOrder, but is not allocated?");
|
2006-09-09 03:03:30 +08:00
|
|
|
if (PhysRegsUsed[R] && PhysRegsUsed[R] != -2) {
|
2003-08-24 07:49:42 +08:00
|
|
|
// If the current register is compatible, use it.
|
2004-08-16 06:23:09 +08:00
|
|
|
if (RC->contains(R)) {
|
2003-08-24 07:49:42 +08:00
|
|
|
PhysReg = R;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// If one of the registers aliased to the current register is
|
|
|
|
// compatible, use it.
|
2008-02-11 02:45:23 +08:00
|
|
|
for (const unsigned *AliasIt = TRI->getAliasSet(R);
|
2006-09-03 15:15:37 +08: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-09 03:03:30 +08:00
|
|
|
PhysRegsUsed[*AliasIt] != 0 &&
|
|
|
|
|
|
|
|
// Make sure the register is allocatable. Don't allocate SIL on
|
|
|
|
// x86-32.
|
|
|
|
PhysRegsUsed[*AliasIt] != -2) {
|
2006-09-03 15:15:37 +08:00
|
|
|
PhysReg = *AliasIt; // Take an aliased register
|
2003-10-08 13:20:08 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-08-24 07:49:42 +08:00
|
|
|
}
|
2002-12-17 10:50:10 +08:00
|
|
|
}
|
2002-12-17 00:15:28 +08:00
|
|
|
}
|
|
|
|
|
2002-12-17 10:50:10 +08:00
|
|
|
assert(PhysReg && "Physical register not assigned!?!?");
|
|
|
|
|
2002-12-17 00:15:28 +08: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-17 01:44:42 +08:00
|
|
|
spillPhysReg(MBB, I, PhysReg);
|
2002-12-17 10:50:10 +08:00
|
|
|
}
|
2002-12-17 01:44:42 +08:00
|
|
|
|
2002-12-17 00:15:28 +08:00
|
|
|
// Now that we know which register we need to assign this to, do it now!
|
2003-01-13 08:25:40 +08:00
|
|
|
assignVirtToPhysReg(VirtReg, PhysReg);
|
2002-12-17 00:15:28 +08:00
|
|
|
return PhysReg;
|
|
|
|
}
|
|
|
|
|
2002-12-17 10:50:10 +08:00
|
|
|
|
2009-05-08 05:20:42 +08:00
|
|
|
/// reloadVirtReg - This method transforms the specified virtual
|
2004-02-17 11:57:19 +08: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-17 00:15:28 +08:00
|
|
|
///
|
2007-05-09 03:02:46 +08:00
|
|
|
MachineInstr *RALocal::reloadVirtReg(MachineBasicBlock &MBB, MachineInstr *MI,
|
2009-01-29 10:20:59 +08:00
|
|
|
unsigned OpNum,
|
|
|
|
SmallSet<unsigned, 4> &ReloadedRegs) {
|
2004-02-17 11:57:19 +08:00
|
|
|
unsigned VirtReg = MI->getOperand(OpNum).getReg();
|
|
|
|
|
|
|
|
// If the virtual register is already available, just update the instruction
|
|
|
|
// and return.
|
2004-02-14 02:20:47 +08:00
|
|
|
if (unsigned PR = getVirt2PhysRegMapSlot(VirtReg)) {
|
2008-03-01 02:52:01 +08:00
|
|
|
MarkPhysRegRecentlyUsed(PR); // Already have this value available!
|
2006-05-05 01:52:23 +08:00
|
|
|
MI->getOperand(OpNum).setReg(PR); // Assign the input register
|
2008-03-01 02:52:01 +08:00
|
|
|
getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
|
2004-02-17 11:57:19 +08:00
|
|
|
return MI;
|
2002-12-17 00:15:28 +08:00
|
|
|
}
|
|
|
|
|
2004-02-17 12:08:37 +08: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 12:13:23 +08:00
|
|
|
const TargetRegisterClass *RC = MF->getRegInfo().getRegClass(VirtReg);
|
2004-02-17 12:08:37 +08:00
|
|
|
unsigned PhysReg = getFreeReg(RC);
|
2004-02-17 16:09:40 +08:00
|
|
|
int FrameIndex = getStackSpaceFor(VirtReg, RC);
|
2004-02-17 12:08:37 +08:00
|
|
|
|
2004-02-17 16:09:40 +08:00
|
|
|
if (PhysReg) { // Register is available, allocate it!
|
|
|
|
assignVirtToPhysReg(VirtReg, PhysReg);
|
|
|
|
} else { // No registers available.
|
2008-02-08 03:46:55 +08:00
|
|
|
// Force some poor hapless value out of the register file to
|
2004-02-17 12:08:37 +08:00
|
|
|
// make room for the new register, and reload it.
|
2009-01-29 09:13:00 +08:00
|
|
|
PhysReg = getReg(MBB, MI, VirtReg, true);
|
2004-02-17 12:08:37 +08:00
|
|
|
}
|
|
|
|
|
2003-01-13 08:25:40 +08:00
|
|
|
markVirtRegModified(VirtReg, false); // Note that this reg was just reloaded
|
|
|
|
|
2009-08-23 04:38:09 +08:00
|
|
|
DEBUG(errs() << " Reloading %reg" << VirtReg << " into "
|
|
|
|
<< TRI->getName(PhysReg) << "\n");
|
2003-08-05 07:36:39 +08:00
|
|
|
|
2002-12-17 00:15:28 +08:00
|
|
|
// Add move instruction(s)
|
2008-01-02 05:11:32 +08:00
|
|
|
TII->loadRegFromStackSlot(MBB, MI, PhysReg, FrameIndex, RC);
|
2004-02-19 14:19:09 +08:00
|
|
|
++NumLoads; // Update statistics
|
2004-02-17 11:57:19 +08:00
|
|
|
|
2007-12-31 12:13:23 +08:00
|
|
|
MF->getRegInfo().setPhysRegUsed(PhysReg);
|
2006-05-05 01:52:23 +08:00
|
|
|
MI->getOperand(OpNum).setReg(PhysReg); // Assign the input register
|
2008-01-17 10:08:17 +08:00
|
|
|
getVirtRegLastUse(VirtReg) = std::make_pair(MI, OpNum);
|
2009-01-29 10:20:59 +08:00
|
|
|
|
|
|
|
if (!ReloadedRegs.insert(PhysReg)) {
|
2009-07-11 21:10:19 +08:00
|
|
|
std::string msg;
|
|
|
|
raw_string_ostream Msg(msg);
|
|
|
|
Msg << "Ran out of registers during register allocation!";
|
2009-01-29 10:20:59 +08:00
|
|
|
if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
|
2009-07-11 21:10:19 +08:00
|
|
|
Msg << "\nPlease check your inline asm statement for invalid "
|
2009-01-29 10:20:59 +08:00
|
|
|
<< "constraints:\n";
|
2009-07-11 21:10:19 +08:00
|
|
|
MI->print(Msg, TM);
|
2009-01-29 10:20:59 +08:00
|
|
|
}
|
2009-07-11 21:10:19 +08:00
|
|
|
llvm_report_error(Msg.str());
|
2009-01-29 10:20:59 +08:00
|
|
|
}
|
|
|
|
for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
|
|
|
|
*SubRegs; ++SubRegs) {
|
|
|
|
if (!ReloadedRegs.insert(*SubRegs)) {
|
2009-07-11 21:10:19 +08:00
|
|
|
std::string msg;
|
|
|
|
raw_string_ostream Msg(msg);
|
|
|
|
Msg << "Ran out of registers during register allocation!";
|
2009-01-29 10:20:59 +08:00
|
|
|
if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {
|
2009-07-11 21:10:19 +08:00
|
|
|
Msg << "\nPlease check your inline asm statement for invalid "
|
2009-01-29 10:20:59 +08:00
|
|
|
<< "constraints:\n";
|
2009-07-11 21:10:19 +08:00
|
|
|
MI->print(Msg, TM);
|
2009-01-29 10:20:59 +08:00
|
|
|
}
|
2009-07-11 21:10:19 +08:00
|
|
|
llvm_report_error(Msg.str());
|
2009-01-29 10:20:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-17 11:57:19 +08:00
|
|
|
return MI;
|
2002-12-17 00:15:28 +08:00
|
|
|
}
|
|
|
|
|
2007-06-27 05:05:13 +08: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 23:45:36 +08:00
|
|
|
if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
|
2007-06-27 05:05:13 +08:00
|
|
|
MO.isDef() && !MO.isDead())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2003-08-05 07:36:39 +08:00
|
|
|
|
2007-06-27 05:05:13 +08: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 23:45:36 +08:00
|
|
|
if (MO.isReg() && MO.getReg() == Reg && MO.isImplicit() &&
|
2007-06-27 05:05:13 +08:00
|
|
|
!MO.isDef() && MO.isKill())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2003-08-05 07:36:39 +08:00
|
|
|
|
2008-07-09 06:24:50 +08: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-10 04:14:53 +08:00
|
|
|
/// ComputeLocalLiveness - Computes liveness of registers within a basic
|
|
|
|
/// block, setting the killed/dead flags as appropriate.
|
|
|
|
void RALocal::ComputeLocalLiveness(MachineBasicBlock& MBB) {
|
2008-07-09 06:24:50 +08: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 09:56:35 +08:00
|
|
|
DenseMap<unsigned, std::pair<MachineInstr*, unsigned> > LastUseDef;
|
2008-07-09 06:24:50 +08: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 12:30:51 +08:00
|
|
|
if (MO.isReg() && MO.getReg() && MO.isUse()) {
|
2008-07-09 06:24:50 +08:00
|
|
|
LastUseDef[MO.getReg()] = std::make_pair(I, i);
|
2008-10-08 12:30:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) continue;
|
|
|
|
|
2009-01-29 10:20:59 +08:00
|
|
|
const unsigned* Aliases = TRI->getAliasSet(MO.getReg());
|
|
|
|
if (Aliases) {
|
|
|
|
while (*Aliases) {
|
2008-10-08 12:30:51 +08:00
|
|
|
DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
|
2009-01-29 10:20:59 +08:00
|
|
|
alias = LastUseDef.find(*Aliases);
|
2008-10-08 12:30:51 +08:00
|
|
|
|
2009-01-29 10:20:59 +08:00
|
|
|
if (alias != LastUseDef.end() && alias->second.first != I)
|
|
|
|
LastUseDef[*Aliases] = std::make_pair(I, i);
|
2008-10-08 12:30:51 +08:00
|
|
|
|
2009-01-29 10:20:59 +08:00
|
|
|
++Aliases;
|
2008-10-08 12:30:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-09 06:24:50 +08: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 23:45:36 +08:00
|
|
|
if (MO.isReg() && MO.getReg() && MO.isDef()) {
|
2008-07-10 09:56:35 +08:00
|
|
|
DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
|
2008-07-09 06:24:50 +08:00
|
|
|
last = LastUseDef.find(MO.getReg());
|
|
|
|
if (last != LastUseDef.end()) {
|
2008-07-10 09:53:01 +08:00
|
|
|
// Check if this is a two address instruction. If so, then
|
|
|
|
// the def does not kill the use.
|
2008-07-10 15:35:43 +08:00
|
|
|
if (last->second.first == I &&
|
2009-04-10 01:16:43 +08:00
|
|
|
I->isRegTiedToUseOperand(i))
|
2008-07-10 15:35:43 +08:00
|
|
|
continue;
|
2008-07-10 05:15:10 +08:00
|
|
|
|
2008-07-09 06:24:50 +08:00
|
|
|
MachineOperand& lastUD =
|
|
|
|
last->second.first->getOperand(last->second.second);
|
|
|
|
if (lastUD.isDef())
|
|
|
|
lastUD.setIsDead(true);
|
2008-07-10 15:35:43 +08:00
|
|
|
else
|
2008-07-09 06:24:50 +08: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 09:56:35 +08:00
|
|
|
for (DenseMap<unsigned, std::pair<MachineInstr*, unsigned> >::iterator
|
2008-07-09 06:24:50 +08: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-09 07:36:37 +08:00
|
|
|
(MO.isDef() && UI.getOperand().isUse() && precedes(&*UI, MI))) {
|
2008-07-09 06:24:50 +08: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 08:31:14 +08:00
|
|
|
if (MO.isUse()) {
|
|
|
|
// Don't mark uses that are tied to defs as kills.
|
2009-03-20 04:30:06 +08:00
|
|
|
if (!MI->isRegTiedToDefOperand(idx))
|
2008-10-04 08:31:14 +08:00
|
|
|
MO.setIsKill(true);
|
|
|
|
} else
|
2008-07-09 06:24:50 +08:00
|
|
|
MO.setIsDead(true);
|
|
|
|
}
|
|
|
|
}
|
2008-07-10 04:14:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RALocal::AllocateBasicBlock(MachineBasicBlock &MBB) {
|
|
|
|
// loop over each instruction
|
|
|
|
MachineBasicBlock::iterator MII = MBB.begin();
|
|
|
|
|
2009-08-23 04:38:09 +08:00
|
|
|
DEBUG({
|
|
|
|
const BasicBlock *LBB = MBB.getBasicBlock();
|
|
|
|
if (LBB)
|
|
|
|
errs() << "\nStarting RegAlloc of BB: " << LBB->getName();
|
|
|
|
});
|
2008-07-10 04:14:53 +08:00
|
|
|
|
2009-01-30 02:37:30 +08:00
|
|
|
// Add live-in registers as active.
|
|
|
|
for (MachineBasicBlock::livein_iterator I = MBB.livein_begin(),
|
2008-07-10 04:14:53 +08:00
|
|
|
E = MBB.livein_end(); I != E; ++I) {
|
2009-01-30 02:37:30 +08: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-10 04:14:53 +08:00
|
|
|
}
|
2009-01-30 02:37:30 +08:00
|
|
|
}
|
2008-07-10 04:14:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ComputeLocalLiveness(MBB);
|
2008-07-09 06:24:50 +08:00
|
|
|
|
2006-06-16 06:21:53 +08:00
|
|
|
// Otherwise, sequentially allocate each instruction in the MBB.
|
2005-11-10 02:22:42 +08:00
|
|
|
while (MII != MBB.end()) {
|
|
|
|
MachineInstr *MI = MII++;
|
2008-01-07 15:27:27 +08:00
|
|
|
const TargetInstrDesc &TID = MI->getDesc();
|
2009-08-23 04:38:09 +08:00
|
|
|
DEBUG({
|
|
|
|
errs() << "\nStarting RegAlloc of: " << *MI;
|
|
|
|
errs() << " Regs have values: ";
|
|
|
|
for (unsigned i = 0; i != TRI->getNumRegs(); ++i)
|
|
|
|
if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2)
|
|
|
|
errs() << "[" << TRI->getName(i)
|
|
|
|
<< ",%reg" << PhysRegsUsed[i] << "] ";
|
|
|
|
errs() << '\n';
|
|
|
|
});
|
2002-12-17 00:15:28 +08:00
|
|
|
|
2003-01-13 08:25:40 +08: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-22 05:15:20 +08:00
|
|
|
if (TID.ImplicitUses) {
|
|
|
|
for (const unsigned *ImplicitUses = TID.ImplicitUses;
|
|
|
|
*ImplicitUses; ++ImplicitUses)
|
|
|
|
MarkPhysRegRecentlyUsed(*ImplicitUses);
|
|
|
|
}
|
2002-12-17 00:15:28 +08:00
|
|
|
|
2006-11-16 04:55:15 +08:00
|
|
|
SmallVector<unsigned, 8> Kills;
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand& MO = MI->getOperand(i);
|
2008-10-03 23:45:36 +08:00
|
|
|
if (MO.isReg() && MO.isKill()) {
|
2007-06-27 05:05:13 +08: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-16 04:55:15 +08:00
|
|
|
}
|
|
|
|
|
2008-09-25 07:13:09 +08: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 23:45:36 +08:00
|
|
|
if (MO.isReg() && MO.isDef() && MO.isEarlyClobber() &&
|
2008-09-25 07:13:09 +08: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);
|
2009-08-23 04:38:09 +08:00
|
|
|
DEBUG(errs() << " Assigning " << TRI->getName(DestPhysReg)
|
|
|
|
<< " to %reg" << DestVirtReg << "\n");
|
2008-09-25 07:13:09 +08:00
|
|
|
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 10:20:59 +08: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-25 07:13:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-16 05:19:25 +08:00
|
|
|
// Get the used operands into registers. This has the potential to spill
|
2003-08-05 07:36:39 +08: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 08:25:40 +08:00
|
|
|
//
|
2009-01-29 10:20:59 +08:00
|
|
|
SmallSet<unsigned, 4> ReloadedRegs;
|
2004-02-27 06:00:20 +08: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 23:45:36 +08:00
|
|
|
if (MO.isReg() && !MO.isDef() && MO.getReg() && !MO.isImplicit() &&
|
2008-02-11 02:45:23 +08:00
|
|
|
TargetRegisterInfo::isVirtualRegister(MO.getReg()))
|
2009-01-29 10:20:59 +08:00
|
|
|
MI = reloadVirtReg(MBB, MI, i, ReloadedRegs);
|
2004-02-27 06:00:20 +08:00
|
|
|
}
|
2004-02-14 02:20:47 +08:00
|
|
|
|
2006-11-16 04:55:15 +08:00
|
|
|
// If this instruction is the last user of this register, kill the
|
2004-02-18 01:49:10 +08:00
|
|
|
// value, freeing the register being used, so it doesn't need to be
|
|
|
|
// spilled to memory.
|
|
|
|
//
|
2006-11-16 04:55:15 +08:00
|
|
|
for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
|
|
|
|
unsigned VirtReg = Kills[i];
|
2004-02-18 01:49:10 +08:00
|
|
|
unsigned PhysReg = VirtReg;
|
2008-02-11 02:45:23 +08:00
|
|
|
if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
|
2004-02-18 01:49:10 +08: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-09 04:21:31 +08:00
|
|
|
} else if (PhysRegsUsed[PhysReg] == -2) {
|
|
|
|
// Unallocatable register dead, ignore.
|
|
|
|
continue;
|
2007-06-27 05:05:13 +08:00
|
|
|
} else {
|
2007-10-23 03:42:28 +08:00
|
|
|
assert((!PhysRegsUsed[PhysReg] || PhysRegsUsed[PhysReg] == -1) &&
|
2007-06-27 05:05:13 +08:00
|
|
|
"Silently clearing a virtual register?");
|
2004-02-18 01:49:10 +08:00
|
|
|
}
|
2002-12-17 00:15:28 +08:00
|
|
|
|
2004-02-18 01:49:10 +08:00
|
|
|
if (PhysReg) {
|
2009-08-23 04:38:09 +08:00
|
|
|
DEBUG(errs() << " Last use of " << TRI->getName(PhysReg)
|
|
|
|
<< "[%reg" << VirtReg <<"], removing it from live set\n");
|
2004-02-18 01:49:10 +08:00
|
|
|
removePhysReg(PhysReg);
|
2009-01-29 10:20:59 +08:00
|
|
|
for (const unsigned *SubRegs = TRI->getSubRegisters(PhysReg);
|
|
|
|
*SubRegs; ++SubRegs) {
|
|
|
|
if (PhysRegsUsed[*SubRegs] != -2) {
|
2009-08-23 04:38:09 +08:00
|
|
|
DEBUG(errs() << " Last use of "
|
|
|
|
<< TRI->getName(*SubRegs) << "[%reg" << VirtReg
|
|
|
|
<<"], removing it from live set\n");
|
2009-01-29 10:20:59 +08:00
|
|
|
removePhysReg(*SubRegs);
|
2006-11-16 04:55:15 +08:00
|
|
|
}
|
|
|
|
}
|
2002-12-17 00:15:28 +08: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-27 06:00:20 +08:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand& MO = MI->getOperand(i);
|
2008-10-03 23:45:36 +08:00
|
|
|
if (MO.isReg() && MO.isDef() && !MO.isImplicit() && MO.getReg() &&
|
2008-09-25 07:13:09 +08:00
|
|
|
!MO.isEarlyClobber() &&
|
2008-02-11 02:45:23 +08:00
|
|
|
TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
|
2004-02-27 06:00:20 +08:00
|
|
|
unsigned Reg = MO.getReg();
|
2006-09-09 03:11:11 +08:00
|
|
|
if (PhysRegsUsed[Reg] == -2) continue; // Something like ESP.
|
2007-06-27 05:05:13 +08: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 12:13:23 +08:00
|
|
|
MF->getRegInfo().setPhysRegUsed(Reg);
|
2006-11-16 04:55:15 +08:00
|
|
|
spillPhysReg(MBB, MI, Reg, true); // Spill any existing value in reg
|
2002-12-24 08:04:55 +08:00
|
|
|
PhysRegsUsed[Reg] = 0; // It is free and reserved now
|
2007-06-27 05:05:13 +08:00
|
|
|
AddToPhysRegsUseOrder(Reg);
|
|
|
|
|
2009-01-29 10:20:59 +08: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-09 03:03:30 +08:00
|
|
|
}
|
2004-01-13 14:24:30 +08:00
|
|
|
}
|
2002-12-17 00:15:28 +08:00
|
|
|
}
|
2004-02-27 06:00:20 +08:00
|
|
|
}
|
2002-12-17 00:15:28 +08:00
|
|
|
|
2003-01-13 08:25:40 +08:00
|
|
|
// Loop over the implicit defs, spilling them as well.
|
2006-07-22 05:15:20 +08:00
|
|
|
if (TID.ImplicitDefs) {
|
|
|
|
for (const unsigned *ImplicitDefs = TID.ImplicitDefs;
|
|
|
|
*ImplicitDefs; ++ImplicitDefs) {
|
|
|
|
unsigned Reg = *ImplicitDefs;
|
2007-06-27 05:05:13 +08:00
|
|
|
if (PhysRegsUsed[Reg] != -2) {
|
2006-09-20 02:02:01 +08:00
|
|
|
spillPhysReg(MBB, MI, Reg, true);
|
2007-06-27 05:05:13 +08:00
|
|
|
AddToPhysRegsUseOrder(Reg);
|
2006-09-20 02:02:01 +08:00
|
|
|
PhysRegsUsed[Reg] = 0; // It is free and reserved now
|
|
|
|
}
|
2007-12-31 12:13:23 +08:00
|
|
|
MF->getRegInfo().setPhysRegUsed(Reg);
|
2009-01-29 10:20:59 +08: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-09 03:03:30 +08:00
|
|
|
}
|
2006-07-22 05:15:20 +08:00
|
|
|
}
|
2004-01-13 14:24:30 +08:00
|
|
|
}
|
2003-12-13 09:20:58 +08:00
|
|
|
}
|
2002-12-17 00:15:28 +08:00
|
|
|
|
2006-11-16 04:55:15 +08:00
|
|
|
SmallVector<unsigned, 8> DeadDefs;
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand& MO = MI->getOperand(i);
|
2008-10-03 23:45:36 +08:00
|
|
|
if (MO.isReg() && MO.isDead())
|
2006-11-16 04:55:15 +08:00
|
|
|
DeadDefs.push_back(MO.getReg());
|
|
|
|
}
|
|
|
|
|
2002-12-17 00:15:28 +08: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-24 06:51:56 +08:00
|
|
|
// explicit defs and assign them to a register, spilling incoming values if
|
2003-01-13 08:25:40 +08:00
|
|
|
// we need to scavenge a register.
|
2002-12-18 16:14:26 +08:00
|
|
|
//
|
2004-02-27 06:00:20 +08:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand& MO = MI->getOperand(i);
|
2008-10-03 23:45:36 +08:00
|
|
|
if (MO.isReg() && MO.isDef() && MO.getReg() &&
|
2008-09-25 07:13:09 +08:00
|
|
|
!MO.isEarlyClobber() &&
|
2008-02-11 02:45:23 +08:00
|
|
|
TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
|
2004-02-27 06:00:20 +08:00
|
|
|
unsigned DestVirtReg = MO.getReg();
|
2002-12-17 00:15:28 +08:00
|
|
|
unsigned DestPhysReg;
|
|
|
|
|
2003-12-18 21:08:52 +08:00
|
|
|
// If DestVirtReg already has a value, use it.
|
2004-02-14 02:20:47 +08:00
|
|
|
if (!(DestPhysReg = getVirt2PhysRegMapSlot(DestVirtReg)))
|
2004-02-12 10:27:10 +08:00
|
|
|
DestPhysReg = getReg(MBB, MI, DestVirtReg);
|
2007-12-31 12:13:23 +08:00
|
|
|
MF->getRegInfo().setPhysRegUsed(DestPhysReg);
|
2003-05-12 11:54:14 +08:00
|
|
|
markVirtRegModified(DestVirtReg);
|
2008-01-17 10:08:17 +08:00
|
|
|
getVirtRegLastUse(DestVirtReg) = std::make_pair((MachineInstr*)0, 0);
|
2009-08-23 04:38:09 +08:00
|
|
|
DEBUG(errs() << " Assigning " << TRI->getName(DestPhysReg)
|
|
|
|
<< " to %reg" << DestVirtReg << "\n");
|
2008-07-10 04:12:26 +08:00
|
|
|
MO.setReg(DestPhysReg); // Assign the output register
|
2002-12-17 00:15:28 +08:00
|
|
|
}
|
2004-02-27 06:00:20 +08:00
|
|
|
}
|
2002-12-18 16:14:26 +08:00
|
|
|
|
2004-02-18 01:49:10 +08:00
|
|
|
// If this instruction defines any registers that are immediately dead,
|
|
|
|
// kill them now.
|
|
|
|
//
|
2006-11-16 04:55:15 +08:00
|
|
|
for (unsigned i = 0, e = DeadDefs.size(); i != e; ++i) {
|
|
|
|
unsigned VirtReg = DeadDefs[i];
|
2004-02-18 01:49:10 +08:00
|
|
|
unsigned PhysReg = VirtReg;
|
2008-02-11 02:45:23 +08:00
|
|
|
if (TargetRegisterInfo::isVirtualRegister(VirtReg)) {
|
2004-02-18 01:49:10 +08:00
|
|
|
unsigned &PhysRegSlot = getVirt2PhysRegMapSlot(VirtReg);
|
|
|
|
PhysReg = PhysRegSlot;
|
|
|
|
assert(PhysReg != 0);
|
|
|
|
PhysRegSlot = 0;
|
2006-09-09 04:21:31 +08:00
|
|
|
} else if (PhysRegsUsed[PhysReg] == -2) {
|
|
|
|
// Unallocatable register dead, ignore.
|
|
|
|
continue;
|
2004-02-18 01:49:10 +08:00
|
|
|
}
|
2003-01-13 08:25:40 +08:00
|
|
|
|
2004-02-18 01:49:10 +08:00
|
|
|
if (PhysReg) {
|
2009-08-23 04:38:09 +08:00
|
|
|
DEBUG(errs() << " Register " << TRI->getName(PhysReg)
|
|
|
|
<< " [%reg" << VirtReg
|
|
|
|
<< "] is never used, removing it from live set\n");
|
2004-02-18 01:49:10 +08:00
|
|
|
removePhysReg(PhysReg);
|
2008-02-11 02:45:23 +08:00
|
|
|
for (const unsigned *AliasSet = TRI->getAliasSet(PhysReg);
|
2006-11-16 04:55:15 +08:00
|
|
|
*AliasSet; ++AliasSet) {
|
|
|
|
if (PhysRegsUsed[*AliasSet] != -2) {
|
2009-08-23 04:38:09 +08:00
|
|
|
DEBUG(errs() << " Register " << TRI->getName(*AliasSet)
|
|
|
|
<< " [%reg" << *AliasSet
|
|
|
|
<< "] is never used, removing it from live set\n");
|
2006-11-16 04:55:15 +08:00
|
|
|
removePhysReg(*AliasSet);
|
|
|
|
}
|
|
|
|
}
|
2002-12-18 16:14:26 +08:00
|
|
|
}
|
|
|
|
}
|
2005-11-10 02:22:42 +08: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-08 07:47:03 +08: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-21 03:12:24 +08: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-08 07:47:03 +08:00
|
|
|
SrcReg == DstReg && DeadDefs.empty())
|
2005-11-10 02:22:42 +08:00
|
|
|
MBB.erase(MI);
|
2002-12-17 00:15:28 +08:00
|
|
|
}
|
|
|
|
|
2005-11-10 02:22:42 +08:00
|
|
|
MachineBasicBlock::iterator MI = MBB.getFirstTerminator();
|
2002-12-17 00:15:28 +08:00
|
|
|
|
|
|
|
// Spill all physical registers holding virtual registers now.
|
2008-02-11 02:45:23 +08:00
|
|
|
for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
|
2008-02-20 20:07:57 +08:00
|
|
|
if (PhysRegsUsed[i] != -1 && PhysRegsUsed[i] != -2) {
|
2004-02-09 09:26:13 +08:00
|
|
|
if (unsigned VirtReg = PhysRegsUsed[i])
|
2004-02-12 10:27:10 +08:00
|
|
|
spillVirtReg(MBB, MI, VirtReg, i);
|
2004-02-09 09:26:13 +08:00
|
|
|
else
|
|
|
|
removePhysReg(i);
|
2008-02-20 20:07:57 +08:00
|
|
|
}
|
2002-12-17 00:15:28 +08:00
|
|
|
|
2005-11-09 13:28:45 +08:00
|
|
|
#if 0
|
|
|
|
// This checking code is very expensive.
|
2004-02-09 10:12:04 +08:00
|
|
|
bool AllOk = true;
|
2008-02-11 02:45:23 +08:00
|
|
|
for (unsigned i = TargetRegisterInfo::FirstVirtualRegister,
|
2007-12-31 12:13:23 +08:00
|
|
|
e = MF->getRegInfo().getLastVirtReg(); i <= e; ++i)
|
2004-02-09 10:12:04 +08:00
|
|
|
if (unsigned PR = Virt2PhysRegMap[i]) {
|
2006-12-08 04:04:42 +08:00
|
|
|
cerr << "Register still mapped: " << i << " -> " << PR << "\n";
|
2004-02-09 10:12:04 +08:00
|
|
|
AllOk = false;
|
|
|
|
}
|
|
|
|
assert(AllOk && "Virtual registers still in phys regs?");
|
|
|
|
#endif
|
2004-02-14 02:20:47 +08:00
|
|
|
|
2003-08-18 02:01:15 +08: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-17 00:15:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// runOnMachineFunction - Register allocate the whole function
|
|
|
|
///
|
2007-05-09 03:02:46 +08:00
|
|
|
bool RALocal::runOnMachineFunction(MachineFunction &Fn) {
|
2009-08-23 04:38:09 +08:00
|
|
|
DEBUG(errs() << "Machine Function\n");
|
2002-12-17 00:15:28 +08:00
|
|
|
MF = &Fn;
|
2002-12-29 04:40:43 +08:00
|
|
|
TM = &Fn.getTarget();
|
2008-02-11 02:45:23 +08:00
|
|
|
TRI = TM->getRegisterInfo();
|
2008-01-07 09:35:56 +08:00
|
|
|
TII = TM->getInstrInfo();
|
2002-12-17 00:15:28 +08:00
|
|
|
|
2008-02-11 02:45:23 +08:00
|
|
|
PhysRegsUsed.assign(TRI->getNumRegs(), -1);
|
2006-09-09 03:03:30 +08: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-11 02:45:23 +08:00
|
|
|
BitVector Allocable = TRI->getAllocatableSet(Fn);
|
2006-09-09 03:03:30 +08:00
|
|
|
for (unsigned i = 0, e = Allocable.size(); i != e; ++i)
|
|
|
|
if (!Allocable[i])
|
|
|
|
PhysRegsUsed[i] = -2; // Mark the reg unallocable.
|
|
|
|
}
|
2004-02-09 09:26:13 +08:00
|
|
|
|
2004-02-14 02:20:47 +08:00
|
|
|
// initialize the virtual->physical register map to have a 'null'
|
|
|
|
// mapping for all virtual registers
|
2008-01-17 08:35:26 +08:00
|
|
|
unsigned LastVirtReg = MF->getRegInfo().getLastVirtReg();
|
2008-07-11 02:23:23 +08:00
|
|
|
StackSlotForVirtReg.grow(LastVirtReg);
|
2008-01-17 08:35:26 +08:00
|
|
|
Virt2PhysRegMap.grow(LastVirtReg);
|
2008-01-17 10:08:17 +08:00
|
|
|
Virt2LastUseMap.grow(LastVirtReg);
|
2008-02-11 02:45:23 +08:00
|
|
|
VirtRegModified.resize(LastVirtReg+1-TargetRegisterInfo::FirstVirtualRegister);
|
2008-07-09 06:24:50 +08:00
|
|
|
UsedInMultipleBlocks.resize(LastVirtReg+1-TargetRegisterInfo::FirstVirtualRegister);
|
|
|
|
|
2002-12-17 00:15:28 +08: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-29 04:40:43 +08:00
|
|
|
StackSlotForVirtReg.clear();
|
2004-02-14 02:20:47 +08:00
|
|
|
PhysRegsUsed.clear();
|
2003-01-13 08:25:40 +08:00
|
|
|
VirtRegModified.clear();
|
2008-07-09 06:24:50 +08:00
|
|
|
UsedInMultipleBlocks.clear();
|
2004-02-09 10:12:04 +08:00
|
|
|
Virt2PhysRegMap.clear();
|
2008-01-17 10:08:17 +08:00
|
|
|
Virt2LastUseMap.clear();
|
2002-12-17 00:15:28 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-02-01 05:27:19 +08:00
|
|
|
FunctionPass *llvm::createLocalRegisterAllocator() {
|
2007-05-09 03:02:46 +08:00
|
|
|
return new RALocal();
|
2002-12-17 00:15:28 +08:00
|
|
|
}
|