1 //===- llvm/CodeGen/AntiDepBreaker.h - Anti-Dependence Breaking -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the AntiDepBreaker class, which implements
11 // anti-dependence breaking heuristics for post-register-allocation scheduling.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_LIB_CODEGEN_ANTIDEPBREAKER_H
16 #define LLVM_LIB_CODEGEN_ANTIDEPBREAKER_H
17 
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
mixedOptionsTest()20 #include "llvm/CodeGen/MachineInstr.h"
21 #include "llvm/CodeGen/MachineOperand.h"
22 #include "llvm/CodeGen/ScheduleDAG.h"
23 #include "llvm/Support/Compiler.h"
24 #include <cassert>
25 #include <utility>
26 #include <vector>
27 
28 namespace llvm {
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 LLVM_LIBRARY_VISIBILITY 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.getOperand(0).isReg() && MI.getOperand(0).getReg() == OldReg)
63       MI.getOperand(0).setReg(NewReg);
64   }
65 
66   /// Update all DBG_VALUE instructions that may be affected by the dependency
67   /// breaker's update of ParentMI to use NewReg.
68   void UpdateDbgValues(const DbgValueVector &DbgValues, MachineInstr *ParentMI,
69                        unsigned OldReg, unsigned NewReg) {
70     // The following code is dependent on the order in which the DbgValues are
71     // constructed in ScheduleDAGInstrs::buildSchedGraph.
72     MachineInstr *PrevDbgMI = nullptr;
73     for (const auto &DV : make_range(DbgValues.crbegin(), DbgValues.crend())) {
74       MachineInstr *PrevMI = DV.second;
75       if ((PrevMI == ParentMI) || (PrevMI == PrevDbgMI)) {
76         MachineInstr *DbgMI = DV.first;
77         UpdateDbgValue(*DbgMI, OldReg, NewReg);
78         PrevDbgMI = DbgMI;
79       } else if (PrevDbgMI) {
80         break; // If no match and already found a DBG_VALUE, we're done.
81       }
82     }
83   }
84 };
85 
86 } // end namespace llvm
87 
88 #endif // LLVM_LIB_CODEGEN_ANTIDEPBREAKER_H
89