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 int64_t 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 =
static_cast<unsigned>(
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 =
static_cast<unsigned>(
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 =
static_cast<unsigned>(
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 =
static_cast<unsigned>(
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 =
static_cast<unsigned>(
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 =
static_cast<unsigned>(
359 TII->getNamedOperand(
MI, AMDGPU::OpName::imm)->
getImm());
361 Status Setreg = Status(Mask,
Mode);
364 RequirePending =
false;
365 NewInfo->Change = NewInfo->Change.merge(Setreg);
367 NewInfo->Change = NewInfo->Change.mergeUnknown(Mask);
373 NewInfo->BoundarySites.emplace_back(&
MI, NewInfo->Change);
374 }
else if (!NewInfo->Change.isCompatible(InstrMode)) {
377 if (InsertionPoint) {
382 if (RequirePending) {
386 NewInfo->FirstInsertionPoint = InsertionPoint;
387 NewInfo->Require = NewInfo->Change;
388 RequirePending =
false;
390 insertSetreg(
MBB, InsertionPoint,
TII,
391 IPChange.
delta(NewInfo->Change));
392 IPChange = NewInfo->Change;
395 InsertionPoint = &
MI;
397 NewInfo->Change = NewInfo->Change.merge(InstrMode);
401 InsertionPoint = &
MI;
402 IPChange = NewInfo->Change;
403 NewInfo->Change = NewInfo->Change.
merge(InstrMode);
407 if (RequirePending) {
410 NewInfo->FirstInsertionPoint = InsertionPoint;
411 NewInfo->Require = NewInfo->Change;
412 }
else if (InsertionPoint) {
414 insertSetreg(
MBB, InsertionPoint,
TII, IPChange.
delta(NewInfo->Change));
416 NewInfo->Exit = NewInfo->Change;
424void SIModeRegister::processBlockPhase2(MachineBasicBlock &
MBB,
425 const SIInstrInfo *
TII) {
426 bool RevisitRequired =
false;
427 bool ExitSet =
false;
431 BlockInfo[ThisBlock]->Pred = DefaultStatus;
445 MachineBasicBlock &
PB = *(*P);
446 unsigned PredBlock =
PB.getNumber();
447 if ((ThisBlock == PredBlock) && (std::next(
P) ==
E)) {
448 BlockInfo[ThisBlock]->Pred = DefaultStatus;
450 }
else if (BlockInfo[PredBlock]->ExitSet) {
451 BlockInfo[ThisBlock]->Pred = BlockInfo[PredBlock]->Exit;
453 }
else if (PredBlock != ThisBlock)
454 RevisitRequired =
true;
456 for (
P = std::next(
P);
P !=
E;
P = std::next(
P)) {
457 MachineBasicBlock *Pred = *
P;
459 if (BlockInfo[PredBlock]->ExitSet) {
460 if (BlockInfo[ThisBlock]->ExitSet) {
461 BlockInfo[ThisBlock]->Pred =
462 BlockInfo[ThisBlock]->Pred.intersect(BlockInfo[PredBlock]->Exit);
464 BlockInfo[ThisBlock]->Pred = BlockInfo[PredBlock]->Exit;
467 }
else if (PredBlock != ThisBlock)
468 RevisitRequired =
true;
472 BlockInfo[ThisBlock]->Pred.merge(BlockInfo[ThisBlock]->Change);
473 if (BlockInfo[ThisBlock]->Exit != TmpStatus) {
474 BlockInfo[ThisBlock]->Exit = TmpStatus;
478 Phase2List.push(Succ);
480 BlockInfo[ThisBlock]->ExitSet = ExitSet;
482 Phase2List.push(&
MBB);
488void SIModeRegister::processBlockPhase3(MachineBasicBlock &
MBB,
489 const SIInstrInfo *
TII) {
491 if (!BlockInfo[ThisBlock]->Pred.isCompatible(BlockInfo[ThisBlock]->Require)) {
493 BlockInfo[ThisBlock]->Pred.delta(BlockInfo[ThisBlock]->Require);
494 if (BlockInfo[ThisBlock]->FirstInsertionPoint)
495 insertSetreg(
MBB, BlockInfo[ThisBlock]->FirstInsertionPoint,
TII, Delta);
501 if (!AnyNonDefaultMode || AnyWritesRoundMode)
503 for (
auto &[
MI, ChangeAtSite] : BlockInfo[ThisBlock]->BoundarySites) {
504 Status AtSite = BlockInfo[ThisBlock]->Pred.merge(ChangeAtSite);
509 if (!
MI->isTerminator()) {
510 insertSetreg(
MBB, std::next(
MI->getIterator()),
TII,
511 DefaultStatus.
delta(AtSite));
517 return SIModeRegister().run(MF);
522 if (!SIModeRegister().
run(MF))
536 if (
F.hasFnAttribute(llvm::Attribute::StrictFP))
547 processBlockPhase1(BB,
TII);
553 Phase2List.push(&BB);
554 while (!Phase2List.empty()) {
555 processBlockPhase2(*Phase2List.front(),
TII);
562 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(Mod->getContext(), 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.
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
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)