10b57cec5SDimitry Andric //===- SILoadStoreOptimizer.cpp -------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This pass tries to fuse DS instructions with close by immediate offsets.
100b57cec5SDimitry Andric // This will fuse operations such as
110b57cec5SDimitry Andric // ds_read_b32 v0, v2 offset:16
120b57cec5SDimitry Andric // ds_read_b32 v1, v2 offset:32
130b57cec5SDimitry Andric // ==>
140b57cec5SDimitry Andric // ds_read2_b32 v[0:1], v2, offset0:4 offset1:8
150b57cec5SDimitry Andric //
160b57cec5SDimitry Andric // The same is done for certain SMEM and VMEM opcodes, e.g.:
170b57cec5SDimitry Andric // s_buffer_load_dword s4, s[0:3], 4
180b57cec5SDimitry Andric // s_buffer_load_dword s5, s[0:3], 8
190b57cec5SDimitry Andric // ==>
200b57cec5SDimitry Andric // s_buffer_load_dwordx2 s[4:5], s[0:3], 4
210b57cec5SDimitry Andric //
220b57cec5SDimitry Andric // This pass also tries to promote constant offset to the immediate by
230b57cec5SDimitry Andric // adjusting the base. It tries to use a base from the nearby instructions that
240b57cec5SDimitry Andric // allows it to have a 13bit constant offset and then promotes the 13bit offset
250b57cec5SDimitry Andric // to the immediate.
260b57cec5SDimitry Andric // E.g.
270b57cec5SDimitry Andric // s_movk_i32 s0, 0x1800
280b57cec5SDimitry Andric // v_add_co_u32_e32 v0, vcc, s0, v2
290b57cec5SDimitry Andric // v_addc_co_u32_e32 v1, vcc, 0, v6, vcc
300b57cec5SDimitry Andric //
310b57cec5SDimitry Andric // s_movk_i32 s0, 0x1000
320b57cec5SDimitry Andric // v_add_co_u32_e32 v5, vcc, s0, v2
330b57cec5SDimitry Andric // v_addc_co_u32_e32 v6, vcc, 0, v6, vcc
340b57cec5SDimitry Andric // global_load_dwordx2 v[5:6], v[5:6], off
350b57cec5SDimitry Andric // global_load_dwordx2 v[0:1], v[0:1], off
360b57cec5SDimitry Andric // =>
370b57cec5SDimitry Andric // s_movk_i32 s0, 0x1000
380b57cec5SDimitry Andric // v_add_co_u32_e32 v5, vcc, s0, v2
390b57cec5SDimitry Andric // v_addc_co_u32_e32 v6, vcc, 0, v6, vcc
400b57cec5SDimitry Andric // global_load_dwordx2 v[5:6], v[5:6], off
410b57cec5SDimitry Andric // global_load_dwordx2 v[0:1], v[5:6], off offset:2048
420b57cec5SDimitry Andric //
430b57cec5SDimitry Andric // Future improvements:
440b57cec5SDimitry Andric //
458bcb0991SDimitry Andric // - This is currently missing stores of constants because loading
460b57cec5SDimitry Andric // the constant into the data register is placed between the stores, although
470b57cec5SDimitry Andric // this is arguably a scheduling problem.
480b57cec5SDimitry Andric //
490b57cec5SDimitry Andric // - Live interval recomputing seems inefficient. This currently only matches
500b57cec5SDimitry Andric // one pair, and recomputes live intervals and moves on to the next pair. It
510b57cec5SDimitry Andric // would be better to compute a list of all merges that need to occur.
520b57cec5SDimitry Andric //
530b57cec5SDimitry Andric // - With a list of instructions to process, we can also merge more. If a
540b57cec5SDimitry Andric // cluster of loads have offsets that are too large to fit in the 8-bit
550b57cec5SDimitry Andric // offsets, but are close enough to fit in the 8 bits, we can add to the base
560b57cec5SDimitry Andric // pointer and use the new reduced offsets.
570b57cec5SDimitry Andric //
580b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
590b57cec5SDimitry Andric
600b57cec5SDimitry Andric #include "AMDGPU.h"
61af732203SDimitry Andric #include "GCNSubtarget.h"
620b57cec5SDimitry Andric #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
630b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
640b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
65480093f4SDimitry Andric #include "llvm/InitializePasses.h"
660b57cec5SDimitry Andric
670b57cec5SDimitry Andric using namespace llvm;
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric #define DEBUG_TYPE "si-load-store-opt"
700b57cec5SDimitry Andric
710b57cec5SDimitry Andric namespace {
720b57cec5SDimitry Andric enum InstClassEnum {
730b57cec5SDimitry Andric UNKNOWN,
740b57cec5SDimitry Andric DS_READ,
750b57cec5SDimitry Andric DS_WRITE,
760b57cec5SDimitry Andric S_BUFFER_LOAD_IMM,
778bcb0991SDimitry Andric BUFFER_LOAD,
788bcb0991SDimitry Andric BUFFER_STORE,
798bcb0991SDimitry Andric MIMG,
80480093f4SDimitry Andric TBUFFER_LOAD,
81480093f4SDimitry Andric TBUFFER_STORE,
820b57cec5SDimitry Andric };
830b57cec5SDimitry Andric
845ffd83dbSDimitry Andric struct AddressRegs {
855ffd83dbSDimitry Andric unsigned char NumVAddrs = 0;
865ffd83dbSDimitry Andric bool SBase = false;
875ffd83dbSDimitry Andric bool SRsrc = false;
885ffd83dbSDimitry Andric bool SOffset = false;
895ffd83dbSDimitry Andric bool VAddr = false;
905ffd83dbSDimitry Andric bool Addr = false;
915ffd83dbSDimitry Andric bool SSamp = false;
920b57cec5SDimitry Andric };
930b57cec5SDimitry Andric
945ffd83dbSDimitry Andric // GFX10 image_sample instructions can have 12 vaddrs + srsrc + ssamp.
955ffd83dbSDimitry Andric const unsigned MaxAddressRegs = 12 + 1 + 1;
965ffd83dbSDimitry Andric
970b57cec5SDimitry Andric class SILoadStoreOptimizer : public MachineFunctionPass {
980b57cec5SDimitry Andric struct CombineInfo {
990b57cec5SDimitry Andric MachineBasicBlock::iterator I;
1000b57cec5SDimitry Andric unsigned EltSize;
101480093f4SDimitry Andric unsigned Offset;
102480093f4SDimitry Andric unsigned Width;
103480093f4SDimitry Andric unsigned Format;
1040b57cec5SDimitry Andric unsigned BaseOff;
105480093f4SDimitry Andric unsigned DMask;
1060b57cec5SDimitry Andric InstClassEnum InstClass;
107*5f7ddb14SDimitry Andric unsigned CPol = 0;
1080b57cec5SDimitry Andric bool UseST64;
1095ffd83dbSDimitry Andric int AddrIdx[MaxAddressRegs];
1105ffd83dbSDimitry Andric const MachineOperand *AddrReg[MaxAddressRegs];
1118bcb0991SDimitry Andric unsigned NumAddresses;
1125ffd83dbSDimitry Andric unsigned Order;
1138bcb0991SDimitry Andric
hasSameBaseAddress__anon8de982330111::SILoadStoreOptimizer::CombineInfo1148bcb0991SDimitry Andric bool hasSameBaseAddress(const MachineInstr &MI) {
1158bcb0991SDimitry Andric for (unsigned i = 0; i < NumAddresses; i++) {
1168bcb0991SDimitry Andric const MachineOperand &AddrRegNext = MI.getOperand(AddrIdx[i]);
1178bcb0991SDimitry Andric
1188bcb0991SDimitry Andric if (AddrReg[i]->isImm() || AddrRegNext.isImm()) {
1198bcb0991SDimitry Andric if (AddrReg[i]->isImm() != AddrRegNext.isImm() ||
1208bcb0991SDimitry Andric AddrReg[i]->getImm() != AddrRegNext.getImm()) {
1218bcb0991SDimitry Andric return false;
1228bcb0991SDimitry Andric }
1238bcb0991SDimitry Andric continue;
1248bcb0991SDimitry Andric }
1258bcb0991SDimitry Andric
1268bcb0991SDimitry Andric // Check same base pointer. Be careful of subregisters, which can occur
1278bcb0991SDimitry Andric // with vectors of pointers.
1288bcb0991SDimitry Andric if (AddrReg[i]->getReg() != AddrRegNext.getReg() ||
1298bcb0991SDimitry Andric AddrReg[i]->getSubReg() != AddrRegNext.getSubReg()) {
1308bcb0991SDimitry Andric return false;
1318bcb0991SDimitry Andric }
1328bcb0991SDimitry Andric }
1338bcb0991SDimitry Andric return true;
1348bcb0991SDimitry Andric }
1358bcb0991SDimitry Andric
hasMergeableAddress__anon8de982330111::SILoadStoreOptimizer::CombineInfo1368bcb0991SDimitry Andric bool hasMergeableAddress(const MachineRegisterInfo &MRI) {
1378bcb0991SDimitry Andric for (unsigned i = 0; i < NumAddresses; ++i) {
1388bcb0991SDimitry Andric const MachineOperand *AddrOp = AddrReg[i];
1398bcb0991SDimitry Andric // Immediates are always OK.
1408bcb0991SDimitry Andric if (AddrOp->isImm())
1418bcb0991SDimitry Andric continue;
1428bcb0991SDimitry Andric
1438bcb0991SDimitry Andric // Don't try to merge addresses that aren't either immediates or registers.
1448bcb0991SDimitry Andric // TODO: Should be possible to merge FrameIndexes and maybe some other
1458bcb0991SDimitry Andric // non-register
1468bcb0991SDimitry Andric if (!AddrOp->isReg())
1478bcb0991SDimitry Andric return false;
1488bcb0991SDimitry Andric
1498bcb0991SDimitry Andric // TODO: We should be able to merge physical reg addreses.
150af732203SDimitry Andric if (AddrOp->getReg().isPhysical())
1518bcb0991SDimitry Andric return false;
1528bcb0991SDimitry Andric
1538bcb0991SDimitry Andric // If an address has only one use then there will be on other
1548bcb0991SDimitry Andric // instructions with the same address, so we can't merge this one.
1558bcb0991SDimitry Andric if (MRI.hasOneNonDBGUse(AddrOp->getReg()))
1568bcb0991SDimitry Andric return false;
1578bcb0991SDimitry Andric }
1588bcb0991SDimitry Andric return true;
1598bcb0991SDimitry Andric }
1608bcb0991SDimitry Andric
1618bcb0991SDimitry Andric void setMI(MachineBasicBlock::iterator MI, const SIInstrInfo &TII,
1628bcb0991SDimitry Andric const GCNSubtarget &STM);
1630b57cec5SDimitry Andric };
1640b57cec5SDimitry Andric
1650b57cec5SDimitry Andric struct BaseRegisters {
1665ffd83dbSDimitry Andric Register LoReg;
1675ffd83dbSDimitry Andric Register HiReg;
1680b57cec5SDimitry Andric
1690b57cec5SDimitry Andric unsigned LoSubReg = 0;
1700b57cec5SDimitry Andric unsigned HiSubReg = 0;
1710b57cec5SDimitry Andric };
1720b57cec5SDimitry Andric
1730b57cec5SDimitry Andric struct MemAddress {
1740b57cec5SDimitry Andric BaseRegisters Base;
1750b57cec5SDimitry Andric int64_t Offset = 0;
1760b57cec5SDimitry Andric };
1770b57cec5SDimitry Andric
1780b57cec5SDimitry Andric using MemInfoMap = DenseMap<MachineInstr *, MemAddress>;
1790b57cec5SDimitry Andric
1800b57cec5SDimitry Andric private:
1810b57cec5SDimitry Andric const GCNSubtarget *STM = nullptr;
1820b57cec5SDimitry Andric const SIInstrInfo *TII = nullptr;
1830b57cec5SDimitry Andric const SIRegisterInfo *TRI = nullptr;
1840b57cec5SDimitry Andric MachineRegisterInfo *MRI = nullptr;
1850b57cec5SDimitry Andric AliasAnalysis *AA = nullptr;
1860b57cec5SDimitry Andric bool OptimizeAgain;
1870b57cec5SDimitry Andric
188480093f4SDimitry Andric static bool dmasksCanBeCombined(const CombineInfo &CI,
189480093f4SDimitry Andric const SIInstrInfo &TII,
190480093f4SDimitry Andric const CombineInfo &Paired);
1915ffd83dbSDimitry Andric static bool offsetsCanBeCombined(CombineInfo &CI, const GCNSubtarget &STI,
1925ffd83dbSDimitry Andric CombineInfo &Paired, bool Modify = false);
1935ffd83dbSDimitry Andric static bool widthsFit(const GCNSubtarget &STI, const CombineInfo &CI,
194480093f4SDimitry Andric const CombineInfo &Paired);
195480093f4SDimitry Andric static unsigned getNewOpcode(const CombineInfo &CI, const CombineInfo &Paired);
196480093f4SDimitry Andric static std::pair<unsigned, unsigned> getSubRegIdxs(const CombineInfo &CI,
197480093f4SDimitry Andric const CombineInfo &Paired);
198480093f4SDimitry Andric const TargetRegisterClass *getTargetRegisterClass(const CombineInfo &CI,
199480093f4SDimitry Andric const CombineInfo &Paired);
200*5f7ddb14SDimitry Andric const TargetRegisterClass *getDataRegClass(const MachineInstr &MI) const;
2010b57cec5SDimitry Andric
2025ffd83dbSDimitry Andric bool checkAndPrepareMerge(CombineInfo &CI, CombineInfo &Paired,
2035ffd83dbSDimitry Andric SmallVectorImpl<MachineInstr *> &InstsToMove);
2040b57cec5SDimitry Andric
2050b57cec5SDimitry Andric unsigned read2Opcode(unsigned EltSize) const;
2060b57cec5SDimitry Andric unsigned read2ST64Opcode(unsigned EltSize) const;
2075ffd83dbSDimitry Andric MachineBasicBlock::iterator mergeRead2Pair(CombineInfo &CI,
2085ffd83dbSDimitry Andric CombineInfo &Paired,
2095ffd83dbSDimitry Andric const SmallVectorImpl<MachineInstr *> &InstsToMove);
2100b57cec5SDimitry Andric
2110b57cec5SDimitry Andric unsigned write2Opcode(unsigned EltSize) const;
2120b57cec5SDimitry Andric unsigned write2ST64Opcode(unsigned EltSize) const;
2135ffd83dbSDimitry Andric MachineBasicBlock::iterator
2145ffd83dbSDimitry Andric mergeWrite2Pair(CombineInfo &CI, CombineInfo &Paired,
2155ffd83dbSDimitry Andric const SmallVectorImpl<MachineInstr *> &InstsToMove);
2165ffd83dbSDimitry Andric MachineBasicBlock::iterator
2175ffd83dbSDimitry Andric mergeImagePair(CombineInfo &CI, CombineInfo &Paired,
2185ffd83dbSDimitry Andric const SmallVectorImpl<MachineInstr *> &InstsToMove);
2195ffd83dbSDimitry Andric MachineBasicBlock::iterator
2205ffd83dbSDimitry Andric mergeSBufferLoadImmPair(CombineInfo &CI, CombineInfo &Paired,
2215ffd83dbSDimitry Andric const SmallVectorImpl<MachineInstr *> &InstsToMove);
2225ffd83dbSDimitry Andric MachineBasicBlock::iterator
2235ffd83dbSDimitry Andric mergeBufferLoadPair(CombineInfo &CI, CombineInfo &Paired,
2245ffd83dbSDimitry Andric const SmallVectorImpl<MachineInstr *> &InstsToMove);
2255ffd83dbSDimitry Andric MachineBasicBlock::iterator
2265ffd83dbSDimitry Andric mergeBufferStorePair(CombineInfo &CI, CombineInfo &Paired,
2275ffd83dbSDimitry Andric const SmallVectorImpl<MachineInstr *> &InstsToMove);
2285ffd83dbSDimitry Andric MachineBasicBlock::iterator
2295ffd83dbSDimitry Andric mergeTBufferLoadPair(CombineInfo &CI, CombineInfo &Paired,
2305ffd83dbSDimitry Andric const SmallVectorImpl<MachineInstr *> &InstsToMove);
2315ffd83dbSDimitry Andric MachineBasicBlock::iterator
2325ffd83dbSDimitry Andric mergeTBufferStorePair(CombineInfo &CI, CombineInfo &Paired,
2335ffd83dbSDimitry Andric const SmallVectorImpl<MachineInstr *> &InstsToMove);
2340b57cec5SDimitry Andric
2355ffd83dbSDimitry Andric void updateBaseAndOffset(MachineInstr &I, Register NewBase,
2368bcb0991SDimitry Andric int32_t NewOffset) const;
2375ffd83dbSDimitry Andric Register computeBase(MachineInstr &MI, const MemAddress &Addr) const;
2388bcb0991SDimitry Andric MachineOperand createRegOrImm(int32_t Val, MachineInstr &MI) const;
2398bcb0991SDimitry Andric Optional<int32_t> extractConstOffset(const MachineOperand &Op) const;
2408bcb0991SDimitry Andric void processBaseWithConstOffset(const MachineOperand &Base, MemAddress &Addr) const;
2410b57cec5SDimitry Andric /// Promotes constant offset to the immediate by adjusting the base. It
2420b57cec5SDimitry Andric /// tries to use a base from the nearby instructions that allows it to have
2430b57cec5SDimitry Andric /// a 13bit constant offset which gets promoted to the immediate.
2440b57cec5SDimitry Andric bool promoteConstantOffsetToImm(MachineInstr &CI,
2450b57cec5SDimitry Andric MemInfoMap &Visited,
2468bcb0991SDimitry Andric SmallPtrSet<MachineInstr *, 4> &Promoted) const;
2478bcb0991SDimitry Andric void addInstToMergeableList(const CombineInfo &CI,
2488bcb0991SDimitry Andric std::list<std::list<CombineInfo> > &MergeableInsts) const;
2495ffd83dbSDimitry Andric
2505ffd83dbSDimitry Andric std::pair<MachineBasicBlock::iterator, bool> collectMergeableInsts(
2515ffd83dbSDimitry Andric MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End,
2525ffd83dbSDimitry Andric MemInfoMap &Visited, SmallPtrSet<MachineInstr *, 4> &AnchorList,
2538bcb0991SDimitry Andric std::list<std::list<CombineInfo>> &MergeableInsts) const;
2540b57cec5SDimitry Andric
2550b57cec5SDimitry Andric public:
2560b57cec5SDimitry Andric static char ID;
2570b57cec5SDimitry Andric
SILoadStoreOptimizer()2580b57cec5SDimitry Andric SILoadStoreOptimizer() : MachineFunctionPass(ID) {
2590b57cec5SDimitry Andric initializeSILoadStoreOptimizerPass(*PassRegistry::getPassRegistry());
2600b57cec5SDimitry Andric }
2610b57cec5SDimitry Andric
2628bcb0991SDimitry Andric bool optimizeInstsWithSameBaseAddr(std::list<CombineInfo> &MergeList,
2638bcb0991SDimitry Andric bool &OptimizeListAgain);
2648bcb0991SDimitry Andric bool optimizeBlock(std::list<std::list<CombineInfo> > &MergeableInsts);
2650b57cec5SDimitry Andric
2660b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override;
2670b57cec5SDimitry Andric
getPassName() const2680b57cec5SDimitry Andric StringRef getPassName() const override { return "SI Load Store Optimizer"; }
2690b57cec5SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const2700b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
2710b57cec5SDimitry Andric AU.setPreservesCFG();
2720b57cec5SDimitry Andric AU.addRequired<AAResultsWrapperPass>();
2730b57cec5SDimitry Andric
2740b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
2750b57cec5SDimitry Andric }
2765ffd83dbSDimitry Andric
getRequiredProperties() const2775ffd83dbSDimitry Andric MachineFunctionProperties getRequiredProperties() const override {
2785ffd83dbSDimitry Andric return MachineFunctionProperties()
2795ffd83dbSDimitry Andric .set(MachineFunctionProperties::Property::IsSSA);
2805ffd83dbSDimitry Andric }
2810b57cec5SDimitry Andric };
2820b57cec5SDimitry Andric
getOpcodeWidth(const MachineInstr & MI,const SIInstrInfo & TII)2838bcb0991SDimitry Andric static unsigned getOpcodeWidth(const MachineInstr &MI, const SIInstrInfo &TII) {
2848bcb0991SDimitry Andric const unsigned Opc = MI.getOpcode();
2858bcb0991SDimitry Andric
2868bcb0991SDimitry Andric if (TII.isMUBUF(Opc)) {
2878bcb0991SDimitry Andric // FIXME: Handle d16 correctly
2888bcb0991SDimitry Andric return AMDGPU::getMUBUFElements(Opc);
2898bcb0991SDimitry Andric }
2908bcb0991SDimitry Andric if (TII.isMIMG(MI)) {
2918bcb0991SDimitry Andric uint64_t DMaskImm =
2928bcb0991SDimitry Andric TII.getNamedOperand(MI, AMDGPU::OpName::dmask)->getImm();
2938bcb0991SDimitry Andric return countPopulation(DMaskImm);
2948bcb0991SDimitry Andric }
295480093f4SDimitry Andric if (TII.isMTBUF(Opc)) {
296480093f4SDimitry Andric return AMDGPU::getMTBUFElements(Opc);
297480093f4SDimitry Andric }
2988bcb0991SDimitry Andric
2998bcb0991SDimitry Andric switch (Opc) {
3008bcb0991SDimitry Andric case AMDGPU::S_BUFFER_LOAD_DWORD_IMM:
3018bcb0991SDimitry Andric return 1;
3028bcb0991SDimitry Andric case AMDGPU::S_BUFFER_LOAD_DWORDX2_IMM:
3038bcb0991SDimitry Andric return 2;
3048bcb0991SDimitry Andric case AMDGPU::S_BUFFER_LOAD_DWORDX4_IMM:
3058bcb0991SDimitry Andric return 4;
306*5f7ddb14SDimitry Andric case AMDGPU::DS_READ_B32: LLVM_FALLTHROUGH;
307*5f7ddb14SDimitry Andric case AMDGPU::DS_READ_B32_gfx9: LLVM_FALLTHROUGH;
308*5f7ddb14SDimitry Andric case AMDGPU::DS_WRITE_B32: LLVM_FALLTHROUGH;
309*5f7ddb14SDimitry Andric case AMDGPU::DS_WRITE_B32_gfx9:
310*5f7ddb14SDimitry Andric return 1;
311*5f7ddb14SDimitry Andric case AMDGPU::DS_READ_B64: LLVM_FALLTHROUGH;
312*5f7ddb14SDimitry Andric case AMDGPU::DS_READ_B64_gfx9: LLVM_FALLTHROUGH;
313*5f7ddb14SDimitry Andric case AMDGPU::DS_WRITE_B64: LLVM_FALLTHROUGH;
314*5f7ddb14SDimitry Andric case AMDGPU::DS_WRITE_B64_gfx9:
315*5f7ddb14SDimitry Andric return 2;
3168bcb0991SDimitry Andric default:
3178bcb0991SDimitry Andric return 0;
3188bcb0991SDimitry Andric }
3198bcb0991SDimitry Andric }
3208bcb0991SDimitry Andric
3218bcb0991SDimitry Andric /// Maps instruction opcode to enum InstClassEnum.
getInstClass(unsigned Opc,const SIInstrInfo & TII)3228bcb0991SDimitry Andric static InstClassEnum getInstClass(unsigned Opc, const SIInstrInfo &TII) {
3238bcb0991SDimitry Andric switch (Opc) {
3248bcb0991SDimitry Andric default:
3258bcb0991SDimitry Andric if (TII.isMUBUF(Opc)) {
3268bcb0991SDimitry Andric switch (AMDGPU::getMUBUFBaseOpcode(Opc)) {
3278bcb0991SDimitry Andric default:
3288bcb0991SDimitry Andric return UNKNOWN;
3298bcb0991SDimitry Andric case AMDGPU::BUFFER_LOAD_DWORD_OFFEN:
3308bcb0991SDimitry Andric case AMDGPU::BUFFER_LOAD_DWORD_OFFEN_exact:
3318bcb0991SDimitry Andric case AMDGPU::BUFFER_LOAD_DWORD_OFFSET:
3328bcb0991SDimitry Andric case AMDGPU::BUFFER_LOAD_DWORD_OFFSET_exact:
3338bcb0991SDimitry Andric return BUFFER_LOAD;
3348bcb0991SDimitry Andric case AMDGPU::BUFFER_STORE_DWORD_OFFEN:
3358bcb0991SDimitry Andric case AMDGPU::BUFFER_STORE_DWORD_OFFEN_exact:
3368bcb0991SDimitry Andric case AMDGPU::BUFFER_STORE_DWORD_OFFSET:
3378bcb0991SDimitry Andric case AMDGPU::BUFFER_STORE_DWORD_OFFSET_exact:
3388bcb0991SDimitry Andric return BUFFER_STORE;
3398bcb0991SDimitry Andric }
3408bcb0991SDimitry Andric }
3418bcb0991SDimitry Andric if (TII.isMIMG(Opc)) {
3428bcb0991SDimitry Andric // Ignore instructions encoded without vaddr.
3435ffd83dbSDimitry Andric if (AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr) == -1 &&
3445ffd83dbSDimitry Andric AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr0) == -1)
3458bcb0991SDimitry Andric return UNKNOWN;
3468bcb0991SDimitry Andric // TODO: Support IMAGE_GET_RESINFO and IMAGE_GET_LOD.
347480093f4SDimitry Andric if (TII.get(Opc).mayStore() || !TII.get(Opc).mayLoad() ||
348480093f4SDimitry Andric TII.isGather4(Opc))
3498bcb0991SDimitry Andric return UNKNOWN;
3508bcb0991SDimitry Andric return MIMG;
3518bcb0991SDimitry Andric }
352480093f4SDimitry Andric if (TII.isMTBUF(Opc)) {
353480093f4SDimitry Andric switch (AMDGPU::getMTBUFBaseOpcode(Opc)) {
354480093f4SDimitry Andric default:
355480093f4SDimitry Andric return UNKNOWN;
356480093f4SDimitry Andric case AMDGPU::TBUFFER_LOAD_FORMAT_X_OFFEN:
357480093f4SDimitry Andric case AMDGPU::TBUFFER_LOAD_FORMAT_X_OFFEN_exact:
358480093f4SDimitry Andric case AMDGPU::TBUFFER_LOAD_FORMAT_X_OFFSET:
359480093f4SDimitry Andric case AMDGPU::TBUFFER_LOAD_FORMAT_X_OFFSET_exact:
360480093f4SDimitry Andric return TBUFFER_LOAD;
361480093f4SDimitry Andric case AMDGPU::TBUFFER_STORE_FORMAT_X_OFFEN:
362480093f4SDimitry Andric case AMDGPU::TBUFFER_STORE_FORMAT_X_OFFEN_exact:
363480093f4SDimitry Andric case AMDGPU::TBUFFER_STORE_FORMAT_X_OFFSET:
364480093f4SDimitry Andric case AMDGPU::TBUFFER_STORE_FORMAT_X_OFFSET_exact:
365480093f4SDimitry Andric return TBUFFER_STORE;
366480093f4SDimitry Andric }
367480093f4SDimitry Andric }
3688bcb0991SDimitry Andric return UNKNOWN;
3698bcb0991SDimitry Andric case AMDGPU::S_BUFFER_LOAD_DWORD_IMM:
3708bcb0991SDimitry Andric case AMDGPU::S_BUFFER_LOAD_DWORDX2_IMM:
3718bcb0991SDimitry Andric case AMDGPU::S_BUFFER_LOAD_DWORDX4_IMM:
3728bcb0991SDimitry Andric return S_BUFFER_LOAD_IMM;
3738bcb0991SDimitry Andric case AMDGPU::DS_READ_B32:
3748bcb0991SDimitry Andric case AMDGPU::DS_READ_B32_gfx9:
3758bcb0991SDimitry Andric case AMDGPU::DS_READ_B64:
3768bcb0991SDimitry Andric case AMDGPU::DS_READ_B64_gfx9:
3778bcb0991SDimitry Andric return DS_READ;
3788bcb0991SDimitry Andric case AMDGPU::DS_WRITE_B32:
3798bcb0991SDimitry Andric case AMDGPU::DS_WRITE_B32_gfx9:
3808bcb0991SDimitry Andric case AMDGPU::DS_WRITE_B64:
3818bcb0991SDimitry Andric case AMDGPU::DS_WRITE_B64_gfx9:
3828bcb0991SDimitry Andric return DS_WRITE;
383af732203SDimitry Andric case AMDGPU::IMAGE_BVH_INTERSECT_RAY_sa:
384af732203SDimitry Andric case AMDGPU::IMAGE_BVH64_INTERSECT_RAY_sa:
385af732203SDimitry Andric case AMDGPU::IMAGE_BVH_INTERSECT_RAY_a16_sa:
386af732203SDimitry Andric case AMDGPU::IMAGE_BVH64_INTERSECT_RAY_a16_sa:
387af732203SDimitry Andric case AMDGPU::IMAGE_BVH_INTERSECT_RAY_nsa:
388af732203SDimitry Andric case AMDGPU::IMAGE_BVH64_INTERSECT_RAY_nsa:
389af732203SDimitry Andric case AMDGPU::IMAGE_BVH_INTERSECT_RAY_a16_nsa:
390af732203SDimitry Andric case AMDGPU::IMAGE_BVH64_INTERSECT_RAY_a16_nsa:
391af732203SDimitry Andric return UNKNOWN;
3928bcb0991SDimitry Andric }
3938bcb0991SDimitry Andric }
3948bcb0991SDimitry Andric
3958bcb0991SDimitry Andric /// Determines instruction subclass from opcode. Only instructions
3968bcb0991SDimitry Andric /// of the same subclass can be merged together.
getInstSubclass(unsigned Opc,const SIInstrInfo & TII)3978bcb0991SDimitry Andric static unsigned getInstSubclass(unsigned Opc, const SIInstrInfo &TII) {
3988bcb0991SDimitry Andric switch (Opc) {
3998bcb0991SDimitry Andric default:
4008bcb0991SDimitry Andric if (TII.isMUBUF(Opc))
4018bcb0991SDimitry Andric return AMDGPU::getMUBUFBaseOpcode(Opc);
4028bcb0991SDimitry Andric if (TII.isMIMG(Opc)) {
4038bcb0991SDimitry Andric const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(Opc);
4048bcb0991SDimitry Andric assert(Info);
4058bcb0991SDimitry Andric return Info->BaseOpcode;
4068bcb0991SDimitry Andric }
407480093f4SDimitry Andric if (TII.isMTBUF(Opc))
408480093f4SDimitry Andric return AMDGPU::getMTBUFBaseOpcode(Opc);
4098bcb0991SDimitry Andric return -1;
4108bcb0991SDimitry Andric case AMDGPU::DS_READ_B32:
4118bcb0991SDimitry Andric case AMDGPU::DS_READ_B32_gfx9:
4128bcb0991SDimitry Andric case AMDGPU::DS_READ_B64:
4138bcb0991SDimitry Andric case AMDGPU::DS_READ_B64_gfx9:
4148bcb0991SDimitry Andric case AMDGPU::DS_WRITE_B32:
4158bcb0991SDimitry Andric case AMDGPU::DS_WRITE_B32_gfx9:
4168bcb0991SDimitry Andric case AMDGPU::DS_WRITE_B64:
4178bcb0991SDimitry Andric case AMDGPU::DS_WRITE_B64_gfx9:
4188bcb0991SDimitry Andric return Opc;
4198bcb0991SDimitry Andric case AMDGPU::S_BUFFER_LOAD_DWORD_IMM:
4208bcb0991SDimitry Andric case AMDGPU::S_BUFFER_LOAD_DWORDX2_IMM:
4218bcb0991SDimitry Andric case AMDGPU::S_BUFFER_LOAD_DWORDX4_IMM:
4228bcb0991SDimitry Andric return AMDGPU::S_BUFFER_LOAD_DWORD_IMM;
4238bcb0991SDimitry Andric }
4248bcb0991SDimitry Andric }
4258bcb0991SDimitry Andric
getRegs(unsigned Opc,const SIInstrInfo & TII)4265ffd83dbSDimitry Andric static AddressRegs getRegs(unsigned Opc, const SIInstrInfo &TII) {
4275ffd83dbSDimitry Andric AddressRegs Result;
4285ffd83dbSDimitry Andric
4298bcb0991SDimitry Andric if (TII.isMUBUF(Opc)) {
4305ffd83dbSDimitry Andric if (AMDGPU::getMUBUFHasVAddr(Opc))
4315ffd83dbSDimitry Andric Result.VAddr = true;
4325ffd83dbSDimitry Andric if (AMDGPU::getMUBUFHasSrsrc(Opc))
4335ffd83dbSDimitry Andric Result.SRsrc = true;
4345ffd83dbSDimitry Andric if (AMDGPU::getMUBUFHasSoffset(Opc))
4355ffd83dbSDimitry Andric Result.SOffset = true;
4368bcb0991SDimitry Andric
4375ffd83dbSDimitry Andric return Result;
4388bcb0991SDimitry Andric }
4398bcb0991SDimitry Andric
4408bcb0991SDimitry Andric if (TII.isMIMG(Opc)) {
4415ffd83dbSDimitry Andric int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr0);
4425ffd83dbSDimitry Andric if (VAddr0Idx >= 0) {
4435ffd83dbSDimitry Andric int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::srsrc);
4445ffd83dbSDimitry Andric Result.NumVAddrs = SRsrcIdx - VAddr0Idx;
4455ffd83dbSDimitry Andric } else {
4465ffd83dbSDimitry Andric Result.VAddr = true;
4475ffd83dbSDimitry Andric }
4485ffd83dbSDimitry Andric Result.SRsrc = true;
4498bcb0991SDimitry Andric const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(Opc);
4508bcb0991SDimitry Andric if (Info && AMDGPU::getMIMGBaseOpcodeInfo(Info->BaseOpcode)->Sampler)
4515ffd83dbSDimitry Andric Result.SSamp = true;
452480093f4SDimitry Andric
4535ffd83dbSDimitry Andric return Result;
454480093f4SDimitry Andric }
455480093f4SDimitry Andric if (TII.isMTBUF(Opc)) {
4565ffd83dbSDimitry Andric if (AMDGPU::getMTBUFHasVAddr(Opc))
4575ffd83dbSDimitry Andric Result.VAddr = true;
4585ffd83dbSDimitry Andric if (AMDGPU::getMTBUFHasSrsrc(Opc))
4595ffd83dbSDimitry Andric Result.SRsrc = true;
4605ffd83dbSDimitry Andric if (AMDGPU::getMTBUFHasSoffset(Opc))
4615ffd83dbSDimitry Andric Result.SOffset = true;
462480093f4SDimitry Andric
4635ffd83dbSDimitry Andric return Result;
4648bcb0991SDimitry Andric }
4658bcb0991SDimitry Andric
4668bcb0991SDimitry Andric switch (Opc) {
4678bcb0991SDimitry Andric default:
4685ffd83dbSDimitry Andric return Result;
4698bcb0991SDimitry Andric case AMDGPU::S_BUFFER_LOAD_DWORD_IMM:
4708bcb0991SDimitry Andric case AMDGPU::S_BUFFER_LOAD_DWORDX2_IMM:
4718bcb0991SDimitry Andric case AMDGPU::S_BUFFER_LOAD_DWORDX4_IMM:
4725ffd83dbSDimitry Andric Result.SBase = true;
4735ffd83dbSDimitry Andric return Result;
4748bcb0991SDimitry Andric case AMDGPU::DS_READ_B32:
4758bcb0991SDimitry Andric case AMDGPU::DS_READ_B64:
4768bcb0991SDimitry Andric case AMDGPU::DS_READ_B32_gfx9:
4778bcb0991SDimitry Andric case AMDGPU::DS_READ_B64_gfx9:
4788bcb0991SDimitry Andric case AMDGPU::DS_WRITE_B32:
4798bcb0991SDimitry Andric case AMDGPU::DS_WRITE_B64:
4808bcb0991SDimitry Andric case AMDGPU::DS_WRITE_B32_gfx9:
4818bcb0991SDimitry Andric case AMDGPU::DS_WRITE_B64_gfx9:
4825ffd83dbSDimitry Andric Result.Addr = true;
4835ffd83dbSDimitry Andric return Result;
4848bcb0991SDimitry Andric }
4858bcb0991SDimitry Andric }
4868bcb0991SDimitry Andric
setMI(MachineBasicBlock::iterator MI,const SIInstrInfo & TII,const GCNSubtarget & STM)4878bcb0991SDimitry Andric void SILoadStoreOptimizer::CombineInfo::setMI(MachineBasicBlock::iterator MI,
4888bcb0991SDimitry Andric const SIInstrInfo &TII,
4898bcb0991SDimitry Andric const GCNSubtarget &STM) {
4908bcb0991SDimitry Andric I = MI;
4918bcb0991SDimitry Andric unsigned Opc = MI->getOpcode();
4928bcb0991SDimitry Andric InstClass = getInstClass(Opc, TII);
4938bcb0991SDimitry Andric
4948bcb0991SDimitry Andric if (InstClass == UNKNOWN)
4958bcb0991SDimitry Andric return;
4968bcb0991SDimitry Andric
4978bcb0991SDimitry Andric switch (InstClass) {
4988bcb0991SDimitry Andric case DS_READ:
4998bcb0991SDimitry Andric EltSize =
5008bcb0991SDimitry Andric (Opc == AMDGPU::DS_READ_B64 || Opc == AMDGPU::DS_READ_B64_gfx9) ? 8
5018bcb0991SDimitry Andric : 4;
5028bcb0991SDimitry Andric break;
5038bcb0991SDimitry Andric case DS_WRITE:
5048bcb0991SDimitry Andric EltSize =
5058bcb0991SDimitry Andric (Opc == AMDGPU::DS_WRITE_B64 || Opc == AMDGPU::DS_WRITE_B64_gfx9) ? 8
5068bcb0991SDimitry Andric : 4;
5078bcb0991SDimitry Andric break;
5088bcb0991SDimitry Andric case S_BUFFER_LOAD_IMM:
5095ffd83dbSDimitry Andric EltSize = AMDGPU::convertSMRDOffsetUnits(STM, 4);
5108bcb0991SDimitry Andric break;
5118bcb0991SDimitry Andric default:
5128bcb0991SDimitry Andric EltSize = 4;
5138bcb0991SDimitry Andric break;
5148bcb0991SDimitry Andric }
5158bcb0991SDimitry Andric
5168bcb0991SDimitry Andric if (InstClass == MIMG) {
517480093f4SDimitry Andric DMask = TII.getNamedOperand(*I, AMDGPU::OpName::dmask)->getImm();
5185ffd83dbSDimitry Andric // Offset is not considered for MIMG instructions.
5195ffd83dbSDimitry Andric Offset = 0;
5208bcb0991SDimitry Andric } else {
5218bcb0991SDimitry Andric int OffsetIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::offset);
522480093f4SDimitry Andric Offset = I->getOperand(OffsetIdx).getImm();
5238bcb0991SDimitry Andric }
5248bcb0991SDimitry Andric
525480093f4SDimitry Andric if (InstClass == TBUFFER_LOAD || InstClass == TBUFFER_STORE)
526480093f4SDimitry Andric Format = TII.getNamedOperand(*I, AMDGPU::OpName::format)->getImm();
527480093f4SDimitry Andric
528480093f4SDimitry Andric Width = getOpcodeWidth(*I, TII);
5298bcb0991SDimitry Andric
5308bcb0991SDimitry Andric if ((InstClass == DS_READ) || (InstClass == DS_WRITE)) {
531480093f4SDimitry Andric Offset &= 0xffff;
5328bcb0991SDimitry Andric } else if (InstClass != MIMG) {
533*5f7ddb14SDimitry Andric CPol = TII.getNamedOperand(*I, AMDGPU::OpName::cpol)->getImm();
5348bcb0991SDimitry Andric }
5358bcb0991SDimitry Andric
5365ffd83dbSDimitry Andric AddressRegs Regs = getRegs(Opc, TII);
5375ffd83dbSDimitry Andric
5388bcb0991SDimitry Andric NumAddresses = 0;
5395ffd83dbSDimitry Andric for (unsigned J = 0; J < Regs.NumVAddrs; J++)
5405ffd83dbSDimitry Andric AddrIdx[NumAddresses++] =
5415ffd83dbSDimitry Andric AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr0) + J;
5425ffd83dbSDimitry Andric if (Regs.Addr)
5435ffd83dbSDimitry Andric AddrIdx[NumAddresses++] =
5445ffd83dbSDimitry Andric AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::addr);
5455ffd83dbSDimitry Andric if (Regs.SBase)
5465ffd83dbSDimitry Andric AddrIdx[NumAddresses++] =
5475ffd83dbSDimitry Andric AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::sbase);
5485ffd83dbSDimitry Andric if (Regs.SRsrc)
5495ffd83dbSDimitry Andric AddrIdx[NumAddresses++] =
5505ffd83dbSDimitry Andric AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::srsrc);
5515ffd83dbSDimitry Andric if (Regs.SOffset)
5525ffd83dbSDimitry Andric AddrIdx[NumAddresses++] =
5535ffd83dbSDimitry Andric AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::soffset);
5545ffd83dbSDimitry Andric if (Regs.VAddr)
5555ffd83dbSDimitry Andric AddrIdx[NumAddresses++] =
5565ffd83dbSDimitry Andric AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr);
5575ffd83dbSDimitry Andric if (Regs.SSamp)
5585ffd83dbSDimitry Andric AddrIdx[NumAddresses++] =
5595ffd83dbSDimitry Andric AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::ssamp);
5605ffd83dbSDimitry Andric assert(NumAddresses <= MaxAddressRegs);
5618bcb0991SDimitry Andric
5625ffd83dbSDimitry Andric for (unsigned J = 0; J < NumAddresses; J++)
5635ffd83dbSDimitry Andric AddrReg[J] = &I->getOperand(AddrIdx[J]);
5648bcb0991SDimitry Andric }
5658bcb0991SDimitry Andric
5660b57cec5SDimitry Andric } // end anonymous namespace.
5670b57cec5SDimitry Andric
5680b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(SILoadStoreOptimizer, DEBUG_TYPE,
5690b57cec5SDimitry Andric "SI Load Store Optimizer", false, false)
5700b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
5710b57cec5SDimitry Andric INITIALIZE_PASS_END(SILoadStoreOptimizer, DEBUG_TYPE, "SI Load Store Optimizer",
5720b57cec5SDimitry Andric false, false)
5730b57cec5SDimitry Andric
5740b57cec5SDimitry Andric char SILoadStoreOptimizer::ID = 0;
5750b57cec5SDimitry Andric
5760b57cec5SDimitry Andric char &llvm::SILoadStoreOptimizerID = SILoadStoreOptimizer::ID;
5770b57cec5SDimitry Andric
createSILoadStoreOptimizerPass()5780b57cec5SDimitry Andric FunctionPass *llvm::createSILoadStoreOptimizerPass() {
5790b57cec5SDimitry Andric return new SILoadStoreOptimizer();
5800b57cec5SDimitry Andric }
5810b57cec5SDimitry Andric
moveInstsAfter(MachineBasicBlock::iterator I,ArrayRef<MachineInstr * > InstsToMove)5820b57cec5SDimitry Andric static void moveInstsAfter(MachineBasicBlock::iterator I,
5830b57cec5SDimitry Andric ArrayRef<MachineInstr *> InstsToMove) {
5840b57cec5SDimitry Andric MachineBasicBlock *MBB = I->getParent();
5850b57cec5SDimitry Andric ++I;
5860b57cec5SDimitry Andric for (MachineInstr *MI : InstsToMove) {
5870b57cec5SDimitry Andric MI->removeFromParent();
5880b57cec5SDimitry Andric MBB->insert(I, MI);
5890b57cec5SDimitry Andric }
5900b57cec5SDimitry Andric }
5910b57cec5SDimitry Andric
addDefsUsesToList(const MachineInstr & MI,DenseSet<Register> & RegDefs,DenseSet<Register> & PhysRegUses)5920b57cec5SDimitry Andric static void addDefsUsesToList(const MachineInstr &MI,
5935ffd83dbSDimitry Andric DenseSet<Register> &RegDefs,
5945ffd83dbSDimitry Andric DenseSet<Register> &PhysRegUses) {
5950b57cec5SDimitry Andric for (const MachineOperand &Op : MI.operands()) {
5960b57cec5SDimitry Andric if (Op.isReg()) {
5970b57cec5SDimitry Andric if (Op.isDef())
5980b57cec5SDimitry Andric RegDefs.insert(Op.getReg());
599af732203SDimitry Andric else if (Op.readsReg() && Op.getReg().isPhysical())
6000b57cec5SDimitry Andric PhysRegUses.insert(Op.getReg());
6010b57cec5SDimitry Andric }
6020b57cec5SDimitry Andric }
6030b57cec5SDimitry Andric }
6040b57cec5SDimitry Andric
memAccessesCanBeReordered(MachineBasicBlock::iterator A,MachineBasicBlock::iterator B,AliasAnalysis * AA)6050b57cec5SDimitry Andric static bool memAccessesCanBeReordered(MachineBasicBlock::iterator A,
6060b57cec5SDimitry Andric MachineBasicBlock::iterator B,
6070b57cec5SDimitry Andric AliasAnalysis *AA) {
6080b57cec5SDimitry Andric // RAW or WAR - cannot reorder
6090b57cec5SDimitry Andric // WAW - cannot reorder
6100b57cec5SDimitry Andric // RAR - safe to reorder
6110b57cec5SDimitry Andric return !(A->mayStore() || B->mayStore()) || !A->mayAlias(AA, *B, true);
6120b57cec5SDimitry Andric }
6130b57cec5SDimitry Andric
6140b57cec5SDimitry Andric // Add MI and its defs to the lists if MI reads one of the defs that are
6150b57cec5SDimitry Andric // already in the list. Returns true in that case.
addToListsIfDependent(MachineInstr & MI,DenseSet<Register> & RegDefs,DenseSet<Register> & PhysRegUses,SmallVectorImpl<MachineInstr * > & Insts)6165ffd83dbSDimitry Andric static bool addToListsIfDependent(MachineInstr &MI, DenseSet<Register> &RegDefs,
6175ffd83dbSDimitry Andric DenseSet<Register> &PhysRegUses,
6180b57cec5SDimitry Andric SmallVectorImpl<MachineInstr *> &Insts) {
6190b57cec5SDimitry Andric for (MachineOperand &Use : MI.operands()) {
6200b57cec5SDimitry Andric // If one of the defs is read, then there is a use of Def between I and the
6210b57cec5SDimitry Andric // instruction that I will potentially be merged with. We will need to move
6220b57cec5SDimitry Andric // this instruction after the merged instructions.
6230b57cec5SDimitry Andric //
6240b57cec5SDimitry Andric // Similarly, if there is a def which is read by an instruction that is to
6250b57cec5SDimitry Andric // be moved for merging, then we need to move the def-instruction as well.
6260b57cec5SDimitry Andric // This can only happen for physical registers such as M0; virtual
6270b57cec5SDimitry Andric // registers are in SSA form.
628af732203SDimitry Andric if (Use.isReg() && ((Use.readsReg() && RegDefs.count(Use.getReg())) ||
6290b57cec5SDimitry Andric (Use.isDef() && RegDefs.count(Use.getReg())) ||
630af732203SDimitry Andric (Use.isDef() && Use.getReg().isPhysical() &&
6310b57cec5SDimitry Andric PhysRegUses.count(Use.getReg())))) {
6320b57cec5SDimitry Andric Insts.push_back(&MI);
6330b57cec5SDimitry Andric addDefsUsesToList(MI, RegDefs, PhysRegUses);
6340b57cec5SDimitry Andric return true;
6350b57cec5SDimitry Andric }
6360b57cec5SDimitry Andric }
6370b57cec5SDimitry Andric
6380b57cec5SDimitry Andric return false;
6390b57cec5SDimitry Andric }
6400b57cec5SDimitry Andric
canMoveInstsAcrossMemOp(MachineInstr & MemOp,ArrayRef<MachineInstr * > InstsToMove,AliasAnalysis * AA)6410b57cec5SDimitry Andric static bool canMoveInstsAcrossMemOp(MachineInstr &MemOp,
6420b57cec5SDimitry Andric ArrayRef<MachineInstr *> InstsToMove,
6430b57cec5SDimitry Andric AliasAnalysis *AA) {
6440b57cec5SDimitry Andric assert(MemOp.mayLoadOrStore());
6450b57cec5SDimitry Andric
6460b57cec5SDimitry Andric for (MachineInstr *InstToMove : InstsToMove) {
6470b57cec5SDimitry Andric if (!InstToMove->mayLoadOrStore())
6480b57cec5SDimitry Andric continue;
6490b57cec5SDimitry Andric if (!memAccessesCanBeReordered(MemOp, *InstToMove, AA))
6500b57cec5SDimitry Andric return false;
6510b57cec5SDimitry Andric }
6520b57cec5SDimitry Andric return true;
6530b57cec5SDimitry Andric }
6540b57cec5SDimitry Andric
6558bcb0991SDimitry Andric // This function assumes that \p A and \p B have are identical except for
6568bcb0991SDimitry Andric // size and offset, and they referecne adjacent memory.
combineKnownAdjacentMMOs(MachineFunction & MF,const MachineMemOperand * A,const MachineMemOperand * B)6578bcb0991SDimitry Andric static MachineMemOperand *combineKnownAdjacentMMOs(MachineFunction &MF,
6588bcb0991SDimitry Andric const MachineMemOperand *A,
6598bcb0991SDimitry Andric const MachineMemOperand *B) {
6608bcb0991SDimitry Andric unsigned MinOffset = std::min(A->getOffset(), B->getOffset());
6618bcb0991SDimitry Andric unsigned Size = A->getSize() + B->getSize();
6628bcb0991SDimitry Andric // This function adds the offset parameter to the existing offset for A,
6638bcb0991SDimitry Andric // so we pass 0 here as the offset and then manually set it to the correct
6648bcb0991SDimitry Andric // value after the call.
6658bcb0991SDimitry Andric MachineMemOperand *MMO = MF.getMachineMemOperand(A, 0, Size);
6668bcb0991SDimitry Andric MMO->setOffset(MinOffset);
6678bcb0991SDimitry Andric return MMO;
6688bcb0991SDimitry Andric }
6698bcb0991SDimitry Andric
dmasksCanBeCombined(const CombineInfo & CI,const SIInstrInfo & TII,const CombineInfo & Paired)670480093f4SDimitry Andric bool SILoadStoreOptimizer::dmasksCanBeCombined(const CombineInfo &CI,
671480093f4SDimitry Andric const SIInstrInfo &TII,
672480093f4SDimitry Andric const CombineInfo &Paired) {
6738bcb0991SDimitry Andric assert(CI.InstClass == MIMG);
6748bcb0991SDimitry Andric
6758bcb0991SDimitry Andric // Ignore instructions with tfe/lwe set.
6768bcb0991SDimitry Andric const auto *TFEOp = TII.getNamedOperand(*CI.I, AMDGPU::OpName::tfe);
6778bcb0991SDimitry Andric const auto *LWEOp = TII.getNamedOperand(*CI.I, AMDGPU::OpName::lwe);
6788bcb0991SDimitry Andric
6798bcb0991SDimitry Andric if ((TFEOp && TFEOp->getImm()) || (LWEOp && LWEOp->getImm()))
6808bcb0991SDimitry Andric return false;
6818bcb0991SDimitry Andric
6828bcb0991SDimitry Andric // Check other optional immediate operands for equality.
683*5f7ddb14SDimitry Andric unsigned OperandsToMatch[] = {AMDGPU::OpName::cpol, AMDGPU::OpName::d16,
684*5f7ddb14SDimitry Andric AMDGPU::OpName::unorm, AMDGPU::OpName::da,
685*5f7ddb14SDimitry Andric AMDGPU::OpName::r128, AMDGPU::OpName::a16};
6868bcb0991SDimitry Andric
6878bcb0991SDimitry Andric for (auto op : OperandsToMatch) {
6888bcb0991SDimitry Andric int Idx = AMDGPU::getNamedOperandIdx(CI.I->getOpcode(), op);
689480093f4SDimitry Andric if (AMDGPU::getNamedOperandIdx(Paired.I->getOpcode(), op) != Idx)
6908bcb0991SDimitry Andric return false;
6918bcb0991SDimitry Andric if (Idx != -1 &&
692480093f4SDimitry Andric CI.I->getOperand(Idx).getImm() != Paired.I->getOperand(Idx).getImm())
6938bcb0991SDimitry Andric return false;
6948bcb0991SDimitry Andric }
6958bcb0991SDimitry Andric
6968bcb0991SDimitry Andric // Check DMask for overlaps.
697480093f4SDimitry Andric unsigned MaxMask = std::max(CI.DMask, Paired.DMask);
698480093f4SDimitry Andric unsigned MinMask = std::min(CI.DMask, Paired.DMask);
6998bcb0991SDimitry Andric
7008bcb0991SDimitry Andric unsigned AllowedBitsForMin = llvm::countTrailingZeros(MaxMask);
7018bcb0991SDimitry Andric if ((1u << AllowedBitsForMin) <= MinMask)
7028bcb0991SDimitry Andric return false;
7038bcb0991SDimitry Andric
7048bcb0991SDimitry Andric return true;
7058bcb0991SDimitry Andric }
7068bcb0991SDimitry Andric
getBufferFormatWithCompCount(unsigned OldFormat,unsigned ComponentCount,const GCNSubtarget & STI)707480093f4SDimitry Andric static unsigned getBufferFormatWithCompCount(unsigned OldFormat,
708480093f4SDimitry Andric unsigned ComponentCount,
7095ffd83dbSDimitry Andric const GCNSubtarget &STI) {
710480093f4SDimitry Andric if (ComponentCount > 4)
711480093f4SDimitry Andric return 0;
712480093f4SDimitry Andric
713480093f4SDimitry Andric const llvm::AMDGPU::GcnBufferFormatInfo *OldFormatInfo =
714480093f4SDimitry Andric llvm::AMDGPU::getGcnBufferFormatInfo(OldFormat, STI);
715480093f4SDimitry Andric if (!OldFormatInfo)
716480093f4SDimitry Andric return 0;
717480093f4SDimitry Andric
718480093f4SDimitry Andric const llvm::AMDGPU::GcnBufferFormatInfo *NewFormatInfo =
719480093f4SDimitry Andric llvm::AMDGPU::getGcnBufferFormatInfo(OldFormatInfo->BitsPerComp,
720480093f4SDimitry Andric ComponentCount,
721480093f4SDimitry Andric OldFormatInfo->NumFormat, STI);
722480093f4SDimitry Andric
723480093f4SDimitry Andric if (!NewFormatInfo)
724480093f4SDimitry Andric return 0;
725480093f4SDimitry Andric
726480093f4SDimitry Andric assert(NewFormatInfo->NumFormat == OldFormatInfo->NumFormat &&
727480093f4SDimitry Andric NewFormatInfo->BitsPerComp == OldFormatInfo->BitsPerComp);
728480093f4SDimitry Andric
729480093f4SDimitry Andric return NewFormatInfo->Format;
730480093f4SDimitry Andric }
731480093f4SDimitry Andric
732*5f7ddb14SDimitry Andric // Return the value in the inclusive range [Lo,Hi] that is aligned to the
733*5f7ddb14SDimitry Andric // highest power of two. Note that the result is well defined for all inputs
734*5f7ddb14SDimitry Andric // including corner cases like:
735*5f7ddb14SDimitry Andric // - if Lo == Hi, return that value
736*5f7ddb14SDimitry Andric // - if Lo == 0, return 0 (even though the "- 1" below underflows
737*5f7ddb14SDimitry Andric // - if Lo > Hi, return 0 (as if the range wrapped around)
mostAlignedValueInRange(uint32_t Lo,uint32_t Hi)738*5f7ddb14SDimitry Andric static uint32_t mostAlignedValueInRange(uint32_t Lo, uint32_t Hi) {
739*5f7ddb14SDimitry Andric return Hi & maskLeadingOnes<uint32_t>(countLeadingZeros((Lo - 1) ^ Hi) + 1);
740*5f7ddb14SDimitry Andric }
741*5f7ddb14SDimitry Andric
offsetsCanBeCombined(CombineInfo & CI,const GCNSubtarget & STI,CombineInfo & Paired,bool Modify)742480093f4SDimitry Andric bool SILoadStoreOptimizer::offsetsCanBeCombined(CombineInfo &CI,
7435ffd83dbSDimitry Andric const GCNSubtarget &STI,
7445ffd83dbSDimitry Andric CombineInfo &Paired,
7455ffd83dbSDimitry Andric bool Modify) {
7468bcb0991SDimitry Andric assert(CI.InstClass != MIMG);
7478bcb0991SDimitry Andric
7480b57cec5SDimitry Andric // XXX - Would the same offset be OK? Is there any reason this would happen or
7490b57cec5SDimitry Andric // be useful?
750480093f4SDimitry Andric if (CI.Offset == Paired.Offset)
7510b57cec5SDimitry Andric return false;
7520b57cec5SDimitry Andric
7530b57cec5SDimitry Andric // This won't be valid if the offset isn't aligned.
754480093f4SDimitry Andric if ((CI.Offset % CI.EltSize != 0) || (Paired.Offset % CI.EltSize != 0))
7550b57cec5SDimitry Andric return false;
7560b57cec5SDimitry Andric
757480093f4SDimitry Andric if (CI.InstClass == TBUFFER_LOAD || CI.InstClass == TBUFFER_STORE) {
758480093f4SDimitry Andric
759480093f4SDimitry Andric const llvm::AMDGPU::GcnBufferFormatInfo *Info0 =
760480093f4SDimitry Andric llvm::AMDGPU::getGcnBufferFormatInfo(CI.Format, STI);
761480093f4SDimitry Andric if (!Info0)
762480093f4SDimitry Andric return false;
763480093f4SDimitry Andric const llvm::AMDGPU::GcnBufferFormatInfo *Info1 =
764480093f4SDimitry Andric llvm::AMDGPU::getGcnBufferFormatInfo(Paired.Format, STI);
765480093f4SDimitry Andric if (!Info1)
766480093f4SDimitry Andric return false;
767480093f4SDimitry Andric
768480093f4SDimitry Andric if (Info0->BitsPerComp != Info1->BitsPerComp ||
769480093f4SDimitry Andric Info0->NumFormat != Info1->NumFormat)
770480093f4SDimitry Andric return false;
771480093f4SDimitry Andric
772480093f4SDimitry Andric // TODO: Should be possible to support more formats, but if format loads
773480093f4SDimitry Andric // are not dword-aligned, the merged load might not be valid.
774480093f4SDimitry Andric if (Info0->BitsPerComp != 32)
775480093f4SDimitry Andric return false;
776480093f4SDimitry Andric
777480093f4SDimitry Andric if (getBufferFormatWithCompCount(CI.Format, CI.Width + Paired.Width, STI) == 0)
778480093f4SDimitry Andric return false;
779480093f4SDimitry Andric }
780480093f4SDimitry Andric
781*5f7ddb14SDimitry Andric uint32_t EltOffset0 = CI.Offset / CI.EltSize;
782*5f7ddb14SDimitry Andric uint32_t EltOffset1 = Paired.Offset / CI.EltSize;
7830b57cec5SDimitry Andric CI.UseST64 = false;
7840b57cec5SDimitry Andric CI.BaseOff = 0;
7850b57cec5SDimitry Andric
786*5f7ddb14SDimitry Andric // Handle all non-DS instructions.
7870b57cec5SDimitry Andric if ((CI.InstClass != DS_READ) && (CI.InstClass != DS_WRITE)) {
788480093f4SDimitry Andric return (EltOffset0 + CI.Width == EltOffset1 ||
789480093f4SDimitry Andric EltOffset1 + Paired.Width == EltOffset0) &&
790*5f7ddb14SDimitry Andric CI.CPol == Paired.CPol &&
791*5f7ddb14SDimitry Andric (CI.InstClass == S_BUFFER_LOAD_IMM || CI.CPol == Paired.CPol);
7920b57cec5SDimitry Andric }
7930b57cec5SDimitry Andric
7940b57cec5SDimitry Andric // If the offset in elements doesn't fit in 8-bits, we might be able to use
7950b57cec5SDimitry Andric // the stride 64 versions.
7960b57cec5SDimitry Andric if ((EltOffset0 % 64 == 0) && (EltOffset1 % 64) == 0 &&
7970b57cec5SDimitry Andric isUInt<8>(EltOffset0 / 64) && isUInt<8>(EltOffset1 / 64)) {
7985ffd83dbSDimitry Andric if (Modify) {
799480093f4SDimitry Andric CI.Offset = EltOffset0 / 64;
800480093f4SDimitry Andric Paired.Offset = EltOffset1 / 64;
8010b57cec5SDimitry Andric CI.UseST64 = true;
8025ffd83dbSDimitry Andric }
8030b57cec5SDimitry Andric return true;
8040b57cec5SDimitry Andric }
8050b57cec5SDimitry Andric
8060b57cec5SDimitry Andric // Check if the new offsets fit in the reduced 8-bit range.
8070b57cec5SDimitry Andric if (isUInt<8>(EltOffset0) && isUInt<8>(EltOffset1)) {
8085ffd83dbSDimitry Andric if (Modify) {
809480093f4SDimitry Andric CI.Offset = EltOffset0;
810480093f4SDimitry Andric Paired.Offset = EltOffset1;
8115ffd83dbSDimitry Andric }
8120b57cec5SDimitry Andric return true;
8130b57cec5SDimitry Andric }
8140b57cec5SDimitry Andric
8150b57cec5SDimitry Andric // Try to shift base address to decrease offsets.
816*5f7ddb14SDimitry Andric uint32_t Min = std::min(EltOffset0, EltOffset1);
817*5f7ddb14SDimitry Andric uint32_t Max = std::max(EltOffset0, EltOffset1);
8180b57cec5SDimitry Andric
819*5f7ddb14SDimitry Andric const uint32_t Mask = maskTrailingOnes<uint32_t>(8) * 64;
820*5f7ddb14SDimitry Andric if (((Max - Min) & ~Mask) == 0) {
8215ffd83dbSDimitry Andric if (Modify) {
822*5f7ddb14SDimitry Andric // From the range of values we could use for BaseOff, choose the one that
823*5f7ddb14SDimitry Andric // is aligned to the highest power of two, to maximise the chance that
824*5f7ddb14SDimitry Andric // the same offset can be reused for other load/store pairs.
825*5f7ddb14SDimitry Andric uint32_t BaseOff = mostAlignedValueInRange(Max - 0xff * 64, Min);
826*5f7ddb14SDimitry Andric // Copy the low bits of the offsets, so that when we adjust them by
827*5f7ddb14SDimitry Andric // subtracting BaseOff they will be multiples of 64.
828*5f7ddb14SDimitry Andric BaseOff |= Min & maskTrailingOnes<uint32_t>(6);
829*5f7ddb14SDimitry Andric CI.BaseOff = BaseOff * CI.EltSize;
830*5f7ddb14SDimitry Andric CI.Offset = (EltOffset0 - BaseOff) / 64;
831*5f7ddb14SDimitry Andric Paired.Offset = (EltOffset1 - BaseOff) / 64;
8320b57cec5SDimitry Andric CI.UseST64 = true;
8335ffd83dbSDimitry Andric }
8340b57cec5SDimitry Andric return true;
8350b57cec5SDimitry Andric }
8360b57cec5SDimitry Andric
837*5f7ddb14SDimitry Andric if (isUInt<8>(Max - Min)) {
8385ffd83dbSDimitry Andric if (Modify) {
839*5f7ddb14SDimitry Andric // From the range of values we could use for BaseOff, choose the one that
840*5f7ddb14SDimitry Andric // is aligned to the highest power of two, to maximise the chance that
841*5f7ddb14SDimitry Andric // the same offset can be reused for other load/store pairs.
842*5f7ddb14SDimitry Andric uint32_t BaseOff = mostAlignedValueInRange(Max - 0xff, Min);
843*5f7ddb14SDimitry Andric CI.BaseOff = BaseOff * CI.EltSize;
844*5f7ddb14SDimitry Andric CI.Offset = EltOffset0 - BaseOff;
845*5f7ddb14SDimitry Andric Paired.Offset = EltOffset1 - BaseOff;
8465ffd83dbSDimitry Andric }
8470b57cec5SDimitry Andric return true;
8480b57cec5SDimitry Andric }
8490b57cec5SDimitry Andric
8500b57cec5SDimitry Andric return false;
8510b57cec5SDimitry Andric }
8520b57cec5SDimitry Andric
widthsFit(const GCNSubtarget & STM,const CombineInfo & CI,const CombineInfo & Paired)8530b57cec5SDimitry Andric bool SILoadStoreOptimizer::widthsFit(const GCNSubtarget &STM,
854480093f4SDimitry Andric const CombineInfo &CI,
855480093f4SDimitry Andric const CombineInfo &Paired) {
856480093f4SDimitry Andric const unsigned Width = (CI.Width + Paired.Width);
8570b57cec5SDimitry Andric switch (CI.InstClass) {
8580b57cec5SDimitry Andric default:
8590b57cec5SDimitry Andric return (Width <= 4) && (STM.hasDwordx3LoadStores() || (Width != 3));
8600b57cec5SDimitry Andric case S_BUFFER_LOAD_IMM:
8610b57cec5SDimitry Andric switch (Width) {
8620b57cec5SDimitry Andric default:
8630b57cec5SDimitry Andric return false;
8640b57cec5SDimitry Andric case 2:
8650b57cec5SDimitry Andric case 4:
8660b57cec5SDimitry Andric return true;
8670b57cec5SDimitry Andric }
8680b57cec5SDimitry Andric }
8690b57cec5SDimitry Andric }
8700b57cec5SDimitry Andric
871*5f7ddb14SDimitry Andric const TargetRegisterClass *
getDataRegClass(const MachineInstr & MI) const872*5f7ddb14SDimitry Andric SILoadStoreOptimizer::getDataRegClass(const MachineInstr &MI) const {
873*5f7ddb14SDimitry Andric if (const auto *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst)) {
874*5f7ddb14SDimitry Andric return TRI->getRegClassForReg(*MRI, Dst->getReg());
875*5f7ddb14SDimitry Andric }
876*5f7ddb14SDimitry Andric if (const auto *Src = TII->getNamedOperand(MI, AMDGPU::OpName::vdata)) {
877*5f7ddb14SDimitry Andric return TRI->getRegClassForReg(*MRI, Src->getReg());
878*5f7ddb14SDimitry Andric }
879*5f7ddb14SDimitry Andric if (const auto *Src = TII->getNamedOperand(MI, AMDGPU::OpName::data0)) {
880*5f7ddb14SDimitry Andric return TRI->getRegClassForReg(*MRI, Src->getReg());
881*5f7ddb14SDimitry Andric }
882*5f7ddb14SDimitry Andric if (const auto *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::sdst)) {
883*5f7ddb14SDimitry Andric return TRI->getRegClassForReg(*MRI, Dst->getReg());
884*5f7ddb14SDimitry Andric }
885*5f7ddb14SDimitry Andric if (const auto *Src = TII->getNamedOperand(MI, AMDGPU::OpName::sdata)) {
886*5f7ddb14SDimitry Andric return TRI->getRegClassForReg(*MRI, Src->getReg());
887*5f7ddb14SDimitry Andric }
888*5f7ddb14SDimitry Andric return nullptr;
889*5f7ddb14SDimitry Andric }
890*5f7ddb14SDimitry Andric
8915ffd83dbSDimitry Andric /// This function assumes that CI comes before Paired in a basic block.
checkAndPrepareMerge(CombineInfo & CI,CombineInfo & Paired,SmallVectorImpl<MachineInstr * > & InstsToMove)8925ffd83dbSDimitry Andric bool SILoadStoreOptimizer::checkAndPrepareMerge(
8935ffd83dbSDimitry Andric CombineInfo &CI, CombineInfo &Paired,
8945ffd83dbSDimitry Andric SmallVectorImpl<MachineInstr *> &InstsToMove) {
8955ffd83dbSDimitry Andric
8965ffd83dbSDimitry Andric // Check both offsets (or masks for MIMG) can be combined and fit in the
8975ffd83dbSDimitry Andric // reduced range.
8985ffd83dbSDimitry Andric if (CI.InstClass == MIMG && !dmasksCanBeCombined(CI, *TII, Paired))
8995ffd83dbSDimitry Andric return false;
9005ffd83dbSDimitry Andric
9015ffd83dbSDimitry Andric if (CI.InstClass != MIMG &&
9025ffd83dbSDimitry Andric (!widthsFit(*STM, CI, Paired) || !offsetsCanBeCombined(CI, *STM, Paired)))
9035ffd83dbSDimitry Andric return false;
9040b57cec5SDimitry Andric
9050b57cec5SDimitry Andric const unsigned Opc = CI.I->getOpcode();
9068bcb0991SDimitry Andric const InstClassEnum InstClass = getInstClass(Opc, *TII);
9070b57cec5SDimitry Andric
9080b57cec5SDimitry Andric if (InstClass == UNKNOWN) {
9090b57cec5SDimitry Andric return false;
9100b57cec5SDimitry Andric }
9118bcb0991SDimitry Andric const unsigned InstSubclass = getInstSubclass(Opc, *TII);
9120b57cec5SDimitry Andric
9138bcb0991SDimitry Andric // Do not merge VMEM buffer instructions with "swizzled" bit set.
9148bcb0991SDimitry Andric int Swizzled =
9158bcb0991SDimitry Andric AMDGPU::getNamedOperandIdx(CI.I->getOpcode(), AMDGPU::OpName::swz);
9168bcb0991SDimitry Andric if (Swizzled != -1 && CI.I->getOperand(Swizzled).getImm())
9170b57cec5SDimitry Andric return false;
9180b57cec5SDimitry Andric
9195ffd83dbSDimitry Andric DenseSet<Register> RegDefsToMove;
9205ffd83dbSDimitry Andric DenseSet<Register> PhysRegUsesToMove;
9210b57cec5SDimitry Andric addDefsUsesToList(*CI.I, RegDefsToMove, PhysRegUsesToMove);
9220b57cec5SDimitry Andric
923*5f7ddb14SDimitry Andric const TargetRegisterClass *DataRC = getDataRegClass(*CI.I);
924*5f7ddb14SDimitry Andric bool IsAGPR = TRI->hasAGPRs(DataRC);
925*5f7ddb14SDimitry Andric
9265ffd83dbSDimitry Andric MachineBasicBlock::iterator E = std::next(Paired.I);
9275ffd83dbSDimitry Andric MachineBasicBlock::iterator MBBI = std::next(CI.I);
9285ffd83dbSDimitry Andric MachineBasicBlock::iterator MBBE = CI.I->getParent()->end();
9290b57cec5SDimitry Andric for (; MBBI != E; ++MBBI) {
9300b57cec5SDimitry Andric
9315ffd83dbSDimitry Andric if (MBBI == MBBE) {
9325ffd83dbSDimitry Andric // CombineInfo::Order is a hint on the instruction ordering within the
9335ffd83dbSDimitry Andric // basic block. This hint suggests that CI precedes Paired, which is
9345ffd83dbSDimitry Andric // true most of the time. However, moveInstsAfter() processing a
9355ffd83dbSDimitry Andric // previous list may have changed this order in a situation when it
9365ffd83dbSDimitry Andric // moves an instruction which exists in some other merge list.
9375ffd83dbSDimitry Andric // In this case it must be dependent.
9385ffd83dbSDimitry Andric return false;
9395ffd83dbSDimitry Andric }
9405ffd83dbSDimitry Andric
9418bcb0991SDimitry Andric if ((getInstClass(MBBI->getOpcode(), *TII) != InstClass) ||
9428bcb0991SDimitry Andric (getInstSubclass(MBBI->getOpcode(), *TII) != InstSubclass)) {
9438bcb0991SDimitry Andric // This is not a matching instruction, but we can keep looking as
9440b57cec5SDimitry Andric // long as one of these conditions are met:
9450b57cec5SDimitry Andric // 1. It is safe to move I down past MBBI.
9460b57cec5SDimitry Andric // 2. It is safe to move MBBI down past the instruction that I will
9470b57cec5SDimitry Andric // be merged into.
9480b57cec5SDimitry Andric
9490b57cec5SDimitry Andric if (MBBI->hasUnmodeledSideEffects()) {
9500b57cec5SDimitry Andric // We can't re-order this instruction with respect to other memory
9510b57cec5SDimitry Andric // operations, so we fail both conditions mentioned above.
9520b57cec5SDimitry Andric return false;
9530b57cec5SDimitry Andric }
9540b57cec5SDimitry Andric
9550b57cec5SDimitry Andric if (MBBI->mayLoadOrStore() &&
9560b57cec5SDimitry Andric (!memAccessesCanBeReordered(*CI.I, *MBBI, AA) ||
9575ffd83dbSDimitry Andric !canMoveInstsAcrossMemOp(*MBBI, InstsToMove, AA))) {
9580b57cec5SDimitry Andric // We fail condition #1, but we may still be able to satisfy condition
9590b57cec5SDimitry Andric // #2. Add this instruction to the move list and then we will check
9600b57cec5SDimitry Andric // if condition #2 holds once we have selected the matching instruction.
9615ffd83dbSDimitry Andric InstsToMove.push_back(&*MBBI);
9620b57cec5SDimitry Andric addDefsUsesToList(*MBBI, RegDefsToMove, PhysRegUsesToMove);
9630b57cec5SDimitry Andric continue;
9640b57cec5SDimitry Andric }
9650b57cec5SDimitry Andric
9660b57cec5SDimitry Andric // When we match I with another DS instruction we will be moving I down
9670b57cec5SDimitry Andric // to the location of the matched instruction any uses of I will need to
9680b57cec5SDimitry Andric // be moved down as well.
9690b57cec5SDimitry Andric addToListsIfDependent(*MBBI, RegDefsToMove, PhysRegUsesToMove,
9705ffd83dbSDimitry Andric InstsToMove);
9710b57cec5SDimitry Andric continue;
9720b57cec5SDimitry Andric }
9730b57cec5SDimitry Andric
9740b57cec5SDimitry Andric // Don't merge volatiles.
9750b57cec5SDimitry Andric if (MBBI->hasOrderedMemoryRef())
9760b57cec5SDimitry Andric return false;
9770b57cec5SDimitry Andric
978480093f4SDimitry Andric int Swizzled =
979480093f4SDimitry Andric AMDGPU::getNamedOperandIdx(MBBI->getOpcode(), AMDGPU::OpName::swz);
980480093f4SDimitry Andric if (Swizzled != -1 && MBBI->getOperand(Swizzled).getImm())
981480093f4SDimitry Andric return false;
982480093f4SDimitry Andric
9830b57cec5SDimitry Andric // Handle a case like
9840b57cec5SDimitry Andric // DS_WRITE_B32 addr, v, idx0
9850b57cec5SDimitry Andric // w = DS_READ_B32 addr, idx0
9860b57cec5SDimitry Andric // DS_WRITE_B32 addr, f(w), idx1
9870b57cec5SDimitry Andric // where the DS_READ_B32 ends up in InstsToMove and therefore prevents
9880b57cec5SDimitry Andric // merging of the two writes.
9890b57cec5SDimitry Andric if (addToListsIfDependent(*MBBI, RegDefsToMove, PhysRegUsesToMove,
9905ffd83dbSDimitry Andric InstsToMove))
9910b57cec5SDimitry Andric continue;
9920b57cec5SDimitry Andric
9935ffd83dbSDimitry Andric if (&*MBBI == &*Paired.I) {
994*5f7ddb14SDimitry Andric if (TRI->hasAGPRs(getDataRegClass(*MBBI)) != IsAGPR)
995*5f7ddb14SDimitry Andric return false;
996*5f7ddb14SDimitry Andric // FIXME: nothing is illegal in a ds_write2 opcode with two AGPR data
997*5f7ddb14SDimitry Andric // operands. However we are reporting that ds_write2 shall have
998*5f7ddb14SDimitry Andric // only VGPR data so that machine copy propagation does not
999*5f7ddb14SDimitry Andric // create an illegal instruction with a VGPR and AGPR sources.
1000*5f7ddb14SDimitry Andric // Consequenctially if we create such instruction the verifier
1001*5f7ddb14SDimitry Andric // will complain.
1002*5f7ddb14SDimitry Andric if (IsAGPR && CI.InstClass == DS_WRITE)
1003*5f7ddb14SDimitry Andric return false;
1004*5f7ddb14SDimitry Andric
10055ffd83dbSDimitry Andric // We need to go through the list of instructions that we plan to
10060b57cec5SDimitry Andric // move and make sure they are all safe to move down past the merged
10070b57cec5SDimitry Andric // instruction.
10085ffd83dbSDimitry Andric if (canMoveInstsAcrossMemOp(*MBBI, InstsToMove, AA)) {
10095ffd83dbSDimitry Andric
10105ffd83dbSDimitry Andric // Call offsetsCanBeCombined with modify = true so that the offsets are
10115ffd83dbSDimitry Andric // correct for the new instruction. This should return true, because
10125ffd83dbSDimitry Andric // this function should only be called on CombineInfo objects that
10135ffd83dbSDimitry Andric // have already been confirmed to be mergeable.
10145ffd83dbSDimitry Andric if (CI.InstClass != MIMG)
10155ffd83dbSDimitry Andric offsetsCanBeCombined(CI, *STM, Paired, true);
10160b57cec5SDimitry Andric return true;
10170b57cec5SDimitry Andric }
10185ffd83dbSDimitry Andric return false;
10195ffd83dbSDimitry Andric }
10200b57cec5SDimitry Andric
10210b57cec5SDimitry Andric // We've found a load/store that we couldn't merge for some reason.
10220b57cec5SDimitry Andric // We could potentially keep looking, but we'd need to make sure that
10230b57cec5SDimitry Andric // it was safe to move I and also all the instruction in InstsToMove
10240b57cec5SDimitry Andric // down past this instruction.
10250b57cec5SDimitry Andric // check if we can move I across MBBI and if we can move all I's users
10260b57cec5SDimitry Andric if (!memAccessesCanBeReordered(*CI.I, *MBBI, AA) ||
10275ffd83dbSDimitry Andric !canMoveInstsAcrossMemOp(*MBBI, InstsToMove, AA))
10280b57cec5SDimitry Andric break;
10290b57cec5SDimitry Andric }
10300b57cec5SDimitry Andric return false;
10310b57cec5SDimitry Andric }
10320b57cec5SDimitry Andric
read2Opcode(unsigned EltSize) const10330b57cec5SDimitry Andric unsigned SILoadStoreOptimizer::read2Opcode(unsigned EltSize) const {
10340b57cec5SDimitry Andric if (STM->ldsRequiresM0Init())
10350b57cec5SDimitry Andric return (EltSize == 4) ? AMDGPU::DS_READ2_B32 : AMDGPU::DS_READ2_B64;
10360b57cec5SDimitry Andric return (EltSize == 4) ? AMDGPU::DS_READ2_B32_gfx9 : AMDGPU::DS_READ2_B64_gfx9;
10370b57cec5SDimitry Andric }
10380b57cec5SDimitry Andric
read2ST64Opcode(unsigned EltSize) const10390b57cec5SDimitry Andric unsigned SILoadStoreOptimizer::read2ST64Opcode(unsigned EltSize) const {
10400b57cec5SDimitry Andric if (STM->ldsRequiresM0Init())
10410b57cec5SDimitry Andric return (EltSize == 4) ? AMDGPU::DS_READ2ST64_B32 : AMDGPU::DS_READ2ST64_B64;
10420b57cec5SDimitry Andric
10430b57cec5SDimitry Andric return (EltSize == 4) ? AMDGPU::DS_READ2ST64_B32_gfx9
10440b57cec5SDimitry Andric : AMDGPU::DS_READ2ST64_B64_gfx9;
10450b57cec5SDimitry Andric }
10460b57cec5SDimitry Andric
10470b57cec5SDimitry Andric MachineBasicBlock::iterator
mergeRead2Pair(CombineInfo & CI,CombineInfo & Paired,const SmallVectorImpl<MachineInstr * > & InstsToMove)10485ffd83dbSDimitry Andric SILoadStoreOptimizer::mergeRead2Pair(CombineInfo &CI, CombineInfo &Paired,
10495ffd83dbSDimitry Andric const SmallVectorImpl<MachineInstr *> &InstsToMove) {
10500b57cec5SDimitry Andric MachineBasicBlock *MBB = CI.I->getParent();
10510b57cec5SDimitry Andric
10520b57cec5SDimitry Andric // Be careful, since the addresses could be subregisters themselves in weird
10530b57cec5SDimitry Andric // cases, like vectors of pointers.
10540b57cec5SDimitry Andric const auto *AddrReg = TII->getNamedOperand(*CI.I, AMDGPU::OpName::addr);
10550b57cec5SDimitry Andric
10560b57cec5SDimitry Andric const auto *Dest0 = TII->getNamedOperand(*CI.I, AMDGPU::OpName::vdst);
1057480093f4SDimitry Andric const auto *Dest1 = TII->getNamedOperand(*Paired.I, AMDGPU::OpName::vdst);
10580b57cec5SDimitry Andric
1059480093f4SDimitry Andric unsigned NewOffset0 = CI.Offset;
1060480093f4SDimitry Andric unsigned NewOffset1 = Paired.Offset;
10610b57cec5SDimitry Andric unsigned Opc =
10620b57cec5SDimitry Andric CI.UseST64 ? read2ST64Opcode(CI.EltSize) : read2Opcode(CI.EltSize);
10630b57cec5SDimitry Andric
10640b57cec5SDimitry Andric unsigned SubRegIdx0 = (CI.EltSize == 4) ? AMDGPU::sub0 : AMDGPU::sub0_sub1;
10650b57cec5SDimitry Andric unsigned SubRegIdx1 = (CI.EltSize == 4) ? AMDGPU::sub1 : AMDGPU::sub2_sub3;
10660b57cec5SDimitry Andric
10670b57cec5SDimitry Andric if (NewOffset0 > NewOffset1) {
10680b57cec5SDimitry Andric // Canonicalize the merged instruction so the smaller offset comes first.
10690b57cec5SDimitry Andric std::swap(NewOffset0, NewOffset1);
10700b57cec5SDimitry Andric std::swap(SubRegIdx0, SubRegIdx1);
10710b57cec5SDimitry Andric }
10720b57cec5SDimitry Andric
10730b57cec5SDimitry Andric assert((isUInt<8>(NewOffset0) && isUInt<8>(NewOffset1)) &&
10740b57cec5SDimitry Andric (NewOffset0 != NewOffset1) && "Computed offset doesn't fit");
10750b57cec5SDimitry Andric
10760b57cec5SDimitry Andric const MCInstrDesc &Read2Desc = TII->get(Opc);
10770b57cec5SDimitry Andric
1078*5f7ddb14SDimitry Andric const TargetRegisterClass *SuperRC = getTargetRegisterClass(CI, Paired);
10798bcb0991SDimitry Andric Register DestReg = MRI->createVirtualRegister(SuperRC);
10800b57cec5SDimitry Andric
10810b57cec5SDimitry Andric DebugLoc DL = CI.I->getDebugLoc();
10820b57cec5SDimitry Andric
10838bcb0991SDimitry Andric Register BaseReg = AddrReg->getReg();
10840b57cec5SDimitry Andric unsigned BaseSubReg = AddrReg->getSubReg();
10850b57cec5SDimitry Andric unsigned BaseRegFlags = 0;
10860b57cec5SDimitry Andric if (CI.BaseOff) {
10878bcb0991SDimitry Andric Register ImmReg = MRI->createVirtualRegister(&AMDGPU::SReg_32RegClass);
1088480093f4SDimitry Andric BuildMI(*MBB, Paired.I, DL, TII->get(AMDGPU::S_MOV_B32), ImmReg)
10890b57cec5SDimitry Andric .addImm(CI.BaseOff);
10900b57cec5SDimitry Andric
10910b57cec5SDimitry Andric BaseReg = MRI->createVirtualRegister(&AMDGPU::VGPR_32RegClass);
10920b57cec5SDimitry Andric BaseRegFlags = RegState::Kill;
10930b57cec5SDimitry Andric
1094480093f4SDimitry Andric TII->getAddNoCarry(*MBB, Paired.I, DL, BaseReg)
10950b57cec5SDimitry Andric .addReg(ImmReg)
10960b57cec5SDimitry Andric .addReg(AddrReg->getReg(), 0, BaseSubReg)
10970b57cec5SDimitry Andric .addImm(0); // clamp bit
10980b57cec5SDimitry Andric BaseSubReg = 0;
10990b57cec5SDimitry Andric }
11000b57cec5SDimitry Andric
11010b57cec5SDimitry Andric MachineInstrBuilder Read2 =
1102480093f4SDimitry Andric BuildMI(*MBB, Paired.I, DL, Read2Desc, DestReg)
11030b57cec5SDimitry Andric .addReg(BaseReg, BaseRegFlags, BaseSubReg) // addr
11040b57cec5SDimitry Andric .addImm(NewOffset0) // offset0
11050b57cec5SDimitry Andric .addImm(NewOffset1) // offset1
11060b57cec5SDimitry Andric .addImm(0) // gds
1107480093f4SDimitry Andric .cloneMergedMemRefs({&*CI.I, &*Paired.I});
11080b57cec5SDimitry Andric
11090b57cec5SDimitry Andric (void)Read2;
11100b57cec5SDimitry Andric
11110b57cec5SDimitry Andric const MCInstrDesc &CopyDesc = TII->get(TargetOpcode::COPY);
11120b57cec5SDimitry Andric
11130b57cec5SDimitry Andric // Copy to the old destination registers.
1114480093f4SDimitry Andric BuildMI(*MBB, Paired.I, DL, CopyDesc)
11150b57cec5SDimitry Andric .add(*Dest0) // Copy to same destination including flags and sub reg.
11160b57cec5SDimitry Andric .addReg(DestReg, 0, SubRegIdx0);
1117480093f4SDimitry Andric MachineInstr *Copy1 = BuildMI(*MBB, Paired.I, DL, CopyDesc)
11180b57cec5SDimitry Andric .add(*Dest1)
11190b57cec5SDimitry Andric .addReg(DestReg, RegState::Kill, SubRegIdx1);
11200b57cec5SDimitry Andric
11215ffd83dbSDimitry Andric moveInstsAfter(Copy1, InstsToMove);
11220b57cec5SDimitry Andric
11230b57cec5SDimitry Andric CI.I->eraseFromParent();
1124480093f4SDimitry Andric Paired.I->eraseFromParent();
11250b57cec5SDimitry Andric
11260b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Inserted read2: " << *Read2 << '\n');
11278bcb0991SDimitry Andric return Read2;
11280b57cec5SDimitry Andric }
11290b57cec5SDimitry Andric
write2Opcode(unsigned EltSize) const11300b57cec5SDimitry Andric unsigned SILoadStoreOptimizer::write2Opcode(unsigned EltSize) const {
11310b57cec5SDimitry Andric if (STM->ldsRequiresM0Init())
11320b57cec5SDimitry Andric return (EltSize == 4) ? AMDGPU::DS_WRITE2_B32 : AMDGPU::DS_WRITE2_B64;
11330b57cec5SDimitry Andric return (EltSize == 4) ? AMDGPU::DS_WRITE2_B32_gfx9
11340b57cec5SDimitry Andric : AMDGPU::DS_WRITE2_B64_gfx9;
11350b57cec5SDimitry Andric }
11360b57cec5SDimitry Andric
write2ST64Opcode(unsigned EltSize) const11370b57cec5SDimitry Andric unsigned SILoadStoreOptimizer::write2ST64Opcode(unsigned EltSize) const {
11380b57cec5SDimitry Andric if (STM->ldsRequiresM0Init())
11390b57cec5SDimitry Andric return (EltSize == 4) ? AMDGPU::DS_WRITE2ST64_B32
11400b57cec5SDimitry Andric : AMDGPU::DS_WRITE2ST64_B64;
11410b57cec5SDimitry Andric
11420b57cec5SDimitry Andric return (EltSize == 4) ? AMDGPU::DS_WRITE2ST64_B32_gfx9
11430b57cec5SDimitry Andric : AMDGPU::DS_WRITE2ST64_B64_gfx9;
11440b57cec5SDimitry Andric }
11450b57cec5SDimitry Andric
11460b57cec5SDimitry Andric MachineBasicBlock::iterator
mergeWrite2Pair(CombineInfo & CI,CombineInfo & Paired,const SmallVectorImpl<MachineInstr * > & InstsToMove)11475ffd83dbSDimitry Andric SILoadStoreOptimizer::mergeWrite2Pair(CombineInfo &CI, CombineInfo &Paired,
11485ffd83dbSDimitry Andric const SmallVectorImpl<MachineInstr *> &InstsToMove) {
11490b57cec5SDimitry Andric MachineBasicBlock *MBB = CI.I->getParent();
11500b57cec5SDimitry Andric
11510b57cec5SDimitry Andric // Be sure to use .addOperand(), and not .addReg() with these. We want to be
11520b57cec5SDimitry Andric // sure we preserve the subregister index and any register flags set on them.
11530b57cec5SDimitry Andric const MachineOperand *AddrReg =
11540b57cec5SDimitry Andric TII->getNamedOperand(*CI.I, AMDGPU::OpName::addr);
11550b57cec5SDimitry Andric const MachineOperand *Data0 =
11560b57cec5SDimitry Andric TII->getNamedOperand(*CI.I, AMDGPU::OpName::data0);
11570b57cec5SDimitry Andric const MachineOperand *Data1 =
1158480093f4SDimitry Andric TII->getNamedOperand(*Paired.I, AMDGPU::OpName::data0);
11590b57cec5SDimitry Andric
1160480093f4SDimitry Andric unsigned NewOffset0 = CI.Offset;
1161480093f4SDimitry Andric unsigned NewOffset1 = Paired.Offset;
11620b57cec5SDimitry Andric unsigned Opc =
11630b57cec5SDimitry Andric CI.UseST64 ? write2ST64Opcode(CI.EltSize) : write2Opcode(CI.EltSize);
11640b57cec5SDimitry Andric
11650b57cec5SDimitry Andric if (NewOffset0 > NewOffset1) {
11660b57cec5SDimitry Andric // Canonicalize the merged instruction so the smaller offset comes first.
11670b57cec5SDimitry Andric std::swap(NewOffset0, NewOffset1);
11680b57cec5SDimitry Andric std::swap(Data0, Data1);
11690b57cec5SDimitry Andric }
11700b57cec5SDimitry Andric
11710b57cec5SDimitry Andric assert((isUInt<8>(NewOffset0) && isUInt<8>(NewOffset1)) &&
11720b57cec5SDimitry Andric (NewOffset0 != NewOffset1) && "Computed offset doesn't fit");
11730b57cec5SDimitry Andric
11740b57cec5SDimitry Andric const MCInstrDesc &Write2Desc = TII->get(Opc);
11750b57cec5SDimitry Andric DebugLoc DL = CI.I->getDebugLoc();
11760b57cec5SDimitry Andric
11778bcb0991SDimitry Andric Register BaseReg = AddrReg->getReg();
11780b57cec5SDimitry Andric unsigned BaseSubReg = AddrReg->getSubReg();
11790b57cec5SDimitry Andric unsigned BaseRegFlags = 0;
11800b57cec5SDimitry Andric if (CI.BaseOff) {
11818bcb0991SDimitry Andric Register ImmReg = MRI->createVirtualRegister(&AMDGPU::SReg_32RegClass);
1182480093f4SDimitry Andric BuildMI(*MBB, Paired.I, DL, TII->get(AMDGPU::S_MOV_B32), ImmReg)
11830b57cec5SDimitry Andric .addImm(CI.BaseOff);
11840b57cec5SDimitry Andric
11850b57cec5SDimitry Andric BaseReg = MRI->createVirtualRegister(&AMDGPU::VGPR_32RegClass);
11860b57cec5SDimitry Andric BaseRegFlags = RegState::Kill;
11870b57cec5SDimitry Andric
1188480093f4SDimitry Andric TII->getAddNoCarry(*MBB, Paired.I, DL, BaseReg)
11890b57cec5SDimitry Andric .addReg(ImmReg)
11900b57cec5SDimitry Andric .addReg(AddrReg->getReg(), 0, BaseSubReg)
11910b57cec5SDimitry Andric .addImm(0); // clamp bit
11920b57cec5SDimitry Andric BaseSubReg = 0;
11930b57cec5SDimitry Andric }
11940b57cec5SDimitry Andric
11950b57cec5SDimitry Andric MachineInstrBuilder Write2 =
1196480093f4SDimitry Andric BuildMI(*MBB, Paired.I, DL, Write2Desc)
11970b57cec5SDimitry Andric .addReg(BaseReg, BaseRegFlags, BaseSubReg) // addr
11980b57cec5SDimitry Andric .add(*Data0) // data0
11990b57cec5SDimitry Andric .add(*Data1) // data1
12000b57cec5SDimitry Andric .addImm(NewOffset0) // offset0
12010b57cec5SDimitry Andric .addImm(NewOffset1) // offset1
12020b57cec5SDimitry Andric .addImm(0) // gds
1203480093f4SDimitry Andric .cloneMergedMemRefs({&*CI.I, &*Paired.I});
12040b57cec5SDimitry Andric
12055ffd83dbSDimitry Andric moveInstsAfter(Write2, InstsToMove);
12060b57cec5SDimitry Andric
12070b57cec5SDimitry Andric CI.I->eraseFromParent();
1208480093f4SDimitry Andric Paired.I->eraseFromParent();
12090b57cec5SDimitry Andric
12100b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Inserted write2 inst: " << *Write2 << '\n');
12118bcb0991SDimitry Andric return Write2;
12120b57cec5SDimitry Andric }
12130b57cec5SDimitry Andric
12140b57cec5SDimitry Andric MachineBasicBlock::iterator
mergeImagePair(CombineInfo & CI,CombineInfo & Paired,const SmallVectorImpl<MachineInstr * > & InstsToMove)12155ffd83dbSDimitry Andric SILoadStoreOptimizer::mergeImagePair(CombineInfo &CI, CombineInfo &Paired,
12165ffd83dbSDimitry Andric const SmallVectorImpl<MachineInstr *> &InstsToMove) {
12170b57cec5SDimitry Andric MachineBasicBlock *MBB = CI.I->getParent();
12180b57cec5SDimitry Andric DebugLoc DL = CI.I->getDebugLoc();
1219480093f4SDimitry Andric const unsigned Opcode = getNewOpcode(CI, Paired);
12200b57cec5SDimitry Andric
1221480093f4SDimitry Andric const TargetRegisterClass *SuperRC = getTargetRegisterClass(CI, Paired);
12220b57cec5SDimitry Andric
12238bcb0991SDimitry Andric Register DestReg = MRI->createVirtualRegister(SuperRC);
1224480093f4SDimitry Andric unsigned MergedDMask = CI.DMask | Paired.DMask;
12258bcb0991SDimitry Andric unsigned DMaskIdx =
12268bcb0991SDimitry Andric AMDGPU::getNamedOperandIdx(CI.I->getOpcode(), AMDGPU::OpName::dmask);
12270b57cec5SDimitry Andric
1228480093f4SDimitry Andric auto MIB = BuildMI(*MBB, Paired.I, DL, TII->get(Opcode), DestReg);
12298bcb0991SDimitry Andric for (unsigned I = 1, E = (*CI.I).getNumOperands(); I != E; ++I) {
12308bcb0991SDimitry Andric if (I == DMaskIdx)
12318bcb0991SDimitry Andric MIB.addImm(MergedDMask);
12328bcb0991SDimitry Andric else
12338bcb0991SDimitry Andric MIB.add((*CI.I).getOperand(I));
12348bcb0991SDimitry Andric }
12350b57cec5SDimitry Andric
12368bcb0991SDimitry Andric // It shouldn't be possible to get this far if the two instructions
12378bcb0991SDimitry Andric // don't have a single memoperand, because MachineInstr::mayAlias()
12388bcb0991SDimitry Andric // will return true if this is the case.
1239480093f4SDimitry Andric assert(CI.I->hasOneMemOperand() && Paired.I->hasOneMemOperand());
12400b57cec5SDimitry Andric
12418bcb0991SDimitry Andric const MachineMemOperand *MMOa = *CI.I->memoperands_begin();
1242480093f4SDimitry Andric const MachineMemOperand *MMOb = *Paired.I->memoperands_begin();
12430b57cec5SDimitry Andric
12448bcb0991SDimitry Andric MachineInstr *New = MIB.addMemOperand(combineKnownAdjacentMMOs(*MBB->getParent(), MMOa, MMOb));
12450b57cec5SDimitry Andric
1246480093f4SDimitry Andric unsigned SubRegIdx0, SubRegIdx1;
1247480093f4SDimitry Andric std::tie(SubRegIdx0, SubRegIdx1) = getSubRegIdxs(CI, Paired);
12480b57cec5SDimitry Andric
12490b57cec5SDimitry Andric // Copy to the old destination registers.
12500b57cec5SDimitry Andric const MCInstrDesc &CopyDesc = TII->get(TargetOpcode::COPY);
12510b57cec5SDimitry Andric const auto *Dest0 = TII->getNamedOperand(*CI.I, AMDGPU::OpName::vdata);
1252480093f4SDimitry Andric const auto *Dest1 = TII->getNamedOperand(*Paired.I, AMDGPU::OpName::vdata);
12530b57cec5SDimitry Andric
1254480093f4SDimitry Andric BuildMI(*MBB, Paired.I, DL, CopyDesc)
12550b57cec5SDimitry Andric .add(*Dest0) // Copy to same destination including flags and sub reg.
12560b57cec5SDimitry Andric .addReg(DestReg, 0, SubRegIdx0);
1257480093f4SDimitry Andric MachineInstr *Copy1 = BuildMI(*MBB, Paired.I, DL, CopyDesc)
12580b57cec5SDimitry Andric .add(*Dest1)
12590b57cec5SDimitry Andric .addReg(DestReg, RegState::Kill, SubRegIdx1);
12600b57cec5SDimitry Andric
12615ffd83dbSDimitry Andric moveInstsAfter(Copy1, InstsToMove);
12620b57cec5SDimitry Andric
12630b57cec5SDimitry Andric CI.I->eraseFromParent();
1264480093f4SDimitry Andric Paired.I->eraseFromParent();
12658bcb0991SDimitry Andric return New;
12668bcb0991SDimitry Andric }
12678bcb0991SDimitry Andric
mergeSBufferLoadImmPair(CombineInfo & CI,CombineInfo & Paired,const SmallVectorImpl<MachineInstr * > & InstsToMove)12685ffd83dbSDimitry Andric MachineBasicBlock::iterator SILoadStoreOptimizer::mergeSBufferLoadImmPair(
12695ffd83dbSDimitry Andric CombineInfo &CI, CombineInfo &Paired,
12705ffd83dbSDimitry Andric const SmallVectorImpl<MachineInstr *> &InstsToMove) {
12718bcb0991SDimitry Andric MachineBasicBlock *MBB = CI.I->getParent();
12728bcb0991SDimitry Andric DebugLoc DL = CI.I->getDebugLoc();
1273480093f4SDimitry Andric const unsigned Opcode = getNewOpcode(CI, Paired);
12748bcb0991SDimitry Andric
1275480093f4SDimitry Andric const TargetRegisterClass *SuperRC = getTargetRegisterClass(CI, Paired);
12768bcb0991SDimitry Andric
12778bcb0991SDimitry Andric Register DestReg = MRI->createVirtualRegister(SuperRC);
1278480093f4SDimitry Andric unsigned MergedOffset = std::min(CI.Offset, Paired.Offset);
12798bcb0991SDimitry Andric
12808bcb0991SDimitry Andric // It shouldn't be possible to get this far if the two instructions
12818bcb0991SDimitry Andric // don't have a single memoperand, because MachineInstr::mayAlias()
12828bcb0991SDimitry Andric // will return true if this is the case.
1283480093f4SDimitry Andric assert(CI.I->hasOneMemOperand() && Paired.I->hasOneMemOperand());
12848bcb0991SDimitry Andric
12858bcb0991SDimitry Andric const MachineMemOperand *MMOa = *CI.I->memoperands_begin();
1286480093f4SDimitry Andric const MachineMemOperand *MMOb = *Paired.I->memoperands_begin();
12878bcb0991SDimitry Andric
12888bcb0991SDimitry Andric MachineInstr *New =
1289480093f4SDimitry Andric BuildMI(*MBB, Paired.I, DL, TII->get(Opcode), DestReg)
12908bcb0991SDimitry Andric .add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::sbase))
12918bcb0991SDimitry Andric .addImm(MergedOffset) // offset
1292*5f7ddb14SDimitry Andric .addImm(CI.CPol) // cpol
12938bcb0991SDimitry Andric .addMemOperand(combineKnownAdjacentMMOs(*MBB->getParent(), MMOa, MMOb));
12948bcb0991SDimitry Andric
1295480093f4SDimitry Andric std::pair<unsigned, unsigned> SubRegIdx = getSubRegIdxs(CI, Paired);
12968bcb0991SDimitry Andric const unsigned SubRegIdx0 = std::get<0>(SubRegIdx);
12978bcb0991SDimitry Andric const unsigned SubRegIdx1 = std::get<1>(SubRegIdx);
12988bcb0991SDimitry Andric
12998bcb0991SDimitry Andric // Copy to the old destination registers.
13008bcb0991SDimitry Andric const MCInstrDesc &CopyDesc = TII->get(TargetOpcode::COPY);
13018bcb0991SDimitry Andric const auto *Dest0 = TII->getNamedOperand(*CI.I, AMDGPU::OpName::sdst);
1302480093f4SDimitry Andric const auto *Dest1 = TII->getNamedOperand(*Paired.I, AMDGPU::OpName::sdst);
13038bcb0991SDimitry Andric
1304480093f4SDimitry Andric BuildMI(*MBB, Paired.I, DL, CopyDesc)
13058bcb0991SDimitry Andric .add(*Dest0) // Copy to same destination including flags and sub reg.
13068bcb0991SDimitry Andric .addReg(DestReg, 0, SubRegIdx0);
1307480093f4SDimitry Andric MachineInstr *Copy1 = BuildMI(*MBB, Paired.I, DL, CopyDesc)
13088bcb0991SDimitry Andric .add(*Dest1)
13098bcb0991SDimitry Andric .addReg(DestReg, RegState::Kill, SubRegIdx1);
13108bcb0991SDimitry Andric
13115ffd83dbSDimitry Andric moveInstsAfter(Copy1, InstsToMove);
13128bcb0991SDimitry Andric
13138bcb0991SDimitry Andric CI.I->eraseFromParent();
1314480093f4SDimitry Andric Paired.I->eraseFromParent();
13158bcb0991SDimitry Andric return New;
13168bcb0991SDimitry Andric }
13178bcb0991SDimitry Andric
mergeBufferLoadPair(CombineInfo & CI,CombineInfo & Paired,const SmallVectorImpl<MachineInstr * > & InstsToMove)13185ffd83dbSDimitry Andric MachineBasicBlock::iterator SILoadStoreOptimizer::mergeBufferLoadPair(
13195ffd83dbSDimitry Andric CombineInfo &CI, CombineInfo &Paired,
13205ffd83dbSDimitry Andric const SmallVectorImpl<MachineInstr *> &InstsToMove) {
13218bcb0991SDimitry Andric MachineBasicBlock *MBB = CI.I->getParent();
13228bcb0991SDimitry Andric DebugLoc DL = CI.I->getDebugLoc();
13238bcb0991SDimitry Andric
1324480093f4SDimitry Andric const unsigned Opcode = getNewOpcode(CI, Paired);
13258bcb0991SDimitry Andric
1326480093f4SDimitry Andric const TargetRegisterClass *SuperRC = getTargetRegisterClass(CI, Paired);
13278bcb0991SDimitry Andric
13288bcb0991SDimitry Andric // Copy to the new source register.
13298bcb0991SDimitry Andric Register DestReg = MRI->createVirtualRegister(SuperRC);
1330480093f4SDimitry Andric unsigned MergedOffset = std::min(CI.Offset, Paired.Offset);
13318bcb0991SDimitry Andric
1332480093f4SDimitry Andric auto MIB = BuildMI(*MBB, Paired.I, DL, TII->get(Opcode), DestReg);
13338bcb0991SDimitry Andric
13345ffd83dbSDimitry Andric AddressRegs Regs = getRegs(Opcode, *TII);
13358bcb0991SDimitry Andric
13365ffd83dbSDimitry Andric if (Regs.VAddr)
13378bcb0991SDimitry Andric MIB.add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::vaddr));
13388bcb0991SDimitry Andric
13398bcb0991SDimitry Andric // It shouldn't be possible to get this far if the two instructions
13408bcb0991SDimitry Andric // don't have a single memoperand, because MachineInstr::mayAlias()
13418bcb0991SDimitry Andric // will return true if this is the case.
1342480093f4SDimitry Andric assert(CI.I->hasOneMemOperand() && Paired.I->hasOneMemOperand());
13438bcb0991SDimitry Andric
13448bcb0991SDimitry Andric const MachineMemOperand *MMOa = *CI.I->memoperands_begin();
1345480093f4SDimitry Andric const MachineMemOperand *MMOb = *Paired.I->memoperands_begin();
13468bcb0991SDimitry Andric
13478bcb0991SDimitry Andric MachineInstr *New =
13488bcb0991SDimitry Andric MIB.add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::srsrc))
13498bcb0991SDimitry Andric .add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::soffset))
13508bcb0991SDimitry Andric .addImm(MergedOffset) // offset
1351*5f7ddb14SDimitry Andric .addImm(CI.CPol) // cpol
13528bcb0991SDimitry Andric .addImm(0) // tfe
13538bcb0991SDimitry Andric .addImm(0) // swz
13548bcb0991SDimitry Andric .addMemOperand(combineKnownAdjacentMMOs(*MBB->getParent(), MMOa, MMOb));
13558bcb0991SDimitry Andric
1356480093f4SDimitry Andric std::pair<unsigned, unsigned> SubRegIdx = getSubRegIdxs(CI, Paired);
13578bcb0991SDimitry Andric const unsigned SubRegIdx0 = std::get<0>(SubRegIdx);
13588bcb0991SDimitry Andric const unsigned SubRegIdx1 = std::get<1>(SubRegIdx);
13598bcb0991SDimitry Andric
13608bcb0991SDimitry Andric // Copy to the old destination registers.
13618bcb0991SDimitry Andric const MCInstrDesc &CopyDesc = TII->get(TargetOpcode::COPY);
13628bcb0991SDimitry Andric const auto *Dest0 = TII->getNamedOperand(*CI.I, AMDGPU::OpName::vdata);
1363480093f4SDimitry Andric const auto *Dest1 = TII->getNamedOperand(*Paired.I, AMDGPU::OpName::vdata);
13648bcb0991SDimitry Andric
1365480093f4SDimitry Andric BuildMI(*MBB, Paired.I, DL, CopyDesc)
13668bcb0991SDimitry Andric .add(*Dest0) // Copy to same destination including flags and sub reg.
13678bcb0991SDimitry Andric .addReg(DestReg, 0, SubRegIdx0);
1368480093f4SDimitry Andric MachineInstr *Copy1 = BuildMI(*MBB, Paired.I, DL, CopyDesc)
13698bcb0991SDimitry Andric .add(*Dest1)
13708bcb0991SDimitry Andric .addReg(DestReg, RegState::Kill, SubRegIdx1);
13718bcb0991SDimitry Andric
13725ffd83dbSDimitry Andric moveInstsAfter(Copy1, InstsToMove);
13738bcb0991SDimitry Andric
13748bcb0991SDimitry Andric CI.I->eraseFromParent();
1375480093f4SDimitry Andric Paired.I->eraseFromParent();
13768bcb0991SDimitry Andric return New;
13770b57cec5SDimitry Andric }
13780b57cec5SDimitry Andric
mergeTBufferLoadPair(CombineInfo & CI,CombineInfo & Paired,const SmallVectorImpl<MachineInstr * > & InstsToMove)13795ffd83dbSDimitry Andric MachineBasicBlock::iterator SILoadStoreOptimizer::mergeTBufferLoadPair(
13805ffd83dbSDimitry Andric CombineInfo &CI, CombineInfo &Paired,
13815ffd83dbSDimitry Andric const SmallVectorImpl<MachineInstr *> &InstsToMove) {
1382480093f4SDimitry Andric MachineBasicBlock *MBB = CI.I->getParent();
1383480093f4SDimitry Andric DebugLoc DL = CI.I->getDebugLoc();
1384480093f4SDimitry Andric
1385480093f4SDimitry Andric const unsigned Opcode = getNewOpcode(CI, Paired);
1386480093f4SDimitry Andric
1387480093f4SDimitry Andric const TargetRegisterClass *SuperRC = getTargetRegisterClass(CI, Paired);
1388480093f4SDimitry Andric
1389480093f4SDimitry Andric // Copy to the new source register.
1390480093f4SDimitry Andric Register DestReg = MRI->createVirtualRegister(SuperRC);
1391480093f4SDimitry Andric unsigned MergedOffset = std::min(CI.Offset, Paired.Offset);
1392480093f4SDimitry Andric
1393480093f4SDimitry Andric auto MIB = BuildMI(*MBB, Paired.I, DL, TII->get(Opcode), DestReg);
1394480093f4SDimitry Andric
13955ffd83dbSDimitry Andric AddressRegs Regs = getRegs(Opcode, *TII);
1396480093f4SDimitry Andric
13975ffd83dbSDimitry Andric if (Regs.VAddr)
1398480093f4SDimitry Andric MIB.add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::vaddr));
1399480093f4SDimitry Andric
1400480093f4SDimitry Andric unsigned JoinedFormat =
14015ffd83dbSDimitry Andric getBufferFormatWithCompCount(CI.Format, CI.Width + Paired.Width, *STM);
1402480093f4SDimitry Andric
1403480093f4SDimitry Andric // It shouldn't be possible to get this far if the two instructions
1404480093f4SDimitry Andric // don't have a single memoperand, because MachineInstr::mayAlias()
1405480093f4SDimitry Andric // will return true if this is the case.
1406480093f4SDimitry Andric assert(CI.I->hasOneMemOperand() && Paired.I->hasOneMemOperand());
1407480093f4SDimitry Andric
1408480093f4SDimitry Andric const MachineMemOperand *MMOa = *CI.I->memoperands_begin();
1409480093f4SDimitry Andric const MachineMemOperand *MMOb = *Paired.I->memoperands_begin();
1410480093f4SDimitry Andric
1411480093f4SDimitry Andric MachineInstr *New =
1412480093f4SDimitry Andric MIB.add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::srsrc))
1413480093f4SDimitry Andric .add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::soffset))
1414480093f4SDimitry Andric .addImm(MergedOffset) // offset
1415480093f4SDimitry Andric .addImm(JoinedFormat) // format
1416*5f7ddb14SDimitry Andric .addImm(CI.CPol) // cpol
1417480093f4SDimitry Andric .addImm(0) // tfe
1418480093f4SDimitry Andric .addImm(0) // swz
1419480093f4SDimitry Andric .addMemOperand(
1420480093f4SDimitry Andric combineKnownAdjacentMMOs(*MBB->getParent(), MMOa, MMOb));
1421480093f4SDimitry Andric
1422480093f4SDimitry Andric std::pair<unsigned, unsigned> SubRegIdx = getSubRegIdxs(CI, Paired);
1423480093f4SDimitry Andric const unsigned SubRegIdx0 = std::get<0>(SubRegIdx);
1424480093f4SDimitry Andric const unsigned SubRegIdx1 = std::get<1>(SubRegIdx);
1425480093f4SDimitry Andric
1426480093f4SDimitry Andric // Copy to the old destination registers.
1427480093f4SDimitry Andric const MCInstrDesc &CopyDesc = TII->get(TargetOpcode::COPY);
1428480093f4SDimitry Andric const auto *Dest0 = TII->getNamedOperand(*CI.I, AMDGPU::OpName::vdata);
1429480093f4SDimitry Andric const auto *Dest1 = TII->getNamedOperand(*Paired.I, AMDGPU::OpName::vdata);
1430480093f4SDimitry Andric
1431480093f4SDimitry Andric BuildMI(*MBB, Paired.I, DL, CopyDesc)
1432480093f4SDimitry Andric .add(*Dest0) // Copy to same destination including flags and sub reg.
1433480093f4SDimitry Andric .addReg(DestReg, 0, SubRegIdx0);
1434480093f4SDimitry Andric MachineInstr *Copy1 = BuildMI(*MBB, Paired.I, DL, CopyDesc)
1435480093f4SDimitry Andric .add(*Dest1)
1436480093f4SDimitry Andric .addReg(DestReg, RegState::Kill, SubRegIdx1);
1437480093f4SDimitry Andric
14385ffd83dbSDimitry Andric moveInstsAfter(Copy1, InstsToMove);
1439480093f4SDimitry Andric
1440480093f4SDimitry Andric CI.I->eraseFromParent();
1441480093f4SDimitry Andric Paired.I->eraseFromParent();
1442480093f4SDimitry Andric return New;
1443480093f4SDimitry Andric }
1444480093f4SDimitry Andric
mergeTBufferStorePair(CombineInfo & CI,CombineInfo & Paired,const SmallVectorImpl<MachineInstr * > & InstsToMove)14455ffd83dbSDimitry Andric MachineBasicBlock::iterator SILoadStoreOptimizer::mergeTBufferStorePair(
14465ffd83dbSDimitry Andric CombineInfo &CI, CombineInfo &Paired,
14475ffd83dbSDimitry Andric const SmallVectorImpl<MachineInstr *> &InstsToMove) {
1448480093f4SDimitry Andric MachineBasicBlock *MBB = CI.I->getParent();
1449480093f4SDimitry Andric DebugLoc DL = CI.I->getDebugLoc();
1450480093f4SDimitry Andric
1451480093f4SDimitry Andric const unsigned Opcode = getNewOpcode(CI, Paired);
1452480093f4SDimitry Andric
1453480093f4SDimitry Andric std::pair<unsigned, unsigned> SubRegIdx = getSubRegIdxs(CI, Paired);
1454480093f4SDimitry Andric const unsigned SubRegIdx0 = std::get<0>(SubRegIdx);
1455480093f4SDimitry Andric const unsigned SubRegIdx1 = std::get<1>(SubRegIdx);
1456480093f4SDimitry Andric
1457480093f4SDimitry Andric // Copy to the new source register.
1458480093f4SDimitry Andric const TargetRegisterClass *SuperRC = getTargetRegisterClass(CI, Paired);
1459480093f4SDimitry Andric Register SrcReg = MRI->createVirtualRegister(SuperRC);
1460480093f4SDimitry Andric
1461480093f4SDimitry Andric const auto *Src0 = TII->getNamedOperand(*CI.I, AMDGPU::OpName::vdata);
1462480093f4SDimitry Andric const auto *Src1 = TII->getNamedOperand(*Paired.I, AMDGPU::OpName::vdata);
1463480093f4SDimitry Andric
1464480093f4SDimitry Andric BuildMI(*MBB, Paired.I, DL, TII->get(AMDGPU::REG_SEQUENCE), SrcReg)
1465480093f4SDimitry Andric .add(*Src0)
1466480093f4SDimitry Andric .addImm(SubRegIdx0)
1467480093f4SDimitry Andric .add(*Src1)
1468480093f4SDimitry Andric .addImm(SubRegIdx1);
1469480093f4SDimitry Andric
1470480093f4SDimitry Andric auto MIB = BuildMI(*MBB, Paired.I, DL, TII->get(Opcode))
1471480093f4SDimitry Andric .addReg(SrcReg, RegState::Kill);
1472480093f4SDimitry Andric
14735ffd83dbSDimitry Andric AddressRegs Regs = getRegs(Opcode, *TII);
1474480093f4SDimitry Andric
14755ffd83dbSDimitry Andric if (Regs.VAddr)
1476480093f4SDimitry Andric MIB.add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::vaddr));
1477480093f4SDimitry Andric
1478480093f4SDimitry Andric unsigned JoinedFormat =
14795ffd83dbSDimitry Andric getBufferFormatWithCompCount(CI.Format, CI.Width + Paired.Width, *STM);
1480480093f4SDimitry Andric
1481480093f4SDimitry Andric // It shouldn't be possible to get this far if the two instructions
1482480093f4SDimitry Andric // don't have a single memoperand, because MachineInstr::mayAlias()
1483480093f4SDimitry Andric // will return true if this is the case.
1484480093f4SDimitry Andric assert(CI.I->hasOneMemOperand() && Paired.I->hasOneMemOperand());
1485480093f4SDimitry Andric
1486480093f4SDimitry Andric const MachineMemOperand *MMOa = *CI.I->memoperands_begin();
1487480093f4SDimitry Andric const MachineMemOperand *MMOb = *Paired.I->memoperands_begin();
1488480093f4SDimitry Andric
1489480093f4SDimitry Andric MachineInstr *New =
1490480093f4SDimitry Andric MIB.add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::srsrc))
1491480093f4SDimitry Andric .add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::soffset))
1492480093f4SDimitry Andric .addImm(std::min(CI.Offset, Paired.Offset)) // offset
1493480093f4SDimitry Andric .addImm(JoinedFormat) // format
1494*5f7ddb14SDimitry Andric .addImm(CI.CPol) // cpol
1495480093f4SDimitry Andric .addImm(0) // tfe
1496480093f4SDimitry Andric .addImm(0) // swz
1497480093f4SDimitry Andric .addMemOperand(
1498480093f4SDimitry Andric combineKnownAdjacentMMOs(*MBB->getParent(), MMOa, MMOb));
1499480093f4SDimitry Andric
15005ffd83dbSDimitry Andric moveInstsAfter(MIB, InstsToMove);
1501480093f4SDimitry Andric
1502480093f4SDimitry Andric CI.I->eraseFromParent();
1503480093f4SDimitry Andric Paired.I->eraseFromParent();
1504480093f4SDimitry Andric return New;
1505480093f4SDimitry Andric }
1506480093f4SDimitry Andric
getNewOpcode(const CombineInfo & CI,const CombineInfo & Paired)1507480093f4SDimitry Andric unsigned SILoadStoreOptimizer::getNewOpcode(const CombineInfo &CI,
1508480093f4SDimitry Andric const CombineInfo &Paired) {
1509480093f4SDimitry Andric const unsigned Width = CI.Width + Paired.Width;
15100b57cec5SDimitry Andric
15110b57cec5SDimitry Andric switch (CI.InstClass) {
15120b57cec5SDimitry Andric default:
15138bcb0991SDimitry Andric assert(CI.InstClass == BUFFER_LOAD || CI.InstClass == BUFFER_STORE);
15148bcb0991SDimitry Andric // FIXME: Handle d16 correctly
15158bcb0991SDimitry Andric return AMDGPU::getMUBUFOpcode(AMDGPU::getMUBUFBaseOpcode(CI.I->getOpcode()),
15168bcb0991SDimitry Andric Width);
1517480093f4SDimitry Andric case TBUFFER_LOAD:
1518480093f4SDimitry Andric case TBUFFER_STORE:
1519480093f4SDimitry Andric return AMDGPU::getMTBUFOpcode(AMDGPU::getMTBUFBaseOpcode(CI.I->getOpcode()),
1520480093f4SDimitry Andric Width);
1521480093f4SDimitry Andric
15220b57cec5SDimitry Andric case UNKNOWN:
15230b57cec5SDimitry Andric llvm_unreachable("Unknown instruction class");
15240b57cec5SDimitry Andric case S_BUFFER_LOAD_IMM:
15250b57cec5SDimitry Andric switch (Width) {
15260b57cec5SDimitry Andric default:
15270b57cec5SDimitry Andric return 0;
15280b57cec5SDimitry Andric case 2:
15290b57cec5SDimitry Andric return AMDGPU::S_BUFFER_LOAD_DWORDX2_IMM;
15300b57cec5SDimitry Andric case 4:
15310b57cec5SDimitry Andric return AMDGPU::S_BUFFER_LOAD_DWORDX4_IMM;
15320b57cec5SDimitry Andric }
15338bcb0991SDimitry Andric case MIMG:
1534480093f4SDimitry Andric assert("No overlaps" && (countPopulation(CI.DMask | Paired.DMask) == Width));
15358bcb0991SDimitry Andric return AMDGPU::getMaskedMIMGOp(CI.I->getOpcode(), Width);
15360b57cec5SDimitry Andric }
15370b57cec5SDimitry Andric }
15380b57cec5SDimitry Andric
15390b57cec5SDimitry Andric std::pair<unsigned, unsigned>
getSubRegIdxs(const CombineInfo & CI,const CombineInfo & Paired)1540480093f4SDimitry Andric SILoadStoreOptimizer::getSubRegIdxs(const CombineInfo &CI, const CombineInfo &Paired) {
15418bcb0991SDimitry Andric
1542480093f4SDimitry Andric if (CI.Width == 0 || Paired.Width == 0 || CI.Width + Paired.Width > 4)
15430b57cec5SDimitry Andric return std::make_pair(0, 0);
15448bcb0991SDimitry Andric
15458bcb0991SDimitry Andric bool ReverseOrder;
15468bcb0991SDimitry Andric if (CI.InstClass == MIMG) {
1547480093f4SDimitry Andric assert((countPopulation(CI.DMask | Paired.DMask) == CI.Width + Paired.Width) &&
15488bcb0991SDimitry Andric "No overlaps");
1549480093f4SDimitry Andric ReverseOrder = CI.DMask > Paired.DMask;
15508bcb0991SDimitry Andric } else
1551480093f4SDimitry Andric ReverseOrder = CI.Offset > Paired.Offset;
15528bcb0991SDimitry Andric
15538bcb0991SDimitry Andric static const unsigned Idxs[4][4] = {
15548bcb0991SDimitry Andric {AMDGPU::sub0, AMDGPU::sub0_sub1, AMDGPU::sub0_sub1_sub2, AMDGPU::sub0_sub1_sub2_sub3},
15558bcb0991SDimitry Andric {AMDGPU::sub1, AMDGPU::sub1_sub2, AMDGPU::sub1_sub2_sub3, 0},
15568bcb0991SDimitry Andric {AMDGPU::sub2, AMDGPU::sub2_sub3, 0, 0},
15578bcb0991SDimitry Andric {AMDGPU::sub3, 0, 0, 0},
15588bcb0991SDimitry Andric };
15598bcb0991SDimitry Andric unsigned Idx0;
15608bcb0991SDimitry Andric unsigned Idx1;
15618bcb0991SDimitry Andric
1562480093f4SDimitry Andric assert(CI.Width >= 1 && CI.Width <= 3);
1563480093f4SDimitry Andric assert(Paired.Width >= 1 && Paired.Width <= 3);
15648bcb0991SDimitry Andric
15658bcb0991SDimitry Andric if (ReverseOrder) {
1566480093f4SDimitry Andric Idx1 = Idxs[0][Paired.Width - 1];
1567480093f4SDimitry Andric Idx0 = Idxs[Paired.Width][CI.Width - 1];
15680b57cec5SDimitry Andric } else {
1569480093f4SDimitry Andric Idx0 = Idxs[0][CI.Width - 1];
1570480093f4SDimitry Andric Idx1 = Idxs[CI.Width][Paired.Width - 1];
15710b57cec5SDimitry Andric }
15728bcb0991SDimitry Andric
15738bcb0991SDimitry Andric return std::make_pair(Idx0, Idx1);
15740b57cec5SDimitry Andric }
15750b57cec5SDimitry Andric
15760b57cec5SDimitry Andric const TargetRegisterClass *
getTargetRegisterClass(const CombineInfo & CI,const CombineInfo & Paired)1577480093f4SDimitry Andric SILoadStoreOptimizer::getTargetRegisterClass(const CombineInfo &CI,
1578480093f4SDimitry Andric const CombineInfo &Paired) {
15790b57cec5SDimitry Andric if (CI.InstClass == S_BUFFER_LOAD_IMM) {
1580480093f4SDimitry Andric switch (CI.Width + Paired.Width) {
15810b57cec5SDimitry Andric default:
15820b57cec5SDimitry Andric return nullptr;
15830b57cec5SDimitry Andric case 2:
15840b57cec5SDimitry Andric return &AMDGPU::SReg_64_XEXECRegClass;
15850b57cec5SDimitry Andric case 4:
15868bcb0991SDimitry Andric return &AMDGPU::SGPR_128RegClass;
15870b57cec5SDimitry Andric case 8:
15885ffd83dbSDimitry Andric return &AMDGPU::SGPR_256RegClass;
15890b57cec5SDimitry Andric case 16:
15905ffd83dbSDimitry Andric return &AMDGPU::SGPR_512RegClass;
15910b57cec5SDimitry Andric }
15920b57cec5SDimitry Andric }
1593*5f7ddb14SDimitry Andric
1594*5f7ddb14SDimitry Andric unsigned BitWidth = 32 * (CI.Width + Paired.Width);
1595*5f7ddb14SDimitry Andric return TRI->hasAGPRs(getDataRegClass(*CI.I))
1596*5f7ddb14SDimitry Andric ? TRI->getAGPRClassForBitWidth(BitWidth)
1597*5f7ddb14SDimitry Andric : TRI->getVGPRClassForBitWidth(BitWidth);
15980b57cec5SDimitry Andric }
15990b57cec5SDimitry Andric
mergeBufferStorePair(CombineInfo & CI,CombineInfo & Paired,const SmallVectorImpl<MachineInstr * > & InstsToMove)16005ffd83dbSDimitry Andric MachineBasicBlock::iterator SILoadStoreOptimizer::mergeBufferStorePair(
16015ffd83dbSDimitry Andric CombineInfo &CI, CombineInfo &Paired,
16025ffd83dbSDimitry Andric const SmallVectorImpl<MachineInstr *> &InstsToMove) {
16030b57cec5SDimitry Andric MachineBasicBlock *MBB = CI.I->getParent();
16040b57cec5SDimitry Andric DebugLoc DL = CI.I->getDebugLoc();
16050b57cec5SDimitry Andric
1606480093f4SDimitry Andric const unsigned Opcode = getNewOpcode(CI, Paired);
16070b57cec5SDimitry Andric
1608480093f4SDimitry Andric std::pair<unsigned, unsigned> SubRegIdx = getSubRegIdxs(CI, Paired);
16090b57cec5SDimitry Andric const unsigned SubRegIdx0 = std::get<0>(SubRegIdx);
16100b57cec5SDimitry Andric const unsigned SubRegIdx1 = std::get<1>(SubRegIdx);
16110b57cec5SDimitry Andric
16120b57cec5SDimitry Andric // Copy to the new source register.
1613480093f4SDimitry Andric const TargetRegisterClass *SuperRC = getTargetRegisterClass(CI, Paired);
16148bcb0991SDimitry Andric Register SrcReg = MRI->createVirtualRegister(SuperRC);
16150b57cec5SDimitry Andric
16160b57cec5SDimitry Andric const auto *Src0 = TII->getNamedOperand(*CI.I, AMDGPU::OpName::vdata);
1617480093f4SDimitry Andric const auto *Src1 = TII->getNamedOperand(*Paired.I, AMDGPU::OpName::vdata);
16180b57cec5SDimitry Andric
1619480093f4SDimitry Andric BuildMI(*MBB, Paired.I, DL, TII->get(AMDGPU::REG_SEQUENCE), SrcReg)
16200b57cec5SDimitry Andric .add(*Src0)
16210b57cec5SDimitry Andric .addImm(SubRegIdx0)
16220b57cec5SDimitry Andric .add(*Src1)
16230b57cec5SDimitry Andric .addImm(SubRegIdx1);
16240b57cec5SDimitry Andric
1625480093f4SDimitry Andric auto MIB = BuildMI(*MBB, Paired.I, DL, TII->get(Opcode))
16260b57cec5SDimitry Andric .addReg(SrcReg, RegState::Kill);
16270b57cec5SDimitry Andric
16285ffd83dbSDimitry Andric AddressRegs Regs = getRegs(Opcode, *TII);
16290b57cec5SDimitry Andric
16305ffd83dbSDimitry Andric if (Regs.VAddr)
16310b57cec5SDimitry Andric MIB.add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::vaddr));
16320b57cec5SDimitry Andric
16338bcb0991SDimitry Andric
16348bcb0991SDimitry Andric // It shouldn't be possible to get this far if the two instructions
16358bcb0991SDimitry Andric // don't have a single memoperand, because MachineInstr::mayAlias()
16368bcb0991SDimitry Andric // will return true if this is the case.
1637480093f4SDimitry Andric assert(CI.I->hasOneMemOperand() && Paired.I->hasOneMemOperand());
16388bcb0991SDimitry Andric
16398bcb0991SDimitry Andric const MachineMemOperand *MMOa = *CI.I->memoperands_begin();
1640480093f4SDimitry Andric const MachineMemOperand *MMOb = *Paired.I->memoperands_begin();
16418bcb0991SDimitry Andric
16428bcb0991SDimitry Andric MachineInstr *New =
16430b57cec5SDimitry Andric MIB.add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::srsrc))
16440b57cec5SDimitry Andric .add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::soffset))
1645480093f4SDimitry Andric .addImm(std::min(CI.Offset, Paired.Offset)) // offset
1646*5f7ddb14SDimitry Andric .addImm(CI.CPol) // cpol
16470b57cec5SDimitry Andric .addImm(0) // tfe
16488bcb0991SDimitry Andric .addImm(0) // swz
16498bcb0991SDimitry Andric .addMemOperand(combineKnownAdjacentMMOs(*MBB->getParent(), MMOa, MMOb));
16500b57cec5SDimitry Andric
16515ffd83dbSDimitry Andric moveInstsAfter(MIB, InstsToMove);
16520b57cec5SDimitry Andric
16530b57cec5SDimitry Andric CI.I->eraseFromParent();
1654480093f4SDimitry Andric Paired.I->eraseFromParent();
16558bcb0991SDimitry Andric return New;
16560b57cec5SDimitry Andric }
16570b57cec5SDimitry Andric
16580b57cec5SDimitry Andric MachineOperand
createRegOrImm(int32_t Val,MachineInstr & MI) const16598bcb0991SDimitry Andric SILoadStoreOptimizer::createRegOrImm(int32_t Val, MachineInstr &MI) const {
16600b57cec5SDimitry Andric APInt V(32, Val, true);
16610b57cec5SDimitry Andric if (TII->isInlineConstant(V))
16620b57cec5SDimitry Andric return MachineOperand::CreateImm(Val);
16630b57cec5SDimitry Andric
16648bcb0991SDimitry Andric Register Reg = MRI->createVirtualRegister(&AMDGPU::SReg_32RegClass);
16650b57cec5SDimitry Andric MachineInstr *Mov =
16660b57cec5SDimitry Andric BuildMI(*MI.getParent(), MI.getIterator(), MI.getDebugLoc(),
16670b57cec5SDimitry Andric TII->get(AMDGPU::S_MOV_B32), Reg)
16680b57cec5SDimitry Andric .addImm(Val);
16690b57cec5SDimitry Andric (void)Mov;
16700b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " "; Mov->dump());
16710b57cec5SDimitry Andric return MachineOperand::CreateReg(Reg, false);
16720b57cec5SDimitry Andric }
16730b57cec5SDimitry Andric
16740b57cec5SDimitry Andric // Compute base address using Addr and return the final register.
computeBase(MachineInstr & MI,const MemAddress & Addr) const16755ffd83dbSDimitry Andric Register SILoadStoreOptimizer::computeBase(MachineInstr &MI,
16768bcb0991SDimitry Andric const MemAddress &Addr) const {
16770b57cec5SDimitry Andric MachineBasicBlock *MBB = MI.getParent();
16780b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = MI.getIterator();
16790b57cec5SDimitry Andric DebugLoc DL = MI.getDebugLoc();
16800b57cec5SDimitry Andric
16810b57cec5SDimitry Andric assert((TRI->getRegSizeInBits(Addr.Base.LoReg, *MRI) == 32 ||
16820b57cec5SDimitry Andric Addr.Base.LoSubReg) &&
16830b57cec5SDimitry Andric "Expected 32-bit Base-Register-Low!!");
16840b57cec5SDimitry Andric
16850b57cec5SDimitry Andric assert((TRI->getRegSizeInBits(Addr.Base.HiReg, *MRI) == 32 ||
16860b57cec5SDimitry Andric Addr.Base.HiSubReg) &&
16870b57cec5SDimitry Andric "Expected 32-bit Base-Register-Hi!!");
16880b57cec5SDimitry Andric
16890b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Re-Computed Anchor-Base:\n");
16900b57cec5SDimitry Andric MachineOperand OffsetLo = createRegOrImm(static_cast<int32_t>(Addr.Offset), MI);
16910b57cec5SDimitry Andric MachineOperand OffsetHi =
16920b57cec5SDimitry Andric createRegOrImm(static_cast<int32_t>(Addr.Offset >> 32), MI);
16930b57cec5SDimitry Andric
16940b57cec5SDimitry Andric const auto *CarryRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID);
16958bcb0991SDimitry Andric Register CarryReg = MRI->createVirtualRegister(CarryRC);
16968bcb0991SDimitry Andric Register DeadCarryReg = MRI->createVirtualRegister(CarryRC);
16970b57cec5SDimitry Andric
16988bcb0991SDimitry Andric Register DestSub0 = MRI->createVirtualRegister(&AMDGPU::VGPR_32RegClass);
16998bcb0991SDimitry Andric Register DestSub1 = MRI->createVirtualRegister(&AMDGPU::VGPR_32RegClass);
17000b57cec5SDimitry Andric MachineInstr *LoHalf =
1701af732203SDimitry Andric BuildMI(*MBB, MBBI, DL, TII->get(AMDGPU::V_ADD_CO_U32_e64), DestSub0)
17020b57cec5SDimitry Andric .addReg(CarryReg, RegState::Define)
17030b57cec5SDimitry Andric .addReg(Addr.Base.LoReg, 0, Addr.Base.LoSubReg)
17040b57cec5SDimitry Andric .add(OffsetLo)
17050b57cec5SDimitry Andric .addImm(0); // clamp bit
17060b57cec5SDimitry Andric (void)LoHalf;
17070b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " "; LoHalf->dump(););
17080b57cec5SDimitry Andric
17090b57cec5SDimitry Andric MachineInstr *HiHalf =
17100b57cec5SDimitry Andric BuildMI(*MBB, MBBI, DL, TII->get(AMDGPU::V_ADDC_U32_e64), DestSub1)
17110b57cec5SDimitry Andric .addReg(DeadCarryReg, RegState::Define | RegState::Dead)
17120b57cec5SDimitry Andric .addReg(Addr.Base.HiReg, 0, Addr.Base.HiSubReg)
17130b57cec5SDimitry Andric .add(OffsetHi)
17140b57cec5SDimitry Andric .addReg(CarryReg, RegState::Kill)
17150b57cec5SDimitry Andric .addImm(0); // clamp bit
17160b57cec5SDimitry Andric (void)HiHalf;
17170b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " "; HiHalf->dump(););
17180b57cec5SDimitry Andric
1719*5f7ddb14SDimitry Andric Register FullDestReg = MRI->createVirtualRegister(TRI->getVGPR64Class());
17200b57cec5SDimitry Andric MachineInstr *FullBase =
17210b57cec5SDimitry Andric BuildMI(*MBB, MBBI, DL, TII->get(TargetOpcode::REG_SEQUENCE), FullDestReg)
17220b57cec5SDimitry Andric .addReg(DestSub0)
17230b57cec5SDimitry Andric .addImm(AMDGPU::sub0)
17240b57cec5SDimitry Andric .addReg(DestSub1)
17250b57cec5SDimitry Andric .addImm(AMDGPU::sub1);
17260b57cec5SDimitry Andric (void)FullBase;
17270b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " "; FullBase->dump(); dbgs() << "\n";);
17280b57cec5SDimitry Andric
17290b57cec5SDimitry Andric return FullDestReg;
17300b57cec5SDimitry Andric }
17310b57cec5SDimitry Andric
17320b57cec5SDimitry Andric // Update base and offset with the NewBase and NewOffset in MI.
updateBaseAndOffset(MachineInstr & MI,Register NewBase,int32_t NewOffset) const17330b57cec5SDimitry Andric void SILoadStoreOptimizer::updateBaseAndOffset(MachineInstr &MI,
17345ffd83dbSDimitry Andric Register NewBase,
17358bcb0991SDimitry Andric int32_t NewOffset) const {
1736480093f4SDimitry Andric auto Base = TII->getNamedOperand(MI, AMDGPU::OpName::vaddr);
1737480093f4SDimitry Andric Base->setReg(NewBase);
1738480093f4SDimitry Andric Base->setIsKill(false);
17390b57cec5SDimitry Andric TII->getNamedOperand(MI, AMDGPU::OpName::offset)->setImm(NewOffset);
17400b57cec5SDimitry Andric }
17410b57cec5SDimitry Andric
17420b57cec5SDimitry Andric Optional<int32_t>
extractConstOffset(const MachineOperand & Op) const17438bcb0991SDimitry Andric SILoadStoreOptimizer::extractConstOffset(const MachineOperand &Op) const {
17440b57cec5SDimitry Andric if (Op.isImm())
17450b57cec5SDimitry Andric return Op.getImm();
17460b57cec5SDimitry Andric
17470b57cec5SDimitry Andric if (!Op.isReg())
17480b57cec5SDimitry Andric return None;
17490b57cec5SDimitry Andric
17500b57cec5SDimitry Andric MachineInstr *Def = MRI->getUniqueVRegDef(Op.getReg());
17510b57cec5SDimitry Andric if (!Def || Def->getOpcode() != AMDGPU::S_MOV_B32 ||
17520b57cec5SDimitry Andric !Def->getOperand(1).isImm())
17530b57cec5SDimitry Andric return None;
17540b57cec5SDimitry Andric
17550b57cec5SDimitry Andric return Def->getOperand(1).getImm();
17560b57cec5SDimitry Andric }
17570b57cec5SDimitry Andric
17580b57cec5SDimitry Andric // Analyze Base and extracts:
17590b57cec5SDimitry Andric // - 32bit base registers, subregisters
17600b57cec5SDimitry Andric // - 64bit constant offset
17610b57cec5SDimitry Andric // Expecting base computation as:
17620b57cec5SDimitry Andric // %OFFSET0:sgpr_32 = S_MOV_B32 8000
17630b57cec5SDimitry Andric // %LO:vgpr_32, %c:sreg_64_xexec =
1764af732203SDimitry Andric // V_ADD_CO_U32_e64 %BASE_LO:vgpr_32, %103:sgpr_32,
17650b57cec5SDimitry Andric // %HI:vgpr_32, = V_ADDC_U32_e64 %BASE_HI:vgpr_32, 0, killed %c:sreg_64_xexec
17660b57cec5SDimitry Andric // %Base:vreg_64 =
17670b57cec5SDimitry Andric // REG_SEQUENCE %LO:vgpr_32, %subreg.sub0, %HI:vgpr_32, %subreg.sub1
processBaseWithConstOffset(const MachineOperand & Base,MemAddress & Addr) const17680b57cec5SDimitry Andric void SILoadStoreOptimizer::processBaseWithConstOffset(const MachineOperand &Base,
17698bcb0991SDimitry Andric MemAddress &Addr) const {
17700b57cec5SDimitry Andric if (!Base.isReg())
17710b57cec5SDimitry Andric return;
17720b57cec5SDimitry Andric
17730b57cec5SDimitry Andric MachineInstr *Def = MRI->getUniqueVRegDef(Base.getReg());
17740b57cec5SDimitry Andric if (!Def || Def->getOpcode() != AMDGPU::REG_SEQUENCE
17750b57cec5SDimitry Andric || Def->getNumOperands() != 5)
17760b57cec5SDimitry Andric return;
17770b57cec5SDimitry Andric
17780b57cec5SDimitry Andric MachineOperand BaseLo = Def->getOperand(1);
17790b57cec5SDimitry Andric MachineOperand BaseHi = Def->getOperand(3);
17800b57cec5SDimitry Andric if (!BaseLo.isReg() || !BaseHi.isReg())
17810b57cec5SDimitry Andric return;
17820b57cec5SDimitry Andric
17830b57cec5SDimitry Andric MachineInstr *BaseLoDef = MRI->getUniqueVRegDef(BaseLo.getReg());
17840b57cec5SDimitry Andric MachineInstr *BaseHiDef = MRI->getUniqueVRegDef(BaseHi.getReg());
17850b57cec5SDimitry Andric
1786af732203SDimitry Andric if (!BaseLoDef || BaseLoDef->getOpcode() != AMDGPU::V_ADD_CO_U32_e64 ||
17870b57cec5SDimitry Andric !BaseHiDef || BaseHiDef->getOpcode() != AMDGPU::V_ADDC_U32_e64)
17880b57cec5SDimitry Andric return;
17890b57cec5SDimitry Andric
17900b57cec5SDimitry Andric const auto *Src0 = TII->getNamedOperand(*BaseLoDef, AMDGPU::OpName::src0);
17910b57cec5SDimitry Andric const auto *Src1 = TII->getNamedOperand(*BaseLoDef, AMDGPU::OpName::src1);
17920b57cec5SDimitry Andric
17930b57cec5SDimitry Andric auto Offset0P = extractConstOffset(*Src0);
17940b57cec5SDimitry Andric if (Offset0P)
17950b57cec5SDimitry Andric BaseLo = *Src1;
17960b57cec5SDimitry Andric else {
17970b57cec5SDimitry Andric if (!(Offset0P = extractConstOffset(*Src1)))
17980b57cec5SDimitry Andric return;
17990b57cec5SDimitry Andric BaseLo = *Src0;
18000b57cec5SDimitry Andric }
18010b57cec5SDimitry Andric
18020b57cec5SDimitry Andric Src0 = TII->getNamedOperand(*BaseHiDef, AMDGPU::OpName::src0);
18030b57cec5SDimitry Andric Src1 = TII->getNamedOperand(*BaseHiDef, AMDGPU::OpName::src1);
18040b57cec5SDimitry Andric
18050b57cec5SDimitry Andric if (Src0->isImm())
18060b57cec5SDimitry Andric std::swap(Src0, Src1);
18070b57cec5SDimitry Andric
18080b57cec5SDimitry Andric if (!Src1->isImm())
18090b57cec5SDimitry Andric return;
18100b57cec5SDimitry Andric
18110b57cec5SDimitry Andric uint64_t Offset1 = Src1->getImm();
18120b57cec5SDimitry Andric BaseHi = *Src0;
18130b57cec5SDimitry Andric
18140b57cec5SDimitry Andric Addr.Base.LoReg = BaseLo.getReg();
18150b57cec5SDimitry Andric Addr.Base.HiReg = BaseHi.getReg();
18160b57cec5SDimitry Andric Addr.Base.LoSubReg = BaseLo.getSubReg();
18170b57cec5SDimitry Andric Addr.Base.HiSubReg = BaseHi.getSubReg();
18180b57cec5SDimitry Andric Addr.Offset = (*Offset0P & 0x00000000ffffffff) | (Offset1 << 32);
18190b57cec5SDimitry Andric }
18200b57cec5SDimitry Andric
promoteConstantOffsetToImm(MachineInstr & MI,MemInfoMap & Visited,SmallPtrSet<MachineInstr *,4> & AnchorList) const18210b57cec5SDimitry Andric bool SILoadStoreOptimizer::promoteConstantOffsetToImm(
18220b57cec5SDimitry Andric MachineInstr &MI,
18230b57cec5SDimitry Andric MemInfoMap &Visited,
18248bcb0991SDimitry Andric SmallPtrSet<MachineInstr *, 4> &AnchorList) const {
18250b57cec5SDimitry Andric
18268bcb0991SDimitry Andric if (!(MI.mayLoad() ^ MI.mayStore()))
18270b57cec5SDimitry Andric return false;
18280b57cec5SDimitry Andric
18298bcb0991SDimitry Andric // TODO: Support flat and scratch.
18308bcb0991SDimitry Andric if (AMDGPU::getGlobalSaddrOp(MI.getOpcode()) < 0)
18318bcb0991SDimitry Andric return false;
18328bcb0991SDimitry Andric
18338bcb0991SDimitry Andric if (MI.mayLoad() && TII->getNamedOperand(MI, AMDGPU::OpName::vdata) != NULL)
18340b57cec5SDimitry Andric return false;
18350b57cec5SDimitry Andric
18360b57cec5SDimitry Andric if (AnchorList.count(&MI))
18370b57cec5SDimitry Andric return false;
18380b57cec5SDimitry Andric
18390b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\nTryToPromoteConstantOffsetToImmFor "; MI.dump());
18400b57cec5SDimitry Andric
18410b57cec5SDimitry Andric if (TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm()) {
18420b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Const-offset is already promoted.\n";);
18430b57cec5SDimitry Andric return false;
18440b57cec5SDimitry Andric }
18450b57cec5SDimitry Andric
18460b57cec5SDimitry Andric // Step1: Find the base-registers and a 64bit constant offset.
18470b57cec5SDimitry Andric MachineOperand &Base = *TII->getNamedOperand(MI, AMDGPU::OpName::vaddr);
18480b57cec5SDimitry Andric MemAddress MAddr;
18490b57cec5SDimitry Andric if (Visited.find(&MI) == Visited.end()) {
18500b57cec5SDimitry Andric processBaseWithConstOffset(Base, MAddr);
18510b57cec5SDimitry Andric Visited[&MI] = MAddr;
18520b57cec5SDimitry Andric } else
18530b57cec5SDimitry Andric MAddr = Visited[&MI];
18540b57cec5SDimitry Andric
18550b57cec5SDimitry Andric if (MAddr.Offset == 0) {
18560b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Failed to extract constant-offset or there are no"
18570b57cec5SDimitry Andric " constant offsets that can be promoted.\n";);
18580b57cec5SDimitry Andric return false;
18590b57cec5SDimitry Andric }
18600b57cec5SDimitry Andric
18610b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " BASE: {" << MAddr.Base.HiReg << ", "
18620b57cec5SDimitry Andric << MAddr.Base.LoReg << "} Offset: " << MAddr.Offset << "\n\n";);
18630b57cec5SDimitry Andric
18640b57cec5SDimitry Andric // Step2: Traverse through MI's basic block and find an anchor(that has the
18650b57cec5SDimitry Andric // same base-registers) with the highest 13bit distance from MI's offset.
18660b57cec5SDimitry Andric // E.g. (64bit loads)
18670b57cec5SDimitry Andric // bb:
18680b57cec5SDimitry Andric // addr1 = &a + 4096; load1 = load(addr1, 0)
18690b57cec5SDimitry Andric // addr2 = &a + 6144; load2 = load(addr2, 0)
18700b57cec5SDimitry Andric // addr3 = &a + 8192; load3 = load(addr3, 0)
18710b57cec5SDimitry Andric // addr4 = &a + 10240; load4 = load(addr4, 0)
18720b57cec5SDimitry Andric // addr5 = &a + 12288; load5 = load(addr5, 0)
18730b57cec5SDimitry Andric //
18740b57cec5SDimitry Andric // Starting from the first load, the optimization will try to find a new base
18750b57cec5SDimitry Andric // from which (&a + 4096) has 13 bit distance. Both &a + 6144 and &a + 8192
18760b57cec5SDimitry Andric // has 13bit distance from &a + 4096. The heuristic considers &a + 8192
18770b57cec5SDimitry Andric // as the new-base(anchor) because of the maximum distance which can
18780b57cec5SDimitry Andric // accomodate more intermediate bases presumeably.
18790b57cec5SDimitry Andric //
18800b57cec5SDimitry Andric // Step3: move (&a + 8192) above load1. Compute and promote offsets from
18810b57cec5SDimitry Andric // (&a + 8192) for load1, load2, load4.
18820b57cec5SDimitry Andric // addr = &a + 8192
18830b57cec5SDimitry Andric // load1 = load(addr, -4096)
18840b57cec5SDimitry Andric // load2 = load(addr, -2048)
18850b57cec5SDimitry Andric // load3 = load(addr, 0)
18860b57cec5SDimitry Andric // load4 = load(addr, 2048)
18870b57cec5SDimitry Andric // addr5 = &a + 12288; load5 = load(addr5, 0)
18880b57cec5SDimitry Andric //
18890b57cec5SDimitry Andric MachineInstr *AnchorInst = nullptr;
18900b57cec5SDimitry Andric MemAddress AnchorAddr;
18910b57cec5SDimitry Andric uint32_t MaxDist = std::numeric_limits<uint32_t>::min();
18920b57cec5SDimitry Andric SmallVector<std::pair<MachineInstr *, int64_t>, 4> InstsWCommonBase;
18930b57cec5SDimitry Andric
18940b57cec5SDimitry Andric MachineBasicBlock *MBB = MI.getParent();
18950b57cec5SDimitry Andric MachineBasicBlock::iterator E = MBB->end();
18960b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = MI.getIterator();
18970b57cec5SDimitry Andric ++MBBI;
18980b57cec5SDimitry Andric const SITargetLowering *TLI =
18990b57cec5SDimitry Andric static_cast<const SITargetLowering *>(STM->getTargetLowering());
19000b57cec5SDimitry Andric
19010b57cec5SDimitry Andric for ( ; MBBI != E; ++MBBI) {
19020b57cec5SDimitry Andric MachineInstr &MINext = *MBBI;
19030b57cec5SDimitry Andric // TODO: Support finding an anchor(with same base) from store addresses or
19040b57cec5SDimitry Andric // any other load addresses where the opcodes are different.
19050b57cec5SDimitry Andric if (MINext.getOpcode() != MI.getOpcode() ||
19060b57cec5SDimitry Andric TII->getNamedOperand(MINext, AMDGPU::OpName::offset)->getImm())
19070b57cec5SDimitry Andric continue;
19080b57cec5SDimitry Andric
19090b57cec5SDimitry Andric const MachineOperand &BaseNext =
19100b57cec5SDimitry Andric *TII->getNamedOperand(MINext, AMDGPU::OpName::vaddr);
19110b57cec5SDimitry Andric MemAddress MAddrNext;
19120b57cec5SDimitry Andric if (Visited.find(&MINext) == Visited.end()) {
19130b57cec5SDimitry Andric processBaseWithConstOffset(BaseNext, MAddrNext);
19140b57cec5SDimitry Andric Visited[&MINext] = MAddrNext;
19150b57cec5SDimitry Andric } else
19160b57cec5SDimitry Andric MAddrNext = Visited[&MINext];
19170b57cec5SDimitry Andric
19180b57cec5SDimitry Andric if (MAddrNext.Base.LoReg != MAddr.Base.LoReg ||
19190b57cec5SDimitry Andric MAddrNext.Base.HiReg != MAddr.Base.HiReg ||
19200b57cec5SDimitry Andric MAddrNext.Base.LoSubReg != MAddr.Base.LoSubReg ||
19210b57cec5SDimitry Andric MAddrNext.Base.HiSubReg != MAddr.Base.HiSubReg)
19220b57cec5SDimitry Andric continue;
19230b57cec5SDimitry Andric
19240b57cec5SDimitry Andric InstsWCommonBase.push_back(std::make_pair(&MINext, MAddrNext.Offset));
19250b57cec5SDimitry Andric
19260b57cec5SDimitry Andric int64_t Dist = MAddr.Offset - MAddrNext.Offset;
19270b57cec5SDimitry Andric TargetLoweringBase::AddrMode AM;
19280b57cec5SDimitry Andric AM.HasBaseReg = true;
19290b57cec5SDimitry Andric AM.BaseOffs = Dist;
19300b57cec5SDimitry Andric if (TLI->isLegalGlobalAddressingMode(AM) &&
19310b57cec5SDimitry Andric (uint32_t)std::abs(Dist) > MaxDist) {
19320b57cec5SDimitry Andric MaxDist = std::abs(Dist);
19330b57cec5SDimitry Andric
19340b57cec5SDimitry Andric AnchorAddr = MAddrNext;
19350b57cec5SDimitry Andric AnchorInst = &MINext;
19360b57cec5SDimitry Andric }
19370b57cec5SDimitry Andric }
19380b57cec5SDimitry Andric
19390b57cec5SDimitry Andric if (AnchorInst) {
19400b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Anchor-Inst(with max-distance from Offset): ";
19410b57cec5SDimitry Andric AnchorInst->dump());
19420b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Anchor-Offset from BASE: "
19430b57cec5SDimitry Andric << AnchorAddr.Offset << "\n\n");
19440b57cec5SDimitry Andric
19450b57cec5SDimitry Andric // Instead of moving up, just re-compute anchor-instruction's base address.
19465ffd83dbSDimitry Andric Register Base = computeBase(MI, AnchorAddr);
19470b57cec5SDimitry Andric
19480b57cec5SDimitry Andric updateBaseAndOffset(MI, Base, MAddr.Offset - AnchorAddr.Offset);
19490b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " After promotion: "; MI.dump(););
19500b57cec5SDimitry Andric
19510b57cec5SDimitry Andric for (auto P : InstsWCommonBase) {
19520b57cec5SDimitry Andric TargetLoweringBase::AddrMode AM;
19530b57cec5SDimitry Andric AM.HasBaseReg = true;
19540b57cec5SDimitry Andric AM.BaseOffs = P.second - AnchorAddr.Offset;
19550b57cec5SDimitry Andric
19560b57cec5SDimitry Andric if (TLI->isLegalGlobalAddressingMode(AM)) {
19570b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Promote Offset(" << P.second;
19580b57cec5SDimitry Andric dbgs() << ")"; P.first->dump());
19590b57cec5SDimitry Andric updateBaseAndOffset(*P.first, Base, P.second - AnchorAddr.Offset);
19600b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " After promotion: "; P.first->dump());
19610b57cec5SDimitry Andric }
19620b57cec5SDimitry Andric }
19630b57cec5SDimitry Andric AnchorList.insert(AnchorInst);
19640b57cec5SDimitry Andric return true;
19650b57cec5SDimitry Andric }
19660b57cec5SDimitry Andric
19670b57cec5SDimitry Andric return false;
19680b57cec5SDimitry Andric }
19690b57cec5SDimitry Andric
addInstToMergeableList(const CombineInfo & CI,std::list<std::list<CombineInfo>> & MergeableInsts) const19708bcb0991SDimitry Andric void SILoadStoreOptimizer::addInstToMergeableList(const CombineInfo &CI,
19718bcb0991SDimitry Andric std::list<std::list<CombineInfo> > &MergeableInsts) const {
19728bcb0991SDimitry Andric for (std::list<CombineInfo> &AddrList : MergeableInsts) {
1973480093f4SDimitry Andric if (AddrList.front().InstClass == CI.InstClass &&
1974480093f4SDimitry Andric AddrList.front().hasSameBaseAddress(*CI.I)) {
19758bcb0991SDimitry Andric AddrList.emplace_back(CI);
19768bcb0991SDimitry Andric return;
19778bcb0991SDimitry Andric }
19788bcb0991SDimitry Andric }
19790b57cec5SDimitry Andric
19808bcb0991SDimitry Andric // Base address not found, so add a new list.
19818bcb0991SDimitry Andric MergeableInsts.emplace_back(1, CI);
19828bcb0991SDimitry Andric }
19838bcb0991SDimitry Andric
19845ffd83dbSDimitry Andric std::pair<MachineBasicBlock::iterator, bool>
collectMergeableInsts(MachineBasicBlock::iterator Begin,MachineBasicBlock::iterator End,MemInfoMap & Visited,SmallPtrSet<MachineInstr *,4> & AnchorList,std::list<std::list<CombineInfo>> & MergeableInsts) const19855ffd83dbSDimitry Andric SILoadStoreOptimizer::collectMergeableInsts(
19865ffd83dbSDimitry Andric MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End,
19875ffd83dbSDimitry Andric MemInfoMap &Visited, SmallPtrSet<MachineInstr *, 4> &AnchorList,
19888bcb0991SDimitry Andric std::list<std::list<CombineInfo>> &MergeableInsts) const {
19898bcb0991SDimitry Andric bool Modified = false;
19900b57cec5SDimitry Andric
19918bcb0991SDimitry Andric // Sort potential mergeable instructions into lists. One list per base address.
19925ffd83dbSDimitry Andric unsigned Order = 0;
19935ffd83dbSDimitry Andric MachineBasicBlock::iterator BlockI = Begin;
19945ffd83dbSDimitry Andric for (; BlockI != End; ++BlockI) {
19955ffd83dbSDimitry Andric MachineInstr &MI = *BlockI;
19965ffd83dbSDimitry Andric
19978bcb0991SDimitry Andric // We run this before checking if an address is mergeable, because it can produce
19988bcb0991SDimitry Andric // better code even if the instructions aren't mergeable.
19990b57cec5SDimitry Andric if (promoteConstantOffsetToImm(MI, Visited, AnchorList))
20000b57cec5SDimitry Andric Modified = true;
20010b57cec5SDimitry Andric
20025ffd83dbSDimitry Andric // Don't combine if volatile. We also won't be able to merge across this, so
20035ffd83dbSDimitry Andric // break the search. We can look after this barrier for separate merges.
20045ffd83dbSDimitry Andric if (MI.hasOrderedMemoryRef()) {
20055ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Breaking search on memory fence: " << MI);
20065ffd83dbSDimitry Andric
20075ffd83dbSDimitry Andric // Search will resume after this instruction in a separate merge list.
20085ffd83dbSDimitry Andric ++BlockI;
20095ffd83dbSDimitry Andric break;
20105ffd83dbSDimitry Andric }
20115ffd83dbSDimitry Andric
20128bcb0991SDimitry Andric const InstClassEnum InstClass = getInstClass(MI.getOpcode(), *TII);
20138bcb0991SDimitry Andric if (InstClass == UNKNOWN)
20148bcb0991SDimitry Andric continue;
20158bcb0991SDimitry Andric
20168bcb0991SDimitry Andric CombineInfo CI;
20178bcb0991SDimitry Andric CI.setMI(MI, *TII, *STM);
20185ffd83dbSDimitry Andric CI.Order = Order++;
20198bcb0991SDimitry Andric
20208bcb0991SDimitry Andric if (!CI.hasMergeableAddress(*MRI))
20218bcb0991SDimitry Andric continue;
20228bcb0991SDimitry Andric
20235ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Mergeable: " << MI);
20245ffd83dbSDimitry Andric
20258bcb0991SDimitry Andric addInstToMergeableList(CI, MergeableInsts);
20268bcb0991SDimitry Andric }
20275ffd83dbSDimitry Andric
20285ffd83dbSDimitry Andric // At this point we have lists of Mergeable instructions.
20295ffd83dbSDimitry Andric //
20305ffd83dbSDimitry Andric // Part 2: Sort lists by offset and then for each CombineInfo object in the
20315ffd83dbSDimitry Andric // list try to find an instruction that can be merged with I. If an instruction
20325ffd83dbSDimitry Andric // is found, it is stored in the Paired field. If no instructions are found, then
20335ffd83dbSDimitry Andric // the CombineInfo object is deleted from the list.
20345ffd83dbSDimitry Andric
20355ffd83dbSDimitry Andric for (std::list<std::list<CombineInfo>>::iterator I = MergeableInsts.begin(),
20365ffd83dbSDimitry Andric E = MergeableInsts.end(); I != E;) {
20375ffd83dbSDimitry Andric
20385ffd83dbSDimitry Andric std::list<CombineInfo> &MergeList = *I;
20395ffd83dbSDimitry Andric if (MergeList.size() <= 1) {
20405ffd83dbSDimitry Andric // This means we have found only one instruction with a given address
20415ffd83dbSDimitry Andric // that can be merged, and we need at least 2 instructions to do a merge,
20425ffd83dbSDimitry Andric // so this list can be discarded.
20435ffd83dbSDimitry Andric I = MergeableInsts.erase(I);
20445ffd83dbSDimitry Andric continue;
20455ffd83dbSDimitry Andric }
20465ffd83dbSDimitry Andric
20475ffd83dbSDimitry Andric // Sort the lists by offsets, this way mergeable instructions will be
20485ffd83dbSDimitry Andric // adjacent to each other in the list, which will make it easier to find
20495ffd83dbSDimitry Andric // matches.
20505ffd83dbSDimitry Andric MergeList.sort(
20515ffd83dbSDimitry Andric [] (const CombineInfo &A, CombineInfo &B) {
20525ffd83dbSDimitry Andric return A.Offset < B.Offset;
20535ffd83dbSDimitry Andric });
20545ffd83dbSDimitry Andric ++I;
20555ffd83dbSDimitry Andric }
20565ffd83dbSDimitry Andric
20575ffd83dbSDimitry Andric return std::make_pair(BlockI, Modified);
20588bcb0991SDimitry Andric }
20598bcb0991SDimitry Andric
20608bcb0991SDimitry Andric // Scan through looking for adjacent LDS operations with constant offsets from
20618bcb0991SDimitry Andric // the same base register. We rely on the scheduler to do the hard work of
20628bcb0991SDimitry Andric // clustering nearby loads, and assume these are all adjacent.
optimizeBlock(std::list<std::list<CombineInfo>> & MergeableInsts)20638bcb0991SDimitry Andric bool SILoadStoreOptimizer::optimizeBlock(
20648bcb0991SDimitry Andric std::list<std::list<CombineInfo> > &MergeableInsts) {
20658bcb0991SDimitry Andric bool Modified = false;
20668bcb0991SDimitry Andric
20675ffd83dbSDimitry Andric for (std::list<std::list<CombineInfo>>::iterator I = MergeableInsts.begin(),
20685ffd83dbSDimitry Andric E = MergeableInsts.end(); I != E;) {
20695ffd83dbSDimitry Andric std::list<CombineInfo> &MergeList = *I;
20708bcb0991SDimitry Andric
20718bcb0991SDimitry Andric bool OptimizeListAgain = false;
20728bcb0991SDimitry Andric if (!optimizeInstsWithSameBaseAddr(MergeList, OptimizeListAgain)) {
20735ffd83dbSDimitry Andric // We weren't able to make any changes, so delete the list so we don't
20748bcb0991SDimitry Andric // process the same instructions the next time we try to optimize this
20758bcb0991SDimitry Andric // block.
20765ffd83dbSDimitry Andric I = MergeableInsts.erase(I);
20770b57cec5SDimitry Andric continue;
20780b57cec5SDimitry Andric }
20790b57cec5SDimitry Andric
20805ffd83dbSDimitry Andric Modified = true;
20815ffd83dbSDimitry Andric
20828bcb0991SDimitry Andric // We made changes, but also determined that there were no more optimization
20838bcb0991SDimitry Andric // opportunities, so we don't need to reprocess the list
20845ffd83dbSDimitry Andric if (!OptimizeListAgain) {
20855ffd83dbSDimitry Andric I = MergeableInsts.erase(I);
20865ffd83dbSDimitry Andric continue;
20875ffd83dbSDimitry Andric }
20885ffd83dbSDimitry Andric OptimizeAgain = true;
20898bcb0991SDimitry Andric }
20908bcb0991SDimitry Andric return Modified;
20918bcb0991SDimitry Andric }
20928bcb0991SDimitry Andric
20938bcb0991SDimitry Andric bool
optimizeInstsWithSameBaseAddr(std::list<CombineInfo> & MergeList,bool & OptimizeListAgain)20948bcb0991SDimitry Andric SILoadStoreOptimizer::optimizeInstsWithSameBaseAddr(
20958bcb0991SDimitry Andric std::list<CombineInfo> &MergeList,
20968bcb0991SDimitry Andric bool &OptimizeListAgain) {
20975ffd83dbSDimitry Andric if (MergeList.empty())
20985ffd83dbSDimitry Andric return false;
20995ffd83dbSDimitry Andric
21008bcb0991SDimitry Andric bool Modified = false;
2101480093f4SDimitry Andric
21025ffd83dbSDimitry Andric for (auto I = MergeList.begin(), Next = std::next(I); Next != MergeList.end();
21035ffd83dbSDimitry Andric Next = std::next(I)) {
21045ffd83dbSDimitry Andric
21055ffd83dbSDimitry Andric auto First = I;
21065ffd83dbSDimitry Andric auto Second = Next;
21075ffd83dbSDimitry Andric
21085ffd83dbSDimitry Andric if ((*First).Order > (*Second).Order)
21095ffd83dbSDimitry Andric std::swap(First, Second);
21105ffd83dbSDimitry Andric CombineInfo &CI = *First;
21115ffd83dbSDimitry Andric CombineInfo &Paired = *Second;
21125ffd83dbSDimitry Andric
21135ffd83dbSDimitry Andric SmallVector<MachineInstr *, 8> InstsToMove;
21145ffd83dbSDimitry Andric if (!checkAndPrepareMerge(CI, Paired, InstsToMove)) {
21155ffd83dbSDimitry Andric ++I;
2116480093f4SDimitry Andric continue;
21175ffd83dbSDimitry Andric }
2118480093f4SDimitry Andric
2119480093f4SDimitry Andric Modified = true;
21205ffd83dbSDimitry Andric
21215ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Merging: " << *CI.I << " with: " << *Paired.I);
21220b57cec5SDimitry Andric
21230b57cec5SDimitry Andric switch (CI.InstClass) {
21240b57cec5SDimitry Andric default:
2125480093f4SDimitry Andric llvm_unreachable("unknown InstClass");
21260b57cec5SDimitry Andric break;
2127480093f4SDimitry Andric case DS_READ: {
21285ffd83dbSDimitry Andric MachineBasicBlock::iterator NewMI =
21295ffd83dbSDimitry Andric mergeRead2Pair(CI, Paired, InstsToMove);
21308bcb0991SDimitry Andric CI.setMI(NewMI, *TII, *STM);
21318bcb0991SDimitry Andric break;
2132480093f4SDimitry Andric }
2133480093f4SDimitry Andric case DS_WRITE: {
21345ffd83dbSDimitry Andric MachineBasicBlock::iterator NewMI =
21355ffd83dbSDimitry Andric mergeWrite2Pair(CI, Paired, InstsToMove);
21368bcb0991SDimitry Andric CI.setMI(NewMI, *TII, *STM);
21378bcb0991SDimitry Andric break;
2138480093f4SDimitry Andric }
2139480093f4SDimitry Andric case S_BUFFER_LOAD_IMM: {
21405ffd83dbSDimitry Andric MachineBasicBlock::iterator NewMI =
21415ffd83dbSDimitry Andric mergeSBufferLoadImmPair(CI, Paired, InstsToMove);
21428bcb0991SDimitry Andric CI.setMI(NewMI, *TII, *STM);
2143480093f4SDimitry Andric OptimizeListAgain |= (CI.Width + Paired.Width) < 16;
21448bcb0991SDimitry Andric break;
2145480093f4SDimitry Andric }
2146480093f4SDimitry Andric case BUFFER_LOAD: {
21475ffd83dbSDimitry Andric MachineBasicBlock::iterator NewMI =
21485ffd83dbSDimitry Andric mergeBufferLoadPair(CI, Paired, InstsToMove);
21498bcb0991SDimitry Andric CI.setMI(NewMI, *TII, *STM);
2150480093f4SDimitry Andric OptimizeListAgain |= (CI.Width + Paired.Width) < 4;
21518bcb0991SDimitry Andric break;
2152480093f4SDimitry Andric }
2153480093f4SDimitry Andric case BUFFER_STORE: {
21545ffd83dbSDimitry Andric MachineBasicBlock::iterator NewMI =
21555ffd83dbSDimitry Andric mergeBufferStorePair(CI, Paired, InstsToMove);
21568bcb0991SDimitry Andric CI.setMI(NewMI, *TII, *STM);
2157480093f4SDimitry Andric OptimizeListAgain |= (CI.Width + Paired.Width) < 4;
21588bcb0991SDimitry Andric break;
2159480093f4SDimitry Andric }
2160480093f4SDimitry Andric case MIMG: {
21615ffd83dbSDimitry Andric MachineBasicBlock::iterator NewMI =
21625ffd83dbSDimitry Andric mergeImagePair(CI, Paired, InstsToMove);
21638bcb0991SDimitry Andric CI.setMI(NewMI, *TII, *STM);
2164480093f4SDimitry Andric OptimizeListAgain |= (CI.Width + Paired.Width) < 4;
21658bcb0991SDimitry Andric break;
21668bcb0991SDimitry Andric }
2167480093f4SDimitry Andric case TBUFFER_LOAD: {
21685ffd83dbSDimitry Andric MachineBasicBlock::iterator NewMI =
21695ffd83dbSDimitry Andric mergeTBufferLoadPair(CI, Paired, InstsToMove);
2170480093f4SDimitry Andric CI.setMI(NewMI, *TII, *STM);
2171480093f4SDimitry Andric OptimizeListAgain |= (CI.Width + Paired.Width) < 4;
2172480093f4SDimitry Andric break;
2173480093f4SDimitry Andric }
2174480093f4SDimitry Andric case TBUFFER_STORE: {
21755ffd83dbSDimitry Andric MachineBasicBlock::iterator NewMI =
21765ffd83dbSDimitry Andric mergeTBufferStorePair(CI, Paired, InstsToMove);
2177480093f4SDimitry Andric CI.setMI(NewMI, *TII, *STM);
2178480093f4SDimitry Andric OptimizeListAgain |= (CI.Width + Paired.Width) < 4;
2179480093f4SDimitry Andric break;
2180480093f4SDimitry Andric }
2181480093f4SDimitry Andric }
21825ffd83dbSDimitry Andric CI.Order = Paired.Order;
21835ffd83dbSDimitry Andric if (I == Second)
21845ffd83dbSDimitry Andric I = Next;
2185480093f4SDimitry Andric
21865ffd83dbSDimitry Andric MergeList.erase(Second);
21870b57cec5SDimitry Andric }
21880b57cec5SDimitry Andric
21890b57cec5SDimitry Andric return Modified;
21900b57cec5SDimitry Andric }
21910b57cec5SDimitry Andric
runOnMachineFunction(MachineFunction & MF)21920b57cec5SDimitry Andric bool SILoadStoreOptimizer::runOnMachineFunction(MachineFunction &MF) {
21930b57cec5SDimitry Andric if (skipFunction(MF.getFunction()))
21940b57cec5SDimitry Andric return false;
21950b57cec5SDimitry Andric
21960b57cec5SDimitry Andric STM = &MF.getSubtarget<GCNSubtarget>();
21970b57cec5SDimitry Andric if (!STM->loadStoreOptEnabled())
21980b57cec5SDimitry Andric return false;
21990b57cec5SDimitry Andric
22000b57cec5SDimitry Andric TII = STM->getInstrInfo();
22010b57cec5SDimitry Andric TRI = &TII->getRegisterInfo();
22020b57cec5SDimitry Andric
22030b57cec5SDimitry Andric MRI = &MF.getRegInfo();
22040b57cec5SDimitry Andric AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
22050b57cec5SDimitry Andric
22060b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Running SILoadStoreOptimizer\n");
22070b57cec5SDimitry Andric
22080b57cec5SDimitry Andric bool Modified = false;
22090b57cec5SDimitry Andric
22105ffd83dbSDimitry Andric // Contains the list of instructions for which constant offsets are being
22115ffd83dbSDimitry Andric // promoted to the IMM. This is tracked for an entire block at time.
22125ffd83dbSDimitry Andric SmallPtrSet<MachineInstr *, 4> AnchorList;
22135ffd83dbSDimitry Andric MemInfoMap Visited;
22148bcb0991SDimitry Andric
22150b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF) {
22165ffd83dbSDimitry Andric MachineBasicBlock::iterator SectionEnd;
22175ffd83dbSDimitry Andric for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E;
22185ffd83dbSDimitry Andric I = SectionEnd) {
22195ffd83dbSDimitry Andric bool CollectModified;
22208bcb0991SDimitry Andric std::list<std::list<CombineInfo>> MergeableInsts;
22215ffd83dbSDimitry Andric
22225ffd83dbSDimitry Andric // First pass: Collect list of all instructions we know how to merge in a
22235ffd83dbSDimitry Andric // subset of the block.
22245ffd83dbSDimitry Andric std::tie(SectionEnd, CollectModified) =
22255ffd83dbSDimitry Andric collectMergeableInsts(I, E, Visited, AnchorList, MergeableInsts);
22265ffd83dbSDimitry Andric
22275ffd83dbSDimitry Andric Modified |= CollectModified;
22285ffd83dbSDimitry Andric
22290b57cec5SDimitry Andric do {
22300b57cec5SDimitry Andric OptimizeAgain = false;
22318bcb0991SDimitry Andric Modified |= optimizeBlock(MergeableInsts);
22320b57cec5SDimitry Andric } while (OptimizeAgain);
22330b57cec5SDimitry Andric }
22340b57cec5SDimitry Andric
22355ffd83dbSDimitry Andric Visited.clear();
22365ffd83dbSDimitry Andric AnchorList.clear();
22375ffd83dbSDimitry Andric }
22385ffd83dbSDimitry Andric
22390b57cec5SDimitry Andric return Modified;
22400b57cec5SDimitry Andric }
2241