BreakFalseDeps.cpp 10 KB

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