blob: 48d1c082a666fdbd544a094ae865ea7d319bcae5 [file] [log] [blame]
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +00001//===--- 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"
15
16#include "llvm/ADT/BitVector.h"
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +000017#include "llvm/CodeGen/MachineBasicBlock.h"
18#include "llvm/CodeGen/MachineInstr.h"
19#include "llvm/CodeGen/MachineRegisterInfo.h"
Alexey Samsonov7217d272016-02-12 23:51:06 +000020#include "llvm/Support/Compiler.h"
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +000021#include "llvm/Support/Debug.h"
22#include "llvm/Support/raw_ostream.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000023#include "llvm/Target/TargetInstrInfo.h"
24#include "llvm/Target/TargetRegisterInfo.h"
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +000025
26#include <map>
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +000027
28using namespace llvm;
29
30bool HexagonBlockRanges::IndexRange::overlaps(const IndexRange &A) const {
31 // If A contains start(), or "this" contains A.start(), then overlap.
32 IndexType S = start(), E = end(), AS = A.start(), AE = A.end();
33 if (AS == S)
34 return true;
35 bool SbAE = (S < AE) || (S == AE && A.TiedEnd); // S-before-AE.
36 bool ASbE = (AS < E) || (AS == E && TiedEnd); // AS-before-E.
37 if ((AS < S && SbAE) || (S < AS && ASbE))
38 return true;
39 // Otherwise no overlap.
40 return false;
41}
42
43
44bool HexagonBlockRanges::IndexRange::contains(const IndexRange &A) const {
45 if (start() <= A.start()) {
46 // Treat "None" in the range end as equal to the range start.
47 IndexType E = (end() != IndexType::None) ? end() : start();
48 IndexType AE = (A.end() != IndexType::None) ? A.end() : A.start();
49 if (AE <= E)
50 return true;
51 }
52 return false;
53}
54
55
56void 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
73
74void HexagonBlockRanges::RangeList::include(const RangeList &RL) {
75 for (auto &R : RL)
David Majnemer42531262016-08-12 03:55:06 +000076 if (!is_contained(*this, R))
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +000077 push_back(R);
78}
79
80
81// Merge all overlapping ranges in the list, so that all that remains
82// is a list of disjoint ranges.
83void HexagonBlockRanges::RangeList::unionize(bool MergeAdjacent) {
84 if (empty())
85 return;
86
87 std::sort(begin(), end());
88 iterator Iter = begin();
89
90 while (Iter != end()-1) {
91 iterator Next = std::next(Iter);
92 // If MergeAdjacent is true, merge ranges A and B, where A.end == B.start.
93 // This allows merging dead ranges, but is not valid for live ranges.
94 bool Merge = MergeAdjacent && (Iter->end() == Next->start());
95 if (Merge || Iter->overlaps(*Next)) {
96 Iter->merge(*Next);
97 erase(Next);
98 continue;
99 }
100 ++Iter;
101 }
102}
103
104
105// Compute a range A-B and add it to the list.
106void 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
141
142// Subtract a given range from each element in the list.
143void HexagonBlockRanges::RangeList::subtract(const IndexRange &Range) {
144 // Cannot assume that the list is unionized (i.e. contains only non-
145 // overlapping ranges.
146 RangeList T;
147 for (iterator Next, I = begin(); I != end(); I = Next) {
148 IndexRange &Rg = *I;
149 if (Rg.overlaps(Range)) {
150 T.addsub(Rg, Range);
151 Next = this->erase(I);
152 } else {
153 Next = std::next(I);
154 }
155 }
156 include(T);
157}
158
159
160HexagonBlockRanges::InstrIndexMap::InstrIndexMap(MachineBasicBlock &B)
161 : Block(B) {
162 IndexType Idx = IndexType::First;
163 First = Idx;
164 for (auto &In : B) {
165 if (In.isDebugValue())
166 continue;
167 assert(getIndex(&In) == IndexType::None && "Instruction already in map");
168 Map.insert(std::make_pair(Idx, &In));
169 ++Idx;
170 }
171 Last = B.empty() ? IndexType::None : unsigned(Idx)-1;
172}
173
174
175MachineInstr *HexagonBlockRanges::InstrIndexMap::getInstr(IndexType Idx) const {
176 auto F = Map.find(Idx);
177 return (F != Map.end()) ? F->second : 0;
178}
179
180
181HexagonBlockRanges::IndexType HexagonBlockRanges::InstrIndexMap::getIndex(
182 MachineInstr *MI) const {
183 for (auto &I : Map)
184 if (I.second == MI)
185 return I.first;
186 return IndexType::None;
187}
188
189
190HexagonBlockRanges::IndexType HexagonBlockRanges::InstrIndexMap::getPrevIndex(
191 IndexType Idx) const {
192 assert (Idx != IndexType::None);
193 if (Idx == IndexType::Entry)
194 return IndexType::None;
195 if (Idx == IndexType::Exit)
196 return Last;
197 if (Idx == First)
198 return IndexType::Entry;
199 return unsigned(Idx)-1;
200}
201
202
203HexagonBlockRanges::IndexType HexagonBlockRanges::InstrIndexMap::getNextIndex(
204 IndexType Idx) const {
205 assert (Idx != IndexType::None);
206 if (Idx == IndexType::Entry)
207 return IndexType::First;
208 if (Idx == IndexType::Exit || Idx == Last)
209 return IndexType::None;
210 return unsigned(Idx)+1;
211}
212
213
214void HexagonBlockRanges::InstrIndexMap::replaceInstr(MachineInstr *OldMI,
215 MachineInstr *NewMI) {
216 for (auto &I : Map) {
217 if (I.second != OldMI)
218 continue;
219 if (NewMI != nullptr)
220 I.second = NewMI;
221 else
222 Map.erase(I.first);
223 break;
224 }
225}
226
227
228HexagonBlockRanges::HexagonBlockRanges(MachineFunction &mf)
229 : MF(mf), HST(mf.getSubtarget<HexagonSubtarget>()),
230 TII(*HST.getInstrInfo()), TRI(*HST.getRegisterInfo()),
231 Reserved(TRI.getReservedRegs(mf)) {
232 // Consider all non-allocatable registers as reserved.
233 for (auto I = TRI.regclass_begin(), E = TRI.regclass_end(); I != E; ++I) {
234 auto *RC = *I;
235 if (RC->isAllocatable())
236 continue;
237 for (unsigned R : *RC)
238 Reserved[R] = true;
239 }
240}
241
242
243HexagonBlockRanges::RegisterSet HexagonBlockRanges::getLiveIns(
Krzysztof Parzyszek5bb417b2016-10-18 19:47:20 +0000244 const MachineBasicBlock &B, const MachineRegisterInfo &MRI,
245 const TargetRegisterInfo &TRI) {
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +0000246 RegisterSet LiveIns;
Krzysztof Parzyszek5bb417b2016-10-18 19:47:20 +0000247 RegisterSet Tmp;
248 for (auto I : B.liveins()) {
249 if (I.LaneMask == ~LaneBitmask(0)) {
250 Tmp.insert({I.PhysReg,0});
251 continue;
252 }
253 for (MCSubRegIndexIterator S(I.PhysReg, &TRI); S.isValid(); ++S) {
254 LaneBitmask M = TRI.getSubRegIndexLaneMask(S.getSubRegIndex());
255 if (M & I.LaneMask)
256 Tmp.insert({S.getSubReg(), 0});
257 }
258 }
259
260 for (auto R : Tmp) {
261 if (!Reserved[R.Reg])
262 LiveIns.insert(R);
263 for (auto S : expandToSubRegs(R, MRI, TRI))
264 if (!Reserved[S.Reg])
265 LiveIns.insert(S);
266 }
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +0000267 return LiveIns;
268}
269
270
271HexagonBlockRanges::RegisterSet HexagonBlockRanges::expandToSubRegs(
272 RegisterRef R, const MachineRegisterInfo &MRI,
273 const TargetRegisterInfo &TRI) {
274 RegisterSet SRs;
275
276 if (R.Sub != 0) {
277 SRs.insert(R);
278 return SRs;
279 }
280
281 if (TargetRegisterInfo::isPhysicalRegister(R.Reg)) {
282 MCSubRegIterator I(R.Reg, &TRI);
283 if (!I.isValid())
284 SRs.insert({R.Reg, 0});
285 for (; I.isValid(); ++I)
286 SRs.insert({*I, 0});
287 } else {
288 assert(TargetRegisterInfo::isVirtualRegister(R.Reg));
289 auto &RC = *MRI.getRegClass(R.Reg);
290 unsigned PReg = *RC.begin();
291 MCSubRegIndexIterator I(PReg, &TRI);
292 if (!I.isValid())
293 SRs.insert({R.Reg, 0});
294 for (; I.isValid(); ++I)
295 SRs.insert({R.Reg, I.getSubRegIndex()});
296 }
297 return SRs;
298}
299
300
301void HexagonBlockRanges::computeInitialLiveRanges(InstrIndexMap &IndexMap,
302 RegToRangeMap &LiveMap) {
303 std::map<RegisterRef,IndexType> LastDef, LastUse;
304 RegisterSet LiveOnEntry;
305 MachineBasicBlock &B = IndexMap.getBlock();
306 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
307
Krzysztof Parzyszek5bb417b2016-10-18 19:47:20 +0000308 for (auto R : getLiveIns(B, MRI, TRI))
309 LiveOnEntry.insert(R);
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +0000310
311 for (auto R : LiveOnEntry)
312 LastDef[R] = IndexType::Entry;
313
314 auto closeRange = [&LastUse,&LastDef,&LiveMap] (RegisterRef R) -> void {
315 auto LD = LastDef[R], LU = LastUse[R];
316 if (LD == IndexType::None)
317 LD = IndexType::Entry;
318 if (LU == IndexType::None)
319 LU = IndexType::Exit;
320 LiveMap[R].add(LD, LU, false, false);
321 LastUse[R] = LastDef[R] = IndexType::None;
322 };
323
324 for (auto &In : B) {
325 if (In.isDebugValue())
326 continue;
327 IndexType Index = IndexMap.getIndex(&In);
328 // Process uses first.
329 for (auto &Op : In.operands()) {
330 if (!Op.isReg() || !Op.isUse() || Op.isUndef())
331 continue;
332 RegisterRef R = { Op.getReg(), Op.getSubReg() };
333 if (TargetRegisterInfo::isPhysicalRegister(R.Reg) && Reserved[R.Reg])
334 continue;
335 bool IsKill = Op.isKill();
336 for (auto S : expandToSubRegs(R, MRI, TRI)) {
337 LastUse[S] = Index;
338 if (IsKill)
339 closeRange(S);
340 }
341 }
342 // Process defs.
343 for (auto &Op : In.operands()) {
344 if (!Op.isReg() || !Op.isDef() || Op.isUndef())
345 continue;
346 RegisterRef R = { Op.getReg(), Op.getSubReg() };
347 if (TargetRegisterInfo::isPhysicalRegister(R.Reg) && Reserved[R.Reg])
348 continue;
349 for (auto S : expandToSubRegs(R, MRI, TRI)) {
Krzysztof Parzyszekc06e7932016-04-22 17:27:22 +0000350 if (LastDef[S] != IndexType::None || LastUse[S] != IndexType::None)
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +0000351 closeRange(S);
352 LastDef[S] = Index;
353 }
354 }
355 }
356
357 // Collect live-on-exit.
358 RegisterSet LiveOnExit;
359 for (auto *SB : B.successors())
Krzysztof Parzyszek5bb417b2016-10-18 19:47:20 +0000360 for (auto R : getLiveIns(*SB, MRI, TRI))
361 LiveOnExit.insert(R);
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +0000362
363 for (auto R : LiveOnExit)
364 LastUse[R] = IndexType::Exit;
365
366 // Process remaining registers.
367 RegisterSet Left;
368 for (auto &I : LastUse)
369 if (I.second != IndexType::None)
370 Left.insert(I.first);
371 for (auto &I : LastDef)
372 if (I.second != IndexType::None)
373 Left.insert(I.first);
374 for (auto R : Left)
375 closeRange(R);
376
377 // Finalize the live ranges.
378 for (auto &P : LiveMap)
379 P.second.unionize();
380}
381
382
383HexagonBlockRanges::RegToRangeMap HexagonBlockRanges::computeLiveMap(
384 InstrIndexMap &IndexMap) {
385 RegToRangeMap LiveMap;
Alexey Samsonov7217d272016-02-12 23:51:06 +0000386 DEBUG(dbgs() << LLVM_FUNCTION_NAME << ": index map\n" << IndexMap << '\n');
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +0000387 computeInitialLiveRanges(IndexMap, LiveMap);
Alexey Samsonov7217d272016-02-12 23:51:06 +0000388 DEBUG(dbgs() << LLVM_FUNCTION_NAME << ": live map\n"
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +0000389 << PrintRangeMap(LiveMap, TRI) << '\n');
390 return LiveMap;
391}
392
393
394HexagonBlockRanges::RegToRangeMap HexagonBlockRanges::computeDeadMap(
395 InstrIndexMap &IndexMap, RegToRangeMap &LiveMap) {
396 RegToRangeMap DeadMap;
397
398 auto addDeadRanges = [&IndexMap,&LiveMap,&DeadMap] (RegisterRef R) -> void {
399 auto F = LiveMap.find(R);
400 if (F == LiveMap.end() || F->second.empty()) {
401 DeadMap[R].add(IndexType::Entry, IndexType::Exit, false, false);
402 return;
403 }
404
405 RangeList &RL = F->second;
406 RangeList::iterator A = RL.begin(), Z = RL.end()-1;
407
408 // Try to create the initial range.
409 if (A->start() != IndexType::Entry) {
410 IndexType DE = IndexMap.getPrevIndex(A->start());
411 if (DE != IndexType::Entry)
412 DeadMap[R].add(IndexType::Entry, DE, false, false);
413 }
414
415 while (A != Z) {
416 // Creating a dead range that follows A. Pay attention to empty
417 // ranges (i.e. those ending with "None").
418 IndexType AE = (A->end() == IndexType::None) ? A->start() : A->end();
419 IndexType DS = IndexMap.getNextIndex(AE);
420 ++A;
421 IndexType DE = IndexMap.getPrevIndex(A->start());
422 if (DS < DE)
423 DeadMap[R].add(DS, DE, false, false);
424 }
425
426 // Try to create the final range.
427 if (Z->end() != IndexType::Exit) {
428 IndexType ZE = (Z->end() == IndexType::None) ? Z->start() : Z->end();
429 IndexType DS = IndexMap.getNextIndex(ZE);
430 if (DS < IndexType::Exit)
431 DeadMap[R].add(DS, IndexType::Exit, false, false);
432 }
433 };
434
435 MachineFunction &MF = *IndexMap.getBlock().getParent();
436 auto &MRI = MF.getRegInfo();
437 unsigned NumRegs = TRI.getNumRegs();
438 BitVector Visited(NumRegs);
439 for (unsigned R = 1; R < NumRegs; ++R) {
440 for (auto S : expandToSubRegs({R,0}, MRI, TRI)) {
441 if (Reserved[S.Reg] || Visited[S.Reg])
442 continue;
443 addDeadRanges(S);
444 Visited[S.Reg] = true;
445 }
446 }
447 for (auto &P : LiveMap)
448 if (TargetRegisterInfo::isVirtualRegister(P.first.Reg))
449 addDeadRanges(P.first);
450
Alexey Samsonov7217d272016-02-12 23:51:06 +0000451 DEBUG(dbgs() << LLVM_FUNCTION_NAME << ": dead map\n"
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +0000452 << PrintRangeMap(DeadMap, TRI) << '\n');
453 return DeadMap;
454}
455
Benjamin Kramer922efd72016-05-27 10:06:40 +0000456raw_ostream &llvm::operator<<(raw_ostream &OS,
457 HexagonBlockRanges::IndexType Idx) {
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +0000458 if (Idx == HexagonBlockRanges::IndexType::None)
459 return OS << '-';
460 if (Idx == HexagonBlockRanges::IndexType::Entry)
461 return OS << 'n';
462 if (Idx == HexagonBlockRanges::IndexType::Exit)
463 return OS << 'x';
464 return OS << unsigned(Idx)-HexagonBlockRanges::IndexType::First+1;
465}
466
467// A mapping to translate between instructions and their indices.
Benjamin Kramer922efd72016-05-27 10:06:40 +0000468raw_ostream &llvm::operator<<(raw_ostream &OS,
469 const HexagonBlockRanges::IndexRange &IR) {
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +0000470 OS << '[' << IR.start() << ':' << IR.end() << (IR.TiedEnd ? '}' : ']');
471 if (IR.Fixed)
472 OS << '!';
473 return OS;
474}
475
Benjamin Kramer922efd72016-05-27 10:06:40 +0000476raw_ostream &llvm::operator<<(raw_ostream &OS,
477 const HexagonBlockRanges::RangeList &RL) {
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +0000478 for (auto &R : RL)
479 OS << R << " ";
480 return OS;
481}
482
Benjamin Kramer922efd72016-05-27 10:06:40 +0000483raw_ostream &llvm::operator<<(raw_ostream &OS,
484 const HexagonBlockRanges::InstrIndexMap &M) {
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +0000485 for (auto &In : M.Block) {
486 HexagonBlockRanges::IndexType Idx = M.getIndex(&In);
487 OS << Idx << (Idx == M.Last ? ". " : " ") << In;
488 }
489 return OS;
490}
491
Benjamin Kramer922efd72016-05-27 10:06:40 +0000492raw_ostream &llvm::operator<<(raw_ostream &OS,
493 const HexagonBlockRanges::PrintRangeMap &P) {
Krzysztof Parzyszek7793ddb2016-02-12 22:53:35 +0000494 for (auto &I : P.Map) {
495 const HexagonBlockRanges::RangeList &RL = I.second;
496 OS << PrintReg(I.first.Reg, &P.TRI, I.first.Sub) << " -> " << RL << "\n";
497 }
498 return OS;
499}