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