10b57cec5SDimitry Andric //===- HexagonBlockRanges.cpp ---------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "HexagonBlockRanges.h"
100b57cec5SDimitry Andric #include "HexagonInstrInfo.h"
110b57cec5SDimitry Andric #include "HexagonSubtarget.h"
120b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
130b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
140b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
150b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
200b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
210b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
220b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
230b57cec5SDimitry Andric #include <algorithm>
240b57cec5SDimitry Andric #include <cassert>
250b57cec5SDimitry Andric #include <cstdint>
260b57cec5SDimitry Andric #include <iterator>
270b57cec5SDimitry Andric #include <map>
280b57cec5SDimitry Andric #include <utility>
290b57cec5SDimitry Andric
300b57cec5SDimitry Andric using namespace llvm;
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric #define DEBUG_TYPE "hbr"
330b57cec5SDimitry Andric
overlaps(const IndexRange & A) const340b57cec5SDimitry Andric bool HexagonBlockRanges::IndexRange::overlaps(const IndexRange &A) const {
350b57cec5SDimitry Andric // If A contains start(), or "this" contains A.start(), then overlap.
360b57cec5SDimitry Andric IndexType S = start(), E = end(), AS = A.start(), AE = A.end();
370b57cec5SDimitry Andric if (AS == S)
380b57cec5SDimitry Andric return true;
390b57cec5SDimitry Andric bool SbAE = (S < AE) || (S == AE && A.TiedEnd); // S-before-AE.
400b57cec5SDimitry Andric bool ASbE = (AS < E) || (AS == E && TiedEnd); // AS-before-E.
410b57cec5SDimitry Andric if ((AS < S && SbAE) || (S < AS && ASbE))
420b57cec5SDimitry Andric return true;
430b57cec5SDimitry Andric // Otherwise no overlap.
440b57cec5SDimitry Andric return false;
450b57cec5SDimitry Andric }
460b57cec5SDimitry Andric
contains(const IndexRange & A) const470b57cec5SDimitry Andric bool HexagonBlockRanges::IndexRange::contains(const IndexRange &A) const {
480b57cec5SDimitry Andric if (start() <= A.start()) {
490b57cec5SDimitry Andric // Treat "None" in the range end as equal to the range start.
500b57cec5SDimitry Andric IndexType E = (end() != IndexType::None) ? end() : start();
510b57cec5SDimitry Andric IndexType AE = (A.end() != IndexType::None) ? A.end() : A.start();
520b57cec5SDimitry Andric if (AE <= E)
530b57cec5SDimitry Andric return true;
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric return false;
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric
merge(const IndexRange & A)580b57cec5SDimitry Andric void HexagonBlockRanges::IndexRange::merge(const IndexRange &A) {
590b57cec5SDimitry Andric // Allow merging adjacent ranges.
600b57cec5SDimitry Andric assert(end() == A.start() || overlaps(A));
610b57cec5SDimitry Andric IndexType AS = A.start(), AE = A.end();
620b57cec5SDimitry Andric if (AS < start() || start() == IndexType::None)
630b57cec5SDimitry Andric setStart(AS);
640b57cec5SDimitry Andric if (end() < AE || end() == IndexType::None) {
650b57cec5SDimitry Andric setEnd(AE);
660b57cec5SDimitry Andric TiedEnd = A.TiedEnd;
670b57cec5SDimitry Andric } else {
680b57cec5SDimitry Andric if (end() == AE)
690b57cec5SDimitry Andric TiedEnd |= A.TiedEnd;
700b57cec5SDimitry Andric }
710b57cec5SDimitry Andric if (A.Fixed)
720b57cec5SDimitry Andric Fixed = true;
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric
include(const RangeList & RL)750b57cec5SDimitry Andric void HexagonBlockRanges::RangeList::include(const RangeList &RL) {
76bdd1243dSDimitry Andric for (const auto &R : RL)
770b57cec5SDimitry Andric if (!is_contained(*this, R))
780b57cec5SDimitry Andric push_back(R);
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric
810b57cec5SDimitry Andric // Merge all overlapping ranges in the list, so that all that remains
820b57cec5SDimitry Andric // is a list of disjoint ranges.
unionize(bool MergeAdjacent)830b57cec5SDimitry Andric void HexagonBlockRanges::RangeList::unionize(bool MergeAdjacent) {
840b57cec5SDimitry Andric if (empty())
850b57cec5SDimitry Andric return;
860b57cec5SDimitry Andric
87e8d8bef9SDimitry Andric llvm::sort(*this);
880b57cec5SDimitry Andric iterator Iter = begin();
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric while (Iter != end()-1) {
910b57cec5SDimitry Andric iterator Next = std::next(Iter);
920b57cec5SDimitry Andric // If MergeAdjacent is true, merge ranges A and B, where A.end == B.start.
930b57cec5SDimitry Andric // This allows merging dead ranges, but is not valid for live ranges.
940b57cec5SDimitry Andric bool Merge = MergeAdjacent && (Iter->end() == Next->start());
950b57cec5SDimitry Andric if (Merge || Iter->overlaps(*Next)) {
960b57cec5SDimitry Andric Iter->merge(*Next);
970b57cec5SDimitry Andric erase(Next);
980b57cec5SDimitry Andric continue;
990b57cec5SDimitry Andric }
1000b57cec5SDimitry Andric ++Iter;
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric }
1030b57cec5SDimitry Andric
1040b57cec5SDimitry Andric // Compute a range A-B and add it to the list.
addsub(const IndexRange & A,const IndexRange & B)1050b57cec5SDimitry Andric void HexagonBlockRanges::RangeList::addsub(const IndexRange &A,
1060b57cec5SDimitry Andric const IndexRange &B) {
1070b57cec5SDimitry Andric // Exclusion of non-overlapping ranges makes some checks simpler
1080b57cec5SDimitry Andric // later in this function.
1090b57cec5SDimitry Andric if (!A.overlaps(B)) {
1100b57cec5SDimitry Andric // A - B = A.
1110b57cec5SDimitry Andric add(A);
1120b57cec5SDimitry Andric return;
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric
1150b57cec5SDimitry Andric IndexType AS = A.start(), AE = A.end();
1160b57cec5SDimitry Andric IndexType BS = B.start(), BE = B.end();
1170b57cec5SDimitry Andric
1180b57cec5SDimitry Andric // If AE is None, then A is included in B, since A and B overlap.
1190b57cec5SDimitry Andric // The result of subtraction if empty, so just return.
1200b57cec5SDimitry Andric if (AE == IndexType::None)
1210b57cec5SDimitry Andric return;
1220b57cec5SDimitry Andric
1230b57cec5SDimitry Andric if (AS < BS) {
1240b57cec5SDimitry Andric // A starts before B.
1250b57cec5SDimitry Andric // AE cannot be None since A and B overlap.
1260b57cec5SDimitry Andric assert(AE != IndexType::None);
1270b57cec5SDimitry Andric // Add the part of A that extends on the "less" side of B.
1280b57cec5SDimitry Andric add(AS, BS, A.Fixed, false);
1290b57cec5SDimitry Andric }
1300b57cec5SDimitry Andric
1310b57cec5SDimitry Andric if (BE < AE) {
1320b57cec5SDimitry Andric // BE cannot be Exit here.
1330b57cec5SDimitry Andric if (BE == IndexType::None)
1340b57cec5SDimitry Andric add(BS, AE, A.Fixed, false);
1350b57cec5SDimitry Andric else
1360b57cec5SDimitry Andric add(BE, AE, A.Fixed, false);
1370b57cec5SDimitry Andric }
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric
1400b57cec5SDimitry Andric // Subtract a given range from each element in the list.
subtract(const IndexRange & Range)1410b57cec5SDimitry Andric void HexagonBlockRanges::RangeList::subtract(const IndexRange &Range) {
1420b57cec5SDimitry Andric // Cannot assume that the list is unionized (i.e. contains only non-
1430b57cec5SDimitry Andric // overlapping ranges.
1440b57cec5SDimitry Andric RangeList T;
1450b57cec5SDimitry Andric for (iterator Next, I = begin(); I != end(); I = Next) {
1460b57cec5SDimitry Andric IndexRange &Rg = *I;
1470b57cec5SDimitry Andric if (Rg.overlaps(Range)) {
1480b57cec5SDimitry Andric T.addsub(Rg, Range);
1490b57cec5SDimitry Andric Next = this->erase(I);
1500b57cec5SDimitry Andric } else {
1510b57cec5SDimitry Andric Next = std::next(I);
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric }
1540b57cec5SDimitry Andric include(T);
1550b57cec5SDimitry Andric }
1560b57cec5SDimitry Andric
InstrIndexMap(MachineBasicBlock & B)1570b57cec5SDimitry Andric HexagonBlockRanges::InstrIndexMap::InstrIndexMap(MachineBasicBlock &B)
1580b57cec5SDimitry Andric : Block(B) {
1590b57cec5SDimitry Andric IndexType Idx = IndexType::First;
1600b57cec5SDimitry Andric First = Idx;
1610b57cec5SDimitry Andric for (auto &In : B) {
1620b57cec5SDimitry Andric if (In.isDebugInstr())
1630b57cec5SDimitry Andric continue;
1640b57cec5SDimitry Andric assert(getIndex(&In) == IndexType::None && "Instruction already in map");
1650b57cec5SDimitry Andric Map.insert(std::make_pair(Idx, &In));
1660b57cec5SDimitry Andric ++Idx;
1670b57cec5SDimitry Andric }
1680b57cec5SDimitry Andric Last = B.empty() ? IndexType::None : unsigned(Idx)-1;
1690b57cec5SDimitry Andric }
1700b57cec5SDimitry Andric
getInstr(IndexType Idx) const1710b57cec5SDimitry Andric MachineInstr *HexagonBlockRanges::InstrIndexMap::getInstr(IndexType Idx) const {
1720b57cec5SDimitry Andric auto F = Map.find(Idx);
1730b57cec5SDimitry Andric return (F != Map.end()) ? F->second : nullptr;
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric
getIndex(MachineInstr * MI) const1760b57cec5SDimitry Andric HexagonBlockRanges::IndexType HexagonBlockRanges::InstrIndexMap::getIndex(
1770b57cec5SDimitry Andric MachineInstr *MI) const {
178bdd1243dSDimitry Andric for (const auto &I : Map)
1790b57cec5SDimitry Andric if (I.second == MI)
1800b57cec5SDimitry Andric return I.first;
1810b57cec5SDimitry Andric return IndexType::None;
1820b57cec5SDimitry Andric }
1830b57cec5SDimitry Andric
getPrevIndex(IndexType Idx) const1840b57cec5SDimitry Andric HexagonBlockRanges::IndexType HexagonBlockRanges::InstrIndexMap::getPrevIndex(
1850b57cec5SDimitry Andric IndexType Idx) const {
1860b57cec5SDimitry Andric assert (Idx != IndexType::None);
1870b57cec5SDimitry Andric if (Idx == IndexType::Entry)
1880b57cec5SDimitry Andric return IndexType::None;
1890b57cec5SDimitry Andric if (Idx == IndexType::Exit)
1900b57cec5SDimitry Andric return Last;
1910b57cec5SDimitry Andric if (Idx == First)
1920b57cec5SDimitry Andric return IndexType::Entry;
1930b57cec5SDimitry Andric return unsigned(Idx)-1;
1940b57cec5SDimitry Andric }
1950b57cec5SDimitry Andric
getNextIndex(IndexType Idx) const1960b57cec5SDimitry Andric HexagonBlockRanges::IndexType HexagonBlockRanges::InstrIndexMap::getNextIndex(
1970b57cec5SDimitry Andric IndexType Idx) const {
1980b57cec5SDimitry Andric assert (Idx != IndexType::None);
1990b57cec5SDimitry Andric if (Idx == IndexType::Entry)
2000b57cec5SDimitry Andric return IndexType::First;
2010b57cec5SDimitry Andric if (Idx == IndexType::Exit || Idx == Last)
2020b57cec5SDimitry Andric return IndexType::None;
2030b57cec5SDimitry Andric return unsigned(Idx)+1;
2040b57cec5SDimitry Andric }
2050b57cec5SDimitry Andric
replaceInstr(MachineInstr * OldMI,MachineInstr * NewMI)2060b57cec5SDimitry Andric void HexagonBlockRanges::InstrIndexMap::replaceInstr(MachineInstr *OldMI,
2070b57cec5SDimitry Andric MachineInstr *NewMI) {
2080b57cec5SDimitry Andric for (auto &I : Map) {
2090b57cec5SDimitry Andric if (I.second != OldMI)
2100b57cec5SDimitry Andric continue;
2110b57cec5SDimitry Andric if (NewMI != nullptr)
2120b57cec5SDimitry Andric I.second = NewMI;
2130b57cec5SDimitry Andric else
2140b57cec5SDimitry Andric Map.erase(I.first);
2150b57cec5SDimitry Andric break;
2160b57cec5SDimitry Andric }
2170b57cec5SDimitry Andric }
2180b57cec5SDimitry Andric
HexagonBlockRanges(MachineFunction & mf)2190b57cec5SDimitry Andric HexagonBlockRanges::HexagonBlockRanges(MachineFunction &mf)
2200b57cec5SDimitry Andric : MF(mf), HST(mf.getSubtarget<HexagonSubtarget>()),
2210b57cec5SDimitry Andric TII(*HST.getInstrInfo()), TRI(*HST.getRegisterInfo()),
2220b57cec5SDimitry Andric Reserved(TRI.getReservedRegs(mf)) {
2230b57cec5SDimitry Andric // Consider all non-allocatable registers as reserved.
2240b57cec5SDimitry Andric for (const TargetRegisterClass *RC : TRI.regclasses()) {
2250b57cec5SDimitry Andric if (RC->isAllocatable())
2260b57cec5SDimitry Andric continue;
2270b57cec5SDimitry Andric for (unsigned R : *RC)
2280b57cec5SDimitry Andric Reserved[R] = true;
2290b57cec5SDimitry Andric }
2300b57cec5SDimitry Andric }
2310b57cec5SDimitry Andric
getLiveIns(const MachineBasicBlock & B,const MachineRegisterInfo & MRI,const TargetRegisterInfo & TRI)2320b57cec5SDimitry Andric HexagonBlockRanges::RegisterSet HexagonBlockRanges::getLiveIns(
2330b57cec5SDimitry Andric const MachineBasicBlock &B, const MachineRegisterInfo &MRI,
2340b57cec5SDimitry Andric const TargetRegisterInfo &TRI) {
2350b57cec5SDimitry Andric RegisterSet LiveIns;
2360b57cec5SDimitry Andric RegisterSet Tmp;
2370b57cec5SDimitry Andric
2380b57cec5SDimitry Andric for (auto I : B.liveins()) {
2390b57cec5SDimitry Andric MCSubRegIndexIterator S(I.PhysReg, &TRI);
2400b57cec5SDimitry Andric if (I.LaneMask.all() || (I.LaneMask.any() && !S.isValid())) {
2410b57cec5SDimitry Andric Tmp.insert({I.PhysReg, 0});
2420b57cec5SDimitry Andric continue;
2430b57cec5SDimitry Andric }
2440b57cec5SDimitry Andric for (; S.isValid(); ++S) {
2450b57cec5SDimitry Andric unsigned SI = S.getSubRegIndex();
2460b57cec5SDimitry Andric if ((I.LaneMask & TRI.getSubRegIndexLaneMask(SI)).any())
2470b57cec5SDimitry Andric Tmp.insert({S.getSubReg(), 0});
2480b57cec5SDimitry Andric }
2490b57cec5SDimitry Andric }
2500b57cec5SDimitry Andric
2510b57cec5SDimitry Andric for (auto R : Tmp) {
2520b57cec5SDimitry Andric if (!Reserved[R.Reg])
2530b57cec5SDimitry Andric LiveIns.insert(R);
2540b57cec5SDimitry Andric for (auto S : expandToSubRegs(R, MRI, TRI))
2550b57cec5SDimitry Andric if (!Reserved[S.Reg])
2560b57cec5SDimitry Andric LiveIns.insert(S);
2570b57cec5SDimitry Andric }
2580b57cec5SDimitry Andric return LiveIns;
2590b57cec5SDimitry Andric }
2600b57cec5SDimitry Andric
expandToSubRegs(RegisterRef R,const MachineRegisterInfo & MRI,const TargetRegisterInfo & TRI)2610b57cec5SDimitry Andric HexagonBlockRanges::RegisterSet HexagonBlockRanges::expandToSubRegs(
2620b57cec5SDimitry Andric RegisterRef R, const MachineRegisterInfo &MRI,
2630b57cec5SDimitry Andric const TargetRegisterInfo &TRI) {
2640b57cec5SDimitry Andric RegisterSet SRs;
2650b57cec5SDimitry Andric
2660b57cec5SDimitry Andric if (R.Sub != 0) {
2670b57cec5SDimitry Andric SRs.insert(R);
2680b57cec5SDimitry Andric return SRs;
2690b57cec5SDimitry Andric }
2700b57cec5SDimitry Andric
271bdd1243dSDimitry Andric if (R.Reg.isPhysical()) {
272*06c3fb27SDimitry Andric if (TRI.subregs(R.Reg).empty())
2730b57cec5SDimitry Andric SRs.insert({R.Reg, 0});
274*06c3fb27SDimitry Andric for (MCPhysReg I : TRI.subregs(R.Reg))
275*06c3fb27SDimitry Andric SRs.insert({I, 0});
2760b57cec5SDimitry Andric } else {
277e8d8bef9SDimitry Andric assert(R.Reg.isVirtual());
2780b57cec5SDimitry Andric auto &RC = *MRI.getRegClass(R.Reg);
2790b57cec5SDimitry Andric unsigned PReg = *RC.begin();
2800b57cec5SDimitry Andric MCSubRegIndexIterator I(PReg, &TRI);
2810b57cec5SDimitry Andric if (!I.isValid())
2820b57cec5SDimitry Andric SRs.insert({R.Reg, 0});
2830b57cec5SDimitry Andric for (; I.isValid(); ++I)
2840b57cec5SDimitry Andric SRs.insert({R.Reg, I.getSubRegIndex()});
2850b57cec5SDimitry Andric }
2860b57cec5SDimitry Andric return SRs;
2870b57cec5SDimitry Andric }
2880b57cec5SDimitry Andric
computeInitialLiveRanges(InstrIndexMap & IndexMap,RegToRangeMap & LiveMap)2890b57cec5SDimitry Andric void HexagonBlockRanges::computeInitialLiveRanges(InstrIndexMap &IndexMap,
2900b57cec5SDimitry Andric RegToRangeMap &LiveMap) {
2910b57cec5SDimitry Andric std::map<RegisterRef,IndexType> LastDef, LastUse;
2920b57cec5SDimitry Andric RegisterSet LiveOnEntry;
2930b57cec5SDimitry Andric MachineBasicBlock &B = IndexMap.getBlock();
2940b57cec5SDimitry Andric MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
2950b57cec5SDimitry Andric
2960b57cec5SDimitry Andric for (auto R : getLiveIns(B, MRI, TRI))
2970b57cec5SDimitry Andric LiveOnEntry.insert(R);
2980b57cec5SDimitry Andric
2990b57cec5SDimitry Andric for (auto R : LiveOnEntry)
3000b57cec5SDimitry Andric LastDef[R] = IndexType::Entry;
3010b57cec5SDimitry Andric
3020b57cec5SDimitry Andric auto closeRange = [&LastUse,&LastDef,&LiveMap] (RegisterRef R) -> void {
3030b57cec5SDimitry Andric auto LD = LastDef[R], LU = LastUse[R];
3040b57cec5SDimitry Andric if (LD == IndexType::None)
3050b57cec5SDimitry Andric LD = IndexType::Entry;
3060b57cec5SDimitry Andric if (LU == IndexType::None)
3070b57cec5SDimitry Andric LU = IndexType::Exit;
3080b57cec5SDimitry Andric LiveMap[R].add(LD, LU, false, false);
3090b57cec5SDimitry Andric LastUse[R] = LastDef[R] = IndexType::None;
3100b57cec5SDimitry Andric };
3110b57cec5SDimitry Andric
3120b57cec5SDimitry Andric RegisterSet Defs, Clobbers;
3130b57cec5SDimitry Andric
3140b57cec5SDimitry Andric for (auto &In : B) {
3150b57cec5SDimitry Andric if (In.isDebugInstr())
3160b57cec5SDimitry Andric continue;
3170b57cec5SDimitry Andric IndexType Index = IndexMap.getIndex(&In);
3180b57cec5SDimitry Andric // Process uses first.
3190b57cec5SDimitry Andric for (auto &Op : In.operands()) {
3200b57cec5SDimitry Andric if (!Op.isReg() || !Op.isUse() || Op.isUndef())
3210b57cec5SDimitry Andric continue;
3220b57cec5SDimitry Andric RegisterRef R = { Op.getReg(), Op.getSubReg() };
323bdd1243dSDimitry Andric if (R.Reg.isPhysical() && Reserved[R.Reg])
3240b57cec5SDimitry Andric continue;
3250b57cec5SDimitry Andric bool IsKill = Op.isKill();
3260b57cec5SDimitry Andric for (auto S : expandToSubRegs(R, MRI, TRI)) {
3270b57cec5SDimitry Andric LastUse[S] = Index;
3280b57cec5SDimitry Andric if (IsKill)
3290b57cec5SDimitry Andric closeRange(S);
3300b57cec5SDimitry Andric }
3310b57cec5SDimitry Andric }
3320b57cec5SDimitry Andric // Process defs and clobbers.
3330b57cec5SDimitry Andric Defs.clear();
3340b57cec5SDimitry Andric Clobbers.clear();
3350b57cec5SDimitry Andric for (auto &Op : In.operands()) {
3360b57cec5SDimitry Andric if (!Op.isReg() || !Op.isDef() || Op.isUndef())
3370b57cec5SDimitry Andric continue;
3380b57cec5SDimitry Andric RegisterRef R = { Op.getReg(), Op.getSubReg() };
3390b57cec5SDimitry Andric for (auto S : expandToSubRegs(R, MRI, TRI)) {
340bdd1243dSDimitry Andric if (S.Reg.isPhysical() && Reserved[S.Reg])
3410b57cec5SDimitry Andric continue;
3420b57cec5SDimitry Andric if (Op.isDead())
3430b57cec5SDimitry Andric Clobbers.insert(S);
3440b57cec5SDimitry Andric else
3450b57cec5SDimitry Andric Defs.insert(S);
3460b57cec5SDimitry Andric }
3470b57cec5SDimitry Andric }
3480b57cec5SDimitry Andric
3490b57cec5SDimitry Andric for (auto &Op : In.operands()) {
3500b57cec5SDimitry Andric if (!Op.isRegMask())
3510b57cec5SDimitry Andric continue;
3520b57cec5SDimitry Andric const uint32_t *BM = Op.getRegMask();
3530b57cec5SDimitry Andric for (unsigned PR = 1, N = TRI.getNumRegs(); PR != N; ++PR) {
3540b57cec5SDimitry Andric // Skip registers that have subregisters. A register is preserved
3550b57cec5SDimitry Andric // iff its bit is set in the regmask, so if R1:0 was preserved, both
3560b57cec5SDimitry Andric // R1 and R0 would also be present.
357*06c3fb27SDimitry Andric if (!TRI.subregs(PR).empty())
3580b57cec5SDimitry Andric continue;
3590b57cec5SDimitry Andric if (Reserved[PR])
3600b57cec5SDimitry Andric continue;
3610b57cec5SDimitry Andric if (BM[PR/32] & (1u << (PR%32)))
3620b57cec5SDimitry Andric continue;
3630b57cec5SDimitry Andric RegisterRef R = { PR, 0 };
3640b57cec5SDimitry Andric if (!Defs.count(R))
3650b57cec5SDimitry Andric Clobbers.insert(R);
3660b57cec5SDimitry Andric }
3670b57cec5SDimitry Andric }
3680b57cec5SDimitry Andric // Defs and clobbers can overlap, e.g.
3690b57cec5SDimitry Andric // dead %d0 = COPY %5, implicit-def %r0, implicit-def %r1
3700b57cec5SDimitry Andric for (RegisterRef R : Defs)
3710b57cec5SDimitry Andric Clobbers.erase(R);
3720b57cec5SDimitry Andric
3730b57cec5SDimitry Andric // Update maps for defs.
3740b57cec5SDimitry Andric for (RegisterRef S : Defs) {
3750b57cec5SDimitry Andric // Defs should already be expanded into subregs.
376*06c3fb27SDimitry Andric assert(!S.Reg.isPhysical() || TRI.subregs(S.Reg).empty());
3770b57cec5SDimitry Andric if (LastDef[S] != IndexType::None || LastUse[S] != IndexType::None)
3780b57cec5SDimitry Andric closeRange(S);
3790b57cec5SDimitry Andric LastDef[S] = Index;
3800b57cec5SDimitry Andric }
3810b57cec5SDimitry Andric // Update maps for clobbers.
3820b57cec5SDimitry Andric for (RegisterRef S : Clobbers) {
3830b57cec5SDimitry Andric // Clobbers should already be expanded into subregs.
384*06c3fb27SDimitry Andric assert(!S.Reg.isPhysical() || TRI.subregs(S.Reg).empty());
3850b57cec5SDimitry Andric if (LastDef[S] != IndexType::None || LastUse[S] != IndexType::None)
3860b57cec5SDimitry Andric closeRange(S);
3870b57cec5SDimitry Andric // Create a single-instruction range.
3880b57cec5SDimitry Andric LastDef[S] = LastUse[S] = Index;
3890b57cec5SDimitry Andric closeRange(S);
3900b57cec5SDimitry Andric }
3910b57cec5SDimitry Andric }
3920b57cec5SDimitry Andric
3930b57cec5SDimitry Andric // Collect live-on-exit.
3940b57cec5SDimitry Andric RegisterSet LiveOnExit;
3950b57cec5SDimitry Andric for (auto *SB : B.successors())
3960b57cec5SDimitry Andric for (auto R : getLiveIns(*SB, MRI, TRI))
3970b57cec5SDimitry Andric LiveOnExit.insert(R);
3980b57cec5SDimitry Andric
3990b57cec5SDimitry Andric for (auto R : LiveOnExit)
4000b57cec5SDimitry Andric LastUse[R] = IndexType::Exit;
4010b57cec5SDimitry Andric
4020b57cec5SDimitry Andric // Process remaining registers.
4030b57cec5SDimitry Andric RegisterSet Left;
4040b57cec5SDimitry Andric for (auto &I : LastUse)
4050b57cec5SDimitry Andric if (I.second != IndexType::None)
4060b57cec5SDimitry Andric Left.insert(I.first);
4070b57cec5SDimitry Andric for (auto &I : LastDef)
4080b57cec5SDimitry Andric if (I.second != IndexType::None)
4090b57cec5SDimitry Andric Left.insert(I.first);
4100b57cec5SDimitry Andric for (auto R : Left)
4110b57cec5SDimitry Andric closeRange(R);
4120b57cec5SDimitry Andric
4130b57cec5SDimitry Andric // Finalize the live ranges.
4140b57cec5SDimitry Andric for (auto &P : LiveMap)
4150b57cec5SDimitry Andric P.second.unionize();
4160b57cec5SDimitry Andric }
4170b57cec5SDimitry Andric
computeLiveMap(InstrIndexMap & IndexMap)4180b57cec5SDimitry Andric HexagonBlockRanges::RegToRangeMap HexagonBlockRanges::computeLiveMap(
4190b57cec5SDimitry Andric InstrIndexMap &IndexMap) {
4200b57cec5SDimitry Andric RegToRangeMap LiveMap;
4210b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << __func__ << ": index map\n" << IndexMap << '\n');
4220b57cec5SDimitry Andric computeInitialLiveRanges(IndexMap, LiveMap);
4230b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << __func__ << ": live map\n"
4240b57cec5SDimitry Andric << PrintRangeMap(LiveMap, TRI) << '\n');
4250b57cec5SDimitry Andric return LiveMap;
4260b57cec5SDimitry Andric }
4270b57cec5SDimitry Andric
computeDeadMap(InstrIndexMap & IndexMap,RegToRangeMap & LiveMap)4280b57cec5SDimitry Andric HexagonBlockRanges::RegToRangeMap HexagonBlockRanges::computeDeadMap(
4290b57cec5SDimitry Andric InstrIndexMap &IndexMap, RegToRangeMap &LiveMap) {
4300b57cec5SDimitry Andric RegToRangeMap DeadMap;
4310b57cec5SDimitry Andric
4320b57cec5SDimitry Andric auto addDeadRanges = [&IndexMap,&LiveMap,&DeadMap] (RegisterRef R) -> void {
4330b57cec5SDimitry Andric auto F = LiveMap.find(R);
4340b57cec5SDimitry Andric if (F == LiveMap.end() || F->second.empty()) {
4350b57cec5SDimitry Andric DeadMap[R].add(IndexType::Entry, IndexType::Exit, false, false);
4360b57cec5SDimitry Andric return;
4370b57cec5SDimitry Andric }
4380b57cec5SDimitry Andric
4390b57cec5SDimitry Andric RangeList &RL = F->second;
4400b57cec5SDimitry Andric RangeList::iterator A = RL.begin(), Z = RL.end()-1;
4410b57cec5SDimitry Andric
4420b57cec5SDimitry Andric // Try to create the initial range.
4430b57cec5SDimitry Andric if (A->start() != IndexType::Entry) {
4440b57cec5SDimitry Andric IndexType DE = IndexMap.getPrevIndex(A->start());
4450b57cec5SDimitry Andric if (DE != IndexType::Entry)
4460b57cec5SDimitry Andric DeadMap[R].add(IndexType::Entry, DE, false, false);
4470b57cec5SDimitry Andric }
4480b57cec5SDimitry Andric
4490b57cec5SDimitry Andric while (A != Z) {
4500b57cec5SDimitry Andric // Creating a dead range that follows A. Pay attention to empty
4510b57cec5SDimitry Andric // ranges (i.e. those ending with "None").
4520b57cec5SDimitry Andric IndexType AE = (A->end() == IndexType::None) ? A->start() : A->end();
4530b57cec5SDimitry Andric IndexType DS = IndexMap.getNextIndex(AE);
4540b57cec5SDimitry Andric ++A;
4550b57cec5SDimitry Andric IndexType DE = IndexMap.getPrevIndex(A->start());
4560b57cec5SDimitry Andric if (DS < DE)
4570b57cec5SDimitry Andric DeadMap[R].add(DS, DE, false, false);
4580b57cec5SDimitry Andric }
4590b57cec5SDimitry Andric
4600b57cec5SDimitry Andric // Try to create the final range.
4610b57cec5SDimitry Andric if (Z->end() != IndexType::Exit) {
4620b57cec5SDimitry Andric IndexType ZE = (Z->end() == IndexType::None) ? Z->start() : Z->end();
4630b57cec5SDimitry Andric IndexType DS = IndexMap.getNextIndex(ZE);
4640b57cec5SDimitry Andric if (DS < IndexType::Exit)
4650b57cec5SDimitry Andric DeadMap[R].add(DS, IndexType::Exit, false, false);
4660b57cec5SDimitry Andric }
4670b57cec5SDimitry Andric };
4680b57cec5SDimitry Andric
4690b57cec5SDimitry Andric MachineFunction &MF = *IndexMap.getBlock().getParent();
4700b57cec5SDimitry Andric auto &MRI = MF.getRegInfo();
4710b57cec5SDimitry Andric unsigned NumRegs = TRI.getNumRegs();
4720b57cec5SDimitry Andric BitVector Visited(NumRegs);
4730b57cec5SDimitry Andric for (unsigned R = 1; R < NumRegs; ++R) {
4740b57cec5SDimitry Andric for (auto S : expandToSubRegs({R,0}, MRI, TRI)) {
4750b57cec5SDimitry Andric if (Reserved[S.Reg] || Visited[S.Reg])
4760b57cec5SDimitry Andric continue;
4770b57cec5SDimitry Andric addDeadRanges(S);
4780b57cec5SDimitry Andric Visited[S.Reg] = true;
4790b57cec5SDimitry Andric }
4800b57cec5SDimitry Andric }
4810b57cec5SDimitry Andric for (auto &P : LiveMap)
482e8d8bef9SDimitry Andric if (P.first.Reg.isVirtual())
4830b57cec5SDimitry Andric addDeadRanges(P.first);
4840b57cec5SDimitry Andric
4850b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << __func__ << ": dead map\n"
4860b57cec5SDimitry Andric << PrintRangeMap(DeadMap, TRI) << '\n');
4870b57cec5SDimitry Andric return DeadMap;
4880b57cec5SDimitry Andric }
4890b57cec5SDimitry Andric
operator <<(raw_ostream & OS,HexagonBlockRanges::IndexType Idx)4900b57cec5SDimitry Andric raw_ostream &llvm::operator<<(raw_ostream &OS,
4910b57cec5SDimitry Andric HexagonBlockRanges::IndexType Idx) {
4920b57cec5SDimitry Andric if (Idx == HexagonBlockRanges::IndexType::None)
4930b57cec5SDimitry Andric return OS << '-';
4940b57cec5SDimitry Andric if (Idx == HexagonBlockRanges::IndexType::Entry)
4950b57cec5SDimitry Andric return OS << 'n';
4960b57cec5SDimitry Andric if (Idx == HexagonBlockRanges::IndexType::Exit)
4970b57cec5SDimitry Andric return OS << 'x';
4980b57cec5SDimitry Andric return OS << unsigned(Idx)-HexagonBlockRanges::IndexType::First+1;
4990b57cec5SDimitry Andric }
5000b57cec5SDimitry Andric
5010b57cec5SDimitry Andric // A mapping to translate between instructions and their indices.
operator <<(raw_ostream & OS,const HexagonBlockRanges::IndexRange & IR)5020b57cec5SDimitry Andric raw_ostream &llvm::operator<<(raw_ostream &OS,
5030b57cec5SDimitry Andric const HexagonBlockRanges::IndexRange &IR) {
5040b57cec5SDimitry Andric OS << '[' << IR.start() << ':' << IR.end() << (IR.TiedEnd ? '}' : ']');
5050b57cec5SDimitry Andric if (IR.Fixed)
5060b57cec5SDimitry Andric OS << '!';
5070b57cec5SDimitry Andric return OS;
5080b57cec5SDimitry Andric }
5090b57cec5SDimitry Andric
operator <<(raw_ostream & OS,const HexagonBlockRanges::RangeList & RL)5100b57cec5SDimitry Andric raw_ostream &llvm::operator<<(raw_ostream &OS,
5110b57cec5SDimitry Andric const HexagonBlockRanges::RangeList &RL) {
512bdd1243dSDimitry Andric for (const auto &R : RL)
5130b57cec5SDimitry Andric OS << R << " ";
5140b57cec5SDimitry Andric return OS;
5150b57cec5SDimitry Andric }
5160b57cec5SDimitry Andric
operator <<(raw_ostream & OS,const HexagonBlockRanges::InstrIndexMap & M)5170b57cec5SDimitry Andric raw_ostream &llvm::operator<<(raw_ostream &OS,
5180b57cec5SDimitry Andric const HexagonBlockRanges::InstrIndexMap &M) {
5190b57cec5SDimitry Andric for (auto &In : M.Block) {
5200b57cec5SDimitry Andric HexagonBlockRanges::IndexType Idx = M.getIndex(&In);
5210b57cec5SDimitry Andric OS << Idx << (Idx == M.Last ? ". " : " ") << In;
5220b57cec5SDimitry Andric }
5230b57cec5SDimitry Andric return OS;
5240b57cec5SDimitry Andric }
5250b57cec5SDimitry Andric
operator <<(raw_ostream & OS,const HexagonBlockRanges::PrintRangeMap & P)5260b57cec5SDimitry Andric raw_ostream &llvm::operator<<(raw_ostream &OS,
5270b57cec5SDimitry Andric const HexagonBlockRanges::PrintRangeMap &P) {
528bdd1243dSDimitry Andric for (const auto &I : P.Map) {
5290b57cec5SDimitry Andric const HexagonBlockRanges::RangeList &RL = I.second;
5300b57cec5SDimitry Andric OS << printReg(I.first.Reg, &P.TRI, I.first.Sub) << " -> " << RL << "\n";
5310b57cec5SDimitry Andric }
5320b57cec5SDimitry Andric return OS;
5330b57cec5SDimitry Andric }
534