David Goodwin | 3487771 | 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 | |
| 17 | #ifndef LLVM_CODEGEN_AGGRESSIVEANTIDEPBREAKER_H |
| 18 | #define LLVM_CODEGEN_AGGRESSIVEANTIDEPBREAKER_H |
| 19 | |
David Goodwin | 82c7248 | 2009-10-28 18:29:54 +0000 | [diff] [blame] | 20 | #include "AntiDepBreaker.h" |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 23 | #include "llvm/CodeGen/MachineFunction.h" |
| 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 25 | #include "llvm/CodeGen/ScheduleDAG.h" |
David Goodwin | 0855dee | 2009-11-10 00:15:47 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetSubtarget.h" |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetRegisterInfo.h" |
| 28 | #include "llvm/ADT/BitVector.h" |
| 29 | #include "llvm/ADT/SmallSet.h" |
| 30 | |
| 31 | namespace llvm { |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 32 | /// Class AggressiveAntiDepState |
| 33 | /// Contains all the state necessary for anti-dep breaking. We place |
| 34 | /// into a separate class so be can conveniently save/restore it to |
| 35 | /// enable multi-pass anti-dep breaking. |
| 36 | class AggressiveAntiDepState { |
| 37 | public: |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 38 | /// RegisterReference - Information about a register reference |
| 39 | /// within a liverange |
| 40 | typedef struct { |
| 41 | /// Operand - The registers operand |
| 42 | MachineOperand *Operand; |
| 43 | /// RC - The register class |
| 44 | const TargetRegisterClass *RC; |
| 45 | } RegisterReference; |
| 46 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 47 | private: |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 48 | /// GroupNodes - Implements a disjoint-union data structure to |
| 49 | /// form register groups. A node is represented by an index into |
| 50 | /// the vector. A node can "point to" itself to indicate that it |
| 51 | /// is the parent of a group, or point to another node to indicate |
| 52 | /// that it is a member of the same group as that node. |
| 53 | std::vector<unsigned> GroupNodes; |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 54 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 55 | /// GroupNodeIndices - For each register, the index of the GroupNode |
| 56 | /// currently representing the group that the register belongs to. |
| 57 | /// Register 0 is always represented by the 0 group, a group |
| 58 | /// composed of registers that are not eligible for anti-aliasing. |
| 59 | unsigned GroupNodeIndices[TargetRegisterInfo::FirstVirtualRegister]; |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 60 | |
| 61 | /// RegRefs - Map registers to all their references within a live range. |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 62 | std::multimap<unsigned, RegisterReference> RegRefs; |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 63 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 64 | /// KillIndices - The index of the most recent kill (proceding bottom-up), |
| 65 | /// or ~0u if the register is not live. |
| 66 | unsigned KillIndices[TargetRegisterInfo::FirstVirtualRegister]; |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 67 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 68 | /// DefIndices - The index of the most recent complete def (proceding bottom |
| 69 | /// up), or ~0u if the register is live. |
| 70 | unsigned DefIndices[TargetRegisterInfo::FirstVirtualRegister]; |
| 71 | |
| 72 | public: |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 73 | AggressiveAntiDepState(MachineBasicBlock *BB); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 74 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 75 | /// GetKillIndices - Return the kill indices. |
| 76 | unsigned *GetKillIndices() { return KillIndices; } |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 77 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 78 | /// GetDefIndices - Return the define indices. |
| 79 | unsigned *GetDefIndices() { return DefIndices; } |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 80 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 81 | /// GetRegRefs - Return the RegRefs map. |
| 82 | std::multimap<unsigned, RegisterReference>& GetRegRefs() { return RegRefs; } |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 83 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 84 | // GetGroup - Get the group for a register. The returned value is |
| 85 | // the index of the GroupNode representing the group. |
| 86 | unsigned GetGroup(unsigned Reg); |
| 87 | |
| 88 | // GetGroupRegs - Return a vector of the registers belonging to a |
| 89 | // group. |
| 90 | void GetGroupRegs(unsigned Group, std::vector<unsigned> &Regs); |
| 91 | |
| 92 | // UnionGroups - Union Reg1's and Reg2's groups to form a new |
| 93 | // group. Return the index of the GroupNode representing the |
| 94 | // group. |
| 95 | unsigned UnionGroups(unsigned Reg1, unsigned Reg2); |
| 96 | |
| 97 | // LeaveGroup - Remove a register from its current group and place |
| 98 | // it alone in its own group. Return the index of the GroupNode |
| 99 | // representing the registers new group. |
| 100 | unsigned LeaveGroup(unsigned Reg); |
| 101 | |
| 102 | /// IsLive - Return true if Reg is live |
| 103 | bool IsLive(unsigned Reg); |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | |
| 107 | /// Class AggressiveAntiDepBreaker |
| 108 | class AggressiveAntiDepBreaker : public AntiDepBreaker { |
| 109 | MachineFunction& MF; |
| 110 | MachineRegisterInfo &MRI; |
| 111 | const TargetRegisterInfo *TRI; |
| 112 | |
| 113 | /// AllocatableSet - The set of allocatable registers. |
| 114 | /// We'll be ignoring anti-dependencies on non-allocatable registers, |
| 115 | /// because they may not be safe to break. |
David Goodwin | 0855dee | 2009-11-10 00:15:47 +0000 | [diff] [blame] | 116 | BitVector AllocatableSet; |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 117 | |
| 118 | /// State - The state used to identify and rename anti-dependence |
| 119 | /// registers. |
| 120 | AggressiveAntiDepState *State; |
| 121 | |
| 122 | /// SavedState - The state for the start of an anti-dep |
| 123 | /// region. Used to restore the state at the beginning of each |
| 124 | /// pass |
| 125 | AggressiveAntiDepState *SavedState; |
| 126 | |
| 127 | public: |
David Goodwin | 0855dee | 2009-11-10 00:15:47 +0000 | [diff] [blame] | 128 | AggressiveAntiDepBreaker(MachineFunction& MFi, |
| 129 | TargetSubtarget::ExcludedRCVector& ExcludedRCs); |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 130 | ~AggressiveAntiDepBreaker(); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 131 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 132 | /// GetMaxTrials - As anti-dependencies are broken, additional |
| 133 | /// dependencies may be exposed, so multiple passes are required. |
| 134 | unsigned GetMaxTrials(); |
| 135 | |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 136 | /// NeedCandidates - Candidates required. |
| 137 | bool NeedCandidates() { return true; } |
| 138 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 139 | /// Start - Initialize anti-dep breaking for a new basic block. |
| 140 | void StartBlock(MachineBasicBlock *BB); |
| 141 | |
| 142 | /// BreakAntiDependencies - Identifiy anti-dependencies along the critical path |
| 143 | /// of the ScheduleDAG and break them by renaming registers. |
| 144 | /// |
| 145 | unsigned BreakAntiDependencies(std::vector<SUnit>& SUnits, |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 146 | CandidateMap& Candidates, |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 147 | MachineBasicBlock::iterator& Begin, |
| 148 | MachineBasicBlock::iterator& End, |
| 149 | unsigned InsertPosIndex); |
| 150 | |
| 151 | /// Observe - Update liveness information to account for the current |
| 152 | /// instruction, which will not be scheduled. |
| 153 | /// |
| 154 | void Observe(MachineInstr *MI, unsigned Count, unsigned InsertPosIndex); |
| 155 | |
| 156 | /// Finish - Finish anti-dep breaking for a basic block. |
| 157 | void FinishBlock(); |
| 158 | |
| 159 | private: |
David Goodwin | 5409783 | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 160 | typedef std::map<const TargetRegisterClass *, |
| 161 | TargetRegisterClass::const_iterator> RenameOrderType; |
| 162 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 163 | /// IsImplicitDefUse - Return true if MO represents a register |
| 164 | /// that is both implicitly used and defined in MI |
| 165 | bool IsImplicitDefUse(MachineInstr *MI, MachineOperand& MO); |
| 166 | |
| 167 | /// GetPassthruRegs - If MI implicitly def/uses a register, then |
| 168 | /// return that register and all subregisters. |
| 169 | void GetPassthruRegs(MachineInstr *MI, std::set<unsigned>& PassthruRegs); |
| 170 | |
David Goodwin | 67a8a7b | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 171 | void HandleLastUse(unsigned Reg, unsigned KillIdx, const char *tag); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 172 | void PrescanInstruction(MachineInstr *MI, unsigned Count, |
| 173 | std::set<unsigned>& PassthruRegs); |
| 174 | void ScanInstruction(MachineInstr *MI, unsigned Count); |
| 175 | BitVector GetRenameRegisters(unsigned Reg); |
| 176 | bool FindSuitableFreeRegisters(unsigned AntiDepGroupIndex, |
David Goodwin | 5409783 | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 177 | RenameOrderType& RenameOrder, |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 178 | std::map<unsigned, unsigned> &RenameMap); |
| 179 | }; |
| 180 | } |
| 181 | |
| 182 | #endif |