Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 1 | //==- llvm/CodeGen/AggressiveAntiDepBreaker.h - Anti-Dep Support -*- C++ -*-==// |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 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 AggressiveAntiDepBreaker class, which |
| 11 | // implements register anti-dependence breaking during post-RA |
| 12 | // scheduling. It attempts to break all anti-dependencies within a |
| 13 | // block. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Benjamin Kramer | 00e08fc | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 17 | #ifndef LLVM_LIB_CODEGEN_AGGRESSIVEANTIDEPBREAKER_H |
| 18 | #define LLVM_LIB_CODEGEN_AGGRESSIVEANTIDEPBREAKER_H |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 19 | |
David Goodwin | 82c7248 | 2009-10-28 18:29:54 +0000 | [diff] [blame] | 20 | #include "AntiDepBreaker.h" |
Chandler Carruth | a1514e2 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/BitVector.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 24 | #include <map> |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 25 | #include <set> |
| 26 | #include <vector> |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 27 | |
| 28 | namespace llvm { |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 29 | |
| 30 | class MachineBasicBlock; |
| 31 | class MachineFunction; |
| 32 | class MachineInstr; |
| 33 | class MachineOperand; |
| 34 | class MachineRegisterInfo; |
Jakob Stoklund Olesen | fa796dd | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 35 | class RegisterClassInfo; |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 36 | class TargetInstrInfo; |
| 37 | class TargetRegisterClass; |
| 38 | class TargetRegisterInfo; |
Jakob Stoklund Olesen | fa796dd | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 39 | |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 40 | /// Contains all the state necessary for anti-dep breaking. |
Benjamin Kramer | b453775 | 2015-07-01 14:47:39 +0000 | [diff] [blame] | 41 | class LLVM_LIBRARY_VISIBILITY AggressiveAntiDepState { |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 42 | public: |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 43 | /// Information about a register reference within a liverange |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 44 | struct RegisterReference { |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 45 | /// The registers operand |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 46 | MachineOperand *Operand; |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 47 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 48 | /// The register class |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 49 | const TargetRegisterClass *RC; |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 50 | }; |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 51 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 52 | private: |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 53 | /// Number of non-virtual target registers (i.e. TRI->getNumRegs()). |
David Goodwin | 990d285 | 2009-12-09 17:18:22 +0000 | [diff] [blame] | 54 | const unsigned NumTargetRegs; |
| 55 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 56 | /// Implements a disjoint-union data structure to |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 57 | /// form register groups. A node is represented by an index into |
| 58 | /// the vector. A node can "point to" itself to indicate that it |
| 59 | /// is the parent of a group, or point to another node to indicate |
| 60 | /// that it is a member of the same group as that node. |
| 61 | std::vector<unsigned> GroupNodes; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 62 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 63 | /// For each register, the index of the GroupNode |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 64 | /// currently representing the group that the register belongs to. |
| 65 | /// Register 0 is always represented by the 0 group, a group |
| 66 | /// composed of registers that are not eligible for anti-aliasing. |
Bill Wendling | dfb4eeb | 2010-07-15 18:40:50 +0000 | [diff] [blame] | 67 | std::vector<unsigned> GroupNodeIndices; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 68 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 69 | /// Map registers to all their references within a live range. |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 70 | std::multimap<unsigned, RegisterReference> RegRefs; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 71 | |
Luqman Aden | 440ae31 | 2015-04-22 17:42:37 +0000 | [diff] [blame] | 72 | /// The index of the most recent kill (proceeding bottom-up), |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 73 | /// or ~0u if the register is not live. |
Bill Wendling | 38306d5 | 2010-07-15 18:43:09 +0000 | [diff] [blame] | 74 | std::vector<unsigned> KillIndices; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 75 | |
Luqman Aden | 440ae31 | 2015-04-22 17:42:37 +0000 | [diff] [blame] | 76 | /// The index of the most recent complete def (proceeding bottom |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 77 | /// up), or ~0u if the register is live. |
Bill Wendling | 38306d5 | 2010-07-15 18:43:09 +0000 | [diff] [blame] | 78 | std::vector<unsigned> DefIndices; |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 79 | |
| 80 | public: |
David Goodwin | 990d285 | 2009-12-09 17:18:22 +0000 | [diff] [blame] | 81 | AggressiveAntiDepState(const unsigned TargetRegs, MachineBasicBlock *BB); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 82 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 83 | /// Return the kill indices. |
Bill Wendling | 38306d5 | 2010-07-15 18:43:09 +0000 | [diff] [blame] | 84 | std::vector<unsigned> &GetKillIndices() { return KillIndices; } |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 85 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 86 | /// Return the define indices. |
Bill Wendling | 38306d5 | 2010-07-15 18:43:09 +0000 | [diff] [blame] | 87 | std::vector<unsigned> &GetDefIndices() { return DefIndices; } |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 88 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 89 | /// Return the RegRefs map. |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 90 | std::multimap<unsigned, RegisterReference>& GetRegRefs() { return RegRefs; } |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 91 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 92 | // Get the group for a register. The returned value is |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 93 | // the index of the GroupNode representing the group. |
| 94 | unsigned GetGroup(unsigned Reg); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 95 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 96 | // Return a vector of the registers belonging to a group. |
| 97 | // If RegRefs is non-NULL then only included referenced registers. |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 98 | void GetGroupRegs( |
| 99 | unsigned Group, |
| 100 | std::vector<unsigned> &Regs, |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 101 | std::multimap<unsigned, |
| 102 | AggressiveAntiDepState::RegisterReference> *RegRefs); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 103 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 104 | // Union Reg1's and Reg2's groups to form a new group. |
| 105 | // Return the index of the GroupNode representing the group. |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 106 | unsigned UnionGroups(unsigned Reg1, unsigned Reg2); |
| 107 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 108 | // Remove a register from its current group and place |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 109 | // it alone in its own group. Return the index of the GroupNode |
| 110 | // representing the registers new group. |
| 111 | unsigned LeaveGroup(unsigned Reg); |
| 112 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 113 | /// Return true if Reg is live. |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 114 | bool IsLive(unsigned Reg); |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
Benjamin Kramer | b453775 | 2015-07-01 14:47:39 +0000 | [diff] [blame] | 117 | class LLVM_LIBRARY_VISIBILITY AggressiveAntiDepBreaker |
| 118 | : public AntiDepBreaker { |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 119 | MachineFunction &MF; |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 120 | MachineRegisterInfo &MRI; |
Evan Cheng | 46df4eb | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 121 | const TargetInstrInfo *TII; |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 122 | const TargetRegisterInfo *TRI; |
Jakob Stoklund Olesen | fa796dd | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 123 | const RegisterClassInfo &RegClassInfo; |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 124 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 125 | /// The set of registers that should only be |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 126 | /// renamed if they are on the critical path. |
| 127 | BitVector CriticalPathSet; |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 128 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 129 | /// The state used to identify and rename anti-dependence registers. |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 130 | AggressiveAntiDepState *State = nullptr; |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 131 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 132 | public: |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 133 | AggressiveAntiDepBreaker(MachineFunction &MFi, |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 134 | const RegisterClassInfo &RCI, |
| 135 | TargetSubtargetInfo::RegClassVector& CriticalPathRCs); |
Alexander Kornienko | c16fc54 | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 136 | ~AggressiveAntiDepBreaker() override; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 137 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 138 | /// Initialize anti-dep breaking for a new basic block. |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 139 | void StartBlock(MachineBasicBlock *BB) override; |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 140 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 141 | /// Identifiy anti-dependencies along the critical path |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 142 | /// of the ScheduleDAG and break them by renaming registers. |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 143 | unsigned BreakAntiDependencies(const std::vector<SUnit> &SUnits, |
Dan Gohman | 66db3a0 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 144 | MachineBasicBlock::iterator Begin, |
| 145 | MachineBasicBlock::iterator End, |
Devang Patel | e29e8e1 | 2011-06-02 21:26:52 +0000 | [diff] [blame] | 146 | unsigned InsertPosIndex, |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 147 | DbgValueVector &DbgValues) override; |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 148 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 149 | /// Update liveness information to account for the current |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 150 | /// instruction, which will not be scheduled. |
Duncan P. N. Exon Smith | a26cd9c | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 151 | void Observe(MachineInstr &MI, unsigned Count, |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 152 | unsigned InsertPosIndex) override; |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 153 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 154 | /// Finish anti-dep breaking for a basic block. |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 155 | void FinishBlock() override; |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 156 | |
| 157 | private: |
Jakob Stoklund Olesen | fa796dd | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 158 | /// Keep track of a position in the allocation order for each regclass. |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 159 | using RenameOrderType = std::map<const TargetRegisterClass *, unsigned>; |
David Goodwin | 5409783 | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 160 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 161 | /// Return true if MO represents a register |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 162 | /// that is both implicitly used and defined in MI |
Duncan P. N. Exon Smith | a26cd9c | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 163 | bool IsImplicitDefUse(MachineInstr &MI, MachineOperand &MO); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 164 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 165 | /// If MI implicitly def/uses a register, then |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 166 | /// return that register and all subregisters. |
Duncan P. N. Exon Smith | a26cd9c | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 167 | void GetPassthruRegs(MachineInstr &MI, std::set<unsigned> &PassthruRegs); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 168 | |
David Goodwin | 3e72d30 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 169 | void HandleLastUse(unsigned Reg, unsigned KillIdx, const char *tag, |
Craig Topper | 695aa80 | 2014-04-16 04:21:27 +0000 | [diff] [blame] | 170 | const char *header = nullptr, |
| 171 | const char *footer = nullptr); |
David Goodwin | 3e72d30 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 172 | |
Duncan P. N. Exon Smith | a26cd9c | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 173 | void PrescanInstruction(MachineInstr &MI, unsigned Count, |
| 174 | std::set<unsigned> &PassthruRegs); |
| 175 | void ScanInstruction(MachineInstr &MI, unsigned Count); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 176 | BitVector GetRenameRegisters(unsigned Reg); |
| 177 | bool FindSuitableFreeRegisters(unsigned AntiDepGroupIndex, |
David Goodwin | 5409783 | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 178 | RenameOrderType& RenameOrder, |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 179 | std::map<unsigned, unsigned> &RenameMap); |
| 180 | }; |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 181 | |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 182 | } // end namespace llvm |
| 183 | |
| 184 | #endif // LLVM_LIB_CODEGEN_AGGRESSIVEANTIDEPBREAKER_H |