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