2002-11-22 22:43:47 +00:00
|
|
|
//===- X86RegisterInfo.cpp - X86 Register Information -----------*- C++ -*-===//
|
2002-10-25 22:55:53 +00:00
|
|
|
//
|
|
|
|
// This file contains the X86 implementation of the MRegisterInfo class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-11-20 18:59:43 +00:00
|
|
|
#include "X86.h"
|
2002-10-25 22:55:53 +00:00
|
|
|
#include "X86RegisterInfo.h"
|
2002-11-22 22:43:47 +00:00
|
|
|
#include "X86InstrBuilder.h"
|
2002-11-20 18:59:43 +00:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Type.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2002-10-25 22:55:53 +00:00
|
|
|
|
|
|
|
// X86Regs - Turn the X86RegisterInfo.def file into a bunch of register
|
|
|
|
// descriptors
|
|
|
|
//
|
|
|
|
static const MRegisterDesc X86Regs[] = {
|
|
|
|
#define R(ENUM, NAME, FLAGS, TSFLAGS) { NAME, FLAGS, TSFLAGS },
|
|
|
|
#include "X86RegisterInfo.def"
|
|
|
|
};
|
|
|
|
|
|
|
|
X86RegisterInfo::X86RegisterInfo()
|
|
|
|
: MRegisterInfo(X86Regs, sizeof(X86Regs)/sizeof(X86Regs[0])) {
|
|
|
|
}
|
2002-11-20 18:59:43 +00:00
|
|
|
|
2002-12-13 12:00:06 +00:00
|
|
|
unsigned getIdx(unsigned dataSize) {
|
|
|
|
switch (dataSize) {
|
|
|
|
case 1: return 0;
|
|
|
|
case 2: return 1;
|
|
|
|
case 4: return 2;
|
|
|
|
// FIXME: longs handled as ints
|
|
|
|
case 8: return 2;
|
|
|
|
default: assert(0 && "Invalid data size!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-22 22:43:47 +00:00
|
|
|
MachineBasicBlock::iterator
|
|
|
|
X86RegisterInfo::storeReg2RegOffset(MachineBasicBlock *MBB,
|
2002-12-04 17:14:13 +00:00
|
|
|
MachineBasicBlock::iterator MBBI,
|
2002-11-22 22:43:47 +00:00
|
|
|
unsigned SrcReg, unsigned DestReg,
|
|
|
|
unsigned ImmOffset, unsigned dataSize)
|
|
|
|
const
|
2002-11-20 18:59:43 +00:00
|
|
|
{
|
2002-12-13 12:00:06 +00:00
|
|
|
static const unsigned Opcode[] = { X86::MOVrm8, X86::MOVrm16, X86::MOVrm32 };
|
|
|
|
MachineInstr *MI = addRegOffset(BuildMI(Opcode[getIdx(dataSize)], 5),
|
2002-12-02 21:10:35 +00:00
|
|
|
DestReg, ImmOffset).addReg(SrcReg);
|
2002-11-22 22:43:47 +00:00
|
|
|
return ++(MBB->insert(MBBI, MI));
|
2002-11-20 18:59:43 +00:00
|
|
|
}
|
|
|
|
|
2002-11-22 22:43:47 +00:00
|
|
|
MachineBasicBlock::iterator
|
|
|
|
X86RegisterInfo::loadRegOffset2Reg(MachineBasicBlock *MBB,
|
2002-12-04 17:14:13 +00:00
|
|
|
MachineBasicBlock::iterator MBBI,
|
2002-11-22 22:43:47 +00:00
|
|
|
unsigned DestReg, unsigned SrcReg,
|
|
|
|
unsigned ImmOffset, unsigned dataSize)
|
|
|
|
const
|
2002-11-20 18:59:43 +00:00
|
|
|
{
|
2002-12-13 12:00:06 +00:00
|
|
|
static const unsigned Opcode[] = { X86::MOVmr8, X86::MOVmr16, X86::MOVmr32 };
|
|
|
|
MachineInstr *MI = addRegOffset(BuildMI(Opcode[getIdx(dataSize)], 5)
|
|
|
|
.addReg(DestReg), SrcReg, ImmOffset);
|
2002-11-22 22:43:47 +00:00
|
|
|
return ++(MBB->insert(MBBI, MI));
|
2002-11-20 18:59:43 +00:00
|
|
|
}
|
|
|
|
|
2002-12-13 09:54:12 +00:00
|
|
|
MachineBasicBlock::iterator
|
|
|
|
X86RegisterInfo::moveReg2Reg(MachineBasicBlock *MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
unsigned DestReg, unsigned SrcReg,
|
|
|
|
unsigned dataSize) const
|
|
|
|
{
|
2002-12-13 12:00:06 +00:00
|
|
|
static const unsigned Opcode[] = { X86::MOVrr8, X86::MOVrr16, X86::MOVrr32 };
|
|
|
|
MachineInstr *MI =
|
|
|
|
BuildMI(Opcode[getIdx(dataSize)], 2).addReg(DestReg).addReg(SrcReg);
|
2002-12-13 09:54:12 +00:00
|
|
|
return ++(MBB->insert(MBBI, MI));
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator
|
|
|
|
X86RegisterInfo::moveImm2Reg(MachineBasicBlock *MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
unsigned DestReg, unsigned Imm, unsigned dataSize)
|
|
|
|
const
|
|
|
|
{
|
2002-12-13 12:00:06 +00:00
|
|
|
static const unsigned Opcode[] = { X86::MOVir8, X86::MOVir16, X86::MOVir32 };
|
|
|
|
MachineInstr *MI =
|
|
|
|
BuildMI(Opcode[getIdx(dataSize)], 2).addReg(DestReg).addReg(Imm);
|
2002-12-13 09:54:12 +00:00
|
|
|
return ++(MBB->insert(MBBI, MI));
|
|
|
|
}
|
|
|
|
|
2002-11-22 22:43:47 +00:00
|
|
|
|
|
|
|
unsigned X86RegisterInfo::getFramePointer() const {
|
|
|
|
return X86::EBP;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned X86RegisterInfo::getStackPointer() const {
|
|
|
|
return X86::ESP;
|
|
|
|
}
|
2002-12-03 23:11:21 +00:00
|
|
|
|
|
|
|
const unsigned* X86RegisterInfo::getCalleeSaveRegs() const {
|
2002-12-04 23:57:03 +00:00
|
|
|
static const unsigned CalleeSaveRegs[] = { X86::ESI, X86::EDI, X86::EBX,
|
2002-12-03 23:11:21 +00:00
|
|
|
MRegisterInfo::NoRegister };
|
|
|
|
return CalleeSaveRegs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const unsigned* X86RegisterInfo::getCallerSaveRegs() const {
|
|
|
|
static const unsigned CallerSaveRegs[] = { X86::EAX, X86::ECX, X86::EDX,
|
|
|
|
MRegisterInfo::NoRegister };
|
|
|
|
return CallerSaveRegs;
|
|
|
|
}
|
2002-12-04 23:57:03 +00:00
|
|
|
|
|
|
|
MachineBasicBlock::iterator
|
|
|
|
X86RegisterInfo::emitPrologue(MachineBasicBlock *MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
unsigned numBytes) const
|
|
|
|
{
|
|
|
|
MachineInstr *MI;
|
|
|
|
|
|
|
|
// PUSH ebp
|
|
|
|
MI = BuildMI (X86::PUSHr32, 1).addReg(X86::EBP);
|
|
|
|
MBBI = ++(MBB->insert(MBBI, MI));
|
|
|
|
|
|
|
|
// MOV ebp, esp
|
|
|
|
MI = BuildMI (X86::MOVrr32, 2).addReg(X86::EBP).addReg(X86::ESP);
|
|
|
|
MBBI = ++(MBB->insert(MBBI, MI));
|
|
|
|
|
|
|
|
// adjust stack pointer
|
|
|
|
MI = BuildMI(X86::SUBri32, 2).addReg(X86::ESP).addZImm(numBytes);
|
|
|
|
MBBI = ++(MBB->insert(MBBI, MI));
|
|
|
|
|
|
|
|
// PUSH all callee-save registers
|
|
|
|
const unsigned* regs = getCalleeSaveRegs();
|
|
|
|
while (*regs) {
|
|
|
|
MI = BuildMI(X86::PUSHr32, 1).addReg(*regs);
|
|
|
|
MBBI = ++(MBB->insert(MBBI, MI));
|
|
|
|
++regs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MBBI;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator
|
|
|
|
X86RegisterInfo::emitEpilogue(MachineBasicBlock *MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
unsigned numBytes) const
|
|
|
|
{
|
|
|
|
MachineInstr *MI;
|
|
|
|
|
|
|
|
// POP all callee-save registers in REVERSE ORDER
|
|
|
|
static const unsigned regs[] = { X86::EBX, X86::EDI, X86::ESI,
|
|
|
|
MRegisterInfo::NoRegister };
|
|
|
|
unsigned idx = 0;
|
|
|
|
while (regs[idx]) {
|
|
|
|
MI = BuildMI(X86::POPr32, 1).addReg(regs[idx++]);
|
|
|
|
MBBI = ++(MBB->insert(MBBI, MI));
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert LEAVE
|
|
|
|
MI = BuildMI(X86::LEAVE, 0);
|
|
|
|
MBBI = ++(MBB->insert(MBBI, MI));
|
|
|
|
|
|
|
|
return MBBI;
|
|
|
|
}
|