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 
14 #ifndef LLVM_LIB_CODEGEN_ANTIDEPBREAKER_H
15 #define LLVM_LIB_CODEGEN_ANTIDEPBREAKER_H
16 
17 #include "llvm/ADT/iterator_range.h"
18 #include "llvm/CodeGen/MachineBasicBlock.h"
19 #include "llvm/CodeGen/MachineInstr.h"
20 #include "llvm/CodeGen/TargetSubtargetInfo.h"
21 #include "llvm/Support/Compiler.h"
22 #include <cassert>
23 #include <utility>
24 #include <vector>
25 
26 namespace llvm {
27 
28 class RegisterClassInfo;
29 
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 
37   virtual ~AntiDepBreaker();
38 
39   /// Initialize anti-dep breaking for a new basic block.
40   virtual void StartBlock(MachineBasicBlock *BB) = 0;
41 
42   /// Identifiy anti-dependencies within a basic-block region and break them by
43   /// renaming registers. Return the number of anti-dependencies broken.
44   virtual unsigned BreakAntiDependencies(const std::vector<SUnit> &SUnits,
45                                          MachineBasicBlock::iterator Begin,
46                                          MachineBasicBlock::iterator End,
47                                          unsigned InsertPosIndex,
48                                          DbgValueVector &DbgValues) = 0;
49 
50   /// Update liveness information to account for the current
51   /// instruction, which will not be scheduled.
52   virtual void Observe(MachineInstr &MI, unsigned Count,
53                        unsigned InsertPosIndex) = 0;
54 
55   /// Finish anti-dep breaking for a basic block.
56   virtual void FinishBlock() = 0;
57 
58   /// Update DBG_VALUE if dependency breaker is updating
59   /// other machine instruction to use NewReg.
60   void UpdateDbgValue(MachineInstr &MI, unsigned OldReg, unsigned NewReg) {
61     assert(MI.isDebugValue() && "MI is not DBG_VALUE!");
62     if (MI.getDebugOperand(0).isReg() &&
63         MI.getDebugOperand(0).getReg() == OldReg)
64       MI.getDebugOperand(0).setReg(NewReg);
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 
87 AntiDepBreaker *createAggressiveAntiDepBreaker(
88     MachineFunction &MFi, const RegisterClassInfo &RCI,
89     TargetSubtargetInfo::RegClassVector &CriticalPathRCs);
90 
91 AntiDepBreaker *createCriticalAntiDepBreaker(MachineFunction &MFi,
92                                              const RegisterClassInfo &RCI);
93 
94 } // end namespace llvm
95 
96 #endif // LLVM_LIB_CODEGEN_ANTIDEPBREAKER_H
97