2009-07-16 13:27:25 +00:00
|
|
|
//===- SystemZRegisterInfo.cpp - SystemZ Register Information -------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains the SystemZ implementation of the TargetRegisterInfo class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SystemZ.h"
|
2009-07-16 14:09:56 +00:00
|
|
|
#include "SystemZInstrInfo.h"
|
2009-07-16 13:50:21 +00:00
|
|
|
#include "SystemZMachineFunctionInfo.h"
|
2009-07-16 13:27:25 +00:00
|
|
|
#include "SystemZRegisterInfo.h"
|
|
|
|
#include "SystemZSubtarget.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2009-07-16 13:51:12 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2009-07-16 13:49:25 +00:00
|
|
|
#include "llvm/Target/TargetFrameInfo.h"
|
2009-07-16 13:49:49 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2009-07-16 13:27:25 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
#include "llvm/ADT/BitVector.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
SystemZRegisterInfo::SystemZRegisterInfo(SystemZTargetMachine &tm,
|
2009-07-16 14:09:56 +00:00
|
|
|
const SystemZInstrInfo &tii)
|
2009-07-16 13:50:21 +00:00
|
|
|
: SystemZGenRegisterInfo(SystemZ::ADJCALLSTACKUP, SystemZ::ADJCALLSTACKDOWN),
|
2009-07-16 13:27:25 +00:00
|
|
|
TM(tm), TII(tii) {
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned*
|
|
|
|
SystemZRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
|
|
|
|
static const unsigned CalleeSavedRegs[] = {
|
2009-07-16 13:35:30 +00:00
|
|
|
SystemZ::R6D, SystemZ::R7D, SystemZ::R8D, SystemZ::R9D,
|
2009-07-16 13:51:12 +00:00
|
|
|
SystemZ::R10D, SystemZ::R11D, SystemZ::R12D, SystemZ::R13D,
|
|
|
|
SystemZ::R14D, SystemZ::R15D,
|
2009-07-16 14:23:01 +00:00
|
|
|
SystemZ::F8L, SystemZ::F9L, SystemZ::F10L, SystemZ::F11L,
|
|
|
|
SystemZ::F12L, SystemZ::F13L, SystemZ::F14L, SystemZ::F15L,
|
2009-07-16 13:27:25 +00:00
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
return CalleeSavedRegs;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitVector SystemZRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
|
|
|
|
BitVector Reserved(getNumRegs());
|
|
|
|
if (hasFP(MF))
|
2009-07-16 13:35:30 +00:00
|
|
|
Reserved.set(SystemZ::R11D);
|
|
|
|
Reserved.set(SystemZ::R14D);
|
|
|
|
Reserved.set(SystemZ::R15D);
|
2009-07-16 13:27:25 +00:00
|
|
|
return Reserved;
|
|
|
|
}
|
|
|
|
|
2009-07-16 13:51:12 +00:00
|
|
|
/// needsFP - Return true if the specified function should have a dedicated
|
|
|
|
/// frame pointer register. This is true if the function has variable sized
|
|
|
|
/// allocas or if frame pointer elimination is disabled.
|
2009-07-16 13:27:25 +00:00
|
|
|
bool SystemZRegisterInfo::hasFP(const MachineFunction &MF) const {
|
|
|
|
const MachineFrameInfo *MFI = MF.getFrameInfo();
|
2010-04-21 03:18:23 +00:00
|
|
|
return DisableFramePointerElim(MF) || MFI->hasVarSizedObjects();
|
2009-07-16 13:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SystemZRegisterInfo::
|
|
|
|
eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I) const {
|
2009-07-16 13:50:21 +00:00
|
|
|
MBB.erase(I);
|
2009-07-16 13:27:25 +00:00
|
|
|
}
|
|
|
|
|
2010-01-26 23:15:09 +00:00
|
|
|
int SystemZRegisterInfo::getFrameIndexOffset(const MachineFunction &MF,
|
|
|
|
int FI) const {
|
2009-07-16 13:49:25 +00:00
|
|
|
const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
|
2010-01-26 23:15:09 +00:00
|
|
|
const MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
const SystemZMachineFunctionInfo *SystemZMFI =
|
2009-07-16 13:50:21 +00:00
|
|
|
MF.getInfo<SystemZMachineFunctionInfo>();
|
2009-07-16 13:49:25 +00:00
|
|
|
int Offset = MFI->getObjectOffset(FI) + MFI->getOffsetAdjustment();
|
|
|
|
uint64_t StackSize = MFI->getStackSize();
|
|
|
|
|
2009-07-16 13:50:21 +00:00
|
|
|
// Fixed objects are really located in the "previous" frame.
|
|
|
|
if (FI < 0)
|
|
|
|
StackSize -= SystemZMFI->getCalleeSavedFrameSize();
|
|
|
|
|
2009-07-16 13:49:25 +00:00
|
|
|
Offset += StackSize - TFI.getOffsetOfLocalArea();
|
|
|
|
|
|
|
|
// Skip the register save area if we generated the stack frame.
|
2010-05-14 22:17:42 +00:00
|
|
|
if (StackSize || MFI->hasCalls())
|
2009-07-16 13:49:25 +00:00
|
|
|
Offset -= TFI.getOffsetOfLocalArea();
|
|
|
|
|
|
|
|
return Offset;
|
|
|
|
}
|
|
|
|
|
2010-08-26 23:32:16 +00:00
|
|
|
void
|
2009-10-07 17:12:56 +00:00
|
|
|
SystemZRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
|
2010-08-26 23:32:16 +00:00
|
|
|
int SPAdj, RegScavenger *RS) const {
|
2009-07-16 13:49:25 +00:00
|
|
|
assert(SPAdj == 0 && "Unxpected");
|
|
|
|
|
|
|
|
unsigned i = 0;
|
|
|
|
MachineInstr &MI = *II;
|
|
|
|
MachineFunction &MF = *MI.getParent()->getParent();
|
|
|
|
while (!MI.getOperand(i).isFI()) {
|
|
|
|
++i;
|
|
|
|
assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
|
|
|
|
}
|
|
|
|
|
|
|
|
int FrameIndex = MI.getOperand(i).getIndex();
|
|
|
|
|
|
|
|
unsigned BasePtr = (hasFP(MF) ? SystemZ::R11D : SystemZ::R15D);
|
|
|
|
|
|
|
|
// This must be part of a rri or ri operand memory reference. Replace the
|
|
|
|
// FrameIndex with base register with BasePtr. Add an offset to the
|
|
|
|
// displacement field.
|
|
|
|
MI.getOperand(i).ChangeToRegister(BasePtr, false);
|
|
|
|
|
2009-07-16 14:09:56 +00:00
|
|
|
// Offset is a either 12-bit unsigned or 20-bit signed integer.
|
2009-07-16 13:49:25 +00:00
|
|
|
// FIXME: handle "too long" displacements.
|
2010-08-26 23:32:16 +00:00
|
|
|
int Offset =
|
|
|
|
getFrameIndexOffset(MF, FrameIndex) + MI.getOperand(i+1).getImm();
|
2009-07-16 14:09:56 +00:00
|
|
|
|
|
|
|
// Check whether displacement is too long to fit into 12 bit zext field.
|
2009-07-16 14:33:52 +00:00
|
|
|
MI.setDesc(TII.getMemoryInstr(MI.getOpcode(), Offset));
|
2009-07-16 14:09:56 +00:00
|
|
|
|
2009-07-16 13:49:25 +00:00
|
|
|
MI.getOperand(i+1).ChangeToImmediate(Offset);
|
2009-07-16 13:27:25 +00:00
|
|
|
}
|
|
|
|
|
2009-07-16 13:51:12 +00:00
|
|
|
void
|
|
|
|
SystemZRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
|
|
|
|
RegScavenger *RS) const {
|
|
|
|
// Determine whether R15/R14 will ever be clobbered inside the function. And
|
|
|
|
// if yes - mark it as 'callee' saved.
|
|
|
|
MachineFrameInfo *FFI = MF.getFrameInfo();
|
2009-07-16 14:23:01 +00:00
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
|
|
|
|
// Check whether high FPRs are ever used, if yes - we need to save R15 as
|
|
|
|
// well.
|
|
|
|
static const unsigned HighFPRs[] = {
|
|
|
|
SystemZ::F8L, SystemZ::F9L, SystemZ::F10L, SystemZ::F11L,
|
|
|
|
SystemZ::F12L, SystemZ::F13L, SystemZ::F14L, SystemZ::F15L,
|
|
|
|
SystemZ::F8S, SystemZ::F9S, SystemZ::F10S, SystemZ::F11S,
|
|
|
|
SystemZ::F12S, SystemZ::F13S, SystemZ::F14S, SystemZ::F15S,
|
|
|
|
};
|
|
|
|
|
|
|
|
bool HighFPRsUsed = false;
|
|
|
|
for (unsigned i = 0, e = array_lengthof(HighFPRs); i != e; ++i)
|
|
|
|
HighFPRsUsed |= MRI.isPhysRegUsed(HighFPRs[i]);
|
2009-07-16 13:51:12 +00:00
|
|
|
|
2010-05-14 22:17:42 +00:00
|
|
|
if (FFI->hasCalls())
|
2009-07-16 14:16:26 +00:00
|
|
|
/* FIXME: function is varargs */
|
|
|
|
/* FIXME: function grabs RA */
|
|
|
|
/* FIXME: function calls eh_return */
|
2009-07-16 14:23:01 +00:00
|
|
|
MRI.setPhysRegUsed(SystemZ::R14D);
|
2009-07-16 13:51:12 +00:00
|
|
|
|
2009-07-16 14:23:01 +00:00
|
|
|
if (HighFPRsUsed ||
|
2010-05-14 22:17:42 +00:00
|
|
|
FFI->hasCalls() ||
|
2009-07-16 14:11:40 +00:00
|
|
|
FFI->getObjectIndexEnd() != 0 || // Contains automatic variables
|
2009-07-16 13:51:12 +00:00
|
|
|
FFI->hasVarSizedObjects() // Function calls dynamic alloca's
|
|
|
|
/* FIXME: function is varargs */)
|
2009-07-16 14:23:01 +00:00
|
|
|
MRI.setPhysRegUsed(SystemZ::R15D);
|
2009-07-16 13:51:12 +00:00
|
|
|
}
|
|
|
|
|
2009-07-16 13:49:49 +00:00
|
|
|
/// emitSPUpdate - Emit a series of instructions to increment / decrement the
|
|
|
|
/// stack pointer by a constant value.
|
|
|
|
static
|
|
|
|
void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
|
|
|
|
int64_t NumBytes, const TargetInstrInfo &TII) {
|
2009-07-16 14:33:52 +00:00
|
|
|
unsigned Opc; uint64_t Chunk;
|
2009-07-16 13:49:49 +00:00
|
|
|
bool isSub = NumBytes < 0;
|
|
|
|
uint64_t Offset = isSub ? -NumBytes : NumBytes;
|
2009-07-16 14:33:52 +00:00
|
|
|
|
|
|
|
if (Offset >= (1LL << 15) - 1) {
|
|
|
|
Opc = SystemZ::ADD64ri32;
|
|
|
|
Chunk = (1LL << 31) - 1;
|
|
|
|
} else {
|
|
|
|
Opc = SystemZ::ADD64ri16;
|
|
|
|
Chunk = (1LL << 15) - 1;
|
|
|
|
}
|
|
|
|
|
2010-04-02 20:16:16 +00:00
|
|
|
DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
|
2009-07-16 13:49:49 +00:00
|
|
|
|
|
|
|
while (Offset) {
|
|
|
|
uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset;
|
|
|
|
MachineInstr *MI =
|
|
|
|
BuildMI(MBB, MBBI, DL, TII.get(Opc), SystemZ::R15D)
|
2010-04-06 23:35:53 +00:00
|
|
|
.addReg(SystemZ::R15D).addImm(isSub ? -ThisVal : ThisVal);
|
2009-07-16 13:49:49 +00:00
|
|
|
// The PSW implicit def is dead.
|
|
|
|
MI->getOperand(3).setIsDead();
|
|
|
|
Offset -= ThisVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-16 13:27:25 +00:00
|
|
|
void SystemZRegisterInfo::emitPrologue(MachineFunction &MF) const {
|
2009-07-16 13:49:49 +00:00
|
|
|
MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
|
|
|
|
const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
|
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
2009-07-16 13:50:21 +00:00
|
|
|
SystemZMachineFunctionInfo *SystemZMFI =
|
|
|
|
MF.getInfo<SystemZMachineFunctionInfo>();
|
2009-07-16 13:49:49 +00:00
|
|
|
MachineBasicBlock::iterator MBBI = MBB.begin();
|
2010-04-02 20:16:16 +00:00
|
|
|
DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
|
2009-07-16 13:49:49 +00:00
|
|
|
|
|
|
|
// Get the number of bytes to allocate from the FrameInfo.
|
2009-07-16 13:50:21 +00:00
|
|
|
// Note that area for callee-saved stuff is already allocated, thus we need to
|
|
|
|
// 'undo' the stack movement.
|
2009-07-16 14:16:26 +00:00
|
|
|
uint64_t StackSize = MFI->getStackSize();
|
|
|
|
StackSize -= SystemZMFI->getCalleeSavedFrameSize();
|
|
|
|
|
|
|
|
uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
|
2009-07-16 13:49:49 +00:00
|
|
|
|
2009-07-16 13:51:12 +00:00
|
|
|
// Skip the callee-saved push instructions.
|
|
|
|
while (MBBI != MBB.end() &&
|
|
|
|
(MBBI->getOpcode() == SystemZ::MOV64mr ||
|
|
|
|
MBBI->getOpcode() == SystemZ::MOV64mrm))
|
|
|
|
++MBBI;
|
2009-07-16 13:49:49 +00:00
|
|
|
|
|
|
|
if (MBBI != MBB.end())
|
|
|
|
DL = MBBI->getDebugLoc();
|
|
|
|
|
2009-07-16 14:12:36 +00:00
|
|
|
// adjust stack pointer: R15 -= numbytes
|
2010-05-14 22:17:42 +00:00
|
|
|
if (StackSize || MFI->hasCalls()) {
|
2009-07-16 14:16:26 +00:00
|
|
|
assert(MF.getRegInfo().isPhysRegUsed(SystemZ::R15D) &&
|
|
|
|
"Invalid stack frame calculation!");
|
2009-07-16 13:49:49 +00:00
|
|
|
emitSPUpdate(MBB, MBBI, -(int64_t)NumBytes, TII);
|
2009-07-16 14:16:26 +00:00
|
|
|
}
|
2009-07-16 13:49:49 +00:00
|
|
|
|
|
|
|
if (hasFP(MF)) {
|
|
|
|
// Update R11 with the new base value...
|
|
|
|
BuildMI(MBB, MBBI, DL, TII.get(SystemZ::MOV64rr), SystemZ::R11D)
|
|
|
|
.addReg(SystemZ::R15D);
|
|
|
|
|
|
|
|
// Mark the FramePtr as live-in in every block except the entry.
|
2009-12-03 00:50:42 +00:00
|
|
|
for (MachineFunction::iterator I = llvm::next(MF.begin()), E = MF.end();
|
2009-07-16 13:49:49 +00:00
|
|
|
I != E; ++I)
|
|
|
|
I->addLiveIn(SystemZ::R11D);
|
|
|
|
|
|
|
|
}
|
2009-07-16 13:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SystemZRegisterInfo::emitEpilogue(MachineFunction &MF,
|
|
|
|
MachineBasicBlock &MBB) const {
|
2009-07-16 13:49:49 +00:00
|
|
|
const MachineFrameInfo *MFI = MF.getFrameInfo();
|
|
|
|
const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
|
|
|
|
MachineBasicBlock::iterator MBBI = prior(MBB.end());
|
2009-07-16 13:50:21 +00:00
|
|
|
SystemZMachineFunctionInfo *SystemZMFI =
|
|
|
|
MF.getInfo<SystemZMachineFunctionInfo>();
|
2009-07-16 13:49:49 +00:00
|
|
|
unsigned RetOpcode = MBBI->getOpcode();
|
|
|
|
|
|
|
|
switch (RetOpcode) {
|
|
|
|
case SystemZ::RET: break; // These are ok
|
|
|
|
default:
|
|
|
|
assert(0 && "Can only insert epilog into returning blocks");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the number of bytes to allocate from the FrameInfo
|
2009-07-16 13:50:21 +00:00
|
|
|
// Note that area for callee-saved stuff is already allocated, thus we need to
|
|
|
|
// 'undo' the stack movement.
|
|
|
|
uint64_t StackSize =
|
|
|
|
MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
|
2009-07-16 13:49:49 +00:00
|
|
|
uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
|
|
|
|
|
2009-07-16 13:51:12 +00:00
|
|
|
// Skip the final terminator instruction.
|
2009-07-16 13:49:49 +00:00
|
|
|
while (MBBI != MBB.begin()) {
|
|
|
|
MachineBasicBlock::iterator PI = prior(MBBI);
|
2009-07-16 13:51:12 +00:00
|
|
|
--MBBI;
|
2009-07-16 13:49:49 +00:00
|
|
|
if (!PI->getDesc().isTerminator())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-07-16 13:51:12 +00:00
|
|
|
// During callee-saved restores emission stack frame was not yet finialized
|
|
|
|
// (and thus - the stack size was unknown). Tune the offset having full stack
|
|
|
|
// size in hands.
|
2010-05-14 22:17:42 +00:00
|
|
|
if (StackSize || MFI->hasCalls()) {
|
2009-07-16 13:51:12 +00:00
|
|
|
assert((MBBI->getOpcode() == SystemZ::MOV64rmm ||
|
|
|
|
MBBI->getOpcode() == SystemZ::MOV64rm) &&
|
|
|
|
"Expected to see callee-save register restore code");
|
2009-07-16 14:16:45 +00:00
|
|
|
assert(MF.getRegInfo().isPhysRegUsed(SystemZ::R15D) &&
|
|
|
|
"Invalid stack frame calculation!");
|
2009-07-16 13:51:12 +00:00
|
|
|
|
|
|
|
unsigned i = 0;
|
|
|
|
MachineInstr &MI = *MBBI;
|
|
|
|
while (!MI.getOperand(i).isImm()) {
|
|
|
|
++i;
|
|
|
|
assert(i < MI.getNumOperands() && "Unexpected restore code!");
|
|
|
|
}
|
|
|
|
|
2009-07-16 14:33:52 +00:00
|
|
|
uint64_t Offset = NumBytes + MI.getOperand(i).getImm();
|
|
|
|
// If Offset does not fit into 20-bit signed displacement field we need to
|
|
|
|
// emit some additional code...
|
|
|
|
if (Offset > 524287) {
|
|
|
|
// Fold the displacement into load instruction as much as possible.
|
|
|
|
NumBytes = Offset - 524287;
|
|
|
|
Offset = 524287;
|
|
|
|
emitSPUpdate(MBB, MBBI, NumBytes, TII);
|
|
|
|
}
|
|
|
|
|
|
|
|
MI.getOperand(i).ChangeToImmediate(Offset);
|
2009-07-16 13:49:49 +00:00
|
|
|
}
|
2009-07-16 13:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned SystemZRegisterInfo::getRARegister() const {
|
|
|
|
assert(0 && "What is the return address register");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-12 21:00:03 +00:00
|
|
|
unsigned
|
|
|
|
SystemZRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
|
2009-07-16 13:27:25 +00:00
|
|
|
assert(0 && "What is the frame register");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned SystemZRegisterInfo::getEHExceptionRegister() const {
|
|
|
|
assert(0 && "What is the exception register");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned SystemZRegisterInfo::getEHHandlerRegister() const {
|
|
|
|
assert(0 && "What is the exception handler register");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SystemZRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
|
|
|
|
assert(0 && "What is the dwarf register number");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "SystemZGenRegisterInfo.inc"
|