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