Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 1 | //===- HexagonBlockRanges.h -------------------------------------*- C++ -*-===// |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +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 | //===----------------------------------------------------------------------===// |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 9 | |
| 10 | #ifndef LLVM_LIB_TARGET_HEXAGON_HEXAGONBLOCKRANGES_H |
| 11 | #define LLVM_LIB_TARGET_HEXAGON_HEXAGONBLOCKRANGES_H |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 12 | |
| 13 | #include "llvm/ADT/BitVector.h" |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 14 | #include <cassert> |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 15 | #include <map> |
| 16 | #include <set> |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 17 | #include <utility> |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 18 | #include <vector> |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 19 | |
| 20 | namespace llvm { |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 21 | |
| 22 | class HexagonSubtarget; |
| 23 | class MachineBasicBlock; |
| 24 | class MachineFunction; |
| 25 | class MachineInstr; |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 26 | class MachineRegisterInfo; |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 27 | class raw_ostream; |
| 28 | class TargetInstrInfo; |
| 29 | class TargetRegisterInfo; |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 30 | |
| 31 | struct HexagonBlockRanges { |
| 32 | HexagonBlockRanges(MachineFunction &MF); |
| 33 | |
| 34 | struct RegisterRef { |
| 35 | unsigned Reg, Sub; |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 36 | |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 37 | bool operator<(RegisterRef R) const { |
| 38 | return Reg < R.Reg || (Reg == R.Reg && Sub < R.Sub); |
| 39 | } |
| 40 | }; |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 41 | using RegisterSet = std::set<RegisterRef>; |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 42 | |
| 43 | // This is to represent an "index", which is an abstraction of a position |
| 44 | // of an instruction within a basic block. |
| 45 | class IndexType { |
| 46 | public: |
| 47 | enum : unsigned { |
| 48 | None = 0, |
| 49 | Entry = 1, |
| 50 | Exit = 2, |
| 51 | First = 11 // 10th + 1st |
| 52 | }; |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 53 | |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 54 | IndexType() {} |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 55 | IndexType(unsigned Idx) : Index(Idx) {} |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 56 | |
| 57 | static bool isInstr(IndexType X) { return X.Index >= First; } |
| 58 | |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 59 | operator unsigned() const; |
| 60 | bool operator== (unsigned x) const; |
| 61 | bool operator== (IndexType Idx) const; |
| 62 | bool operator!= (unsigned x) const; |
| 63 | bool operator!= (IndexType Idx) const; |
| 64 | IndexType operator++ (); |
| 65 | bool operator< (unsigned Idx) const; |
| 66 | bool operator< (IndexType Idx) const; |
| 67 | bool operator<= (IndexType Idx) const; |
| 68 | |
| 69 | private: |
| 70 | bool operator> (IndexType Idx) const; |
| 71 | bool operator>= (IndexType Idx) const; |
| 72 | |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 73 | unsigned Index = None; |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | // A range of indices, essentially a representation of a live range. |
| 77 | // This is also used to represent "dead ranges", i.e. ranges where a |
| 78 | // register is dead. |
Eugene Zelenko | efd3d58 | 2017-07-26 23:45:28 +0000 | [diff] [blame] | 79 | class IndexRange : public std::pair<IndexType,IndexType> { |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 80 | public: |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 81 | IndexRange() = default; |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 82 | IndexRange(IndexType Start, IndexType End, bool F = false, bool T = false) |
| 83 | : std::pair<IndexType,IndexType>(Start, End), Fixed(F), TiedEnd(T) {} |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 84 | |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 85 | IndexType start() const { return first; } |
| 86 | IndexType end() const { return second; } |
| 87 | |
| 88 | bool operator< (const IndexRange &A) const { |
| 89 | return start() < A.start(); |
| 90 | } |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 91 | |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 92 | bool overlaps(const IndexRange &A) const; |
| 93 | bool contains(const IndexRange &A) const; |
| 94 | void merge(const IndexRange &A); |
| 95 | |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 96 | bool Fixed = false; // Can be renamed? "Fixed" means "no". |
| 97 | bool TiedEnd = false; // The end is not a use, but a dead def tied to a use. |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 98 | |
| 99 | private: |
| 100 | void setStart(const IndexType &S) { first = S; } |
| 101 | void setEnd(const IndexType &E) { second = E; } |
| 102 | }; |
| 103 | |
| 104 | // A list of index ranges. This represents liveness of a register |
| 105 | // in a basic block. |
| 106 | class RangeList : public std::vector<IndexRange> { |
| 107 | public: |
| 108 | void add(IndexType Start, IndexType End, bool Fixed, bool TiedEnd) { |
| 109 | push_back(IndexRange(Start, End, Fixed, TiedEnd)); |
| 110 | } |
| 111 | void add(const IndexRange &Range) { |
| 112 | push_back(Range); |
| 113 | } |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 114 | |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 115 | void include(const RangeList &RL); |
| 116 | void unionize(bool MergeAdjacent = false); |
| 117 | void subtract(const IndexRange &Range); |
| 118 | |
| 119 | private: |
| 120 | void addsub(const IndexRange &A, const IndexRange &B); |
| 121 | }; |
| 122 | |
| 123 | class InstrIndexMap { |
| 124 | public: |
| 125 | InstrIndexMap(MachineBasicBlock &B); |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 126 | |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 127 | MachineInstr *getInstr(IndexType Idx) const; |
| 128 | IndexType getIndex(MachineInstr *MI) const; |
| 129 | MachineBasicBlock &getBlock() const { return Block; } |
| 130 | IndexType getPrevIndex(IndexType Idx) const; |
| 131 | IndexType getNextIndex(IndexType Idx) const; |
| 132 | void replaceInstr(MachineInstr *OldMI, MachineInstr *NewMI); |
| 133 | |
| 134 | friend raw_ostream &operator<< (raw_ostream &OS, const InstrIndexMap &Map); |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 135 | |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 136 | IndexType First, Last; |
| 137 | |
| 138 | private: |
| 139 | MachineBasicBlock &Block; |
| 140 | std::map<IndexType,MachineInstr*> Map; |
| 141 | }; |
| 142 | |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 143 | using RegToRangeMap = std::map<RegisterRef, RangeList>; |
| 144 | |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 145 | RegToRangeMap computeLiveMap(InstrIndexMap &IndexMap); |
| 146 | RegToRangeMap computeDeadMap(InstrIndexMap &IndexMap, RegToRangeMap &LiveMap); |
| 147 | static RegisterSet expandToSubRegs(RegisterRef R, |
| 148 | const MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI); |
| 149 | |
| 150 | struct PrintRangeMap { |
| 151 | PrintRangeMap(const RegToRangeMap &M, const TargetRegisterInfo &I) |
| 152 | : Map(M), TRI(I) {} |
| 153 | |
| 154 | friend raw_ostream &operator<< (raw_ostream &OS, const PrintRangeMap &P); |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 155 | |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 156 | private: |
| 157 | const RegToRangeMap ⤅ |
| 158 | const TargetRegisterInfo &TRI; |
| 159 | }; |
| 160 | |
| 161 | private: |
Krzysztof Parzyszek | 5bb417b | 2016-10-18 19:47:20 +0000 | [diff] [blame] | 162 | RegisterSet getLiveIns(const MachineBasicBlock &B, |
| 163 | const MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI); |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 164 | |
| 165 | void computeInitialLiveRanges(InstrIndexMap &IndexMap, |
| 166 | RegToRangeMap &LiveMap); |
| 167 | |
| 168 | MachineFunction &MF; |
| 169 | const HexagonSubtarget &HST; |
| 170 | const TargetInstrInfo &TII; |
| 171 | const TargetRegisterInfo &TRI; |
| 172 | BitVector Reserved; |
| 173 | }; |
| 174 | |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 175 | inline HexagonBlockRanges::IndexType::operator unsigned() const { |
| 176 | assert(Index >= First); |
| 177 | return Index; |
| 178 | } |
| 179 | |
| 180 | inline bool HexagonBlockRanges::IndexType::operator== (unsigned x) const { |
| 181 | return Index == x; |
| 182 | } |
| 183 | |
| 184 | inline bool HexagonBlockRanges::IndexType::operator== (IndexType Idx) const { |
| 185 | return Index == Idx.Index; |
| 186 | } |
| 187 | |
| 188 | inline bool HexagonBlockRanges::IndexType::operator!= (unsigned x) const { |
| 189 | return Index != x; |
| 190 | } |
| 191 | |
| 192 | inline bool HexagonBlockRanges::IndexType::operator!= (IndexType Idx) const { |
| 193 | return Index != Idx.Index; |
| 194 | } |
| 195 | |
| 196 | inline |
| 197 | HexagonBlockRanges::IndexType HexagonBlockRanges::IndexType::operator++ () { |
| 198 | assert(Index != None); |
| 199 | assert(Index != Exit); |
| 200 | if (Index == Entry) |
| 201 | Index = First; |
| 202 | else |
| 203 | ++Index; |
| 204 | return *this; |
| 205 | } |
| 206 | |
| 207 | inline bool HexagonBlockRanges::IndexType::operator< (unsigned Idx) const { |
| 208 | return operator< (IndexType(Idx)); |
| 209 | } |
| 210 | |
| 211 | inline bool HexagonBlockRanges::IndexType::operator< (IndexType Idx) const { |
| 212 | // !(x < x). |
| 213 | if (Index == Idx.Index) |
| 214 | return false; |
| 215 | // !(None < x) for all x. |
| 216 | // !(x < None) for all x. |
| 217 | if (Index == None || Idx.Index == None) |
| 218 | return false; |
| 219 | // !(Exit < x) for all x. |
| 220 | // !(x < Entry) for all x. |
| 221 | if (Index == Exit || Idx.Index == Entry) |
| 222 | return false; |
| 223 | // Entry < x for all x != Entry. |
| 224 | // x < Exit for all x != Exit. |
| 225 | if (Index == Entry || Idx.Index == Exit) |
| 226 | return true; |
| 227 | |
| 228 | return Index < Idx.Index; |
| 229 | } |
| 230 | |
| 231 | inline bool HexagonBlockRanges::IndexType::operator<= (IndexType Idx) const { |
| 232 | return operator==(Idx) || operator<(Idx); |
| 233 | } |
| 234 | |
Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 235 | raw_ostream &operator<< (raw_ostream &OS, HexagonBlockRanges::IndexType Idx); |
| 236 | raw_ostream &operator<< (raw_ostream &OS, |
| 237 | const HexagonBlockRanges::IndexRange &IR); |
| 238 | raw_ostream &operator<< (raw_ostream &OS, |
| 239 | const HexagonBlockRanges::RangeList &RL); |
| 240 | raw_ostream &operator<< (raw_ostream &OS, |
| 241 | const HexagonBlockRanges::InstrIndexMap &M); |
| 242 | raw_ostream &operator<< (raw_ostream &OS, |
| 243 | const HexagonBlockRanges::PrintRangeMap &P); |
| 244 | |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 245 | } // end namespace llvm |
Benjamin Kramer | 922efd7 | 2016-05-27 10:06:40 +0000 | [diff] [blame] | 246 | |
Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 247 | #endif // LLVM_LIB_TARGET_HEXAGON_HEXAGONBLOCKRANGES_H |