SILoadStoreOptimizer.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  1. //===- SILoadStoreOptimizer.cpp -------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This pass tries to fuse DS instructions with close by immediate offsets.
  10. // This will fuse operations such as
  11. // ds_read_b32 v0, v2 offset:16
  12. // ds_read_b32 v1, v2 offset:32
  13. // ==>
  14. // ds_read2_b32 v[0:1], v2, offset0:4 offset1:8
  15. //
  16. // The same is done for certain SMEM and VMEM opcodes, e.g.:
  17. // s_buffer_load_dword s4, s[0:3], 4
  18. // s_buffer_load_dword s5, s[0:3], 8
  19. // ==>
  20. // s_buffer_load_dwordx2 s[4:5], s[0:3], 4
  21. //
  22. // This pass also tries to promote constant offset to the immediate by
  23. // adjusting the base. It tries to use a base from the nearby instructions that
  24. // allows it to have a 13bit constant offset and then promotes the 13bit offset
  25. // to the immediate.
  26. // E.g.
  27. // s_movk_i32 s0, 0x1800
  28. // v_add_co_u32_e32 v0, vcc, s0, v2
  29. // v_addc_co_u32_e32 v1, vcc, 0, v6, vcc
  30. //
  31. // s_movk_i32 s0, 0x1000
  32. // v_add_co_u32_e32 v5, vcc, s0, v2
  33. // v_addc_co_u32_e32 v6, vcc, 0, v6, vcc
  34. // global_load_dwordx2 v[5:6], v[5:6], off
  35. // global_load_dwordx2 v[0:1], v[0:1], off
  36. // =>
  37. // s_movk_i32 s0, 0x1000
  38. // v_add_co_u32_e32 v5, vcc, s0, v2
  39. // v_addc_co_u32_e32 v6, vcc, 0, v6, vcc
  40. // global_load_dwordx2 v[5:6], v[5:6], off
  41. // global_load_dwordx2 v[0:1], v[5:6], off offset:2048
  42. //
  43. // Future improvements:
  44. //
  45. // - This currently relies on the scheduler to place loads and stores next to
  46. // each other, and then only merges adjacent pairs of instructions. It would
  47. // be good to be more flexible with interleaved instructions, and possibly run
  48. // before scheduling. It currently missing stores of constants because loading
  49. // the constant into the data register is placed between the stores, although
  50. // this is arguably a scheduling problem.
  51. //
  52. // - Live interval recomputing seems inefficient. This currently only matches
  53. // one pair, and recomputes live intervals and moves on to the next pair. It
  54. // would be better to compute a list of all merges that need to occur.
  55. //
  56. // - With a list of instructions to process, we can also merge more. If a
  57. // cluster of loads have offsets that are too large to fit in the 8-bit
  58. // offsets, but are close enough to fit in the 8 bits, we can add to the base
  59. // pointer and use the new reduced offsets.
  60. //
  61. //===----------------------------------------------------------------------===//
  62. #include "AMDGPU.h"
  63. #include "AMDGPUSubtarget.h"
  64. #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
  65. #include "SIInstrInfo.h"
  66. #include "SIRegisterInfo.h"
  67. #include "Utils/AMDGPUBaseInfo.h"
  68. #include "llvm/ADT/ArrayRef.h"
  69. #include "llvm/ADT/SmallVector.h"
  70. #include "llvm/ADT/StringRef.h"
  71. #include "llvm/Analysis/AliasAnalysis.h"
  72. #include "llvm/CodeGen/MachineBasicBlock.h"
  73. #include "llvm/CodeGen/MachineFunction.h"
  74. #include "llvm/CodeGen/MachineFunctionPass.h"
  75. #include "llvm/CodeGen/MachineInstr.h"
  76. #include "llvm/CodeGen/MachineInstrBuilder.h"
  77. #include "llvm/CodeGen/MachineOperand.h"
  78. #include "llvm/CodeGen/MachineRegisterInfo.h"
  79. #include "llvm/IR/DebugLoc.h"
  80. #include "llvm/Pass.h"
  81. #include "llvm/Support/Debug.h"
  82. #include "llvm/Support/MathExtras.h"
  83. #include "llvm/Support/raw_ostream.h"
  84. #include <algorithm>
  85. #include <cassert>
  86. #include <cstdlib>
  87. #include <iterator>
  88. #include <utility>
  89. using namespace llvm;
  90. #define DEBUG_TYPE "si-load-store-opt"
  91. namespace {
  92. enum InstClassEnum {
  93. UNKNOWN,
  94. DS_READ,
  95. DS_WRITE,
  96. S_BUFFER_LOAD_IMM,
  97. BUFFER_LOAD_OFFEN = AMDGPU::BUFFER_LOAD_DWORD_OFFEN,
  98. BUFFER_LOAD_OFFSET = AMDGPU::BUFFER_LOAD_DWORD_OFFSET,
  99. BUFFER_STORE_OFFEN = AMDGPU::BUFFER_STORE_DWORD_OFFEN,
  100. BUFFER_STORE_OFFSET = AMDGPU::BUFFER_STORE_DWORD_OFFSET,
  101. BUFFER_LOAD_OFFEN_exact = AMDGPU::BUFFER_LOAD_DWORD_OFFEN_exact,
  102. BUFFER_LOAD_OFFSET_exact = AMDGPU::BUFFER_LOAD_DWORD_OFFSET_exact,
  103. BUFFER_STORE_OFFEN_exact = AMDGPU::BUFFER_STORE_DWORD_OFFEN_exact,
  104. BUFFER_STORE_OFFSET_exact = AMDGPU::BUFFER_STORE_DWORD_OFFSET_exact,
  105. };
  106. enum RegisterEnum {
  107. SBASE = 0x1,
  108. SRSRC = 0x2,
  109. SOFFSET = 0x4,
  110. VADDR = 0x8,
  111. ADDR = 0x10,
  112. };
  113. class SILoadStoreOptimizer : public MachineFunctionPass {
  114. struct CombineInfo {
  115. MachineBasicBlock::iterator I;
  116. MachineBasicBlock::iterator Paired;
  117. unsigned EltSize;
  118. unsigned Offset0;
  119. unsigned Offset1;
  120. unsigned Width0;
  121. unsigned Width1;
  122. unsigned BaseOff;
  123. InstClassEnum InstClass;
  124. bool GLC0;
  125. bool GLC1;
  126. bool SLC0;
  127. bool SLC1;
  128. bool DLC0;
  129. bool DLC1;
  130. bool UseST64;
  131. SmallVector<MachineInstr *, 8> InstsToMove;
  132. };
  133. struct BaseRegisters {
  134. unsigned LoReg = 0;
  135. unsigned HiReg = 0;
  136. unsigned LoSubReg = 0;
  137. unsigned HiSubReg = 0;
  138. };
  139. struct MemAddress {
  140. BaseRegisters Base;
  141. int64_t Offset = 0;
  142. };
  143. using MemInfoMap = DenseMap<MachineInstr *, MemAddress>;
  144. private:
  145. const GCNSubtarget *STM = nullptr;
  146. const SIInstrInfo *TII = nullptr;
  147. const SIRegisterInfo *TRI = nullptr;
  148. MachineRegisterInfo *MRI = nullptr;
  149. AliasAnalysis *AA = nullptr;
  150. bool OptimizeAgain;
  151. static bool offsetsCanBeCombined(CombineInfo &CI);
  152. static bool widthsFit(const GCNSubtarget &STM, const CombineInfo &CI);
  153. static unsigned getNewOpcode(const CombineInfo &CI);
  154. static std::pair<unsigned, unsigned> getSubRegIdxs(const CombineInfo &CI);
  155. const TargetRegisterClass *getTargetRegisterClass(const CombineInfo &CI);
  156. unsigned getOpcodeWidth(const MachineInstr &MI);
  157. InstClassEnum getInstClass(unsigned Opc);
  158. unsigned getRegs(unsigned Opc);
  159. bool findMatchingInst(CombineInfo &CI);
  160. unsigned read2Opcode(unsigned EltSize) const;
  161. unsigned read2ST64Opcode(unsigned EltSize) const;
  162. MachineBasicBlock::iterator mergeRead2Pair(CombineInfo &CI);
  163. unsigned write2Opcode(unsigned EltSize) const;
  164. unsigned write2ST64Opcode(unsigned EltSize) const;
  165. MachineBasicBlock::iterator mergeWrite2Pair(CombineInfo &CI);
  166. MachineBasicBlock::iterator mergeSBufferLoadImmPair(CombineInfo &CI);
  167. MachineBasicBlock::iterator mergeBufferLoadPair(CombineInfo &CI);
  168. MachineBasicBlock::iterator mergeBufferStorePair(CombineInfo &CI);
  169. void updateBaseAndOffset(MachineInstr &I, unsigned NewBase,
  170. int32_t NewOffset);
  171. unsigned computeBase(MachineInstr &MI, const MemAddress &Addr);
  172. MachineOperand createRegOrImm(int32_t Val, MachineInstr &MI);
  173. Optional<int32_t> extractConstOffset(const MachineOperand &Op);
  174. void processBaseWithConstOffset(const MachineOperand &Base, MemAddress &Addr);
  175. /// Promotes constant offset to the immediate by adjusting the base. It
  176. /// tries to use a base from the nearby instructions that allows it to have
  177. /// a 13bit constant offset which gets promoted to the immediate.
  178. bool promoteConstantOffsetToImm(MachineInstr &CI,
  179. MemInfoMap &Visited,
  180. SmallPtrSet<MachineInstr *, 4> &Promoted);
  181. public:
  182. static char ID;
  183. SILoadStoreOptimizer() : MachineFunctionPass(ID) {
  184. initializeSILoadStoreOptimizerPass(*PassRegistry::getPassRegistry());
  185. }
  186. bool optimizeBlock(MachineBasicBlock &MBB);
  187. bool runOnMachineFunction(MachineFunction &MF) override;
  188. StringRef getPassName() const override { return "SI Load Store Optimizer"; }
  189. void getAnalysisUsage(AnalysisUsage &AU) const override {
  190. AU.setPreservesCFG();
  191. AU.addRequired<AAResultsWrapperPass>();
  192. MachineFunctionPass::getAnalysisUsage(AU);
  193. }
  194. };
  195. } // end anonymous namespace.
  196. INITIALIZE_PASS_BEGIN(SILoadStoreOptimizer, DEBUG_TYPE,
  197. "SI Load Store Optimizer", false, false)
  198. INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
  199. INITIALIZE_PASS_END(SILoadStoreOptimizer, DEBUG_TYPE, "SI Load Store Optimizer",
  200. false, false)
  201. char SILoadStoreOptimizer::ID = 0;
  202. char &llvm::SILoadStoreOptimizerID = SILoadStoreOptimizer::ID;
  203. FunctionPass *llvm::createSILoadStoreOptimizerPass() {
  204. return new SILoadStoreOptimizer();
  205. }
  206. static void moveInstsAfter(MachineBasicBlock::iterator I,
  207. ArrayRef<MachineInstr *> InstsToMove) {
  208. MachineBasicBlock *MBB = I->getParent();
  209. ++I;
  210. for (MachineInstr *MI : InstsToMove) {
  211. MI->removeFromParent();
  212. MBB->insert(I, MI);
  213. }
  214. }
  215. static void addDefsUsesToList(const MachineInstr &MI,
  216. DenseSet<unsigned> &RegDefs,
  217. DenseSet<unsigned> &PhysRegUses) {
  218. for (const MachineOperand &Op : MI.operands()) {
  219. if (Op.isReg()) {
  220. if (Op.isDef())
  221. RegDefs.insert(Op.getReg());
  222. else if (Op.readsReg() &&
  223. TargetRegisterInfo::isPhysicalRegister(Op.getReg()))
  224. PhysRegUses.insert(Op.getReg());
  225. }
  226. }
  227. }
  228. static bool memAccessesCanBeReordered(MachineBasicBlock::iterator A,
  229. MachineBasicBlock::iterator B,
  230. AliasAnalysis *AA) {
  231. // RAW or WAR - cannot reorder
  232. // WAW - cannot reorder
  233. // RAR - safe to reorder
  234. return !(A->mayStore() || B->mayStore()) || !A->mayAlias(AA, *B, true);
  235. }
  236. // Add MI and its defs to the lists if MI reads one of the defs that are
  237. // already in the list. Returns true in that case.
  238. static bool addToListsIfDependent(MachineInstr &MI, DenseSet<unsigned> &RegDefs,
  239. DenseSet<unsigned> &PhysRegUses,
  240. SmallVectorImpl<MachineInstr *> &Insts) {
  241. for (MachineOperand &Use : MI.operands()) {
  242. // If one of the defs is read, then there is a use of Def between I and the
  243. // instruction that I will potentially be merged with. We will need to move
  244. // this instruction after the merged instructions.
  245. //
  246. // Similarly, if there is a def which is read by an instruction that is to
  247. // be moved for merging, then we need to move the def-instruction as well.
  248. // This can only happen for physical registers such as M0; virtual
  249. // registers are in SSA form.
  250. if (Use.isReg() &&
  251. ((Use.readsReg() && RegDefs.count(Use.getReg())) ||
  252. (Use.isDef() && TargetRegisterInfo::isPhysicalRegister(Use.getReg()) &&
  253. PhysRegUses.count(Use.getReg())))) {
  254. Insts.push_back(&MI);
  255. addDefsUsesToList(MI, RegDefs, PhysRegUses);
  256. return true;
  257. }
  258. }
  259. return false;
  260. }
  261. static bool canMoveInstsAcrossMemOp(MachineInstr &MemOp,
  262. ArrayRef<MachineInstr *> InstsToMove,
  263. AliasAnalysis *AA) {
  264. assert(MemOp.mayLoadOrStore());
  265. for (MachineInstr *InstToMove : InstsToMove) {
  266. if (!InstToMove->mayLoadOrStore())
  267. continue;
  268. if (!memAccessesCanBeReordered(MemOp, *InstToMove, AA))
  269. return false;
  270. }
  271. return true;
  272. }
  273. bool SILoadStoreOptimizer::offsetsCanBeCombined(CombineInfo &CI) {
  274. // XXX - Would the same offset be OK? Is there any reason this would happen or
  275. // be useful?
  276. if (CI.Offset0 == CI.Offset1)
  277. return false;
  278. // This won't be valid if the offset isn't aligned.
  279. if ((CI.Offset0 % CI.EltSize != 0) || (CI.Offset1 % CI.EltSize != 0))
  280. return false;
  281. unsigned EltOffset0 = CI.Offset0 / CI.EltSize;
  282. unsigned EltOffset1 = CI.Offset1 / CI.EltSize;
  283. CI.UseST64 = false;
  284. CI.BaseOff = 0;
  285. // Handle SMEM and VMEM instructions.
  286. if ((CI.InstClass != DS_READ) && (CI.InstClass != DS_WRITE)) {
  287. return (EltOffset0 + CI.Width0 == EltOffset1 ||
  288. EltOffset1 + CI.Width1 == EltOffset0) &&
  289. CI.GLC0 == CI.GLC1 && CI.DLC0 == CI.DLC1 &&
  290. (CI.InstClass == S_BUFFER_LOAD_IMM || CI.SLC0 == CI.SLC1);
  291. }
  292. // If the offset in elements doesn't fit in 8-bits, we might be able to use
  293. // the stride 64 versions.
  294. if ((EltOffset0 % 64 == 0) && (EltOffset1 % 64) == 0 &&
  295. isUInt<8>(EltOffset0 / 64) && isUInt<8>(EltOffset1 / 64)) {
  296. CI.Offset0 = EltOffset0 / 64;
  297. CI.Offset1 = EltOffset1 / 64;
  298. CI.UseST64 = true;
  299. return true;
  300. }
  301. // Check if the new offsets fit in the reduced 8-bit range.
  302. if (isUInt<8>(EltOffset0) && isUInt<8>(EltOffset1)) {
  303. CI.Offset0 = EltOffset0;
  304. CI.Offset1 = EltOffset1;
  305. return true;
  306. }
  307. // Try to shift base address to decrease offsets.
  308. unsigned OffsetDiff = std::abs((int)EltOffset1 - (int)EltOffset0);
  309. CI.BaseOff = std::min(CI.Offset0, CI.Offset1);
  310. if ((OffsetDiff % 64 == 0) && isUInt<8>(OffsetDiff / 64)) {
  311. CI.Offset0 = (EltOffset0 - CI.BaseOff / CI.EltSize) / 64;
  312. CI.Offset1 = (EltOffset1 - CI.BaseOff / CI.EltSize) / 64;
  313. CI.UseST64 = true;
  314. return true;
  315. }
  316. if (isUInt<8>(OffsetDiff)) {
  317. CI.Offset0 = EltOffset0 - CI.BaseOff / CI.EltSize;
  318. CI.Offset1 = EltOffset1 - CI.BaseOff / CI.EltSize;
  319. return true;
  320. }
  321. return false;
  322. }
  323. bool SILoadStoreOptimizer::widthsFit(const GCNSubtarget &STM,
  324. const CombineInfo &CI) {
  325. const unsigned Width = (CI.Width0 + CI.Width1);
  326. switch (CI.InstClass) {
  327. default:
  328. return (Width <= 4) && (STM.hasDwordx3LoadStores() || (Width != 3));
  329. case S_BUFFER_LOAD_IMM:
  330. switch (Width) {
  331. default:
  332. return false;
  333. case 2:
  334. case 4:
  335. return true;
  336. }
  337. }
  338. }
  339. unsigned SILoadStoreOptimizer::getOpcodeWidth(const MachineInstr &MI) {
  340. const unsigned Opc = MI.getOpcode();
  341. if (TII->isMUBUF(MI)) {
  342. return AMDGPU::getMUBUFDwords(Opc);
  343. }
  344. switch (Opc) {
  345. default:
  346. return 0;
  347. case AMDGPU::S_BUFFER_LOAD_DWORD_IMM:
  348. return 1;
  349. case AMDGPU::S_BUFFER_LOAD_DWORDX2_IMM:
  350. return 2;
  351. case AMDGPU::S_BUFFER_LOAD_DWORDX4_IMM:
  352. return 4;
  353. }
  354. }
  355. InstClassEnum SILoadStoreOptimizer::getInstClass(unsigned Opc) {
  356. if (TII->isMUBUF(Opc)) {
  357. const int baseOpcode = AMDGPU::getMUBUFBaseOpcode(Opc);
  358. // If we couldn't identify the opcode, bail out.
  359. if (baseOpcode == -1) {
  360. return UNKNOWN;
  361. }
  362. switch (baseOpcode) {
  363. default:
  364. return UNKNOWN;
  365. case AMDGPU::BUFFER_LOAD_DWORD_OFFEN:
  366. return BUFFER_LOAD_OFFEN;
  367. case AMDGPU::BUFFER_LOAD_DWORD_OFFSET:
  368. return BUFFER_LOAD_OFFSET;
  369. case AMDGPU::BUFFER_STORE_DWORD_OFFEN:
  370. return BUFFER_STORE_OFFEN;
  371. case AMDGPU::BUFFER_STORE_DWORD_OFFSET:
  372. return BUFFER_STORE_OFFSET;
  373. case AMDGPU::BUFFER_LOAD_DWORD_OFFEN_exact:
  374. return BUFFER_LOAD_OFFEN_exact;
  375. case AMDGPU::BUFFER_LOAD_DWORD_OFFSET_exact:
  376. return BUFFER_LOAD_OFFSET_exact;
  377. case AMDGPU::BUFFER_STORE_DWORD_OFFEN_exact:
  378. return BUFFER_STORE_OFFEN_exact;
  379. case AMDGPU::BUFFER_STORE_DWORD_OFFSET_exact:
  380. return BUFFER_STORE_OFFSET_exact;
  381. }
  382. }
  383. switch (Opc) {
  384. default:
  385. return UNKNOWN;
  386. case AMDGPU::S_BUFFER_LOAD_DWORD_IMM:
  387. case AMDGPU::S_BUFFER_LOAD_DWORDX2_IMM:
  388. case AMDGPU::S_BUFFER_LOAD_DWORDX4_IMM:
  389. return S_BUFFER_LOAD_IMM;
  390. case AMDGPU::DS_READ_B32:
  391. case AMDGPU::DS_READ_B64:
  392. case AMDGPU::DS_READ_B32_gfx9:
  393. case AMDGPU::DS_READ_B64_gfx9:
  394. return DS_READ;
  395. case AMDGPU::DS_WRITE_B32:
  396. case AMDGPU::DS_WRITE_B64:
  397. case AMDGPU::DS_WRITE_B32_gfx9:
  398. case AMDGPU::DS_WRITE_B64_gfx9:
  399. return DS_WRITE;
  400. }
  401. }
  402. unsigned SILoadStoreOptimizer::getRegs(unsigned Opc) {
  403. if (TII->isMUBUF(Opc)) {
  404. unsigned result = 0;
  405. if (AMDGPU::getMUBUFHasVAddr(Opc)) {
  406. result |= VADDR;
  407. }
  408. if (AMDGPU::getMUBUFHasSrsrc(Opc)) {
  409. result |= SRSRC;
  410. }
  411. if (AMDGPU::getMUBUFHasSoffset(Opc)) {
  412. result |= SOFFSET;
  413. }
  414. return result;
  415. }
  416. switch (Opc) {
  417. default:
  418. return 0;
  419. case AMDGPU::S_BUFFER_LOAD_DWORD_IMM:
  420. case AMDGPU::S_BUFFER_LOAD_DWORDX2_IMM:
  421. case AMDGPU::S_BUFFER_LOAD_DWORDX4_IMM:
  422. return SBASE;
  423. case AMDGPU::DS_READ_B32:
  424. case AMDGPU::DS_READ_B64:
  425. case AMDGPU::DS_READ_B32_gfx9:
  426. case AMDGPU::DS_READ_B64_gfx9:
  427. case AMDGPU::DS_WRITE_B32:
  428. case AMDGPU::DS_WRITE_B64:
  429. case AMDGPU::DS_WRITE_B32_gfx9:
  430. case AMDGPU::DS_WRITE_B64_gfx9:
  431. return ADDR;
  432. }
  433. }
  434. bool SILoadStoreOptimizer::findMatchingInst(CombineInfo &CI) {
  435. MachineBasicBlock *MBB = CI.I->getParent();
  436. MachineBasicBlock::iterator E = MBB->end();
  437. MachineBasicBlock::iterator MBBI = CI.I;
  438. const unsigned Opc = CI.I->getOpcode();
  439. const InstClassEnum InstClass = getInstClass(Opc);
  440. if (InstClass == UNKNOWN) {
  441. return false;
  442. }
  443. const unsigned Regs = getRegs(Opc);
  444. unsigned AddrOpName[5] = {0};
  445. int AddrIdx[5];
  446. const MachineOperand *AddrReg[5];
  447. unsigned NumAddresses = 0;
  448. if (Regs & ADDR) {
  449. AddrOpName[NumAddresses++] = AMDGPU::OpName::addr;
  450. }
  451. if (Regs & SBASE) {
  452. AddrOpName[NumAddresses++] = AMDGPU::OpName::sbase;
  453. }
  454. if (Regs & SRSRC) {
  455. AddrOpName[NumAddresses++] = AMDGPU::OpName::srsrc;
  456. }
  457. if (Regs & SOFFSET) {
  458. AddrOpName[NumAddresses++] = AMDGPU::OpName::soffset;
  459. }
  460. if (Regs & VADDR) {
  461. AddrOpName[NumAddresses++] = AMDGPU::OpName::vaddr;
  462. }
  463. for (unsigned i = 0; i < NumAddresses; i++) {
  464. AddrIdx[i] = AMDGPU::getNamedOperandIdx(CI.I->getOpcode(), AddrOpName[i]);
  465. AddrReg[i] = &CI.I->getOperand(AddrIdx[i]);
  466. // We only ever merge operations with the same base address register, so
  467. // don't bother scanning forward if there are no other uses.
  468. if (AddrReg[i]->isReg() &&
  469. (TargetRegisterInfo::isPhysicalRegister(AddrReg[i]->getReg()) ||
  470. MRI->hasOneNonDBGUse(AddrReg[i]->getReg())))
  471. return false;
  472. }
  473. ++MBBI;
  474. DenseSet<unsigned> RegDefsToMove;
  475. DenseSet<unsigned> PhysRegUsesToMove;
  476. addDefsUsesToList(*CI.I, RegDefsToMove, PhysRegUsesToMove);
  477. for (; MBBI != E; ++MBBI) {
  478. const bool IsDS = (InstClass == DS_READ) || (InstClass == DS_WRITE);
  479. if ((getInstClass(MBBI->getOpcode()) != InstClass) ||
  480. (IsDS && (MBBI->getOpcode() != Opc))) {
  481. // This is not a matching DS instruction, but we can keep looking as
  482. // long as one of these conditions are met:
  483. // 1. It is safe to move I down past MBBI.
  484. // 2. It is safe to move MBBI down past the instruction that I will
  485. // be merged into.
  486. if (MBBI->hasUnmodeledSideEffects()) {
  487. // We can't re-order this instruction with respect to other memory
  488. // operations, so we fail both conditions mentioned above.
  489. return false;
  490. }
  491. if (MBBI->mayLoadOrStore() &&
  492. (!memAccessesCanBeReordered(*CI.I, *MBBI, AA) ||
  493. !canMoveInstsAcrossMemOp(*MBBI, CI.InstsToMove, AA))) {
  494. // We fail condition #1, but we may still be able to satisfy condition
  495. // #2. Add this instruction to the move list and then we will check
  496. // if condition #2 holds once we have selected the matching instruction.
  497. CI.InstsToMove.push_back(&*MBBI);
  498. addDefsUsesToList(*MBBI, RegDefsToMove, PhysRegUsesToMove);
  499. continue;
  500. }
  501. // When we match I with another DS instruction we will be moving I down
  502. // to the location of the matched instruction any uses of I will need to
  503. // be moved down as well.
  504. addToListsIfDependent(*MBBI, RegDefsToMove, PhysRegUsesToMove,
  505. CI.InstsToMove);
  506. continue;
  507. }
  508. // Don't merge volatiles.
  509. if (MBBI->hasOrderedMemoryRef())
  510. return false;
  511. // Handle a case like
  512. // DS_WRITE_B32 addr, v, idx0
  513. // w = DS_READ_B32 addr, idx0
  514. // DS_WRITE_B32 addr, f(w), idx1
  515. // where the DS_READ_B32 ends up in InstsToMove and therefore prevents
  516. // merging of the two writes.
  517. if (addToListsIfDependent(*MBBI, RegDefsToMove, PhysRegUsesToMove,
  518. CI.InstsToMove))
  519. continue;
  520. bool Match = true;
  521. for (unsigned i = 0; i < NumAddresses; i++) {
  522. const MachineOperand &AddrRegNext = MBBI->getOperand(AddrIdx[i]);
  523. if (AddrReg[i]->isImm() || AddrRegNext.isImm()) {
  524. if (AddrReg[i]->isImm() != AddrRegNext.isImm() ||
  525. AddrReg[i]->getImm() != AddrRegNext.getImm()) {
  526. Match = false;
  527. break;
  528. }
  529. continue;
  530. }
  531. // Check same base pointer. Be careful of subregisters, which can occur
  532. // with vectors of pointers.
  533. if (AddrReg[i]->getReg() != AddrRegNext.getReg() ||
  534. AddrReg[i]->getSubReg() != AddrRegNext.getSubReg()) {
  535. Match = false;
  536. break;
  537. }
  538. }
  539. if (Match) {
  540. int OffsetIdx =
  541. AMDGPU::getNamedOperandIdx(CI.I->getOpcode(), AMDGPU::OpName::offset);
  542. CI.Offset0 = CI.I->getOperand(OffsetIdx).getImm();
  543. CI.Width0 = getOpcodeWidth(*CI.I);
  544. CI.Offset1 = MBBI->getOperand(OffsetIdx).getImm();
  545. CI.Width1 = getOpcodeWidth(*MBBI);
  546. CI.Paired = MBBI;
  547. if ((CI.InstClass == DS_READ) || (CI.InstClass == DS_WRITE)) {
  548. CI.Offset0 &= 0xffff;
  549. CI.Offset1 &= 0xffff;
  550. } else {
  551. CI.GLC0 = TII->getNamedOperand(*CI.I, AMDGPU::OpName::glc)->getImm();
  552. CI.GLC1 = TII->getNamedOperand(*MBBI, AMDGPU::OpName::glc)->getImm();
  553. if (CI.InstClass != S_BUFFER_LOAD_IMM) {
  554. CI.SLC0 = TII->getNamedOperand(*CI.I, AMDGPU::OpName::slc)->getImm();
  555. CI.SLC1 = TII->getNamedOperand(*MBBI, AMDGPU::OpName::slc)->getImm();
  556. }
  557. CI.DLC0 = TII->getNamedOperand(*CI.I, AMDGPU::OpName::dlc)->getImm();
  558. CI.DLC1 = TII->getNamedOperand(*MBBI, AMDGPU::OpName::dlc)->getImm();
  559. }
  560. // Check both offsets fit in the reduced range.
  561. // We also need to go through the list of instructions that we plan to
  562. // move and make sure they are all safe to move down past the merged
  563. // instruction.
  564. if (widthsFit(*STM, CI) && offsetsCanBeCombined(CI))
  565. if (canMoveInstsAcrossMemOp(*MBBI, CI.InstsToMove, AA))
  566. return true;
  567. }
  568. // We've found a load/store that we couldn't merge for some reason.
  569. // We could potentially keep looking, but we'd need to make sure that
  570. // it was safe to move I and also all the instruction in InstsToMove
  571. // down past this instruction.
  572. // check if we can move I across MBBI and if we can move all I's users
  573. if (!memAccessesCanBeReordered(*CI.I, *MBBI, AA) ||
  574. !canMoveInstsAcrossMemOp(*MBBI, CI.InstsToMove, AA))
  575. break;
  576. }
  577. return false;
  578. }
  579. unsigned SILoadStoreOptimizer::read2Opcode(unsigned EltSize) const {
  580. if (STM->ldsRequiresM0Init())
  581. return (EltSize == 4) ? AMDGPU::DS_READ2_B32 : AMDGPU::DS_READ2_B64;
  582. return (EltSize == 4) ? AMDGPU::DS_READ2_B32_gfx9 : AMDGPU::DS_READ2_B64_gfx9;
  583. }
  584. unsigned SILoadStoreOptimizer::read2ST64Opcode(unsigned EltSize) const {
  585. if (STM->ldsRequiresM0Init())
  586. return (EltSize == 4) ? AMDGPU::DS_READ2ST64_B32 : AMDGPU::DS_READ2ST64_B64;
  587. return (EltSize == 4) ? AMDGPU::DS_READ2ST64_B32_gfx9
  588. : AMDGPU::DS_READ2ST64_B64_gfx9;
  589. }
  590. MachineBasicBlock::iterator
  591. SILoadStoreOptimizer::mergeRead2Pair(CombineInfo &CI) {
  592. MachineBasicBlock *MBB = CI.I->getParent();
  593. // Be careful, since the addresses could be subregisters themselves in weird
  594. // cases, like vectors of pointers.
  595. const auto *AddrReg = TII->getNamedOperand(*CI.I, AMDGPU::OpName::addr);
  596. const auto *Dest0 = TII->getNamedOperand(*CI.I, AMDGPU::OpName::vdst);
  597. const auto *Dest1 = TII->getNamedOperand(*CI.Paired, AMDGPU::OpName::vdst);
  598. unsigned NewOffset0 = CI.Offset0;
  599. unsigned NewOffset1 = CI.Offset1;
  600. unsigned Opc =
  601. CI.UseST64 ? read2ST64Opcode(CI.EltSize) : read2Opcode(CI.EltSize);
  602. unsigned SubRegIdx0 = (CI.EltSize == 4) ? AMDGPU::sub0 : AMDGPU::sub0_sub1;
  603. unsigned SubRegIdx1 = (CI.EltSize == 4) ? AMDGPU::sub1 : AMDGPU::sub2_sub3;
  604. if (NewOffset0 > NewOffset1) {
  605. // Canonicalize the merged instruction so the smaller offset comes first.
  606. std::swap(NewOffset0, NewOffset1);
  607. std::swap(SubRegIdx0, SubRegIdx1);
  608. }
  609. assert((isUInt<8>(NewOffset0) && isUInt<8>(NewOffset1)) &&
  610. (NewOffset0 != NewOffset1) && "Computed offset doesn't fit");
  611. const MCInstrDesc &Read2Desc = TII->get(Opc);
  612. const TargetRegisterClass *SuperRC =
  613. (CI.EltSize == 4) ? &AMDGPU::VReg_64RegClass : &AMDGPU::VReg_128RegClass;
  614. unsigned DestReg = MRI->createVirtualRegister(SuperRC);
  615. DebugLoc DL = CI.I->getDebugLoc();
  616. unsigned BaseReg = AddrReg->getReg();
  617. unsigned BaseSubReg = AddrReg->getSubReg();
  618. unsigned BaseRegFlags = 0;
  619. if (CI.BaseOff) {
  620. unsigned ImmReg = MRI->createVirtualRegister(&AMDGPU::SGPR_32RegClass);
  621. BuildMI(*MBB, CI.Paired, DL, TII->get(AMDGPU::S_MOV_B32), ImmReg)
  622. .addImm(CI.BaseOff);
  623. BaseReg = MRI->createVirtualRegister(&AMDGPU::VGPR_32RegClass);
  624. BaseRegFlags = RegState::Kill;
  625. TII->getAddNoCarry(*MBB, CI.Paired, DL, BaseReg)
  626. .addReg(ImmReg)
  627. .addReg(AddrReg->getReg(), 0, BaseSubReg)
  628. .addImm(0); // clamp bit
  629. BaseSubReg = 0;
  630. }
  631. MachineInstrBuilder Read2 =
  632. BuildMI(*MBB, CI.Paired, DL, Read2Desc, DestReg)
  633. .addReg(BaseReg, BaseRegFlags, BaseSubReg) // addr
  634. .addImm(NewOffset0) // offset0
  635. .addImm(NewOffset1) // offset1
  636. .addImm(0) // gds
  637. .cloneMergedMemRefs({&*CI.I, &*CI.Paired});
  638. (void)Read2;
  639. const MCInstrDesc &CopyDesc = TII->get(TargetOpcode::COPY);
  640. // Copy to the old destination registers.
  641. BuildMI(*MBB, CI.Paired, DL, CopyDesc)
  642. .add(*Dest0) // Copy to same destination including flags and sub reg.
  643. .addReg(DestReg, 0, SubRegIdx0);
  644. MachineInstr *Copy1 = BuildMI(*MBB, CI.Paired, DL, CopyDesc)
  645. .add(*Dest1)
  646. .addReg(DestReg, RegState::Kill, SubRegIdx1);
  647. moveInstsAfter(Copy1, CI.InstsToMove);
  648. MachineBasicBlock::iterator Next = std::next(CI.I);
  649. CI.I->eraseFromParent();
  650. CI.Paired->eraseFromParent();
  651. LLVM_DEBUG(dbgs() << "Inserted read2: " << *Read2 << '\n');
  652. return Next;
  653. }
  654. unsigned SILoadStoreOptimizer::write2Opcode(unsigned EltSize) const {
  655. if (STM->ldsRequiresM0Init())
  656. return (EltSize == 4) ? AMDGPU::DS_WRITE2_B32 : AMDGPU::DS_WRITE2_B64;
  657. return (EltSize == 4) ? AMDGPU::DS_WRITE2_B32_gfx9
  658. : AMDGPU::DS_WRITE2_B64_gfx9;
  659. }
  660. unsigned SILoadStoreOptimizer::write2ST64Opcode(unsigned EltSize) const {
  661. if (STM->ldsRequiresM0Init())
  662. return (EltSize == 4) ? AMDGPU::DS_WRITE2ST64_B32
  663. : AMDGPU::DS_WRITE2ST64_B64;
  664. return (EltSize == 4) ? AMDGPU::DS_WRITE2ST64_B32_gfx9
  665. : AMDGPU::DS_WRITE2ST64_B64_gfx9;
  666. }
  667. MachineBasicBlock::iterator
  668. SILoadStoreOptimizer::mergeWrite2Pair(CombineInfo &CI) {
  669. MachineBasicBlock *MBB = CI.I->getParent();
  670. // Be sure to use .addOperand(), and not .addReg() with these. We want to be
  671. // sure we preserve the subregister index and any register flags set on them.
  672. const MachineOperand *AddrReg =
  673. TII->getNamedOperand(*CI.I, AMDGPU::OpName::addr);
  674. const MachineOperand *Data0 =
  675. TII->getNamedOperand(*CI.I, AMDGPU::OpName::data0);
  676. const MachineOperand *Data1 =
  677. TII->getNamedOperand(*CI.Paired, AMDGPU::OpName::data0);
  678. unsigned NewOffset0 = CI.Offset0;
  679. unsigned NewOffset1 = CI.Offset1;
  680. unsigned Opc =
  681. CI.UseST64 ? write2ST64Opcode(CI.EltSize) : write2Opcode(CI.EltSize);
  682. if (NewOffset0 > NewOffset1) {
  683. // Canonicalize the merged instruction so the smaller offset comes first.
  684. std::swap(NewOffset0, NewOffset1);
  685. std::swap(Data0, Data1);
  686. }
  687. assert((isUInt<8>(NewOffset0) && isUInt<8>(NewOffset1)) &&
  688. (NewOffset0 != NewOffset1) && "Computed offset doesn't fit");
  689. const MCInstrDesc &Write2Desc = TII->get(Opc);
  690. DebugLoc DL = CI.I->getDebugLoc();
  691. unsigned BaseReg = AddrReg->getReg();
  692. unsigned BaseSubReg = AddrReg->getSubReg();
  693. unsigned BaseRegFlags = 0;
  694. if (CI.BaseOff) {
  695. unsigned ImmReg = MRI->createVirtualRegister(&AMDGPU::SGPR_32RegClass);
  696. BuildMI(*MBB, CI.Paired, DL, TII->get(AMDGPU::S_MOV_B32), ImmReg)
  697. .addImm(CI.BaseOff);
  698. BaseReg = MRI->createVirtualRegister(&AMDGPU::VGPR_32RegClass);
  699. BaseRegFlags = RegState::Kill;
  700. TII->getAddNoCarry(*MBB, CI.Paired, DL, BaseReg)
  701. .addReg(ImmReg)
  702. .addReg(AddrReg->getReg(), 0, BaseSubReg)
  703. .addImm(0); // clamp bit
  704. BaseSubReg = 0;
  705. }
  706. MachineInstrBuilder Write2 =
  707. BuildMI(*MBB, CI.Paired, DL, Write2Desc)
  708. .addReg(BaseReg, BaseRegFlags, BaseSubReg) // addr
  709. .add(*Data0) // data0
  710. .add(*Data1) // data1
  711. .addImm(NewOffset0) // offset0
  712. .addImm(NewOffset1) // offset1
  713. .addImm(0) // gds
  714. .cloneMergedMemRefs({&*CI.I, &*CI.Paired});
  715. moveInstsAfter(Write2, CI.InstsToMove);
  716. MachineBasicBlock::iterator Next = std::next(CI.I);
  717. CI.I->eraseFromParent();
  718. CI.Paired->eraseFromParent();
  719. LLVM_DEBUG(dbgs() << "Inserted write2 inst: " << *Write2 << '\n');
  720. return Next;
  721. }
  722. MachineBasicBlock::iterator
  723. SILoadStoreOptimizer::mergeSBufferLoadImmPair(CombineInfo &CI) {
  724. MachineBasicBlock *MBB = CI.I->getParent();
  725. DebugLoc DL = CI.I->getDebugLoc();
  726. const unsigned Opcode = getNewOpcode(CI);
  727. const TargetRegisterClass *SuperRC = getTargetRegisterClass(CI);
  728. unsigned DestReg = MRI->createVirtualRegister(SuperRC);
  729. unsigned MergedOffset = std::min(CI.Offset0, CI.Offset1);
  730. BuildMI(*MBB, CI.Paired, DL, TII->get(Opcode), DestReg)
  731. .add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::sbase))
  732. .addImm(MergedOffset) // offset
  733. .addImm(CI.GLC0) // glc
  734. .addImm(CI.DLC0) // dlc
  735. .cloneMergedMemRefs({&*CI.I, &*CI.Paired});
  736. std::pair<unsigned, unsigned> SubRegIdx = getSubRegIdxs(CI);
  737. const unsigned SubRegIdx0 = std::get<0>(SubRegIdx);
  738. const unsigned SubRegIdx1 = std::get<1>(SubRegIdx);
  739. // Copy to the old destination registers.
  740. const MCInstrDesc &CopyDesc = TII->get(TargetOpcode::COPY);
  741. const auto *Dest0 = TII->getNamedOperand(*CI.I, AMDGPU::OpName::sdst);
  742. const auto *Dest1 = TII->getNamedOperand(*CI.Paired, AMDGPU::OpName::sdst);
  743. BuildMI(*MBB, CI.Paired, DL, CopyDesc)
  744. .add(*Dest0) // Copy to same destination including flags and sub reg.
  745. .addReg(DestReg, 0, SubRegIdx0);
  746. MachineInstr *Copy1 = BuildMI(*MBB, CI.Paired, DL, CopyDesc)
  747. .add(*Dest1)
  748. .addReg(DestReg, RegState::Kill, SubRegIdx1);
  749. moveInstsAfter(Copy1, CI.InstsToMove);
  750. MachineBasicBlock::iterator Next = std::next(CI.I);
  751. CI.I->eraseFromParent();
  752. CI.Paired->eraseFromParent();
  753. return Next;
  754. }
  755. MachineBasicBlock::iterator
  756. SILoadStoreOptimizer::mergeBufferLoadPair(CombineInfo &CI) {
  757. MachineBasicBlock *MBB = CI.I->getParent();
  758. DebugLoc DL = CI.I->getDebugLoc();
  759. const unsigned Opcode = getNewOpcode(CI);
  760. const TargetRegisterClass *SuperRC = getTargetRegisterClass(CI);
  761. // Copy to the new source register.
  762. unsigned DestReg = MRI->createVirtualRegister(SuperRC);
  763. unsigned MergedOffset = std::min(CI.Offset0, CI.Offset1);
  764. auto MIB = BuildMI(*MBB, CI.Paired, DL, TII->get(Opcode), DestReg);
  765. const unsigned Regs = getRegs(Opcode);
  766. if (Regs & VADDR)
  767. MIB.add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::vaddr));
  768. MIB.add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::srsrc))
  769. .add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::soffset))
  770. .addImm(MergedOffset) // offset
  771. .addImm(CI.GLC0) // glc
  772. .addImm(CI.SLC0) // slc
  773. .addImm(0) // tfe
  774. .addImm(CI.DLC0) // dlc
  775. .cloneMergedMemRefs({&*CI.I, &*CI.Paired});
  776. std::pair<unsigned, unsigned> SubRegIdx = getSubRegIdxs(CI);
  777. const unsigned SubRegIdx0 = std::get<0>(SubRegIdx);
  778. const unsigned SubRegIdx1 = std::get<1>(SubRegIdx);
  779. // Copy to the old destination registers.
  780. const MCInstrDesc &CopyDesc = TII->get(TargetOpcode::COPY);
  781. const auto *Dest0 = TII->getNamedOperand(*CI.I, AMDGPU::OpName::vdata);
  782. const auto *Dest1 = TII->getNamedOperand(*CI.Paired, AMDGPU::OpName::vdata);
  783. BuildMI(*MBB, CI.Paired, DL, CopyDesc)
  784. .add(*Dest0) // Copy to same destination including flags and sub reg.
  785. .addReg(DestReg, 0, SubRegIdx0);
  786. MachineInstr *Copy1 = BuildMI(*MBB, CI.Paired, DL, CopyDesc)
  787. .add(*Dest1)
  788. .addReg(DestReg, RegState::Kill, SubRegIdx1);
  789. moveInstsAfter(Copy1, CI.InstsToMove);
  790. MachineBasicBlock::iterator Next = std::next(CI.I);
  791. CI.I->eraseFromParent();
  792. CI.Paired->eraseFromParent();
  793. return Next;
  794. }
  795. unsigned SILoadStoreOptimizer::getNewOpcode(const CombineInfo &CI) {
  796. const unsigned Width = CI.Width0 + CI.Width1;
  797. switch (CI.InstClass) {
  798. default:
  799. return AMDGPU::getMUBUFOpcode(CI.InstClass, Width);
  800. case UNKNOWN:
  801. llvm_unreachable("Unknown instruction class");
  802. case S_BUFFER_LOAD_IMM:
  803. switch (Width) {
  804. default:
  805. return 0;
  806. case 2:
  807. return AMDGPU::S_BUFFER_LOAD_DWORDX2_IMM;
  808. case 4:
  809. return AMDGPU::S_BUFFER_LOAD_DWORDX4_IMM;
  810. }
  811. }
  812. }
  813. std::pair<unsigned, unsigned>
  814. SILoadStoreOptimizer::getSubRegIdxs(const CombineInfo &CI) {
  815. if (CI.Offset0 > CI.Offset1) {
  816. switch (CI.Width0) {
  817. default:
  818. return std::make_pair(0, 0);
  819. case 1:
  820. switch (CI.Width1) {
  821. default:
  822. return std::make_pair(0, 0);
  823. case 1:
  824. return std::make_pair(AMDGPU::sub1, AMDGPU::sub0);
  825. case 2:
  826. return std::make_pair(AMDGPU::sub2, AMDGPU::sub0_sub1);
  827. case 3:
  828. return std::make_pair(AMDGPU::sub3, AMDGPU::sub0_sub1_sub2);
  829. }
  830. case 2:
  831. switch (CI.Width1) {
  832. default:
  833. return std::make_pair(0, 0);
  834. case 1:
  835. return std::make_pair(AMDGPU::sub1_sub2, AMDGPU::sub0);
  836. case 2:
  837. return std::make_pair(AMDGPU::sub2_sub3, AMDGPU::sub0_sub1);
  838. }
  839. case 3:
  840. switch (CI.Width1) {
  841. default:
  842. return std::make_pair(0, 0);
  843. case 1:
  844. return std::make_pair(AMDGPU::sub1_sub2_sub3, AMDGPU::sub0);
  845. }
  846. }
  847. } else {
  848. switch (CI.Width0) {
  849. default:
  850. return std::make_pair(0, 0);
  851. case 1:
  852. switch (CI.Width1) {
  853. default:
  854. return std::make_pair(0, 0);
  855. case 1:
  856. return std::make_pair(AMDGPU::sub0, AMDGPU::sub1);
  857. case 2:
  858. return std::make_pair(AMDGPU::sub0, AMDGPU::sub1_sub2);
  859. case 3:
  860. return std::make_pair(AMDGPU::sub0, AMDGPU::sub1_sub2_sub3);
  861. }
  862. case 2:
  863. switch (CI.Width1) {
  864. default:
  865. return std::make_pair(0, 0);
  866. case 1:
  867. return std::make_pair(AMDGPU::sub0_sub1, AMDGPU::sub2);
  868. case 2:
  869. return std::make_pair(AMDGPU::sub0_sub1, AMDGPU::sub2_sub3);
  870. }
  871. case 3:
  872. switch (CI.Width1) {
  873. default:
  874. return std::make_pair(0, 0);
  875. case 1:
  876. return std::make_pair(AMDGPU::sub0_sub1_sub2, AMDGPU::sub3);
  877. }
  878. }
  879. }
  880. }
  881. const TargetRegisterClass *
  882. SILoadStoreOptimizer::getTargetRegisterClass(const CombineInfo &CI) {
  883. if (CI.InstClass == S_BUFFER_LOAD_IMM) {
  884. switch (CI.Width0 + CI.Width1) {
  885. default:
  886. return nullptr;
  887. case 2:
  888. return &AMDGPU::SReg_64_XEXECRegClass;
  889. case 4:
  890. return &AMDGPU::SReg_128RegClass;
  891. case 8:
  892. return &AMDGPU::SReg_256RegClass;
  893. case 16:
  894. return &AMDGPU::SReg_512RegClass;
  895. }
  896. } else {
  897. switch (CI.Width0 + CI.Width1) {
  898. default:
  899. return nullptr;
  900. case 2:
  901. return &AMDGPU::VReg_64RegClass;
  902. case 3:
  903. return &AMDGPU::VReg_96RegClass;
  904. case 4:
  905. return &AMDGPU::VReg_128RegClass;
  906. }
  907. }
  908. }
  909. MachineBasicBlock::iterator
  910. SILoadStoreOptimizer::mergeBufferStorePair(CombineInfo &CI) {
  911. MachineBasicBlock *MBB = CI.I->getParent();
  912. DebugLoc DL = CI.I->getDebugLoc();
  913. const unsigned Opcode = getNewOpcode(CI);
  914. std::pair<unsigned, unsigned> SubRegIdx = getSubRegIdxs(CI);
  915. const unsigned SubRegIdx0 = std::get<0>(SubRegIdx);
  916. const unsigned SubRegIdx1 = std::get<1>(SubRegIdx);
  917. // Copy to the new source register.
  918. const TargetRegisterClass *SuperRC = getTargetRegisterClass(CI);
  919. unsigned SrcReg = MRI->createVirtualRegister(SuperRC);
  920. const auto *Src0 = TII->getNamedOperand(*CI.I, AMDGPU::OpName::vdata);
  921. const auto *Src1 = TII->getNamedOperand(*CI.Paired, AMDGPU::OpName::vdata);
  922. BuildMI(*MBB, CI.Paired, DL, TII->get(AMDGPU::REG_SEQUENCE), SrcReg)
  923. .add(*Src0)
  924. .addImm(SubRegIdx0)
  925. .add(*Src1)
  926. .addImm(SubRegIdx1);
  927. auto MIB = BuildMI(*MBB, CI.Paired, DL, TII->get(Opcode))
  928. .addReg(SrcReg, RegState::Kill);
  929. const unsigned Regs = getRegs(Opcode);
  930. if (Regs & VADDR)
  931. MIB.add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::vaddr));
  932. MIB.add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::srsrc))
  933. .add(*TII->getNamedOperand(*CI.I, AMDGPU::OpName::soffset))
  934. .addImm(std::min(CI.Offset0, CI.Offset1)) // offset
  935. .addImm(CI.GLC0) // glc
  936. .addImm(CI.SLC0) // slc
  937. .addImm(0) // tfe
  938. .addImm(CI.DLC0) // dlc
  939. .cloneMergedMemRefs({&*CI.I, &*CI.Paired});
  940. moveInstsAfter(MIB, CI.InstsToMove);
  941. MachineBasicBlock::iterator Next = std::next(CI.I);
  942. CI.I->eraseFromParent();
  943. CI.Paired->eraseFromParent();
  944. return Next;
  945. }
  946. MachineOperand
  947. SILoadStoreOptimizer::createRegOrImm(int32_t Val, MachineInstr &MI) {
  948. APInt V(32, Val, true);
  949. if (TII->isInlineConstant(V))
  950. return MachineOperand::CreateImm(Val);
  951. unsigned Reg = MRI->createVirtualRegister(&AMDGPU::SReg_32RegClass);
  952. MachineInstr *Mov =
  953. BuildMI(*MI.getParent(), MI.getIterator(), MI.getDebugLoc(),
  954. TII->get(AMDGPU::S_MOV_B32), Reg)
  955. .addImm(Val);
  956. (void)Mov;
  957. LLVM_DEBUG(dbgs() << " "; Mov->dump());
  958. return MachineOperand::CreateReg(Reg, false);
  959. }
  960. // Compute base address using Addr and return the final register.
  961. unsigned SILoadStoreOptimizer::computeBase(MachineInstr &MI,
  962. const MemAddress &Addr) {
  963. MachineBasicBlock *MBB = MI.getParent();
  964. MachineBasicBlock::iterator MBBI = MI.getIterator();
  965. DebugLoc DL = MI.getDebugLoc();
  966. assert((TRI->getRegSizeInBits(Addr.Base.LoReg, *MRI) == 32 ||
  967. Addr.Base.LoSubReg) &&
  968. "Expected 32-bit Base-Register-Low!!");
  969. assert((TRI->getRegSizeInBits(Addr.Base.HiReg, *MRI) == 32 ||
  970. Addr.Base.HiSubReg) &&
  971. "Expected 32-bit Base-Register-Hi!!");
  972. LLVM_DEBUG(dbgs() << " Re-Computed Anchor-Base:\n");
  973. MachineOperand OffsetLo = createRegOrImm(static_cast<int32_t>(Addr.Offset), MI);
  974. MachineOperand OffsetHi =
  975. createRegOrImm(static_cast<int32_t>(Addr.Offset >> 32), MI);
  976. unsigned CarryReg = MRI->createVirtualRegister(&AMDGPU::SReg_64_XEXECRegClass);
  977. unsigned DeadCarryReg =
  978. MRI->createVirtualRegister(&AMDGPU::SReg_64_XEXECRegClass);
  979. unsigned DestSub0 = MRI->createVirtualRegister(&AMDGPU::VGPR_32RegClass);
  980. unsigned DestSub1 = MRI->createVirtualRegister(&AMDGPU::VGPR_32RegClass);
  981. MachineInstr *LoHalf =
  982. BuildMI(*MBB, MBBI, DL, TII->get(AMDGPU::V_ADD_I32_e64), DestSub0)
  983. .addReg(CarryReg, RegState::Define)
  984. .addReg(Addr.Base.LoReg, 0, Addr.Base.LoSubReg)
  985. .add(OffsetLo)
  986. .addImm(0); // clamp bit
  987. (void)LoHalf;
  988. LLVM_DEBUG(dbgs() << " "; LoHalf->dump(););
  989. MachineInstr *HiHalf =
  990. BuildMI(*MBB, MBBI, DL, TII->get(AMDGPU::V_ADDC_U32_e64), DestSub1)
  991. .addReg(DeadCarryReg, RegState::Define | RegState::Dead)
  992. .addReg(Addr.Base.HiReg, 0, Addr.Base.HiSubReg)
  993. .add(OffsetHi)
  994. .addReg(CarryReg, RegState::Kill)
  995. .addImm(0); // clamp bit
  996. (void)HiHalf;
  997. LLVM_DEBUG(dbgs() << " "; HiHalf->dump(););
  998. unsigned FullDestReg = MRI->createVirtualRegister(&AMDGPU::VReg_64RegClass);
  999. MachineInstr *FullBase =
  1000. BuildMI(*MBB, MBBI, DL, TII->get(TargetOpcode::REG_SEQUENCE), FullDestReg)
  1001. .addReg(DestSub0)
  1002. .addImm(AMDGPU::sub0)
  1003. .addReg(DestSub1)
  1004. .addImm(AMDGPU::sub1);
  1005. (void)FullBase;
  1006. LLVM_DEBUG(dbgs() << " "; FullBase->dump(); dbgs() << "\n";);
  1007. return FullDestReg;
  1008. }
  1009. // Update base and offset with the NewBase and NewOffset in MI.
  1010. void SILoadStoreOptimizer::updateBaseAndOffset(MachineInstr &MI,
  1011. unsigned NewBase,
  1012. int32_t NewOffset) {
  1013. TII->getNamedOperand(MI, AMDGPU::OpName::vaddr)->setReg(NewBase);
  1014. TII->getNamedOperand(MI, AMDGPU::OpName::offset)->setImm(NewOffset);
  1015. }
  1016. Optional<int32_t>
  1017. SILoadStoreOptimizer::extractConstOffset(const MachineOperand &Op) {
  1018. if (Op.isImm())
  1019. return Op.getImm();
  1020. if (!Op.isReg())
  1021. return None;
  1022. MachineInstr *Def = MRI->getUniqueVRegDef(Op.getReg());
  1023. if (!Def || Def->getOpcode() != AMDGPU::S_MOV_B32 ||
  1024. !Def->getOperand(1).isImm())
  1025. return None;
  1026. return Def->getOperand(1).getImm();
  1027. }
  1028. // Analyze Base and extracts:
  1029. // - 32bit base registers, subregisters
  1030. // - 64bit constant offset
  1031. // Expecting base computation as:
  1032. // %OFFSET0:sgpr_32 = S_MOV_B32 8000
  1033. // %LO:vgpr_32, %c:sreg_64_xexec =
  1034. // V_ADD_I32_e64 %BASE_LO:vgpr_32, %103:sgpr_32,
  1035. // %HI:vgpr_32, = V_ADDC_U32_e64 %BASE_HI:vgpr_32, 0, killed %c:sreg_64_xexec
  1036. // %Base:vreg_64 =
  1037. // REG_SEQUENCE %LO:vgpr_32, %subreg.sub0, %HI:vgpr_32, %subreg.sub1
  1038. void SILoadStoreOptimizer::processBaseWithConstOffset(const MachineOperand &Base,
  1039. MemAddress &Addr) {
  1040. if (!Base.isReg())
  1041. return;
  1042. MachineInstr *Def = MRI->getUniqueVRegDef(Base.getReg());
  1043. if (!Def || Def->getOpcode() != AMDGPU::REG_SEQUENCE
  1044. || Def->getNumOperands() != 5)
  1045. return;
  1046. MachineOperand BaseLo = Def->getOperand(1);
  1047. MachineOperand BaseHi = Def->getOperand(3);
  1048. if (!BaseLo.isReg() || !BaseHi.isReg())
  1049. return;
  1050. MachineInstr *BaseLoDef = MRI->getUniqueVRegDef(BaseLo.getReg());
  1051. MachineInstr *BaseHiDef = MRI->getUniqueVRegDef(BaseHi.getReg());
  1052. if (!BaseLoDef || BaseLoDef->getOpcode() != AMDGPU::V_ADD_I32_e64 ||
  1053. !BaseHiDef || BaseHiDef->getOpcode() != AMDGPU::V_ADDC_U32_e64)
  1054. return;
  1055. const auto *Src0 = TII->getNamedOperand(*BaseLoDef, AMDGPU::OpName::src0);
  1056. const auto *Src1 = TII->getNamedOperand(*BaseLoDef, AMDGPU::OpName::src1);
  1057. auto Offset0P = extractConstOffset(*Src0);
  1058. if (Offset0P)
  1059. BaseLo = *Src1;
  1060. else {
  1061. if (!(Offset0P = extractConstOffset(*Src1)))
  1062. return;
  1063. BaseLo = *Src0;
  1064. }
  1065. Src0 = TII->getNamedOperand(*BaseHiDef, AMDGPU::OpName::src0);
  1066. Src1 = TII->getNamedOperand(*BaseHiDef, AMDGPU::OpName::src1);
  1067. if (Src0->isImm())
  1068. std::swap(Src0, Src1);
  1069. if (!Src1->isImm())
  1070. return;
  1071. uint64_t Offset1 = Src1->getImm();
  1072. BaseHi = *Src0;
  1073. Addr.Base.LoReg = BaseLo.getReg();
  1074. Addr.Base.HiReg = BaseHi.getReg();
  1075. Addr.Base.LoSubReg = BaseLo.getSubReg();
  1076. Addr.Base.HiSubReg = BaseHi.getSubReg();
  1077. Addr.Offset = (*Offset0P & 0x00000000ffffffff) | (Offset1 << 32);
  1078. }
  1079. bool SILoadStoreOptimizer::promoteConstantOffsetToImm(
  1080. MachineInstr &MI,
  1081. MemInfoMap &Visited,
  1082. SmallPtrSet<MachineInstr *, 4> &AnchorList) {
  1083. // TODO: Support flat and scratch.
  1084. if (AMDGPU::getGlobalSaddrOp(MI.getOpcode()) < 0 ||
  1085. TII->getNamedOperand(MI, AMDGPU::OpName::vdata) != NULL)
  1086. return false;
  1087. // TODO: Support Store.
  1088. if (!MI.mayLoad())
  1089. return false;
  1090. if (AnchorList.count(&MI))
  1091. return false;
  1092. LLVM_DEBUG(dbgs() << "\nTryToPromoteConstantOffsetToImmFor "; MI.dump());
  1093. if (TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm()) {
  1094. LLVM_DEBUG(dbgs() << " Const-offset is already promoted.\n";);
  1095. return false;
  1096. }
  1097. // Step1: Find the base-registers and a 64bit constant offset.
  1098. MachineOperand &Base = *TII->getNamedOperand(MI, AMDGPU::OpName::vaddr);
  1099. MemAddress MAddr;
  1100. if (Visited.find(&MI) == Visited.end()) {
  1101. processBaseWithConstOffset(Base, MAddr);
  1102. Visited[&MI] = MAddr;
  1103. } else
  1104. MAddr = Visited[&MI];
  1105. if (MAddr.Offset == 0) {
  1106. LLVM_DEBUG(dbgs() << " Failed to extract constant-offset or there are no"
  1107. " constant offsets that can be promoted.\n";);
  1108. return false;
  1109. }
  1110. LLVM_DEBUG(dbgs() << " BASE: {" << MAddr.Base.HiReg << ", "
  1111. << MAddr.Base.LoReg << "} Offset: " << MAddr.Offset << "\n\n";);
  1112. // Step2: Traverse through MI's basic block and find an anchor(that has the
  1113. // same base-registers) with the highest 13bit distance from MI's offset.
  1114. // E.g. (64bit loads)
  1115. // bb:
  1116. // addr1 = &a + 4096; load1 = load(addr1, 0)
  1117. // addr2 = &a + 6144; load2 = load(addr2, 0)
  1118. // addr3 = &a + 8192; load3 = load(addr3, 0)
  1119. // addr4 = &a + 10240; load4 = load(addr4, 0)
  1120. // addr5 = &a + 12288; load5 = load(addr5, 0)
  1121. //
  1122. // Starting from the first load, the optimization will try to find a new base
  1123. // from which (&a + 4096) has 13 bit distance. Both &a + 6144 and &a + 8192
  1124. // has 13bit distance from &a + 4096. The heuristic considers &a + 8192
  1125. // as the new-base(anchor) because of the maximum distance which can
  1126. // accomodate more intermediate bases presumeably.
  1127. //
  1128. // Step3: move (&a + 8192) above load1. Compute and promote offsets from
  1129. // (&a + 8192) for load1, load2, load4.
  1130. // addr = &a + 8192
  1131. // load1 = load(addr, -4096)
  1132. // load2 = load(addr, -2048)
  1133. // load3 = load(addr, 0)
  1134. // load4 = load(addr, 2048)
  1135. // addr5 = &a + 12288; load5 = load(addr5, 0)
  1136. //
  1137. MachineInstr *AnchorInst = nullptr;
  1138. MemAddress AnchorAddr;
  1139. uint32_t MaxDist = std::numeric_limits<uint32_t>::min();
  1140. SmallVector<std::pair<MachineInstr *, int64_t>, 4> InstsWCommonBase;
  1141. MachineBasicBlock *MBB = MI.getParent();
  1142. MachineBasicBlock::iterator E = MBB->end();
  1143. MachineBasicBlock::iterator MBBI = MI.getIterator();
  1144. ++MBBI;
  1145. const SITargetLowering *TLI =
  1146. static_cast<const SITargetLowering *>(STM->getTargetLowering());
  1147. for ( ; MBBI != E; ++MBBI) {
  1148. MachineInstr &MINext = *MBBI;
  1149. // TODO: Support finding an anchor(with same base) from store addresses or
  1150. // any other load addresses where the opcodes are different.
  1151. if (MINext.getOpcode() != MI.getOpcode() ||
  1152. TII->getNamedOperand(MINext, AMDGPU::OpName::offset)->getImm())
  1153. continue;
  1154. const MachineOperand &BaseNext =
  1155. *TII->getNamedOperand(MINext, AMDGPU::OpName::vaddr);
  1156. MemAddress MAddrNext;
  1157. if (Visited.find(&MINext) == Visited.end()) {
  1158. processBaseWithConstOffset(BaseNext, MAddrNext);
  1159. Visited[&MINext] = MAddrNext;
  1160. } else
  1161. MAddrNext = Visited[&MINext];
  1162. if (MAddrNext.Base.LoReg != MAddr.Base.LoReg ||
  1163. MAddrNext.Base.HiReg != MAddr.Base.HiReg ||
  1164. MAddrNext.Base.LoSubReg != MAddr.Base.LoSubReg ||
  1165. MAddrNext.Base.HiSubReg != MAddr.Base.HiSubReg)
  1166. continue;
  1167. InstsWCommonBase.push_back(std::make_pair(&MINext, MAddrNext.Offset));
  1168. int64_t Dist = MAddr.Offset - MAddrNext.Offset;
  1169. TargetLoweringBase::AddrMode AM;
  1170. AM.HasBaseReg = true;
  1171. AM.BaseOffs = Dist;
  1172. if (TLI->isLegalGlobalAddressingMode(AM) &&
  1173. (uint32_t)std::abs(Dist) > MaxDist) {
  1174. MaxDist = std::abs(Dist);
  1175. AnchorAddr = MAddrNext;
  1176. AnchorInst = &MINext;
  1177. }
  1178. }
  1179. if (AnchorInst) {
  1180. LLVM_DEBUG(dbgs() << " Anchor-Inst(with max-distance from Offset): ";
  1181. AnchorInst->dump());
  1182. LLVM_DEBUG(dbgs() << " Anchor-Offset from BASE: "
  1183. << AnchorAddr.Offset << "\n\n");
  1184. // Instead of moving up, just re-compute anchor-instruction's base address.
  1185. unsigned Base = computeBase(MI, AnchorAddr);
  1186. updateBaseAndOffset(MI, Base, MAddr.Offset - AnchorAddr.Offset);
  1187. LLVM_DEBUG(dbgs() << " After promotion: "; MI.dump(););
  1188. for (auto P : InstsWCommonBase) {
  1189. TargetLoweringBase::AddrMode AM;
  1190. AM.HasBaseReg = true;
  1191. AM.BaseOffs = P.second - AnchorAddr.Offset;
  1192. if (TLI->isLegalGlobalAddressingMode(AM)) {
  1193. LLVM_DEBUG(dbgs() << " Promote Offset(" << P.second;
  1194. dbgs() << ")"; P.first->dump());
  1195. updateBaseAndOffset(*P.first, Base, P.second - AnchorAddr.Offset);
  1196. LLVM_DEBUG(dbgs() << " After promotion: "; P.first->dump());
  1197. }
  1198. }
  1199. AnchorList.insert(AnchorInst);
  1200. return true;
  1201. }
  1202. return false;
  1203. }
  1204. // Scan through looking for adjacent LDS operations with constant offsets from
  1205. // the same base register. We rely on the scheduler to do the hard work of
  1206. // clustering nearby loads, and assume these are all adjacent.
  1207. bool SILoadStoreOptimizer::optimizeBlock(MachineBasicBlock &MBB) {
  1208. bool Modified = false;
  1209. // Contain the list
  1210. MemInfoMap Visited;
  1211. // Contains the list of instructions for which constant offsets are being
  1212. // promoted to the IMM.
  1213. SmallPtrSet<MachineInstr *, 4> AnchorList;
  1214. for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E;) {
  1215. MachineInstr &MI = *I;
  1216. if (promoteConstantOffsetToImm(MI, Visited, AnchorList))
  1217. Modified = true;
  1218. // Don't combine if volatile.
  1219. if (MI.hasOrderedMemoryRef()) {
  1220. ++I;
  1221. continue;
  1222. }
  1223. const unsigned Opc = MI.getOpcode();
  1224. CombineInfo CI;
  1225. CI.I = I;
  1226. CI.InstClass = getInstClass(Opc);
  1227. switch (CI.InstClass) {
  1228. default:
  1229. break;
  1230. case DS_READ:
  1231. CI.EltSize =
  1232. (Opc == AMDGPU::DS_READ_B64 || Opc == AMDGPU::DS_READ_B64_gfx9) ? 8
  1233. : 4;
  1234. if (findMatchingInst(CI)) {
  1235. Modified = true;
  1236. I = mergeRead2Pair(CI);
  1237. } else {
  1238. ++I;
  1239. }
  1240. continue;
  1241. case DS_WRITE:
  1242. CI.EltSize =
  1243. (Opc == AMDGPU::DS_WRITE_B64 || Opc == AMDGPU::DS_WRITE_B64_gfx9) ? 8
  1244. : 4;
  1245. if (findMatchingInst(CI)) {
  1246. Modified = true;
  1247. I = mergeWrite2Pair(CI);
  1248. } else {
  1249. ++I;
  1250. }
  1251. continue;
  1252. case S_BUFFER_LOAD_IMM:
  1253. CI.EltSize = AMDGPU::getSMRDEncodedOffset(*STM, 4);
  1254. if (findMatchingInst(CI)) {
  1255. Modified = true;
  1256. I = mergeSBufferLoadImmPair(CI);
  1257. OptimizeAgain |= (CI.Width0 + CI.Width1) < 16;
  1258. } else {
  1259. ++I;
  1260. }
  1261. continue;
  1262. case BUFFER_LOAD_OFFEN:
  1263. case BUFFER_LOAD_OFFSET:
  1264. case BUFFER_LOAD_OFFEN_exact:
  1265. case BUFFER_LOAD_OFFSET_exact:
  1266. CI.EltSize = 4;
  1267. if (findMatchingInst(CI)) {
  1268. Modified = true;
  1269. I = mergeBufferLoadPair(CI);
  1270. OptimizeAgain |= (CI.Width0 + CI.Width1) < 4;
  1271. } else {
  1272. ++I;
  1273. }
  1274. continue;
  1275. case BUFFER_STORE_OFFEN:
  1276. case BUFFER_STORE_OFFSET:
  1277. case BUFFER_STORE_OFFEN_exact:
  1278. case BUFFER_STORE_OFFSET_exact:
  1279. CI.EltSize = 4;
  1280. if (findMatchingInst(CI)) {
  1281. Modified = true;
  1282. I = mergeBufferStorePair(CI);
  1283. OptimizeAgain |= (CI.Width0 + CI.Width1) < 4;
  1284. } else {
  1285. ++I;
  1286. }
  1287. continue;
  1288. }
  1289. ++I;
  1290. }
  1291. return Modified;
  1292. }
  1293. bool SILoadStoreOptimizer::runOnMachineFunction(MachineFunction &MF) {
  1294. if (skipFunction(MF.getFunction()))
  1295. return false;
  1296. STM = &MF.getSubtarget<GCNSubtarget>();
  1297. if (!STM->loadStoreOptEnabled())
  1298. return false;
  1299. TII = STM->getInstrInfo();
  1300. TRI = &TII->getRegisterInfo();
  1301. MRI = &MF.getRegInfo();
  1302. AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
  1303. assert(MRI->isSSA() && "Must be run on SSA");
  1304. LLVM_DEBUG(dbgs() << "Running SILoadStoreOptimizer\n");
  1305. bool Modified = false;
  1306. for (MachineBasicBlock &MBB : MF) {
  1307. do {
  1308. OptimizeAgain = false;
  1309. Modified |= optimizeBlock(MBB);
  1310. } while (OptimizeAgain);
  1311. }
  1312. return Modified;
  1313. }