A15SDOptimizer.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. //=== A15SDOptimizerPass.cpp - Optimize DPR and SPR register accesses on A15==//
  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. // The Cortex-A15 processor employs a tracking scheme in its register renaming
  10. // in order to process each instruction's micro-ops speculatively and
  11. // out-of-order with appropriate forwarding. The ARM architecture allows VFP
  12. // instructions to read and write 32-bit S-registers. Each S-register
  13. // corresponds to one half (upper or lower) of an overlaid 64-bit D-register.
  14. //
  15. // There are several instruction patterns which can be used to provide this
  16. // capability which can provide higher performance than other, potentially more
  17. // direct patterns, specifically around when one micro-op reads a D-register
  18. // operand that has recently been written as one or more S-register results.
  19. //
  20. // This file defines a pre-regalloc pass which looks for SPR producers which
  21. // are going to be used by a DPR (or QPR) consumers and creates the more
  22. // optimized access pattern.
  23. //
  24. //===----------------------------------------------------------------------===//
  25. #include "ARM.h"
  26. #include "ARMBaseInstrInfo.h"
  27. #include "ARMBaseRegisterInfo.h"
  28. #include "ARMSubtarget.h"
  29. #include "llvm/ADT/Statistic.h"
  30. #include "llvm/CodeGen/MachineFunction.h"
  31. #include "llvm/CodeGen/MachineFunctionPass.h"
  32. #include "llvm/CodeGen/MachineInstr.h"
  33. #include "llvm/CodeGen/MachineInstrBuilder.h"
  34. #include "llvm/CodeGen/MachineRegisterInfo.h"
  35. #include "llvm/CodeGen/TargetRegisterInfo.h"
  36. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  37. #include "llvm/Support/Debug.h"
  38. #include "llvm/Support/raw_ostream.h"
  39. #include <map>
  40. #include <set>
  41. using namespace llvm;
  42. #define DEBUG_TYPE "a15-sd-optimizer"
  43. namespace {
  44. struct A15SDOptimizer : public MachineFunctionPass {
  45. static char ID;
  46. A15SDOptimizer() : MachineFunctionPass(ID) {}
  47. bool runOnMachineFunction(MachineFunction &Fn) override;
  48. StringRef getPassName() const override { return "ARM A15 S->D optimizer"; }
  49. private:
  50. const ARMBaseInstrInfo *TII;
  51. const TargetRegisterInfo *TRI;
  52. MachineRegisterInfo *MRI;
  53. bool runOnInstruction(MachineInstr *MI);
  54. //
  55. // Instruction builder helpers
  56. //
  57. unsigned createDupLane(MachineBasicBlock &MBB,
  58. MachineBasicBlock::iterator InsertBefore,
  59. const DebugLoc &DL, unsigned Reg, unsigned Lane,
  60. bool QPR = false);
  61. unsigned createExtractSubreg(MachineBasicBlock &MBB,
  62. MachineBasicBlock::iterator InsertBefore,
  63. const DebugLoc &DL, unsigned DReg,
  64. unsigned Lane, const TargetRegisterClass *TRC);
  65. unsigned createVExt(MachineBasicBlock &MBB,
  66. MachineBasicBlock::iterator InsertBefore,
  67. const DebugLoc &DL, unsigned Ssub0, unsigned Ssub1);
  68. unsigned createRegSequence(MachineBasicBlock &MBB,
  69. MachineBasicBlock::iterator InsertBefore,
  70. const DebugLoc &DL, unsigned Reg1,
  71. unsigned Reg2);
  72. unsigned createInsertSubreg(MachineBasicBlock &MBB,
  73. MachineBasicBlock::iterator InsertBefore,
  74. const DebugLoc &DL, unsigned DReg,
  75. unsigned Lane, unsigned ToInsert);
  76. unsigned createImplicitDef(MachineBasicBlock &MBB,
  77. MachineBasicBlock::iterator InsertBefore,
  78. const DebugLoc &DL);
  79. //
  80. // Various property checkers
  81. //
  82. bool usesRegClass(MachineOperand &MO, const TargetRegisterClass *TRC);
  83. bool hasPartialWrite(MachineInstr *MI);
  84. SmallVector<unsigned, 8> getReadDPRs(MachineInstr *MI);
  85. unsigned getDPRLaneFromSPR(unsigned SReg);
  86. //
  87. // Methods used for getting the definitions of partial registers
  88. //
  89. MachineInstr *elideCopies(MachineInstr *MI);
  90. void elideCopiesAndPHIs(MachineInstr *MI,
  91. SmallVectorImpl<MachineInstr*> &Outs);
  92. //
  93. // Pattern optimization methods
  94. //
  95. unsigned optimizeAllLanesPattern(MachineInstr *MI, unsigned Reg);
  96. unsigned optimizeSDPattern(MachineInstr *MI);
  97. unsigned getPrefSPRLane(unsigned SReg);
  98. //
  99. // Sanitizing method - used to make sure if don't leave dead code around.
  100. //
  101. void eraseInstrWithNoUses(MachineInstr *MI);
  102. //
  103. // A map used to track the changes done by this pass.
  104. //
  105. std::map<MachineInstr*, unsigned> Replacements;
  106. std::set<MachineInstr *> DeadInstr;
  107. };
  108. char A15SDOptimizer::ID = 0;
  109. } // end anonymous namespace
  110. // Returns true if this is a use of a SPR register.
  111. bool A15SDOptimizer::usesRegClass(MachineOperand &MO,
  112. const TargetRegisterClass *TRC) {
  113. if (!MO.isReg())
  114. return false;
  115. Register Reg = MO.getReg();
  116. if (Reg.isVirtual())
  117. return MRI->getRegClass(Reg)->hasSuperClassEq(TRC);
  118. else
  119. return TRC->contains(Reg);
  120. }
  121. unsigned A15SDOptimizer::getDPRLaneFromSPR(unsigned SReg) {
  122. unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1,
  123. &ARM::DPRRegClass);
  124. if (DReg != ARM::NoRegister) return ARM::ssub_1;
  125. return ARM::ssub_0;
  126. }
  127. // Get the subreg type that is most likely to be coalesced
  128. // for an SPR register that will be used in VDUP32d pseudo.
  129. unsigned A15SDOptimizer::getPrefSPRLane(unsigned SReg) {
  130. if (!Register::isVirtualRegister(SReg))
  131. return getDPRLaneFromSPR(SReg);
  132. MachineInstr *MI = MRI->getVRegDef(SReg);
  133. if (!MI) return ARM::ssub_0;
  134. MachineOperand *MO = MI->findRegisterDefOperand(SReg);
  135. if (!MO) return ARM::ssub_0;
  136. assert(MO->isReg() && "Non-register operand found!");
  137. if (MI->isCopy() && usesRegClass(MI->getOperand(1),
  138. &ARM::SPRRegClass)) {
  139. SReg = MI->getOperand(1).getReg();
  140. }
  141. if (Register::isVirtualRegister(SReg)) {
  142. if (MO->getSubReg() == ARM::ssub_1) return ARM::ssub_1;
  143. return ARM::ssub_0;
  144. }
  145. return getDPRLaneFromSPR(SReg);
  146. }
  147. // MI is known to be dead. Figure out what instructions
  148. // are also made dead by this and mark them for removal.
  149. void A15SDOptimizer::eraseInstrWithNoUses(MachineInstr *MI) {
  150. SmallVector<MachineInstr *, 8> Front;
  151. DeadInstr.insert(MI);
  152. LLVM_DEBUG(dbgs() << "Deleting base instruction " << *MI << "\n");
  153. Front.push_back(MI);
  154. while (Front.size() != 0) {
  155. MI = Front.pop_back_val();
  156. // MI is already known to be dead. We need to see
  157. // if other instructions can also be removed.
  158. for (MachineOperand &MO : MI->operands()) {
  159. if ((!MO.isReg()) || (!MO.isUse()))
  160. continue;
  161. Register Reg = MO.getReg();
  162. if (!Reg.isVirtual())
  163. continue;
  164. MachineOperand *Op = MI->findRegisterDefOperand(Reg);
  165. if (!Op)
  166. continue;
  167. MachineInstr *Def = Op->getParent();
  168. // We don't need to do anything if we have already marked
  169. // this instruction as being dead.
  170. if (DeadInstr.find(Def) != DeadInstr.end())
  171. continue;
  172. // Check if all the uses of this instruction are marked as
  173. // dead. If so, we can also mark this instruction as being
  174. // dead.
  175. bool IsDead = true;
  176. for (MachineOperand &MODef : Def->operands()) {
  177. if ((!MODef.isReg()) || (!MODef.isDef()))
  178. continue;
  179. Register DefReg = MODef.getReg();
  180. if (!DefReg.isVirtual()) {
  181. IsDead = false;
  182. break;
  183. }
  184. for (MachineInstr &Use : MRI->use_instructions(Reg)) {
  185. // We don't care about self references.
  186. if (&Use == Def)
  187. continue;
  188. if (DeadInstr.find(&Use) == DeadInstr.end()) {
  189. IsDead = false;
  190. break;
  191. }
  192. }
  193. }
  194. if (!IsDead) continue;
  195. LLVM_DEBUG(dbgs() << "Deleting instruction " << *Def << "\n");
  196. DeadInstr.insert(Def);
  197. }
  198. }
  199. }
  200. // Creates the more optimized patterns and generally does all the code
  201. // transformations in this pass.
  202. unsigned A15SDOptimizer::optimizeSDPattern(MachineInstr *MI) {
  203. if (MI->isCopy()) {
  204. return optimizeAllLanesPattern(MI, MI->getOperand(1).getReg());
  205. }
  206. if (MI->isInsertSubreg()) {
  207. Register DPRReg = MI->getOperand(1).getReg();
  208. Register SPRReg = MI->getOperand(2).getReg();
  209. if (DPRReg.isVirtual() && SPRReg.isVirtual()) {
  210. MachineInstr *DPRMI = MRI->getVRegDef(MI->getOperand(1).getReg());
  211. MachineInstr *SPRMI = MRI->getVRegDef(MI->getOperand(2).getReg());
  212. if (DPRMI && SPRMI) {
  213. // See if the first operand of this insert_subreg is IMPLICIT_DEF
  214. MachineInstr *ECDef = elideCopies(DPRMI);
  215. if (ECDef && ECDef->isImplicitDef()) {
  216. // Another corner case - if we're inserting something that is purely
  217. // a subreg copy of a DPR, just use that DPR.
  218. MachineInstr *EC = elideCopies(SPRMI);
  219. // Is it a subreg copy of ssub_0?
  220. if (EC && EC->isCopy() &&
  221. EC->getOperand(1).getSubReg() == ARM::ssub_0) {
  222. LLVM_DEBUG(dbgs() << "Found a subreg copy: " << *SPRMI);
  223. // Find the thing we're subreg copying out of - is it of the same
  224. // regclass as DPRMI? (i.e. a DPR or QPR).
  225. Register FullReg = SPRMI->getOperand(1).getReg();
  226. const TargetRegisterClass *TRC =
  227. MRI->getRegClass(MI->getOperand(1).getReg());
  228. if (TRC->hasSuperClassEq(MRI->getRegClass(FullReg))) {
  229. LLVM_DEBUG(dbgs() << "Subreg copy is compatible - returning ");
  230. LLVM_DEBUG(dbgs() << printReg(FullReg) << "\n");
  231. eraseInstrWithNoUses(MI);
  232. return FullReg;
  233. }
  234. }
  235. return optimizeAllLanesPattern(MI, MI->getOperand(2).getReg());
  236. }
  237. }
  238. }
  239. return optimizeAllLanesPattern(MI, MI->getOperand(0).getReg());
  240. }
  241. if (MI->isRegSequence() && usesRegClass(MI->getOperand(1),
  242. &ARM::SPRRegClass)) {
  243. // See if all bar one of the operands are IMPLICIT_DEF and insert the
  244. // optimizer pattern accordingly.
  245. unsigned NumImplicit = 0, NumTotal = 0;
  246. unsigned NonImplicitReg = ~0U;
  247. for (MachineOperand &MO : llvm::drop_begin(MI->explicit_operands())) {
  248. if (!MO.isReg())
  249. continue;
  250. ++NumTotal;
  251. Register OpReg = MO.getReg();
  252. if (!OpReg.isVirtual())
  253. break;
  254. MachineInstr *Def = MRI->getVRegDef(OpReg);
  255. if (!Def)
  256. break;
  257. if (Def->isImplicitDef())
  258. ++NumImplicit;
  259. else
  260. NonImplicitReg = MO.getReg();
  261. }
  262. if (NumImplicit == NumTotal - 1)
  263. return optimizeAllLanesPattern(MI, NonImplicitReg);
  264. else
  265. return optimizeAllLanesPattern(MI, MI->getOperand(0).getReg());
  266. }
  267. llvm_unreachable("Unhandled update pattern!");
  268. }
  269. // Return true if this MachineInstr inserts a scalar (SPR) value into
  270. // a D or Q register.
  271. bool A15SDOptimizer::hasPartialWrite(MachineInstr *MI) {
  272. // The only way we can do a partial register update is through a COPY,
  273. // INSERT_SUBREG or REG_SEQUENCE.
  274. if (MI->isCopy() && usesRegClass(MI->getOperand(1), &ARM::SPRRegClass))
  275. return true;
  276. if (MI->isInsertSubreg() && usesRegClass(MI->getOperand(2),
  277. &ARM::SPRRegClass))
  278. return true;
  279. if (MI->isRegSequence() && usesRegClass(MI->getOperand(1), &ARM::SPRRegClass))
  280. return true;
  281. return false;
  282. }
  283. // Looks through full copies to get the instruction that defines the input
  284. // operand for MI.
  285. MachineInstr *A15SDOptimizer::elideCopies(MachineInstr *MI) {
  286. if (!MI->isFullCopy())
  287. return MI;
  288. if (!MI->getOperand(1).getReg().isVirtual())
  289. return nullptr;
  290. MachineInstr *Def = MRI->getVRegDef(MI->getOperand(1).getReg());
  291. if (!Def)
  292. return nullptr;
  293. return elideCopies(Def);
  294. }
  295. // Look through full copies and PHIs to get the set of non-copy MachineInstrs
  296. // that can produce MI.
  297. void A15SDOptimizer::elideCopiesAndPHIs(MachineInstr *MI,
  298. SmallVectorImpl<MachineInstr*> &Outs) {
  299. // Looking through PHIs may create loops so we need to track what
  300. // instructions we have visited before.
  301. std::set<MachineInstr *> Reached;
  302. SmallVector<MachineInstr *, 8> Front;
  303. Front.push_back(MI);
  304. while (Front.size() != 0) {
  305. MI = Front.pop_back_val();
  306. // If we have already explored this MachineInstr, ignore it.
  307. if (!Reached.insert(MI).second)
  308. continue;
  309. if (MI->isPHI()) {
  310. for (unsigned I = 1, E = MI->getNumOperands(); I != E; I += 2) {
  311. Register Reg = MI->getOperand(I).getReg();
  312. if (!Reg.isVirtual()) {
  313. continue;
  314. }
  315. MachineInstr *NewMI = MRI->getVRegDef(Reg);
  316. if (!NewMI)
  317. continue;
  318. Front.push_back(NewMI);
  319. }
  320. } else if (MI->isFullCopy()) {
  321. if (!MI->getOperand(1).getReg().isVirtual())
  322. continue;
  323. MachineInstr *NewMI = MRI->getVRegDef(MI->getOperand(1).getReg());
  324. if (!NewMI)
  325. continue;
  326. Front.push_back(NewMI);
  327. } else {
  328. LLVM_DEBUG(dbgs() << "Found partial copy" << *MI << "\n");
  329. Outs.push_back(MI);
  330. }
  331. }
  332. }
  333. // Return the DPR virtual registers that are read by this machine instruction
  334. // (if any).
  335. SmallVector<unsigned, 8> A15SDOptimizer::getReadDPRs(MachineInstr *MI) {
  336. if (MI->isCopyLike() || MI->isInsertSubreg() || MI->isRegSequence() ||
  337. MI->isKill())
  338. return SmallVector<unsigned, 8>();
  339. SmallVector<unsigned, 8> Defs;
  340. for (MachineOperand &MO : MI->operands()) {
  341. if (!MO.isReg() || !MO.isUse())
  342. continue;
  343. if (!usesRegClass(MO, &ARM::DPRRegClass) &&
  344. !usesRegClass(MO, &ARM::QPRRegClass) &&
  345. !usesRegClass(MO, &ARM::DPairRegClass)) // Treat DPair as QPR
  346. continue;
  347. Defs.push_back(MO.getReg());
  348. }
  349. return Defs;
  350. }
  351. // Creates a DPR register from an SPR one by using a VDUP.
  352. unsigned A15SDOptimizer::createDupLane(MachineBasicBlock &MBB,
  353. MachineBasicBlock::iterator InsertBefore,
  354. const DebugLoc &DL, unsigned Reg,
  355. unsigned Lane, bool QPR) {
  356. Register Out =
  357. MRI->createVirtualRegister(QPR ? &ARM::QPRRegClass : &ARM::DPRRegClass);
  358. BuildMI(MBB, InsertBefore, DL,
  359. TII->get(QPR ? ARM::VDUPLN32q : ARM::VDUPLN32d), Out)
  360. .addReg(Reg)
  361. .addImm(Lane)
  362. .add(predOps(ARMCC::AL));
  363. return Out;
  364. }
  365. // Creates a SPR register from a DPR by copying the value in lane 0.
  366. unsigned A15SDOptimizer::createExtractSubreg(
  367. MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
  368. const DebugLoc &DL, unsigned DReg, unsigned Lane,
  369. const TargetRegisterClass *TRC) {
  370. Register Out = MRI->createVirtualRegister(TRC);
  371. BuildMI(MBB,
  372. InsertBefore,
  373. DL,
  374. TII->get(TargetOpcode::COPY), Out)
  375. .addReg(DReg, 0, Lane);
  376. return Out;
  377. }
  378. // Takes two SPR registers and creates a DPR by using a REG_SEQUENCE.
  379. unsigned A15SDOptimizer::createRegSequence(
  380. MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
  381. const DebugLoc &DL, unsigned Reg1, unsigned Reg2) {
  382. Register Out = MRI->createVirtualRegister(&ARM::QPRRegClass);
  383. BuildMI(MBB,
  384. InsertBefore,
  385. DL,
  386. TII->get(TargetOpcode::REG_SEQUENCE), Out)
  387. .addReg(Reg1)
  388. .addImm(ARM::dsub_0)
  389. .addReg(Reg2)
  390. .addImm(ARM::dsub_1);
  391. return Out;
  392. }
  393. // Takes two DPR registers that have previously been VDUPed (Ssub0 and Ssub1)
  394. // and merges them into one DPR register.
  395. unsigned A15SDOptimizer::createVExt(MachineBasicBlock &MBB,
  396. MachineBasicBlock::iterator InsertBefore,
  397. const DebugLoc &DL, unsigned Ssub0,
  398. unsigned Ssub1) {
  399. Register Out = MRI->createVirtualRegister(&ARM::DPRRegClass);
  400. BuildMI(MBB, InsertBefore, DL, TII->get(ARM::VEXTd32), Out)
  401. .addReg(Ssub0)
  402. .addReg(Ssub1)
  403. .addImm(1)
  404. .add(predOps(ARMCC::AL));
  405. return Out;
  406. }
  407. unsigned A15SDOptimizer::createInsertSubreg(
  408. MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
  409. const DebugLoc &DL, unsigned DReg, unsigned Lane, unsigned ToInsert) {
  410. Register Out = MRI->createVirtualRegister(&ARM::DPR_VFP2RegClass);
  411. BuildMI(MBB,
  412. InsertBefore,
  413. DL,
  414. TII->get(TargetOpcode::INSERT_SUBREG), Out)
  415. .addReg(DReg)
  416. .addReg(ToInsert)
  417. .addImm(Lane);
  418. return Out;
  419. }
  420. unsigned
  421. A15SDOptimizer::createImplicitDef(MachineBasicBlock &MBB,
  422. MachineBasicBlock::iterator InsertBefore,
  423. const DebugLoc &DL) {
  424. Register Out = MRI->createVirtualRegister(&ARM::DPRRegClass);
  425. BuildMI(MBB,
  426. InsertBefore,
  427. DL,
  428. TII->get(TargetOpcode::IMPLICIT_DEF), Out);
  429. return Out;
  430. }
  431. // This function inserts instructions in order to optimize interactions between
  432. // SPR registers and DPR/QPR registers. It does so by performing VDUPs on all
  433. // lanes, and the using VEXT instructions to recompose the result.
  434. unsigned
  435. A15SDOptimizer::optimizeAllLanesPattern(MachineInstr *MI, unsigned Reg) {
  436. MachineBasicBlock::iterator InsertPt(MI);
  437. DebugLoc DL = MI->getDebugLoc();
  438. MachineBasicBlock &MBB = *MI->getParent();
  439. InsertPt++;
  440. unsigned Out;
  441. // DPair has the same length as QPR and also has two DPRs as subreg.
  442. // Treat DPair as QPR.
  443. if (MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::QPRRegClass) ||
  444. MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::DPairRegClass)) {
  445. unsigned DSub0 = createExtractSubreg(MBB, InsertPt, DL, Reg,
  446. ARM::dsub_0, &ARM::DPRRegClass);
  447. unsigned DSub1 = createExtractSubreg(MBB, InsertPt, DL, Reg,
  448. ARM::dsub_1, &ARM::DPRRegClass);
  449. unsigned Out1 = createDupLane(MBB, InsertPt, DL, DSub0, 0);
  450. unsigned Out2 = createDupLane(MBB, InsertPt, DL, DSub0, 1);
  451. Out = createVExt(MBB, InsertPt, DL, Out1, Out2);
  452. unsigned Out3 = createDupLane(MBB, InsertPt, DL, DSub1, 0);
  453. unsigned Out4 = createDupLane(MBB, InsertPt, DL, DSub1, 1);
  454. Out2 = createVExt(MBB, InsertPt, DL, Out3, Out4);
  455. Out = createRegSequence(MBB, InsertPt, DL, Out, Out2);
  456. } else if (MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::DPRRegClass)) {
  457. unsigned Out1 = createDupLane(MBB, InsertPt, DL, Reg, 0);
  458. unsigned Out2 = createDupLane(MBB, InsertPt, DL, Reg, 1);
  459. Out = createVExt(MBB, InsertPt, DL, Out1, Out2);
  460. } else {
  461. assert(MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::SPRRegClass) &&
  462. "Found unexpected regclass!");
  463. unsigned PrefLane = getPrefSPRLane(Reg);
  464. unsigned Lane;
  465. switch (PrefLane) {
  466. case ARM::ssub_0: Lane = 0; break;
  467. case ARM::ssub_1: Lane = 1; break;
  468. default: llvm_unreachable("Unknown preferred lane!");
  469. }
  470. // Treat DPair as QPR
  471. bool UsesQPR = usesRegClass(MI->getOperand(0), &ARM::QPRRegClass) ||
  472. usesRegClass(MI->getOperand(0), &ARM::DPairRegClass);
  473. Out = createImplicitDef(MBB, InsertPt, DL);
  474. Out = createInsertSubreg(MBB, InsertPt, DL, Out, PrefLane, Reg);
  475. Out = createDupLane(MBB, InsertPt, DL, Out, Lane, UsesQPR);
  476. eraseInstrWithNoUses(MI);
  477. }
  478. return Out;
  479. }
  480. bool A15SDOptimizer::runOnInstruction(MachineInstr *MI) {
  481. // We look for instructions that write S registers that are then read as
  482. // D/Q registers. These can only be caused by COPY, INSERT_SUBREG and
  483. // REG_SEQUENCE pseudos that insert an SPR value into a DPR register or
  484. // merge two SPR values to form a DPR register. In order avoid false
  485. // positives we make sure that there is an SPR producer so we look past
  486. // COPY and PHI nodes to find it.
  487. //
  488. // The best code pattern for when an SPR producer is going to be used by a
  489. // DPR or QPR consumer depends on whether the other lanes of the
  490. // corresponding DPR/QPR are currently defined.
  491. //
  492. // We can handle these efficiently, depending on the type of
  493. // pseudo-instruction that is producing the pattern
  494. //
  495. // * COPY: * VDUP all lanes and merge the results together
  496. // using VEXTs.
  497. //
  498. // * INSERT_SUBREG: * If the SPR value was originally in another DPR/QPR
  499. // lane, and the other lane(s) of the DPR/QPR register
  500. // that we are inserting in are undefined, use the
  501. // original DPR/QPR value.
  502. // * Otherwise, fall back on the same stategy as COPY.
  503. //
  504. // * REG_SEQUENCE: * If all except one of the input operands are
  505. // IMPLICIT_DEFs, insert the VDUP pattern for just the
  506. // defined input operand
  507. // * Otherwise, fall back on the same stategy as COPY.
  508. //
  509. // First, get all the reads of D-registers done by this instruction.
  510. SmallVector<unsigned, 8> Defs = getReadDPRs(MI);
  511. bool Modified = false;
  512. for (unsigned I : Defs) {
  513. // Follow the def-use chain for this DPR through COPYs, and also through
  514. // PHIs (which are essentially multi-way COPYs). It is because of PHIs that
  515. // we can end up with multiple defs of this DPR.
  516. SmallVector<MachineInstr *, 8> DefSrcs;
  517. if (!Register::isVirtualRegister(I))
  518. continue;
  519. MachineInstr *Def = MRI->getVRegDef(I);
  520. if (!Def)
  521. continue;
  522. elideCopiesAndPHIs(Def, DefSrcs);
  523. for (MachineInstr *MI : DefSrcs) {
  524. // If we've already analyzed and replaced this operand, don't do
  525. // anything.
  526. if (Replacements.find(MI) != Replacements.end())
  527. continue;
  528. // Now, work out if the instruction causes a SPR->DPR dependency.
  529. if (!hasPartialWrite(MI))
  530. continue;
  531. // Collect all the uses of this MI's DPR def for updating later.
  532. SmallVector<MachineOperand*, 8> Uses;
  533. Register DPRDefReg = MI->getOperand(0).getReg();
  534. for (MachineOperand &MO : MRI->use_operands(DPRDefReg))
  535. Uses.push_back(&MO);
  536. // We can optimize this.
  537. unsigned NewReg = optimizeSDPattern(MI);
  538. if (NewReg != 0) {
  539. Modified = true;
  540. for (MachineOperand *Use : Uses) {
  541. // Make sure to constrain the register class of the new register to
  542. // match what we're replacing. Otherwise we can optimize a DPR_VFP2
  543. // reference into a plain DPR, and that will end poorly. NewReg is
  544. // always virtual here, so there will always be a matching subclass
  545. // to find.
  546. MRI->constrainRegClass(NewReg, MRI->getRegClass(Use->getReg()));
  547. LLVM_DEBUG(dbgs() << "Replacing operand " << *Use << " with "
  548. << printReg(NewReg) << "\n");
  549. Use->substVirtReg(NewReg, 0, *TRI);
  550. }
  551. }
  552. Replacements[MI] = NewReg;
  553. }
  554. }
  555. return Modified;
  556. }
  557. bool A15SDOptimizer::runOnMachineFunction(MachineFunction &Fn) {
  558. if (skipFunction(Fn.getFunction()))
  559. return false;
  560. const ARMSubtarget &STI = Fn.getSubtarget<ARMSubtarget>();
  561. // Since the A15SDOptimizer pass can insert VDUP instructions, it can only be
  562. // enabled when NEON is available.
  563. if (!(STI.useSplatVFPToNeon() && STI.hasNEON()))
  564. return false;
  565. TII = STI.getInstrInfo();
  566. TRI = STI.getRegisterInfo();
  567. MRI = &Fn.getRegInfo();
  568. bool Modified = false;
  569. LLVM_DEBUG(dbgs() << "Running on function " << Fn.getName() << "\n");
  570. DeadInstr.clear();
  571. Replacements.clear();
  572. for (MachineBasicBlock &MBB : Fn) {
  573. for (MachineInstr &MI : MBB) {
  574. Modified |= runOnInstruction(&MI);
  575. }
  576. }
  577. for (MachineInstr *MI : DeadInstr) {
  578. MI->eraseFromParent();
  579. }
  580. return Modified;
  581. }
  582. FunctionPass *llvm::createA15SDOptimizerPass() {
  583. return new A15SDOptimizer();
  584. }