X86LowerTileCopy.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //===-- X86LowerTileCopy.cpp - Expand Tile Copy Instructions---------------===//
  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 file defines the pass which lower AMX tile copy instructions. Since
  10. // there is no tile copy instruction, we need store tile register to stack
  11. // and load from stack to another tile register. We need extra GR to hold
  12. // the stride, and we need stack slot to hold the tile data register.
  13. // We would run this pass after copy propagation, so that we don't miss copy
  14. // optimization. And we would run this pass before prolog/epilog insertion,
  15. // so that we can allocate stack slot.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #include "X86.h"
  19. #include "X86InstrBuilder.h"
  20. #include "X86InstrInfo.h"
  21. #include "X86Subtarget.h"
  22. #include "llvm/CodeGen/MachineBasicBlock.h"
  23. #include "llvm/CodeGen/MachineFrameInfo.h"
  24. #include "llvm/CodeGen/MachineFunction.h"
  25. #include "llvm/CodeGen/MachineFunctionPass.h"
  26. #include "llvm/CodeGen/MachineInstr.h"
  27. #include "llvm/CodeGen/MachineInstrBuilder.h"
  28. #include "llvm/CodeGen/MachineOperand.h"
  29. #include "llvm/CodeGen/Passes.h"
  30. #include "llvm/IR/DebugLoc.h"
  31. #include "llvm/InitializePasses.h"
  32. #include "llvm/Support/Debug.h"
  33. using namespace llvm;
  34. #define DEBUG_TYPE "x86-lower-tile-copy"
  35. namespace {
  36. class X86LowerTileCopy : public MachineFunctionPass {
  37. public:
  38. static char ID;
  39. X86LowerTileCopy() : MachineFunctionPass(ID) {}
  40. void getAnalysisUsage(AnalysisUsage &AU) const override;
  41. bool runOnMachineFunction(MachineFunction &MF) override;
  42. StringRef getPassName() const override { return "X86 Lower Tile Copy"; }
  43. };
  44. } // namespace
  45. char X86LowerTileCopy::ID = 0;
  46. INITIALIZE_PASS_BEGIN(X86LowerTileCopy, "lowertilecopy", "Tile Copy Lowering",
  47. false, false)
  48. INITIALIZE_PASS_END(X86LowerTileCopy, "lowertilecopy", "Tile Copy Lowering",
  49. false, false)
  50. void X86LowerTileCopy::getAnalysisUsage(AnalysisUsage &AU) const {
  51. AU.setPreservesAll();
  52. MachineFunctionPass::getAnalysisUsage(AU);
  53. }
  54. FunctionPass *llvm::createX86LowerTileCopyPass() {
  55. return new X86LowerTileCopy();
  56. }
  57. bool X86LowerTileCopy::runOnMachineFunction(MachineFunction &MF) {
  58. const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
  59. const X86InstrInfo *TII = ST.getInstrInfo();
  60. bool Changed = false;
  61. for (MachineBasicBlock &MBB : MF) {
  62. for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
  63. if (!MI.isCopy())
  64. continue;
  65. MachineOperand &DstMO = MI.getOperand(0);
  66. MachineOperand &SrcMO = MI.getOperand(1);
  67. Register SrcReg = SrcMO.getReg();
  68. Register DstReg = DstMO.getReg();
  69. if (!X86::TILERegClass.contains(DstReg, SrcReg))
  70. continue;
  71. const TargetRegisterInfo *TRI = ST.getRegisterInfo();
  72. // Allocate stack slot for tile register
  73. unsigned Size = TRI->getSpillSize(X86::TILERegClass);
  74. Align Alignment = TRI->getSpillAlign(X86::TILERegClass);
  75. int TileSS = MF.getFrameInfo().CreateSpillStackObject(Size, Alignment);
  76. // Allocate stack slot for stride register
  77. Size = TRI->getSpillSize(X86::GR64RegClass);
  78. Alignment = TRI->getSpillAlign(X86::GR64RegClass);
  79. int StrideSS = MF.getFrameInfo().CreateSpillStackObject(Size, Alignment);
  80. // TODO: Pick a killed regiter to avoid save/reload. There is problem
  81. // to get live interval in this stage.
  82. Register GR64Cand = X86::RAX;
  83. const DebugLoc &DL = MI.getDebugLoc();
  84. // mov %rax (%sp)
  85. BuildMI(MBB, MI, DL, TII->get(X86::IMPLICIT_DEF), GR64Cand);
  86. addFrameReference(BuildMI(MBB, MI, DL, TII->get(X86::MOV64mr)), StrideSS)
  87. .addReg(GR64Cand);
  88. // mov 64 %rax
  89. BuildMI(MBB, MI, DL, TII->get(X86::MOV64ri), GR64Cand).addImm(64);
  90. // tilestored %tmm, (%sp, %idx)
  91. unsigned Opc = X86::TILESTORED;
  92. MachineInstr *NewMI =
  93. addFrameReference(BuildMI(MBB, MI, DL, TII->get(Opc)), TileSS)
  94. .addReg(SrcReg, getKillRegState(SrcMO.isKill()));
  95. MachineOperand &MO = NewMI->getOperand(2);
  96. MO.setReg(GR64Cand);
  97. MO.setIsKill(true);
  98. // tileloadd (%sp, %idx), %tmm
  99. Opc = X86::TILELOADD;
  100. NewMI = addFrameReference(BuildMI(MBB, MI, DL, TII->get(Opc), DstReg),
  101. TileSS);
  102. // restore %rax
  103. // mov (%sp) %rax
  104. addFrameReference(BuildMI(MBB, MI, DL, TII->get(X86::MOV64rm), GR64Cand),
  105. StrideSS);
  106. MI.eraseFromParent();
  107. Changed = true;
  108. }
  109. }
  110. return Changed;
  111. }