1 //===- HexagonBlockRanges.cpp ---------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "HexagonBlockRanges.h"
10 #include "HexagonInstrInfo.h"
11 #include "HexagonSubtarget.h"
12 #include "llvm/ADT/BitVector.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/CodeGen/MachineBasicBlock.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/CodeGen/MachineInstr.h"
17 #include "llvm/CodeGen/MachineOperand.h"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
19 #include "llvm/CodeGen/TargetRegisterInfo.h"
20 #include "llvm/MC/MCRegisterInfo.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
31 #define DEBUG_TYPE "hbr"
33 bool HexagonBlockRanges::IndexRange::overlaps(const IndexRange
&A
) const {
34 // If A contains start(), or "this" contains A.start(), then overlap.
35 IndexType S
= start(), E
= end(), AS
= A
.start(), AE
= A
.end();
38 bool SbAE
= (S
< AE
) || (S
== AE
&& A
.TiedEnd
); // S-before-AE.
39 bool ASbE
= (AS
< E
) || (AS
== E
&& TiedEnd
); // AS-before-E.
40 if ((AS
< S
&& SbAE
) || (S
< AS
&& ASbE
))
42 // Otherwise no overlap.
46 bool HexagonBlockRanges::IndexRange::contains(const IndexRange
&A
) const {
47 if (start() <= A
.start()) {
48 // Treat "None" in the range end as equal to the range start.
49 IndexType E
= (end() != IndexType::None
) ? end() : start();
50 IndexType AE
= (A
.end() != IndexType::None
) ? A
.end() : A
.start();
57 void HexagonBlockRanges::IndexRange::merge(const IndexRange
&A
) {
58 // Allow merging adjacent ranges.
59 assert(end() == A
.start() || overlaps(A
));
60 IndexType AS
= A
.start(), AE
= A
.end();
61 if (AS
< start() || start() == IndexType::None
)
63 if (end() < AE
|| end() == IndexType::None
) {
74 void HexagonBlockRanges::RangeList::include(const RangeList
&RL
) {
75 for (const auto &R
: RL
)
76 if (!is_contained(*this, R
))
80 // Merge all overlapping ranges in the list, so that all that remains
81 // is a list of disjoint ranges.
82 void HexagonBlockRanges::RangeList::unionize(bool MergeAdjacent
) {
87 iterator Iter
= begin();
89 while (Iter
!= end()-1) {
90 iterator Next
= std::next(Iter
);
91 // If MergeAdjacent is true, merge ranges A and B, where A.end == B.start.
92 // This allows merging dead ranges, but is not valid for live ranges.
93 bool Merge
= MergeAdjacent
&& (Iter
->end() == Next
->start());
94 if (Merge
|| Iter
->overlaps(*Next
)) {
103 // Compute a range A-B and add it to the list.
104 void HexagonBlockRanges::RangeList::addsub(const IndexRange
&A
,
105 const IndexRange
&B
) {
106 // Exclusion of non-overlapping ranges makes some checks simpler
107 // later in this function.
108 if (!A
.overlaps(B
)) {
114 IndexType AS
= A
.start(), AE
= A
.end();
115 IndexType BS
= B
.start(), BE
= B
.end();
117 // If AE is None, then A is included in B, since A and B overlap.
118 // The result of subtraction if empty, so just return.
119 if (AE
== IndexType::None
)
123 // A starts before B.
124 // AE cannot be None since A and B overlap.
125 assert(AE
!= IndexType::None
);
126 // Add the part of A that extends on the "less" side of B.
127 add(AS
, BS
, A
.Fixed
, false);
131 // BE cannot be Exit here.
132 if (BE
== IndexType::None
)
133 add(BS
, AE
, A
.Fixed
, false);
135 add(BE
, AE
, A
.Fixed
, false);
139 // Subtract a given range from each element in the list.
140 void HexagonBlockRanges::RangeList::subtract(const IndexRange
&Range
) {
141 // Cannot assume that the list is unionized (i.e. contains only non-
142 // overlapping ranges.
144 for (iterator Next
, I
= begin(); I
!= end(); I
= Next
) {
146 if (Rg
.overlaps(Range
)) {
148 Next
= this->erase(I
);
156 HexagonBlockRanges::InstrIndexMap::InstrIndexMap(MachineBasicBlock
&B
)
158 IndexType Idx
= IndexType::First
;
161 if (In
.isDebugInstr())
163 assert(getIndex(&In
) == IndexType::None
&& "Instruction already in map");
164 Map
.insert(std::make_pair(Idx
, &In
));
167 Last
= B
.empty() ? IndexType::None
: unsigned(Idx
)-1;
170 MachineInstr
*HexagonBlockRanges::InstrIndexMap::getInstr(IndexType Idx
) const {
171 auto F
= Map
.find(Idx
);
172 return (F
!= Map
.end()) ? F
->second
: nullptr;
175 HexagonBlockRanges::IndexType
HexagonBlockRanges::InstrIndexMap::getIndex(
176 MachineInstr
*MI
) const {
177 for (const auto &I
: Map
)
180 return IndexType::None
;
183 HexagonBlockRanges::IndexType
HexagonBlockRanges::InstrIndexMap::getPrevIndex(
184 IndexType Idx
) const {
185 assert (Idx
!= IndexType::None
);
186 if (Idx
== IndexType::Entry
)
187 return IndexType::None
;
188 if (Idx
== IndexType::Exit
)
191 return IndexType::Entry
;
192 return unsigned(Idx
)-1;
195 HexagonBlockRanges::IndexType
HexagonBlockRanges::InstrIndexMap::getNextIndex(
196 IndexType Idx
) const {
197 assert (Idx
!= IndexType::None
);
198 if (Idx
== IndexType::Entry
)
199 return IndexType::First
;
200 if (Idx
== IndexType::Exit
|| Idx
== Last
)
201 return IndexType::None
;
202 return unsigned(Idx
)+1;
205 void HexagonBlockRanges::InstrIndexMap::replaceInstr(MachineInstr
*OldMI
,
206 MachineInstr
*NewMI
) {
207 for (auto &I
: Map
) {
208 if (I
.second
!= OldMI
)
210 if (NewMI
!= nullptr)
218 HexagonBlockRanges::HexagonBlockRanges(MachineFunction
&mf
)
219 : MF(mf
), HST(mf
.getSubtarget
<HexagonSubtarget
>()),
220 TII(*HST
.getInstrInfo()), TRI(*HST
.getRegisterInfo()),
221 Reserved(TRI
.getReservedRegs(mf
)) {
222 // Consider all non-allocatable registers as reserved.
223 for (const TargetRegisterClass
*RC
: TRI
.regclasses()) {
224 if (RC
->isAllocatable())
226 for (unsigned R
: *RC
)
231 HexagonBlockRanges::RegisterSet
HexagonBlockRanges::getLiveIns(
232 const MachineBasicBlock
&B
, const MachineRegisterInfo
&MRI
,
233 const TargetRegisterInfo
&TRI
) {
237 for (auto I
: B
.liveins()) {
238 MCSubRegIndexIterator
S(I
.PhysReg
, &TRI
);
239 if (I
.LaneMask
.all() || (I
.LaneMask
.any() && !S
.isValid())) {
240 Tmp
.insert({I
.PhysReg
, 0});
243 for (; S
.isValid(); ++S
) {
244 unsigned SI
= S
.getSubRegIndex();
245 if ((I
.LaneMask
& TRI
.getSubRegIndexLaneMask(SI
)).any())
246 Tmp
.insert({S
.getSubReg(), 0});
251 if (!Reserved
[R
.Reg
])
253 for (auto S
: expandToSubRegs(R
, MRI
, TRI
))
254 if (!Reserved
[S
.Reg
])
260 HexagonBlockRanges::RegisterSet
HexagonBlockRanges::expandToSubRegs(
261 RegisterRef R
, const MachineRegisterInfo
&MRI
,
262 const TargetRegisterInfo
&TRI
) {
270 if (R
.Reg
.isPhysical()) {
271 if (TRI
.subregs(R
.Reg
).empty())
272 SRs
.insert({R
.Reg
, 0});
273 for (MCPhysReg I
: TRI
.subregs(R
.Reg
))
276 assert(R
.Reg
.isVirtual());
277 auto &RC
= *MRI
.getRegClass(R
.Reg
);
278 unsigned PReg
= *RC
.begin();
279 MCSubRegIndexIterator
I(PReg
, &TRI
);
281 SRs
.insert({R
.Reg
, 0});
282 for (; I
.isValid(); ++I
)
283 SRs
.insert({R
.Reg
, I
.getSubRegIndex()});
288 void HexagonBlockRanges::computeInitialLiveRanges(InstrIndexMap
&IndexMap
,
289 RegToRangeMap
&LiveMap
) {
290 std::map
<RegisterRef
,IndexType
> LastDef
, LastUse
;
291 RegisterSet LiveOnEntry
;
292 MachineBasicBlock
&B
= IndexMap
.getBlock();
293 MachineRegisterInfo
&MRI
= B
.getParent()->getRegInfo();
295 for (auto R
: getLiveIns(B
, MRI
, TRI
))
296 LiveOnEntry
.insert(R
);
298 for (auto R
: LiveOnEntry
)
299 LastDef
[R
] = IndexType::Entry
;
301 auto closeRange
= [&LastUse
,&LastDef
,&LiveMap
] (RegisterRef R
) -> void {
302 auto LD
= LastDef
[R
], LU
= LastUse
[R
];
303 if (LD
== IndexType::None
)
304 LD
= IndexType::Entry
;
305 if (LU
== IndexType::None
)
306 LU
= IndexType::Exit
;
307 LiveMap
[R
].add(LD
, LU
, false, false);
308 LastUse
[R
] = LastDef
[R
] = IndexType::None
;
311 RegisterSet Defs
, Clobbers
;
314 if (In
.isDebugInstr())
316 IndexType Index
= IndexMap
.getIndex(&In
);
317 // Process uses first.
318 for (auto &Op
: In
.operands()) {
319 if (!Op
.isReg() || !Op
.isUse() || Op
.isUndef())
321 RegisterRef R
= { Op
.getReg(), Op
.getSubReg() };
322 if (R
.Reg
.isPhysical() && Reserved
[R
.Reg
])
324 bool IsKill
= Op
.isKill();
325 for (auto S
: expandToSubRegs(R
, MRI
, TRI
)) {
331 // Process defs and clobbers.
334 for (auto &Op
: In
.operands()) {
335 if (!Op
.isReg() || !Op
.isDef() || Op
.isUndef())
337 RegisterRef R
= { Op
.getReg(), Op
.getSubReg() };
338 for (auto S
: expandToSubRegs(R
, MRI
, TRI
)) {
339 if (S
.Reg
.isPhysical() && Reserved
[S
.Reg
])
348 for (auto &Op
: In
.operands()) {
351 const uint32_t *BM
= Op
.getRegMask();
352 for (unsigned PR
= 1, N
= TRI
.getNumRegs(); PR
!= N
; ++PR
) {
353 // Skip registers that have subregisters. A register is preserved
354 // iff its bit is set in the regmask, so if R1:0 was preserved, both
355 // R1 and R0 would also be present.
356 if (!TRI
.subregs(PR
).empty())
360 if (BM
[PR
/32] & (1u << (PR
%32)))
362 RegisterRef R
= { PR
, 0 };
367 // Defs and clobbers can overlap, e.g.
368 // dead %d0 = COPY %5, implicit-def %r0, implicit-def %r1
369 for (RegisterRef R
: Defs
)
372 // Update maps for defs.
373 for (RegisterRef S
: Defs
) {
374 // Defs should already be expanded into subregs.
375 assert(!S
.Reg
.isPhysical() || TRI
.subregs(S
.Reg
).empty());
376 if (LastDef
[S
] != IndexType::None
|| LastUse
[S
] != IndexType::None
)
380 // Update maps for clobbers.
381 for (RegisterRef S
: Clobbers
) {
382 // Clobbers should already be expanded into subregs.
383 assert(!S
.Reg
.isPhysical() || TRI
.subregs(S
.Reg
).empty());
384 if (LastDef
[S
] != IndexType::None
|| LastUse
[S
] != IndexType::None
)
386 // Create a single-instruction range.
387 LastDef
[S
] = LastUse
[S
] = Index
;
392 // Collect live-on-exit.
393 RegisterSet LiveOnExit
;
394 for (auto *SB
: B
.successors())
395 for (auto R
: getLiveIns(*SB
, MRI
, TRI
))
396 LiveOnExit
.insert(R
);
398 for (auto R
: LiveOnExit
)
399 LastUse
[R
] = IndexType::Exit
;
401 // Process remaining registers.
403 for (auto &I
: LastUse
)
404 if (I
.second
!= IndexType::None
)
405 Left
.insert(I
.first
);
406 for (auto &I
: LastDef
)
407 if (I
.second
!= IndexType::None
)
408 Left
.insert(I
.first
);
412 // Finalize the live ranges.
413 for (auto &P
: LiveMap
)
417 HexagonBlockRanges::RegToRangeMap
HexagonBlockRanges::computeLiveMap(
418 InstrIndexMap
&IndexMap
) {
419 RegToRangeMap LiveMap
;
420 LLVM_DEBUG(dbgs() << __func__
<< ": index map\n" << IndexMap
<< '\n');
421 computeInitialLiveRanges(IndexMap
, LiveMap
);
422 LLVM_DEBUG(dbgs() << __func__
<< ": live map\n"
423 << PrintRangeMap(LiveMap
, TRI
) << '\n');
427 HexagonBlockRanges::RegToRangeMap
HexagonBlockRanges::computeDeadMap(
428 InstrIndexMap
&IndexMap
, RegToRangeMap
&LiveMap
) {
429 RegToRangeMap DeadMap
;
431 auto addDeadRanges
= [&IndexMap
,&LiveMap
,&DeadMap
] (RegisterRef R
) -> void {
432 auto F
= LiveMap
.find(R
);
433 if (F
== LiveMap
.end() || F
->second
.empty()) {
434 DeadMap
[R
].add(IndexType::Entry
, IndexType::Exit
, false, false);
438 RangeList
&RL
= F
->second
;
439 RangeList::iterator A
= RL
.begin(), Z
= RL
.end()-1;
441 // Try to create the initial range.
442 if (A
->start() != IndexType::Entry
) {
443 IndexType DE
= IndexMap
.getPrevIndex(A
->start());
444 if (DE
!= IndexType::Entry
)
445 DeadMap
[R
].add(IndexType::Entry
, DE
, false, false);
449 // Creating a dead range that follows A. Pay attention to empty
450 // ranges (i.e. those ending with "None").
451 IndexType AE
= (A
->end() == IndexType::None
) ? A
->start() : A
->end();
452 IndexType DS
= IndexMap
.getNextIndex(AE
);
454 IndexType DE
= IndexMap
.getPrevIndex(A
->start());
456 DeadMap
[R
].add(DS
, DE
, false, false);
459 // Try to create the final range.
460 if (Z
->end() != IndexType::Exit
) {
461 IndexType ZE
= (Z
->end() == IndexType::None
) ? Z
->start() : Z
->end();
462 IndexType DS
= IndexMap
.getNextIndex(ZE
);
463 if (DS
< IndexType::Exit
)
464 DeadMap
[R
].add(DS
, IndexType::Exit
, false, false);
468 MachineFunction
&MF
= *IndexMap
.getBlock().getParent();
469 auto &MRI
= MF
.getRegInfo();
470 unsigned NumRegs
= TRI
.getNumRegs();
471 BitVector
Visited(NumRegs
);
472 for (unsigned R
= 1; R
< NumRegs
; ++R
) {
473 for (auto S
: expandToSubRegs({R
,0}, MRI
, TRI
)) {
474 if (Reserved
[S
.Reg
] || Visited
[S
.Reg
])
477 Visited
[S
.Reg
] = true;
480 for (auto &P
: LiveMap
)
481 if (P
.first
.Reg
.isVirtual())
482 addDeadRanges(P
.first
);
484 LLVM_DEBUG(dbgs() << __func__
<< ": dead map\n"
485 << PrintRangeMap(DeadMap
, TRI
) << '\n');
489 raw_ostream
&llvm::operator<<(raw_ostream
&OS
,
490 HexagonBlockRanges::IndexType Idx
) {
491 if (Idx
== HexagonBlockRanges::IndexType::None
)
493 if (Idx
== HexagonBlockRanges::IndexType::Entry
)
495 if (Idx
== HexagonBlockRanges::IndexType::Exit
)
497 return OS
<< unsigned(Idx
)-HexagonBlockRanges::IndexType::First
+1;
500 // A mapping to translate between instructions and their indices.
501 raw_ostream
&llvm::operator<<(raw_ostream
&OS
,
502 const HexagonBlockRanges::IndexRange
&IR
) {
503 OS
<< '[' << IR
.start() << ':' << IR
.end() << (IR
.TiedEnd
? '}' : ']');
509 raw_ostream
&llvm::operator<<(raw_ostream
&OS
,
510 const HexagonBlockRanges::RangeList
&RL
) {
511 for (const auto &R
: RL
)
516 raw_ostream
&llvm::operator<<(raw_ostream
&OS
,
517 const HexagonBlockRanges::InstrIndexMap
&M
) {
518 for (auto &In
: M
.Block
) {
519 HexagonBlockRanges::IndexType Idx
= M
.getIndex(&In
);
520 OS
<< Idx
<< (Idx
== M
.Last
? ". " : " ") << In
;
525 raw_ostream
&llvm::operator<<(raw_ostream
&OS
,
526 const HexagonBlockRanges::PrintRangeMap
&P
) {
527 for (const auto &I
: P
.Map
) {
528 const HexagonBlockRanges::RangeList
&RL
= I
.second
;
529 OS
<< printReg(I
.first
.Reg
, &P
.TRI
, I
.first
.Sub
) << " -> " << RL
<< "\n";