2012-12-11 22:25:42 +01:00
|
|
|
//===-- SIMachineFunctionInfo.cpp - SI Machine Function Info -------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
/// \file
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
|
|
#include "SIMachineFunctionInfo.h"
|
2014-05-02 17:41:42 +02:00
|
|
|
#include "SIInstrInfo.h"
|
2013-11-27 22:23:35 +01:00
|
|
|
#include "SIRegisterInfo.h"
|
2014-08-21 22:40:54 +02:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2013-11-27 22:23:35 +01:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2014-05-02 17:41:42 +02:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2013-11-27 22:23:35 +01:00
|
|
|
|
|
|
|
#define MAX_LANES 64
|
2012-12-11 22:25:42 +01:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2013-11-19 01:57:56 +01:00
|
|
|
|
|
|
|
// Pin the vtable to this file.
|
|
|
|
void SIMachineFunctionInfo::anchor() {}
|
|
|
|
|
2012-12-11 22:25:42 +01:00
|
|
|
SIMachineFunctionInfo::SIMachineFunctionInfo(const MachineFunction &MF)
|
2013-04-01 23:47:53 +02:00
|
|
|
: AMDGPUMachineFunction(MF),
|
2013-11-27 22:23:35 +01:00
|
|
|
PSInputAddr(0),
|
2014-07-21 17:45:01 +02:00
|
|
|
SpillTracker(),
|
|
|
|
NumUserSGPRs(0) { }
|
2013-11-27 22:23:35 +01:00
|
|
|
|
2014-05-02 17:41:42 +02:00
|
|
|
static unsigned createLaneVGPR(MachineRegisterInfo &MRI, MachineFunction *MF) {
|
|
|
|
unsigned VGPR = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
|
|
|
|
|
|
|
|
// We need to add this register as live out for the function, in order to
|
|
|
|
// have the live range calculated directly.
|
|
|
|
//
|
|
|
|
// When register spilling begins, we have already calculated the live
|
|
|
|
// live intervals for all the registers. Since we are spilling SGPRs to
|
|
|
|
// VGPRs, we need to update the Lane VGPR's live interval every time we
|
|
|
|
// spill or restore a register.
|
|
|
|
//
|
|
|
|
// Unfortunately, there is no good way to update the live interval as
|
|
|
|
// the TargetInstrInfo callbacks for spilling and restoring don't give
|
|
|
|
// us access to the live interval information.
|
|
|
|
//
|
|
|
|
// We are lucky, though, because the InlineSpiller calls
|
|
|
|
// LiveRangeEdit::calculateRegClassAndHint() which iterates through
|
|
|
|
// all the new register that have been created when restoring a register
|
|
|
|
// and calls LiveIntervals::getInterval(), which creates and computes
|
|
|
|
// the live interval for the newly created register. However, once this
|
|
|
|
// live intervals is created, it doesn't change and since we usually reuse
|
|
|
|
// the Lane VGPR multiple times, this means any uses after the first aren't
|
|
|
|
// added to the live interval.
|
|
|
|
//
|
|
|
|
// To work around this, we add Lane VGPRs to the functions live out list,
|
|
|
|
// so that we can guarantee its live range will cover all of its uses.
|
|
|
|
|
|
|
|
for (MachineBasicBlock &MBB : *MF) {
|
|
|
|
if (MBB.back().getOpcode() == AMDGPU::S_ENDPGM) {
|
|
|
|
MBB.back().addOperand(*MF, MachineOperand::CreateReg(VGPR, false, true));
|
|
|
|
return VGPR;
|
|
|
|
}
|
|
|
|
}
|
2014-07-07 20:34:42 +02:00
|
|
|
|
|
|
|
LLVMContext &Ctx = MF->getFunction()->getContext();
|
|
|
|
Ctx.emitError("Could not find S_ENDPGM instruction.");
|
|
|
|
|
2014-05-02 17:41:42 +02:00
|
|
|
return VGPR;
|
2013-11-27 22:23:35 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 17:41:42 +02:00
|
|
|
unsigned SIMachineFunctionInfo::RegSpillTracker::reserveLanes(
|
|
|
|
MachineRegisterInfo &MRI, MachineFunction *MF, unsigned NumRegs) {
|
|
|
|
unsigned StartLane = CurrentLane;
|
|
|
|
CurrentLane += NumRegs;
|
2013-11-27 22:23:35 +01:00
|
|
|
if (!LaneVGPR) {
|
2014-05-02 17:41:42 +02:00
|
|
|
LaneVGPR = createLaneVGPR(MRI, MF);
|
2013-11-27 22:23:35 +01:00
|
|
|
} else {
|
2014-05-02 17:41:42 +02:00
|
|
|
if (CurrentLane >= MAX_LANES) {
|
|
|
|
StartLane = CurrentLane = 0;
|
|
|
|
LaneVGPR = createLaneVGPR(MRI, MF);
|
2013-11-27 22:23:35 +01:00
|
|
|
}
|
|
|
|
}
|
2014-05-02 17:41:42 +02:00
|
|
|
return StartLane;
|
2013-11-27 22:23:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SIMachineFunctionInfo::RegSpillTracker::addSpilledReg(unsigned FrameIndex,
|
|
|
|
unsigned Reg,
|
|
|
|
int Lane) {
|
|
|
|
SpilledRegisters[FrameIndex] = SpilledReg(Reg, Lane);
|
|
|
|
}
|
2014-08-21 22:40:54 +02:00
|
|
|
/// \brief Returns a register that is not used at any point in the function.
|
|
|
|
/// If all registers are used, then this function will return
|
|
|
|
// AMDGPU::NoRegister.
|
|
|
|
static unsigned findUnusedVGPR(const MachineRegisterInfo &MRI) {
|
2013-11-27 22:23:35 +01:00
|
|
|
|
2014-08-21 22:40:54 +02:00
|
|
|
const TargetRegisterClass *RC = &AMDGPU::VGPR_32RegClass;
|
|
|
|
|
|
|
|
for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (!MRI.isPhysRegUsed(*I))
|
|
|
|
return *I;
|
|
|
|
}
|
|
|
|
return AMDGPU::NoRegister;
|
|
|
|
}
|
|
|
|
|
|
|
|
SIMachineFunctionInfo::SpilledReg SIMachineFunctionInfo::getSpilledReg(
|
|
|
|
MachineFunction *MF,
|
|
|
|
unsigned FrameIndex,
|
|
|
|
unsigned SubIdx) {
|
|
|
|
const MachineFrameInfo *FrameInfo = MF->getFrameInfo();
|
|
|
|
MachineRegisterInfo &MRI = MF->getRegInfo();
|
|
|
|
int64_t Offset = FrameInfo->getObjectOffset(FrameIndex);
|
|
|
|
Offset += SubIdx * 4;
|
|
|
|
|
|
|
|
unsigned LaneVGPRIdx = Offset / (64 * 4);
|
|
|
|
unsigned Lane = (Offset / 4) % 64;
|
|
|
|
|
|
|
|
struct SpilledReg Spill;
|
|
|
|
|
|
|
|
if (!LaneVGPRs.count(LaneVGPRIdx)) {
|
|
|
|
unsigned LaneVGPR = findUnusedVGPR(MRI);
|
|
|
|
LaneVGPRs[LaneVGPRIdx] = LaneVGPR;
|
|
|
|
MRI.setPhysRegUsed(LaneVGPR);
|
|
|
|
|
|
|
|
// Add this register as live-in to all blocks to avoid machine verifer
|
|
|
|
// complaining about use of an undefined physical register.
|
|
|
|
for (MachineFunction::iterator BI = MF->begin(), BE = MF->end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
BI->addLiveIn(LaneVGPR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Spill.VGPR = LaneVGPRs[LaneVGPRIdx];
|
|
|
|
Spill.Lane = Lane;
|
|
|
|
return Spill;
|
2013-11-27 22:23:35 +01:00
|
|
|
}
|