LLVM 23.0.0git
AggressiveAntiDepBreaker.cpp
Go to the documentation of this file.
1//===- AggressiveAntiDepBreaker.cpp - Anti-dep breaker --------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the AggressiveAntiDepBreaker class, which
10// implements register anti-dependence breaking during post-RA
11// scheduling. It attempts to break all anti-dependencies within a
12// block.
13//
14//===----------------------------------------------------------------------===//
15
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/SmallSet.h"
31#include "llvm/MC/MCInstrDesc.h"
34#include "llvm/Support/Debug.h"
36#include <cassert>
37
38using namespace llvm;
39
40#define DEBUG_TYPE "post-RA-sched"
41
42// If DebugDiv > 0 then only break antidep with (ID % DebugDiv) == DebugMod
43static cl::opt<int>
44DebugDiv("agg-antidep-debugdiv",
45 cl::desc("Debug control for aggressive anti-dep breaker"),
47
48static cl::opt<int>
49DebugMod("agg-antidep-debugmod",
50 cl::desc("Debug control for aggressive anti-dep breaker"),
52
55 : NumTargetRegs(TargetRegs), GroupNodes(TargetRegs, 0),
56 GroupNodeIndices(TargetRegs, 0), KillIndices(TargetRegs, 0),
57 DefIndices(TargetRegs, 0) {
58 const unsigned BBSize = BB->size();
59 for (unsigned i = 0; i < NumTargetRegs; ++i) {
60 // Initialize all registers to be in their own group. Initially we
61 // assign the register to the same-indexed GroupNode.
62 GroupNodeIndices[i] = i;
63 // Initialize the indices to indicate that no registers are live.
64 KillIndices[i] = ~0u;
65 DefIndices[i] = BBSize;
66 }
67}
68
70 unsigned Node = GroupNodeIndices[Reg.id()];
71 while (GroupNodes[Node] != Node)
72 Node = GroupNodes[Node];
73
74 return Node;
75}
76
78 unsigned Group, std::vector<MCRegister> &Regs,
79 std::multimap<MCRegister, AggressiveAntiDepState::RegisterReference>
80 *RegRefs) {
81 for (unsigned Reg = 0; Reg != NumTargetRegs; ++Reg) {
82 if ((GetGroup(Reg) == Group) && (RegRefs->count(Reg) > 0))
83 Regs.push_back(Reg);
84 }
85}
86
88 assert(GroupNodes[0] == 0 && "GroupNode 0 not parent!");
89 assert(GroupNodeIndices[0] == 0 && "Reg 0 not in Group 0!");
90
91 // find group for each register
92 unsigned Group1 = GetGroup(Reg1);
93 unsigned Group2 = GetGroup(Reg2);
94
95 // if either group is 0, then that must become the parent
96 unsigned Parent = (Group1 == 0) ? Group1 : Group2;
97 unsigned Other = (Parent == Group1) ? Group2 : Group1;
98 GroupNodes.at(Other) = Parent;
99 return Parent;
100}
101
103 // Create a new GroupNode for Reg. Reg's existing GroupNode must
104 // stay as is because there could be other GroupNodes referring to
105 // it.
106 unsigned idx = GroupNodes.size();
107 GroupNodes.push_back(idx);
108 GroupNodeIndices[Reg.id()] = idx;
109 return idx;
110}
111
113 // KillIndex must be defined and DefIndex not defined for a register
114 // to be live.
115 return ((KillIndices[Reg.id()] != ~0u) && (DefIndices[Reg.id()] == ~0u));
116}
117
119 MachineFunction &MFi, const RegisterClassInfo &RCI,
121 : MF(MFi), MRI(MF.getRegInfo()), TII(MF.getSubtarget().getInstrInfo()),
122 TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RCI) {
123 /* Collect a bitset of all registers that are only broken if they
124 are on the critical path. */
125 for (const TargetRegisterClass *RC : CriticalPathRCs) {
126 BitVector CPSet = TRI->getAllocatableSet(MF, RC);
127 if (CriticalPathSet.none())
128 CriticalPathSet = CPSet;
129 else
130 CriticalPathSet |= CPSet;
131 }
132
133 LLVM_DEBUG(dbgs() << "AntiDep Critical-Path Registers:");
134 LLVM_DEBUG(for (unsigned r
135 : CriticalPathSet.set_bits()) dbgs()
136 << " " << printReg(r, TRI));
137 LLVM_DEBUG(dbgs() << '\n');
138}
139
143
145 assert(!State);
146 State = new AggressiveAntiDepState(TRI->getNumRegs(), BB);
147
148 bool IsReturnBlock = BB->isReturnBlock();
149 std::vector<unsigned> &KillIndices = State->GetKillIndices();
150 std::vector<unsigned> &DefIndices = State->GetDefIndices();
151
152 // Examine the live-in regs of all successors.
153 for (MachineBasicBlock *Succ : BB->successors())
154 for (const auto &LI : Succ->liveins()) {
155 for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI) {
156 MCRegister Reg = *AI;
157 State->UnionGroups(Reg, 0);
158 KillIndices[Reg.id()] = BB->size();
159 DefIndices[Reg.id()] = ~0u;
160 }
161 }
162
163 // Mark live-out callee-saved registers. In a return block this is
164 // all callee-saved registers. In non-return this is any
165 // callee-saved register that is not saved in the prolog.
166 const MachineFrameInfo &MFI = MF.getFrameInfo();
167 BitVector Pristine = MFI.getPristineRegs(MF);
168 for (const MCPhysReg *I = MF.getRegInfo().getCalleeSavedRegs(); *I;
169 ++I) {
170 unsigned Reg = *I;
171 if (!IsReturnBlock && !Pristine.test(Reg))
172 continue;
173 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
174 MCRegister AliasReg = *AI;
175 State->UnionGroups(AliasReg, 0);
176 KillIndices[AliasReg.id()] = BB->size();
177 DefIndices[AliasReg.id()] = ~0u;
178 }
179 }
180}
181
183 delete State;
184 State = nullptr;
185}
186
188 unsigned InsertPosIndex) {
189 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
190
191 std::set<MCRegister> PassthruRegs;
192 GetPassthruRegs(MI, PassthruRegs);
193 PrescanInstruction(MI, Count, PassthruRegs);
194 ScanInstruction(MI, Count);
195
196 LLVM_DEBUG(dbgs() << "Observe: ");
197 LLVM_DEBUG(MI.dump());
198 LLVM_DEBUG(dbgs() << "\tRegs:");
199
200 std::vector<unsigned> &DefIndices = State->GetDefIndices();
201 for (unsigned Reg = 1; Reg != TRI->getNumRegs(); ++Reg) {
202 // If Reg is current live, then mark that it can't be renamed as
203 // we don't know the extent of its live-range anymore (now that it
204 // has been scheduled). If it is not live but was defined in the
205 // previous schedule region, then set its def index to the most
206 // conservative location (i.e. the beginning of the previous
207 // schedule region).
208 if (State->IsLive(Reg)) {
209 LLVM_DEBUG(if (State->GetGroup(Reg) != 0) dbgs()
210 << " " << printReg(Reg, TRI) << "=g" << State->GetGroup(Reg)
211 << "->g0(region live-out)");
212 State->UnionGroups(Reg, 0);
213 } else if ((DefIndices[Reg] < InsertPosIndex)
214 && (DefIndices[Reg] >= Count)) {
215 DefIndices[Reg] = Count;
216 }
217 }
218 LLVM_DEBUG(dbgs() << '\n');
219}
220
221bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr &MI,
222 MachineOperand &MO) {
223 if (!MO.isReg() || !MO.isImplicit())
224 return false;
225
226 Register Reg = MO.getReg();
227 if (Reg == 0)
228 return false;
229
230 MachineOperand *Op = nullptr;
231 if (MO.isDef())
232 Op = MI.findRegisterUseOperand(Reg, /*TRI=*/nullptr, true);
233 else
234 Op = MI.findRegisterDefOperand(Reg, /*TRI=*/nullptr);
235
236 return(Op && Op->isImplicit());
237}
238
239void AggressiveAntiDepBreaker::GetPassthruRegs(
240 MachineInstr &MI, std::set<MCRegister> &PassthruRegs) {
241 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
242 MachineOperand &MO = MI.getOperand(i);
243 if (!MO.isReg()) continue;
244 if ((MO.isDef() && MI.isRegTiedToUseOperand(i)) ||
245 IsImplicitDefUse(MI, MO)) {
246 const Register Reg = MO.getReg();
247 for (MCPhysReg SubReg : TRI->subregs_inclusive(Reg))
248 PassthruRegs.insert(SubReg);
249 }
250 }
251}
252
253/// AntiDepEdges - Return in Edges the anti- and output- dependencies
254/// in SU that we want to consider for breaking.
255static void AntiDepEdges(const SUnit *SU, std::vector<const SDep *> &Edges) {
257 for (const SDep &Pred : SU->Preds) {
258 if ((Pred.getKind() == SDep::Anti) || (Pred.getKind() == SDep::Output)) {
259 if (RegSet.insert(Pred.getReg()).second)
260 Edges.push_back(&Pred);
261 }
262 }
263}
264
265/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
266/// critical path.
267static const SUnit *CriticalPathStep(const SUnit *SU) {
268 const SDep *Next = nullptr;
269 unsigned NextDepth = 0;
270 // Find the predecessor edge with the greatest depth.
271 if (SU) {
272 for (const SDep &Pred : SU->Preds) {
273 const SUnit *PredSU = Pred.getSUnit();
274 unsigned PredLatency = Pred.getLatency();
275 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
276 // In the case of a latency tie, prefer an anti-dependency edge over
277 // other types of edges.
278 if (NextDepth < PredTotalLatency ||
279 (NextDepth == PredTotalLatency && Pred.getKind() == SDep::Anti)) {
280 NextDepth = PredTotalLatency;
281 Next = &Pred;
282 }
283 }
284 }
285
286 return (Next) ? Next->getSUnit() : nullptr;
287}
288
289void AggressiveAntiDepBreaker::HandleLastUse(MCRegister Reg, unsigned KillIdx,
290 const char *tag,
291 const char *header,
292 const char *footer) {
293 std::vector<unsigned> &KillIndices = State->GetKillIndices();
294 std::vector<unsigned> &DefIndices = State->GetDefIndices();
295 std::multimap<MCRegister, AggressiveAntiDepState::RegisterReference>
296 &RegRefs = State->GetRegRefs();
297
298 // FIXME: We must leave subregisters of live super registers as live, so that
299 // we don't clear out the register tracking information for subregisters of
300 // super registers we're still tracking (and with which we're unioning
301 // subregister definitions).
302 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
303 if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI)) {
304 LLVM_DEBUG(if (!header && footer) dbgs() << footer);
305 return;
306 }
307
308 if (!State->IsLive(Reg)) {
309 KillIndices[Reg.id()] = KillIdx;
310 DefIndices[Reg.id()] = ~0u;
311 RegRefs.erase(Reg);
312 State->LeaveGroup(Reg);
313 LLVM_DEBUG(if (header) {
314 dbgs() << header << printReg(Reg, TRI);
315 header = nullptr;
316 });
317 LLVM_DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag);
318 // Repeat for subregisters. Note that we only do this if the superregister
319 // was not live because otherwise, regardless whether we have an explicit
320 // use of the subregister, the subregister's contents are needed for the
321 // uses of the superregister.
322 for (MCPhysReg SubregReg : TRI->subregs(Reg)) {
323 if (!State->IsLive(SubregReg)) {
324 KillIndices[SubregReg] = KillIdx;
325 DefIndices[SubregReg] = ~0u;
326 RegRefs.erase(SubregReg);
327 State->LeaveGroup(SubregReg);
328 LLVM_DEBUG(if (header) {
329 dbgs() << header << printReg(Reg, TRI);
330 header = nullptr;
331 });
332 LLVM_DEBUG(dbgs() << " " << printReg(SubregReg, TRI) << "->g"
333 << State->GetGroup(SubregReg) << tag);
334 }
335 }
336 }
337
338 LLVM_DEBUG(if (!header && footer) dbgs() << footer);
339}
340
341void AggressiveAntiDepBreaker::PrescanInstruction(
342 MachineInstr &MI, unsigned Count,
343 const std::set<MCRegister> &PassthruRegs) {
344 std::vector<unsigned> &DefIndices = State->GetDefIndices();
345 std::multimap<MCRegister, AggressiveAntiDepState::RegisterReference>
346 &RegRefs = State->GetRegRefs();
347
348 // Handle dead defs by simulating a last-use of the register just
349 // after the def. A dead def can occur because the def is truly
350 // dead, or because only a subregister is live at the def. If we
351 // don't do this the dead def will be incorrectly merged into the
352 // previous def.
353 for (const MachineOperand &MO : MI.all_defs()) {
354 Register Reg = MO.getReg();
355 if (!Reg)
356 continue;
357
358 HandleLastUse(Reg.asMCReg(), Count + 1, "", "\tDead Def: ", "\n");
359 }
360
361 LLVM_DEBUG(dbgs() << "\tDef Groups:");
362 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
363 MachineOperand &MO = MI.getOperand(i);
364 if (!MO.isReg() || !MO.isDef()) continue;
365 Register Reg = MO.getReg();
366 if (!Reg)
367 continue;
368
369 LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI) << "=g"
370 << State->GetGroup(Reg));
371
372 // If MI's defs have a special allocation requirement, don't allow
373 // any def registers to be changed. Also assume all registers
374 // defined in a call must not be changed (ABI). Inline assembly may
375 // reference either system calls or the register directly. Skip it until we
376 // can tell user specified registers from compiler-specified.
377 if (MI.isCall() || MI.hasExtraDefRegAllocReq() || TII->isPredicated(MI) ||
378 MI.isInlineAsm()) {
379 LLVM_DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
380 State->UnionGroups(Reg, 0);
381 }
382
383 // Any aliased that are live at this point are completely or
384 // partially defined here, so group those aliases with Reg.
385 for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) {
386 MCRegister AliasReg = *AI;
387 if (State->IsLive(AliasReg)) {
388 State->UnionGroups(Reg, AliasReg);
389 LLVM_DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via "
390 << printReg(AliasReg, TRI) << ")");
391 }
392 }
393
394 // Note register reference...
395 const TargetRegisterClass *RC = nullptr;
396 if (i < MI.getDesc().getNumOperands())
397 RC = TII->getRegClass(MI.getDesc(), i);
398 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
399 RegRefs.emplace(Reg.asMCReg(), RR);
400 }
401
402 LLVM_DEBUG(dbgs() << '\n');
403
404 // Scan the register defs for this instruction and update
405 // live-ranges.
406 for (const MachineOperand &MO : MI.all_defs()) {
407 Register Reg = MO.getReg();
408 if (!Reg)
409 continue;
410 // Ignore KILLs and passthru registers for liveness...
411 if (MI.isKill() || (PassthruRegs.count(Reg) != 0))
412 continue;
413
414 // Update def for Reg and aliases.
415 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
416 // We need to be careful here not to define already-live super registers.
417 // If the super register is already live, then this definition is not
418 // a definition of the whole super register (just a partial insertion
419 // into it). Earlier subregister definitions (which we've not yet visited
420 // because we're iterating bottom-up) need to be linked to the same group
421 // as this definition.
422 if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI))
423 continue;
424
425 DefIndices[(*AI).id()] = Count;
426 }
427 }
428}
429
430void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr &MI,
431 unsigned Count) {
432 LLVM_DEBUG(dbgs() << "\tUse Groups:");
433 std::multimap<MCRegister, AggressiveAntiDepState::RegisterReference>
434 &RegRefs = State->GetRegRefs();
435
436 // If MI's uses have special allocation requirement, don't allow
437 // any use registers to be changed. Also assume all registers
438 // used in a call must not be changed (ABI).
439 // Inline Assembly register uses also cannot be safely changed.
440 // FIXME: The issue with predicated instruction is more complex. We are being
441 // conservatively here because the kill markers cannot be trusted after
442 // if-conversion:
443 // %r6 = LDR %sp, %reg0, 92, 14, %reg0; mem:LD4[FixedStack14]
444 // ...
445 // STR %r0, killed %r6, %reg0, 0, 0, %cpsr; mem:ST4[%395]
446 // %r6 = LDR %sp, %reg0, 100, 0, %cpsr; mem:LD4[FixedStack12]
447 // STR %r0, killed %r6, %reg0, 0, 14, %reg0; mem:ST4[%396](align=8)
448 //
449 // The first R6 kill is not really a kill since it's killed by a predicated
450 // instruction which may not be executed. The second R6 def may or may not
451 // re-define R6 so it's not safe to change it since the last R6 use cannot be
452 // changed.
453 bool Special = MI.isCall() || MI.hasExtraSrcRegAllocReq() ||
454 TII->isPredicated(MI) || MI.isInlineAsm();
455
456 // Scan the register uses for this instruction and update
457 // live-ranges, groups and RegRefs.
458 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
459 MachineOperand &MO = MI.getOperand(i);
460 if (!MO.isReg() || !MO.isUse()) continue;
461 Register Reg = MO.getReg();
462 if (!Reg)
463 continue;
464
465 LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI) << "=g"
466 << State->GetGroup(Reg));
467
468 // It wasn't previously live but now it is, this is a kill. Forget
469 // the previous live-range information and start a new live-range
470 // for the register.
471 HandleLastUse(Reg.asMCReg(), Count, "(last-use)");
472
473 if (Special) {
474 LLVM_DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
475 State->UnionGroups(Reg, 0);
476 }
477
478 // Note register reference...
479 const TargetRegisterClass *RC = nullptr;
480 if (i < MI.getDesc().getNumOperands())
481 RC = TII->getRegClass(MI.getDesc(), i);
482 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
483 RegRefs.emplace(Reg.asMCReg(), RR);
484 }
485
486 LLVM_DEBUG(dbgs() << '\n');
487
488 // Form a group of all defs and uses of a KILL instruction to ensure
489 // that all registers are renamed as a group.
490 if (MI.isKill()) {
491 LLVM_DEBUG(dbgs() << "\tKill Group:");
492
493 Register FirstReg;
494 for (const MachineOperand &MO : MI.operands()) {
495 if (!MO.isReg()) continue;
496 Register Reg = MO.getReg();
497 if (!Reg)
498 continue;
499
500 if (FirstReg) {
501 LLVM_DEBUG(dbgs() << "=" << printReg(Reg, TRI));
502 State->UnionGroups(FirstReg, Reg);
503 } else {
504 LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI));
505 FirstReg = Reg;
506 }
507 }
508
509 LLVM_DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n');
510 }
511}
512
513BitVector AggressiveAntiDepBreaker::GetRenameRegisters(MCRegister Reg) {
514 BitVector BV(TRI->getNumRegs(), false);
515 bool first = true;
516
517 // Check all references that need rewriting for Reg. For each, use
518 // the corresponding register class to narrow the set of registers
519 // that are appropriate for renaming.
520 for (const auto &Q : make_range(State->GetRegRefs().equal_range(Reg))) {
521 const TargetRegisterClass *RC = Q.second.RC;
522 if (!RC) continue;
523
524 BitVector RCBV = TRI->getAllocatableSet(MF, RC);
525 if (first) {
526 BV |= RCBV;
527 first = false;
528 } else {
529 BV &= RCBV;
530 }
531
532 LLVM_DEBUG(dbgs() << " " << TRI->getRegClassName(RC));
533 }
534
535 return BV;
536}
537
538bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
539 MCRegister SuperReg, unsigned AntiDepGroupIndex,
540 RenameOrderType &RenameOrder, std::map<MCRegister, MCRegister> &RenameMap) {
541 std::vector<unsigned> &KillIndices = State->GetKillIndices();
542 std::vector<unsigned> &DefIndices = State->GetDefIndices();
543 std::multimap<MCRegister, AggressiveAntiDepState::RegisterReference>
544 &RegRefs = State->GetRegRefs();
545
546 // Collect all referenced registers in the same group as
547 // AntiDepReg. These all need to be renamed together if we are to
548 // break the anti-dependence.
549 std::vector<MCRegister> Regs;
550 State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs);
551 assert(!Regs.empty() && "Empty register group!");
552 if (Regs.empty())
553 return false;
554
555 // Collect the BitVector of registers that can be used to rename
556 // each register.
557 LLVM_DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex
558 << ":\n");
559 std::map<MCRegister, BitVector> RenameRegisterMap;
560 for (MCRegister Reg : Regs) {
561 // If Reg has any references, then collect possible rename regs
562 if (RegRefs.count(Reg) > 0) {
563 LLVM_DEBUG(dbgs() << "\t\t" << printReg(Reg, TRI) << ":");
564
565 BitVector &BV = RenameRegisterMap[Reg];
566 assert(BV.empty());
567 BV = GetRenameRegisters(Reg);
568
569 LLVM_DEBUG({
570 dbgs() << " ::";
571 for (unsigned r : BV.set_bits())
572 dbgs() << " " << printReg(r, TRI);
573 dbgs() << "\n";
574 });
575 }
576 }
577
578 // All group registers should be a subreg of SuperReg.
579 for (MCRegister Reg : Regs) {
580 if (Reg == SuperReg) continue;
581 bool IsSub = TRI->isSubRegister(SuperReg, Reg);
582 // FIXME: remove this once PR18663 has been properly fixed. For now,
583 // return a conservative answer:
584 // assert(IsSub && "Expecting group subregister");
585 if (!IsSub)
586 return false;
587 }
588
589#ifndef NDEBUG
590 // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod
591 if (DebugDiv > 0) {
592 static int renamecnt = 0;
593 if (renamecnt++ % DebugDiv != DebugMod)
594 return false;
595
596 dbgs() << "*** Performing rename " << printReg(SuperReg, TRI)
597 << " for debug ***\n";
598 }
599#endif
600
601 // Check each possible rename register for SuperReg in round-robin
602 // order. If that register is available, and the corresponding
603 // registers are available for the other group subregisters, then we
604 // can use those registers to rename.
605
606 // FIXME: Using getMinimalPhysRegClass is very conservative. We should
607 // check every use of the register and find the largest register class
608 // that can be used in all of them.
609 const TargetRegisterClass *SuperRC = TRI->getMinimalPhysRegClass(SuperReg);
610
611 ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(SuperRC);
612 if (Order.empty()) {
613 LLVM_DEBUG(dbgs() << "\tEmpty Super Regclass!!\n");
614 return false;
615 }
616
617 LLVM_DEBUG(dbgs() << "\tFind Registers:");
618
619 RenameOrder.insert(RenameOrderType::value_type(SuperRC, Order.size()));
620
621 unsigned OrigR = RenameOrder[SuperRC];
622 unsigned EndR = ((OrigR == Order.size()) ? 0 : OrigR);
623 unsigned R = OrigR;
624 do {
625 if (R == 0) R = Order.size();
626 --R;
627 const MCRegister NewSuperReg = Order[R];
628 // Don't consider non-allocatable registers
629 if (!MRI.isAllocatable(NewSuperReg)) continue;
630 // Don't replace a register with itself.
631 if (NewSuperReg == SuperReg) continue;
632
633 LLVM_DEBUG(dbgs() << " [" << printReg(NewSuperReg, TRI) << ':');
634 RenameMap.clear();
635
636 // For each referenced group register (which must be a SuperReg or
637 // a subregister of SuperReg), find the corresponding subregister
638 // of NewSuperReg and make sure it is free to be renamed.
639 for (MCRegister Reg : Regs) {
640 MCRegister NewReg;
641 if (Reg == SuperReg) {
642 NewReg = NewSuperReg;
643 } else {
644 unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg);
645 if (NewSubRegIdx != 0)
646 NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx);
647 }
648
649 LLVM_DEBUG(dbgs() << " " << printReg(NewReg, TRI));
650
651 // Check if Reg can be renamed to NewReg.
652 if (!RenameRegisterMap[Reg].test(NewReg.id())) {
653 LLVM_DEBUG(dbgs() << "(no rename)");
654 goto next_super_reg;
655 }
656
657 // If NewReg is dead and NewReg's most recent def is not before
658 // Regs's kill, it's safe to replace Reg with NewReg. We
659 // must also check all aliases of NewReg, because we can't define a
660 // register when any sub or super is already live.
661 if (State->IsLive(NewReg) ||
662 (KillIndices[Reg.id()] > DefIndices[NewReg.id()])) {
663 LLVM_DEBUG(dbgs() << "(live)");
664 goto next_super_reg;
665 } else {
666 bool found = false;
667 for (MCRegAliasIterator AI(NewReg, TRI, false); AI.isValid(); ++AI) {
668 MCRegister AliasReg = *AI;
669 if (State->IsLive(AliasReg) ||
670 (KillIndices[Reg.id()] > DefIndices[AliasReg.id()])) {
672 << "(alias " << printReg(AliasReg, TRI) << " live)");
673 found = true;
674 break;
675 }
676 }
677 if (found)
678 goto next_super_reg;
679 }
680
681 // We cannot rename 'Reg' to 'NewReg' if one of the uses of 'Reg' also
682 // defines 'NewReg' via an early-clobber operand.
683 for (const auto &Q : make_range(RegRefs.equal_range(Reg))) {
684 MachineInstr *UseMI = Q.second.Operand->getParent();
685 int Idx = UseMI->findRegisterDefOperandIdx(NewReg, TRI, false, true);
686 if (Idx == -1)
687 continue;
688
689 if (UseMI->getOperand(Idx).isEarlyClobber()) {
690 LLVM_DEBUG(dbgs() << "(ec)");
691 goto next_super_reg;
692 }
693 }
694
695 // Also, we cannot rename 'Reg' to 'NewReg' if the instruction defining
696 // 'Reg' is an early-clobber define and that instruction also uses
697 // 'NewReg'.
698 for (const auto &Q : make_range(RegRefs.equal_range(Reg))) {
699 if (!Q.second.Operand->isDef() || !Q.second.Operand->isEarlyClobber())
700 continue;
701
702 MachineInstr *DefMI = Q.second.Operand->getParent();
703 if (DefMI->readsRegister(NewReg, TRI)) {
704 LLVM_DEBUG(dbgs() << "(ec)");
705 goto next_super_reg;
706 }
707 }
708
709 // Record that 'Reg' can be renamed to 'NewReg'.
710 RenameMap.insert(std::make_pair(Reg, NewReg));
711 }
712
713 // If we fall-out here, then every register in the group can be
714 // renamed, as recorded in RenameMap.
715 RenameOrder.erase(SuperRC);
716 RenameOrder.insert(RenameOrderType::value_type(SuperRC, R));
717 LLVM_DEBUG(dbgs() << "]\n");
718 return true;
719
720 next_super_reg:
721 LLVM_DEBUG(dbgs() << ']');
722 } while (R != EndR);
723
724 LLVM_DEBUG(dbgs() << '\n');
725
726 // No registers are free and available!
727 return false;
728}
729
730/// BreakAntiDependencies - Identifiy anti-dependencies within the
731/// ScheduleDAG and break them by renaming registers.
733 const std::vector<SUnit> &SUnits,
736 unsigned InsertPosIndex,
737 DbgValueVector &DbgValues) {
738 std::vector<unsigned> &KillIndices = State->GetKillIndices();
739 std::vector<unsigned> &DefIndices = State->GetDefIndices();
740 std::multimap<MCRegister, AggressiveAntiDepState::RegisterReference>
741 &RegRefs = State->GetRegRefs();
742
743 // The code below assumes that there is at least one instruction,
744 // so just duck out immediately if the block is empty.
745 if (SUnits.empty()) return 0;
746
747 // For each regclass the next register to use for renaming.
748 RenameOrderType RenameOrder;
749
750 // ...need a map from MI to SUnit.
751 std::map<MachineInstr *, const SUnit *> MISUnitMap;
752 for (const SUnit &SU : SUnits)
753 MISUnitMap.insert(std::make_pair(SU.getInstr(), &SU));
754
755 // Track progress along the critical path through the SUnit graph as
756 // we walk the instructions. This is needed for regclasses that only
757 // break critical-path anti-dependencies.
758 const SUnit *CriticalPathSU = nullptr;
759 MachineInstr *CriticalPathMI = nullptr;
760 if (CriticalPathSet.any()) {
761 for (const SUnit &SU : SUnits) {
762 if (!CriticalPathSU ||
763 ((SU.getDepth() + SU.Latency) >
764 (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) {
765 CriticalPathSU = &SU;
766 }
767 }
768 assert(CriticalPathSU && "Failed to find SUnit critical path");
769 CriticalPathMI = CriticalPathSU->getInstr();
770 }
771
772#ifndef NDEBUG
773 LLVM_DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n");
774 LLVM_DEBUG(dbgs() << "Available regs:");
775 for (unsigned Reg = 1; Reg < TRI->getNumRegs(); ++Reg) {
776 if (!State->IsLive(Reg))
777 LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI));
778 }
779 LLVM_DEBUG(dbgs() << '\n');
780#endif
781
782 BitVector RegAliases(TRI->getNumRegs());
783
784 // Attempt to break anti-dependence edges. Walk the instructions
785 // from the bottom up, tracking information about liveness as we go
786 // to help determine which registers are available.
787 unsigned Broken = 0;
788 unsigned Count = InsertPosIndex - 1;
789 for (MachineBasicBlock::iterator I = End, E = Begin;
790 I != E; --Count) {
791 MachineInstr &MI = *--I;
792
793 if (MI.isDebugInstr())
794 continue;
795
796 LLVM_DEBUG(dbgs() << "Anti: ");
797 LLVM_DEBUG(MI.dump());
798
799 std::set<MCRegister> PassthruRegs;
800 GetPassthruRegs(MI, PassthruRegs);
801
802 // Process the defs in MI...
803 PrescanInstruction(MI, Count, PassthruRegs);
804
805 // The dependence edges that represent anti- and output-
806 // dependencies that are candidates for breaking.
807 std::vector<const SDep *> Edges;
808 const SUnit *PathSU = MISUnitMap[&MI];
809 AntiDepEdges(PathSU, Edges);
810
811 // If MI is not on the critical path, then we don't rename
812 // registers in the CriticalPathSet.
813 BitVector *ExcludeRegs = nullptr;
814 if (&MI == CriticalPathMI) {
815 CriticalPathSU = CriticalPathStep(CriticalPathSU);
816 CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : nullptr;
817 } else if (CriticalPathSet.any()) {
818 ExcludeRegs = &CriticalPathSet;
819 }
820
821 // Ignore KILL instructions (they form a group in ScanInstruction
822 // but don't cause any anti-dependence breaking themselves)
823 if (!MI.isKill()) {
824 // Attempt to break each anti-dependency...
825 for (const SDep *Edge : Edges) {
826 SUnit *NextSU = Edge->getSUnit();
827
828 if ((Edge->getKind() != SDep::Anti) &&
829 (Edge->getKind() != SDep::Output)) continue;
830
831 MCRegister AntiDepReg = Edge->getReg().asMCReg();
832 LLVM_DEBUG(dbgs() << "\tAntidep reg: " << printReg(AntiDepReg, TRI));
833 assert(AntiDepReg && "Anti-dependence on reg0?");
834
835 if (!MRI.isAllocatable(AntiDepReg)) {
836 // Don't break anti-dependencies on non-allocatable registers.
837 LLVM_DEBUG(dbgs() << " (non-allocatable)\n");
838 continue;
839 } else if (ExcludeRegs && ExcludeRegs->test(AntiDepReg.id())) {
840 // Don't break anti-dependencies for critical path registers
841 // if not on the critical path
842 LLVM_DEBUG(dbgs() << " (not critical-path)\n");
843 continue;
844 } else if (PassthruRegs.count(AntiDepReg) != 0) {
845 // If the anti-dep register liveness "passes-thru", then
846 // don't try to change it. It will be changed along with
847 // the use if required to break an earlier antidep.
848 LLVM_DEBUG(dbgs() << " (passthru)\n");
849 continue;
850 } else {
851 // No anti-dep breaking for implicit deps
852 MachineOperand *AntiDepOp =
853 MI.findRegisterDefOperand(AntiDepReg, /*TRI=*/nullptr);
854 assert(AntiDepOp && "Can't find index for defined register operand");
855 if (!AntiDepOp || AntiDepOp->isImplicit()) {
856 LLVM_DEBUG(dbgs() << " (implicit)\n");
857 continue;
858 }
859
860 // If the SUnit has other dependencies on the SUnit that
861 // it anti-depends on, don't bother breaking the
862 // anti-dependency since those edges would prevent such
863 // units from being scheduled past each other
864 // regardless.
865 //
866 // Also, if there are dependencies on other SUnits with the
867 // same register as the anti-dependency, don't attempt to
868 // break it.
869 for (const SDep &Pred : PathSU->Preds) {
870 if (Pred.getSUnit() == NextSU ? (Pred.getKind() != SDep::Anti ||
871 Pred.getReg() != AntiDepReg)
872 : (Pred.getKind() == SDep::Data &&
873 Pred.getReg() == AntiDepReg)) {
874 AntiDepReg = MCRegister();
875 break;
876 }
877 }
878 for (const SDep &Pred : PathSU->Preds) {
879 if ((Pred.getSUnit() == NextSU) && (Pred.getKind() != SDep::Anti) &&
880 (Pred.getKind() != SDep::Output)) {
881 LLVM_DEBUG(dbgs() << " (real dependency)\n");
882 AntiDepReg = MCRegister();
883 break;
884 } else if ((Pred.getSUnit() != NextSU) &&
885 (Pred.getKind() == SDep::Data) &&
886 (Pred.getReg() == AntiDepReg)) {
887 LLVM_DEBUG(dbgs() << " (other dependency)\n");
888 AntiDepReg = MCRegister();
889 break;
890 }
891 }
892
893 if (!AntiDepReg)
894 continue;
895 }
896
897 assert(AntiDepReg);
898
899 // Determine AntiDepReg's register group.
900 const unsigned GroupIndex = State->GetGroup(AntiDepReg);
901 if (GroupIndex == 0) {
902 LLVM_DEBUG(dbgs() << " (zero group)\n");
903 continue;
904 }
905
906 LLVM_DEBUG(dbgs() << '\n');
907
908 // Look for a suitable register to use to break the anti-dependence.
909 std::map<MCRegister, MCRegister> RenameMap;
910 if (FindSuitableFreeRegisters(AntiDepReg, GroupIndex, RenameOrder,
911 RenameMap)) {
912 LLVM_DEBUG(dbgs() << "\tBreaking anti-dependence edge on "
913 << printReg(AntiDepReg, TRI) << ":");
914
915 // Handle each group register...
916 for (const auto &P : RenameMap) {
917 MCRegister CurrReg = P.first;
918 MCRegister NewReg = P.second;
919
920 LLVM_DEBUG(dbgs() << " " << printReg(CurrReg, TRI) << "->"
921 << printReg(NewReg, TRI) << "("
922 << RegRefs.count(CurrReg) << " refs)");
923
924 // Update the references to the old register CurrReg to
925 // refer to the new register NewReg.
926 for (const auto &Q : make_range(RegRefs.equal_range(CurrReg))) {
927 Q.second.Operand->setReg(NewReg);
928 // If the SU for the instruction being updated has debug
929 // information related to the anti-dependency register, make
930 // sure to update that as well.
931 const SUnit *SU = MISUnitMap[Q.second.Operand->getParent()];
932 if (!SU) continue;
933 UpdateDbgValues(DbgValues, Q.second.Operand->getParent(),
934 AntiDepReg, NewReg);
935 }
936
937 // We just went back in time and modified history; the
938 // liveness information for CurrReg is now inconsistent. Set
939 // the state as if it were dead.
940 State->UnionGroups(NewReg, 0);
941 RegRefs.erase(NewReg);
942 DefIndices[NewReg.id()] = DefIndices[CurrReg.id()];
943 KillIndices[NewReg.id()] = KillIndices[CurrReg.id()];
944
945 State->UnionGroups(CurrReg, 0);
946 RegRefs.erase(CurrReg);
947 DefIndices[CurrReg.id()] = KillIndices[CurrReg.id()];
948 KillIndices[CurrReg.id()] = ~0u;
949 assert(((KillIndices[CurrReg.id()] == ~0u) !=
950 (DefIndices[CurrReg.id()] == ~0u)) &&
951 "Kill and Def maps aren't consistent for AntiDepReg!");
952 }
953
954 ++Broken;
955 LLVM_DEBUG(dbgs() << '\n');
956 }
957 }
958 }
959
960 ScanInstruction(MI, Count);
961 }
962
963 return Broken;
964}
965
967 MachineFunction &MFi, const RegisterClassInfo &RCI,
968 TargetSubtargetInfo::RegClassVector &CriticalPathRCs) {
969 return new AggressiveAntiDepBreaker(MFi, RCI, CriticalPathRCs);
970}
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static void AntiDepEdges(const SUnit *SU, std::vector< const SDep * > &Edges)
AntiDepEdges - Return in Edges the anti- and output- dependencies in SU that we want to consider for ...
static const SUnit * CriticalPathStep(const SUnit *SU)
CriticalPathStep - Return the next SUnit after SU on the bottom-up critical path.
static cl::opt< int > DebugDiv("agg-antidep-debugdiv", cl::desc("Debug control for aggressive anti-dep breaker"), cl::init(0), cl::Hidden)
static cl::opt< int > DebugMod("agg-antidep-debugmod", cl::desc("Debug control for aggressive anti-dep breaker"), cl::init(0), cl::Hidden)
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
modulo schedule test
#define P(N)
This file defines the SmallSet class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
void Observe(MachineInstr &MI, unsigned Count, unsigned InsertPosIndex) override
Update liveness information to account for the current instruction, which will not be scheduled.
void FinishBlock() override
Finish anti-dep breaking for a basic block.
unsigned BreakAntiDependencies(const std::vector< SUnit > &SUnits, MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End, unsigned InsertPosIndex, DbgValueVector &DbgValues) override
Identifiy anti-dependencies along the critical path of the ScheduleDAG and break them by renaming reg...
void StartBlock(MachineBasicBlock *BB) override
Initialize anti-dep breaking for a new basic block.
AggressiveAntiDepBreaker(MachineFunction &MFi, const RegisterClassInfo &RCI, TargetSubtargetInfo::RegClassVector &CriticalPathRCs)
Contains all the state necessary for anti-dep breaking.
AggressiveAntiDepState(const unsigned TargetRegs, MachineBasicBlock *BB)
void GetGroupRegs(unsigned Group, std::vector< MCRegister > &Regs, std::multimap< MCRegister, AggressiveAntiDepState::RegisterReference > *RegRefs)
bool IsLive(MCRegister Reg)
Return true if Reg is live.
unsigned UnionGroups(MCRegister Reg1, MCRegister Reg2)
This class works in conjunction with the post-RA scheduler to rename registers to break register anti...
void UpdateDbgValues(const DbgValueVector &DbgValues, MachineInstr *ParentMI, MCRegister OldReg, MCRegister NewReg)
Update all DBG_VALUE instructions that may be affected by the dependency breaker's update of ParentMI...
std::vector< std::pair< MachineInstr *, MachineInstr * > > DbgValueVector
size_t size() const
Get the array size.
Definition ArrayRef.h:141
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
bool test(unsigned Idx) const
Returns true if bit Idx is set.
Definition BitVector.h:482
iterator_range< const_set_bits_iterator > set_bits() const
Definition BitVector.h:159
bool empty() const
Returns whether there are no bits in this bitvector.
Definition BitVector.h:175
MCRegAliasIterator enumerates all registers aliasing Reg.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
constexpr unsigned id() const
Definition MCRegister.h:82
bool isReturnBlock() const
Convenience function that returns true if the block ends in a return instruction.
iterator_range< succ_iterator > successors()
MachineInstrBundleIterator< MachineInstr > iterator
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
LLVM_ABI BitVector getPristineRegs(const MachineFunction &MF) const
Return a set of physical registers that are pristine.
Representation of each machine instruction.
const MachineBasicBlock * getParent() const
bool readsRegister(Register Reg, const TargetRegisterInfo *TRI) const
Return true if the MachineInstr reads the specified register.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI int findRegisterDefOperandIdx(Register Reg, const TargetRegisterInfo *TRI, bool isDead=false, bool Overlap=false) const
Returns the operand index that is a def of the specified register or -1 if it is not found.
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isEarlyClobber() const
Register getReg() const
getReg - Returns the register number.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
Definition Register.h:107
constexpr unsigned id() const
Definition Register.h:100
Scheduling dependency.
Definition ScheduleDAG.h:51
@ Output
A register output-dependence (aka WAW).
Definition ScheduleDAG.h:57
@ Anti
A register anti-dependence (aka WAR).
Definition ScheduleDAG.h:56
@ Data
Regular data dependence (aka true-dependence).
Definition ScheduleDAG.h:55
Scheduling unit. This is a node in the scheduling DAG.
unsigned short Latency
Node latency.
unsigned getDepth() const
Returns the depth of this node, which is the length of the maximum path up to any node which has no p...
SmallVector< SDep, 4 > Preds
All sunit predecessors.
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:134
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
SmallVectorImpl< const TargetRegisterClass * > RegClassVector
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI AntiDepBreaker * createAggressiveAntiDepBreaker(MachineFunction &MFi, const RegisterClassInfo &RCI, TargetSubtargetInfo::RegClassVector &CriticalPathRCs)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
@ Other
Any other memory.
Definition ModRef.h:68
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
FunctionAddr VTableAddr Next
Definition InstrProf.h:141
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.