14f81cdd8SEugene Zelenko //===- AggressiveAntiDepBreaker.cpp - Anti-dep breaker --------------------===//
2de11f36aSDavid Goodwin //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6de11f36aSDavid Goodwin //
7de11f36aSDavid Goodwin //===----------------------------------------------------------------------===//
8de11f36aSDavid Goodwin //
9de11f36aSDavid Goodwin // This file implements the AggressiveAntiDepBreaker class, which
10de11f36aSDavid Goodwin // implements register anti-dependence breaking during post-RA
11de11f36aSDavid Goodwin // scheduling. It attempts to break all anti-dependencies within a
12de11f36aSDavid Goodwin // block.
13de11f36aSDavid Goodwin //
14de11f36aSDavid Goodwin //===----------------------------------------------------------------------===//
15de11f36aSDavid Goodwin
16de11f36aSDavid Goodwin #include "AggressiveAntiDepBreaker.h"
174f81cdd8SEugene Zelenko #include "llvm/ADT/ArrayRef.h"
184f81cdd8SEugene Zelenko #include "llvm/ADT/SmallSet.h"
194f81cdd8SEugene Zelenko #include "llvm/ADT/iterator_range.h"
20de11f36aSDavid Goodwin #include "llvm/CodeGen/MachineBasicBlock.h"
21de11f36aSDavid Goodwin #include "llvm/CodeGen/MachineFrameInfo.h"
224f81cdd8SEugene Zelenko #include "llvm/CodeGen/MachineFunction.h"
23de11f36aSDavid Goodwin #include "llvm/CodeGen/MachineInstr.h"
244f81cdd8SEugene Zelenko #include "llvm/CodeGen/MachineOperand.h"
254f81cdd8SEugene Zelenko #include "llvm/CodeGen/MachineRegisterInfo.h"
2605ff4667SAndrew Trick #include "llvm/CodeGen/RegisterClassInfo.h"
274f81cdd8SEugene Zelenko #include "llvm/CodeGen/ScheduleDAG.h"
283f833edcSDavid Blaikie #include "llvm/CodeGen/TargetInstrInfo.h"
29b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetRegisterInfo.h"
304f81cdd8SEugene Zelenko #include "llvm/MC/MCInstrDesc.h"
314f81cdd8SEugene Zelenko #include "llvm/MC/MCRegisterInfo.h"
32e056d107SDavid Goodwin #include "llvm/Support/CommandLine.h"
33de11f36aSDavid Goodwin #include "llvm/Support/Debug.h"
3413e77db2SDavid Blaikie #include "llvm/Support/MachineValueType.h"
35de11f36aSDavid Goodwin #include "llvm/Support/raw_ostream.h"
364f81cdd8SEugene Zelenko #include <cassert>
374f81cdd8SEugene Zelenko #include <utility>
384f81cdd8SEugene Zelenko
39de11f36aSDavid Goodwin using namespace llvm;
40de11f36aSDavid Goodwin
411b9dde08SChandler Carruth #define DEBUG_TYPE "post-RA-sched"
421b9dde08SChandler Carruth
43dd1c6198SDavid Goodwin // If DebugDiv > 0 then only break antidep with (ID % DebugDiv) == DebugMod
44dd1c6198SDavid Goodwin static cl::opt<int>
45dd1c6198SDavid Goodwin DebugDiv("agg-antidep-debugdiv",
46dd1c6198SDavid Goodwin cl::desc("Debug control for aggressive anti-dep breaker"),
47dd1c6198SDavid Goodwin cl::init(0), cl::Hidden);
484f81cdd8SEugene Zelenko
49dd1c6198SDavid Goodwin static cl::opt<int>
50dd1c6198SDavid Goodwin DebugMod("agg-antidep-debugmod",
51dd1c6198SDavid Goodwin cl::desc("Debug control for aggressive anti-dep breaker"),
52dd1c6198SDavid Goodwin cl::init(0), cl::Hidden);
53dd1c6198SDavid Goodwin
AggressiveAntiDepState(const unsigned TargetRegs,MachineBasicBlock * BB)54a45fe676SDavid Goodwin AggressiveAntiDepState::AggressiveAntiDepState(const unsigned TargetRegs,
554f81cdd8SEugene Zelenko MachineBasicBlock *BB)
564f81cdd8SEugene Zelenko : NumTargetRegs(TargetRegs), GroupNodes(TargetRegs, 0),
574f81cdd8SEugene Zelenko GroupNodeIndices(TargetRegs, 0), KillIndices(TargetRegs, 0),
584f81cdd8SEugene Zelenko DefIndices(TargetRegs, 0) {
59a45fe676SDavid Goodwin const unsigned BBSize = BB->size();
60a45fe676SDavid Goodwin for (unsigned i = 0; i < NumTargetRegs; ++i) {
61de11f36aSDavid Goodwin // Initialize all registers to be in their own group. Initially we
62de11f36aSDavid Goodwin // assign the register to the same-indexed GroupNode.
63de11f36aSDavid Goodwin GroupNodeIndices[i] = i;
64de11f36aSDavid Goodwin // Initialize the indices to indicate that no registers are live.
65a45fe676SDavid Goodwin KillIndices[i] = ~0u;
66a45fe676SDavid Goodwin DefIndices[i] = BBSize;
67a45fe676SDavid Goodwin }
68de11f36aSDavid Goodwin }
69de11f36aSDavid Goodwin
GetGroup(unsigned Reg)705a8d15c5SBill Wendling unsigned AggressiveAntiDepState::GetGroup(unsigned Reg) {
71de11f36aSDavid Goodwin unsigned Node = GroupNodeIndices[Reg];
72de11f36aSDavid Goodwin while (GroupNodes[Node] != Node)
73de11f36aSDavid Goodwin Node = GroupNodes[Node];
74de11f36aSDavid Goodwin
75de11f36aSDavid Goodwin return Node;
76de11f36aSDavid Goodwin }
77de11f36aSDavid Goodwin
GetGroupRegs(unsigned Group,std::vector<unsigned> & Regs,std::multimap<unsigned,AggressiveAntiDepState::RegisterReference> * RegRefs)78b9fe5d5dSDavid Goodwin void AggressiveAntiDepState::GetGroupRegs(
79b9fe5d5dSDavid Goodwin unsigned Group,
80b9fe5d5dSDavid Goodwin std::vector<unsigned> &Regs,
81b9fe5d5dSDavid Goodwin std::multimap<unsigned, AggressiveAntiDepState::RegisterReference> *RegRefs)
82de11f36aSDavid Goodwin {
83a45fe676SDavid Goodwin for (unsigned Reg = 0; Reg != NumTargetRegs; ++Reg) {
84b9fe5d5dSDavid Goodwin if ((GetGroup(Reg) == Group) && (RegRefs->count(Reg) > 0))
85de11f36aSDavid Goodwin Regs.push_back(Reg);
86de11f36aSDavid Goodwin }
87de11f36aSDavid Goodwin }
88de11f36aSDavid Goodwin
UnionGroups(unsigned Reg1,unsigned Reg2)894f81cdd8SEugene Zelenko unsigned AggressiveAntiDepState::UnionGroups(unsigned Reg1, unsigned Reg2) {
90de11f36aSDavid Goodwin assert(GroupNodes[0] == 0 && "GroupNode 0 not parent!");
91de11f36aSDavid Goodwin assert(GroupNodeIndices[0] == 0 && "Reg 0 not in Group 0!");
92de11f36aSDavid Goodwin
93de11f36aSDavid Goodwin // find group for each register
94de11f36aSDavid Goodwin unsigned Group1 = GetGroup(Reg1);
95de11f36aSDavid Goodwin unsigned Group2 = GetGroup(Reg2);
96de11f36aSDavid Goodwin
97de11f36aSDavid Goodwin // if either group is 0, then that must become the parent
98de11f36aSDavid Goodwin unsigned Parent = (Group1 == 0) ? Group1 : Group2;
99de11f36aSDavid Goodwin unsigned Other = (Parent == Group1) ? Group2 : Group1;
100de11f36aSDavid Goodwin GroupNodes.at(Other) = Parent;
101de11f36aSDavid Goodwin return Parent;
102de11f36aSDavid Goodwin }
103de11f36aSDavid Goodwin
LeaveGroup(unsigned Reg)1044f81cdd8SEugene Zelenko unsigned AggressiveAntiDepState::LeaveGroup(unsigned Reg) {
105de11f36aSDavid Goodwin // Create a new GroupNode for Reg. Reg's existing GroupNode must
106de11f36aSDavid Goodwin // stay as is because there could be other GroupNodes referring to
107de11f36aSDavid Goodwin // it.
108de11f36aSDavid Goodwin unsigned idx = GroupNodes.size();
109de11f36aSDavid Goodwin GroupNodes.push_back(idx);
110de11f36aSDavid Goodwin GroupNodeIndices[Reg] = idx;
111de11f36aSDavid Goodwin return idx;
112de11f36aSDavid Goodwin }
113de11f36aSDavid Goodwin
IsLive(unsigned Reg)1144f81cdd8SEugene Zelenko bool AggressiveAntiDepState::IsLive(unsigned Reg) {
115de11f36aSDavid Goodwin // KillIndex must be defined and DefIndex not defined for a register
116de11f36aSDavid Goodwin // to be live.
117de11f36aSDavid Goodwin return((KillIndices[Reg] != ~0u) && (DefIndices[Reg] == ~0u));
118de11f36aSDavid Goodwin }
119de11f36aSDavid Goodwin
AggressiveAntiDepBreaker(MachineFunction & MFi,const RegisterClassInfo & RCI,TargetSubtargetInfo::RegClassVector & CriticalPathRCs)120d913448bSEric Christopher AggressiveAntiDepBreaker::AggressiveAntiDepBreaker(
121d913448bSEric Christopher MachineFunction &MFi, const RegisterClassInfo &RCI,
122d913448bSEric Christopher TargetSubtargetInfo::RegClassVector &CriticalPathRCs)
123*b932bdf5SKazu Hirata : MF(MFi), MRI(MF.getRegInfo()), TII(MF.getSubtarget().getInstrInfo()),
1244f81cdd8SEugene Zelenko TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RCI) {
125b9fe5d5dSDavid Goodwin /* Collect a bitset of all registers that are only broken if they
126b9fe5d5dSDavid Goodwin are on the critical path. */
127b9fe5d5dSDavid Goodwin for (unsigned i = 0, e = CriticalPathRCs.size(); i < e; ++i) {
128b9fe5d5dSDavid Goodwin BitVector CPSet = TRI->getAllocatableSet(MF, CriticalPathRCs[i]);
129b9fe5d5dSDavid Goodwin if (CriticalPathSet.none())
130b9fe5d5dSDavid Goodwin CriticalPathSet = CPSet;
131b9fe5d5dSDavid Goodwin else
132b9fe5d5dSDavid Goodwin CriticalPathSet |= CPSet;
133cf89db13SDavid Goodwin }
134cf89db13SDavid Goodwin
135d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "AntiDep Critical-Path Registers:");
136d34e60caSNicola Zaghen LLVM_DEBUG(for (unsigned r
137d34e60caSNicola Zaghen : CriticalPathSet.set_bits()) dbgs()
138d34e60caSNicola Zaghen << " " << printReg(r, TRI));
139d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << '\n');
140e056d107SDavid Goodwin }
141e056d107SDavid Goodwin
~AggressiveAntiDepBreaker()142e056d107SDavid Goodwin AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() {
143e056d107SDavid Goodwin delete State;
144e056d107SDavid Goodwin }
145e056d107SDavid Goodwin
StartBlock(MachineBasicBlock * BB)146e056d107SDavid Goodwin void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
147c0196b1bSCraig Topper assert(!State);
148a45fe676SDavid Goodwin State = new AggressiveAntiDepState(TRI->getNumRegs(), BB);
149e056d107SDavid Goodwin
150c2d4befbSMatthias Braun bool IsReturnBlock = BB->isReturnBlock();
151030b0286SBill Wendling std::vector<unsigned> &KillIndices = State->GetKillIndices();
152030b0286SBill Wendling std::vector<unsigned> &DefIndices = State->GetDefIndices();
153e056d107SDavid Goodwin
154c338679cSJakob Stoklund Olesen // Examine the live-in regs of all successors.
155d61b4cb9SKazu Hirata for (MachineBasicBlock *Succ : BB->successors())
156d61b4cb9SKazu Hirata for (const auto &LI : Succ->liveins()) {
157d9da1627SMatthias Braun for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI) {
15854038d79SJakob Stoklund Olesen unsigned Reg = *AI;
159e056d107SDavid Goodwin State->UnionGroups(Reg, 0);
160e056d107SDavid Goodwin KillIndices[Reg] = BB->size();
161e056d107SDavid Goodwin DefIndices[Reg] = ~0u;
162e056d107SDavid Goodwin }
163e056d107SDavid Goodwin }
164e056d107SDavid Goodwin
165e056d107SDavid Goodwin // Mark live-out callee-saved registers. In a return block this is
166e056d107SDavid Goodwin // all callee-saved registers. In non-return this is any
167e056d107SDavid Goodwin // callee-saved register that is not saved in the prolog.
168941a705bSMatthias Braun const MachineFrameInfo &MFI = MF.getFrameInfo();
169941a705bSMatthias Braun BitVector Pristine = MFI.getPristineRegs(MF);
170fe34c5e4SOren Ben Simhon for (const MCPhysReg *I = MF.getRegInfo().getCalleeSavedRegs(); *I;
171fe34c5e4SOren Ben Simhon ++I) {
172e056d107SDavid Goodwin unsigned Reg = *I;
1730bd0aa8fSTim Shen if (!IsReturnBlock && !Pristine.test(Reg))
174b9c56d12SEric Christopher continue;
17554038d79SJakob Stoklund Olesen for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
17654038d79SJakob Stoklund Olesen unsigned AliasReg = *AI;
177e056d107SDavid Goodwin State->UnionGroups(AliasReg, 0);
178e056d107SDavid Goodwin KillIndices[AliasReg] = BB->size();
179e056d107SDavid Goodwin DefIndices[AliasReg] = ~0u;
180e056d107SDavid Goodwin }
181e056d107SDavid Goodwin }
182e056d107SDavid Goodwin }
183e056d107SDavid Goodwin
FinishBlock()184e056d107SDavid Goodwin void AggressiveAntiDepBreaker::FinishBlock() {
185e056d107SDavid Goodwin delete State;
186c0196b1bSCraig Topper State = nullptr;
187e056d107SDavid Goodwin }
188e056d107SDavid Goodwin
Observe(MachineInstr & MI,unsigned Count,unsigned InsertPosIndex)1895e6e8c7aSDuncan P. N. Exon Smith void AggressiveAntiDepBreaker::Observe(MachineInstr &MI, unsigned Count,
190e056d107SDavid Goodwin unsigned InsertPosIndex) {
191e056d107SDavid Goodwin assert(Count < InsertPosIndex && "Instruction index out of expected range!");
192e056d107SDavid Goodwin
193faa7660fSDavid Goodwin std::set<unsigned> PassthruRegs;
194faa7660fSDavid Goodwin GetPassthruRegs(MI, PassthruRegs);
195faa7660fSDavid Goodwin PrescanInstruction(MI, Count, PassthruRegs);
196faa7660fSDavid Goodwin ScanInstruction(MI, Count);
197faa7660fSDavid Goodwin
198d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Observe: ");
199d34e60caSNicola Zaghen LLVM_DEBUG(MI.dump());
200d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\tRegs:");
201e056d107SDavid Goodwin
202030b0286SBill Wendling std::vector<unsigned> &DefIndices = State->GetDefIndices();
203a45fe676SDavid Goodwin for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
204e056d107SDavid Goodwin // If Reg is current live, then mark that it can't be renamed as
205e056d107SDavid Goodwin // we don't know the extent of its live-range anymore (now that it
206e056d107SDavid Goodwin // has been scheduled). If it is not live but was defined in the
207e056d107SDavid Goodwin // previous schedule region, then set its def index to the most
208e056d107SDavid Goodwin // conservative location (i.e. the beginning of the previous
209e056d107SDavid Goodwin // schedule region).
210e056d107SDavid Goodwin if (State->IsLive(Reg)) {
211d34e60caSNicola Zaghen LLVM_DEBUG(if (State->GetGroup(Reg) != 0) dbgs()
212d34e60caSNicola Zaghen << " " << printReg(Reg, TRI) << "=g" << State->GetGroup(Reg)
213d34e60caSNicola Zaghen << "->g0(region live-out)");
214e056d107SDavid Goodwin State->UnionGroups(Reg, 0);
215eb431da0SJim Grosbach } else if ((DefIndices[Reg] < InsertPosIndex)
216eb431da0SJim Grosbach && (DefIndices[Reg] >= Count)) {
217e056d107SDavid Goodwin DefIndices[Reg] = Count;
218e056d107SDavid Goodwin }
219e056d107SDavid Goodwin }
220d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << '\n');
221e056d107SDavid Goodwin }
222e056d107SDavid Goodwin
IsImplicitDefUse(MachineInstr & MI,MachineOperand & MO)2235e6e8c7aSDuncan P. N. Exon Smith bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr &MI,
2245e6e8c7aSDuncan P. N. Exon Smith MachineOperand &MO) {
225de11f36aSDavid Goodwin if (!MO.isReg() || !MO.isImplicit())
226de11f36aSDavid Goodwin return false;
227de11f36aSDavid Goodwin
2280c476111SDaniel Sanders Register Reg = MO.getReg();
229de11f36aSDavid Goodwin if (Reg == 0)
230de11f36aSDavid Goodwin return false;
231de11f36aSDavid Goodwin
23247eba05bSChad Rosier MachineOperand *Op = nullptr;
23347eba05bSChad Rosier if (MO.isDef())
2345e6e8c7aSDuncan P. N. Exon Smith Op = MI.findRegisterUseOperand(Reg, true);
23547eba05bSChad Rosier else
2365e6e8c7aSDuncan P. N. Exon Smith Op = MI.findRegisterDefOperand(Reg);
23747eba05bSChad Rosier
238c0196b1bSCraig Topper return(Op && Op->isImplicit());
239de11f36aSDavid Goodwin }
240de11f36aSDavid Goodwin
GetPassthruRegs(MachineInstr & MI,std::set<unsigned> & PassthruRegs)2415e6e8c7aSDuncan P. N. Exon Smith void AggressiveAntiDepBreaker::GetPassthruRegs(
2425e6e8c7aSDuncan P. N. Exon Smith MachineInstr &MI, std::set<unsigned> &PassthruRegs) {
2435e6e8c7aSDuncan P. N. Exon Smith for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
2445e6e8c7aSDuncan P. N. Exon Smith MachineOperand &MO = MI.getOperand(i);
245de11f36aSDavid Goodwin if (!MO.isReg()) continue;
2465e6e8c7aSDuncan P. N. Exon Smith if ((MO.isDef() && MI.isRegTiedToUseOperand(i)) ||
247de11f36aSDavid Goodwin IsImplicitDefUse(MI, MO)) {
2480c476111SDaniel Sanders const Register Reg = MO.getReg();
249abdb1d69SChad Rosier for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
250abdb1d69SChad Rosier SubRegs.isValid(); ++SubRegs)
25154038d79SJakob Stoklund Olesen PassthruRegs.insert(*SubRegs);
252de11f36aSDavid Goodwin }
253de11f36aSDavid Goodwin }
254de11f36aSDavid Goodwin }
255de11f36aSDavid Goodwin
25680a03cc0SDavid Goodwin /// AntiDepEdges - Return in Edges the anti- and output- dependencies
25780a03cc0SDavid Goodwin /// in SU that we want to consider for breaking.
AntiDepEdges(const SUnit * SU,std::vector<const SDep * > & Edges)25835bc4d46SDan Gohman static void AntiDepEdges(const SUnit *SU, std::vector<const SDep *> &Edges) {
25980a03cc0SDavid Goodwin SmallSet<unsigned, 4> RegSet;
260d61b4cb9SKazu Hirata for (const SDep &Pred : SU->Preds) {
261d61b4cb9SKazu Hirata if ((Pred.getKind() == SDep::Anti) || (Pred.getKind() == SDep::Output)) {
262d61b4cb9SKazu Hirata if (RegSet.insert(Pred.getReg()).second)
263d61b4cb9SKazu Hirata Edges.push_back(&Pred);
264de11f36aSDavid Goodwin }
265de11f36aSDavid Goodwin }
266de11f36aSDavid Goodwin }
267de11f36aSDavid Goodwin
268b9fe5d5dSDavid Goodwin /// CriticalPathStep - Return the next SUnit after SU on the bottom-up
269b9fe5d5dSDavid Goodwin /// critical path.
CriticalPathStep(const SUnit * SU)27035bc4d46SDan Gohman static const SUnit *CriticalPathStep(const SUnit *SU) {
271c0196b1bSCraig Topper const SDep *Next = nullptr;
272b9fe5d5dSDavid Goodwin unsigned NextDepth = 0;
273b9fe5d5dSDavid Goodwin // Find the predecessor edge with the greatest depth.
274c0196b1bSCraig Topper if (SU) {
275d61b4cb9SKazu Hirata for (const SDep &Pred : SU->Preds) {
276d61b4cb9SKazu Hirata const SUnit *PredSU = Pred.getSUnit();
277d61b4cb9SKazu Hirata unsigned PredLatency = Pred.getLatency();
278b9fe5d5dSDavid Goodwin unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
279b9fe5d5dSDavid Goodwin // In the case of a latency tie, prefer an anti-dependency edge over
280b9fe5d5dSDavid Goodwin // other types of edges.
281b9fe5d5dSDavid Goodwin if (NextDepth < PredTotalLatency ||
282d61b4cb9SKazu Hirata (NextDepth == PredTotalLatency && Pred.getKind() == SDep::Anti)) {
283b9fe5d5dSDavid Goodwin NextDepth = PredTotalLatency;
284d61b4cb9SKazu Hirata Next = &Pred;
285b9fe5d5dSDavid Goodwin }
286b9fe5d5dSDavid Goodwin }
287b9fe5d5dSDavid Goodwin }
288b9fe5d5dSDavid Goodwin
289c0196b1bSCraig Topper return (Next) ? Next->getSUnit() : nullptr;
290b9fe5d5dSDavid Goodwin }
291b9fe5d5dSDavid Goodwin
HandleLastUse(unsigned Reg,unsigned KillIdx,const char * tag,const char * header,const char * footer)2929f1b2d46SDavid Goodwin void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx,
293eb431da0SJim Grosbach const char *tag,
294eb431da0SJim Grosbach const char *header,
295dd1c6198SDavid Goodwin const char *footer) {
296030b0286SBill Wendling std::vector<unsigned> &KillIndices = State->GetKillIndices();
297030b0286SBill Wendling std::vector<unsigned> &DefIndices = State->GetDefIndices();
2989f1b2d46SDavid Goodwin std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
2999f1b2d46SDavid Goodwin RegRefs = State->GetRegRefs();
3009f1b2d46SDavid Goodwin
30134c94d5cSHal Finkel // FIXME: We must leave subregisters of live super registers as live, so that
30234c94d5cSHal Finkel // we don't clear out the register tracking information for subregisters of
30334c94d5cSHal Finkel // super registers we're still tracking (and with which we're unioning
30434c94d5cSHal Finkel // subregister definitions).
30534c94d5cSHal Finkel for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
30634c94d5cSHal Finkel if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI)) {
307d34e60caSNicola Zaghen LLVM_DEBUG(if (!header && footer) dbgs() << footer);
30834c94d5cSHal Finkel return;
30934c94d5cSHal Finkel }
31034c94d5cSHal Finkel
3119f1b2d46SDavid Goodwin if (!State->IsLive(Reg)) {
3129f1b2d46SDavid Goodwin KillIndices[Reg] = KillIdx;
3139f1b2d46SDavid Goodwin DefIndices[Reg] = ~0u;
3149f1b2d46SDavid Goodwin RegRefs.erase(Reg);
3159f1b2d46SDavid Goodwin State->LeaveGroup(Reg);
316d34e60caSNicola Zaghen LLVM_DEBUG(if (header) {
317d34e60caSNicola Zaghen dbgs() << header << printReg(Reg, TRI);
318d34e60caSNicola Zaghen header = nullptr;
319d34e60caSNicola Zaghen });
320d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag);
32135c61819SChuang-Yu Cheng // Repeat for subregisters. Note that we only do this if the superregister
32235c61819SChuang-Yu Cheng // was not live because otherwise, regardless whether we have an explicit
32335c61819SChuang-Yu Cheng // use of the subregister, the subregister's contents are needed for the
32435c61819SChuang-Yu Cheng // uses of the superregister.
32554038d79SJakob Stoklund Olesen for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
32654038d79SJakob Stoklund Olesen unsigned SubregReg = *SubRegs;
3279f1b2d46SDavid Goodwin if (!State->IsLive(SubregReg)) {
3289f1b2d46SDavid Goodwin KillIndices[SubregReg] = KillIdx;
3299f1b2d46SDavid Goodwin DefIndices[SubregReg] = ~0u;
3309f1b2d46SDavid Goodwin RegRefs.erase(SubregReg);
3319f1b2d46SDavid Goodwin State->LeaveGroup(SubregReg);
332d34e60caSNicola Zaghen LLVM_DEBUG(if (header) {
333d34e60caSNicola Zaghen dbgs() << header << printReg(Reg, TRI);
334d34e60caSNicola Zaghen header = nullptr;
335d34e60caSNicola Zaghen });
336d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " " << printReg(SubregReg, TRI) << "->g"
337d34e60caSNicola Zaghen << State->GetGroup(SubregReg) << tag);
3389f1b2d46SDavid Goodwin }
3399f1b2d46SDavid Goodwin }
34035c61819SChuang-Yu Cheng }
341dd1c6198SDavid Goodwin
342d34e60caSNicola Zaghen LLVM_DEBUG(if (!header && footer) dbgs() << footer);
3439f1b2d46SDavid Goodwin }
3449f1b2d46SDavid Goodwin
PrescanInstruction(MachineInstr & MI,unsigned Count,std::set<unsigned> & PassthruRegs)3455e6e8c7aSDuncan P. N. Exon Smith void AggressiveAntiDepBreaker::PrescanInstruction(
3465e6e8c7aSDuncan P. N. Exon Smith MachineInstr &MI, unsigned Count, std::set<unsigned> &PassthruRegs) {
347030b0286SBill Wendling std::vector<unsigned> &DefIndices = State->GetDefIndices();
348e056d107SDavid Goodwin std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
349e056d107SDavid Goodwin RegRefs = State->GetRegRefs();
350e056d107SDavid Goodwin
3519f1b2d46SDavid Goodwin // Handle dead defs by simulating a last-use of the register just
3520ab5e2cdSChris Lattner // after the def. A dead def can occur because the def is truly
3539f1b2d46SDavid Goodwin // dead, or because only a subregister is live at the def. If we
3549f1b2d46SDavid Goodwin // don't do this the dead def will be incorrectly merged into the
3559f1b2d46SDavid Goodwin // previous def.
356259cd6f8SKazu Hirata for (const MachineOperand &MO : MI.operands()) {
357de11f36aSDavid Goodwin if (!MO.isReg() || !MO.isDef()) continue;
3580c476111SDaniel Sanders Register Reg = MO.getReg();
359de11f36aSDavid Goodwin if (Reg == 0) continue;
360de11f36aSDavid Goodwin
361dd1c6198SDavid Goodwin HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n");
362de11f36aSDavid Goodwin }
363de11f36aSDavid Goodwin
364d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\tDef Groups:");
3655e6e8c7aSDuncan P. N. Exon Smith for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
3665e6e8c7aSDuncan P. N. Exon Smith MachineOperand &MO = MI.getOperand(i);
367de11f36aSDavid Goodwin if (!MO.isReg() || !MO.isDef()) continue;
3680c476111SDaniel Sanders Register Reg = MO.getReg();
369de11f36aSDavid Goodwin if (Reg == 0) continue;
370de11f36aSDavid Goodwin
371d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI) << "=g"
372d34e60caSNicola Zaghen << State->GetGroup(Reg));
373de11f36aSDavid Goodwin
3749f1b2d46SDavid Goodwin // If MI's defs have a special allocation requirement, don't allow
375de11f36aSDavid Goodwin // any def registers to be changed. Also assume all registers
376cf6a8bfeSKyle Butt // defined in a call must not be changed (ABI). Inline assembly may
377cf6a8bfeSKyle Butt // reference either system calls or the register directly. Skip it until we
378cf6a8bfeSKyle Butt // can tell user specified registers from compiler-specified.
3795e6e8c7aSDuncan P. N. Exon Smith if (MI.isCall() || MI.hasExtraDefRegAllocReq() || TII->isPredicated(MI) ||
3805e6e8c7aSDuncan P. N. Exon Smith MI.isInlineAsm()) {
381d34e60caSNicola Zaghen LLVM_DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
382e056d107SDavid Goodwin State->UnionGroups(Reg, 0);
383de11f36aSDavid Goodwin }
384de11f36aSDavid Goodwin
385de11f36aSDavid Goodwin // Any aliased that are live at this point are completely or
3869f1b2d46SDavid Goodwin // partially defined here, so group those aliases with Reg.
38754038d79SJakob Stoklund Olesen for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) {
38854038d79SJakob Stoklund Olesen unsigned AliasReg = *AI;
389e056d107SDavid Goodwin if (State->IsLive(AliasReg)) {
390e056d107SDavid Goodwin State->UnionGroups(Reg, AliasReg);
391d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via "
392c71cced0SFrancis Visoiu Mistrih << printReg(AliasReg, TRI) << ")");
393de11f36aSDavid Goodwin }
394de11f36aSDavid Goodwin }
395de11f36aSDavid Goodwin
396de11f36aSDavid Goodwin // Note register reference...
397c0196b1bSCraig Topper const TargetRegisterClass *RC = nullptr;
3985e6e8c7aSDuncan P. N. Exon Smith if (i < MI.getDesc().getNumOperands())
3995e6e8c7aSDuncan P. N. Exon Smith RC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
400e056d107SDavid Goodwin AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
401de11f36aSDavid Goodwin RegRefs.insert(std::make_pair(Reg, RR));
402de11f36aSDavid Goodwin }
403de11f36aSDavid Goodwin
404d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << '\n');
4059f1b2d46SDavid Goodwin
4069f1b2d46SDavid Goodwin // Scan the register defs for this instruction and update
4079f1b2d46SDavid Goodwin // live-ranges.
408259cd6f8SKazu Hirata for (const MachineOperand &MO : MI.operands()) {
4099f1b2d46SDavid Goodwin if (!MO.isReg() || !MO.isDef()) continue;
4100c476111SDaniel Sanders Register Reg = MO.getReg();
4119f1b2d46SDavid Goodwin if (Reg == 0) continue;
412dd1c6198SDavid Goodwin // Ignore KILLs and passthru registers for liveness...
4135e6e8c7aSDuncan P. N. Exon Smith if (MI.isKill() || (PassthruRegs.count(Reg) != 0))
414dd1c6198SDavid Goodwin continue;
4159f1b2d46SDavid Goodwin
416dd1c6198SDavid Goodwin // Update def for Reg and aliases.
417121caf63SHal Finkel for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
418121caf63SHal Finkel // We need to be careful here not to define already-live super registers.
419121caf63SHal Finkel // If the super register is already live, then this definition is not
420121caf63SHal Finkel // a definition of the whole super register (just a partial insertion
421121caf63SHal Finkel // into it). Earlier subregister definitions (which we've not yet visited
422121caf63SHal Finkel // because we're iterating bottom-up) need to be linked to the same group
423121caf63SHal Finkel // as this definition.
424121caf63SHal Finkel if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI))
425121caf63SHal Finkel continue;
426121caf63SHal Finkel
42754038d79SJakob Stoklund Olesen DefIndices[*AI] = Count;
4289f1b2d46SDavid Goodwin }
4299f1b2d46SDavid Goodwin }
430121caf63SHal Finkel }
431de11f36aSDavid Goodwin
ScanInstruction(MachineInstr & MI,unsigned Count)4325e6e8c7aSDuncan P. N. Exon Smith void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr &MI,
433de11f36aSDavid Goodwin unsigned Count) {
434d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\tUse Groups:");
435e056d107SDavid Goodwin std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
436e056d107SDavid Goodwin RegRefs = State->GetRegRefs();
437de11f36aSDavid Goodwin
438f128bdcbSEvan Cheng // If MI's uses have special allocation requirement, don't allow
439f128bdcbSEvan Cheng // any use registers to be changed. Also assume all registers
440f128bdcbSEvan Cheng // used in a call must not be changed (ABI).
441cf6a8bfeSKyle Butt // Inline Assembly register uses also cannot be safely changed.
442f128bdcbSEvan Cheng // FIXME: The issue with predicated instruction is more complex. We are being
443f128bdcbSEvan Cheng // conservatively here because the kill markers cannot be trusted after
444f128bdcbSEvan Cheng // if-conversion:
4457d9bef8fSFrancis Visoiu Mistrih // %r6 = LDR %sp, %reg0, 92, 14, %reg0; mem:LD4[FixedStack14]
446f128bdcbSEvan Cheng // ...
4477d9bef8fSFrancis Visoiu Mistrih // STR %r0, killed %r6, %reg0, 0, 0, %cpsr; mem:ST4[%395]
4487d9bef8fSFrancis Visoiu Mistrih // %r6 = LDR %sp, %reg0, 100, 0, %cpsr; mem:LD4[FixedStack12]
4497d9bef8fSFrancis Visoiu Mistrih // STR %r0, killed %r6, %reg0, 0, 14, %reg0; mem:ST4[%396](align=8)
450f128bdcbSEvan Cheng //
451f128bdcbSEvan Cheng // The first R6 kill is not really a kill since it's killed by a predicated
452f128bdcbSEvan Cheng // instruction which may not be executed. The second R6 def may or may not
453f128bdcbSEvan Cheng // re-define R6 so it's not safe to change it since the last R6 use cannot be
454f128bdcbSEvan Cheng // changed.
4555e6e8c7aSDuncan P. N. Exon Smith bool Special = MI.isCall() || MI.hasExtraSrcRegAllocReq() ||
4565e6e8c7aSDuncan P. N. Exon Smith TII->isPredicated(MI) || MI.isInlineAsm();
457f128bdcbSEvan Cheng
458de11f36aSDavid Goodwin // Scan the register uses for this instruction and update
459de11f36aSDavid Goodwin // live-ranges, groups and RegRefs.
4605e6e8c7aSDuncan P. N. Exon Smith for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
4615e6e8c7aSDuncan P. N. Exon Smith MachineOperand &MO = MI.getOperand(i);
462de11f36aSDavid Goodwin if (!MO.isReg() || !MO.isUse()) continue;
4630c476111SDaniel Sanders Register Reg = MO.getReg();
464de11f36aSDavid Goodwin if (Reg == 0) continue;
465de11f36aSDavid Goodwin
466d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI) << "=g"
467d34e60caSNicola Zaghen << State->GetGroup(Reg));
468de11f36aSDavid Goodwin
469de11f36aSDavid Goodwin // It wasn't previously live but now it is, this is a kill. Forget
470de11f36aSDavid Goodwin // the previous live-range information and start a new live-range
471de11f36aSDavid Goodwin // for the register.
4729f1b2d46SDavid Goodwin HandleLastUse(Reg, Count, "(last-use)");
473de11f36aSDavid Goodwin
474f128bdcbSEvan Cheng if (Special) {
475d34e60caSNicola Zaghen LLVM_DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
476e056d107SDavid Goodwin State->UnionGroups(Reg, 0);
477de11f36aSDavid Goodwin }
478de11f36aSDavid Goodwin
479de11f36aSDavid Goodwin // Note register reference...
480c0196b1bSCraig Topper const TargetRegisterClass *RC = nullptr;
4815e6e8c7aSDuncan P. N. Exon Smith if (i < MI.getDesc().getNumOperands())
4825e6e8c7aSDuncan P. N. Exon Smith RC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
483e056d107SDavid Goodwin AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
484de11f36aSDavid Goodwin RegRefs.insert(std::make_pair(Reg, RR));
485de11f36aSDavid Goodwin }
486de11f36aSDavid Goodwin
487d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << '\n');
488de11f36aSDavid Goodwin
489de11f36aSDavid Goodwin // Form a group of all defs and uses of a KILL instruction to ensure
490de11f36aSDavid Goodwin // that all registers are renamed as a group.
4915e6e8c7aSDuncan P. N. Exon Smith if (MI.isKill()) {
492d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\tKill Group:");
493de11f36aSDavid Goodwin
494de11f36aSDavid Goodwin unsigned FirstReg = 0;
495259cd6f8SKazu Hirata for (const MachineOperand &MO : MI.operands()) {
496de11f36aSDavid Goodwin if (!MO.isReg()) continue;
4970c476111SDaniel Sanders Register Reg = MO.getReg();
498de11f36aSDavid Goodwin if (Reg == 0) continue;
499de11f36aSDavid Goodwin
500de11f36aSDavid Goodwin if (FirstReg != 0) {
501d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "=" << printReg(Reg, TRI));
502e056d107SDavid Goodwin State->UnionGroups(FirstReg, Reg);
503de11f36aSDavid Goodwin } else {
504d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI));
505de11f36aSDavid Goodwin FirstReg = Reg;
506de11f36aSDavid Goodwin }
507de11f36aSDavid Goodwin }
508de11f36aSDavid Goodwin
509d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n');
510de11f36aSDavid Goodwin }
511de11f36aSDavid Goodwin }
512de11f36aSDavid Goodwin
GetRenameRegisters(unsigned Reg)513de11f36aSDavid Goodwin BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) {
514de11f36aSDavid Goodwin BitVector BV(TRI->getNumRegs(), false);
515de11f36aSDavid Goodwin bool first = true;
516de11f36aSDavid Goodwin
517de11f36aSDavid Goodwin // Check all references that need rewriting for Reg. For each, use
518de11f36aSDavid Goodwin // the corresponding register class to narrow the set of registers
519de11f36aSDavid Goodwin // that are appropriate for renaming.
520c9436ad6SBenjamin Kramer for (const auto &Q : make_range(State->GetRegRefs().equal_range(Reg))) {
521c9436ad6SBenjamin Kramer const TargetRegisterClass *RC = Q.second.RC;
522c0196b1bSCraig Topper if (!RC) continue;
523de11f36aSDavid Goodwin
524de11f36aSDavid Goodwin BitVector RCBV = TRI->getAllocatableSet(MF, RC);
525de11f36aSDavid Goodwin if (first) {
526de11f36aSDavid Goodwin BV |= RCBV;
527de11f36aSDavid Goodwin first = false;
528de11f36aSDavid Goodwin } else {
529de11f36aSDavid Goodwin BV &= RCBV;
530de11f36aSDavid Goodwin }
531de11f36aSDavid Goodwin
532d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " " << TRI->getRegClassName(RC));
533de11f36aSDavid Goodwin }
534de11f36aSDavid Goodwin
535de11f36aSDavid Goodwin return BV;
536de11f36aSDavid Goodwin }
537de11f36aSDavid Goodwin
FindSuitableFreeRegisters(unsigned AntiDepGroupIndex,RenameOrderType & RenameOrder,std::map<unsigned,unsigned> & RenameMap)538de11f36aSDavid Goodwin bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
539de11f36aSDavid Goodwin unsigned AntiDepGroupIndex,
5407d8878adSDavid Goodwin RenameOrderType& RenameOrder,
541de11f36aSDavid Goodwin std::map<unsigned, unsigned> &RenameMap) {
542030b0286SBill Wendling std::vector<unsigned> &KillIndices = State->GetKillIndices();
543030b0286SBill Wendling std::vector<unsigned> &DefIndices = State->GetDefIndices();
544e056d107SDavid Goodwin std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
545e056d107SDavid Goodwin RegRefs = State->GetRegRefs();
546e056d107SDavid Goodwin
547b9fe5d5dSDavid Goodwin // Collect all referenced registers in the same group as
548b9fe5d5dSDavid Goodwin // AntiDepReg. These all need to be renamed together if we are to
549b9fe5d5dSDavid Goodwin // break the anti-dependence.
550de11f36aSDavid Goodwin std::vector<unsigned> Regs;
551b9fe5d5dSDavid Goodwin State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs);
5524f81cdd8SEugene Zelenko assert(!Regs.empty() && "Empty register group!");
5534f81cdd8SEugene Zelenko if (Regs.empty())
554de11f36aSDavid Goodwin return false;
555de11f36aSDavid Goodwin
556de11f36aSDavid Goodwin // Find the "superest" register in the group. At the same time,
557de11f36aSDavid Goodwin // collect the BitVector of registers that can be used to rename
558de11f36aSDavid Goodwin // each register.
559d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex
560eb431da0SJim Grosbach << ":\n");
561de11f36aSDavid Goodwin std::map<unsigned, BitVector> RenameRegisterMap;
562de11f36aSDavid Goodwin unsigned SuperReg = 0;
5633aed2822SKazu Hirata for (unsigned Reg : Regs) {
564de11f36aSDavid Goodwin if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg))
565de11f36aSDavid Goodwin SuperReg = Reg;
566de11f36aSDavid Goodwin
567de11f36aSDavid Goodwin // If Reg has any references, then collect possible rename regs
568de11f36aSDavid Goodwin if (RegRefs.count(Reg) > 0) {
569d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\t\t" << printReg(Reg, TRI) << ":");
570de11f36aSDavid Goodwin
5717f75e940SBenjamin Kramer BitVector &BV = RenameRegisterMap[Reg];
5727f75e940SBenjamin Kramer assert(BV.empty());
5737f75e940SBenjamin Kramer BV = GetRenameRegisters(Reg);
574de11f36aSDavid Goodwin
575d34e60caSNicola Zaghen LLVM_DEBUG({
5767f75e940SBenjamin Kramer dbgs() << " ::";
577b52e0366SFrancis Visoiu Mistrih for (unsigned r : BV.set_bits())
578c71cced0SFrancis Visoiu Mistrih dbgs() << " " << printReg(r, TRI);
5797f75e940SBenjamin Kramer dbgs() << "\n";
5807f75e940SBenjamin Kramer });
581de11f36aSDavid Goodwin }
582de11f36aSDavid Goodwin }
583de11f36aSDavid Goodwin
584de11f36aSDavid Goodwin // All group registers should be a subreg of SuperReg.
5853aed2822SKazu Hirata for (unsigned Reg : Regs) {
586de11f36aSDavid Goodwin if (Reg == SuperReg) continue;
587de11f36aSDavid Goodwin bool IsSub = TRI->isSubRegister(SuperReg, Reg);
58844ff8f06SWill Schmidt // FIXME: remove this once PR18663 has been properly fixed. For now,
58944ff8f06SWill Schmidt // return a conservative answer:
59044ff8f06SWill Schmidt // assert(IsSub && "Expecting group subregister");
591de11f36aSDavid Goodwin if (!IsSub)
592de11f36aSDavid Goodwin return false;
593de11f36aSDavid Goodwin }
594de11f36aSDavid Goodwin
595dd1c6198SDavid Goodwin #ifndef NDEBUG
596dd1c6198SDavid Goodwin // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod
597dd1c6198SDavid Goodwin if (DebugDiv > 0) {
598dd1c6198SDavid Goodwin static int renamecnt = 0;
599dd1c6198SDavid Goodwin if (renamecnt++ % DebugDiv != DebugMod)
600dd1c6198SDavid Goodwin return false;
601dd1c6198SDavid Goodwin
602c71cced0SFrancis Visoiu Mistrih dbgs() << "*** Performing rename " << printReg(SuperReg, TRI)
603c71cced0SFrancis Visoiu Mistrih << " for debug ***\n";
604dd1c6198SDavid Goodwin }
605dd1c6198SDavid Goodwin #endif
606dd1c6198SDavid Goodwin
6075305dc0bSDavid Goodwin // Check each possible rename register for SuperReg in round-robin
6085305dc0bSDavid Goodwin // order. If that register is available, and the corresponding
6095305dc0bSDavid Goodwin // registers are available for the other group subregisters, then we
6105305dc0bSDavid Goodwin // can use those registers to rename.
611871c7247SRafael Espindola
612871c7247SRafael Espindola // FIXME: Using getMinimalPhysRegClass is very conservative. We should
613871c7247SRafael Espindola // check every use of the register and find the largest register class
614871c7247SRafael Espindola // that can be used in all of them.
6155305dc0bSDavid Goodwin const TargetRegisterClass *SuperRC =
616871c7247SRafael Espindola TRI->getMinimalPhysRegClass(SuperReg, MVT::Other);
6175305dc0bSDavid Goodwin
618bdb55e0cSJakob Stoklund Olesen ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(SuperRC);
6194f5f84c7SJakob Stoklund Olesen if (Order.empty()) {
620d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\tEmpty Super Regclass!!\n");
6215305dc0bSDavid Goodwin return false;
6225305dc0bSDavid Goodwin }
6235305dc0bSDavid Goodwin
624d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\tFind Registers:");
6255305dc0bSDavid Goodwin
6264f5f84c7SJakob Stoklund Olesen RenameOrder.insert(RenameOrderType::value_type(SuperRC, Order.size()));
6277d8878adSDavid Goodwin
6284f5f84c7SJakob Stoklund Olesen unsigned OrigR = RenameOrder[SuperRC];
6294f5f84c7SJakob Stoklund Olesen unsigned EndR = ((OrigR == Order.size()) ? 0 : OrigR);
6304f5f84c7SJakob Stoklund Olesen unsigned R = OrigR;
6317d8878adSDavid Goodwin do {
6324f5f84c7SJakob Stoklund Olesen if (R == 0) R = Order.size();
6337d8878adSDavid Goodwin --R;
6344f5f84c7SJakob Stoklund Olesen const unsigned NewSuperReg = Order[R];
635944aece3SJim Grosbach // Don't consider non-allocatable registers
636f67bf3e0SJakob Stoklund Olesen if (!MRI.isAllocatable(NewSuperReg)) continue;
637de11f36aSDavid Goodwin // Don't replace a register with itself.
6385305dc0bSDavid Goodwin if (NewSuperReg == SuperReg) continue;
639de11f36aSDavid Goodwin
640d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " [" << printReg(NewSuperReg, TRI) << ':');
6415305dc0bSDavid Goodwin RenameMap.clear();
642de11f36aSDavid Goodwin
6435305dc0bSDavid Goodwin // For each referenced group register (which must be a SuperReg or
6445305dc0bSDavid Goodwin // a subregister of SuperReg), find the corresponding subregister
6455305dc0bSDavid Goodwin // of NewSuperReg and make sure it is free to be renamed.
6463aed2822SKazu Hirata for (unsigned Reg : Regs) {
6475305dc0bSDavid Goodwin unsigned NewReg = 0;
6485305dc0bSDavid Goodwin if (Reg == SuperReg) {
6495305dc0bSDavid Goodwin NewReg = NewSuperReg;
6505305dc0bSDavid Goodwin } else {
6515305dc0bSDavid Goodwin unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg);
6525305dc0bSDavid Goodwin if (NewSubRegIdx != 0)
6535305dc0bSDavid Goodwin NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx);
6545305dc0bSDavid Goodwin }
6555305dc0bSDavid Goodwin
656d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " " << printReg(NewReg, TRI));
6575305dc0bSDavid Goodwin
6585305dc0bSDavid Goodwin // Check if Reg can be renamed to NewReg.
6597f75e940SBenjamin Kramer if (!RenameRegisterMap[Reg].test(NewReg)) {
660d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "(no rename)");
6615305dc0bSDavid Goodwin goto next_super_reg;
6625305dc0bSDavid Goodwin }
6635305dc0bSDavid Goodwin
6645305dc0bSDavid Goodwin // If NewReg is dead and NewReg's most recent def is not before
6655305dc0bSDavid Goodwin // Regs's kill, it's safe to replace Reg with NewReg. We
6665305dc0bSDavid Goodwin // must also check all aliases of NewReg, because we can't define a
667dd1c6198SDavid Goodwin // register when any sub or super is already live.
6685305dc0bSDavid Goodwin if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) {
669d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "(live)");
6705305dc0bSDavid Goodwin goto next_super_reg;
671de11f36aSDavid Goodwin } else {
672de11f36aSDavid Goodwin bool found = false;
67354038d79SJakob Stoklund Olesen for (MCRegAliasIterator AI(NewReg, TRI, false); AI.isValid(); ++AI) {
67454038d79SJakob Stoklund Olesen unsigned AliasReg = *AI;
675eb431da0SJim Grosbach if (State->IsLive(AliasReg) ||
676eb431da0SJim Grosbach (KillIndices[Reg] > DefIndices[AliasReg])) {
677d34e60caSNicola Zaghen LLVM_DEBUG(dbgs()
678d34e60caSNicola Zaghen << "(alias " << printReg(AliasReg, TRI) << " live)");
679de11f36aSDavid Goodwin found = true;
680de11f36aSDavid Goodwin break;
681de11f36aSDavid Goodwin }
682de11f36aSDavid Goodwin }
683de11f36aSDavid Goodwin if (found)
6845305dc0bSDavid Goodwin goto next_super_reg;
685de11f36aSDavid Goodwin }
686de11f36aSDavid Goodwin
687c8cf2b88SHal Finkel // We cannot rename 'Reg' to 'NewReg' if one of the uses of 'Reg' also
688c8cf2b88SHal Finkel // defines 'NewReg' via an early-clobber operand.
689c9436ad6SBenjamin Kramer for (const auto &Q : make_range(RegRefs.equal_range(Reg))) {
690c9436ad6SBenjamin Kramer MachineInstr *UseMI = Q.second.Operand->getParent();
691c8cf2b88SHal Finkel int Idx = UseMI->findRegisterDefOperandIdx(NewReg, false, true, TRI);
692c8cf2b88SHal Finkel if (Idx == -1)
693c8cf2b88SHal Finkel continue;
694c8cf2b88SHal Finkel
695c8cf2b88SHal Finkel if (UseMI->getOperand(Idx).isEarlyClobber()) {
696d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "(ec)");
697c8cf2b88SHal Finkel goto next_super_reg;
698c8cf2b88SHal Finkel }
699c8cf2b88SHal Finkel }
700c8cf2b88SHal Finkel
701e0a28e54SHal Finkel // Also, we cannot rename 'Reg' to 'NewReg' if the instruction defining
702e0a28e54SHal Finkel // 'Reg' is an early-clobber define and that instruction also uses
703e0a28e54SHal Finkel // 'NewReg'.
704e0a28e54SHal Finkel for (const auto &Q : make_range(RegRefs.equal_range(Reg))) {
705e0a28e54SHal Finkel if (!Q.second.Operand->isDef() || !Q.second.Operand->isEarlyClobber())
706e0a28e54SHal Finkel continue;
707e0a28e54SHal Finkel
708e0a28e54SHal Finkel MachineInstr *DefMI = Q.second.Operand->getParent();
709e0a28e54SHal Finkel if (DefMI->readsRegister(NewReg, TRI)) {
710d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "(ec)");
711e0a28e54SHal Finkel goto next_super_reg;
712e0a28e54SHal Finkel }
713e0a28e54SHal Finkel }
714e0a28e54SHal Finkel
7155305dc0bSDavid Goodwin // Record that 'Reg' can be renamed to 'NewReg'.
7165305dc0bSDavid Goodwin RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg));
7175305dc0bSDavid Goodwin }
7185305dc0bSDavid Goodwin
7195305dc0bSDavid Goodwin // If we fall-out here, then every register in the group can be
7205305dc0bSDavid Goodwin // renamed, as recorded in RenameMap.
7217d8878adSDavid Goodwin RenameOrder.erase(SuperRC);
7227d8878adSDavid Goodwin RenameOrder.insert(RenameOrderType::value_type(SuperRC, R));
723d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "]\n");
724de11f36aSDavid Goodwin return true;
7255305dc0bSDavid Goodwin
7265305dc0bSDavid Goodwin next_super_reg:
727d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << ']');
7287d8878adSDavid Goodwin } while (R != EndR);
729de11f36aSDavid Goodwin
730d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << '\n');
731de11f36aSDavid Goodwin
732de11f36aSDavid Goodwin // No registers are free and available!
733de11f36aSDavid Goodwin return false;
734de11f36aSDavid Goodwin }
735de11f36aSDavid Goodwin
736de11f36aSDavid Goodwin /// BreakAntiDependencies - Identifiy anti-dependencies within the
737de11f36aSDavid Goodwin /// ScheduleDAG and break them by renaming registers.
BreakAntiDependencies(const std::vector<SUnit> & SUnits,MachineBasicBlock::iterator Begin,MachineBasicBlock::iterator End,unsigned InsertPosIndex,DbgValueVector & DbgValues)738e056d107SDavid Goodwin unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
73935bc4d46SDan Gohman const std::vector<SUnit> &SUnits,
74035bc4d46SDan Gohman MachineBasicBlock::iterator Begin,
74135bc4d46SDan Gohman MachineBasicBlock::iterator End,
742f02a376fSDevang Patel unsigned InsertPosIndex,
743f02a376fSDevang Patel DbgValueVector &DbgValues) {
744030b0286SBill Wendling std::vector<unsigned> &KillIndices = State->GetKillIndices();
745030b0286SBill Wendling std::vector<unsigned> &DefIndices = State->GetDefIndices();
746e056d107SDavid Goodwin std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
747e056d107SDavid Goodwin RegRefs = State->GetRegRefs();
748e056d107SDavid Goodwin
749de11f36aSDavid Goodwin // The code below assumes that there is at least one instruction,
750de11f36aSDavid Goodwin // so just duck out immediately if the block is empty.
7518501dbbeSDavid Goodwin if (SUnits.empty()) return 0;
752de11f36aSDavid Goodwin
7537d8878adSDavid Goodwin // For each regclass the next register to use for renaming.
7547d8878adSDavid Goodwin RenameOrderType RenameOrder;
7557d8878adSDavid Goodwin
756de11f36aSDavid Goodwin // ...need a map from MI to SUnit.
75735bc4d46SDan Gohman std::map<MachineInstr *, const SUnit *> MISUnitMap;
758f6bce30cSKazu Hirata for (const SUnit &SU : SUnits)
759f6bce30cSKazu Hirata MISUnitMap.insert(std::make_pair(SU.getInstr(), &SU));
760de11f36aSDavid Goodwin
761b9fe5d5dSDavid Goodwin // Track progress along the critical path through the SUnit graph as
762b9fe5d5dSDavid Goodwin // we walk the instructions. This is needed for regclasses that only
763b9fe5d5dSDavid Goodwin // break critical-path anti-dependencies.
764c0196b1bSCraig Topper const SUnit *CriticalPathSU = nullptr;
765c0196b1bSCraig Topper MachineInstr *CriticalPathMI = nullptr;
766b9fe5d5dSDavid Goodwin if (CriticalPathSet.any()) {
767f6bce30cSKazu Hirata for (const SUnit &SU : SUnits) {
768b9fe5d5dSDavid Goodwin if (!CriticalPathSU ||
769f6bce30cSKazu Hirata ((SU.getDepth() + SU.Latency) >
770b9fe5d5dSDavid Goodwin (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) {
771f6bce30cSKazu Hirata CriticalPathSU = &SU;
772b9fe5d5dSDavid Goodwin }
773b9fe5d5dSDavid Goodwin }
774be9beef5SSimon Pilgrim assert(CriticalPathSU && "Failed to find SUnit critical path");
775b9fe5d5dSDavid Goodwin CriticalPathMI = CriticalPathSU->getInstr();
776b9fe5d5dSDavid Goodwin }
777b9fe5d5dSDavid Goodwin
778de11f36aSDavid Goodwin #ifndef NDEBUG
779d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n");
780d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Available regs:");
781de11f36aSDavid Goodwin for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
782e056d107SDavid Goodwin if (!State->IsLive(Reg))
783d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI));
784de11f36aSDavid Goodwin }
785d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << '\n');
786de11f36aSDavid Goodwin #endif
787de11f36aSDavid Goodwin
788143f684aSKrzysztof Parzyszek BitVector RegAliases(TRI->getNumRegs());
789143f684aSKrzysztof Parzyszek
790de11f36aSDavid Goodwin // Attempt to break anti-dependence edges. Walk the instructions
791de11f36aSDavid Goodwin // from the bottom up, tracking information about liveness as we go
792de11f36aSDavid Goodwin // to help determine which registers are available.
793de11f36aSDavid Goodwin unsigned Broken = 0;
794de11f36aSDavid Goodwin unsigned Count = InsertPosIndex - 1;
795de11f36aSDavid Goodwin for (MachineBasicBlock::iterator I = End, E = Begin;
796de11f36aSDavid Goodwin I != E; --Count) {
7975e6e8c7aSDuncan P. N. Exon Smith MachineInstr &MI = *--I;
798de11f36aSDavid Goodwin
799801bf7ebSShiva Chen if (MI.isDebugInstr())
8008606e3c7SHal Finkel continue;
8018606e3c7SHal Finkel
802d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Anti: ");
803d34e60caSNicola Zaghen LLVM_DEBUG(MI.dump());
804de11f36aSDavid Goodwin
805de11f36aSDavid Goodwin std::set<unsigned> PassthruRegs;
806de11f36aSDavid Goodwin GetPassthruRegs(MI, PassthruRegs);
807de11f36aSDavid Goodwin
808de11f36aSDavid Goodwin // Process the defs in MI...
809de11f36aSDavid Goodwin PrescanInstruction(MI, Count, PassthruRegs);
810de11f36aSDavid Goodwin
81180a03cc0SDavid Goodwin // The dependence edges that represent anti- and output-
812b9fe5d5dSDavid Goodwin // dependencies that are candidates for breaking.
81335bc4d46SDan Gohman std::vector<const SDep *> Edges;
8145e6e8c7aSDuncan P. N. Exon Smith const SUnit *PathSU = MISUnitMap[&MI];
81580a03cc0SDavid Goodwin AntiDepEdges(PathSU, Edges);
816b9fe5d5dSDavid Goodwin
817b9fe5d5dSDavid Goodwin // If MI is not on the critical path, then we don't rename
818b9fe5d5dSDavid Goodwin // registers in the CriticalPathSet.
819c0196b1bSCraig Topper BitVector *ExcludeRegs = nullptr;
8205e6e8c7aSDuncan P. N. Exon Smith if (&MI == CriticalPathMI) {
821b9fe5d5dSDavid Goodwin CriticalPathSU = CriticalPathStep(CriticalPathSU);
822c0196b1bSCraig Topper CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : nullptr;
8236f1ff8e1SHal Finkel } else if (CriticalPathSet.any()) {
824b9fe5d5dSDavid Goodwin ExcludeRegs = &CriticalPathSet;
825b9fe5d5dSDavid Goodwin }
826de11f36aSDavid Goodwin
827de11f36aSDavid Goodwin // Ignore KILL instructions (they form a group in ScanInstruction
828de11f36aSDavid Goodwin // but don't cause any anti-dependence breaking themselves)
8295e6e8c7aSDuncan P. N. Exon Smith if (!MI.isKill()) {
830de11f36aSDavid Goodwin // Attempt to break each anti-dependency...
831f6bce30cSKazu Hirata for (const SDep *Edge : Edges) {
832de11f36aSDavid Goodwin SUnit *NextSU = Edge->getSUnit();
833de11f36aSDavid Goodwin
834da83f7d5SDavid Goodwin if ((Edge->getKind() != SDep::Anti) &&
835da83f7d5SDavid Goodwin (Edge->getKind() != SDep::Output)) continue;
836de11f36aSDavid Goodwin
837de11f36aSDavid Goodwin unsigned AntiDepReg = Edge->getReg();
838d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\tAntidep reg: " << printReg(AntiDepReg, TRI));
839de11f36aSDavid Goodwin assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
840de11f36aSDavid Goodwin
841f67bf3e0SJakob Stoklund Olesen if (!MRI.isAllocatable(AntiDepReg)) {
842de11f36aSDavid Goodwin // Don't break anti-dependencies on non-allocatable registers.
843d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " (non-allocatable)\n");
844de11f36aSDavid Goodwin continue;
845c0196b1bSCraig Topper } else if (ExcludeRegs && ExcludeRegs->test(AntiDepReg)) {
846b9fe5d5dSDavid Goodwin // Don't break anti-dependencies for critical path registers
847b9fe5d5dSDavid Goodwin // if not on the critical path
848d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " (not critical-path)\n");
849b9fe5d5dSDavid Goodwin continue;
850de11f36aSDavid Goodwin } else if (PassthruRegs.count(AntiDepReg) != 0) {
851de11f36aSDavid Goodwin // If the anti-dep register liveness "passes-thru", then
852de11f36aSDavid Goodwin // don't try to change it. It will be changed along with
853de11f36aSDavid Goodwin // the use if required to break an earlier antidep.
854d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " (passthru)\n");
855de11f36aSDavid Goodwin continue;
856de11f36aSDavid Goodwin } else {
857de11f36aSDavid Goodwin // No anti-dep breaking for implicit deps
8585e6e8c7aSDuncan P. N. Exon Smith MachineOperand *AntiDepOp = MI.findRegisterDefOperand(AntiDepReg);
859c0196b1bSCraig Topper assert(AntiDepOp && "Can't find index for defined register operand");
860c0196b1bSCraig Topper if (!AntiDepOp || AntiDepOp->isImplicit()) {
861d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " (implicit)\n");
862de11f36aSDavid Goodwin continue;
863de11f36aSDavid Goodwin }
864de11f36aSDavid Goodwin
865de11f36aSDavid Goodwin // If the SUnit has other dependencies on the SUnit that
866de11f36aSDavid Goodwin // it anti-depends on, don't bother breaking the
867de11f36aSDavid Goodwin // anti-dependency since those edges would prevent such
868de11f36aSDavid Goodwin // units from being scheduled past each other
869de11f36aSDavid Goodwin // regardless.
87080a03cc0SDavid Goodwin //
87180a03cc0SDavid Goodwin // Also, if there are dependencies on other SUnits with the
87280a03cc0SDavid Goodwin // same register as the anti-dependency, don't attempt to
87380a03cc0SDavid Goodwin // break it.
874d61b4cb9SKazu Hirata for (const SDep &Pred : PathSU->Preds) {
875d61b4cb9SKazu Hirata if (Pred.getSUnit() == NextSU ? (Pred.getKind() != SDep::Anti ||
876d61b4cb9SKazu Hirata Pred.getReg() != AntiDepReg)
877d61b4cb9SKazu Hirata : (Pred.getKind() == SDep::Data &&
878d61b4cb9SKazu Hirata Pred.getReg() == AntiDepReg)) {
87980a03cc0SDavid Goodwin AntiDepReg = 0;
88080a03cc0SDavid Goodwin break;
88180a03cc0SDavid Goodwin }
88280a03cc0SDavid Goodwin }
883d61b4cb9SKazu Hirata for (const SDep &Pred : PathSU->Preds) {
884d61b4cb9SKazu Hirata if ((Pred.getSUnit() == NextSU) && (Pred.getKind() != SDep::Anti) &&
885d61b4cb9SKazu Hirata (Pred.getKind() != SDep::Output)) {
886d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " (real dependency)\n");
887de11f36aSDavid Goodwin AntiDepReg = 0;
888de11f36aSDavid Goodwin break;
889d61b4cb9SKazu Hirata } else if ((Pred.getSUnit() != NextSU) &&
890d61b4cb9SKazu Hirata (Pred.getKind() == SDep::Data) &&
891d61b4cb9SKazu Hirata (Pred.getReg() == AntiDepReg)) {
892d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " (other dependency)\n");
89380a03cc0SDavid Goodwin AntiDepReg = 0;
89480a03cc0SDavid Goodwin break;
895de11f36aSDavid Goodwin }
896de11f36aSDavid Goodwin }
897de11f36aSDavid Goodwin
898de11f36aSDavid Goodwin if (AntiDepReg == 0) continue;
899143f684aSKrzysztof Parzyszek
900143f684aSKrzysztof Parzyszek // If the definition of the anti-dependency register does not start
901143f684aSKrzysztof Parzyszek // a new live range, bail out. This can happen if the anti-dep
902143f684aSKrzysztof Parzyszek // register is a sub-register of another register whose live range
903143f684aSKrzysztof Parzyszek // spans over PathSU. In such case, PathSU defines only a part of
904143f684aSKrzysztof Parzyszek // the larger register.
905143f684aSKrzysztof Parzyszek RegAliases.reset();
906143f684aSKrzysztof Parzyszek for (MCRegAliasIterator AI(AntiDepReg, TRI, true); AI.isValid(); ++AI)
907143f684aSKrzysztof Parzyszek RegAliases.set(*AI);
908143f684aSKrzysztof Parzyszek for (SDep S : PathSU->Succs) {
909143f684aSKrzysztof Parzyszek SDep::Kind K = S.getKind();
910143f684aSKrzysztof Parzyszek if (K != SDep::Data && K != SDep::Output && K != SDep::Anti)
911143f684aSKrzysztof Parzyszek continue;
912143f684aSKrzysztof Parzyszek unsigned R = S.getReg();
913143f684aSKrzysztof Parzyszek if (!RegAliases[R])
914143f684aSKrzysztof Parzyszek continue;
915143f684aSKrzysztof Parzyszek if (R == AntiDepReg || TRI->isSubRegister(AntiDepReg, R))
916143f684aSKrzysztof Parzyszek continue;
917143f684aSKrzysztof Parzyszek AntiDepReg = 0;
918143f684aSKrzysztof Parzyszek break;
919143f684aSKrzysztof Parzyszek }
920143f684aSKrzysztof Parzyszek
921143f684aSKrzysztof Parzyszek if (AntiDepReg == 0) continue;
922de11f36aSDavid Goodwin }
923de11f36aSDavid Goodwin
924de11f36aSDavid Goodwin assert(AntiDepReg != 0);
925de11f36aSDavid Goodwin if (AntiDepReg == 0) continue;
926de11f36aSDavid Goodwin
927de11f36aSDavid Goodwin // Determine AntiDepReg's register group.
928e056d107SDavid Goodwin const unsigned GroupIndex = State->GetGroup(AntiDepReg);
929de11f36aSDavid Goodwin if (GroupIndex == 0) {
930d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " (zero group)\n");
931de11f36aSDavid Goodwin continue;
932de11f36aSDavid Goodwin }
933de11f36aSDavid Goodwin
934d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << '\n');
935de11f36aSDavid Goodwin
936de11f36aSDavid Goodwin // Look for a suitable register to use to break the anti-dependence.
937de11f36aSDavid Goodwin std::map<unsigned, unsigned> RenameMap;
9387d8878adSDavid Goodwin if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) {
939d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "\tBreaking anti-dependence edge on "
940c71cced0SFrancis Visoiu Mistrih << printReg(AntiDepReg, TRI) << ":");
941de11f36aSDavid Goodwin
942de11f36aSDavid Goodwin // Handle each group register...
94322f00f61SKazu Hirata for (const auto &P : RenameMap) {
94422f00f61SKazu Hirata unsigned CurrReg = P.first;
94522f00f61SKazu Hirata unsigned NewReg = P.second;
946de11f36aSDavid Goodwin
947d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " " << printReg(CurrReg, TRI) << "->"
948c71cced0SFrancis Visoiu Mistrih << printReg(NewReg, TRI) << "("
949c71cced0SFrancis Visoiu Mistrih << RegRefs.count(CurrReg) << " refs)");
950de11f36aSDavid Goodwin
951de11f36aSDavid Goodwin // Update the references to the old register CurrReg to
952de11f36aSDavid Goodwin // refer to the new register NewReg.
953c9436ad6SBenjamin Kramer for (const auto &Q : make_range(RegRefs.equal_range(CurrReg))) {
954c9436ad6SBenjamin Kramer Q.second.Operand->setReg(NewReg);
95512ac8f03SJim Grosbach // If the SU for the instruction being updated has debug
95612ac8f03SJim Grosbach // information related to the anti-dependency register, make
95712ac8f03SJim Grosbach // sure to update that as well.
958c9436ad6SBenjamin Kramer const SUnit *SU = MISUnitMap[Q.second.Operand->getParent()];
95984854830SJim Grosbach if (!SU) continue;
96010ebfe06SAndrew Ng UpdateDbgValues(DbgValues, Q.second.Operand->getParent(),
96110ebfe06SAndrew Ng AntiDepReg, NewReg);
962de11f36aSDavid Goodwin }
963de11f36aSDavid Goodwin
964de11f36aSDavid Goodwin // We just went back in time and modified history; the
965de11f36aSDavid Goodwin // liveness information for CurrReg is now inconsistent. Set
966de11f36aSDavid Goodwin // the state as if it were dead.
967e056d107SDavid Goodwin State->UnionGroups(NewReg, 0);
968de11f36aSDavid Goodwin RegRefs.erase(NewReg);
969de11f36aSDavid Goodwin DefIndices[NewReg] = DefIndices[CurrReg];
970de11f36aSDavid Goodwin KillIndices[NewReg] = KillIndices[CurrReg];
971de11f36aSDavid Goodwin
972e056d107SDavid Goodwin State->UnionGroups(CurrReg, 0);
973de11f36aSDavid Goodwin RegRefs.erase(CurrReg);
974de11f36aSDavid Goodwin DefIndices[CurrReg] = KillIndices[CurrReg];
975de11f36aSDavid Goodwin KillIndices[CurrReg] = ~0u;
976de11f36aSDavid Goodwin assert(((KillIndices[CurrReg] == ~0u) !=
977de11f36aSDavid Goodwin (DefIndices[CurrReg] == ~0u)) &&
978de11f36aSDavid Goodwin "Kill and Def maps aren't consistent for AntiDepReg!");
979de11f36aSDavid Goodwin }
980de11f36aSDavid Goodwin
981de11f36aSDavid Goodwin ++Broken;
982d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << '\n');
983de11f36aSDavid Goodwin }
984de11f36aSDavid Goodwin }
985de11f36aSDavid Goodwin }
986de11f36aSDavid Goodwin
987de11f36aSDavid Goodwin ScanInstruction(MI, Count);
988de11f36aSDavid Goodwin }
989de11f36aSDavid Goodwin
990de11f36aSDavid Goodwin return Broken;
991de11f36aSDavid Goodwin }
992c228c717SThomas Raoux
createAggressiveAntiDepBreaker(MachineFunction & MFi,const RegisterClassInfo & RCI,TargetSubtargetInfo::RegClassVector & CriticalPathRCs)993c228c717SThomas Raoux AntiDepBreaker *llvm::createAggressiveAntiDepBreaker(
994c228c717SThomas Raoux MachineFunction &MFi, const RegisterClassInfo &RCI,
995c228c717SThomas Raoux TargetSubtargetInfo::RegClassVector &CriticalPathRCs) {
996c228c717SThomas Raoux return new AggressiveAntiDepBreaker(MFi, RCI, CriticalPathRCs);
997c228c717SThomas Raoux }
998