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