43#define DEBUG_TYPE "gcn-create-vopd"
44STATISTIC(NumVOPDCreated,
"Number of VOPD Insts Created.");
46 "Number of immediates moved into a scalar register to allow VOPD3 "
49 "Number of VOPD3 candidate edges skipped because no scalar register "
50 "was free for their immediate.");
61 bool needsMaterialization()
const {
return !Match.
LiteralFixups.empty(); }
63 bool isFeasible()
const {
64 return !needsMaterialization() || MaterializationReg;
77 if (!
MI.isDebugInstr())
111 const GCNSubtarget *ST =
nullptr;
114 assignMaterializationRegisters(MachineBasicBlock &
MBB,
116 auto Candidate = Candidates.
rbegin();
117 auto SkipPlainCandidates = [&] {
118 while (Candidate != Candidates.
rend() &&
119 !Candidate->needsMaterialization())
122 SkipPlainCandidates();
123 if (Candidate == Candidates.
rend())
127 LiveRegUnits Walk(*ST->getRegisterInfo());
128 Walk.addLiveOuts(
MBB);
133 if (Candidate != Candidates.
rend() &&
134 Candidate->Match.InOrder[1] == &
MI) {
135 LiveRegUnits RangeLive = Walk;
136 addRangeUses(RangeLive, Candidate->Match.InOrder[0]->getIterator(),
137 *Candidate->Match.InOrder[1]);
138 Candidate->MaterializationReg =
139 takeFreeSGPR(*ST, MRI, RangeLive, Candidate->Match.LiteralFixups);
141 SkipPlainCandidates();
143 if (!
MI.isDebugInstr())
144 Walk.stepBackward(
MI);
147 "every candidate range must end in this block");
153 unsigned NumPairs = 0;
154 unsigned NumMoves = 0;
157 const size_t NumCandidates = Candidates.
size();
159 SmallBitVector Take(NumCandidates);
160 auto NextNonOverlapping = [&](
size_t I) {
162 if (
Next != NumCandidates &&
163 Candidates[
Next].Match.InOrder[0] == Candidates[
I].Match.InOrder[1])
170 for (
size_t I = NumCandidates;
I-- != 0;) {
171 Best[
I] = Best[
I + 1];
172 if (!Candidates[
I].isFeasible()) {
173 ++NumCandidateEdgesWithoutFreeSGPR;
177 Score With = Best[NextNonOverlapping(
I)];
179 With.NumMoves += Candidates[
I].needsMaterialization();
180 if (With.NumPairs > Best[
I].NumPairs ||
181 (With.NumPairs == Best[
I].NumPairs &&
182 With.NumMoves <= Best[
I].NumMoves)) {
189 for (
size_t I = 0;
I != NumCandidates;) {
195 I = NextNonOverlapping(
I);
200 void materializeLiteral(
const SIInstrInfo &
TII, VOPDCandidate &Candidate) {
201 if (!Candidate.needsMaterialization())
205 assert(Candidate.MaterializationReg);
208 return Fixup.Imm == Imm;
211 MachineInstr *InsertPt = Candidate.Match.
InOrder[0];
213 TII.get(AMDGPU::S_MOV_B32), Candidate.MaterializationReg)
215 ++NumLiteralsMaterialized;
217 for (
const VOPDLiteralFixup &
Fixup : Fixups) {
219 ? Candidate.Match.
getMIX()
220 : Candidate.Match.
getMIY();
226 bool doReplace(
const SIInstrInfo *SII, VOPDMatchInfo &Match) {
227 MachineInstr *MIX = Match.
getMIX();
228 MachineInstr *MIY = Match.
getMIY();
231 unsigned EncodingFamily =
236 EncodingFamily, Match.
IsVOPD3);
238 "Should have previously determined this as a possible VOPD\n");
244 namespace VOPD = AMDGPU::VOPD;
245 MachineInstr *
MI[] = {MIX, MIY};
248 for (
auto CompIdx : VOPD::COMPONENTS) {
249 auto MCOprIdx = InstInfo[CompIdx].getIndexOfDstInMCOperands();
250 VOPDInst.add(
MI[CompIdx]->getOperand(MCOprIdx));
253 const AMDGPU::OpName Mods[2][3] = {
254 {AMDGPU::OpName::src0X_modifiers, AMDGPU::OpName::vsrc1X_modifiers,
255 AMDGPU::OpName::vsrc2X_modifiers},
256 {AMDGPU::OpName::src0Y_modifiers, AMDGPU::OpName::vsrc1Y_modifiers,
257 AMDGPU::OpName::vsrc2Y_modifiers}};
258 const AMDGPU::OpName SrcMods[3] = {AMDGPU::OpName::src0_modifiers,
259 AMDGPU::OpName::src1_modifiers,
260 AMDGPU::OpName::src2_modifiers};
261 const unsigned VOPDOpc = VOPDInst->getOpcode();
263 for (
auto CompIdx : VOPD::COMPONENTS) {
264 auto CompSrcOprNum = InstInfo[CompIdx].getCompSrcOperandsNum();
265 bool IsVOP3 = SII->
isVOP3(*
MI[CompIdx]);
266 for (
unsigned CompSrcIdx = 0; CompSrcIdx < CompSrcOprNum; ++CompSrcIdx) {
268 const MachineOperand *
Mod =
270 VOPDInst.addImm(
Mod ?
Mod->getImm() : 0);
273 InstInfo[CompIdx].getIndexOfSrcInMCOperands(CompSrcIdx, IsVOP3);
274 VOPDInst.add(
MI[CompIdx]->getOperand(MCOprIdx));
276 if (
MI[CompIdx]->
getOpcode() == AMDGPU::V_CNDMASK_B32_e32 &&
278 VOPDInst.addReg(AMDGPU::VCC_LO);
283 VOPDInst.addImm(BitOp2);
287 for (
auto CompIdx : VOPD::COMPONENTS)
288 VOPDInst.copyImplicitOps(*
MI[CompIdx]);
290 LLVM_DEBUG(
dbgs() <<
"VOPD Fused: " << *VOPDInst <<
" from\tX: " << *MIX
291 <<
"\tY: " << *MIY <<
"\n");
293 for (
auto CompIdx : VOPD::COMPONENTS)
294 MI[CompIdx]->eraseFromParent();
306 const SIInstrInfo *SII = ST->getInstrInfo();
309 for (MachineBasicBlock &
MBB : MF) {
313 MachineInstr *FirstMI = &*MII;
319 MachineInstr *SecondMI = &*MII;
321 if (std::optional<VOPDMatchInfo> Match =
326 assignMaterializationRegisters(
MBB, Candidates);
328 for (VOPDCandidate *Candidate : Selected) {
329 materializeLiteral(*SII, *Candidate);
330 Changed |= doReplace(SII, Candidate->Match);
341 GCNCreateVOPDLegacy() : MachineFunctionPass(ID) {}
343 StringRef getPassName()
const override {
344 return "GCN Create VOPD Instructions";
348 void getAnalysisUsage(AnalysisUsage &AU)
const override {
357 return GCNCreateVOPD().run(MF);
366 if (!GCNCreateVOPD().
run(MF))
371char GCNCreateVOPDLegacy::ID = 0;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static void addRangeUses(LiveRegUnits &Live, MachineBasicBlock::iterator Begin, MachineInstr &RangeEnd)
Add everything the instructions in [Begin, RangeEnd] touch to Live, which already holds what is live ...
static Register takeFreeSGPR(const GCNSubtarget &ST, const MachineRegisterInfo &MRI, const LiveRegUnits &Live, ArrayRef< VOPDLiteralFixup > Fixups)
Return a scalar register which every fixup can read and which no value in Live occupies,...
AMD GCN specific subclass of TargetSubtarget.
const HexagonInstrInfo * TII
Register const TargetRegisterInfo * TRI
Promote Memory to Register
PowerPC TLS Dynamic Call Fixup
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Interface definition for SIInstrInfo.
This file implements the SmallBitVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
Get the array size.
Represents analyses that only rely on functions' control flow.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &AM)
A set of register units used to track register liveness.
bool available(MCRegister Reg) const
Returns true if no part of physical register Reg is live.
LLVM_ABI void accumulate(const MachineInstr &MI)
Adds all register units used, defined or clobbered in MI.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
bool isDebugInstr() const
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
uint32_t getFlags() const
Return the MI flags bitvector.
LLVM_ABI void ChangeToRegister(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isDebug=false)
ChangeToRegister - Replace this operand with a new register operand of the specified value.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
bool isReserved(MCRegister PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
reverse_iterator rbegin() const
reverse_iterator rend() const
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Wrapper class representing virtual and physical registers.
const GCNSubtarget & getSubtarget() const
static bool isVOP3(const MCInstrDesc &Desc)
void fixImplicitOperands(MachineInstr &MI) const
LLVM_READONLY MachineOperand * getNamedOperand(MachineInstr &MI, AMDGPU::OpName OperandName) const
Returns the operand named Op.
void push_back(const T &Elt)
unsigned getVOPDOpcode(unsigned Opc, bool VOPD3)
LLVM_READONLY bool hasNamedOperand(uint64_t Opcode, OpName NamedIdx)
unsigned getVOPDEncodingFamily(const MCSubtargetInfo &ST)
unsigned getBitOp2(unsigned Opc)
VOPD::InstInfo getVOPDInstInfo(const MCInstrDesc &OpX, const MCInstrDesc &OpY)
bool hasVOPD(const MCSubtargetInfo &STI)
int getVOPDFull(unsigned OpX, unsigned OpY, unsigned EncodingFamily, bool VOPD3)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
unsigned getOpcode(const VPValue *V)
Return the instruction opcode for the recipe defining V or 0 for unsupported recipes and VPValues not...
This is an optimization pass for GlobalISel generic memory operations.
IterT next_nodbg(IterT It, IterT End, bool SkipPseudoOp=true)
Increment It, then continue incrementing it while it points to a debug instruction.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
auto reverse(ContainerTy &&C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
MutableArrayRef(T &OneElt) -> MutableArrayRef< T >
@ Mod
The access may modify the value stored in memory.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
ArrayRef(const T &OneElt) -> ArrayRef< T >
std::optional< VOPDMatchInfo > tryMatchVOPDPair(const SIInstrInfo &TII, MachineInstr &FirstMI, MachineInstr &SecondMI)
Check whether FirstMI and SecondMI can be combined into a VOPD instruction.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
A 32-bit immediate which the VOPD encoding cannot hold.
Describes a matched VOPD pair.
SmallVector< VOPDLiteralFixup, 2 > LiteralFixups
Immediates which have to be moved into scalar registers before the pair can be built.
MachineInstr * getMIX() const
MachineInstr * getMIY() const
MachineInstr * InOrder[2]
The component instructions in program order.