xref: /aosp_15_r20/external/llvm/lib/CodeGen/CriticalAntiDepBreaker.h (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //=- llvm/CodeGen/CriticalAntiDepBreaker.h - Anti-Dep Support -*- C++ -*-=//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the CriticalAntiDepBreaker class, which
11*9880d681SAndroid Build Coastguard Worker // implements register anti-dependence breaking along a blocks
12*9880d681SAndroid Build Coastguard Worker // critical path during post-RA scheduler.
13*9880d681SAndroid Build Coastguard Worker //
14*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15*9880d681SAndroid Build Coastguard Worker 
16*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_LIB_CODEGEN_CRITICALANTIDEPBREAKER_H
17*9880d681SAndroid Build Coastguard Worker #define LLVM_LIB_CODEGEN_CRITICALANTIDEPBREAKER_H
18*9880d681SAndroid Build Coastguard Worker 
19*9880d681SAndroid Build Coastguard Worker #include "AntiDepBreaker.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/BitVector.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBasicBlock.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/RegisterClassInfo.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/ScheduleDAG.h"
25*9880d681SAndroid Build Coastguard Worker 
26*9880d681SAndroid Build Coastguard Worker namespace llvm {
27*9880d681SAndroid Build Coastguard Worker class RegisterClassInfo;
28*9880d681SAndroid Build Coastguard Worker class TargetInstrInfo;
29*9880d681SAndroid Build Coastguard Worker class TargetRegisterInfo;
30*9880d681SAndroid Build Coastguard Worker class MachineFunction;
31*9880d681SAndroid Build Coastguard Worker 
32*9880d681SAndroid Build Coastguard Worker class LLVM_LIBRARY_VISIBILITY CriticalAntiDepBreaker : public AntiDepBreaker {
33*9880d681SAndroid Build Coastguard Worker     MachineFunction& MF;
34*9880d681SAndroid Build Coastguard Worker     MachineRegisterInfo &MRI;
35*9880d681SAndroid Build Coastguard Worker     const TargetInstrInfo *TII;
36*9880d681SAndroid Build Coastguard Worker     const TargetRegisterInfo *TRI;
37*9880d681SAndroid Build Coastguard Worker     const RegisterClassInfo &RegClassInfo;
38*9880d681SAndroid Build Coastguard Worker 
39*9880d681SAndroid Build Coastguard Worker     /// The set of allocatable registers.
40*9880d681SAndroid Build Coastguard Worker     /// We'll be ignoring anti-dependencies on non-allocatable registers,
41*9880d681SAndroid Build Coastguard Worker     /// because they may not be safe to break.
42*9880d681SAndroid Build Coastguard Worker     const BitVector AllocatableSet;
43*9880d681SAndroid Build Coastguard Worker 
44*9880d681SAndroid Build Coastguard Worker     /// For live regs that are only used in one register class in a
45*9880d681SAndroid Build Coastguard Worker     /// live range, the register class. If the register is not live, the
46*9880d681SAndroid Build Coastguard Worker     /// corresponding value is null. If the register is live but used in
47*9880d681SAndroid Build Coastguard Worker     /// multiple register classes, the corresponding value is -1 casted to a
48*9880d681SAndroid Build Coastguard Worker     /// pointer.
49*9880d681SAndroid Build Coastguard Worker     std::vector<const TargetRegisterClass*> Classes;
50*9880d681SAndroid Build Coastguard Worker 
51*9880d681SAndroid Build Coastguard Worker     /// Map registers to all their references within a live range.
52*9880d681SAndroid Build Coastguard Worker     std::multimap<unsigned, MachineOperand *> RegRefs;
53*9880d681SAndroid Build Coastguard Worker     typedef std::multimap<unsigned, MachineOperand *>::const_iterator
54*9880d681SAndroid Build Coastguard Worker       RegRefIter;
55*9880d681SAndroid Build Coastguard Worker 
56*9880d681SAndroid Build Coastguard Worker     /// The index of the most recent kill (proceeding bottom-up),
57*9880d681SAndroid Build Coastguard Worker     /// or ~0u if the register is not live.
58*9880d681SAndroid Build Coastguard Worker     std::vector<unsigned> KillIndices;
59*9880d681SAndroid Build Coastguard Worker 
60*9880d681SAndroid Build Coastguard Worker     /// The index of the most recent complete def (proceeding
61*9880d681SAndroid Build Coastguard Worker     /// bottom up), or ~0u if the register is live.
62*9880d681SAndroid Build Coastguard Worker     std::vector<unsigned> DefIndices;
63*9880d681SAndroid Build Coastguard Worker 
64*9880d681SAndroid Build Coastguard Worker     /// A set of registers which are live and cannot be changed to
65*9880d681SAndroid Build Coastguard Worker     /// break anti-dependencies.
66*9880d681SAndroid Build Coastguard Worker     BitVector KeepRegs;
67*9880d681SAndroid Build Coastguard Worker 
68*9880d681SAndroid Build Coastguard Worker   public:
69*9880d681SAndroid Build Coastguard Worker     CriticalAntiDepBreaker(MachineFunction& MFi, const RegisterClassInfo&);
70*9880d681SAndroid Build Coastguard Worker     ~CriticalAntiDepBreaker() override;
71*9880d681SAndroid Build Coastguard Worker 
72*9880d681SAndroid Build Coastguard Worker     /// Initialize anti-dep breaking for a new basic block.
73*9880d681SAndroid Build Coastguard Worker     void StartBlock(MachineBasicBlock *BB) override;
74*9880d681SAndroid Build Coastguard Worker 
75*9880d681SAndroid Build Coastguard Worker     /// Identifiy anti-dependencies along the critical path
76*9880d681SAndroid Build Coastguard Worker     /// of the ScheduleDAG and break them by renaming registers.
77*9880d681SAndroid Build Coastguard Worker     unsigned BreakAntiDependencies(const std::vector<SUnit>& SUnits,
78*9880d681SAndroid Build Coastguard Worker                                    MachineBasicBlock::iterator Begin,
79*9880d681SAndroid Build Coastguard Worker                                    MachineBasicBlock::iterator End,
80*9880d681SAndroid Build Coastguard Worker                                    unsigned InsertPosIndex,
81*9880d681SAndroid Build Coastguard Worker                                    DbgValueVector &DbgValues) override;
82*9880d681SAndroid Build Coastguard Worker 
83*9880d681SAndroid Build Coastguard Worker     /// Update liveness information to account for the current
84*9880d681SAndroid Build Coastguard Worker     /// instruction, which will not be scheduled.
85*9880d681SAndroid Build Coastguard Worker     void Observe(MachineInstr &MI, unsigned Count,
86*9880d681SAndroid Build Coastguard Worker                  unsigned InsertPosIndex) override;
87*9880d681SAndroid Build Coastguard Worker 
88*9880d681SAndroid Build Coastguard Worker     /// Finish anti-dep breaking for a basic block.
89*9880d681SAndroid Build Coastguard Worker     void FinishBlock() override;
90*9880d681SAndroid Build Coastguard Worker 
91*9880d681SAndroid Build Coastguard Worker   private:
92*9880d681SAndroid Build Coastguard Worker     void PrescanInstruction(MachineInstr &MI);
93*9880d681SAndroid Build Coastguard Worker     void ScanInstruction(MachineInstr &MI, unsigned Count);
94*9880d681SAndroid Build Coastguard Worker     bool isNewRegClobberedByRefs(RegRefIter RegRefBegin,
95*9880d681SAndroid Build Coastguard Worker                                  RegRefIter RegRefEnd,
96*9880d681SAndroid Build Coastguard Worker                                  unsigned NewReg);
97*9880d681SAndroid Build Coastguard Worker     unsigned findSuitableFreeRegister(RegRefIter RegRefBegin,
98*9880d681SAndroid Build Coastguard Worker                                       RegRefIter RegRefEnd,
99*9880d681SAndroid Build Coastguard Worker                                       unsigned AntiDepReg,
100*9880d681SAndroid Build Coastguard Worker                                       unsigned LastNewReg,
101*9880d681SAndroid Build Coastguard Worker                                       const TargetRegisterClass *RC,
102*9880d681SAndroid Build Coastguard Worker                                       SmallVectorImpl<unsigned> &Forbid);
103*9880d681SAndroid Build Coastguard Worker   };
104*9880d681SAndroid Build Coastguard Worker }
105*9880d681SAndroid Build Coastguard Worker 
106*9880d681SAndroid Build Coastguard Worker #endif
107