| Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 1 | //===- HexagonBlockRanges.cpp ---------------------------------------------===// | 
| 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 | //===----------------------------------------------------------------------===// | 
|  | 9 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 10 | #include "HexagonBlockRanges.h" | 
|  | 11 | #include "HexagonInstrInfo.h" | 
|  | 12 | #include "HexagonSubtarget.h" | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/BitVector.h" | 
| Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/STLExtras.h" | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineBasicBlock.h" | 
| Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFunction.h" | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineInstr.h" | 
| Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineOperand.h" | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineRegisterInfo.h" | 
| David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/TargetRegisterInfo.h" | 
| Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCRegisterInfo.h" | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" | 
|  | 23 | #include "llvm/Support/raw_ostream.h" | 
| Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 24 | #include <algorithm> | 
|  | 25 | #include <cassert> | 
| Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 26 | #include <cstdint> | 
| Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 27 | #include <iterator> | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 28 | #include <map> | 
| Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 29 | #include <utility> | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 30 |  | 
|  | 31 | using namespace llvm; | 
|  | 32 |  | 
| Eugene Zelenko | 3b87336 | 2017-09-28 22:27:31 +0000 | [diff] [blame] | 33 | #define DEBUG_TYPE "hbr" | 
|  | 34 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 35 | bool HexagonBlockRanges::IndexRange::overlaps(const IndexRange &A) const { | 
|  | 36 | // If A contains start(), or "this" contains A.start(), then overlap. | 
|  | 37 | IndexType S = start(), E = end(), AS = A.start(), AE = A.end(); | 
|  | 38 | if (AS == S) | 
|  | 39 | return true; | 
|  | 40 | bool SbAE = (S < AE) || (S == AE && A.TiedEnd);  // S-before-AE. | 
|  | 41 | bool ASbE = (AS < E) || (AS == E && TiedEnd);    // AS-before-E. | 
|  | 42 | if ((AS < S && SbAE) || (S < AS && ASbE)) | 
|  | 43 | return true; | 
|  | 44 | // Otherwise no overlap. | 
|  | 45 | return false; | 
|  | 46 | } | 
|  | 47 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 48 | bool HexagonBlockRanges::IndexRange::contains(const IndexRange &A) const { | 
|  | 49 | if (start() <= A.start()) { | 
|  | 50 | // Treat "None" in the range end as equal to the range start. | 
|  | 51 | IndexType E = (end() != IndexType::None) ? end() : start(); | 
|  | 52 | IndexType AE = (A.end() != IndexType::None) ? A.end() : A.start(); | 
|  | 53 | if (AE <= E) | 
|  | 54 | return true; | 
|  | 55 | } | 
|  | 56 | return false; | 
|  | 57 | } | 
|  | 58 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 59 | void HexagonBlockRanges::IndexRange::merge(const IndexRange &A) { | 
|  | 60 | // Allow merging adjacent ranges. | 
|  | 61 | assert(end() == A.start() || overlaps(A)); | 
|  | 62 | IndexType AS = A.start(), AE = A.end(); | 
|  | 63 | if (AS < start() || start() == IndexType::None) | 
|  | 64 | setStart(AS); | 
|  | 65 | if (end() < AE || end() == IndexType::None) { | 
|  | 66 | setEnd(AE); | 
|  | 67 | TiedEnd = A.TiedEnd; | 
|  | 68 | } else { | 
|  | 69 | if (end() == AE) | 
|  | 70 | TiedEnd |= A.TiedEnd; | 
|  | 71 | } | 
|  | 72 | if (A.Fixed) | 
|  | 73 | Fixed = true; | 
|  | 74 | } | 
|  | 75 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 76 | void HexagonBlockRanges::RangeList::include(const RangeList &RL) { | 
|  | 77 | for (auto &R : RL) | 
| David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 78 | if (!is_contained(*this, R)) | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 79 | push_back(R); | 
|  | 80 | } | 
|  | 81 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 82 | // Merge all overlapping ranges in the list, so that all that remains | 
|  | 83 | // is a list of disjoint ranges. | 
|  | 84 | void HexagonBlockRanges::RangeList::unionize(bool MergeAdjacent) { | 
|  | 85 | if (empty()) | 
|  | 86 | return; | 
|  | 87 |  | 
|  | 88 | std::sort(begin(), end()); | 
|  | 89 | iterator Iter = begin(); | 
|  | 90 |  | 
|  | 91 | while (Iter != end()-1) { | 
|  | 92 | iterator Next = std::next(Iter); | 
|  | 93 | // If MergeAdjacent is true, merge ranges A and B, where A.end == B.start. | 
|  | 94 | // This allows merging dead ranges, but is not valid for live ranges. | 
|  | 95 | bool Merge = MergeAdjacent && (Iter->end() == Next->start()); | 
|  | 96 | if (Merge || Iter->overlaps(*Next)) { | 
|  | 97 | Iter->merge(*Next); | 
|  | 98 | erase(Next); | 
|  | 99 | continue; | 
|  | 100 | } | 
|  | 101 | ++Iter; | 
|  | 102 | } | 
|  | 103 | } | 
|  | 104 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 105 | // Compute a range A-B and add it to the list. | 
|  | 106 | void HexagonBlockRanges::RangeList::addsub(const IndexRange &A, | 
|  | 107 | const IndexRange &B) { | 
|  | 108 | // Exclusion of non-overlapping ranges makes some checks simpler | 
|  | 109 | // later in this function. | 
|  | 110 | if (!A.overlaps(B)) { | 
|  | 111 | // A - B = A. | 
|  | 112 | add(A); | 
|  | 113 | return; | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | IndexType AS = A.start(), AE = A.end(); | 
|  | 117 | IndexType BS = B.start(), BE = B.end(); | 
|  | 118 |  | 
|  | 119 | // If AE is None, then A is included in B, since A and B overlap. | 
|  | 120 | // The result of subtraction if empty, so just return. | 
|  | 121 | if (AE == IndexType::None) | 
|  | 122 | return; | 
|  | 123 |  | 
|  | 124 | if (AS < BS) { | 
|  | 125 | // A starts before B. | 
|  | 126 | // AE cannot be None since A and B overlap. | 
|  | 127 | assert(AE != IndexType::None); | 
|  | 128 | // Add the part of A that extends on the "less" side of B. | 
|  | 129 | add(AS, BS, A.Fixed, false); | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | if (BE < AE) { | 
|  | 133 | // BE cannot be Exit here. | 
|  | 134 | if (BE == IndexType::None) | 
|  | 135 | add(BS, AE, A.Fixed, false); | 
|  | 136 | else | 
|  | 137 | add(BE, AE, A.Fixed, false); | 
|  | 138 | } | 
|  | 139 | } | 
|  | 140 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 141 | // Subtract a given range from each element in the list. | 
|  | 142 | void HexagonBlockRanges::RangeList::subtract(const IndexRange &Range) { | 
|  | 143 | // Cannot assume that the list is unionized (i.e. contains only non- | 
|  | 144 | // overlapping ranges. | 
|  | 145 | RangeList T; | 
|  | 146 | for (iterator Next, I = begin(); I != end(); I = Next) { | 
|  | 147 | IndexRange &Rg = *I; | 
|  | 148 | if (Rg.overlaps(Range)) { | 
|  | 149 | T.addsub(Rg, Range); | 
|  | 150 | Next = this->erase(I); | 
|  | 151 | } else { | 
|  | 152 | Next = std::next(I); | 
|  | 153 | } | 
|  | 154 | } | 
|  | 155 | include(T); | 
|  | 156 | } | 
|  | 157 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 158 | HexagonBlockRanges::InstrIndexMap::InstrIndexMap(MachineBasicBlock &B) | 
|  | 159 | : Block(B) { | 
|  | 160 | IndexType Idx = IndexType::First; | 
|  | 161 | First = Idx; | 
|  | 162 | for (auto &In : B) { | 
|  | 163 | if (In.isDebugValue()) | 
|  | 164 | continue; | 
|  | 165 | assert(getIndex(&In) == IndexType::None && "Instruction already in map"); | 
|  | 166 | Map.insert(std::make_pair(Idx, &In)); | 
|  | 167 | ++Idx; | 
|  | 168 | } | 
|  | 169 | Last = B.empty() ? IndexType::None : unsigned(Idx)-1; | 
|  | 170 | } | 
|  | 171 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 172 | MachineInstr *HexagonBlockRanges::InstrIndexMap::getInstr(IndexType Idx) const { | 
|  | 173 | auto F = Map.find(Idx); | 
| Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 174 | return (F != Map.end()) ? F->second : nullptr; | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 175 | } | 
|  | 176 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 177 | HexagonBlockRanges::IndexType HexagonBlockRanges::InstrIndexMap::getIndex( | 
|  | 178 | MachineInstr *MI) const { | 
|  | 179 | for (auto &I : Map) | 
|  | 180 | if (I.second == MI) | 
|  | 181 | return I.first; | 
|  | 182 | return IndexType::None; | 
|  | 183 | } | 
|  | 184 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 185 | HexagonBlockRanges::IndexType HexagonBlockRanges::InstrIndexMap::getPrevIndex( | 
|  | 186 | IndexType Idx) const { | 
|  | 187 | assert (Idx != IndexType::None); | 
|  | 188 | if (Idx == IndexType::Entry) | 
|  | 189 | return IndexType::None; | 
|  | 190 | if (Idx == IndexType::Exit) | 
|  | 191 | return Last; | 
|  | 192 | if (Idx == First) | 
|  | 193 | return IndexType::Entry; | 
|  | 194 | return unsigned(Idx)-1; | 
|  | 195 | } | 
|  | 196 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 197 | HexagonBlockRanges::IndexType HexagonBlockRanges::InstrIndexMap::getNextIndex( | 
|  | 198 | IndexType Idx) const { | 
|  | 199 | assert (Idx != IndexType::None); | 
|  | 200 | if (Idx == IndexType::Entry) | 
|  | 201 | return IndexType::First; | 
|  | 202 | if (Idx == IndexType::Exit || Idx == Last) | 
|  | 203 | return IndexType::None; | 
|  | 204 | return unsigned(Idx)+1; | 
|  | 205 | } | 
|  | 206 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 207 | void HexagonBlockRanges::InstrIndexMap::replaceInstr(MachineInstr *OldMI, | 
|  | 208 | MachineInstr *NewMI) { | 
|  | 209 | for (auto &I : Map) { | 
|  | 210 | if (I.second != OldMI) | 
|  | 211 | continue; | 
|  | 212 | if (NewMI != nullptr) | 
|  | 213 | I.second = NewMI; | 
|  | 214 | else | 
|  | 215 | Map.erase(I.first); | 
|  | 216 | break; | 
|  | 217 | } | 
|  | 218 | } | 
|  | 219 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 220 | HexagonBlockRanges::HexagonBlockRanges(MachineFunction &mf) | 
|  | 221 | : MF(mf), HST(mf.getSubtarget<HexagonSubtarget>()), | 
|  | 222 | TII(*HST.getInstrInfo()), TRI(*HST.getRegisterInfo()), | 
|  | 223 | Reserved(TRI.getReservedRegs(mf)) { | 
|  | 224 | // Consider all non-allocatable registers as reserved. | 
| Krzysztof Parzyszek | ee9aa3f | 2017-01-25 19:29:04 +0000 | [diff] [blame] | 225 | for (const TargetRegisterClass *RC : TRI.regclasses()) { | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 226 | if (RC->isAllocatable()) | 
|  | 227 | continue; | 
|  | 228 | for (unsigned R : *RC) | 
|  | 229 | Reserved[R] = true; | 
|  | 230 | } | 
|  | 231 | } | 
|  | 232 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 233 | HexagonBlockRanges::RegisterSet HexagonBlockRanges::getLiveIns( | 
| Krzysztof Parzyszek | 5bb417b | 2016-10-18 19:47:20 +0000 | [diff] [blame] | 234 | const MachineBasicBlock &B, const MachineRegisterInfo &MRI, | 
|  | 235 | const TargetRegisterInfo &TRI) { | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 236 | RegisterSet LiveIns; | 
| Krzysztof Parzyszek | 5bb417b | 2016-10-18 19:47:20 +0000 | [diff] [blame] | 237 | RegisterSet Tmp; | 
| Krzysztof Parzyszek | f928e24 | 2017-04-14 16:21:55 +0000 | [diff] [blame] | 238 |  | 
| Krzysztof Parzyszek | 5bb417b | 2016-10-18 19:47:20 +0000 | [diff] [blame] | 239 | for (auto I : B.liveins()) { | 
| Krzysztof Parzyszek | f928e24 | 2017-04-14 16:21:55 +0000 | [diff] [blame] | 240 | MCSubRegIndexIterator S(I.PhysReg, &TRI); | 
|  | 241 | if (I.LaneMask.all() || (I.LaneMask.any() && !S.isValid())) { | 
|  | 242 | Tmp.insert({I.PhysReg, 0}); | 
| Krzysztof Parzyszek | 5bb417b | 2016-10-18 19:47:20 +0000 | [diff] [blame] | 243 | continue; | 
|  | 244 | } | 
| Krzysztof Parzyszek | f928e24 | 2017-04-14 16:21:55 +0000 | [diff] [blame] | 245 | for (; S.isValid(); ++S) { | 
|  | 246 | unsigned SI = S.getSubRegIndex(); | 
|  | 247 | if ((I.LaneMask & TRI.getSubRegIndexLaneMask(SI)).any()) | 
| Krzysztof Parzyszek | 5bb417b | 2016-10-18 19:47:20 +0000 | [diff] [blame] | 248 | Tmp.insert({S.getSubReg(), 0}); | 
|  | 249 | } | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | for (auto R : Tmp) { | 
|  | 253 | if (!Reserved[R.Reg]) | 
|  | 254 | LiveIns.insert(R); | 
|  | 255 | for (auto S : expandToSubRegs(R, MRI, TRI)) | 
|  | 256 | if (!Reserved[S.Reg]) | 
|  | 257 | LiveIns.insert(S); | 
|  | 258 | } | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 259 | return LiveIns; | 
|  | 260 | } | 
|  | 261 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 262 | HexagonBlockRanges::RegisterSet HexagonBlockRanges::expandToSubRegs( | 
|  | 263 | RegisterRef R, const MachineRegisterInfo &MRI, | 
|  | 264 | const TargetRegisterInfo &TRI) { | 
|  | 265 | RegisterSet SRs; | 
|  | 266 |  | 
|  | 267 | if (R.Sub != 0) { | 
|  | 268 | SRs.insert(R); | 
|  | 269 | return SRs; | 
|  | 270 | } | 
|  | 271 |  | 
|  | 272 | if (TargetRegisterInfo::isPhysicalRegister(R.Reg)) { | 
|  | 273 | MCSubRegIterator I(R.Reg, &TRI); | 
|  | 274 | if (!I.isValid()) | 
|  | 275 | SRs.insert({R.Reg, 0}); | 
|  | 276 | for (; I.isValid(); ++I) | 
|  | 277 | SRs.insert({*I, 0}); | 
|  | 278 | } else { | 
|  | 279 | assert(TargetRegisterInfo::isVirtualRegister(R.Reg)); | 
|  | 280 | auto &RC = *MRI.getRegClass(R.Reg); | 
|  | 281 | unsigned PReg = *RC.begin(); | 
|  | 282 | MCSubRegIndexIterator I(PReg, &TRI); | 
|  | 283 | if (!I.isValid()) | 
|  | 284 | SRs.insert({R.Reg, 0}); | 
|  | 285 | for (; I.isValid(); ++I) | 
|  | 286 | SRs.insert({R.Reg, I.getSubRegIndex()}); | 
|  | 287 | } | 
|  | 288 | return SRs; | 
|  | 289 | } | 
|  | 290 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 291 | void HexagonBlockRanges::computeInitialLiveRanges(InstrIndexMap &IndexMap, | 
|  | 292 | RegToRangeMap &LiveMap) { | 
|  | 293 | std::map<RegisterRef,IndexType> LastDef, LastUse; | 
|  | 294 | RegisterSet LiveOnEntry; | 
|  | 295 | MachineBasicBlock &B = IndexMap.getBlock(); | 
|  | 296 | MachineRegisterInfo &MRI = B.getParent()->getRegInfo(); | 
|  | 297 |  | 
| Krzysztof Parzyszek | 5bb417b | 2016-10-18 19:47:20 +0000 | [diff] [blame] | 298 | for (auto R : getLiveIns(B, MRI, TRI)) | 
|  | 299 | LiveOnEntry.insert(R); | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 300 |  | 
|  | 301 | for (auto R : LiveOnEntry) | 
|  | 302 | LastDef[R] = IndexType::Entry; | 
|  | 303 |  | 
|  | 304 | auto closeRange = [&LastUse,&LastDef,&LiveMap] (RegisterRef R) -> void { | 
|  | 305 | auto LD = LastDef[R], LU = LastUse[R]; | 
|  | 306 | if (LD == IndexType::None) | 
|  | 307 | LD = IndexType::Entry; | 
|  | 308 | if (LU == IndexType::None) | 
|  | 309 | LU = IndexType::Exit; | 
|  | 310 | LiveMap[R].add(LD, LU, false, false); | 
|  | 311 | LastUse[R] = LastDef[R] = IndexType::None; | 
|  | 312 | }; | 
|  | 313 |  | 
| Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 314 | RegisterSet Defs, Clobbers; | 
|  | 315 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 316 | for (auto &In : B) { | 
|  | 317 | if (In.isDebugValue()) | 
|  | 318 | continue; | 
|  | 319 | IndexType Index = IndexMap.getIndex(&In); | 
|  | 320 | // Process uses first. | 
|  | 321 | for (auto &Op : In.operands()) { | 
|  | 322 | if (!Op.isReg() || !Op.isUse() || Op.isUndef()) | 
|  | 323 | continue; | 
|  | 324 | RegisterRef R = { Op.getReg(), Op.getSubReg() }; | 
|  | 325 | if (TargetRegisterInfo::isPhysicalRegister(R.Reg) && Reserved[R.Reg]) | 
|  | 326 | continue; | 
|  | 327 | bool IsKill = Op.isKill(); | 
|  | 328 | for (auto S : expandToSubRegs(R, MRI, TRI)) { | 
|  | 329 | LastUse[S] = Index; | 
|  | 330 | if (IsKill) | 
|  | 331 | closeRange(S); | 
|  | 332 | } | 
|  | 333 | } | 
| Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 334 | // Process defs and clobbers. | 
|  | 335 | Defs.clear(); | 
|  | 336 | Clobbers.clear(); | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 337 | for (auto &Op : In.operands()) { | 
|  | 338 | if (!Op.isReg() || !Op.isDef() || Op.isUndef()) | 
|  | 339 | continue; | 
|  | 340 | RegisterRef R = { Op.getReg(), Op.getSubReg() }; | 
| Rafael Espindola | 6eab404 | 2017-02-17 02:08:58 +0000 | [diff] [blame] | 341 | for (auto S : expandToSubRegs(R, MRI, TRI)) { | 
| Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 342 | if (TargetRegisterInfo::isPhysicalRegister(S.Reg) && Reserved[S.Reg]) | 
|  | 343 | continue; | 
|  | 344 | if (Op.isDead()) | 
|  | 345 | Clobbers.insert(S); | 
|  | 346 | else | 
|  | 347 | Defs.insert(S); | 
| Krzysztof Parzyszek | fb9503c | 2017-02-16 20:25:23 +0000 | [diff] [blame] | 348 | } | 
|  | 349 | } | 
| Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 350 |  | 
|  | 351 | for (auto &Op : In.operands()) { | 
|  | 352 | if (!Op.isRegMask()) | 
|  | 353 | continue; | 
|  | 354 | const uint32_t *BM = Op.getRegMask(); | 
|  | 355 | for (unsigned PR = 1, N = TRI.getNumRegs(); PR != N; ++PR) { | 
|  | 356 | // Skip registers that have subregisters. A register is preserved | 
|  | 357 | // iff its bit is set in the regmask, so if R1:0 was preserved, both | 
|  | 358 | // R1 and R0 would also be present. | 
|  | 359 | if (MCSubRegIterator(PR, &TRI, false).isValid()) | 
|  | 360 | continue; | 
|  | 361 | if (Reserved[PR]) | 
|  | 362 | continue; | 
|  | 363 | if (BM[PR/32] & (1u << (PR%32))) | 
|  | 364 | continue; | 
|  | 365 | RegisterRef R = { PR, 0 }; | 
|  | 366 | if (!Defs.count(R)) | 
|  | 367 | Clobbers.insert(R); | 
|  | 368 | } | 
|  | 369 | } | 
| Krzysztof Parzyszek | e9be355 | 2017-02-27 18:03:35 +0000 | [diff] [blame] | 370 | // Defs and clobbers can overlap, e.g. | 
| Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 371 | // dead %d0 = COPY %5, implicit-def %r0, implicit-def %r1 | 
| Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 372 | for (RegisterRef R : Defs) | 
| Krzysztof Parzyszek | e9be355 | 2017-02-27 18:03:35 +0000 | [diff] [blame] | 373 | Clobbers.erase(R); | 
|  | 374 |  | 
| Krzysztof Parzyszek | 1aaf41a | 2017-02-17 22:14:51 +0000 | [diff] [blame] | 375 | // Update maps for defs. | 
|  | 376 | for (RegisterRef S : Defs) { | 
|  | 377 | // Defs should already be expanded into subregs. | 
|  | 378 | assert(!TargetRegisterInfo::isPhysicalRegister(S.Reg) || | 
|  | 379 | !MCSubRegIterator(S.Reg, &TRI, false).isValid()); | 
|  | 380 | if (LastDef[S] != IndexType::None || LastUse[S] != IndexType::None) | 
|  | 381 | closeRange(S); | 
|  | 382 | LastDef[S] = Index; | 
|  | 383 | } | 
|  | 384 | // Update maps for clobbers. | 
|  | 385 | for (RegisterRef S : Clobbers) { | 
|  | 386 | // Clobbers should already be expanded into subregs. | 
|  | 387 | assert(!TargetRegisterInfo::isPhysicalRegister(S.Reg) || | 
|  | 388 | !MCSubRegIterator(S.Reg, &TRI, false).isValid()); | 
|  | 389 | if (LastDef[S] != IndexType::None || LastUse[S] != IndexType::None) | 
|  | 390 | closeRange(S); | 
|  | 391 | // Create a single-instruction range. | 
|  | 392 | LastDef[S] = LastUse[S] = Index; | 
|  | 393 | closeRange(S); | 
|  | 394 | } | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 395 | } | 
|  | 396 |  | 
|  | 397 | // Collect live-on-exit. | 
|  | 398 | RegisterSet LiveOnExit; | 
|  | 399 | for (auto *SB : B.successors()) | 
| Krzysztof Parzyszek | 5bb417b | 2016-10-18 19:47:20 +0000 | [diff] [blame] | 400 | for (auto R : getLiveIns(*SB, MRI, TRI)) | 
|  | 401 | LiveOnExit.insert(R); | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 402 |  | 
|  | 403 | for (auto R : LiveOnExit) | 
|  | 404 | LastUse[R] = IndexType::Exit; | 
|  | 405 |  | 
|  | 406 | // Process remaining registers. | 
|  | 407 | RegisterSet Left; | 
|  | 408 | for (auto &I : LastUse) | 
|  | 409 | if (I.second != IndexType::None) | 
|  | 410 | Left.insert(I.first); | 
|  | 411 | for (auto &I : LastDef) | 
|  | 412 | if (I.second != IndexType::None) | 
|  | 413 | Left.insert(I.first); | 
|  | 414 | for (auto R : Left) | 
|  | 415 | closeRange(R); | 
|  | 416 |  | 
|  | 417 | // Finalize the live ranges. | 
|  | 418 | for (auto &P : LiveMap) | 
|  | 419 | P.second.unionize(); | 
|  | 420 | } | 
|  | 421 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 422 | HexagonBlockRanges::RegToRangeMap HexagonBlockRanges::computeLiveMap( | 
|  | 423 | InstrIndexMap &IndexMap) { | 
|  | 424 | RegToRangeMap LiveMap; | 
| Reid Kleckner | 40d7230 | 2016-10-20 00:22:23 +0000 | [diff] [blame] | 425 | DEBUG(dbgs() << __func__ << ": index map\n" << IndexMap << '\n'); | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 426 | computeInitialLiveRanges(IndexMap, LiveMap); | 
| Reid Kleckner | 40d7230 | 2016-10-20 00:22:23 +0000 | [diff] [blame] | 427 | DEBUG(dbgs() << __func__ << ": live map\n" | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 428 | << PrintRangeMap(LiveMap, TRI) << '\n'); | 
|  | 429 | return LiveMap; | 
|  | 430 | } | 
|  | 431 |  | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 432 | HexagonBlockRanges::RegToRangeMap HexagonBlockRanges::computeDeadMap( | 
|  | 433 | InstrIndexMap &IndexMap, RegToRangeMap &LiveMap) { | 
|  | 434 | RegToRangeMap DeadMap; | 
|  | 435 |  | 
|  | 436 | auto addDeadRanges = [&IndexMap,&LiveMap,&DeadMap] (RegisterRef R) -> void { | 
|  | 437 | auto F = LiveMap.find(R); | 
|  | 438 | if (F == LiveMap.end() || F->second.empty()) { | 
|  | 439 | DeadMap[R].add(IndexType::Entry, IndexType::Exit, false, false); | 
|  | 440 | return; | 
|  | 441 | } | 
|  | 442 |  | 
|  | 443 | RangeList &RL = F->second; | 
|  | 444 | RangeList::iterator A = RL.begin(), Z = RL.end()-1; | 
|  | 445 |  | 
|  | 446 | // Try to create the initial range. | 
|  | 447 | if (A->start() != IndexType::Entry) { | 
|  | 448 | IndexType DE = IndexMap.getPrevIndex(A->start()); | 
|  | 449 | if (DE != IndexType::Entry) | 
|  | 450 | DeadMap[R].add(IndexType::Entry, DE, false, false); | 
|  | 451 | } | 
|  | 452 |  | 
|  | 453 | while (A != Z) { | 
|  | 454 | // Creating a dead range that follows A.  Pay attention to empty | 
|  | 455 | // ranges (i.e. those ending with "None"). | 
|  | 456 | IndexType AE = (A->end() == IndexType::None) ? A->start() : A->end(); | 
|  | 457 | IndexType DS = IndexMap.getNextIndex(AE); | 
|  | 458 | ++A; | 
|  | 459 | IndexType DE = IndexMap.getPrevIndex(A->start()); | 
|  | 460 | if (DS < DE) | 
|  | 461 | DeadMap[R].add(DS, DE, false, false); | 
|  | 462 | } | 
|  | 463 |  | 
|  | 464 | // Try to create the final range. | 
|  | 465 | if (Z->end() != IndexType::Exit) { | 
|  | 466 | IndexType ZE = (Z->end() == IndexType::None) ? Z->start() : Z->end(); | 
|  | 467 | IndexType DS = IndexMap.getNextIndex(ZE); | 
|  | 468 | if (DS < IndexType::Exit) | 
|  | 469 | DeadMap[R].add(DS, IndexType::Exit, false, false); | 
|  | 470 | } | 
|  | 471 | }; | 
|  | 472 |  | 
|  | 473 | MachineFunction &MF = *IndexMap.getBlock().getParent(); | 
|  | 474 | auto &MRI = MF.getRegInfo(); | 
|  | 475 | unsigned NumRegs = TRI.getNumRegs(); | 
|  | 476 | BitVector Visited(NumRegs); | 
|  | 477 | for (unsigned R = 1; R < NumRegs; ++R) { | 
|  | 478 | for (auto S : expandToSubRegs({R,0}, MRI, TRI)) { | 
|  | 479 | if (Reserved[S.Reg] || Visited[S.Reg]) | 
|  | 480 | continue; | 
|  | 481 | addDeadRanges(S); | 
|  | 482 | Visited[S.Reg] = true; | 
|  | 483 | } | 
|  | 484 | } | 
|  | 485 | for (auto &P : LiveMap) | 
|  | 486 | if (TargetRegisterInfo::isVirtualRegister(P.first.Reg)) | 
|  | 487 | addDeadRanges(P.first); | 
|  | 488 |  | 
| Reid Kleckner | 40d7230 | 2016-10-20 00:22:23 +0000 | [diff] [blame] | 489 | DEBUG(dbgs() << __func__ << ": dead map\n" | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 490 | << PrintRangeMap(DeadMap, TRI) << '\n'); | 
|  | 491 | return DeadMap; | 
|  | 492 | } | 
|  | 493 |  | 
| Benjamin Kramer | 922efd7 | 2016-05-27 10:06:40 +0000 | [diff] [blame] | 494 | raw_ostream &llvm::operator<<(raw_ostream &OS, | 
|  | 495 | HexagonBlockRanges::IndexType Idx) { | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 496 | if (Idx == HexagonBlockRanges::IndexType::None) | 
|  | 497 | return OS << '-'; | 
|  | 498 | if (Idx == HexagonBlockRanges::IndexType::Entry) | 
|  | 499 | return OS << 'n'; | 
|  | 500 | if (Idx == HexagonBlockRanges::IndexType::Exit) | 
|  | 501 | return OS << 'x'; | 
|  | 502 | return OS << unsigned(Idx)-HexagonBlockRanges::IndexType::First+1; | 
|  | 503 | } | 
|  | 504 |  | 
|  | 505 | // A mapping to translate between instructions and their indices. | 
| Benjamin Kramer | 922efd7 | 2016-05-27 10:06:40 +0000 | [diff] [blame] | 506 | raw_ostream &llvm::operator<<(raw_ostream &OS, | 
|  | 507 | const HexagonBlockRanges::IndexRange &IR) { | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 508 | OS << '[' << IR.start() << ':' << IR.end() << (IR.TiedEnd ? '}' : ']'); | 
|  | 509 | if (IR.Fixed) | 
|  | 510 | OS << '!'; | 
|  | 511 | return OS; | 
|  | 512 | } | 
|  | 513 |  | 
| Benjamin Kramer | 922efd7 | 2016-05-27 10:06:40 +0000 | [diff] [blame] | 514 | raw_ostream &llvm::operator<<(raw_ostream &OS, | 
|  | 515 | const HexagonBlockRanges::RangeList &RL) { | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 516 | for (auto &R : RL) | 
|  | 517 | OS << R << " "; | 
|  | 518 | return OS; | 
|  | 519 | } | 
|  | 520 |  | 
| Benjamin Kramer | 922efd7 | 2016-05-27 10:06:40 +0000 | [diff] [blame] | 521 | raw_ostream &llvm::operator<<(raw_ostream &OS, | 
|  | 522 | const HexagonBlockRanges::InstrIndexMap &M) { | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 523 | for (auto &In : M.Block) { | 
|  | 524 | HexagonBlockRanges::IndexType Idx = M.getIndex(&In); | 
|  | 525 | OS << Idx << (Idx == M.Last ? ". " : "  ") << In; | 
|  | 526 | } | 
|  | 527 | return OS; | 
|  | 528 | } | 
|  | 529 |  | 
| Benjamin Kramer | 922efd7 | 2016-05-27 10:06:40 +0000 | [diff] [blame] | 530 | raw_ostream &llvm::operator<<(raw_ostream &OS, | 
|  | 531 | const HexagonBlockRanges::PrintRangeMap &P) { | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 532 | for (auto &I : P.Map) { | 
|  | 533 | const HexagonBlockRanges::RangeList &RL = I.second; | 
| Francis Visoiu Mistrih | 9d419d3 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 534 | OS << printReg(I.first.Reg, &P.TRI, I.first.Sub) << " -> " << RL << "\n"; | 
| Krzysztof Parzyszek | 7793ddb | 2016-02-12 22:53:35 +0000 | [diff] [blame] | 535 | } | 
|  | 536 | return OS; | 
|  | 537 | } |