BreakFalseDeps.cpp 10.0 KB

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