A15SDOptimizer.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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 (Register::isVirtualRegister(Reg))
  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 (!Register::isVirtualRegister(Reg))
  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 (!Register::isVirtualRegister(DefReg)) {
  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 (Register::isVirtualRegister(DPRReg) && Register::isVirtualRegister(SPRReg)) {
  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 (unsigned I = 1; I < MI->getNumExplicitOperands(); ++I) {
  248. if (!MI->getOperand(I).isReg())
  249. continue;
  250. ++NumTotal;
  251. Register OpReg = MI->getOperand(I).getReg();
  252. if (!Register::isVirtualRegister(OpReg))
  253. break;
  254. MachineInstr *Def = MRI->getVRegDef(OpReg);
  255. if (!Def)
  256. break;
  257. if (Def->isImplicitDef())
  258. ++NumImplicit;
  259. else
  260. NonImplicitReg = MI->getOperand(I).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 (!Register::isVirtualRegister(MI->getOperand(1).getReg()))
  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.find(MI) != Reached.end())
  308. continue;
  309. Reached.insert(MI);
  310. if (MI->isPHI()) {
  311. for (unsigned I = 1, E = MI->getNumOperands(); I != E; I += 2) {
  312. Register Reg = MI->getOperand(I).getReg();
  313. if (!Register::isVirtualRegister(Reg)) {
  314. continue;
  315. }
  316. MachineInstr *NewMI = MRI->getVRegDef(Reg);
  317. if (!NewMI)
  318. continue;
  319. Front.push_back(NewMI);
  320. }
  321. } else if (MI->isFullCopy()) {
  322. if (!Register::isVirtualRegister(MI->getOperand(1).getReg()))
  323. continue;
  324. MachineInstr *NewMI = MRI->getVRegDef(MI->getOperand(1).getReg());
  325. if (!NewMI)
  326. continue;
  327. Front.push_back(NewMI);
  328. } else {
  329. LLVM_DEBUG(dbgs() << "Found partial copy" << *MI << "\n");
  330. Outs.push_back(MI);
  331. }
  332. }
  333. }
  334. // Return the DPR virtual registers that are read by this machine instruction
  335. // (if any).
  336. SmallVector<unsigned, 8> A15SDOptimizer::getReadDPRs(MachineInstr *MI) {
  337. if (MI->isCopyLike() || MI->isInsertSubreg() || MI->isRegSequence() ||
  338. MI->isKill())
  339. return SmallVector<unsigned, 8>();
  340. SmallVector<unsigned, 8> Defs;
  341. for (MachineOperand &MO : MI->operands()) {
  342. if (!MO.isReg() || !MO.isUse())
  343. continue;
  344. if (!usesRegClass(MO, &ARM::DPRRegClass) &&
  345. !usesRegClass(MO, &ARM::QPRRegClass) &&
  346. !usesRegClass(MO, &ARM::DPairRegClass)) // Treat DPair as QPR
  347. continue;
  348. Defs.push_back(MO.getReg());
  349. }
  350. return Defs;
  351. }
  352. // Creates a DPR register from an SPR one by using a VDUP.
  353. unsigned A15SDOptimizer::createDupLane(MachineBasicBlock &MBB,
  354. MachineBasicBlock::iterator InsertBefore,
  355. const DebugLoc &DL, unsigned Reg,
  356. unsigned Lane, bool QPR) {
  357. Register Out =
  358. MRI->createVirtualRegister(QPR ? &ARM::QPRRegClass : &ARM::DPRRegClass);
  359. BuildMI(MBB, InsertBefore, DL,
  360. TII->get(QPR ? ARM::VDUPLN32q : ARM::VDUPLN32d), Out)
  361. .addReg(Reg)
  362. .addImm(Lane)
  363. .add(predOps(ARMCC::AL));
  364. return Out;
  365. }
  366. // Creates a SPR register from a DPR by copying the value in lane 0.
  367. unsigned A15SDOptimizer::createExtractSubreg(
  368. MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
  369. const DebugLoc &DL, unsigned DReg, unsigned Lane,
  370. const TargetRegisterClass *TRC) {
  371. Register Out = MRI->createVirtualRegister(TRC);
  372. BuildMI(MBB,
  373. InsertBefore,
  374. DL,
  375. TII->get(TargetOpcode::COPY), Out)
  376. .addReg(DReg, 0, Lane);
  377. return Out;
  378. }
  379. // Takes two SPR registers and creates a DPR by using a REG_SEQUENCE.
  380. unsigned A15SDOptimizer::createRegSequence(
  381. MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
  382. const DebugLoc &DL, unsigned Reg1, unsigned Reg2) {
  383. Register Out = MRI->createVirtualRegister(&ARM::QPRRegClass);
  384. BuildMI(MBB,
  385. InsertBefore,
  386. DL,
  387. TII->get(TargetOpcode::REG_SEQUENCE), Out)
  388. .addReg(Reg1)
  389. .addImm(ARM::dsub_0)
  390. .addReg(Reg2)
  391. .addImm(ARM::dsub_1);
  392. return Out;
  393. }
  394. // Takes two DPR registers that have previously been VDUPed (Ssub0 and Ssub1)
  395. // and merges them into one DPR register.
  396. unsigned A15SDOptimizer::createVExt(MachineBasicBlock &MBB,
  397. MachineBasicBlock::iterator InsertBefore,
  398. const DebugLoc &DL, unsigned Ssub0,
  399. unsigned Ssub1) {
  400. Register Out = MRI->createVirtualRegister(&ARM::DPRRegClass);
  401. BuildMI(MBB, InsertBefore, DL, TII->get(ARM::VEXTd32), Out)
  402. .addReg(Ssub0)
  403. .addReg(Ssub1)
  404. .addImm(1)
  405. .add(predOps(ARMCC::AL));
  406. return Out;
  407. }
  408. unsigned A15SDOptimizer::createInsertSubreg(
  409. MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
  410. const DebugLoc &DL, unsigned DReg, unsigned Lane, unsigned ToInsert) {
  411. Register Out = MRI->createVirtualRegister(&ARM::DPR_VFP2RegClass);
  412. BuildMI(MBB,
  413. InsertBefore,
  414. DL,
  415. TII->get(TargetOpcode::INSERT_SUBREG), Out)
  416. .addReg(DReg)
  417. .addReg(ToInsert)
  418. .addImm(Lane);
  419. return Out;
  420. }
  421. unsigned
  422. A15SDOptimizer::createImplicitDef(MachineBasicBlock &MBB,
  423. MachineBasicBlock::iterator InsertBefore,
  424. const DebugLoc &DL) {
  425. Register Out = MRI->createVirtualRegister(&ARM::DPRRegClass);
  426. BuildMI(MBB,
  427. InsertBefore,
  428. DL,
  429. TII->get(TargetOpcode::IMPLICIT_DEF), Out);
  430. return Out;
  431. }
  432. // This function inserts instructions in order to optimize interactions between
  433. // SPR registers and DPR/QPR registers. It does so by performing VDUPs on all
  434. // lanes, and the using VEXT instructions to recompose the result.
  435. unsigned
  436. A15SDOptimizer::optimizeAllLanesPattern(MachineInstr *MI, unsigned Reg) {
  437. MachineBasicBlock::iterator InsertPt(MI);
  438. DebugLoc DL = MI->getDebugLoc();
  439. MachineBasicBlock &MBB = *MI->getParent();
  440. InsertPt++;
  441. unsigned Out;
  442. // DPair has the same length as QPR and also has two DPRs as subreg.
  443. // Treat DPair as QPR.
  444. if (MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::QPRRegClass) ||
  445. MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::DPairRegClass)) {
  446. unsigned DSub0 = createExtractSubreg(MBB, InsertPt, DL, Reg,
  447. ARM::dsub_0, &ARM::DPRRegClass);
  448. unsigned DSub1 = createExtractSubreg(MBB, InsertPt, DL, Reg,
  449. ARM::dsub_1, &ARM::DPRRegClass);
  450. unsigned Out1 = createDupLane(MBB, InsertPt, DL, DSub0, 0);
  451. unsigned Out2 = createDupLane(MBB, InsertPt, DL, DSub0, 1);
  452. Out = createVExt(MBB, InsertPt, DL, Out1, Out2);
  453. unsigned Out3 = createDupLane(MBB, InsertPt, DL, DSub1, 0);
  454. unsigned Out4 = createDupLane(MBB, InsertPt, DL, DSub1, 1);
  455. Out2 = createVExt(MBB, InsertPt, DL, Out3, Out4);
  456. Out = createRegSequence(MBB, InsertPt, DL, Out, Out2);
  457. } else if (MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::DPRRegClass)) {
  458. unsigned Out1 = createDupLane(MBB, InsertPt, DL, Reg, 0);
  459. unsigned Out2 = createDupLane(MBB, InsertPt, DL, Reg, 1);
  460. Out = createVExt(MBB, InsertPt, DL, Out1, Out2);
  461. } else {
  462. assert(MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::SPRRegClass) &&
  463. "Found unexpected regclass!");
  464. unsigned PrefLane = getPrefSPRLane(Reg);
  465. unsigned Lane;
  466. switch (PrefLane) {
  467. case ARM::ssub_0: Lane = 0; break;
  468. case ARM::ssub_1: Lane = 1; break;
  469. default: llvm_unreachable("Unknown preferred lane!");
  470. }
  471. // Treat DPair as QPR
  472. bool UsesQPR = usesRegClass(MI->getOperand(0), &ARM::QPRRegClass) ||
  473. usesRegClass(MI->getOperand(0), &ARM::DPairRegClass);
  474. Out = createImplicitDef(MBB, InsertPt, DL);
  475. Out = createInsertSubreg(MBB, InsertPt, DL, Out, PrefLane, Reg);
  476. Out = createDupLane(MBB, InsertPt, DL, Out, Lane, UsesQPR);
  477. eraseInstrWithNoUses(MI);
  478. }
  479. return Out;
  480. }
  481. bool A15SDOptimizer::runOnInstruction(MachineInstr *MI) {
  482. // We look for instructions that write S registers that are then read as
  483. // D/Q registers. These can only be caused by COPY, INSERT_SUBREG and
  484. // REG_SEQUENCE pseudos that insert an SPR value into a DPR register or
  485. // merge two SPR values to form a DPR register. In order avoid false
  486. // positives we make sure that there is an SPR producer so we look past
  487. // COPY and PHI nodes to find it.
  488. //
  489. // The best code pattern for when an SPR producer is going to be used by a
  490. // DPR or QPR consumer depends on whether the other lanes of the
  491. // corresponding DPR/QPR are currently defined.
  492. //
  493. // We can handle these efficiently, depending on the type of
  494. // pseudo-instruction that is producing the pattern
  495. //
  496. // * COPY: * VDUP all lanes and merge the results together
  497. // using VEXTs.
  498. //
  499. // * INSERT_SUBREG: * If the SPR value was originally in another DPR/QPR
  500. // lane, and the other lane(s) of the DPR/QPR register
  501. // that we are inserting in are undefined, use the
  502. // original DPR/QPR value.
  503. // * Otherwise, fall back on the same stategy as COPY.
  504. //
  505. // * REG_SEQUENCE: * If all except one of the input operands are
  506. // IMPLICIT_DEFs, insert the VDUP pattern for just the
  507. // defined input operand
  508. // * Otherwise, fall back on the same stategy as COPY.
  509. //
  510. // First, get all the reads of D-registers done by this instruction.
  511. SmallVector<unsigned, 8> Defs = getReadDPRs(MI);
  512. bool Modified = false;
  513. for (unsigned I : Defs) {
  514. // Follow the def-use chain for this DPR through COPYs, and also through
  515. // PHIs (which are essentially multi-way COPYs). It is because of PHIs that
  516. // we can end up with multiple defs of this DPR.
  517. SmallVector<MachineInstr *, 8> DefSrcs;
  518. if (!Register::isVirtualRegister(I))
  519. continue;
  520. MachineInstr *Def = MRI->getVRegDef(I);
  521. if (!Def)
  522. continue;
  523. elideCopiesAndPHIs(Def, DefSrcs);
  524. for (MachineInstr *MI : DefSrcs) {
  525. // If we've already analyzed and replaced this operand, don't do
  526. // anything.
  527. if (Replacements.find(MI) != Replacements.end())
  528. continue;
  529. // Now, work out if the instruction causes a SPR->DPR dependency.
  530. if (!hasPartialWrite(MI))
  531. continue;
  532. // Collect all the uses of this MI's DPR def for updating later.
  533. SmallVector<MachineOperand*, 8> Uses;
  534. Register DPRDefReg = MI->getOperand(0).getReg();
  535. for (MachineOperand &MO : MRI->use_operands(DPRDefReg))
  536. Uses.push_back(&MO);
  537. // We can optimize this.
  538. unsigned NewReg = optimizeSDPattern(MI);
  539. if (NewReg != 0) {
  540. Modified = true;
  541. for (MachineOperand *Use : Uses) {
  542. // Make sure to constrain the register class of the new register to
  543. // match what we're replacing. Otherwise we can optimize a DPR_VFP2
  544. // reference into a plain DPR, and that will end poorly. NewReg is
  545. // always virtual here, so there will always be a matching subclass
  546. // to find.
  547. MRI->constrainRegClass(NewReg, MRI->getRegClass(Use->getReg()));
  548. LLVM_DEBUG(dbgs() << "Replacing operand " << *Use << " with "
  549. << printReg(NewReg) << "\n");
  550. Use->substVirtReg(NewReg, 0, *TRI);
  551. }
  552. }
  553. Replacements[MI] = NewReg;
  554. }
  555. }
  556. return Modified;
  557. }
  558. bool A15SDOptimizer::runOnMachineFunction(MachineFunction &Fn) {
  559. if (skipFunction(Fn.getFunction()))
  560. return false;
  561. const ARMSubtarget &STI = Fn.getSubtarget<ARMSubtarget>();
  562. // Since the A15SDOptimizer pass can insert VDUP instructions, it can only be
  563. // enabled when NEON is available.
  564. if (!(STI.useSplatVFPToNeon() && STI.hasNEON()))
  565. return false;
  566. TII = STI.getInstrInfo();
  567. TRI = STI.getRegisterInfo();
  568. MRI = &Fn.getRegInfo();
  569. bool Modified = false;
  570. LLVM_DEBUG(dbgs() << "Running on function " << Fn.getName() << "\n");
  571. DeadInstr.clear();
  572. Replacements.clear();
  573. for (MachineBasicBlock &MBB : Fn) {
  574. for (MachineInstr &MI : MBB) {
  575. Modified |= runOnInstruction(&MI);
  576. }
  577. }
  578. for (MachineInstr *MI : DeadInstr) {
  579. MI->eraseFromParent();
  580. }
  581. return Modified;
  582. }
  583. FunctionPass *llvm::createA15SDOptimizerPass() {
  584. return new A15SDOptimizer();
  585. }