37#define DEBUG_TYPE "amdgpu-reg-bank-legalize"
46template <
typename SrcTy>
48m_GAMDGPUReadAnyLane(
const SrcTy &Src) {
62 return "AMDGPU Register Bank Legalize";
83 "AMDGPU Register Bank Legalize",
false,
false)
91char AMDGPURegBankLegalizeLegacy::ID = 0;
96 return new AMDGPURegBankLegalizeLegacy();
101 static std::mutex GlobalMutex;
104 std::lock_guard<std::mutex> Lock(GlobalMutex);
105 auto [It, Inserted] = CacheForRuleSet.
try_emplace(ST.getGeneration());
107 It->second = std::make_unique<RegBankLegalizeRules>(ST,
MRI);
109 It->second->refreshRefs(ST,
MRI);
128 : B(B), MRI(*B.getMRI()), TRI(TRI),
129 SgprRB(&RBI.getRegBank(
AMDGPU::SGPRRegBankID)),
130 VgprRB(&RBI.getRegBank(
AMDGPU::VGPRRegBankID)) {};
133 std::pair<MachineInstr *, Register>
tryMatch(
Register Src,
unsigned Opcode);
146 if (RB && RB->
getID() == AMDGPU::VCCRegBankID)
150 return RC && TRI.isSGPRClass(RC) && MRI.getType(Reg) ==
LLT::scalar(1);
153std::pair<MachineInstr *, Register>
166 if (!UnMerge || UnMerge->getNumDefs() != DefRegs.
size())
168 for (
unsigned I = 1;
I < DefRegs.
size(); ++
I) {
169 if (UnMerge->getReg(
I) != DefRegs[
I])
172 return UnMerge->getSourceReg();
180 for (
unsigned i = 0; i <
Merge->getNumSources(); ++i) {
183 m_GAMDGPUReadAnyLane(
m_Reg(Src))))
187 return ReadAnyLaneSrcs;
220 if (ReadAnyLaneSrcs.
empty())
228 return ReadAnyLaneSrcs;
238 int Idx = UnMerge->findRegisterDefOperandIdx(Src,
nullptr);
240 if (!
Merge || UnMerge->getNumDefs() !=
Merge->getNumSources())
244 if (MRI.getType(Src) != MRI.getType(SrcRegIdx))
247 auto [RALEl, RALElSrc] =
tryMatch(SrcRegIdx, AMDGPU::G_AMDGPU_READANYLANE);
257 MRI.replaceRegWith(Dst, Src);
259 B.buildCopy(Dst, Src);
264 Register Dst = Copy.getOperand(0).getReg();
265 Register Src = Copy.getOperand(1).getReg();
268 if (Dst.isVirtual() ? (MRI.getRegBankOrNull(Dst) != VgprRB)
269 : !TRI.isVGPR(MRI, Dst))
273 if (!Src.isVirtual() || MRI.getRegClassOrNull(Src))
279 B.setInstrAndDebugLoc(Copy);
281 if (ReadAnyLaneSrcRegs.
empty())
285 if (ReadAnyLaneSrcRegs.
size() == 1) {
286 ReadAnyLaneSrc = ReadAnyLaneSrcRegs[0];
289 auto Merge = B.buildMergeLikeInstr({VgprRB, MRI.getType(RALDst)},
291 ReadAnyLaneSrc =
Merge.getReg(0);
307 auto Bitcast = B.buildBitcast({VgprRB, MRI.getType(Src)}, ReadAnyLaneSrc);
322 if (!Dst.isVirtual() || !Src.isVirtual())
332 if (
isLaneMask(Dst) && MRI.getRegBankOrNull(Src) == SgprRB) {
335 "sgpr S1 must be result of G_TRUNC of sgpr S32");
339 LLT Ty = MRI.getType(TruncS32Src);
340 auto One = B.buildConstant({SgprRB, Ty}, 1);
341 auto BoolSrc = B.buildAnd({SgprRB, Ty}, TruncS32Src, One);
342 B.buildInstr(AMDGPU::G_AMDGPU_COPY_VCC_SCC, {Dst}, {BoolSrc});
354 if (MRI.getType(Src) != S1)
361 LLT DstTy = MRI.getType(Dst);
362 LLT TruncSrcTy = MRI.getType(TruncSrc);
364 if (DstTy == TruncSrcTy) {
365 MRI.replaceRegWith(Dst, TruncSrc);
372 if (DstTy == S32 && TruncSrcTy == S64) {
373 auto Unmerge = B.buildUnmerge({SgprRB, DstTy}, TruncSrc);
374 MRI.replaceRegWith(Dst, Unmerge.getReg(0));
379 if (DstTy == S64 && TruncSrcTy == S32) {
380 B.buildMergeLikeInstr(
MI.getOperand(0).getReg(),
381 {TruncSrc, B.buildUndef({SgprRB, TruncSrcTy})});
386 if (DstTy ==
S32 && TruncSrcTy ==
S16) {
387 B.buildAnyExt(Dst, TruncSrc);
392 if (DstTy ==
S16 && TruncSrcTy ==
S32) {
393 B.buildTrunc(Dst, TruncSrc);
404 for (
unsigned i = 0; i <
MRI.getNumVirtRegs(); ++i) {
410 if (RB && RB->
getID() == AMDGPU::SGPRRegBankID) {
437 B.setCSEInfo(&CSEInfo);
438 B.setChangeObserver(Observer);
462 if (!
MI->isPreISelOpcode())
496 if (
MI.getOpcode() == AMDGPU::COPY) {
500 if (
MI.getOpcode() == AMDGPU::G_ANYEXT) {
508 "Registers with sgpr reg bank and S1 LLT are not legal after "
509 "AMDGPURegBankLegalize. Should lower to sgpr S32");
514bool AMDGPURegBankLegalizeLegacy::runOnMachineFunction(
MachineFunction &MF) {
518 GISelCSEAnalysisWrapper &
Wrapper =
519 getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();
520 return &
Wrapper.get(getAnalysis<TargetPassConfig>().getCSEConfig());
523 return &getAnalysis<MachineUniformityAnalysisPass>()
524 .getUniformityInfo();
527 return &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
amdgpu aa AMDGPU Address space based Alias Analysis Wrapper
static Register getAnySgprS1(const MachineRegisterInfo &MRI)
static bool runRegBankLegalize(MachineFunction &MF, function_ref< GISelCSEInfo *()> GetCSEInfo, function_ref< const MachineUniformityInfo *()> GetMUI, function_ref< GISelValueTracking *()> GetVT)
const RegBankLegalizeRules & getRules(const GCNSubtarget &ST, MachineRegisterInfo &MRI)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Provides analysis for continuously CSEing during GISel passes.
This file implements a version of MachineIRBuilder which CSEs insts within a MachineBasicBlock.
AMD GCN specific subclass of TargetSubtarget.
Provides analysis for querying information about KnownBits during GISel passes.
Declares convenience wrapper classes for interpreting MachineInstr instances as specific generic oper...
Contains matchers for matching SSA Machine Instructions.
Promote Memory to Register
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
MachineRegisterInfo & MRI
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
Target-Independent Code Generator Pass Configuration Options pass.
Register tryMatchUnmergeDefs(SmallVectorImpl< Register > &DefRegs)
void replaceRegWithOrBuildCopy(Register Dst, Register Src)
AMDGPURegBankLegalizeCombiner(MachineIRBuilder &B, const SIRegisterInfo &TRI, const RegisterBankInfo &RBI)
bool isLaneMask(Register Reg)
void tryCombineS1AnyExt(MachineInstr &MI)
std::pair< MachineInstr *, Register > tryMatch(Register Src, unsigned Opcode)
SmallVector< Register > tryMatchMergeReadAnyLane(GMergeLikeInstr *Merge)
void tryCombineCopy(MachineInstr &MI)
bool tryEliminateReadAnyLane(MachineInstr &Copy)
SmallVector< Register > getReadAnyLaneSrcs(Register Src)
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
bool findRuleAndApplyMapping(MachineInstr &MI)
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
Defines a builder that does CSE of MachineInstructions using GISelCSEInfo.
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
FunctionPass class - This class is used to implement most global optimizations.
The actual analysis pass wrapper.
Simple wrapper observer that takes several observers, and calls each one for each event.
void addObserver(GISelChangeObserver *O)
To use KnownBitsInfo analysis in a pass, KnownBitsInfo &Info = getAnalysis<GISelValueTrackingInfoAnal...
Represents G_BUILD_VECTOR, G_CONCAT_VECTORS or G_MERGE_VALUES.
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
An RAII based helper class to modify MachineFunctionProperties when running pass.
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.
Properties which a MachineFunction may have at a given point in time.
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.
const MachineFunctionProperties & getProperties() const
Get the function properties.
Helper class to build MachineInstr.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
A simple RAII based Delegate installer.
A simple RAII based Observer installer.
Holds all the information related to register banks.
This class implements the register bank concept.
unsigned getID() const
Get the identifier of this register bank.
Wrapper class representing virtual and physical registers.
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Target-Independent Code Generator Pass Configuration Options.
An efficient, type-erasing, non-owning reference to a callable.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
operand_type_match m_Reg()
bool mi_match(Reg R, const MachineRegisterInfo &MRI, Pattern &&P)
UnaryOp_match< SrcTy, TargetOpcode::G_BITCAST > m_GBitcast(const SrcTy &Src)
UnaryOp_match< SrcTy, TargetOpcode::G_ANYEXT > m_GAnyExt(const SrcTy &Src)
UnaryOp_match< SrcTy, TargetOpcode::G_TRUNC > m_GTrunc(const SrcTy &Src)
This is an optimization pass for GlobalISel generic memory operations.
GenericUniformityInfo< MachineSSAContext > MachineUniformityInfo
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
LLVM_ABI MachineInstr * getOpcodeDef(unsigned Opcode, Register Reg, const MachineRegisterInfo &MRI)
See if Reg is defined by an single def instruction that is Opcode.
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...
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
char & AMDGPURegBankLegalizeLegacyID
LLVM_ABI void eraseInstr(MachineInstr &MI, MachineRegisterInfo &MRI, LostDebugLocObserver *LocObserver=nullptr)
FunctionPass * createAMDGPURegBankLegalizeLegacyPass()
MCRegisterClass TargetRegisterClass