2013-08-07 07:08:28 +08:00
|
|
|
//===-- SIFixSGPRCopies.cpp - Remove potential VGPR => SGPR copies --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file
|
|
|
|
/// Copies from VGPR to SGPR registers are illegal and the register coalescer
|
|
|
|
/// will sometimes generate these illegal copies in situations like this:
|
|
|
|
///
|
|
|
|
/// Register Class <vsrc> is the union of <vgpr> and <sgpr>
|
|
|
|
///
|
|
|
|
/// BB0:
|
|
|
|
/// %vreg0 <sgpr> = SCALAR_INST
|
|
|
|
/// %vreg1 <vsrc> = COPY %vreg0 <sgpr>
|
|
|
|
/// ...
|
|
|
|
/// BRANCH %cond BB1, BB2
|
|
|
|
/// BB1:
|
|
|
|
/// %vreg2 <vgpr> = VECTOR_INST
|
|
|
|
/// %vreg3 <vsrc> = COPY %vreg2 <vgpr>
|
|
|
|
/// BB2:
|
|
|
|
/// %vreg4 <vsrc> = PHI %vreg1 <vsrc>, <BB#0>, %vreg3 <vrsc>, <BB#1>
|
2013-11-14 12:05:22 +08:00
|
|
|
/// %vreg5 <vgpr> = VECTOR_INST %vreg4 <vsrc>
|
|
|
|
///
|
2013-08-07 07:08:28 +08:00
|
|
|
///
|
|
|
|
/// The coalescer will begin at BB0 and eliminate its copy, then the resulting
|
|
|
|
/// code will look like this:
|
|
|
|
///
|
|
|
|
/// BB0:
|
|
|
|
/// %vreg0 <sgpr> = SCALAR_INST
|
|
|
|
/// ...
|
|
|
|
/// BRANCH %cond BB1, BB2
|
|
|
|
/// BB1:
|
|
|
|
/// %vreg2 <vgpr> = VECTOR_INST
|
|
|
|
/// %vreg3 <vsrc> = COPY %vreg2 <vgpr>
|
|
|
|
/// BB2:
|
|
|
|
/// %vreg4 <sgpr> = PHI %vreg0 <sgpr>, <BB#0>, %vreg3 <vsrc>, <BB#1>
|
|
|
|
/// %vreg5 <vgpr> = VECTOR_INST %vreg4 <sgpr>
|
|
|
|
///
|
|
|
|
/// Now that the result of the PHI instruction is an SGPR, the register
|
|
|
|
/// allocator is now forced to constrain the register class of %vreg3 to
|
|
|
|
/// <sgpr> so we end up with final code like this:
|
2013-11-14 12:05:22 +08:00
|
|
|
///
|
2013-08-07 07:08:28 +08:00
|
|
|
/// BB0:
|
|
|
|
/// %vreg0 <sgpr> = SCALAR_INST
|
|
|
|
/// ...
|
|
|
|
/// BRANCH %cond BB1, BB2
|
|
|
|
/// BB1:
|
|
|
|
/// %vreg2 <vgpr> = VECTOR_INST
|
|
|
|
/// %vreg3 <sgpr> = COPY %vreg2 <vgpr>
|
|
|
|
/// BB2:
|
|
|
|
/// %vreg4 <sgpr> = PHI %vreg0 <sgpr>, <BB#0>, %vreg3 <sgpr>, <BB#1>
|
|
|
|
/// %vreg5 <vgpr> = VECTOR_INST %vreg4 <sgpr>
|
|
|
|
///
|
2013-11-14 12:05:22 +08:00
|
|
|
/// Now this code contains an illegal copy from a VGPR to an SGPR.
|
2013-08-07 07:08:28 +08:00
|
|
|
///
|
|
|
|
/// In order to avoid this problem, this pass searches for PHI instructions
|
|
|
|
/// which define a <vsrc> register and constrains its definition class to
|
|
|
|
/// <vgpr> if the user of the PHI's definition register is a vector instruction.
|
|
|
|
/// If the PHI's definition class is constrained to <vgpr> then the coalescer
|
|
|
|
/// will be unable to perform the COPY removal from the above example which
|
|
|
|
/// ultimately led to the creation of an illegal COPY.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AMDGPU.h"
|
2014-08-05 05:25:23 +08:00
|
|
|
#include "AMDGPUSubtarget.h"
|
2013-08-07 07:08:28 +08:00
|
|
|
#include "SIInstrInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2013-11-14 07:36:37 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2013-08-07 07:08:28 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2013-11-14 07:36:37 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2013-11-15 07:24:09 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-08-07 07:08:28 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:41:26 +08:00
|
|
|
#define DEBUG_TYPE "sgpr-copies"
|
|
|
|
|
2013-08-07 07:08:28 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class SIFixSGPRCopies : public MachineFunctionPass {
|
|
|
|
|
|
|
|
private:
|
|
|
|
static char ID;
|
2013-11-14 07:36:37 +08:00
|
|
|
const TargetRegisterClass *inferRegClassFromUses(const SIRegisterInfo *TRI,
|
2013-08-07 07:08:28 +08:00
|
|
|
const MachineRegisterInfo &MRI,
|
2013-11-14 07:36:37 +08:00
|
|
|
unsigned Reg,
|
|
|
|
unsigned SubReg) const;
|
|
|
|
const TargetRegisterClass *inferRegClassFromDef(const SIRegisterInfo *TRI,
|
|
|
|
const MachineRegisterInfo &MRI,
|
|
|
|
unsigned Reg,
|
|
|
|
unsigned SubReg) const;
|
|
|
|
bool isVGPRToSGPRCopy(const MachineInstr &Copy, const SIRegisterInfo *TRI,
|
|
|
|
const MachineRegisterInfo &MRI) const;
|
2013-08-07 07:08:28 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
SIFixSGPRCopies(TargetMachine &tm) : MachineFunctionPass(ID) { }
|
|
|
|
|
2014-04-29 15:57:24 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2013-08-07 07:08:28 +08:00
|
|
|
|
2014-04-29 15:57:24 +08:00
|
|
|
const char *getPassName() const override {
|
2013-08-07 07:08:28 +08:00
|
|
|
return "SI Fix SGPR copies";
|
|
|
|
}
|
|
|
|
|
2015-09-26 01:21:28 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2013-08-07 07:08:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End anonymous namespace
|
|
|
|
|
|
|
|
char SIFixSGPRCopies::ID = 0;
|
|
|
|
|
|
|
|
FunctionPass *llvm::createSIFixSGPRCopiesPass(TargetMachine &tm) {
|
|
|
|
return new SIFixSGPRCopies(tm);
|
|
|
|
}
|
|
|
|
|
2013-11-14 07:36:37 +08:00
|
|
|
static bool hasVGPROperands(const MachineInstr &MI, const SIRegisterInfo *TRI) {
|
|
|
|
const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
|
|
|
|
for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
|
|
|
|
if (!MI.getOperand(i).isReg() ||
|
|
|
|
!TargetRegisterInfo::isVirtualRegister(MI.getOperand(i).getReg()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (TRI->hasVGPRs(MRI.getRegClass(MI.getOperand(i).getReg())))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This functions walks the use list of Reg until it finds an Instruction
|
|
|
|
/// that isn't a COPY returns the register class of that instruction.
|
2013-11-14 12:05:28 +08:00
|
|
|
/// \return The register defined by the first non-COPY instruction.
|
2013-11-14 07:36:37 +08:00
|
|
|
const TargetRegisterClass *SIFixSGPRCopies::inferRegClassFromUses(
|
|
|
|
const SIRegisterInfo *TRI,
|
2013-08-07 07:08:28 +08:00
|
|
|
const MachineRegisterInfo &MRI,
|
2013-11-14 07:36:37 +08:00
|
|
|
unsigned Reg,
|
|
|
|
unsigned SubReg) const {
|
2013-08-07 07:08:28 +08:00
|
|
|
|
2014-12-03 13:22:38 +08:00
|
|
|
const TargetRegisterClass *RC
|
|
|
|
= TargetRegisterInfo::isVirtualRegister(Reg) ?
|
|
|
|
MRI.getRegClass(Reg) :
|
2015-05-12 22:18:11 +08:00
|
|
|
TRI->getPhysRegClass(Reg);
|
2014-12-03 13:22:38 +08:00
|
|
|
|
2013-11-14 07:36:37 +08:00
|
|
|
RC = TRI->getSubRegClass(RC, SubReg);
|
2014-03-14 07:12:04 +08:00
|
|
|
for (MachineRegisterInfo::use_instr_iterator
|
|
|
|
I = MRI.use_instr_begin(Reg), E = MRI.use_instr_end(); I != E; ++I) {
|
2013-08-07 07:08:28 +08:00
|
|
|
switch (I->getOpcode()) {
|
|
|
|
case AMDGPU::COPY:
|
2013-11-14 07:36:37 +08:00
|
|
|
RC = TRI->getCommonSubClass(RC, inferRegClassFromUses(TRI, MRI,
|
|
|
|
I->getOperand(0).getReg(),
|
|
|
|
I->getOperand(0).getSubReg()));
|
2013-08-07 07:08:28 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return RC;
|
|
|
|
}
|
|
|
|
|
2013-11-14 07:36:37 +08:00
|
|
|
const TargetRegisterClass *SIFixSGPRCopies::inferRegClassFromDef(
|
|
|
|
const SIRegisterInfo *TRI,
|
|
|
|
const MachineRegisterInfo &MRI,
|
|
|
|
unsigned Reg,
|
|
|
|
unsigned SubReg) const {
|
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
|
|
|
|
const TargetRegisterClass *RC = TRI->getPhysRegClass(Reg);
|
|
|
|
return TRI->getSubRegClass(RC, SubReg);
|
|
|
|
}
|
|
|
|
MachineInstr *Def = MRI.getVRegDef(Reg);
|
|
|
|
if (Def->getOpcode() != AMDGPU::COPY) {
|
|
|
|
return TRI->getSubRegClass(MRI.getRegClass(Reg), SubReg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return inferRegClassFromDef(TRI, MRI, Def->getOperand(1).getReg(),
|
|
|
|
Def->getOperand(1).getSubReg());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SIFixSGPRCopies::isVGPRToSGPRCopy(const MachineInstr &Copy,
|
|
|
|
const SIRegisterInfo *TRI,
|
|
|
|
const MachineRegisterInfo &MRI) const {
|
|
|
|
|
|
|
|
unsigned DstReg = Copy.getOperand(0).getReg();
|
|
|
|
unsigned SrcReg = Copy.getOperand(1).getReg();
|
|
|
|
unsigned SrcSubReg = Copy.getOperand(1).getSubReg();
|
2014-12-03 13:22:39 +08:00
|
|
|
|
2015-05-12 22:18:11 +08:00
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(DstReg)) {
|
|
|
|
// If the destination register is a physical register there isn't really
|
|
|
|
// much we can do to fix this.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TargetRegisterClass *DstRC = MRI.getRegClass(DstReg);
|
2014-12-03 13:22:39 +08:00
|
|
|
|
2013-11-19 02:50:15 +08:00
|
|
|
const TargetRegisterClass *SrcRC;
|
2013-11-14 07:36:37 +08:00
|
|
|
|
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(SrcReg) ||
|
2014-04-30 23:31:33 +08:00
|
|
|
MRI.getRegClass(SrcReg) == &AMDGPU::VReg_1RegClass)
|
2013-11-14 07:36:37 +08:00
|
|
|
return false;
|
|
|
|
|
2014-02-05 01:18:42 +08:00
|
|
|
SrcRC = TRI->getSubRegClass(MRI.getRegClass(SrcReg), SrcSubReg);
|
2013-11-19 02:50:20 +08:00
|
|
|
return TRI->isSGPRClass(DstRC) && TRI->hasVGPRs(SrcRC);
|
2013-11-14 07:36:37 +08:00
|
|
|
}
|
|
|
|
|
2013-08-07 07:08:28 +08:00
|
|
|
bool SIFixSGPRCopies::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
2014-08-05 10:39:49 +08:00
|
|
|
const SIRegisterInfo *TRI =
|
|
|
|
static_cast<const SIRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
|
|
|
|
const SIInstrInfo *TII =
|
|
|
|
static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
|
2013-08-07 07:08:28 +08:00
|
|
|
for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
|
|
|
|
MachineBasicBlock &MBB = *BI;
|
|
|
|
for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
MachineInstr &MI = *I;
|
2013-11-14 07:36:37 +08:00
|
|
|
|
|
|
|
switch (MI.getOpcode()) {
|
2015-09-22 00:27:22 +08:00
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
case AMDGPU::COPY: {
|
|
|
|
if (isVGPRToSGPRCopy(MI, TRI, MRI)) {
|
|
|
|
DEBUG(dbgs() << "Fixing VGPR -> SGPR copy: " << MI);
|
|
|
|
TII->moveToVALU(MI);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2013-11-14 07:36:37 +08:00
|
|
|
case AMDGPU::PHI: {
|
2014-11-26 05:03:22 +08:00
|
|
|
DEBUG(dbgs() << "Fixing PHI: " << MI);
|
2013-11-14 07:36:37 +08:00
|
|
|
|
2014-12-03 13:22:32 +08:00
|
|
|
for (unsigned i = 1; i < MI.getNumOperands(); i += 2) {
|
|
|
|
const MachineOperand &Op = MI.getOperand(i);
|
|
|
|
unsigned Reg = Op.getReg();
|
|
|
|
const TargetRegisterClass *RC
|
|
|
|
= inferRegClassFromDef(TRI, MRI, Reg, Op.getSubReg());
|
|
|
|
|
|
|
|
MRI.constrainRegClass(Op.getReg(), RC);
|
2013-11-14 07:36:37 +08:00
|
|
|
}
|
|
|
|
unsigned Reg = MI.getOperand(0).getReg();
|
|
|
|
const TargetRegisterClass *RC = inferRegClassFromUses(TRI, MRI, Reg,
|
|
|
|
MI.getOperand(0).getSubReg());
|
2015-01-08 04:59:25 +08:00
|
|
|
if (TRI->getCommonSubClass(RC, &AMDGPU::VGPR_32RegClass)) {
|
|
|
|
MRI.constrainRegClass(Reg, &AMDGPU::VGPR_32RegClass);
|
2013-11-14 07:36:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!TRI->isSGPRClass(MRI.getRegClass(Reg)))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// If a PHI node defines an SGPR and any of its operands are VGPRs,
|
|
|
|
// then we need to move it to the VALU.
|
2014-09-24 09:33:26 +08:00
|
|
|
//
|
|
|
|
// Also, if a PHI node defines an SGPR and has all SGPR operands
|
|
|
|
// we must move it to the VALU, because the SGPR operands will
|
|
|
|
// all end up being assigned the same register, which means
|
|
|
|
// there is a potential for a conflict if different threads take
|
2014-10-17 08:36:20 +08:00
|
|
|
// different control flow paths.
|
2014-09-24 09:33:26 +08:00
|
|
|
//
|
|
|
|
// For Example:
|
|
|
|
//
|
|
|
|
// sgpr0 = def;
|
|
|
|
// ...
|
|
|
|
// sgpr1 = def;
|
|
|
|
// ...
|
|
|
|
// sgpr2 = PHI sgpr0, sgpr1
|
|
|
|
// use sgpr2;
|
|
|
|
//
|
|
|
|
// Will Become:
|
|
|
|
//
|
|
|
|
// sgpr2 = def;
|
|
|
|
// ...
|
|
|
|
// sgpr2 = def;
|
|
|
|
// ...
|
|
|
|
// use sgpr2
|
|
|
|
//
|
|
|
|
// FIXME: This is OK if the branching decision is made based on an
|
|
|
|
// SGPR value.
|
|
|
|
bool SGPRBranch = false;
|
|
|
|
|
|
|
|
// The one exception to this rule is when one of the operands
|
|
|
|
// is defined by a SI_BREAK, SI_IF_BREAK, or SI_ELSE_BREAK
|
|
|
|
// instruction. In this case, there we know the program will
|
|
|
|
// never enter the second block (the loop) without entering
|
|
|
|
// the first block (where the condition is computed), so there
|
|
|
|
// is no chance for values to be over-written.
|
|
|
|
|
|
|
|
bool HasBreakDef = false;
|
2013-11-14 07:36:37 +08:00
|
|
|
for (unsigned i = 1; i < MI.getNumOperands(); i+=2) {
|
|
|
|
unsigned Reg = MI.getOperand(i).getReg();
|
|
|
|
if (TRI->hasVGPRs(MRI.getRegClass(Reg))) {
|
|
|
|
TII->moveToVALU(MI);
|
|
|
|
break;
|
|
|
|
}
|
2014-09-24 09:33:26 +08:00
|
|
|
MachineInstr *DefInstr = MRI.getUniqueVRegDef(Reg);
|
|
|
|
assert(DefInstr);
|
|
|
|
switch(DefInstr->getOpcode()) {
|
|
|
|
|
|
|
|
case AMDGPU::SI_BREAK:
|
|
|
|
case AMDGPU::SI_IF_BREAK:
|
|
|
|
case AMDGPU::SI_ELSE_BREAK:
|
|
|
|
// If we see a PHI instruction that defines an SGPR, then that PHI
|
|
|
|
// instruction has already been considered and should have
|
|
|
|
// a *_BREAK as an operand.
|
|
|
|
case AMDGPU::PHI:
|
|
|
|
HasBreakDef = true;
|
|
|
|
break;
|
|
|
|
}
|
2013-11-14 07:36:37 +08:00
|
|
|
}
|
|
|
|
|
2014-09-24 09:33:26 +08:00
|
|
|
if (!SGPRBranch && !HasBreakDef)
|
|
|
|
TII->moveToVALU(MI);
|
2013-11-14 07:36:37 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AMDGPU::REG_SEQUENCE: {
|
|
|
|
if (TRI->hasVGPRs(TII->getOpRegClass(MI, 0)) ||
|
|
|
|
!hasVGPROperands(MI, TRI))
|
|
|
|
continue;
|
|
|
|
|
2014-10-17 08:36:20 +08:00
|
|
|
DEBUG(dbgs() << "Fixing REG_SEQUENCE: " << MI);
|
2013-11-14 07:36:37 +08:00
|
|
|
|
|
|
|
TII->moveToVALU(MI);
|
|
|
|
break;
|
2013-08-07 07:08:28 +08:00
|
|
|
}
|
2014-04-08 03:45:45 +08:00
|
|
|
case AMDGPU::INSERT_SUBREG: {
|
2014-05-15 22:41:55 +08:00
|
|
|
const TargetRegisterClass *DstRC, *Src0RC, *Src1RC;
|
2014-04-08 03:45:45 +08:00
|
|
|
DstRC = MRI.getRegClass(MI.getOperand(0).getReg());
|
2014-05-15 22:41:55 +08:00
|
|
|
Src0RC = MRI.getRegClass(MI.getOperand(1).getReg());
|
|
|
|
Src1RC = MRI.getRegClass(MI.getOperand(2).getReg());
|
|
|
|
if (TRI->isSGPRClass(DstRC) &&
|
|
|
|
(TRI->hasVGPRs(Src0RC) || TRI->hasVGPRs(Src1RC))) {
|
2014-10-17 08:36:20 +08:00
|
|
|
DEBUG(dbgs() << " Fixing INSERT_SUBREG: " << MI);
|
2014-05-15 22:41:55 +08:00
|
|
|
TII->moveToVALU(MI);
|
|
|
|
}
|
|
|
|
break;
|
2014-04-08 03:45:45 +08:00
|
|
|
}
|
2013-08-07 07:08:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 05:11:34 +08:00
|
|
|
|
|
|
|
return true;
|
2013-08-07 07:08:28 +08:00
|
|
|
}
|