2002-11-22 22:44:32 +00:00
|
|
|
//===-- RegAllocSimple.cpp - A simple generic register allocator --- ------===//
|
|
|
|
//
|
|
|
|
// This file implements a simple register allocator. *Very* simple.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2002-12-15 18:19:24 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2002-12-04 23:58:08 +00:00
|
|
|
#include "llvm/Target/MachineInstrInfo.h"
|
2002-11-22 22:44:32 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "Support/Statistic.h"
|
2002-12-15 18:19:24 +00:00
|
|
|
#include <iostream>
|
2002-12-15 20:36:09 +00:00
|
|
|
#include <set>
|
2002-11-22 22:44:32 +00:00
|
|
|
|
2002-12-15 18:38:59 +00:00
|
|
|
/// PhysRegClassMap - Construct a mapping of physical register numbers to their
|
|
|
|
/// register classes.
|
|
|
|
///
|
|
|
|
/// NOTE: This class will eventually be pulled out to somewhere shared.
|
|
|
|
///
|
|
|
|
class PhysRegClassMap {
|
|
|
|
std::map<unsigned, const TargetRegisterClass*> PhysReg2RegClassMap;
|
|
|
|
public:
|
|
|
|
PhysRegClassMap(const MRegisterInfo *RI) {
|
|
|
|
for (MRegisterInfo::const_iterator I = RI->regclass_begin(),
|
|
|
|
E = RI->regclass_end(); I != E; ++I)
|
|
|
|
for (unsigned i=0; i < (*I)->getNumRegs(); ++i)
|
|
|
|
PhysReg2RegClassMap[(*I)->getRegister(i)] = *I;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TargetRegisterClass *operator[](unsigned Reg) {
|
|
|
|
assert(PhysReg2RegClassMap[Reg] && "Register is not a known physreg!");
|
|
|
|
return PhysReg2RegClassMap[Reg];
|
|
|
|
}
|
|
|
|
|
|
|
|
const TargetRegisterClass *get(unsigned Reg) { return operator[](Reg); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-12-13 10:42:31 +00:00
|
|
|
namespace {
|
2002-12-15 20:36:09 +00:00
|
|
|
Statistic<> NumSpilled ("ra-simple", "Number of registers spilled");
|
|
|
|
Statistic<> NumReloaded("ra-simple", "Number of registers reloaded");
|
|
|
|
|
|
|
|
class RegAllocSimple : public FunctionPass {
|
2002-11-22 22:44:32 +00:00
|
|
|
TargetMachine &TM;
|
|
|
|
MachineFunction *MF;
|
|
|
|
const MRegisterInfo *RegInfo;
|
2002-12-15 19:07:34 +00:00
|
|
|
unsigned NumBytesAllocated;
|
2002-11-22 22:44:32 +00:00
|
|
|
|
|
|
|
// Maps SSA Regs => offsets on the stack where these values are stored
|
2002-12-15 18:15:24 +00:00
|
|
|
std::map<unsigned, unsigned> VirtReg2OffsetMap;
|
2002-11-22 22:44:32 +00:00
|
|
|
|
2002-12-03 23:15:19 +00:00
|
|
|
// Maps physical register to their register classes
|
2002-12-15 18:38:59 +00:00
|
|
|
PhysRegClassMap PhysRegClasses;
|
2002-12-12 23:20:31 +00:00
|
|
|
|
2002-12-15 20:36:09 +00:00
|
|
|
// RegsUsed - Keep track of what registers are currently in use.
|
|
|
|
std::set<unsigned> RegsUsed;
|
|
|
|
|
|
|
|
// RegClassIdx - Maps RegClass => which index we can take a register
|
|
|
|
// from. Since this is a simple register allocator, when we need a register
|
|
|
|
// of a certain class, we just take the next available one.
|
2002-11-22 22:44:32 +00:00
|
|
|
std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
|
|
|
|
|
2002-12-15 20:36:09 +00:00
|
|
|
public:
|
|
|
|
|
2002-12-15 19:51:14 +00:00
|
|
|
RegAllocSimple(TargetMachine &tm)
|
|
|
|
: TM(tm), RegInfo(tm.getRegisterInfo()), PhysRegClasses(RegInfo) {
|
2002-12-15 20:36:09 +00:00
|
|
|
RegsUsed.insert(RegInfo->getFramePointer());
|
|
|
|
RegsUsed.insert(RegInfo->getStackPointer());
|
2002-12-13 04:34:02 +00:00
|
|
|
|
|
|
|
cleanupAfterFunction();
|
2002-11-22 22:44:32 +00:00
|
|
|
}
|
|
|
|
|
2002-12-15 20:36:09 +00:00
|
|
|
bool runOnFunction(Function &Fn) {
|
|
|
|
return runOnMachineFunction(MachineFunction::get(&Fn));
|
|
|
|
}
|
|
|
|
|
2002-12-15 21:13:12 +00:00
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "Simple Register Allocator";
|
|
|
|
}
|
|
|
|
|
2002-12-15 20:36:09 +00:00
|
|
|
private:
|
|
|
|
/// runOnMachineFunction - Register allocate the whole function
|
|
|
|
bool runOnMachineFunction(MachineFunction &Fn);
|
|
|
|
|
|
|
|
/// AllocateBasicBlock - Register allocate the specified basic block.
|
|
|
|
void AllocateBasicBlock(MachineBasicBlock &MBB);
|
|
|
|
|
|
|
|
/// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
|
|
|
|
/// in predecessor basic blocks.
|
|
|
|
void EliminatePHINodes(MachineBasicBlock &MBB);
|
|
|
|
|
|
|
|
|
2002-11-22 22:44:32 +00:00
|
|
|
bool isAvailableReg(unsigned Reg) {
|
|
|
|
// assert(Reg < MRegisterInfo::FirstVirtualReg && "...");
|
|
|
|
return RegsUsed.find(Reg) == RegsUsed.end();
|
|
|
|
}
|
|
|
|
|
2002-12-15 19:51:14 +00:00
|
|
|
/// allocateStackSpaceFor - This allocates space for the specified virtual
|
|
|
|
/// register to be held on the stack.
|
2002-12-02 21:11:58 +00:00
|
|
|
unsigned allocateStackSpaceFor(unsigned VirtReg,
|
|
|
|
const TargetRegisterClass *regClass);
|
|
|
|
|
2002-12-15 20:36:09 +00:00
|
|
|
/// Given a virtual register, returns a physical register that is currently
|
|
|
|
/// unused.
|
|
|
|
///
|
2002-11-22 22:44:32 +00:00
|
|
|
/// Side effect: marks that register as being used until manually cleared
|
2002-12-15 20:36:09 +00:00
|
|
|
///
|
2002-11-22 22:44:32 +00:00
|
|
|
unsigned getFreeReg(unsigned virtualReg);
|
|
|
|
|
|
|
|
/// Returns all `borrowed' registers back to the free pool
|
|
|
|
void clearAllRegs() {
|
2002-12-15 19:51:14 +00:00
|
|
|
RegClassIdx.clear();
|
2002-11-22 22:44:32 +00:00
|
|
|
}
|
|
|
|
|
2002-12-13 11:33:22 +00:00
|
|
|
/// Invalidates any references, real or implicit, to physical registers
|
|
|
|
///
|
|
|
|
void invalidatePhysRegs(const MachineInstr *MI) {
|
|
|
|
unsigned Opcode = MI->getOpcode();
|
2002-12-15 20:36:09 +00:00
|
|
|
const MachineInstrDescriptor &Desc = TM.getInstrInfo().get(Opcode);
|
2002-12-13 11:33:22 +00:00
|
|
|
const unsigned *regs = Desc.ImplicitUses;
|
|
|
|
while (*regs)
|
2002-12-15 20:36:09 +00:00
|
|
|
RegsUsed.insert(*regs++);
|
2002-12-13 11:33:22 +00:00
|
|
|
|
|
|
|
regs = Desc.ImplicitDefs;
|
|
|
|
while (*regs)
|
2002-12-15 20:36:09 +00:00
|
|
|
RegsUsed.insert(*regs++);
|
2002-12-13 11:33:22 +00:00
|
|
|
}
|
|
|
|
|
2002-12-04 23:58:08 +00:00
|
|
|
void cleanupAfterFunction() {
|
2002-12-15 18:15:24 +00:00
|
|
|
VirtReg2OffsetMap.clear();
|
2002-12-15 19:51:14 +00:00
|
|
|
NumBytesAllocated = 4; // FIXME: This is X86 specific
|
2002-12-04 23:58:08 +00:00
|
|
|
}
|
|
|
|
|
2002-11-22 22:44:32 +00:00
|
|
|
/// Moves value from memory into that register
|
|
|
|
MachineBasicBlock::iterator
|
2002-12-15 19:51:14 +00:00
|
|
|
moveUseToReg (MachineBasicBlock &MBB,
|
2002-12-13 09:54:36 +00:00
|
|
|
MachineBasicBlock::iterator I, unsigned VirtReg,
|
2002-11-22 22:44:32 +00:00
|
|
|
unsigned &PhysReg);
|
|
|
|
|
|
|
|
/// Saves reg value on the stack (maps virtual register to stack value)
|
|
|
|
MachineBasicBlock::iterator
|
2002-12-15 19:51:14 +00:00
|
|
|
saveVirtRegToStack (MachineBasicBlock &MBB,
|
2002-12-13 09:54:36 +00:00
|
|
|
MachineBasicBlock::iterator I, unsigned VirtReg,
|
2002-12-03 23:15:19 +00:00
|
|
|
unsigned PhysReg);
|
2002-11-22 22:44:32 +00:00
|
|
|
};
|
|
|
|
|
2002-12-13 10:42:31 +00:00
|
|
|
}
|
2002-11-22 22:44:32 +00:00
|
|
|
|
2002-12-15 19:51:14 +00:00
|
|
|
/// allocateStackSpaceFor - This allocates space for the specified virtual
|
|
|
|
/// register to be held on the stack.
|
2002-12-02 21:11:58 +00:00
|
|
|
unsigned RegAllocSimple::allocateStackSpaceFor(unsigned VirtReg,
|
|
|
|
const TargetRegisterClass *regClass)
|
|
|
|
{
|
2002-12-15 18:15:24 +00:00
|
|
|
if (VirtReg2OffsetMap.find(VirtReg) == VirtReg2OffsetMap.end()) {
|
2002-12-15 19:07:34 +00:00
|
|
|
unsigned RegSize = regClass->getDataSize();
|
|
|
|
|
|
|
|
// Align NumBytesAllocated. We should be using TargetData alignment stuff
|
|
|
|
// to determine this, but we don't know the LLVM type associated with the
|
|
|
|
// virtual register. Instead, just align to a multiple of the size for now.
|
|
|
|
NumBytesAllocated += RegSize-1;
|
|
|
|
NumBytesAllocated = NumBytesAllocated/RegSize*RegSize;
|
|
|
|
|
|
|
|
// Assign the slot...
|
2002-12-15 18:15:24 +00:00
|
|
|
VirtReg2OffsetMap[VirtReg] = NumBytesAllocated;
|
2002-12-15 19:07:34 +00:00
|
|
|
|
|
|
|
// Reserve the space!
|
|
|
|
NumBytesAllocated += RegSize;
|
2002-12-02 21:11:58 +00:00
|
|
|
}
|
2002-12-15 18:15:24 +00:00
|
|
|
return VirtReg2OffsetMap[VirtReg];
|
2002-12-02 21:11:58 +00:00
|
|
|
}
|
|
|
|
|
2002-11-22 22:44:32 +00:00
|
|
|
unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
|
|
|
|
const TargetRegisterClass* regClass = MF->getRegClass(virtualReg);
|
|
|
|
unsigned physReg;
|
|
|
|
assert(regClass);
|
|
|
|
if (RegClassIdx.find(regClass) != RegClassIdx.end()) {
|
|
|
|
unsigned regIdx = RegClassIdx[regClass]++;
|
|
|
|
assert(regIdx < regClass->getNumRegs() && "Not enough registers!");
|
|
|
|
physReg = regClass->getRegister(regIdx);
|
|
|
|
} else {
|
|
|
|
physReg = regClass->getRegister(0);
|
|
|
|
// assert(physReg < regClass->getNumRegs() && "No registers in class!");
|
|
|
|
RegClassIdx[regClass] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isAvailableReg(physReg))
|
|
|
|
return physReg;
|
2002-12-15 20:36:09 +00:00
|
|
|
else
|
2002-11-22 22:44:32 +00:00
|
|
|
return getFreeReg(virtualReg);
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator
|
2002-12-15 19:51:14 +00:00
|
|
|
RegAllocSimple::moveUseToReg (MachineBasicBlock &MBB,
|
2002-12-13 09:54:36 +00:00
|
|
|
MachineBasicBlock::iterator I,
|
2002-11-22 22:44:32 +00:00
|
|
|
unsigned VirtReg, unsigned &PhysReg)
|
|
|
|
{
|
|
|
|
const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
|
2002-12-02 21:11:58 +00:00
|
|
|
unsigned stackOffset = allocateStackSpaceFor(VirtReg, regClass);
|
2002-11-22 22:44:32 +00:00
|
|
|
PhysReg = getFreeReg(VirtReg);
|
|
|
|
|
2002-12-02 21:11:58 +00:00
|
|
|
// Add move instruction(s)
|
2002-12-15 20:36:09 +00:00
|
|
|
++NumReloaded;
|
2002-12-15 20:06:35 +00:00
|
|
|
return RegInfo->loadRegOffset2Reg(MBB, I, PhysReg,
|
2002-12-02 21:11:58 +00:00
|
|
|
RegInfo->getFramePointer(),
|
2002-12-04 23:58:08 +00:00
|
|
|
-stackOffset, regClass->getDataSize());
|
2002-11-22 22:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator
|
2002-12-15 19:51:14 +00:00
|
|
|
RegAllocSimple::saveVirtRegToStack (MachineBasicBlock &MBB,
|
2002-12-13 09:54:36 +00:00
|
|
|
MachineBasicBlock::iterator I,
|
2002-12-03 23:15:19 +00:00
|
|
|
unsigned VirtReg, unsigned PhysReg)
|
2002-11-22 22:44:32 +00:00
|
|
|
{
|
|
|
|
const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
|
2002-12-04 23:58:08 +00:00
|
|
|
unsigned stackOffset = allocateStackSpaceFor(VirtReg, regClass);
|
2002-12-02 21:11:58 +00:00
|
|
|
|
2002-11-22 22:44:32 +00:00
|
|
|
// Add move instruction(s)
|
2002-12-15 20:36:09 +00:00
|
|
|
++NumSpilled;
|
2002-12-15 20:06:35 +00:00
|
|
|
return RegInfo->storeReg2RegOffset(MBB, I, PhysReg,
|
2002-11-22 22:44:32 +00:00
|
|
|
RegInfo->getFramePointer(),
|
2002-12-04 23:58:08 +00:00
|
|
|
-stackOffset, regClass->getDataSize());
|
2002-11-22 22:44:32 +00:00
|
|
|
}
|
|
|
|
|
2002-12-03 23:15:19 +00:00
|
|
|
|
2002-12-15 19:51:14 +00:00
|
|
|
/// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
|
|
|
|
/// predecessor basic blocks.
|
|
|
|
void RegAllocSimple::EliminatePHINodes(MachineBasicBlock &MBB) {
|
2002-12-15 20:36:09 +00:00
|
|
|
const MachineInstrInfo &MII = TM.getInstrInfo();
|
|
|
|
|
2002-12-15 19:51:14 +00:00
|
|
|
while (MBB.front()->getOpcode() == 0) {
|
|
|
|
MachineInstr *MI = MBB.front();
|
2002-12-15 20:36:09 +00:00
|
|
|
// Unlink the PHI node from the basic block... but don't delete the PHI
|
2002-12-15 19:51:14 +00:00
|
|
|
MBB.erase(MBB.begin());
|
2002-12-13 09:54:36 +00:00
|
|
|
|
2002-12-15 19:51:14 +00:00
|
|
|
// a preliminary pass that will invalidate any registers that
|
|
|
|
// are used by the instruction (including implicit uses)
|
|
|
|
invalidatePhysRegs(MI);
|
|
|
|
|
|
|
|
DEBUG(std::cerr << "num invalid regs: " << RegsUsed.size() << "\n");
|
|
|
|
DEBUG(std::cerr << "num ops: " << MI->getNumOperands() << "\n");
|
|
|
|
MachineOperand &targetReg = MI->getOperand(0);
|
|
|
|
|
2002-12-15 20:36:09 +00:00
|
|
|
// If it's a virtual register, allocate a physical one otherwise, just use
|
|
|
|
// whatever register is there now note: it MUST be a register -- we're
|
|
|
|
// assigning to it!
|
|
|
|
//
|
2002-12-15 19:51:14 +00:00
|
|
|
unsigned virtualReg = (unsigned) targetReg.getAllocatedRegNum();
|
|
|
|
unsigned physReg;
|
|
|
|
if (targetReg.isVirtualRegister()) {
|
|
|
|
physReg = getFreeReg(virtualReg);
|
|
|
|
} else {
|
|
|
|
physReg = virtualReg;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the register class of the target register: should be the
|
|
|
|
// same as the values we're trying to store there
|
|
|
|
const TargetRegisterClass* regClass = PhysRegClasses[physReg];
|
|
|
|
assert(regClass && "Target register class not found!");
|
|
|
|
unsigned dataSize = regClass->getDataSize();
|
2002-12-15 20:48:03 +00:00
|
|
|
|
2002-12-15 19:51:14 +00:00
|
|
|
for (int i = MI->getNumOperands() - 1; i >= 2; i-=2) {
|
|
|
|
MachineOperand &opVal = MI->getOperand(i-1);
|
|
|
|
|
|
|
|
// Get the MachineBasicBlock equivalent of the BasicBlock that is the
|
|
|
|
// source path the phi
|
|
|
|
MachineBasicBlock &opBlock = *MI->getOperand(i).getMachineBasicBlock();
|
2002-12-15 20:48:03 +00:00
|
|
|
|
|
|
|
// Check to make sure we haven't already emitted the copy for this block.
|
|
|
|
// This can happen because PHI nodes may have multiple entries for the
|
|
|
|
// same basic block. It doesn't matter which entry we use though, because
|
|
|
|
// all incoming values are guaranteed to be the same for a particular bb.
|
|
|
|
//
|
|
|
|
// Note that this is N^2 in the number of phi node entries, but since the
|
|
|
|
// # of entries is tiny, this is not a problem.
|
|
|
|
//
|
|
|
|
bool HaveNotEmitted = true;
|
|
|
|
for (int op = MI->getNumOperands() - 1; op != i; op -= 2)
|
|
|
|
if (&opBlock == MI->getOperand(op).getMachineBasicBlock()) {
|
|
|
|
HaveNotEmitted = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HaveNotEmitted) {
|
|
|
|
MachineBasicBlock::iterator opI = opBlock.end();
|
|
|
|
MachineInstr *opMI = *--opI;
|
|
|
|
|
|
|
|
// must backtrack over ALL the branches in the previous block
|
|
|
|
while (MII.isBranch(opMI->getOpcode()) && opI != opBlock.begin())
|
|
|
|
opMI = *--opI;
|
|
|
|
|
|
|
|
// move back to the first branch instruction so new instructions
|
|
|
|
// are inserted right in front of it and not in front of a non-branch
|
|
|
|
if (!MII.isBranch(opMI->getOpcode()))
|
|
|
|
++opI;
|
|
|
|
|
|
|
|
// Retrieve the constant value from this op, move it to target
|
|
|
|
// register of the phi
|
|
|
|
if (opVal.isImmediate()) {
|
|
|
|
opI = RegInfo->moveImm2Reg(opBlock, opI, physReg,
|
|
|
|
(unsigned) opVal.getImmedValue(),
|
|
|
|
dataSize);
|
|
|
|
} else {
|
|
|
|
// Allocate a physical register and add a move in the BB
|
2002-12-15 21:13:12 +00:00
|
|
|
unsigned opVirtualReg = opVal.getAllocatedRegNum();
|
2002-12-15 20:48:03 +00:00
|
|
|
unsigned opPhysReg;
|
|
|
|
opI = moveUseToReg(opBlock, opI, opVirtualReg, physReg);
|
|
|
|
|
|
|
|
}
|
2002-12-15 21:33:51 +00:00
|
|
|
|
|
|
|
// Save that register value to the stack of the TARGET REG
|
|
|
|
saveVirtRegToStack(opBlock, opI, virtualReg, physReg);
|
2002-12-13 09:54:36 +00:00
|
|
|
}
|
2002-12-15 20:48:03 +00:00
|
|
|
|
2002-12-15 19:51:14 +00:00
|
|
|
// make regs available to other instructions
|
|
|
|
clearAllRegs();
|
2002-12-13 09:54:36 +00:00
|
|
|
}
|
2002-12-15 19:51:14 +00:00
|
|
|
|
|
|
|
// really delete the instruction
|
|
|
|
delete MI;
|
|
|
|
}
|
|
|
|
}
|
2002-12-13 09:54:36 +00:00
|
|
|
|
2002-12-15 19:51:14 +00:00
|
|
|
|
|
|
|
void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
|
|
|
|
// Handle PHI instructions specially: add moves to each pred block
|
|
|
|
EliminatePHINodes(MBB);
|
|
|
|
|
2002-12-15 21:33:51 +00:00
|
|
|
// loop over each instruction
|
2002-12-15 19:51:14 +00:00
|
|
|
for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
|
2002-12-15 21:24:30 +00:00
|
|
|
// Made to combat the incorrect allocation of r2 = add r1, r1
|
|
|
|
std::map<unsigned, unsigned> VirtReg2PhysRegMap;
|
|
|
|
|
2002-12-15 19:51:14 +00:00
|
|
|
MachineInstr *MI = *I;
|
|
|
|
|
|
|
|
// a preliminary pass that will invalidate any registers that
|
|
|
|
// are used by the instruction (including implicit uses)
|
|
|
|
invalidatePhysRegs(MI);
|
|
|
|
|
|
|
|
// Loop over uses, move from memory into registers
|
|
|
|
for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
|
|
|
|
MachineOperand &op = MI->getOperand(i);
|
|
|
|
|
2002-12-15 20:36:09 +00:00
|
|
|
if (op.isVirtualRegister()) {
|
2002-12-15 19:51:14 +00:00
|
|
|
unsigned virtualReg = (unsigned) op.getAllocatedRegNum();
|
|
|
|
DEBUG(std::cerr << "op: " << op << "\n");
|
|
|
|
DEBUG(std::cerr << "\t inst[" << i << "]: ";
|
|
|
|
MI->print(std::cerr, TM));
|
|
|
|
|
|
|
|
// make sure the same virtual register maps to the same physical
|
|
|
|
// register in any given instruction
|
|
|
|
unsigned physReg;
|
|
|
|
if (VirtReg2PhysRegMap.find(virtualReg) != VirtReg2PhysRegMap.end()) {
|
|
|
|
physReg = VirtReg2PhysRegMap[virtualReg];
|
|
|
|
} else {
|
|
|
|
if (op.opIsDef()) {
|
|
|
|
if (TM.getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
|
|
|
|
// must be same register number as the first operand
|
|
|
|
// This maps a = b + c into b += c, and saves b into a's spot
|
2002-12-15 21:02:20 +00:00
|
|
|
assert(MI->getOperand(1).isRegister() &&
|
|
|
|
MI->getOperand(1).getAllocatedRegNum() &&
|
|
|
|
MF->getRegClass(virtualReg) ==
|
|
|
|
PhysRegClasses[MI->getOperand(1).getAllocatedRegNum()] &&
|
|
|
|
"Two address instruction invalid!");
|
|
|
|
|
2002-12-15 20:36:09 +00:00
|
|
|
physReg = MI->getOperand(1).getAllocatedRegNum();
|
2002-12-12 23:20:31 +00:00
|
|
|
} else {
|
2002-12-15 19:51:14 +00:00
|
|
|
physReg = getFreeReg(virtualReg);
|
2002-12-12 23:20:31 +00:00
|
|
|
}
|
2002-12-15 19:51:14 +00:00
|
|
|
MachineBasicBlock::iterator J = I;
|
|
|
|
J = saveVirtRegToStack(MBB, ++J, virtualReg, physReg);
|
|
|
|
I = --J;
|
|
|
|
} else {
|
|
|
|
I = moveUseToReg(MBB, I, virtualReg, physReg);
|
2002-12-02 21:11:58 +00:00
|
|
|
}
|
2002-12-15 19:51:14 +00:00
|
|
|
VirtReg2PhysRegMap[virtualReg] = physReg;
|
2002-11-22 22:44:32 +00:00
|
|
|
}
|
2002-12-15 19:51:14 +00:00
|
|
|
MI->SetMachineOperandReg(i, physReg);
|
|
|
|
DEBUG(std::cerr << "virt: " << virtualReg <<
|
|
|
|
", phys: " << op.getAllocatedRegNum() << "\n");
|
2002-11-22 22:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
2002-12-15 19:51:14 +00:00
|
|
|
clearAllRegs();
|
2002-11-22 22:44:32 +00:00
|
|
|
}
|
2002-12-15 19:51:14 +00:00
|
|
|
}
|
|
|
|
|
2002-12-15 20:36:09 +00:00
|
|
|
/// runOnMachineFunction - Register allocate the whole function
|
|
|
|
///
|
2002-12-15 19:51:14 +00:00
|
|
|
bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
|
|
|
|
DEBUG(std::cerr << "Machine Function " << "\n");
|
|
|
|
MF = &Fn;
|
|
|
|
|
|
|
|
for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
|
|
|
|
MBB != MBBe; ++MBB)
|
|
|
|
AllocateBasicBlock(*MBB);
|
2002-11-22 22:44:32 +00:00
|
|
|
|
2002-12-04 23:58:08 +00:00
|
|
|
// add prologue we should preserve callee-save registers...
|
2002-12-15 20:06:35 +00:00
|
|
|
RegInfo->emitPrologue(Fn, NumBytesAllocated);
|
2002-12-04 23:58:08 +00:00
|
|
|
|
2002-12-15 19:51:14 +00:00
|
|
|
const MachineInstrInfo &MII = TM.getInstrInfo();
|
|
|
|
|
2002-12-04 23:58:08 +00:00
|
|
|
// add epilogue to restore the callee-save registers
|
|
|
|
// loop over the basic block
|
|
|
|
for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
|
2002-12-15 19:51:14 +00:00
|
|
|
MBB != MBBe; ++MBB) {
|
2002-12-04 23:58:08 +00:00
|
|
|
// check if last instruction is a RET
|
2002-12-15 20:36:09 +00:00
|
|
|
if (MII.isReturn(MBB->back()->getOpcode())) {
|
2002-12-04 23:58:08 +00:00
|
|
|
// this block has a return instruction, add epilogue
|
2002-12-15 20:06:35 +00:00
|
|
|
RegInfo->emitEpilogue(*MBB, NumBytesAllocated);
|
2002-12-04 23:58:08 +00:00
|
|
|
}
|
|
|
|
}
|
2002-11-22 22:44:32 +00:00
|
|
|
|
2002-12-15 19:51:14 +00:00
|
|
|
cleanupAfterFunction();
|
2002-11-22 22:44:32 +00:00
|
|
|
return false; // We never modify the LLVM itself.
|
|
|
|
}
|
|
|
|
|
|
|
|
Pass *createSimpleX86RegisterAllocator(TargetMachine &TM) {
|
|
|
|
return new RegAllocSimple(TM);
|
|
|
|
}
|