AntiDepBreaker.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/CodeGen/AntiDepBreaker.h - Anti-Dependence Breaking -*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file implements the AntiDepBreaker class, which implements
  15. // anti-dependence breaking heuristics for post-register-allocation scheduling.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CODEGEN_ANTIDEPBREAKER_H
  19. #define LLVM_CODEGEN_ANTIDEPBREAKER_H
  20. #include "llvm/ADT/iterator_range.h"
  21. #include "llvm/CodeGen/MachineBasicBlock.h"
  22. #include "llvm/CodeGen/MachineInstr.h"
  23. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  24. #include "llvm/Support/Compiler.h"
  25. #include <cassert>
  26. #include <utility>
  27. #include <vector>
  28. namespace llvm {
  29. class RegisterClassInfo;
  30. /// This class works in conjunction with the post-RA scheduler to rename
  31. /// registers to break register anti-dependencies (WAR hazards).
  32. class AntiDepBreaker {
  33. public:
  34. using DbgValueVector =
  35. std::vector<std::pair<MachineInstr *, MachineInstr *>>;
  36. virtual ~AntiDepBreaker();
  37. /// Initialize anti-dep breaking for a new basic block.
  38. virtual void StartBlock(MachineBasicBlock *BB) = 0;
  39. /// Identifiy anti-dependencies within a basic-block region and break them by
  40. /// renaming registers. Return the number of anti-dependencies broken.
  41. virtual unsigned BreakAntiDependencies(const std::vector<SUnit> &SUnits,
  42. MachineBasicBlock::iterator Begin,
  43. MachineBasicBlock::iterator End,
  44. unsigned InsertPosIndex,
  45. DbgValueVector &DbgValues) = 0;
  46. /// Update liveness information to account for the current
  47. /// instruction, which will not be scheduled.
  48. virtual void Observe(MachineInstr &MI, unsigned Count,
  49. unsigned InsertPosIndex) = 0;
  50. /// Finish anti-dep breaking for a basic block.
  51. virtual void FinishBlock() = 0;
  52. /// Update DBG_VALUE or DBG_PHI if dependency breaker is updating
  53. /// other machine instruction to use NewReg.
  54. void UpdateDbgValue(MachineInstr &MI, unsigned OldReg, unsigned NewReg) {
  55. if (MI.isDebugValue()) {
  56. if (MI.getDebugOperand(0).isReg() &&
  57. MI.getDebugOperand(0).getReg() == OldReg)
  58. MI.getDebugOperand(0).setReg(NewReg);
  59. } else if (MI.isDebugPHI()) {
  60. if (MI.getOperand(0).isReg() &&
  61. MI.getOperand(0).getReg() == OldReg)
  62. MI.getOperand(0).setReg(NewReg);
  63. } else {
  64. llvm_unreachable("MI is not DBG_VALUE / DBG_PHI!");
  65. }
  66. }
  67. /// Update all DBG_VALUE instructions that may be affected by the dependency
  68. /// breaker's update of ParentMI to use NewReg.
  69. void UpdateDbgValues(const DbgValueVector &DbgValues, MachineInstr *ParentMI,
  70. unsigned OldReg, unsigned NewReg) {
  71. // The following code is dependent on the order in which the DbgValues are
  72. // constructed in ScheduleDAGInstrs::buildSchedGraph.
  73. MachineInstr *PrevDbgMI = nullptr;
  74. for (const auto &DV : make_range(DbgValues.crbegin(), DbgValues.crend())) {
  75. MachineInstr *PrevMI = DV.second;
  76. if ((PrevMI == ParentMI) || (PrevMI == PrevDbgMI)) {
  77. MachineInstr *DbgMI = DV.first;
  78. UpdateDbgValue(*DbgMI, OldReg, NewReg);
  79. PrevDbgMI = DbgMI;
  80. } else if (PrevDbgMI) {
  81. break; // If no match and already found a DBG_VALUE, we're done.
  82. }
  83. }
  84. }
  85. };
  86. AntiDepBreaker *createAggressiveAntiDepBreaker(
  87. MachineFunction &MFi, const RegisterClassInfo &RCI,
  88. TargetSubtargetInfo::RegClassVector &CriticalPathRCs);
  89. AntiDepBreaker *createCriticalAntiDepBreaker(MachineFunction &MFi,
  90. const RegisterClassInfo &RCI);
  91. } // end namespace llvm
  92. #endif // LLVM_CODEGEN_ANTIDEPBREAKER_H
  93. #ifdef __GNUC__
  94. #pragma GCC diagnostic pop
  95. #endif