MachineCycleAnalysis.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //===- MachineCycleAnalysis.cpp - Compute CycleInfo for Machine IR --------===//
  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. #include "llvm/CodeGen/MachineCycleAnalysis.h"
  9. #include "llvm/ADT/GenericCycleImpl.h"
  10. #include "llvm/CodeGen/MachineRegisterInfo.h"
  11. #include "llvm/CodeGen/MachineSSAContext.h"
  12. #include "llvm/CodeGen/TargetInstrInfo.h"
  13. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  14. #include "llvm/InitializePasses.h"
  15. using namespace llvm;
  16. template class llvm::GenericCycleInfo<llvm::MachineSSAContext>;
  17. template class llvm::GenericCycle<llvm::MachineSSAContext>;
  18. char MachineCycleInfoWrapperPass::ID = 0;
  19. MachineCycleInfoWrapperPass::MachineCycleInfoWrapperPass()
  20. : MachineFunctionPass(ID) {
  21. initializeMachineCycleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
  22. }
  23. INITIALIZE_PASS_BEGIN(MachineCycleInfoWrapperPass, "machine-cycles",
  24. "Machine Cycle Info Analysis", true, true)
  25. INITIALIZE_PASS_END(MachineCycleInfoWrapperPass, "machine-cycles",
  26. "Machine Cycle Info Analysis", true, true)
  27. void MachineCycleInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  28. AU.setPreservesAll();
  29. MachineFunctionPass::getAnalysisUsage(AU);
  30. }
  31. bool MachineCycleInfoWrapperPass::runOnMachineFunction(MachineFunction &Func) {
  32. CI.clear();
  33. F = &Func;
  34. CI.compute(Func);
  35. return false;
  36. }
  37. void MachineCycleInfoWrapperPass::print(raw_ostream &OS, const Module *) const {
  38. OS << "MachineCycleInfo for function: " << F->getName() << "\n";
  39. CI.print(OS);
  40. }
  41. void MachineCycleInfoWrapperPass::releaseMemory() {
  42. CI.clear();
  43. F = nullptr;
  44. }
  45. namespace {
  46. class MachineCycleInfoPrinterPass : public MachineFunctionPass {
  47. public:
  48. static char ID;
  49. MachineCycleInfoPrinterPass();
  50. bool runOnMachineFunction(MachineFunction &F) override;
  51. void getAnalysisUsage(AnalysisUsage &AU) const override;
  52. };
  53. } // namespace
  54. char MachineCycleInfoPrinterPass::ID = 0;
  55. MachineCycleInfoPrinterPass::MachineCycleInfoPrinterPass()
  56. : MachineFunctionPass(ID) {
  57. initializeMachineCycleInfoPrinterPassPass(*PassRegistry::getPassRegistry());
  58. }
  59. INITIALIZE_PASS_BEGIN(MachineCycleInfoPrinterPass, "print-machine-cycles",
  60. "Print Machine Cycle Info Analysis", true, true)
  61. INITIALIZE_PASS_DEPENDENCY(MachineCycleInfoWrapperPass)
  62. INITIALIZE_PASS_END(MachineCycleInfoPrinterPass, "print-machine-cycles",
  63. "Print Machine Cycle Info Analysis", true, true)
  64. void MachineCycleInfoPrinterPass::getAnalysisUsage(AnalysisUsage &AU) const {
  65. AU.setPreservesAll();
  66. AU.addRequired<MachineCycleInfoWrapperPass>();
  67. MachineFunctionPass::getAnalysisUsage(AU);
  68. }
  69. bool MachineCycleInfoPrinterPass::runOnMachineFunction(MachineFunction &F) {
  70. auto &CI = getAnalysis<MachineCycleInfoWrapperPass>();
  71. CI.print(errs());
  72. return false;
  73. }
  74. bool llvm::isCycleInvariant(const MachineCycle *Cycle, MachineInstr &I) {
  75. MachineFunction *MF = I.getParent()->getParent();
  76. MachineRegisterInfo *MRI = &MF->getRegInfo();
  77. const TargetSubtargetInfo &ST = MF->getSubtarget();
  78. const TargetRegisterInfo *TRI = ST.getRegisterInfo();
  79. const TargetInstrInfo *TII = ST.getInstrInfo();
  80. // The instruction is cycle invariant if all of its operands are.
  81. for (const MachineOperand &MO : I.operands()) {
  82. if (!MO.isReg())
  83. continue;
  84. Register Reg = MO.getReg();
  85. if (Reg == 0)
  86. continue;
  87. // An instruction that uses or defines a physical register can't e.g. be
  88. // hoisted, so mark this as not invariant.
  89. if (Reg.isPhysical()) {
  90. if (MO.isUse()) {
  91. // If the physreg has no defs anywhere, it's just an ambient register
  92. // and we can freely move its uses. Alternatively, if it's allocatable,
  93. // it could get allocated to something with a def during allocation.
  94. // However, if the physreg is known to always be caller saved/restored
  95. // then this use is safe to hoist.
  96. if (!MRI->isConstantPhysReg(Reg) &&
  97. !(TRI->isCallerPreservedPhysReg(Reg.asMCReg(), *I.getMF())) &&
  98. !TII->isIgnorableUse(MO))
  99. return false;
  100. // Otherwise it's safe to move.
  101. continue;
  102. } else if (!MO.isDead()) {
  103. // A def that isn't dead can't be moved.
  104. return false;
  105. } else if (any_of(Cycle->getEntries(),
  106. [&](const MachineBasicBlock *Block) {
  107. return Block->isLiveIn(Reg);
  108. })) {
  109. // If the reg is live into any header of the cycle we can't hoist an
  110. // instruction which would clobber it.
  111. return false;
  112. }
  113. }
  114. if (!MO.isUse())
  115. continue;
  116. assert(MRI->getVRegDef(Reg) && "Machine instr not mapped for this vreg?!");
  117. // If the cycle contains the definition of an operand, then the instruction
  118. // isn't cycle invariant.
  119. if (Cycle->contains(MRI->getVRegDef(Reg)->getParent()))
  120. return false;
  121. }
  122. // If we got this far, the instruction is cycle invariant!
  123. return true;
  124. }