23#define DEBUG_TYPE "si-mode-register"
25STATISTIC(NumSetregInserted,
"Number of setreg of mode register inserted.");
57 unsigned NewMode = (
Mode & NewMask);
58 return Status(NewMask, NewMode);
114class SIModeRegister {
116 std::vector<std::unique_ptr<BlockData>> BlockInfo;
117 std::queue<MachineBasicBlock *> Phase2List;
132 bool AnyNonDefaultMode =
false;
133 bool AnyWritesRoundMode =
false;
153 SIModeRegisterLegacy() : MachineFunctionPass(ID) {}
157 void getAnalysisUsage(AnalysisUsage &AU)
const override {
165 "Insert required mode register values",
false,
false)
167char SIModeRegisterLegacy::ID = 0;
172 return new SIModeRegisterLegacy();
176 switch (
MI.getOpcode()) {
177 case AMDGPU::FPTRUNC_ROUND_F16_F32_PSEUDO:
178 case AMDGPU::FPTRUNC_ROUND_F16_F32_PSEUDO_fake16_e32:
179 case AMDGPU::FPTRUNC_ROUND_F16_F32_PSEUDO_t16_e64:
180 case AMDGPU::FPTRUNC_ROUND_F32_F64_PSEUDO:
181 case AMDGPU::FPTRUNC_ROUND_F16_F32_SALU_PSEUDO:
190 switch (
MI.getOpcode()) {
191 case AMDGPU::V_INTERP_P1LL_F16:
192 case AMDGPU::V_INTERP_P1LV_F16:
193 case AMDGPU::V_INTERP_P2_F16:
201static std::optional<std::pair<unsigned, unsigned>>
203 switch (
MI.getOpcode()) {
204 case AMDGPU::S_SETREG_B32:
205 case AMDGPU::S_SETREG_B32_mode:
206 case AMDGPU::S_SETREG_IMM32_B32:
207 case AMDGPU::S_SETREG_IMM32_B32_mode:
213 unsigned Dst =
TII->getNamedOperand(
MI, AMDGPU::OpName::simm16)->getImm();
214 auto [Id,
Offset, Width] = HwregEncoding::decode(Dst);
222 switch (
MI.getOpcode()) {
223 case AMDGPU::S_ENDPGM:
224 case AMDGPU::S_ENDPGM_SAVED:
225 case AMDGPU::S_ENDPGM_ORDERED_PS_DONE:
228 return MI.isReturn();
238 unsigned Opcode =
MI.getOpcode();
241 case AMDGPU::V_INTERP_P1LL_F16:
242 case AMDGPU::V_INTERP_P1LV_F16:
243 case AMDGPU::V_INTERP_P2_F16:
247 case AMDGPU::FPTRUNC_ROUND_F16_F32_PSEUDO: {
248 unsigned Mode =
MI.getOperand(2).getImm();
250 MI.setDesc(
TII->get(AMDGPU::V_CVT_F16_F32_e32));
253 case AMDGPU::FPTRUNC_ROUND_F16_F32_PSEUDO_fake16_e32: {
254 unsigned Mode =
MI.getOperand(2).getImm();
256 MI.setDesc(
TII->get(AMDGPU::V_CVT_F16_F32_fake16_e32));
259 case AMDGPU::FPTRUNC_ROUND_F16_F32_PSEUDO_t16_e64: {
260 unsigned Mode =
MI.getOperand(6).getImm();
262 MI.setDesc(
TII->get(AMDGPU::V_CVT_F16_F32_t16_e64));
265 case AMDGPU::FPTRUNC_ROUND_F32_F64_PSEUDO: {
266 unsigned Mode =
MI.getOperand(2).getImm();
268 MI.setDesc(
TII->get(AMDGPU::V_CVT_F32_F64_e32));
271 case AMDGPU::FPTRUNC_ROUND_F16_F32_SALU_PSEUDO: {
272 unsigned Mode =
MI.getOperand(2).getImm();
274 MI.setDesc(
TII->get(AMDGPU::S_CVT_F16_F32));
278 return DefaultStatus;
289void SIModeRegister::insertSetreg(MachineBasicBlock &
MBB,
291 const SIInstrInfo *
TII, Status InstrMode) {
292 while (InstrMode.
Mask) {
296 using namespace AMDGPU::Hwreg;
302 InstrMode.
Mask &= ~(((1 << Width) - 1) <<
Offset);
325void SIModeRegister::processBlockPhase1(MachineBasicBlock &
MBB,
326 const SIInstrInfo *
TII) {
327 auto NewInfo = std::make_unique<BlockData>();
328 MachineInstr *InsertionPoint =
nullptr;
334 bool RequirePending =
true;
336 for (MachineInstr &
MI :
MBB) {
339 AnyWritesRoundMode |=
MI.getOpcode() == AMDGPU::S_ROUND_MODE;
340 Status InstrMode = getInstructionMode(
MI,
TII);
349 if (InsertionPoint) {
350 insertSetreg(
MBB, InsertionPoint,
TII, IPChange.
delta(NewInfo->Change));
351 InsertionPoint =
nullptr;
356 if (
MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 ||
357 MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32_mode) {
358 unsigned Val =
TII->getNamedOperand(
MI, AMDGPU::OpName::imm)->getImm();
360 Status Setreg = Status(Mask,
Mode);
363 RequirePending =
false;
364 NewInfo->Change = NewInfo->Change.merge(Setreg);
366 NewInfo->Change = NewInfo->Change.mergeUnknown(Mask);
372 NewInfo->BoundarySites.emplace_back(&
MI, NewInfo->Change);
373 }
else if (!NewInfo->Change.isCompatible(InstrMode)) {
376 if (InsertionPoint) {
381 if (RequirePending) {
385 NewInfo->FirstInsertionPoint = InsertionPoint;
386 NewInfo->Require = NewInfo->Change;
387 RequirePending =
false;
389 insertSetreg(
MBB, InsertionPoint,
TII,
390 IPChange.
delta(NewInfo->Change));
391 IPChange = NewInfo->Change;
394 InsertionPoint = &
MI;
396 NewInfo->Change = NewInfo->Change.merge(InstrMode);
400 InsertionPoint = &
MI;
401 IPChange = NewInfo->Change;
402 NewInfo->Change = NewInfo->Change.
merge(InstrMode);
406 if (RequirePending) {
409 NewInfo->FirstInsertionPoint = InsertionPoint;
410 NewInfo->Require = NewInfo->Change;
411 }
else if (InsertionPoint) {
413 insertSetreg(
MBB, InsertionPoint,
TII, IPChange.
delta(NewInfo->Change));
415 NewInfo->Exit = NewInfo->Change;
423void SIModeRegister::processBlockPhase2(MachineBasicBlock &
MBB,
424 const SIInstrInfo *
TII) {
425 bool RevisitRequired =
false;
426 bool ExitSet =
false;
430 BlockInfo[ThisBlock]->Pred = DefaultStatus;
444 MachineBasicBlock &
PB = *(*P);
445 unsigned PredBlock =
PB.getNumber();
446 if ((ThisBlock == PredBlock) && (std::next(
P) ==
E)) {
447 BlockInfo[ThisBlock]->Pred = DefaultStatus;
449 }
else if (BlockInfo[PredBlock]->ExitSet) {
450 BlockInfo[ThisBlock]->Pred = BlockInfo[PredBlock]->Exit;
452 }
else if (PredBlock != ThisBlock)
453 RevisitRequired =
true;
455 for (
P = std::next(
P);
P !=
E;
P = std::next(
P)) {
456 MachineBasicBlock *Pred = *
P;
458 if (BlockInfo[PredBlock]->ExitSet) {
459 if (BlockInfo[ThisBlock]->ExitSet) {
460 BlockInfo[ThisBlock]->Pred =
461 BlockInfo[ThisBlock]->Pred.intersect(BlockInfo[PredBlock]->Exit);
463 BlockInfo[ThisBlock]->Pred = BlockInfo[PredBlock]->Exit;
466 }
else if (PredBlock != ThisBlock)
467 RevisitRequired =
true;
471 BlockInfo[ThisBlock]->Pred.merge(BlockInfo[ThisBlock]->Change);
472 if (BlockInfo[ThisBlock]->Exit != TmpStatus) {
473 BlockInfo[ThisBlock]->Exit = TmpStatus;
477 Phase2List.push(Succ);
479 BlockInfo[ThisBlock]->ExitSet = ExitSet;
481 Phase2List.push(&
MBB);
487void SIModeRegister::processBlockPhase3(MachineBasicBlock &
MBB,
488 const SIInstrInfo *
TII) {
490 if (!BlockInfo[ThisBlock]->Pred.isCompatible(BlockInfo[ThisBlock]->Require)) {
492 BlockInfo[ThisBlock]->Pred.delta(BlockInfo[ThisBlock]->Require);
493 if (BlockInfo[ThisBlock]->FirstInsertionPoint)
494 insertSetreg(
MBB, BlockInfo[ThisBlock]->FirstInsertionPoint,
TII, Delta);
500 if (!AnyNonDefaultMode || AnyWritesRoundMode)
502 for (
auto &[
MI, ChangeAtSite] : BlockInfo[ThisBlock]->BoundarySites) {
503 Status AtSite = BlockInfo[ThisBlock]->Pred.merge(ChangeAtSite);
508 if (!
MI->isTerminator()) {
509 insertSetreg(
MBB, std::next(
MI->getIterator()),
TII,
510 DefaultStatus.
delta(AtSite));
516 return SIModeRegister().run(MF);
521 if (!SIModeRegister().
run(MF))
535 if (
F.hasFnAttribute(llvm::Attribute::StrictFP))
546 processBlockPhase1(BB,
TII);
552 Phase2List.push(&BB);
553 while (!Phase2List.empty()) {
554 processBlockPhase2(*Phase2List.front(),
TII);
561 processBlockPhase3(BB,
TII);
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
AMD GCN specific subclass of TargetSubtarget.
const HexagonInstrInfo * TII
OptimizedStructLayoutField Field
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
static bool isFPTruncRoundPseudo(const MachineInstr &MI)
static std::optional< std::pair< unsigned, unsigned > > getModeSetregField(const MachineInstr &MI, const SIInstrInfo *TII)
static bool mayNeedNonDefaultMode(const MachineInstr &MI)
static bool isReturnToCaller(const MachineInstr &MI)
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
MachineInstr * FirstInsertionPoint
SmallVector< std::pair< MachineInstr *, Status >, 2 > BoundarySites
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()
MachineInstrBundleIterator< MachineInstr > iterator
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)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
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)