2015-01-14 23:42:31 +08:00
|
|
|
//===-- SIPrepareScratchRegs.cpp - Use predicates for control flow --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file
|
|
|
|
///
|
|
|
|
/// This pass loads scratch pointer and scratch offset into a register or a
|
|
|
|
/// frame index which can be used anywhere in the program. These values will
|
|
|
|
/// be used for spilling VGPRs.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AMDGPU.h"
|
|
|
|
#include "AMDGPUSubtarget.h"
|
|
|
|
#include "SIDefines.h"
|
|
|
|
#include "SIInstrInfo.h"
|
|
|
|
#include "SIMachineFunctionInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/RegisterScavenging.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class SIPrepareScratchRegs : public MachineFunctionPass {
|
|
|
|
|
|
|
|
private:
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SIPrepareScratchRegs() : MachineFunctionPass(ID) { }
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
|
|
|
|
const char *getPassName() const override {
|
|
|
|
return "SI prepare scratch registers";
|
|
|
|
}
|
|
|
|
|
2015-09-26 01:21:28 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2015-01-14 23:42:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End anonymous namespace
|
|
|
|
|
|
|
|
char SIPrepareScratchRegs::ID = 0;
|
|
|
|
|
|
|
|
FunctionPass *llvm::createSIPrepareScratchRegs() {
|
|
|
|
return new SIPrepareScratchRegs();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SIPrepareScratchRegs::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
|
|
|
|
const SIInstrInfo *TII =
|
|
|
|
static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
|
|
|
|
const SIRegisterInfo *TRI = &TII->getRegisterInfo();
|
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
MachineFrameInfo *FrameInfo = MF.getFrameInfo();
|
2015-10-14 04:07:10 +08:00
|
|
|
MachineBasicBlock *Entry = &MF.front();
|
2015-01-14 23:42:31 +08:00
|
|
|
MachineBasicBlock::iterator I = Entry->begin();
|
|
|
|
DebugLoc DL = I->getDebugLoc();
|
|
|
|
|
|
|
|
// FIXME: If we don't have enough VGPRs for SGPR spilling we will need to
|
|
|
|
// run this pass.
|
|
|
|
if (!MFI->hasSpilledVGPRs())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned ScratchPtrPreloadReg =
|
|
|
|
TRI->getPreloadedValue(MF, SIRegisterInfo::SCRATCH_PTR);
|
|
|
|
unsigned ScratchOffsetPreloadReg =
|
|
|
|
TRI->getPreloadedValue(MF, SIRegisterInfo::SCRATCH_WAVE_OFFSET);
|
|
|
|
|
|
|
|
if (!Entry->isLiveIn(ScratchPtrPreloadReg))
|
|
|
|
Entry->addLiveIn(ScratchPtrPreloadReg);
|
|
|
|
|
|
|
|
if (!Entry->isLiveIn(ScratchOffsetPreloadReg))
|
|
|
|
Entry->addLiveIn(ScratchOffsetPreloadReg);
|
|
|
|
|
|
|
|
// Load the scratch offset.
|
|
|
|
unsigned ScratchOffsetReg =
|
|
|
|
TRI->findUnusedRegister(MRI, &AMDGPU::SGPR_32RegClass);
|
2015-01-21 01:49:47 +08:00
|
|
|
int ScratchOffsetFI = -1;
|
2015-01-14 23:42:31 +08:00
|
|
|
|
|
|
|
if (ScratchOffsetReg != AMDGPU::NoRegister) {
|
|
|
|
// Found an SGPR to use
|
|
|
|
BuildMI(*Entry, I, DL, TII->get(AMDGPU::S_MOV_B32), ScratchOffsetReg)
|
|
|
|
.addReg(ScratchOffsetPreloadReg);
|
|
|
|
} else {
|
|
|
|
// No SGPR is available, we must spill.
|
|
|
|
ScratchOffsetFI = FrameInfo->CreateSpillStackObject(4,4);
|
|
|
|
BuildMI(*Entry, I, DL, TII->get(AMDGPU::SI_SPILL_S32_SAVE))
|
|
|
|
.addReg(ScratchOffsetPreloadReg)
|
2015-01-21 03:33:02 +08:00
|
|
|
.addFrameIndex(ScratchOffsetFI)
|
|
|
|
.addReg(AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3, RegState::Undef)
|
|
|
|
.addReg(AMDGPU::SGPR0, RegState::Undef);
|
2015-01-14 23:42:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Now that we have the scratch pointer and offset values, we need to
|
|
|
|
// add them to all the SI_SPILL_V* instructions.
|
|
|
|
|
|
|
|
RegScavenger RS;
|
2015-01-21 01:49:47 +08:00
|
|
|
unsigned ScratchRsrcFI = FrameInfo->CreateSpillStackObject(16, 4);
|
|
|
|
RS.addScavengingFrameIndex(ScratchRsrcFI);
|
|
|
|
|
2015-01-14 23:42:31 +08:00
|
|
|
for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
|
|
|
|
MachineBasicBlock &MBB = *BI;
|
2015-01-21 01:49:47 +08:00
|
|
|
// Add the scratch offset reg as a live-in so that the register scavenger
|
|
|
|
// doesn't re-use it.
|
2015-01-21 03:33:02 +08:00
|
|
|
if (!MBB.isLiveIn(ScratchOffsetReg) &&
|
|
|
|
ScratchOffsetReg != AMDGPU::NoRegister)
|
2015-01-21 01:49:47 +08:00
|
|
|
MBB.addLiveIn(ScratchOffsetReg);
|
|
|
|
RS.enterBasicBlock(&MBB);
|
2015-01-14 23:42:31 +08:00
|
|
|
|
|
|
|
for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
MachineInstr &MI = *I;
|
2015-01-21 01:49:47 +08:00
|
|
|
RS.forward(I);
|
2015-01-14 23:42:31 +08:00
|
|
|
DebugLoc DL = MI.getDebugLoc();
|
2015-05-13 02:59:17 +08:00
|
|
|
if (!TII->isVGPRSpill(MI.getOpcode()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Scratch resource
|
|
|
|
unsigned ScratchRsrcReg =
|
|
|
|
RS.scavengeRegister(&AMDGPU::SReg_128RegClass, 0);
|
|
|
|
|
2015-09-30 07:37:32 +08:00
|
|
|
uint64_t Rsrc23 = TII->getScratchRsrcWords23();
|
2015-05-13 02:59:17 +08:00
|
|
|
|
|
|
|
unsigned Rsrc0 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub0);
|
|
|
|
unsigned Rsrc1 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub1);
|
|
|
|
unsigned Rsrc2 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub2);
|
|
|
|
unsigned Rsrc3 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub3);
|
|
|
|
|
|
|
|
BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), Rsrc0)
|
|
|
|
.addExternalSymbol("SCRATCH_RSRC_DWORD0")
|
|
|
|
.addReg(ScratchRsrcReg, RegState::ImplicitDefine);
|
|
|
|
|
|
|
|
BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), Rsrc1)
|
|
|
|
.addExternalSymbol("SCRATCH_RSRC_DWORD1")
|
|
|
|
.addReg(ScratchRsrcReg, RegState::ImplicitDefine);
|
|
|
|
|
|
|
|
BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), Rsrc2)
|
2015-09-30 07:37:32 +08:00
|
|
|
.addImm(Rsrc23 & 0xffffffff)
|
2015-05-13 02:59:17 +08:00
|
|
|
.addReg(ScratchRsrcReg, RegState::ImplicitDefine);
|
|
|
|
|
|
|
|
BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), Rsrc3)
|
2015-09-30 07:37:32 +08:00
|
|
|
.addImm(Rsrc23 >> 32)
|
2015-05-13 02:59:17 +08:00
|
|
|
.addReg(ScratchRsrcReg, RegState::ImplicitDefine);
|
|
|
|
|
|
|
|
// Scratch Offset
|
|
|
|
if (ScratchOffsetReg == AMDGPU::NoRegister) {
|
|
|
|
ScratchOffsetReg = RS.scavengeRegister(&AMDGPU::SGPR_32RegClass, 0);
|
|
|
|
BuildMI(MBB, I, DL, TII->get(AMDGPU::SI_SPILL_S32_RESTORE),
|
|
|
|
ScratchOffsetReg)
|
|
|
|
.addFrameIndex(ScratchOffsetFI)
|
|
|
|
.addReg(AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3, RegState::Undef)
|
|
|
|
.addReg(AMDGPU::SGPR0, RegState::Undef);
|
|
|
|
} else if (!MBB.isLiveIn(ScratchOffsetReg)) {
|
|
|
|
MBB.addLiveIn(ScratchOffsetReg);
|
2015-01-14 23:42:31 +08:00
|
|
|
}
|
2015-05-13 02:59:17 +08:00
|
|
|
|
|
|
|
if (ScratchRsrcReg == AMDGPU::NoRegister ||
|
|
|
|
ScratchOffsetReg == AMDGPU::NoRegister) {
|
|
|
|
LLVMContext &Ctx = MF.getFunction()->getContext();
|
|
|
|
Ctx.emitError("ran out of SGPRs for spilling VGPRs");
|
|
|
|
ScratchRsrcReg = AMDGPU::SGPR0;
|
|
|
|
ScratchOffsetReg = AMDGPU::SGPR0;
|
|
|
|
}
|
|
|
|
MI.getOperand(2).setReg(ScratchRsrcReg);
|
|
|
|
MI.getOperand(2).setIsKill(true);
|
|
|
|
MI.getOperand(2).setIsUndef(false);
|
|
|
|
MI.getOperand(3).setReg(ScratchOffsetReg);
|
|
|
|
MI.getOperand(3).setIsUndef(false);
|
|
|
|
MI.getOperand(3).setIsKill(false);
|
|
|
|
MI.addOperand(MachineOperand::CreateReg(Rsrc0, false, true, true));
|
|
|
|
MI.addOperand(MachineOperand::CreateReg(Rsrc1, false, true, true));
|
|
|
|
MI.addOperand(MachineOperand::CreateReg(Rsrc2, false, true, true));
|
|
|
|
MI.addOperand(MachineOperand::CreateReg(Rsrc3, false, true, true));
|
2015-01-14 23:42:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|