LLVM 19.0.0git
X86LowerTileCopy.cpp
Go to the documentation of this file.
1//===-- X86LowerTileCopy.cpp - Expand Tile Copy Instructions---------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the pass which lower AMX tile copy instructions. Since
10// there is no tile copy instruction, we need store tile register to stack
11// and load from stack to another tile register. We need extra GR to hold
12// the stride, and we need stack slot to hold the tile data register.
13// We would run this pass after copy propagation, so that we don't miss copy
14// optimization. And we would run this pass before prolog/epilog insertion,
15// so that we can allocate stack slot.
16//
17//===----------------------------------------------------------------------===//
18
19#include "X86.h"
20#include "X86InstrBuilder.h"
21#include "X86InstrInfo.h"
22#include "X86Subtarget.h"
31#include "llvm/CodeGen/Passes.h"
32#include "llvm/IR/DebugLoc.h"
34#include "llvm/Support/Debug.h"
35
36using namespace llvm;
37
38#define DEBUG_TYPE "x86-lower-tile-copy"
39
40namespace {
41
42class X86LowerTileCopy : public MachineFunctionPass {
43public:
44 static char ID;
45
46 X86LowerTileCopy() : MachineFunctionPass(ID) {}
47
48 void getAnalysisUsage(AnalysisUsage &AU) const override;
49
50 bool runOnMachineFunction(MachineFunction &MF) override;
51
52 StringRef getPassName() const override { return "X86 Lower Tile Copy"; }
53};
54
55} // namespace
56
57char X86LowerTileCopy::ID = 0;
58
59INITIALIZE_PASS_BEGIN(X86LowerTileCopy, "lowertilecopy", "Tile Copy Lowering",
60 false, false)
61INITIALIZE_PASS_END(X86LowerTileCopy, "lowertilecopy", "Tile Copy Lowering",
63
64void X86LowerTileCopy::getAnalysisUsage(AnalysisUsage &AU) const {
65 AU.setPreservesAll();
67}
68
70 return new X86LowerTileCopy();
71}
72
73bool X86LowerTileCopy::runOnMachineFunction(MachineFunction &MF) {
74 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
75 const X86InstrInfo *TII = ST.getInstrInfo();
76 const TargetRegisterInfo *TRI = ST.getRegisterInfo();
77 BitVector GR64Regs =
78 TRI->getAllocatableSet(MF, TRI->getRegClass(X86::GR64RegClassID));
79 BitVector TILERegs =
80 TRI->getAllocatableSet(MF, TRI->getRegClass(X86::TILERegClassID));
81 bool Changed = false;
82
83 for (MachineBasicBlock &MBB : MF) {
84 // There won't be a tile copy if no tile register live in.
85 bool HasTileCopy = false;
86 for (const auto &LI : MBB.liveins()) {
87 if (TILERegs.test(LI.PhysReg)) {
88 HasTileCopy = true;
89 break;
90 }
91 }
92 if (!HasTileCopy)
93 continue;
94 LiveRegUnits UsedRegs(*TRI);
95 UsedRegs.addLiveOuts(MBB);
97 UsedRegs.stepBackward(MI);
98 if (!MI.isCopy())
99 continue;
100 MachineOperand &DstMO = MI.getOperand(0);
101 MachineOperand &SrcMO = MI.getOperand(1);
102 Register SrcReg = SrcMO.getReg();
103 Register DstReg = DstMO.getReg();
104 if (!X86::TILERegClass.contains(DstReg, SrcReg))
105 continue;
106
107 // Allocate stack slot for tile register
108 unsigned Size = TRI->getSpillSize(X86::TILERegClass);
109 Align Alignment = TRI->getSpillAlign(X86::TILERegClass);
110 int TileSS = MF.getFrameInfo().CreateSpillStackObject(Size, Alignment);
111
112 int StrideSS = 0;
113
114 // Pick a killed register to avoid a save/reload.
115 Register GR64Cand = X86::NoRegister;
116 for (auto RegT : GR64Regs.set_bits()) {
117 if (UsedRegs.available(RegT)) {
118 GR64Cand = RegT;
119 break;
120 }
121 }
122
123 const DebugLoc &DL = MI.getDebugLoc();
124 if (GR64Cand) {
125 // mov 64 %reg
126 BuildMI(MBB, MI, DL, TII->get(X86::MOV64ri), GR64Cand).addImm(64);
127 } else {
128 // No available register? Save RAX and reload it after use.
129
130 // Allocate stack slot for stride register
131 Size = TRI->getSpillSize(X86::GR64RegClass);
132 Alignment = TRI->getSpillAlign(X86::GR64RegClass);
133 StrideSS = MF.getFrameInfo().CreateSpillStackObject(Size, Alignment);
134
135 // mov %reg (%sp)
136 addFrameReference(BuildMI(MBB, MI, DL, TII->get(X86::MOV64mr)),
137 StrideSS)
138 .addReg(X86::RAX);
139 // mov 64 %reg
140 BuildMI(MBB, MI, DL, TII->get(X86::MOV64ri), X86::RAX).addImm(64);
141 }
142 // tilestored %tmm, (%sp, %idx)
143#define GET_EGPR_IF_ENABLED(OPC) (ST.hasEGPR() ? OPC##_EVEX : OPC)
144 unsigned Opc = GET_EGPR_IF_ENABLED(X86::TILESTORED);
145 MachineInstr *NewMI =
146 addFrameReference(BuildMI(MBB, MI, DL, TII->get(Opc)), TileSS)
147 .addReg(SrcReg, getKillRegState(SrcMO.isKill()));
148 MachineOperand &MO = NewMI->getOperand(2);
149 MO.setReg(GR64Cand);
150 MO.setIsKill(true);
151 // tileloadd (%sp, %idx), %tmm
152 Opc = GET_EGPR_IF_ENABLED(X86::TILELOADD);
153#undef GET_EGPR_IF_ENABLED
154 NewMI = addFrameReference(BuildMI(MBB, MI, DL, TII->get(Opc), DstReg),
155 TileSS);
156 if (!GR64Cand) {
157 // restore %rax
158 // mov (%sp) %rax
160 BuildMI(MBB, MI, DL, TII->get(X86::MOV64rm), GR64Cand), StrideSS);
161 }
162 MI.eraseFromParent();
163 Changed = true;
164 }
165 }
166 return Changed;
167}
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
uint64_t Size
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
A set of register units.
unsigned const TargetRegisterInfo * TRI
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:59
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition: Value.cpp:469
lowertilecopy
#define GET_EGPR_IF_ENABLED(OPC)
Tile Copy Lowering
Represent the analysis usage information of a pass.
bool test(unsigned Idx) const
Definition: BitVector.h:461
A debug info location.
Definition: DebugLoc.h:33
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
A set of register units used to track register liveness.
Definition: LiveRegUnits.h:30
iterator_range< livein_iterator > liveins() const
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
Definition: MachineInstr.h:69
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:568
MachineOperand class - Representation of each machine instruction operand.
void setReg(Register Reg)
Change the register this operand corresponds to.
void setIsKill(bool Val=true)
Register getReg() const
getReg - Returns the register number.
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
const unsigned GR64Regs[16]
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:656
static const MachineInstrBuilder & addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset=0, bool mem=true)
addFrameReference - This function is used to add a reference to the base of an abstract object on the...
FunctionPass * createX86LowerTileCopyPass()
Return a pass that lower the tile copy instruction.
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:419
unsigned getKillRegState(bool B)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39