LLVM  5.0.0
Macros | Functions | Variables
SIFixSGPRCopies.cpp File Reference

Copies from VGPR to SGPR registers are illegal and the register coalescer will sometimes generate these illegal copies in situations like this: More...

#include "AMDGPU.h"
#include "AMDGPUSubtarget.h"
#include "SIInstrInfo.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/CodeGen/MachineDominators.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
Include dependency graph for SIFixSGPRCopies.cpp:

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "si-fix-sgpr-copies"
 

Functions

 INITIALIZE_PASS_BEGIN (SIFixSGPRCopies, DEBUG_TYPE,"SI Fix SGPR copies", false, false) INITIALIZE_PASS_END(SIFixSGPRCopies
 
static bool hasVGPROperands (const MachineInstr &MI, const SIRegisterInfo *TRI)
 
static std::pair< const
TargetRegisterClass *, const
TargetRegisterClass * > 
getCopyRegClasses (const MachineInstr &Copy, const SIRegisterInfo &TRI, const MachineRegisterInfo &MRI)
 
static bool isVGPRToSGPRCopy (const TargetRegisterClass *SrcRC, const TargetRegisterClass *DstRC, const SIRegisterInfo &TRI)
 
static bool isSGPRToVGPRCopy (const TargetRegisterClass *SrcRC, const TargetRegisterClass *DstRC, const SIRegisterInfo &TRI)
 
static bool tryChangeVGPRtoSGPRinCopy (MachineInstr &MI, const SIRegisterInfo *TRI, const SIInstrInfo *TII)
 
static bool foldVGPRCopyIntoRegSequence (MachineInstr &MI, const SIRegisterInfo *TRI, const SIInstrInfo *TII, MachineRegisterInfo &MRI)
 
static bool phiHasVGPROperands (const MachineInstr &PHI, const MachineRegisterInfo &MRI, const SIRegisterInfo *TRI, const SIInstrInfo *TII)
 
static bool phiHasBreakDef (const MachineInstr &PHI, const MachineRegisterInfo &MRI, SmallSet< unsigned, 8 > &Visited)
 
static bool hasTerminatorThatModifiesExec (const MachineBasicBlock &MBB, const TargetRegisterInfo &TRI)
 
static bool isSafeToFoldImmIntoCopy (const MachineInstr *Copy, const MachineInstr *MoveImm, const SIInstrInfo *TII, unsigned &SMovOp, int64_t &Imm)
 
template<class UnaryPredicate >
bool searchPredecessors (const MachineBasicBlock *MBB, const MachineBasicBlock *CutOff, UnaryPredicate Predicate)
 
static bool predsHasDivergentTerminator (MachineBasicBlock *MBB, const TargetRegisterInfo *TRI)
 
static bool isReachable (const MachineInstr *From, const MachineInstr *To, const MachineBasicBlock *CutOff, MachineDominatorTree &MDT)
 
static bool hoistAndMergeSGPRInits (unsigned Reg, const MachineRegisterInfo &MRI, MachineDominatorTree &MDT)
 

Variables

static cl::opt< bool > EnableM0Merge ("amdgpu-enable-merge-m0", cl::desc("Merge and hoist M0 initializations"), cl::init(false))
 
 DEBUG_TYPE
 
SI Fix SGPR copies
 
SI Fix SGPR false
 

Detailed Description

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> vreg5 <vgpr> = VECTOR_INST vreg4 <vsrc>

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:

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>

Now this code contains an illegal copy from a VGPR to an SGPR.

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.

Definition in file SIFixSGPRCopies.cpp.

Macro Definition Documentation

#define DEBUG_TYPE   "si-fix-sgpr-copies"

Definition at line 82 of file SIFixSGPRCopies.cpp.

Function Documentation

static bool foldVGPRCopyIntoRegSequence ( MachineInstr &  MI,
const SIRegisterInfo *  TRI,
const SIInstrInfo *  TII,
MachineRegisterInfo &  MRI 
)
static
static std::pair<const TargetRegisterClass *, const TargetRegisterClass *> getCopyRegClasses ( const MachineInstr &  Copy,
const SIRegisterInfo &  TRI,
const MachineRegisterInfo &  MRI 
)
static
static bool hasTerminatorThatModifiesExec ( const MachineBasicBlock &  MBB,
const TargetRegisterInfo &  TRI 
)
static
static bool hasVGPROperands ( const MachineInstr &  MI,
const SIRegisterInfo *  TRI 
)
static
static bool hoistAndMergeSGPRInits ( unsigned  Reg,
const MachineRegisterInfo &  MRI,
MachineDominatorTree &  MDT 
)
static
INITIALIZE_PASS_BEGIN ( SIFixSGPRCopies  ,
DEBUG_TYPE  ,
"SI Fix SGPR copies"  ,
false  ,
false   
)
static bool isReachable ( const MachineInstr *  From,
const MachineInstr *  To,
const MachineBasicBlock *  CutOff,
MachineDominatorTree &  MDT 
)
static
static bool isSafeToFoldImmIntoCopy ( const MachineInstr *  Copy,
const MachineInstr *  MoveImm,
const SIInstrInfo *  TII,
unsigned &  SMovOp,
int64_t &  Imm 
)
static
static bool isSGPRToVGPRCopy ( const TargetRegisterClass *  SrcRC,
const TargetRegisterClass *  DstRC,
const SIRegisterInfo &  TRI 
)
static
static bool isVGPRToSGPRCopy ( const TargetRegisterClass *  SrcRC,
const TargetRegisterClass *  DstRC,
const SIRegisterInfo &  TRI 
)
static
static bool phiHasBreakDef ( const MachineInstr &  PHI,
const MachineRegisterInfo &  MRI,
SmallSet< unsigned, 8 > &  Visited 
)
static
static bool phiHasVGPROperands ( const MachineInstr &  PHI,
const MachineRegisterInfo &  MRI,
const SIRegisterInfo *  TRI,
const SIInstrInfo *  TII 
)
static
static bool predsHasDivergentTerminator ( MachineBasicBlock *  MBB,
const TargetRegisterInfo *  TRI 
)
static

Definition at line 395 of file SIFixSGPRCopies.cpp.

References hasTerminatorThatModifiesExec(), and searchPredecessors().

template<class UnaryPredicate >
bool searchPredecessors ( const MachineBasicBlock *  MBB,
const MachineBasicBlock *  CutOff,
UnaryPredicate  Predicate 
)
static bool tryChangeVGPRtoSGPRinCopy ( MachineInstr &  MI,
const SIRegisterInfo *  TRI,
const SIInstrInfo *  TII 
)
static

Variable Documentation

SI Fix SGPR copies

Definition at line 117 of file SIFixSGPRCopies.cpp.

DEBUG_TYPE

Definition at line 117 of file SIFixSGPRCopies.cpp.

cl::opt<bool> EnableM0Merge("amdgpu-enable-merge-m0", cl::desc("Merge and hoist M0 initializations"), cl::init(false))
static
SI Fix SGPR false

Definition at line 117 of file SIFixSGPRCopies.cpp.