BreakFalseDeps.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //==- llvm/CodeGen/BreakFalseDeps.cpp - Break False Dependency Fix -*- C++ -*==//
  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. /// \file Break False Dependency pass.
  10. ///
  11. /// Some instructions have false dependencies which cause unnecessary stalls.
  12. /// For example, instructions may write part of a register and implicitly
  13. /// need to read the other parts of the register. This may cause unwanted
  14. /// stalls preventing otherwise unrelated instructions from executing in
  15. /// parallel in an out-of-order CPU.
  16. /// This pass is aimed at identifying and avoiding these dependencies.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #include "llvm/CodeGen/LivePhysRegs.h"
  20. #include "llvm/CodeGen/MachineFunctionPass.h"
  21. #include "llvm/CodeGen/ReachingDefAnalysis.h"
  22. #include "llvm/CodeGen/RegisterClassInfo.h"
  23. #include "llvm/CodeGen/MachineRegisterInfo.h"
  24. #include "llvm/CodeGen/TargetInstrInfo.h"
  25. using namespace llvm;
  26. namespace llvm {
  27. class BreakFalseDeps : public MachineFunctionPass {
  28. private:
  29. MachineFunction *MF;
  30. const TargetInstrInfo *TII;
  31. const TargetRegisterInfo *TRI;
  32. RegisterClassInfo RegClassInfo;
  33. /// List of undefined register reads in this block in forward order.
  34. std::vector<std::pair<MachineInstr *, unsigned>> UndefReads;
  35. /// Storage for register unit liveness.
  36. LivePhysRegs LiveRegSet;
  37. ReachingDefAnalysis *RDA;
  38. public:
  39. static char ID; // Pass identification, replacement for typeid
  40. BreakFalseDeps() : MachineFunctionPass(ID) {
  41. initializeBreakFalseDepsPass(*PassRegistry::getPassRegistry());
  42. }
  43. void getAnalysisUsage(AnalysisUsage &AU) const override {
  44. AU.setPreservesAll();
  45. AU.addRequired<ReachingDefAnalysis>();
  46. MachineFunctionPass::getAnalysisUsage(AU);
  47. }
  48. bool runOnMachineFunction(MachineFunction &MF) override;
  49. MachineFunctionProperties getRequiredProperties() const override {
  50. return MachineFunctionProperties().set(
  51. MachineFunctionProperties::Property::NoVRegs);
  52. }
  53. private:
  54. /// Process he given basic block.
  55. void processBasicBlock(MachineBasicBlock *MBB);
  56. /// Update def-ages for registers defined by MI.
  57. /// Also break dependencies on partial defs and undef uses.
  58. void processDefs(MachineInstr *MI);
  59. /// Helps avoid false dependencies on undef registers by updating the
  60. /// machine instructions' undef operand to use a register that the instruction
  61. /// is truly dependent on, or use a register with clearance higher than Pref.
  62. /// Returns true if it was able to find a true dependency, thus not requiring
  63. /// a dependency breaking instruction regardless of clearance.
  64. bool pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
  65. unsigned Pref);
  66. /// Return true to if it makes sense to break dependence on a partial
  67. /// def or undef use.
  68. bool shouldBreakDependence(MachineInstr *, unsigned OpIdx, unsigned Pref);
  69. /// Break false dependencies on undefined register reads.
  70. /// Walk the block backward computing precise liveness. This is expensive, so
  71. /// we only do it on demand. Note that the occurrence of undefined register
  72. /// reads that should be broken is very rare, but when they occur we may have
  73. /// many in a single block.
  74. void processUndefReads(MachineBasicBlock *);
  75. };
  76. } // namespace llvm
  77. #define DEBUG_TYPE "break-false-deps"
  78. char BreakFalseDeps::ID = 0;
  79. INITIALIZE_PASS_BEGIN(BreakFalseDeps, DEBUG_TYPE, "BreakFalseDeps", false, false)
  80. INITIALIZE_PASS_DEPENDENCY(ReachingDefAnalysis)
  81. INITIALIZE_PASS_END(BreakFalseDeps, DEBUG_TYPE, "BreakFalseDeps", false, false)
  82. FunctionPass *llvm::createBreakFalseDeps() { return new BreakFalseDeps(); }
  83. bool BreakFalseDeps::pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
  84. unsigned Pref) {
  85. MachineOperand &MO = MI->getOperand(OpIdx);
  86. assert(MO.isUndef() && "Expected undef machine operand");
  87. Register OriginalReg = MO.getReg();
  88. // Update only undef operands that have reg units that are mapped to one root.
  89. for (MCRegUnitIterator Unit(OriginalReg, TRI); Unit.isValid(); ++Unit) {
  90. unsigned NumRoots = 0;
  91. for (MCRegUnitRootIterator Root(*Unit, TRI); Root.isValid(); ++Root) {
  92. NumRoots++;
  93. if (NumRoots > 1)
  94. return false;
  95. }
  96. }
  97. // Get the undef operand's register class
  98. const TargetRegisterClass *OpRC =
  99. TII->getRegClass(MI->getDesc(), OpIdx, TRI, *MF);
  100. // If the instruction has a true dependency, we can hide the false depdency
  101. // behind it.
  102. for (MachineOperand &CurrMO : MI->operands()) {
  103. if (!CurrMO.isReg() || CurrMO.isDef() || CurrMO.isUndef() ||
  104. !OpRC->contains(CurrMO.getReg()))
  105. continue;
  106. // We found a true dependency - replace the undef register with the true
  107. // dependency.
  108. MO.setReg(CurrMO.getReg());
  109. return true;
  110. }
  111. // Go over all registers in the register class and find the register with
  112. // max clearance or clearance higher than Pref.
  113. unsigned MaxClearance = 0;
  114. unsigned MaxClearanceReg = OriginalReg;
  115. ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(OpRC);
  116. for (MCPhysReg Reg : Order) {
  117. unsigned Clearance = RDA->getClearance(MI, Reg);
  118. if (Clearance <= MaxClearance)
  119. continue;
  120. MaxClearance = Clearance;
  121. MaxClearanceReg = Reg;
  122. if (MaxClearance > Pref)
  123. break;
  124. }
  125. // Update the operand if we found a register with better clearance.
  126. if (MaxClearanceReg != OriginalReg)
  127. MO.setReg(MaxClearanceReg);
  128. return false;
  129. }
  130. bool BreakFalseDeps::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
  131. unsigned Pref) {
  132. Register reg = MI->getOperand(OpIdx).getReg();
  133. unsigned Clearance = RDA->getClearance(MI, reg);
  134. LLVM_DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
  135. if (Pref > Clearance) {
  136. LLVM_DEBUG(dbgs() << ": Break dependency.\n");
  137. return true;
  138. }
  139. LLVM_DEBUG(dbgs() << ": OK .\n");
  140. return false;
  141. }
  142. void BreakFalseDeps::processDefs(MachineInstr *MI) {
  143. assert(!MI->isDebugInstr() && "Won't process debug values");
  144. // Break dependence on undef uses. Do this before updating LiveRegs below.
  145. unsigned OpNum;
  146. unsigned Pref = TII->getUndefRegClearance(*MI, OpNum, TRI);
  147. if (Pref) {
  148. bool HadTrueDependency = pickBestRegisterForUndef(MI, OpNum, Pref);
  149. // We don't need to bother trying to break a dependency if this
  150. // instruction has a true dependency on that register through another
  151. // operand - we'll have to wait for it to be available regardless.
  152. if (!HadTrueDependency && shouldBreakDependence(MI, OpNum, Pref))
  153. UndefReads.push_back(std::make_pair(MI, OpNum));
  154. }
  155. const MCInstrDesc &MCID = MI->getDesc();
  156. for (unsigned i = 0,
  157. e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
  158. i != e; ++i) {
  159. MachineOperand &MO = MI->getOperand(i);
  160. if (!MO.isReg() || !MO.getReg())
  161. continue;
  162. if (MO.isUse())
  163. continue;
  164. // Check clearance before partial register updates.
  165. unsigned Pref = TII->getPartialRegUpdateClearance(*MI, i, TRI);
  166. if (Pref && shouldBreakDependence(MI, i, Pref))
  167. TII->breakPartialRegDependency(*MI, i, TRI);
  168. }
  169. }
  170. void BreakFalseDeps::processUndefReads(MachineBasicBlock *MBB) {
  171. if (UndefReads.empty())
  172. return;
  173. // Collect this block's live out register units.
  174. LiveRegSet.init(*TRI);
  175. // We do not need to care about pristine registers as they are just preserved
  176. // but not actually used in the function.
  177. LiveRegSet.addLiveOutsNoPristines(*MBB);
  178. MachineInstr *UndefMI = UndefReads.back().first;
  179. unsigned OpIdx = UndefReads.back().second;
  180. for (MachineInstr &I : make_range(MBB->rbegin(), MBB->rend())) {
  181. // Update liveness, including the current instruction's defs.
  182. LiveRegSet.stepBackward(I);
  183. if (UndefMI == &I) {
  184. if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg()))
  185. TII->breakPartialRegDependency(*UndefMI, OpIdx, TRI);
  186. UndefReads.pop_back();
  187. if (UndefReads.empty())
  188. return;
  189. UndefMI = UndefReads.back().first;
  190. OpIdx = UndefReads.back().second;
  191. }
  192. }
  193. }
  194. void BreakFalseDeps::processBasicBlock(MachineBasicBlock *MBB) {
  195. UndefReads.clear();
  196. // If this block is not done, it makes little sense to make any decisions
  197. // based on clearance information. We need to make a second pass anyway,
  198. // and by then we'll have better information, so we can avoid doing the work
  199. // to try and break dependencies now.
  200. for (MachineInstr &MI : *MBB) {
  201. if (!MI.isDebugInstr())
  202. processDefs(&MI);
  203. }
  204. processUndefReads(MBB);
  205. }
  206. bool BreakFalseDeps::runOnMachineFunction(MachineFunction &mf) {
  207. if (skipFunction(mf.getFunction()))
  208. return false;
  209. MF = &mf;
  210. TII = MF->getSubtarget().getInstrInfo();
  211. TRI = MF->getSubtarget().getRegisterInfo();
  212. RDA = &getAnalysis<ReachingDefAnalysis>();
  213. RegClassInfo.runOnMachineFunction(mf);
  214. LLVM_DEBUG(dbgs() << "********** BREAK FALSE DEPENDENCIES **********\n");
  215. // Traverse the basic blocks.
  216. for (MachineBasicBlock &MBB : mf) {
  217. processBasicBlock(&MBB);
  218. }
  219. return false;
  220. }