22#define DEBUG_TYPE "si-mode-register"
24STATISTIC(NumSetregInserted,
"Number of setreg of mode register inserted.");
56 unsigned NewMode = (
Mode & NewMask);
57 return Status(NewMask, NewMode);
109class SIModeRegister {
111 std::vector<std::unique_ptr<BlockData>> BlockInfo;
112 std::queue<MachineBasicBlock *> Phase2List;
143 SIModeRegisterLegacy() : MachineFunctionPass(ID) {}
147 void getAnalysisUsage(AnalysisUsage &AU)
const override {
155 "Insert required mode register values",
false,
false)
157char SIModeRegisterLegacy::ID = 0;
162 return new SIModeRegisterLegacy();
171 unsigned Opcode =
MI.getOpcode();
172 if (
TII->usesFPDPRounding(
MI) ||
173 Opcode == AMDGPU::FPTRUNC_ROUND_F16_F32_PSEUDO ||
174 Opcode == AMDGPU::FPTRUNC_ROUND_F16_F32_PSEUDO_fake16_e32 ||
175 Opcode == AMDGPU::FPTRUNC_ROUND_F16_F32_PSEUDO_t16_e64 ||
176 Opcode == AMDGPU::FPTRUNC_ROUND_F32_F64_PSEUDO ||
177 Opcode == AMDGPU::FPTRUNC_ROUND_F16_F32_SALU_PSEUDO) {
179 case AMDGPU::V_INTERP_P1LL_F16:
180 case AMDGPU::V_INTERP_P1LV_F16:
181 case AMDGPU::V_INTERP_P2_F16:
185 case AMDGPU::FPTRUNC_ROUND_F16_F32_PSEUDO: {
186 unsigned Mode =
MI.getOperand(2).getImm();
188 MI.setDesc(
TII->get(AMDGPU::V_CVT_F16_F32_e32));
191 case AMDGPU::FPTRUNC_ROUND_F16_F32_PSEUDO_fake16_e32: {
192 unsigned Mode =
MI.getOperand(2).getImm();
194 MI.setDesc(
TII->get(AMDGPU::V_CVT_F16_F32_fake16_e32));
197 case AMDGPU::FPTRUNC_ROUND_F16_F32_PSEUDO_t16_e64: {
198 unsigned Mode =
MI.getOperand(6).getImm();
200 MI.setDesc(
TII->get(AMDGPU::V_CVT_F16_F32_t16_e64));
203 case AMDGPU::FPTRUNC_ROUND_F32_F64_PSEUDO: {
204 unsigned Mode =
MI.getOperand(2).getImm();
206 MI.setDesc(
TII->get(AMDGPU::V_CVT_F32_F64_e32));
209 case AMDGPU::FPTRUNC_ROUND_F16_F32_SALU_PSEUDO: {
210 unsigned Mode =
MI.getOperand(2).getImm();
212 MI.setDesc(
TII->get(AMDGPU::S_CVT_F16_F32));
216 return DefaultStatus;
227void SIModeRegister::insertSetreg(MachineBasicBlock &
MBB, MachineInstr *
MI,
228 const SIInstrInfo *
TII, Status InstrMode) {
229 while (InstrMode.
Mask) {
233 using namespace AMDGPU::Hwreg;
239 InstrMode.
Mask &= ~(((1 << Width) - 1) <<
Offset);
262void SIModeRegister::processBlockPhase1(MachineBasicBlock &
MBB,
263 const SIInstrInfo *
TII) {
264 auto NewInfo = std::make_unique<BlockData>();
265 MachineInstr *InsertionPoint =
nullptr;
271 bool RequirePending =
true;
273 for (MachineInstr &
MI :
MBB) {
274 Status InstrMode = getInstructionMode(
MI,
TII);
275 if (
MI.getOpcode() == AMDGPU::S_SETREG_B32 ||
276 MI.getOpcode() == AMDGPU::S_SETREG_B32_mode ||
277 MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 ||
278 MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32_mode) {
282 unsigned Dst =
TII->getNamedOperand(
MI, AMDGPU::OpName::simm16)->getImm();
283 using namespace AMDGPU::Hwreg;
284 auto [
Id,
Offset, Width] = HwregEncoding::decode(Dst);
291 if (InsertionPoint) {
292 insertSetreg(
MBB, InsertionPoint,
TII, IPChange.
delta(NewInfo->Change));
293 InsertionPoint =
nullptr;
298 if (
MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 ||
299 MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32_mode) {
300 unsigned Val =
TII->getNamedOperand(
MI, AMDGPU::OpName::imm)->getImm();
302 Status Setreg = Status(Mask,
Mode);
305 RequirePending =
false;
306 NewInfo->Change = NewInfo->Change.merge(Setreg);
308 NewInfo->Change = NewInfo->Change.mergeUnknown(Mask);
310 }
else if (!NewInfo->Change.isCompatible(InstrMode)) {
313 if (InsertionPoint) {
318 if (RequirePending) {
322 NewInfo->FirstInsertionPoint = InsertionPoint;
323 NewInfo->Require = NewInfo->Change;
324 RequirePending =
false;
326 insertSetreg(
MBB, InsertionPoint,
TII,
327 IPChange.
delta(NewInfo->Change));
328 IPChange = NewInfo->Change;
331 InsertionPoint = &
MI;
333 NewInfo->Change = NewInfo->Change.merge(InstrMode);
337 InsertionPoint = &
MI;
338 IPChange = NewInfo->Change;
339 NewInfo->Change = NewInfo->Change.
merge(InstrMode);
343 if (RequirePending) {
346 NewInfo->FirstInsertionPoint = InsertionPoint;
347 NewInfo->Require = NewInfo->Change;
348 }
else if (InsertionPoint) {
350 insertSetreg(
MBB, InsertionPoint,
TII, IPChange.
delta(NewInfo->Change));
352 NewInfo->Exit = NewInfo->Change;
360void SIModeRegister::processBlockPhase2(MachineBasicBlock &
MBB,
361 const SIInstrInfo *
TII) {
362 bool RevisitRequired =
false;
363 bool ExitSet =
false;
367 BlockInfo[ThisBlock]->Pred = DefaultStatus;
381 MachineBasicBlock &
PB = *(*P);
382 unsigned PredBlock =
PB.getNumber();
383 if ((ThisBlock == PredBlock) && (std::next(
P) ==
E)) {
384 BlockInfo[ThisBlock]->Pred = DefaultStatus;
386 }
else if (BlockInfo[PredBlock]->ExitSet) {
387 BlockInfo[ThisBlock]->Pred = BlockInfo[PredBlock]->Exit;
389 }
else if (PredBlock != ThisBlock)
390 RevisitRequired =
true;
392 for (
P = std::next(
P);
P !=
E;
P = std::next(
P)) {
393 MachineBasicBlock *Pred = *
P;
395 if (BlockInfo[PredBlock]->ExitSet) {
396 if (BlockInfo[ThisBlock]->ExitSet) {
397 BlockInfo[ThisBlock]->Pred =
398 BlockInfo[ThisBlock]->Pred.intersect(BlockInfo[PredBlock]->Exit);
400 BlockInfo[ThisBlock]->Pred = BlockInfo[PredBlock]->Exit;
403 }
else if (PredBlock != ThisBlock)
404 RevisitRequired =
true;
408 BlockInfo[ThisBlock]->Pred.merge(BlockInfo[ThisBlock]->Change);
409 if (BlockInfo[ThisBlock]->Exit != TmpStatus) {
410 BlockInfo[ThisBlock]->Exit = TmpStatus;
414 Phase2List.push(Succ);
416 BlockInfo[ThisBlock]->ExitSet = ExitSet;
418 Phase2List.push(&
MBB);
424void SIModeRegister::processBlockPhase3(MachineBasicBlock &
MBB,
425 const SIInstrInfo *
TII) {
427 if (!BlockInfo[ThisBlock]->Pred.isCompatible(BlockInfo[ThisBlock]->Require)) {
429 BlockInfo[ThisBlock]->Pred.delta(BlockInfo[ThisBlock]->Require);
430 if (BlockInfo[ThisBlock]->FirstInsertionPoint)
431 insertSetreg(
MBB, BlockInfo[ThisBlock]->FirstInsertionPoint,
TII, Delta);
438 return SIModeRegister().run(MF);
443 if (!SIModeRegister().
run(MF))
457 if (
F.hasFnAttribute(llvm::Attribute::StrictFP))
468 processBlockPhase1(BB,
TII);
474 Phase2List.push(&BB);
475 while (!Phase2List.empty()) {
476 processBlockPhase2(*Phase2List.front(),
TII);
483 processBlockPhase3(BB,
TII);
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
AMD GCN specific subclass of TargetSubtarget.
const HexagonInstrInfo * TII
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
#define FP_ROUND_MODE_DP(x)
#define FP_ROUND_ROUND_TO_NEAREST
#define FP_ROUND_ROUND_TO_ZERO
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
MachineInstr * FirstInsertionPoint
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represents analyses that only rely on functions' control flow.
FunctionPass class - This class is used to implement most global optimizations.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
SmallVectorImpl< MachineBasicBlock * >::iterator pred_iterator
pred_iterator pred_begin()
iterator_range< succ_iterator > successors()
MachineInstr & instr_front()
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.
Function & getFunction()
Return the LLVM function that this machine code represents.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Representation of each machine instruction.
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 run(MachineFunction &F, MachineFunctionAnalysisManager &AM)
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionPass * createSIModeRegisterPass()
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
Status delta(const Status &S) const
Status(unsigned NewMask, unsigned NewMode)
bool isCombinable(Status &S)
bool operator==(const Status &S) const
bool isCompatible(Status &S)
Status merge(const Status &S) const
Status intersect(const Status &S) const
bool operator!=(const Status &S) const
Status mergeUnknown(unsigned newMask)