2002-12-16 15:37:00 +01:00
|
|
|
//===-- RegAllocSimple.cpp - A simple generic register allocator ----------===//
|
2005-04-22 00:36:52 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-22 00:36:52 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-11-22 23:44:32 +01:00
|
|
|
//
|
2002-12-28 21:42:14 +01:00
|
|
|
// This file implements a simple register allocator. *Very* simple: It immediate
|
|
|
|
// spills every value right after it is computed, and it reloads all used
|
|
|
|
// operands from the spill area to temporary registers before each instruction.
|
|
|
|
// It does not keep values in registers across instructions.
|
2002-11-22 23:44:32 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-08-03 23:47:31 +02:00
|
|
|
#define DEBUG_TYPE "regalloc"
|
2003-01-13 01:26:08 +01:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2002-12-28 21:42:14 +01:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2002-12-15 19:19:24 +01:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2002-12-25 06:04:20 +01:00
|
|
|
#include "llvm/CodeGen/SSARegMap.h"
|
2002-12-28 22:08:26 +01:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2003-01-14 23:00:31 +01:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2002-11-22 23:44:32 +01:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2004-02-15 22:38:28 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2002-12-13 11:42:31 +01:00
|
|
|
namespace {
|
2004-02-19 07:19:09 +01:00
|
|
|
Statistic<> NumStores("ra-simple", "Number of stores added");
|
|
|
|
Statistic<> NumLoads ("ra-simple", "Number of loads added");
|
2002-12-15 21:36:09 +01:00
|
|
|
|
2002-12-28 21:42:14 +01:00
|
|
|
class RegAllocSimple : public MachineFunctionPass {
|
2002-11-22 23:44:32 +01:00
|
|
|
MachineFunction *MF;
|
2002-12-28 21:42:14 +01:00
|
|
|
const TargetMachine *TM;
|
2002-11-22 23:44:32 +01:00
|
|
|
const MRegisterInfo *RegInfo;
|
2005-01-23 23:55:45 +01:00
|
|
|
bool *PhysRegsEverUsed;
|
2005-04-22 00:36:52 +02:00
|
|
|
|
2002-12-28 21:42:14 +01:00
|
|
|
// StackSlotForVirtReg - Maps SSA Regs => frame index on the stack where
|
|
|
|
// these values are spilled
|
|
|
|
std::map<unsigned, int> StackSlotForVirtReg;
|
2002-11-22 23:44:32 +01:00
|
|
|
|
2002-12-28 21:42:14 +01:00
|
|
|
// RegsUsed - Keep track of what registers are currently in use. This is a
|
|
|
|
// bitset.
|
|
|
|
std::vector<bool> RegsUsed;
|
2002-12-15 21:36:09 +01:00
|
|
|
|
|
|
|
// 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 23:44:32 +01:00
|
|
|
std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
|
|
|
|
|
2002-12-15 21:36:09 +01:00
|
|
|
public:
|
2002-12-15 22:13:12 +01:00
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "Simple Register Allocator";
|
|
|
|
}
|
|
|
|
|
2002-12-15 21:36:09 +01:00
|
|
|
/// runOnMachineFunction - Register allocate the whole function
|
|
|
|
bool runOnMachineFunction(MachineFunction &Fn);
|
|
|
|
|
2003-01-13 01:26:08 +01:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequiredID(PHIEliminationID); // Eliminate PHI nodes
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2002-12-28 21:42:14 +01:00
|
|
|
private:
|
2002-12-15 21:36:09 +01:00
|
|
|
/// AllocateBasicBlock - Register allocate the specified basic block.
|
|
|
|
void AllocateBasicBlock(MachineBasicBlock &MBB);
|
|
|
|
|
2002-12-15 23:19:19 +01:00
|
|
|
/// getStackSpaceFor - This returns the offset of the specified virtual
|
2003-08-18 16:43:39 +02:00
|
|
|
/// register on the stack, allocating space if necessary.
|
2002-12-28 21:42:14 +01:00
|
|
|
int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
|
2002-12-02 22:11:58 +01:00
|
|
|
|
2002-12-15 23:19:19 +01:00
|
|
|
/// Given a virtual register, return a compatible physical register that is
|
|
|
|
/// currently unused.
|
2002-12-15 21:36:09 +01:00
|
|
|
///
|
2002-11-22 23:44:32 +01:00
|
|
|
/// Side effect: marks that register as being used until manually cleared
|
2002-12-15 21:36:09 +01:00
|
|
|
///
|
2002-11-22 23:44:32 +01:00
|
|
|
unsigned getFreeReg(unsigned virtualReg);
|
|
|
|
|
|
|
|
/// Moves value from memory into that register
|
2002-12-16 00:01:26 +01:00
|
|
|
unsigned reloadVirtReg(MachineBasicBlock &MBB,
|
2004-02-23 05:12:30 +01:00
|
|
|
MachineBasicBlock::iterator I, unsigned VirtReg);
|
2002-11-22 23:44:32 +01:00
|
|
|
|
|
|
|
/// Saves reg value on the stack (maps virtual register to stack value)
|
2004-02-23 05:12:30 +01:00
|
|
|
void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
|
2002-12-16 00:01:26 +01:00
|
|
|
unsigned VirtReg, unsigned PhysReg);
|
2002-11-22 23:44:32 +01:00
|
|
|
};
|
|
|
|
|
2002-12-13 11:42:31 +01:00
|
|
|
}
|
2002-11-22 23:44:32 +01:00
|
|
|
|
2002-12-15 23:19:19 +01:00
|
|
|
/// getStackSpaceFor - This allocates space for the specified virtual
|
2002-12-15 20:51:14 +01:00
|
|
|
/// register to be held on the stack.
|
2002-12-28 21:42:14 +01:00
|
|
|
int RegAllocSimple::getStackSpaceFor(unsigned VirtReg,
|
2005-04-22 06:01:18 +02:00
|
|
|
const TargetRegisterClass *RC) {
|
2002-12-15 23:19:19 +01:00
|
|
|
// Find the location VirtReg would belong...
|
2002-12-28 21:42:14 +01:00
|
|
|
std::map<unsigned, int>::iterator I =
|
|
|
|
StackSlotForVirtReg.lower_bound(VirtReg);
|
2002-12-15 23:19:19 +01:00
|
|
|
|
2002-12-28 21:42:14 +01:00
|
|
|
if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
|
2002-12-15 23:19:19 +01:00
|
|
|
return I->second; // Already has space allocated?
|
|
|
|
|
2002-12-28 21:42:14 +01:00
|
|
|
// Allocate a new stack object for this spill location...
|
2004-08-16 00:02:22 +02:00
|
|
|
int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
|
|
|
|
RC->getAlignment());
|
2005-04-22 00:36:52 +02:00
|
|
|
|
2002-12-15 23:19:19 +01:00
|
|
|
// Assign the slot...
|
2002-12-28 21:42:14 +01:00
|
|
|
StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
|
|
|
|
|
|
|
|
return FrameIdx;
|
2002-12-02 22:11:58 +01:00
|
|
|
}
|
|
|
|
|
2002-11-22 23:44:32 +01:00
|
|
|
unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
|
2002-12-25 06:04:20 +01:00
|
|
|
const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(virtualReg);
|
2002-12-28 21:42:14 +01:00
|
|
|
TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
|
|
|
|
TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
|
|
|
|
|
|
|
|
while (1) {
|
2005-04-22 00:36:52 +02:00
|
|
|
unsigned regIdx = RegClassIdx[RC]++;
|
2002-12-28 21:42:14 +01:00
|
|
|
assert(RI+regIdx != RE && "Not enough registers!");
|
|
|
|
unsigned PhysReg = *(RI+regIdx);
|
2005-04-22 00:36:52 +02:00
|
|
|
|
2005-01-23 23:55:45 +01:00
|
|
|
if (!RegsUsed[PhysReg]) {
|
|
|
|
PhysRegsEverUsed[PhysReg] = true;
|
2002-12-28 21:42:14 +01:00
|
|
|
return PhysReg;
|
2005-01-23 23:55:45 +01:00
|
|
|
}
|
2002-12-28 21:42:14 +01:00
|
|
|
}
|
2002-11-22 23:44:32 +01:00
|
|
|
}
|
|
|
|
|
2002-12-16 00:01:26 +01:00
|
|
|
unsigned RegAllocSimple::reloadVirtReg(MachineBasicBlock &MBB,
|
2004-02-23 05:12:30 +01:00
|
|
|
MachineBasicBlock::iterator I,
|
2002-12-16 00:01:26 +01:00
|
|
|
unsigned VirtReg) {
|
2002-12-25 06:04:20 +01:00
|
|
|
const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(VirtReg);
|
2002-12-28 21:42:14 +01:00
|
|
|
int FrameIdx = getStackSpaceFor(VirtReg, RC);
|
2002-12-16 00:01:26 +01:00
|
|
|
unsigned PhysReg = getFreeReg(VirtReg);
|
2002-11-22 23:44:32 +01:00
|
|
|
|
2002-12-02 22:11:58 +01:00
|
|
|
// Add move instruction(s)
|
2004-02-19 07:19:09 +01:00
|
|
|
++NumLoads;
|
2005-09-30 03:29:00 +02:00
|
|
|
RegInfo->loadRegFromStackSlot(MBB, I, PhysReg, FrameIdx, RC);
|
2002-12-16 00:01:26 +01:00
|
|
|
return PhysReg;
|
2002-11-22 23:44:32 +01:00
|
|
|
}
|
|
|
|
|
2002-12-16 00:01:26 +01:00
|
|
|
void RegAllocSimple::spillVirtReg(MachineBasicBlock &MBB,
|
2004-02-23 05:12:30 +01:00
|
|
|
MachineBasicBlock::iterator I,
|
2002-12-28 21:42:14 +01:00
|
|
|
unsigned VirtReg, unsigned PhysReg) {
|
2002-12-25 06:04:20 +01:00
|
|
|
const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(VirtReg);
|
2002-12-28 21:42:14 +01:00
|
|
|
int FrameIdx = getStackSpaceFor(VirtReg, RC);
|
2002-12-02 22:11:58 +01:00
|
|
|
|
2002-11-22 23:44:32 +01:00
|
|
|
// Add move instruction(s)
|
2004-02-19 07:19:09 +01:00
|
|
|
++NumStores;
|
2005-09-30 03:29:00 +02:00
|
|
|
RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIdx, RC);
|
2002-11-22 23:44:32 +01:00
|
|
|
}
|
|
|
|
|
2002-12-04 00:15:19 +01:00
|
|
|
|
2002-12-15 20:51:14 +01:00
|
|
|
void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
|
2002-12-15 22:33:51 +01:00
|
|
|
// loop over each instruction
|
2004-02-12 03:27:10 +01:00
|
|
|
for (MachineBasicBlock::iterator MI = MBB.begin(); MI != MBB.end(); ++MI) {
|
2002-12-15 22:24:30 +01:00
|
|
|
// Made to combat the incorrect allocation of r2 = add r1, r1
|
2002-12-15 23:19:19 +01:00
|
|
|
std::map<unsigned, unsigned> Virt2PhysRegMap;
|
2002-12-15 22:24:30 +01:00
|
|
|
|
2004-02-15 22:37:17 +01:00
|
|
|
RegsUsed.resize(RegInfo->getNumRegs());
|
2005-04-22 00:36:52 +02:00
|
|
|
|
2005-01-23 23:55:45 +01:00
|
|
|
// This is a preliminary pass that will invalidate any registers that are
|
|
|
|
// used by the instruction (including implicit uses).
|
2002-12-28 21:42:14 +01:00
|
|
|
unsigned Opcode = MI->getOpcode();
|
2004-06-02 07:57:12 +02:00
|
|
|
const TargetInstrDescriptor &Desc = TM->getInstrInfo()->get(Opcode);
|
2005-01-23 23:55:45 +01:00
|
|
|
const unsigned *Regs;
|
|
|
|
for (Regs = Desc.ImplicitUses; *Regs; ++Regs)
|
|
|
|
RegsUsed[*Regs] = true;
|
2005-04-22 00:36:52 +02:00
|
|
|
|
2005-01-23 23:55:45 +01:00
|
|
|
for (Regs = Desc.ImplicitDefs; *Regs; ++Regs) {
|
|
|
|
RegsUsed[*Regs] = true;
|
|
|
|
PhysRegsEverUsed[*Regs] = true;
|
|
|
|
}
|
2005-04-22 00:36:52 +02:00
|
|
|
|
2005-01-23 23:55:45 +01:00
|
|
|
// Loop over uses, move from memory into registers.
|
2002-12-15 20:51:14 +01:00
|
|
|
for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
|
|
|
|
MachineOperand &op = MI->getOperand(i);
|
2005-04-22 00:36:52 +02:00
|
|
|
|
2004-03-16 02:45:55 +01:00
|
|
|
if (op.isRegister() && op.getReg() &&
|
|
|
|
MRegisterInfo::isVirtualRegister(op.getReg())) {
|
2004-02-13 22:01:20 +01:00
|
|
|
unsigned virtualReg = (unsigned) op.getReg();
|
2002-12-15 20:51:14 +01:00
|
|
|
DEBUG(std::cerr << "op: " << op << "\n");
|
|
|
|
DEBUG(std::cerr << "\t inst[" << i << "]: ";
|
2004-06-25 02:13:11 +02:00
|
|
|
MI->print(std::cerr, TM));
|
2005-04-22 00:36:52 +02:00
|
|
|
|
2002-12-15 20:51:14 +01:00
|
|
|
// make sure the same virtual register maps to the same physical
|
|
|
|
// register in any given instruction
|
2002-12-15 23:19:19 +01:00
|
|
|
unsigned physReg = Virt2PhysRegMap[virtualReg];
|
|
|
|
if (physReg == 0) {
|
2003-12-14 14:24:17 +01:00
|
|
|
if (op.isDef()) {
|
2004-06-02 07:57:12 +02:00
|
|
|
if (!TM->getInstrInfo()->isTwoAddrInstr(MI->getOpcode()) || i) {
|
2004-02-15 22:38:28 +01:00
|
|
|
physReg = getFreeReg(virtualReg);
|
|
|
|
} else {
|
2002-12-15 20:51:14 +01:00
|
|
|
// 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 22:02:20 +01:00
|
|
|
assert(MI->getOperand(1).isRegister() &&
|
2004-02-13 22:01:20 +01:00
|
|
|
MI->getOperand(1).getReg() &&
|
2003-12-14 14:24:17 +01:00
|
|
|
MI->getOperand(1).isUse() &&
|
2002-12-15 22:02:20 +01:00
|
|
|
"Two address instruction invalid!");
|
|
|
|
|
2004-02-13 22:01:20 +01:00
|
|
|
physReg = MI->getOperand(1).getReg();
|
2004-02-23 05:12:30 +01:00
|
|
|
spillVirtReg(MBB, next(MI), virtualReg, physReg);
|
2004-02-15 22:38:28 +01:00
|
|
|
MI->getOperand(1).setDef();
|
|
|
|
MI->RemoveOperand(0);
|
|
|
|
break; // This is the last operand to process
|
2002-12-13 00:20:31 +01:00
|
|
|
}
|
2004-02-23 05:12:30 +01:00
|
|
|
spillVirtReg(MBB, next(MI), virtualReg, physReg);
|
2002-12-15 20:51:14 +01:00
|
|
|
} else {
|
2004-02-12 03:27:10 +01:00
|
|
|
physReg = reloadVirtReg(MBB, MI, virtualReg);
|
2002-12-16 00:01:26 +01:00
|
|
|
Virt2PhysRegMap[virtualReg] = physReg;
|
2002-12-02 22:11:58 +01:00
|
|
|
}
|
2002-11-22 23:44:32 +01:00
|
|
|
}
|
2002-12-15 20:51:14 +01:00
|
|
|
MI->SetMachineOperandReg(i, physReg);
|
2005-04-22 00:36:52 +02:00
|
|
|
DEBUG(std::cerr << "virt: " << virtualReg <<
|
2004-02-13 22:01:20 +01:00
|
|
|
", phys: " << op.getReg() << "\n");
|
2002-11-22 23:44:32 +01:00
|
|
|
}
|
|
|
|
}
|
2002-12-28 21:42:14 +01:00
|
|
|
RegClassIdx.clear();
|
|
|
|
RegsUsed.clear();
|
2002-12-17 05:19:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-15 21:36:09 +01:00
|
|
|
/// runOnMachineFunction - Register allocate the whole function
|
|
|
|
///
|
2002-12-15 20:51:14 +01:00
|
|
|
bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
|
|
|
|
DEBUG(std::cerr << "Machine Function " << "\n");
|
|
|
|
MF = &Fn;
|
2002-12-28 21:42:14 +01:00
|
|
|
TM = &MF->getTarget();
|
|
|
|
RegInfo = TM->getRegisterInfo();
|
2002-12-15 20:51:14 +01:00
|
|
|
|
2005-01-23 23:55:45 +01:00
|
|
|
PhysRegsEverUsed = new bool[RegInfo->getNumRegs()];
|
|
|
|
std::fill(PhysRegsEverUsed, PhysRegsEverUsed+RegInfo->getNumRegs(), false);
|
|
|
|
Fn.setUsedPhysRegs(PhysRegsEverUsed);
|
|
|
|
|
2002-12-15 23:19:19 +01:00
|
|
|
// Loop over all of the basic blocks, eliminating virtual register references
|
2002-12-15 20:51:14 +01:00
|
|
|
for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
|
|
|
|
MBB != MBBe; ++MBB)
|
|
|
|
AllocateBasicBlock(*MBB);
|
2002-11-22 23:44:32 +01:00
|
|
|
|
2002-12-28 21:42:14 +01:00
|
|
|
StackSlotForVirtReg.clear();
|
2002-12-15 23:19:19 +01:00
|
|
|
return true;
|
2002-11-22 23:44:32 +01:00
|
|
|
}
|
|
|
|
|
2004-02-15 22:38:28 +01:00
|
|
|
FunctionPass *llvm::createSimpleRegisterAllocator() {
|
2002-12-28 21:42:14 +01:00
|
|
|
return new RegAllocSimple();
|
2002-11-22 23:44:32 +01:00
|
|
|
}
|