MLxExpansionPass.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. //===-- MLxExpansionPass.cpp - Expand MLx instrs to avoid hazards ---------===//
  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. // Expand VFP / NEON floating point MLA / MLS instructions (each to a pair of
  10. // multiple and add / sub instructions) when special VMLx hazards are detected.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "ARM.h"
  14. #include "ARMBaseInstrInfo.h"
  15. #include "ARMSubtarget.h"
  16. #include "llvm/ADT/SmallPtrSet.h"
  17. #include "llvm/ADT/Statistic.h"
  18. #include "llvm/CodeGen/MachineFunctionPass.h"
  19. #include "llvm/CodeGen/MachineInstr.h"
  20. #include "llvm/CodeGen/MachineInstrBuilder.h"
  21. #include "llvm/CodeGen/MachineRegisterInfo.h"
  22. #include "llvm/CodeGen/TargetRegisterInfo.h"
  23. #include "llvm/Support/CommandLine.h"
  24. #include "llvm/Support/Debug.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. using namespace llvm;
  27. #define DEBUG_TYPE "mlx-expansion"
  28. static cl::opt<bool>
  29. ForceExapnd("expand-all-fp-mlx", cl::init(false), cl::Hidden);
  30. static cl::opt<unsigned>
  31. ExpandLimit("expand-limit", cl::init(~0U), cl::Hidden);
  32. STATISTIC(NumExpand, "Number of fp MLA / MLS instructions expanded");
  33. namespace {
  34. struct MLxExpansion : public MachineFunctionPass {
  35. static char ID;
  36. MLxExpansion() : MachineFunctionPass(ID) {}
  37. bool runOnMachineFunction(MachineFunction &Fn) override;
  38. StringRef getPassName() const override {
  39. return "ARM MLA / MLS expansion pass";
  40. }
  41. private:
  42. const ARMBaseInstrInfo *TII;
  43. const TargetRegisterInfo *TRI;
  44. MachineRegisterInfo *MRI;
  45. bool isLikeA9;
  46. bool isSwift;
  47. unsigned MIIdx;
  48. MachineInstr* LastMIs[4];
  49. SmallPtrSet<MachineInstr*, 4> IgnoreStall;
  50. void clearStack();
  51. void pushStack(MachineInstr *MI);
  52. MachineInstr *getAccDefMI(MachineInstr *MI) const;
  53. unsigned getDefReg(MachineInstr *MI) const;
  54. bool hasLoopHazard(MachineInstr *MI) const;
  55. bool hasRAWHazard(unsigned Reg, MachineInstr *MI) const;
  56. bool FindMLxHazard(MachineInstr *MI);
  57. void ExpandFPMLxInstruction(MachineBasicBlock &MBB, MachineInstr *MI,
  58. unsigned MulOpc, unsigned AddSubOpc,
  59. bool NegAcc, bool HasLane);
  60. bool ExpandFPMLxInstructions(MachineBasicBlock &MBB);
  61. };
  62. char MLxExpansion::ID = 0;
  63. }
  64. void MLxExpansion::clearStack() {
  65. std::fill(LastMIs, LastMIs + 4, nullptr);
  66. MIIdx = 0;
  67. }
  68. void MLxExpansion::pushStack(MachineInstr *MI) {
  69. LastMIs[MIIdx] = MI;
  70. if (++MIIdx == 4)
  71. MIIdx = 0;
  72. }
  73. MachineInstr *MLxExpansion::getAccDefMI(MachineInstr *MI) const {
  74. // Look past COPY and INSERT_SUBREG instructions to find the
  75. // real definition MI. This is important for _sfp instructions.
  76. Register Reg = MI->getOperand(1).getReg();
  77. if (Register::isPhysicalRegister(Reg))
  78. return nullptr;
  79. MachineBasicBlock *MBB = MI->getParent();
  80. MachineInstr *DefMI = MRI->getVRegDef(Reg);
  81. while (true) {
  82. if (DefMI->getParent() != MBB)
  83. break;
  84. if (DefMI->isCopyLike()) {
  85. Reg = DefMI->getOperand(1).getReg();
  86. if (Register::isVirtualRegister(Reg)) {
  87. DefMI = MRI->getVRegDef(Reg);
  88. continue;
  89. }
  90. } else if (DefMI->isInsertSubreg()) {
  91. Reg = DefMI->getOperand(2).getReg();
  92. if (Register::isVirtualRegister(Reg)) {
  93. DefMI = MRI->getVRegDef(Reg);
  94. continue;
  95. }
  96. }
  97. break;
  98. }
  99. return DefMI;
  100. }
  101. unsigned MLxExpansion::getDefReg(MachineInstr *MI) const {
  102. Register Reg = MI->getOperand(0).getReg();
  103. if (Register::isPhysicalRegister(Reg) || !MRI->hasOneNonDBGUse(Reg))
  104. return Reg;
  105. MachineBasicBlock *MBB = MI->getParent();
  106. MachineInstr *UseMI = &*MRI->use_instr_nodbg_begin(Reg);
  107. if (UseMI->getParent() != MBB)
  108. return Reg;
  109. while (UseMI->isCopy() || UseMI->isInsertSubreg()) {
  110. Reg = UseMI->getOperand(0).getReg();
  111. if (Register::isPhysicalRegister(Reg) || !MRI->hasOneNonDBGUse(Reg))
  112. return Reg;
  113. UseMI = &*MRI->use_instr_nodbg_begin(Reg);
  114. if (UseMI->getParent() != MBB)
  115. return Reg;
  116. }
  117. return Reg;
  118. }
  119. /// hasLoopHazard - Check whether an MLx instruction is chained to itself across
  120. /// a single-MBB loop.
  121. bool MLxExpansion::hasLoopHazard(MachineInstr *MI) const {
  122. Register Reg = MI->getOperand(1).getReg();
  123. if (Register::isPhysicalRegister(Reg))
  124. return false;
  125. MachineBasicBlock *MBB = MI->getParent();
  126. MachineInstr *DefMI = MRI->getVRegDef(Reg);
  127. while (true) {
  128. outer_continue:
  129. if (DefMI->getParent() != MBB)
  130. break;
  131. if (DefMI->isPHI()) {
  132. for (unsigned i = 1, e = DefMI->getNumOperands(); i < e; i += 2) {
  133. if (DefMI->getOperand(i + 1).getMBB() == MBB) {
  134. Register SrcReg = DefMI->getOperand(i).getReg();
  135. if (Register::isVirtualRegister(SrcReg)) {
  136. DefMI = MRI->getVRegDef(SrcReg);
  137. goto outer_continue;
  138. }
  139. }
  140. }
  141. } else if (DefMI->isCopyLike()) {
  142. Reg = DefMI->getOperand(1).getReg();
  143. if (Register::isVirtualRegister(Reg)) {
  144. DefMI = MRI->getVRegDef(Reg);
  145. continue;
  146. }
  147. } else if (DefMI->isInsertSubreg()) {
  148. Reg = DefMI->getOperand(2).getReg();
  149. if (Register::isVirtualRegister(Reg)) {
  150. DefMI = MRI->getVRegDef(Reg);
  151. continue;
  152. }
  153. }
  154. break;
  155. }
  156. return DefMI == MI;
  157. }
  158. bool MLxExpansion::hasRAWHazard(unsigned Reg, MachineInstr *MI) const {
  159. // FIXME: Detect integer instructions properly.
  160. const MCInstrDesc &MCID = MI->getDesc();
  161. unsigned Domain = MCID.TSFlags & ARMII::DomainMask;
  162. if (MI->mayStore())
  163. return false;
  164. unsigned Opcode = MCID.getOpcode();
  165. if (Opcode == ARM::VMOVRS || Opcode == ARM::VMOVRRD)
  166. return false;
  167. if ((Domain & ARMII::DomainVFP) || (Domain & ARMII::DomainNEON))
  168. return MI->readsRegister(Reg, TRI);
  169. return false;
  170. }
  171. static bool isFpMulInstruction(unsigned Opcode) {
  172. switch (Opcode) {
  173. case ARM::VMULS:
  174. case ARM::VMULfd:
  175. case ARM::VMULfq:
  176. case ARM::VMULD:
  177. case ARM::VMULslfd:
  178. case ARM::VMULslfq:
  179. return true;
  180. default:
  181. return false;
  182. }
  183. }
  184. bool MLxExpansion::FindMLxHazard(MachineInstr *MI) {
  185. if (NumExpand >= ExpandLimit)
  186. return false;
  187. if (ForceExapnd)
  188. return true;
  189. MachineInstr *DefMI = getAccDefMI(MI);
  190. if (TII->isFpMLxInstruction(DefMI->getOpcode())) {
  191. // r0 = vmla
  192. // r3 = vmla r0, r1, r2
  193. // takes 16 - 17 cycles
  194. //
  195. // r0 = vmla
  196. // r4 = vmul r1, r2
  197. // r3 = vadd r0, r4
  198. // takes about 14 - 15 cycles even with vmul stalling for 4 cycles.
  199. IgnoreStall.insert(DefMI);
  200. return true;
  201. }
  202. // On Swift, we mostly care about hazards from multiplication instructions
  203. // writing the accumulator and the pipelining of loop iterations by out-of-
  204. // order execution.
  205. if (isSwift)
  206. return isFpMulInstruction(DefMI->getOpcode()) || hasLoopHazard(MI);
  207. if (IgnoreStall.count(MI))
  208. return false;
  209. // If a VMLA.F is followed by an VADD.F or VMUL.F with no RAW hazard, the
  210. // VADD.F or VMUL.F will stall 4 cycles before issue. The 4 cycle stall
  211. // preserves the in-order retirement of the instructions.
  212. // Look at the next few instructions, if *most* of them can cause hazards,
  213. // then the scheduler can't *fix* this, we'd better break up the VMLA.
  214. unsigned Limit1 = isLikeA9 ? 1 : 4;
  215. unsigned Limit2 = isLikeA9 ? 1 : 4;
  216. for (unsigned i = 1; i <= 4; ++i) {
  217. int Idx = ((int)MIIdx - i + 4) % 4;
  218. MachineInstr *NextMI = LastMIs[Idx];
  219. if (!NextMI)
  220. continue;
  221. if (TII->canCauseFpMLxStall(NextMI->getOpcode())) {
  222. if (i <= Limit1)
  223. return true;
  224. }
  225. // Look for VMLx RAW hazard.
  226. if (i <= Limit2 && hasRAWHazard(getDefReg(MI), NextMI))
  227. return true;
  228. }
  229. return false;
  230. }
  231. /// ExpandFPMLxInstructions - Expand a MLA / MLS instruction into a pair
  232. /// of MUL + ADD / SUB instructions.
  233. void
  234. MLxExpansion::ExpandFPMLxInstruction(MachineBasicBlock &MBB, MachineInstr *MI,
  235. unsigned MulOpc, unsigned AddSubOpc,
  236. bool NegAcc, bool HasLane) {
  237. Register DstReg = MI->getOperand(0).getReg();
  238. bool DstDead = MI->getOperand(0).isDead();
  239. Register AccReg = MI->getOperand(1).getReg();
  240. Register Src1Reg = MI->getOperand(2).getReg();
  241. Register Src2Reg = MI->getOperand(3).getReg();
  242. bool Src1Kill = MI->getOperand(2).isKill();
  243. bool Src2Kill = MI->getOperand(3).isKill();
  244. unsigned LaneImm = HasLane ? MI->getOperand(4).getImm() : 0;
  245. unsigned NextOp = HasLane ? 5 : 4;
  246. ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NextOp).getImm();
  247. Register PredReg = MI->getOperand(++NextOp).getReg();
  248. const MCInstrDesc &MCID1 = TII->get(MulOpc);
  249. const MCInstrDesc &MCID2 = TII->get(AddSubOpc);
  250. const MachineFunction &MF = *MI->getParent()->getParent();
  251. Register TmpReg =
  252. MRI->createVirtualRegister(TII->getRegClass(MCID1, 0, TRI, MF));
  253. MachineInstrBuilder MIB = BuildMI(MBB, MI, MI->getDebugLoc(), MCID1, TmpReg)
  254. .addReg(Src1Reg, getKillRegState(Src1Kill))
  255. .addReg(Src2Reg, getKillRegState(Src2Kill));
  256. if (HasLane)
  257. MIB.addImm(LaneImm);
  258. MIB.addImm(Pred).addReg(PredReg);
  259. MIB = BuildMI(MBB, MI, MI->getDebugLoc(), MCID2)
  260. .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstDead));
  261. if (NegAcc) {
  262. bool AccKill = MRI->hasOneNonDBGUse(AccReg);
  263. MIB.addReg(TmpReg, getKillRegState(true))
  264. .addReg(AccReg, getKillRegState(AccKill));
  265. } else {
  266. MIB.addReg(AccReg).addReg(TmpReg, getKillRegState(true));
  267. }
  268. MIB.addImm(Pred).addReg(PredReg);
  269. LLVM_DEBUG({
  270. dbgs() << "Expanding: " << *MI;
  271. dbgs() << " to:\n";
  272. MachineBasicBlock::iterator MII = MI;
  273. MII = std::prev(MII);
  274. MachineInstr &MI2 = *MII;
  275. MII = std::prev(MII);
  276. MachineInstr &MI1 = *MII;
  277. dbgs() << " " << MI1;
  278. dbgs() << " " << MI2;
  279. });
  280. MI->eraseFromParent();
  281. ++NumExpand;
  282. }
  283. bool MLxExpansion::ExpandFPMLxInstructions(MachineBasicBlock &MBB) {
  284. bool Changed = false;
  285. clearStack();
  286. IgnoreStall.clear();
  287. unsigned Skip = 0;
  288. MachineBasicBlock::reverse_iterator MII = MBB.rbegin(), E = MBB.rend();
  289. while (MII != E) {
  290. MachineInstr *MI = &*MII++;
  291. if (MI->isPosition() || MI->isImplicitDef() || MI->isCopy())
  292. continue;
  293. const MCInstrDesc &MCID = MI->getDesc();
  294. if (MI->isBarrier()) {
  295. clearStack();
  296. Skip = 0;
  297. continue;
  298. }
  299. unsigned Domain = MCID.TSFlags & ARMII::DomainMask;
  300. if (Domain == ARMII::DomainGeneral) {
  301. if (++Skip == 2)
  302. // Assume dual issues of non-VFP / NEON instructions.
  303. pushStack(nullptr);
  304. } else {
  305. Skip = 0;
  306. unsigned MulOpc, AddSubOpc;
  307. bool NegAcc, HasLane;
  308. if (!TII->isFpMLxInstruction(MCID.getOpcode(),
  309. MulOpc, AddSubOpc, NegAcc, HasLane) ||
  310. !FindMLxHazard(MI))
  311. pushStack(MI);
  312. else {
  313. ExpandFPMLxInstruction(MBB, MI, MulOpc, AddSubOpc, NegAcc, HasLane);
  314. Changed = true;
  315. }
  316. }
  317. }
  318. return Changed;
  319. }
  320. bool MLxExpansion::runOnMachineFunction(MachineFunction &Fn) {
  321. if (skipFunction(Fn.getFunction()))
  322. return false;
  323. TII = static_cast<const ARMBaseInstrInfo *>(Fn.getSubtarget().getInstrInfo());
  324. TRI = Fn.getSubtarget().getRegisterInfo();
  325. MRI = &Fn.getRegInfo();
  326. const ARMSubtarget *STI = &Fn.getSubtarget<ARMSubtarget>();
  327. if (!STI->expandMLx())
  328. return false;
  329. isLikeA9 = STI->isLikeA9() || STI->isSwift();
  330. isSwift = STI->isSwift();
  331. bool Modified = false;
  332. for (MachineBasicBlock &MBB : Fn)
  333. Modified |= ExpandFPMLxInstructions(MBB);
  334. return Modified;
  335. }
  336. FunctionPass *llvm::createMLxExpansionPass() {
  337. return new MLxExpansion();
  338. }