2002-12-16 22:37:00 +08:00
|
|
|
//===-- RegAllocSimple.cpp - A simple generic register allocator ----------===//
|
2005-04-22 06:36:52 +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.
|
2005-04-22 06:36:52 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-11-23 06:44:32 +08:00
|
|
|
//
|
2002-12-29 04:42:14 +08: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-23 06:44:32 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-08-04 05:47:31 +08:00
|
|
|
#define DEBUG_TYPE "regalloc"
|
2003-01-13 08:26:08 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2002-12-29 04:42:14 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2002-12-16 02:19:24 +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"
|
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-11-23 06:44:32 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2006-08-27 20:54:02 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2008-03-22 07:51:57 +08:00
|
|
|
#include <map>
|
2004-02-16 05:38:28 +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");
|
2002-12-16 04:36:09 +08:00
|
|
|
|
2006-12-20 06:41:21 +08:00
|
|
|
namespace {
|
2006-08-01 22:21:23 +08:00
|
|
|
static RegisterRegAlloc
|
2008-10-15 04:25:08 +08:00
|
|
|
simpleRegAlloc("simple", "simple register allocator",
|
2006-08-01 22:21:23 +08:00
|
|
|
createSimpleRegisterAllocator);
|
|
|
|
|
2006-06-29 06:17:39 +08:00
|
|
|
class VISIBILITY_HIDDEN RegAllocSimple : 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
|
|
|
RegAllocSimple() : MachineFunctionPass(&ID) {}
|
2007-05-02 05:15:47 +08:00
|
|
|
private:
|
2002-11-23 06:44:32 +08:00
|
|
|
MachineFunction *MF;
|
2002-12-29 04:42:14 +08:00
|
|
|
const TargetMachine *TM;
|
2008-02-11 02:45:23 +08:00
|
|
|
const TargetRegisterInfo *TRI;
|
2008-07-10 03:56:01 +08:00
|
|
|
const TargetInstrInfo *TII;
|
2005-04-22 06:36:52 +08:00
|
|
|
|
2002-12-29 04:42:14 +08:00
|
|
|
// StackSlotForVirtReg - Maps SSA Regs => frame index on the stack where
|
|
|
|
// these values are spilled
|
|
|
|
std::map<unsigned, int> StackSlotForVirtReg;
|
2002-11-23 06:44:32 +08:00
|
|
|
|
2002-12-29 04:42:14 +08:00
|
|
|
// RegsUsed - Keep track of what registers are currently in use. This is a
|
|
|
|
// bitset.
|
|
|
|
std::vector<bool> RegsUsed;
|
2002-12-16 04:36:09 +08: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-23 06:44:32 +08:00
|
|
|
std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
|
|
|
|
|
2002-12-16 04:36:09 +08:00
|
|
|
public:
|
2002-12-16 05:13:12 +08:00
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "Simple Register Allocator";
|
|
|
|
}
|
|
|
|
|
2002-12-16 04:36:09 +08:00
|
|
|
/// runOnMachineFunction - Register allocate the whole function
|
|
|
|
bool runOnMachineFunction(MachineFunction &Fn);
|
|
|
|
|
2003-01-13 08:26:08 +08:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2009-08-01 07:37:33 +08:00
|
|
|
AU.setPreservesCFG();
|
2003-01-13 08:26:08 +08:00
|
|
|
AU.addRequiredID(PHIEliminationID); // Eliminate PHI nodes
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2002-12-29 04:42:14 +08:00
|
|
|
private:
|
2002-12-16 04:36:09 +08:00
|
|
|
/// AllocateBasicBlock - Register allocate the specified basic block.
|
|
|
|
void AllocateBasicBlock(MachineBasicBlock &MBB);
|
|
|
|
|
2002-12-16 06:19:19 +08:00
|
|
|
/// getStackSpaceFor - This returns the offset of the specified virtual
|
2003-08-18 22:43:39 +08:00
|
|
|
/// register on the stack, allocating space if necessary.
|
2002-12-29 04:42:14 +08:00
|
|
|
int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
|
2002-12-03 05:11:58 +08:00
|
|
|
|
2002-12-16 06:19:19 +08:00
|
|
|
/// Given a virtual register, return a compatible physical register that is
|
|
|
|
/// currently unused.
|
2002-12-16 04:36:09 +08:00
|
|
|
///
|
2002-11-23 06:44:32 +08:00
|
|
|
/// Side effect: marks that register as being used until manually cleared
|
2002-12-16 04:36:09 +08:00
|
|
|
///
|
2002-11-23 06:44:32 +08:00
|
|
|
unsigned getFreeReg(unsigned virtualReg);
|
|
|
|
|
|
|
|
/// Moves value from memory into that register
|
2002-12-16 07:01:26 +08:00
|
|
|
unsigned reloadVirtReg(MachineBasicBlock &MBB,
|
2004-02-23 12:12:30 +08:00
|
|
|
MachineBasicBlock::iterator I, unsigned VirtReg);
|
2002-11-23 06:44:32 +08:00
|
|
|
|
|
|
|
/// Saves reg value on the stack (maps virtual register to stack value)
|
2004-02-23 12:12:30 +08:00
|
|
|
void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
|
2002-12-16 07:01:26 +08:00
|
|
|
unsigned VirtReg, unsigned PhysReg);
|
2002-11-23 06:44:32 +08:00
|
|
|
};
|
2007-05-03 09:11:54 +08:00
|
|
|
char RegAllocSimple::ID = 0;
|
2002-12-13 18:42:31 +08:00
|
|
|
}
|
2002-11-23 06:44:32 +08:00
|
|
|
|
2002-12-16 06:19:19 +08:00
|
|
|
/// getStackSpaceFor - This allocates space for the specified virtual
|
2002-12-16 03:51:14 +08:00
|
|
|
/// register to be held on the stack.
|
2002-12-29 04:42:14 +08:00
|
|
|
int RegAllocSimple::getStackSpaceFor(unsigned VirtReg,
|
2005-04-22 12:01:18 +08:00
|
|
|
const TargetRegisterClass *RC) {
|
2002-12-16 06:19:19 +08:00
|
|
|
// Find the location VirtReg would belong...
|
2008-07-10 03:51:00 +08:00
|
|
|
std::map<unsigned, int>::iterator I = StackSlotForVirtReg.find(VirtReg);
|
2002-12-16 06:19:19 +08:00
|
|
|
|
2008-07-10 03:51:00 +08:00
|
|
|
if (I != StackSlotForVirtReg.end())
|
2002-12-16 06:19:19 +08:00
|
|
|
return I->second; // Already has space allocated?
|
|
|
|
|
2002-12-29 04:42:14 +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(),
|
|
|
|
RC->getAlignment());
|
2005-04-22 06:36:52 +08:00
|
|
|
|
2002-12-16 06:19:19 +08:00
|
|
|
// Assign the slot...
|
2002-12-29 04:42:14 +08:00
|
|
|
StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
|
|
|
|
|
|
|
|
return FrameIdx;
|
2002-12-03 05:11:58 +08:00
|
|
|
}
|
|
|
|
|
2002-11-23 06:44:32 +08:00
|
|
|
unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
|
2007-12-31 12:13:23 +08:00
|
|
|
const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtualReg);
|
2002-12-29 04:42:14 +08:00
|
|
|
TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
|
2008-12-24 05:55:04 +08:00
|
|
|
#ifndef NDEBUG
|
2002-12-29 04:42:14 +08:00
|
|
|
TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
|
2008-12-24 05:55:04 +08:00
|
|
|
#endif
|
2002-12-29 04:42:14 +08:00
|
|
|
|
|
|
|
while (1) {
|
2005-04-22 06:36:52 +08:00
|
|
|
unsigned regIdx = RegClassIdx[RC]++;
|
2002-12-29 04:42:14 +08:00
|
|
|
assert(RI+regIdx != RE && "Not enough registers!");
|
|
|
|
unsigned PhysReg = *(RI+regIdx);
|
2005-04-22 06:36:52 +08:00
|
|
|
|
2005-01-24 06:55:45 +08:00
|
|
|
if (!RegsUsed[PhysReg]) {
|
2007-12-31 12:13:23 +08:00
|
|
|
MF->getRegInfo().setPhysRegUsed(PhysReg);
|
2002-12-29 04:42:14 +08:00
|
|
|
return PhysReg;
|
2005-01-24 06:55:45 +08:00
|
|
|
}
|
2002-12-29 04:42:14 +08:00
|
|
|
}
|
2002-11-23 06:44:32 +08:00
|
|
|
}
|
|
|
|
|
2002-12-16 07:01:26 +08:00
|
|
|
unsigned RegAllocSimple::reloadVirtReg(MachineBasicBlock &MBB,
|
2004-02-23 12:12:30 +08:00
|
|
|
MachineBasicBlock::iterator I,
|
2002-12-16 07:01:26 +08:00
|
|
|
unsigned VirtReg) {
|
2007-12-31 12:13:23 +08:00
|
|
|
const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(VirtReg);
|
2002-12-29 04:42:14 +08:00
|
|
|
int FrameIdx = getStackSpaceFor(VirtReg, RC);
|
2002-12-16 07:01:26 +08:00
|
|
|
unsigned PhysReg = getFreeReg(VirtReg);
|
2002-11-23 06:44:32 +08:00
|
|
|
|
2002-12-03 05:11:58 +08:00
|
|
|
// Add move instruction(s)
|
2004-02-19 14:19:09 +08:00
|
|
|
++NumLoads;
|
2008-01-02 05:11:32 +08:00
|
|
|
TII->loadRegFromStackSlot(MBB, I, PhysReg, FrameIdx, RC);
|
2002-12-16 07:01:26 +08:00
|
|
|
return PhysReg;
|
2002-11-23 06:44:32 +08:00
|
|
|
}
|
|
|
|
|
2002-12-16 07:01:26 +08:00
|
|
|
void RegAllocSimple::spillVirtReg(MachineBasicBlock &MBB,
|
2004-02-23 12:12:30 +08:00
|
|
|
MachineBasicBlock::iterator I,
|
2002-12-29 04:42:14 +08:00
|
|
|
unsigned VirtReg, unsigned PhysReg) {
|
2007-12-31 12:13:23 +08:00
|
|
|
const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(VirtReg);
|
2008-01-02 05:11:32 +08:00
|
|
|
|
2002-12-29 04:42:14 +08:00
|
|
|
int FrameIdx = getStackSpaceFor(VirtReg, RC);
|
2002-12-03 05:11:58 +08:00
|
|
|
|
2002-11-23 06:44:32 +08:00
|
|
|
// Add move instruction(s)
|
2004-02-19 14:19:09 +08:00
|
|
|
++NumStores;
|
2008-01-02 05:11:32 +08:00
|
|
|
TII->storeRegToStackSlot(MBB, I, PhysReg, true, FrameIdx, RC);
|
2002-11-23 06:44:32 +08:00
|
|
|
}
|
|
|
|
|
2002-12-04 07:15:19 +08:00
|
|
|
|
2002-12-16 03:51:14 +08:00
|
|
|
void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
|
2002-12-16 05:33:51 +08:00
|
|
|
// loop over each instruction
|
2004-02-12 10:27:10 +08:00
|
|
|
for (MachineBasicBlock::iterator MI = MBB.begin(); MI != MBB.end(); ++MI) {
|
2002-12-16 05:24:30 +08:00
|
|
|
// Made to combat the incorrect allocation of r2 = add r1, r1
|
2002-12-16 06:19:19 +08:00
|
|
|
std::map<unsigned, unsigned> Virt2PhysRegMap;
|
2002-12-16 05:24:30 +08:00
|
|
|
|
2008-02-11 02:45:23 +08:00
|
|
|
RegsUsed.resize(TRI->getNumRegs());
|
2005-04-22 06:36:52 +08:00
|
|
|
|
2005-01-24 06:55:45 +08:00
|
|
|
// This is a preliminary pass that will invalidate any registers that are
|
|
|
|
// used by the instruction (including implicit uses).
|
2008-01-07 15:27:27 +08:00
|
|
|
const TargetInstrDesc &Desc = MI->getDesc();
|
2005-01-24 06:55:45 +08:00
|
|
|
const unsigned *Regs;
|
2006-07-22 05:15:20 +08:00
|
|
|
if (Desc.ImplicitUses) {
|
|
|
|
for (Regs = Desc.ImplicitUses; *Regs; ++Regs)
|
|
|
|
RegsUsed[*Regs] = true;
|
|
|
|
}
|
2005-04-22 06:36:52 +08:00
|
|
|
|
2006-07-22 05:15:20 +08:00
|
|
|
if (Desc.ImplicitDefs) {
|
|
|
|
for (Regs = Desc.ImplicitDefs; *Regs; ++Regs) {
|
|
|
|
RegsUsed[*Regs] = true;
|
2007-12-31 12:13:23 +08:00
|
|
|
MF->getRegInfo().setPhysRegUsed(*Regs);
|
2006-07-22 05:15:20 +08:00
|
|
|
}
|
2005-01-24 06:55:45 +08:00
|
|
|
}
|
2005-04-22 06:36:52 +08:00
|
|
|
|
2005-01-24 06:55:45 +08:00
|
|
|
// Loop over uses, move from memory into registers.
|
2002-12-16 03:51:14 +08:00
|
|
|
for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
|
2008-07-10 04:12:26 +08:00
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
2005-04-22 06:36:52 +08:00
|
|
|
|
2008-10-03 23:45:36 +08:00
|
|
|
if (MO.isReg() && MO.getReg() &&
|
2008-07-10 04:12:26 +08:00
|
|
|
TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
|
|
|
|
unsigned virtualReg = (unsigned) MO.getReg();
|
|
|
|
DOUT << "op: " << MO << "\n";
|
2006-11-29 06:48:48 +08:00
|
|
|
DOUT << "\t inst[" << i << "]: ";
|
2006-12-08 04:28:15 +08:00
|
|
|
DEBUG(MI->print(*cerr.stream(), TM));
|
2005-04-22 06:36:52 +08:00
|
|
|
|
2002-12-16 03:51:14 +08:00
|
|
|
// make sure the same virtual register maps to the same physical
|
|
|
|
// register in any given instruction
|
2002-12-16 06:19:19 +08:00
|
|
|
unsigned physReg = Virt2PhysRegMap[virtualReg];
|
|
|
|
if (physReg == 0) {
|
2008-07-10 04:12:26 +08:00
|
|
|
if (MO.isDef()) {
|
2009-04-10 01:16:43 +08:00
|
|
|
unsigned TiedOp;
|
|
|
|
if (!MI->isRegTiedToUseOperand(i, &TiedOp)) {
|
2004-02-16 05:38:28 +08:00
|
|
|
physReg = getFreeReg(virtualReg);
|
|
|
|
} else {
|
2006-11-02 07:06:55 +08:00
|
|
|
// must be same register number as the source operand that is
|
|
|
|
// tied to. This maps a = b + c into b = b + c, and saves b into
|
|
|
|
// a's spot.
|
2008-10-03 23:45:36 +08:00
|
|
|
assert(MI->getOperand(TiedOp).isReg() &&
|
2006-11-02 07:06:55 +08:00
|
|
|
MI->getOperand(TiedOp).getReg() &&
|
|
|
|
MI->getOperand(TiedOp).isUse() &&
|
2002-12-16 05:02:20 +08:00
|
|
|
"Two address instruction invalid!");
|
|
|
|
|
2006-11-02 07:06:55 +08:00
|
|
|
physReg = MI->getOperand(TiedOp).getReg();
|
2002-12-13 07:20:31 +08:00
|
|
|
}
|
2004-02-23 12:12:30 +08:00
|
|
|
spillVirtReg(MBB, next(MI), virtualReg, physReg);
|
2002-12-16 03:51:14 +08:00
|
|
|
} else {
|
2004-02-12 10:27:10 +08:00
|
|
|
physReg = reloadVirtReg(MBB, MI, virtualReg);
|
2002-12-16 07:01:26 +08:00
|
|
|
Virt2PhysRegMap[virtualReg] = physReg;
|
2002-12-03 05:11:58 +08:00
|
|
|
}
|
2002-11-23 06:44:32 +08:00
|
|
|
}
|
2008-07-10 04:12:26 +08:00
|
|
|
MO.setReg(physReg);
|
|
|
|
DOUT << "virt: " << virtualReg << ", phys: " << MO.getReg() << "\n";
|
2002-11-23 06:44:32 +08:00
|
|
|
}
|
|
|
|
}
|
2002-12-29 04:42:14 +08:00
|
|
|
RegClassIdx.clear();
|
|
|
|
RegsUsed.clear();
|
2002-12-17 12:19:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-16 04:36:09 +08:00
|
|
|
/// runOnMachineFunction - Register allocate the whole function
|
|
|
|
///
|
2002-12-16 03:51:14 +08:00
|
|
|
bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
|
2006-11-29 06:48:48 +08:00
|
|
|
DOUT << "Machine Function\n";
|
2002-12-16 03:51:14 +08:00
|
|
|
MF = &Fn;
|
2002-12-29 04:42:14 +08:00
|
|
|
TM = &MF->getTarget();
|
2008-02-11 02:45:23 +08:00
|
|
|
TRI = TM->getRegisterInfo();
|
2008-07-10 03:56:01 +08:00
|
|
|
TII = TM->getInstrInfo();
|
2002-12-16 03:51:14 +08:00
|
|
|
|
2002-12-16 06:19:19 +08:00
|
|
|
// Loop over all of the basic blocks, eliminating virtual register references
|
2002-12-16 03:51:14 +08:00
|
|
|
for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
|
|
|
|
MBB != MBBe; ++MBB)
|
|
|
|
AllocateBasicBlock(*MBB);
|
2002-11-23 06:44:32 +08:00
|
|
|
|
2002-12-29 04:42:14 +08:00
|
|
|
StackSlotForVirtReg.clear();
|
2002-12-16 06:19:19 +08:00
|
|
|
return true;
|
2002-11-23 06:44:32 +08:00
|
|
|
}
|
|
|
|
|
2004-02-16 05:38:28 +08:00
|
|
|
FunctionPass *llvm::createSimpleRegisterAllocator() {
|
2002-12-29 04:42:14 +08:00
|
|
|
return new RegAllocSimple();
|
2002-11-23 06:44:32 +08:00
|
|
|
}
|