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();
100 static std::mutex GlobalMutex;
103 std::lock_guard<std::mutex> Lock(GlobalMutex);
104 auto [It, Inserted] = CacheForRuleSet.
try_emplace(ST.getGeneration());
106 It->second = std::make_unique<RegBankLegalizeRules>(ST);
108 It->second->refreshRefs(ST);
127 : B(B), MRI(*B.getMRI()), TRI(TRI),
128 SgprRB(&RBI.getRegBank(
AMDGPU::SGPRRegBankID)),
129 VgprRB(&RBI.getRegBank(
AMDGPU::VGPRRegBankID)) {};
132 std::pair<MachineInstr *, Register>
tryMatch(
Register Src,
unsigned Opcode);
145 if (RB && RB->
getID() == AMDGPU::VCCRegBankID)
149 return RC && TRI.isSGPRClass(RC) && MRI.getType(Reg) ==
LLT::scalar(1);
152std::pair<MachineInstr *, Register>
165 if (!UnMerge || UnMerge->getNumDefs() != DefRegs.
size())
167 for (
unsigned I = 1;
I < DefRegs.
size(); ++
I) {
168 if (UnMerge->getReg(
I) != DefRegs[
I])
171 return UnMerge->getSourceReg();
179 for (
unsigned i = 0; i <
Merge->getNumSources(); ++i) {
182 m_GAMDGPUReadAnyLane(
m_Reg(Src))))
186 return ReadAnyLaneSrcs;
219 if (ReadAnyLaneSrcs.
empty())
227 return ReadAnyLaneSrcs;
237 int Idx = UnMerge->findRegisterDefOperandIdx(Src,
nullptr);
239 if (!
Merge || UnMerge->getNumDefs() !=
Merge->getNumSources())
243 if (MRI.getType(Src) != MRI.getType(SrcRegIdx))
246 auto [RALEl, RALElSrc] =
tryMatch(SrcRegIdx, AMDGPU::G_AMDGPU_READANYLANE);
256 MRI.replaceRegWith(Dst, Src);
258 B.buildCopy(Dst, Src);
263 Register Dst = Copy.getOperand(0).getReg();
264 Register Src = Copy.getOperand(1).getReg();
267 if (Dst.isVirtual() ? (MRI.getRegBankOrNull(Dst) != VgprRB)
268 : !TRI.isVGPR(MRI, Dst))
272 if (!Src.isVirtual() || MRI.getRegClassOrNull(Src))
278 B.setInstrAndDebugLoc(Copy);
280 if (ReadAnyLaneSrcRegs.
empty())
284 if (ReadAnyLaneSrcRegs.
size() == 1) {
285 ReadAnyLaneSrc = ReadAnyLaneSrcRegs[0];
288 auto Merge = B.buildMergeLikeInstr({VgprRB, MRI.getType(RALDst)},
290 ReadAnyLaneSrc =
Merge.getReg(0);
306 auto Bitcast = B.buildBitcast({VgprRB, MRI.getType(Src)}, ReadAnyLaneSrc);
321 if (!Dst.isVirtual() || !Src.isVirtual())
331 if (
isLaneMask(Dst) && MRI.getRegBankOrNull(Src) == SgprRB) {
334 "sgpr S1 must be result of G_TRUNC of sgpr S32");
338 LLT Ty = MRI.getType(TruncS32Src);
339 auto One = B.buildConstant({SgprRB, Ty}, 1);
340 auto BoolSrc = B.buildAnd({SgprRB, Ty}, TruncS32Src, One);
341 B.buildInstr(AMDGPU::G_AMDGPU_COPY_VCC_SCC, {Dst}, {BoolSrc});
353 if (MRI.getType(Src) != S1)
360 LLT DstTy = MRI.getType(Dst);
361 LLT TruncSrcTy = MRI.getType(TruncSrc);
363 if (DstTy == TruncSrcTy) {
364 MRI.replaceRegWith(Dst, TruncSrc);
371 if (DstTy == S32 && TruncSrcTy == S64) {
372 auto Unmerge = B.buildUnmerge({SgprRB, DstTy}, TruncSrc);
373 MRI.replaceRegWith(Dst, Unmerge.getReg(0));
378 if (DstTy == S64 && TruncSrcTy == S32) {
379 B.buildMergeLikeInstr(
MI.getOperand(0).getReg(),
380 {TruncSrc, B.buildUndef({SgprRB, TruncSrcTy})});
385 if (DstTy ==
S32 && TruncSrcTy ==
S16) {
386 B.buildAnyExt(Dst, TruncSrc);
391 if (DstTy ==
S16 && TruncSrcTy ==
S32) {
392 B.buildTrunc(Dst, TruncSrc);
403 for (
unsigned i = 0; i <
MRI.getNumVirtRegs(); ++i) {
409 if (RB && RB->
getID() == AMDGPU::SGPRRegBankID) {
436 B.setCSEInfo(&CSEInfo);
437 B.setChangeObserver(Observer);
460 if (!
MI->isPreISelOpcode())
494 if (
MI.getOpcode() == AMDGPU::COPY) {
498 if (
MI.getOpcode() == AMDGPU::G_ANYEXT) {
506 "Registers with sgpr reg bank and S1 LLT are not legal after "
507 "AMDGPURegBankLegalize. Should lower to sgpr S32");
512bool AMDGPURegBankLegalizeLegacy::runOnMachineFunction(
MachineFunction &MF) {
516 GISelCSEAnalysisWrapper &
Wrapper =
517 getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();
518 return &
Wrapper.get(getAnalysis<TargetPassConfig>().getCSEConfig());
521 return &getAnalysis<MachineUniformityAnalysisPass>()
522 .getUniformityInfo();
525 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)
const RegBankLegalizeRules & getRules(const GCNSubtarget &ST)
static bool runRegBankLegalize(MachineFunction &MF, function_ref< GISelCSEInfo *()> GetCSEInfo, function_ref< const MachineUniformityInfo *()> GetMUI, function_ref< GISelValueTracking *()> GetVT)
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
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.
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.
constexpr bool isValid() const
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