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" |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 30 | #include <map> |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 31 | |
| 32 | namespace llvm { |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 33 | /// Class AggressiveAntiDepState |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 34 | /// Contains all the state necessary for anti-dep breaking. |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 35 | class AggressiveAntiDepState { |
| 36 | public: |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 37 | /// RegisterReference - Information about a register reference |
| 38 | /// within a liverange |
| 39 | typedef struct { |
| 40 | /// Operand - The registers operand |
| 41 | MachineOperand *Operand; |
| 42 | /// RC - The register class |
| 43 | const TargetRegisterClass *RC; |
| 44 | } RegisterReference; |
| 45 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 46 | private: |
David Goodwin | 990d285 | 2009-12-09 17:18:22 +0000 | [diff] [blame] | 47 | /// NumTargetRegs - Number of non-virtual target registers |
| 48 | /// (i.e. TRI->getNumRegs()). |
| 49 | const unsigned NumTargetRegs; |
| 50 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 51 | /// GroupNodes - Implements a disjoint-union data structure to |
| 52 | /// form register groups. A node is represented by an index into |
| 53 | /// the vector. A node can "point to" itself to indicate that it |
| 54 | /// is the parent of a group, or point to another node to indicate |
| 55 | /// that it is a member of the same group as that node. |
| 56 | std::vector<unsigned> GroupNodes; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 57 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 58 | /// GroupNodeIndices - For each register, the index of the GroupNode |
| 59 | /// currently representing the group that the register belongs to. |
| 60 | /// Register 0 is always represented by the 0 group, a group |
| 61 | /// composed of registers that are not eligible for anti-aliasing. |
| 62 | unsigned GroupNodeIndices[TargetRegisterInfo::FirstVirtualRegister]; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 63 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 64 | /// RegRefs - Map registers to all their references within a live range. |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 65 | std::multimap<unsigned, RegisterReference> RegRefs; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 66 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 67 | /// KillIndices - The index of the most recent kill (proceding bottom-up), |
| 68 | /// or ~0u if the register is not live. |
| 69 | unsigned KillIndices[TargetRegisterInfo::FirstVirtualRegister]; |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 70 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 71 | /// DefIndices - The index of the most recent complete def (proceding bottom |
| 72 | /// up), or ~0u if the register is live. |
| 73 | unsigned DefIndices[TargetRegisterInfo::FirstVirtualRegister]; |
| 74 | |
| 75 | public: |
David Goodwin | 990d285 | 2009-12-09 17:18:22 +0000 | [diff] [blame] | 76 | AggressiveAntiDepState(const unsigned TargetRegs, MachineBasicBlock *BB); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 77 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 78 | /// GetKillIndices - Return the kill indices. |
| 79 | unsigned *GetKillIndices() { return KillIndices; } |
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 | /// GetDefIndices - Return the define indices. |
| 82 | unsigned *GetDefIndices() { return DefIndices; } |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 83 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 84 | /// GetRegRefs - Return the RegRefs map. |
| 85 | std::multimap<unsigned, RegisterReference>& GetRegRefs() { return RegRefs; } |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 86 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 87 | // GetGroup - Get the group for a register. The returned value is |
| 88 | // the index of the GroupNode representing the group. |
| 89 | unsigned GetGroup(unsigned Reg); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 90 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 91 | // GetGroupRegs - Return a vector of the registers belonging to a |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 92 | // group. If RegRefs is non-NULL then only included referenced registers. |
| 93 | void GetGroupRegs( |
| 94 | unsigned Group, |
| 95 | std::vector<unsigned> &Regs, |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 96 | std::multimap<unsigned, |
| 97 | AggressiveAntiDepState::RegisterReference> *RegRefs); |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 98 | |
| 99 | // UnionGroups - Union Reg1's and Reg2's groups to form a new |
| 100 | // group. Return the index of the GroupNode representing the |
| 101 | // group. |
| 102 | unsigned UnionGroups(unsigned Reg1, unsigned Reg2); |
| 103 | |
| 104 | // LeaveGroup - Remove a register from its current group and place |
| 105 | // it alone in its own group. Return the index of the GroupNode |
| 106 | // representing the registers new group. |
| 107 | unsigned LeaveGroup(unsigned Reg); |
| 108 | |
| 109 | /// IsLive - Return true if Reg is live |
| 110 | bool IsLive(unsigned Reg); |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 114 | /// Class AggressiveAntiDepBreaker |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 115 | class AggressiveAntiDepBreaker : public AntiDepBreaker { |
| 116 | MachineFunction& MF; |
| 117 | MachineRegisterInfo &MRI; |
| 118 | const TargetRegisterInfo *TRI; |
| 119 | |
| 120 | /// AllocatableSet - The set of allocatable registers. |
| 121 | /// We'll be ignoring anti-dependencies on non-allocatable registers, |
| 122 | /// because they may not be safe to break. |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 123 | const BitVector AllocatableSet; |
| 124 | |
| 125 | /// CriticalPathSet - The set of registers that should only be |
| 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 | |
| 129 | /// State - The state used to identify and rename anti-dependence |
| 130 | /// registers. |
| 131 | AggressiveAntiDepState *State; |
| 132 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 133 | public: |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 134 | AggressiveAntiDepBreaker(MachineFunction& MFi, |
David Goodwin | 87d21b9 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 135 | TargetSubtarget::RegClassVector& CriticalPathRCs); |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 136 | ~AggressiveAntiDepBreaker(); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 137 | |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 138 | /// Start - Initialize anti-dep breaking for a new basic block. |
| 139 | void StartBlock(MachineBasicBlock *BB); |
| 140 | |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 141 | /// BreakAntiDependencies - Identifiy anti-dependencies along the critical |
| 142 | /// path |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 143 | /// of the ScheduleDAG and break them by renaming registers. |
| 144 | /// |
Dan Gohman | 66db3a0 | 2010-04-19 23:11:58 +0000 | [diff] [blame^] | 145 | unsigned BreakAntiDependencies(const std::vector<SUnit>& SUnits, |
| 146 | MachineBasicBlock::iterator Begin, |
| 147 | MachineBasicBlock::iterator End, |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 148 | unsigned InsertPosIndex); |
| 149 | |
| 150 | /// Observe - Update liveness information to account for the current |
| 151 | /// instruction, which will not be scheduled. |
| 152 | /// |
| 153 | void Observe(MachineInstr *MI, unsigned Count, unsigned InsertPosIndex); |
| 154 | |
| 155 | /// Finish - Finish anti-dep breaking for a basic block. |
| 156 | void FinishBlock(); |
| 157 | |
| 158 | private: |
David Goodwin | 5409783 | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 159 | typedef std::map<const TargetRegisterClass *, |
| 160 | TargetRegisterClass::const_iterator> RenameOrderType; |
| 161 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 162 | /// IsImplicitDefUse - Return true if MO represents a register |
| 163 | /// that is both implicitly used and defined in MI |
| 164 | bool IsImplicitDefUse(MachineInstr *MI, MachineOperand& MO); |
Jim Grosbach | 2973b57 | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 165 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 166 | /// GetPassthruRegs - If MI implicitly def/uses a register, then |
| 167 | /// return that register and all subregisters. |
| 168 | void GetPassthruRegs(MachineInstr *MI, std::set<unsigned>& PassthruRegs); |
| 169 | |
David Goodwin | 3e72d30 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 170 | void HandleLastUse(unsigned Reg, unsigned KillIdx, const char *tag, |
| 171 | const char *header =NULL, const char *footer =NULL); |
| 172 | |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 173 | void PrescanInstruction(MachineInstr *MI, unsigned Count, |
| 174 | std::set<unsigned>& PassthruRegs); |
| 175 | void ScanInstruction(MachineInstr *MI, unsigned Count); |
| 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 | }; |
| 181 | } |
| 182 | |
| 183 | #endif |