AntiDepBreaker.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //===- llvm/CodeGen/AntiDepBreaker.h - Anti-Dependence Breaking -*- 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. // This file implements the AntiDepBreaker class, which implements
  10. // anti-dependence breaking heuristics for post-register-allocation scheduling.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_LIB_CODEGEN_ANTIDEPBREAKER_H
  14. #define LLVM_LIB_CODEGEN_ANTIDEPBREAKER_H
  15. #include "llvm/ADT/iterator_range.h"
  16. #include "llvm/CodeGen/MachineBasicBlock.h"
  17. #include "llvm/CodeGen/MachineInstr.h"
  18. #include "llvm/CodeGen/MachineOperand.h"
  19. #include "llvm/CodeGen/ScheduleDAG.h"
  20. #include "llvm/Support/Compiler.h"
  21. #include <cassert>
  22. #include <utility>
  23. #include <vector>
  24. namespace llvm {
  25. /// This class works in conjunction with the post-RA scheduler to rename
  26. /// registers to break register anti-dependencies (WAR hazards).
  27. class LLVM_LIBRARY_VISIBILITY AntiDepBreaker {
  28. public:
  29. using DbgValueVector =
  30. std::vector<std::pair<MachineInstr *, MachineInstr *>>;
  31. virtual ~AntiDepBreaker();
  32. /// Initialize anti-dep breaking for a new basic block.
  33. virtual void StartBlock(MachineBasicBlock *BB) = 0;
  34. /// Identifiy anti-dependencies within a basic-block region and break them by
  35. /// renaming registers. Return the number of anti-dependencies broken.
  36. virtual unsigned BreakAntiDependencies(const std::vector<SUnit> &SUnits,
  37. MachineBasicBlock::iterator Begin,
  38. MachineBasicBlock::iterator End,
  39. unsigned InsertPosIndex,
  40. DbgValueVector &DbgValues) = 0;
  41. /// Update liveness information to account for the current
  42. /// instruction, which will not be scheduled.
  43. virtual void Observe(MachineInstr &MI, unsigned Count,
  44. unsigned InsertPosIndex) = 0;
  45. /// Finish anti-dep breaking for a basic block.
  46. virtual void FinishBlock() = 0;
  47. /// Update DBG_VALUE if dependency breaker is updating
  48. /// other machine instruction to use NewReg.
  49. void UpdateDbgValue(MachineInstr &MI, unsigned OldReg, unsigned NewReg) {
  50. assert(MI.isDebugValue() && "MI is not DBG_VALUE!");
  51. if (MI.getOperand(0).isReg() && MI.getOperand(0).getReg() == OldReg)
  52. MI.getOperand(0).setReg(NewReg);
  53. }
  54. /// Update all DBG_VALUE instructions that may be affected by the dependency
  55. /// breaker's update of ParentMI to use NewReg.
  56. void UpdateDbgValues(const DbgValueVector &DbgValues, MachineInstr *ParentMI,
  57. unsigned OldReg, unsigned NewReg) {
  58. // The following code is dependent on the order in which the DbgValues are
  59. // constructed in ScheduleDAGInstrs::buildSchedGraph.
  60. MachineInstr *PrevDbgMI = nullptr;
  61. for (const auto &DV : make_range(DbgValues.crbegin(), DbgValues.crend())) {
  62. MachineInstr *PrevMI = DV.second;
  63. if ((PrevMI == ParentMI) || (PrevMI == PrevDbgMI)) {
  64. MachineInstr *DbgMI = DV.first;
  65. UpdateDbgValue(*DbgMI, OldReg, NewReg);
  66. PrevDbgMI = DbgMI;
  67. } else if (PrevDbgMI) {
  68. break; // If no match and already found a DBG_VALUE, we're done.
  69. }
  70. }
  71. }
  72. };
  73. } // end namespace llvm
  74. #endif // LLVM_LIB_CODEGEN_ANTIDEPBREAKER_H