1 //===- CriticalAntiDepBreaker.cpp - Anti-dep breaker ----------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the CriticalAntiDepBreaker class, which
10 // implements register anti-dependence breaking along a blocks
11 // critical path during post-RA scheduler.
13 //===----------------------------------------------------------------------===//
15 #include "CriticalAntiDepBreaker.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineInstr.h"
23 #include "llvm/CodeGen/MachineOperand.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/RegisterClassInfo.h"
26 #include "llvm/CodeGen/ScheduleDAG.h"
27 #include "llvm/CodeGen/TargetInstrInfo.h"
28 #include "llvm/CodeGen/TargetRegisterInfo.h"
29 #include "llvm/CodeGen/TargetSubtargetInfo.h"
30 #include "llvm/MC/MCInstrDesc.h"
31 #include "llvm/MC/MCRegisterInfo.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/raw_ostream.h"
39 #define DEBUG_TYPE "post-RA-sched"
41 CriticalAntiDepBreaker::CriticalAntiDepBreaker(MachineFunction
&MFi
,
42 const RegisterClassInfo
&RCI
)
43 : MF(MFi
), MRI(MF
.getRegInfo()), TII(MF
.getSubtarget().getInstrInfo()),
44 TRI(MF
.getSubtarget().getRegisterInfo()), RegClassInfo(RCI
),
45 Classes(TRI
->getNumRegs(), nullptr), KillIndices(TRI
->getNumRegs(), 0),
46 DefIndices(TRI
->getNumRegs(), 0), KeepRegs(TRI
->getNumRegs(), false) {}
48 CriticalAntiDepBreaker::~CriticalAntiDepBreaker() = default;
50 void CriticalAntiDepBreaker::StartBlock(MachineBasicBlock
*BB
) {
51 const unsigned BBSize
= BB
->size();
52 for (unsigned i
= 1, e
= TRI
->getNumRegs(); i
!= e
; ++i
) {
53 // Clear out the register class data.
56 // Initialize the indices to indicate that no registers are live.
58 DefIndices
[i
] = BBSize
;
61 // Clear "do not change" set.
64 bool IsReturnBlock
= BB
->isReturnBlock();
66 // Examine the live-in regs of all successors.
67 for (const MachineBasicBlock
*Succ
: BB
->successors())
68 for (const auto &LI
: Succ
->liveins()) {
69 for (MCRegAliasIterator
AI(LI
.PhysReg
, TRI
, true); AI
.isValid(); ++AI
) {
71 Classes
[Reg
] = reinterpret_cast<TargetRegisterClass
*>(-1);
72 KillIndices
[Reg
] = BBSize
;
73 DefIndices
[Reg
] = ~0u;
77 // Mark live-out callee-saved registers. In a return block this is
78 // all callee-saved registers. In non-return this is any
79 // callee-saved register that is not saved in the prolog.
80 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
81 BitVector Pristine
= MFI
.getPristineRegs(MF
);
82 for (const MCPhysReg
*I
= MF
.getRegInfo().getCalleeSavedRegs(); *I
;
85 if (!IsReturnBlock
&& !Pristine
.test(Reg
))
87 for (MCRegAliasIterator
AI(*I
, TRI
, true); AI
.isValid(); ++AI
) {
89 Classes
[Reg
] = reinterpret_cast<TargetRegisterClass
*>(-1);
90 KillIndices
[Reg
] = BBSize
;
91 DefIndices
[Reg
] = ~0u;
96 void CriticalAntiDepBreaker::FinishBlock() {
101 void CriticalAntiDepBreaker::Observe(MachineInstr
&MI
, unsigned Count
,
102 unsigned InsertPosIndex
) {
103 // Kill instructions can define registers but are really nops, and there might
104 // be a real definition earlier that needs to be paired with uses dominated by
107 // FIXME: It may be possible to remove the isKill() restriction once PR18663
108 // has been properly fixed. There can be value in processing kills as seen in
109 // the AggressiveAntiDepBreaker class.
110 if (MI
.isDebugInstr() || MI
.isKill())
112 assert(Count
< InsertPosIndex
&& "Instruction index out of expected range!");
114 for (unsigned Reg
= 1; Reg
!= TRI
->getNumRegs(); ++Reg
) {
115 if (KillIndices
[Reg
] != ~0u) {
116 // If Reg is currently live, then mark that it can't be renamed as
117 // we don't know the extent of its live-range anymore (now that it
118 // has been scheduled).
119 Classes
[Reg
] = reinterpret_cast<TargetRegisterClass
*>(-1);
120 KillIndices
[Reg
] = Count
;
121 } else if (DefIndices
[Reg
] < InsertPosIndex
&& DefIndices
[Reg
] >= Count
) {
122 // Any register which was defined within the previous scheduling region
123 // may have been rescheduled and its lifetime may overlap with registers
124 // in ways not reflected in our current liveness state. For each such
125 // register, adjust the liveness state to be conservatively correct.
126 Classes
[Reg
] = reinterpret_cast<TargetRegisterClass
*>(-1);
128 // Move the def index to the end of the previous region, to reflect
129 // that the def could theoretically have been scheduled at the end.
130 DefIndices
[Reg
] = InsertPosIndex
;
134 PrescanInstruction(MI
);
135 ScanInstruction(MI
, Count
);
138 /// CriticalPathStep - Return the next SUnit after SU on the bottom-up
140 static const SDep
*CriticalPathStep(const SUnit
*SU
) {
141 const SDep
*Next
= nullptr;
142 unsigned NextDepth
= 0;
143 // Find the predecessor edge with the greatest depth.
144 for (const SDep
&P
: SU
->Preds
) {
145 const SUnit
*PredSU
= P
.getSUnit();
146 unsigned PredLatency
= P
.getLatency();
147 unsigned PredTotalLatency
= PredSU
->getDepth() + PredLatency
;
148 // In the case of a latency tie, prefer an anti-dependency edge over
149 // other types of edges.
150 if (NextDepth
< PredTotalLatency
||
151 (NextDepth
== PredTotalLatency
&& P
.getKind() == SDep::Anti
)) {
152 NextDepth
= PredTotalLatency
;
159 void CriticalAntiDepBreaker::PrescanInstruction(MachineInstr
&MI
) {
160 // It's not safe to change register allocation for source operands of
161 // instructions that have special allocation requirements. Also assume all
162 // registers used in a call must not be changed (ABI).
163 // FIXME: The issue with predicated instruction is more complex. We are being
164 // conservative here because the kill markers cannot be trusted after
166 // %r6 = LDR %sp, %reg0, 92, 14, %reg0; mem:LD4[FixedStack14]
168 // STR %r0, killed %r6, %reg0, 0, 0, %cpsr; mem:ST4[%395]
169 // %r6 = LDR %sp, %reg0, 100, 0, %cpsr; mem:LD4[FixedStack12]
170 // STR %r0, killed %r6, %reg0, 0, 14, %reg0; mem:ST4[%396](align=8)
172 // The first R6 kill is not really a kill since it's killed by a predicated
173 // instruction which may not be executed. The second R6 def may or may not
174 // re-define R6 so it's not safe to change it since the last R6 use cannot be
177 MI
.isCall() || MI
.hasExtraSrcRegAllocReq() || TII
->isPredicated(MI
);
179 // Scan the register operands for this instruction and update
180 // Classes and RegRefs.
181 for (unsigned i
= 0, e
= MI
.getNumOperands(); i
!= e
; ++i
) {
182 MachineOperand
&MO
= MI
.getOperand(i
);
183 if (!MO
.isReg()) continue;
184 Register Reg
= MO
.getReg();
185 if (Reg
== 0) continue;
186 const TargetRegisterClass
*NewRC
= nullptr;
188 if (i
< MI
.getDesc().getNumOperands())
189 NewRC
= TII
->getRegClass(MI
.getDesc(), i
, TRI
, MF
);
191 // For now, only allow the register to be changed if its register
192 // class is consistent across all uses.
193 if (!Classes
[Reg
] && NewRC
)
194 Classes
[Reg
] = NewRC
;
195 else if (!NewRC
|| Classes
[Reg
] != NewRC
)
196 Classes
[Reg
] = reinterpret_cast<TargetRegisterClass
*>(-1);
198 // Now check for aliases.
199 for (MCRegAliasIterator
AI(Reg
, TRI
, false); AI
.isValid(); ++AI
) {
200 // If an alias of the reg is used during the live range, give up.
201 // Note that this allows us to skip checking if AntiDepReg
202 // overlaps with any of the aliases, among other things.
203 unsigned AliasReg
= *AI
;
204 if (Classes
[AliasReg
]) {
205 Classes
[AliasReg
] = reinterpret_cast<TargetRegisterClass
*>(-1);
206 Classes
[Reg
] = reinterpret_cast<TargetRegisterClass
*>(-1);
210 // If we're still willing to consider this register, note the reference.
211 if (Classes
[Reg
] != reinterpret_cast<TargetRegisterClass
*>(-1))
212 RegRefs
.insert(std::make_pair(Reg
, &MO
));
214 if (MO
.isUse() && Special
) {
215 if (!KeepRegs
.test(Reg
)) {
216 for (MCPhysReg SubReg
: TRI
->subregs_inclusive(Reg
))
217 KeepRegs
.set(SubReg
);
222 for (unsigned I
= 0, E
= MI
.getNumOperands(); I
!= E
; ++I
) {
223 const MachineOperand
&MO
= MI
.getOperand(I
);
224 if (!MO
.isReg()) continue;
225 Register Reg
= MO
.getReg();
228 // If this reg is tied and live (Classes[Reg] is set to -1), we can't change
229 // it or any of its sub or super regs. We need to use KeepRegs to mark the
230 // reg because not all uses of the same reg within an instruction are
231 // necessarily tagged as tied.
232 // Example: an x86 "xor %eax, %eax" will have one source operand tied to the
233 // def register but not the second (see PR20020 for details).
234 // FIXME: can this check be relaxed to account for undef uses
235 // of a register? In the above 'xor' example, the uses of %eax are undef, so
236 // earlier instructions could still replace %eax even though the 'xor'
237 // itself can't be changed.
238 if (MI
.isRegTiedToUseOperand(I
) &&
239 Classes
[Reg
] == reinterpret_cast<TargetRegisterClass
*>(-1)) {
240 for (MCPhysReg SubReg
: TRI
->subregs_inclusive(Reg
)) {
241 KeepRegs
.set(SubReg
);
243 for (MCPhysReg SuperReg
: TRI
->superregs(Reg
)) {
244 KeepRegs
.set(SuperReg
);
250 void CriticalAntiDepBreaker::ScanInstruction(MachineInstr
&MI
, unsigned Count
) {
252 // Proceeding upwards, registers that are defed but not used in this
253 // instruction are now dead.
254 assert(!MI
.isKill() && "Attempting to scan a kill instruction");
256 if (!TII
->isPredicated(MI
)) {
257 // Predicated defs are modeled as read + write, i.e. similar to two
259 for (unsigned i
= 0, e
= MI
.getNumOperands(); i
!= e
; ++i
) {
260 MachineOperand
&MO
= MI
.getOperand(i
);
262 if (MO
.isRegMask()) {
263 auto ClobbersPhysRegAndSubRegs
= [&](unsigned PhysReg
) {
264 return all_of(TRI
->subregs_inclusive(PhysReg
),
265 [&](MCPhysReg SR
) { return MO
.clobbersPhysReg(SR
); });
268 for (unsigned i
= 1, e
= TRI
->getNumRegs(); i
!= e
; ++i
) {
269 if (ClobbersPhysRegAndSubRegs(i
)) {
270 DefIndices
[i
] = Count
;
271 KillIndices
[i
] = ~0u;
273 Classes
[i
] = nullptr;
279 if (!MO
.isReg()) continue;
280 Register Reg
= MO
.getReg();
281 if (Reg
== 0) continue;
282 if (!MO
.isDef()) continue;
284 // Ignore two-addr defs.
285 if (MI
.isRegTiedToUseOperand(i
))
288 // If we've already marked this reg as unchangeable, don't remove
289 // it or any of its subregs from KeepRegs.
290 bool Keep
= KeepRegs
.test(Reg
);
292 // For the reg itself and all subregs: update the def to current;
293 // reset the kill state, any restrictions, and references.
294 for (MCPhysReg SubregReg
: TRI
->subregs_inclusive(Reg
)) {
295 DefIndices
[SubregReg
] = Count
;
296 KillIndices
[SubregReg
] = ~0u;
297 Classes
[SubregReg
] = nullptr;
298 RegRefs
.erase(SubregReg
);
300 KeepRegs
.reset(SubregReg
);
302 // Conservatively mark super-registers as unusable.
303 for (MCPhysReg SR
: TRI
->superregs(Reg
))
304 Classes
[SR
] = reinterpret_cast<TargetRegisterClass
*>(-1);
307 for (unsigned i
= 0, e
= MI
.getNumOperands(); i
!= e
; ++i
) {
308 MachineOperand
&MO
= MI
.getOperand(i
);
309 if (!MO
.isReg()) continue;
310 Register Reg
= MO
.getReg();
311 if (Reg
== 0) continue;
312 if (!MO
.isUse()) continue;
314 const TargetRegisterClass
*NewRC
= nullptr;
315 if (i
< MI
.getDesc().getNumOperands())
316 NewRC
= TII
->getRegClass(MI
.getDesc(), i
, TRI
, MF
);
318 // For now, only allow the register to be changed if its register
319 // class is consistent across all uses.
320 if (!Classes
[Reg
] && NewRC
)
321 Classes
[Reg
] = NewRC
;
322 else if (!NewRC
|| Classes
[Reg
] != NewRC
)
323 Classes
[Reg
] = reinterpret_cast<TargetRegisterClass
*>(-1);
325 RegRefs
.insert(std::make_pair(Reg
, &MO
));
327 // It wasn't previously live but now it is, this is a kill.
328 // Repeat for all aliases.
329 for (MCRegAliasIterator
AI(Reg
, TRI
, true); AI
.isValid(); ++AI
) {
330 unsigned AliasReg
= *AI
;
331 if (KillIndices
[AliasReg
] == ~0u) {
332 KillIndices
[AliasReg
] = Count
;
333 DefIndices
[AliasReg
] = ~0u;
339 // Check all machine operands that reference the antidependent register and must
340 // be replaced by NewReg. Return true if any of their parent instructions may
341 // clobber the new register.
343 // Note: AntiDepReg may be referenced by a two-address instruction such that
344 // it's use operand is tied to a def operand. We guard against the case in which
345 // the two-address instruction also defines NewReg, as may happen with
346 // pre/postincrement loads. In this case, both the use and def operands are in
347 // RegRefs because the def is inserted by PrescanInstruction and not erased
348 // during ScanInstruction. So checking for an instruction with definitions of
349 // both NewReg and AntiDepReg covers it.
351 CriticalAntiDepBreaker::isNewRegClobberedByRefs(RegRefIter RegRefBegin
,
352 RegRefIter RegRefEnd
,
354 for (RegRefIter I
= RegRefBegin
; I
!= RegRefEnd
; ++I
) {
355 MachineOperand
*RefOper
= I
->second
;
357 // Don't allow the instruction defining AntiDepReg to earlyclobber its
358 // operands, in case they may be assigned to NewReg. In this case antidep
359 // breaking must fail, but it's too rare to bother optimizing.
360 if (RefOper
->isDef() && RefOper
->isEarlyClobber())
363 // Handle cases in which this instruction defines NewReg.
364 MachineInstr
*MI
= RefOper
->getParent();
365 for (const MachineOperand
&CheckOper
: MI
->operands()) {
366 if (CheckOper
.isRegMask() && CheckOper
.clobbersPhysReg(NewReg
))
369 if (!CheckOper
.isReg() || !CheckOper
.isDef() ||
370 CheckOper
.getReg() != NewReg
)
373 // Don't allow the instruction to define NewReg and AntiDepReg.
374 // When AntiDepReg is renamed it will be an illegal op.
375 if (RefOper
->isDef())
378 // Don't allow an instruction using AntiDepReg to be earlyclobbered by
380 if (CheckOper
.isEarlyClobber())
383 // Don't allow inline asm to define NewReg at all. Who knows what it's
385 if (MI
->isInlineAsm())
392 unsigned CriticalAntiDepBreaker::
393 findSuitableFreeRegister(RegRefIter RegRefBegin
,
394 RegRefIter RegRefEnd
,
397 const TargetRegisterClass
*RC
,
398 SmallVectorImpl
<unsigned> &Forbid
) {
399 ArrayRef
<MCPhysReg
> Order
= RegClassInfo
.getOrder(RC
);
400 for (unsigned NewReg
: Order
) {
401 // Don't replace a register with itself.
402 if (NewReg
== AntiDepReg
) continue;
403 // Don't replace a register with one that was recently used to repair
404 // an anti-dependence with this AntiDepReg, because that would
405 // re-introduce that anti-dependence.
406 if (NewReg
== LastNewReg
) continue;
407 // If any instructions that define AntiDepReg also define the NewReg, it's
408 // not suitable. For example, Instruction with multiple definitions can
409 // result in this condition.
410 if (isNewRegClobberedByRefs(RegRefBegin
, RegRefEnd
, NewReg
)) continue;
411 // If NewReg is dead and NewReg's most recent def is not before
412 // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.
413 assert(((KillIndices
[AntiDepReg
] == ~0u) != (DefIndices
[AntiDepReg
] == ~0u))
414 && "Kill and Def maps aren't consistent for AntiDepReg!");
415 assert(((KillIndices
[NewReg
] == ~0u) != (DefIndices
[NewReg
] == ~0u))
416 && "Kill and Def maps aren't consistent for NewReg!");
417 if (KillIndices
[NewReg
] != ~0u ||
418 Classes
[NewReg
] == reinterpret_cast<TargetRegisterClass
*>(-1) ||
419 KillIndices
[AntiDepReg
] > DefIndices
[NewReg
])
421 // If NewReg overlaps any of the forbidden registers, we can't use it.
422 bool Forbidden
= false;
423 for (unsigned R
: Forbid
)
424 if (TRI
->regsOverlap(NewReg
, R
)) {
428 if (Forbidden
) continue;
432 // No registers are free and available!
436 unsigned CriticalAntiDepBreaker::
437 BreakAntiDependencies(const std::vector
<SUnit
> &SUnits
,
438 MachineBasicBlock::iterator Begin
,
439 MachineBasicBlock::iterator End
,
440 unsigned InsertPosIndex
,
441 DbgValueVector
&DbgValues
) {
442 // The code below assumes that there is at least one instruction,
443 // so just duck out immediately if the block is empty.
444 if (SUnits
.empty()) return 0;
446 // Keep a map of the MachineInstr*'s back to the SUnit representing them.
447 // This is used for updating debug information.
449 // FIXME: Replace this with the existing map in ScheduleDAGInstrs::MISUnitMap
450 DenseMap
<MachineInstr
*, const SUnit
*> MISUnitMap
;
452 // Find the node at the bottom of the critical path.
453 const SUnit
*Max
= nullptr;
454 for (const SUnit
&SU
: SUnits
) {
455 MISUnitMap
[SU
.getInstr()] = &SU
;
456 if (!Max
|| SU
.getDepth() + SU
.Latency
> Max
->getDepth() + Max
->Latency
)
459 assert(Max
&& "Failed to find bottom of the critical path");
463 LLVM_DEBUG(dbgs() << "Critical path has total latency "
464 << (Max
->getDepth() + Max
->Latency
) << "\n");
465 LLVM_DEBUG(dbgs() << "Available regs:");
466 for (unsigned Reg
= 1; Reg
< TRI
->getNumRegs(); ++Reg
) {
467 if (KillIndices
[Reg
] == ~0u)
468 LLVM_DEBUG(dbgs() << " " << printReg(Reg
, TRI
));
470 LLVM_DEBUG(dbgs() << '\n');
474 // Track progress along the critical path through the SUnit graph as we walk
476 const SUnit
*CriticalPathSU
= Max
;
477 MachineInstr
*CriticalPathMI
= CriticalPathSU
->getInstr();
479 // Consider this pattern:
488 // There are three anti-dependencies here, and without special care,
489 // we'd break all of them using the same register:
498 // because at each anti-dependence, B is the first register that
499 // isn't A which is free. This re-introduces anti-dependencies
500 // at all but one of the original anti-dependencies that we were
501 // trying to break. To avoid this, keep track of the most recent
502 // register that each register was replaced with, avoid
503 // using it to repair an anti-dependence on the same register.
504 // This lets us produce this:
513 // This still has an anti-dependence on B, but at least it isn't on the
514 // original critical path.
516 // TODO: If we tracked more than one register here, we could potentially
517 // fix that remaining critical edge too. This is a little more involved,
518 // because unlike the most recent register, less recent registers should
519 // still be considered, though only if no other registers are available.
520 std::vector
<unsigned> LastNewReg(TRI
->getNumRegs(), 0);
522 // Attempt to break anti-dependence edges on the critical path. Walk the
523 // instructions from the bottom up, tracking information about liveness
524 // as we go to help determine which registers are available.
526 unsigned Count
= InsertPosIndex
- 1;
527 for (MachineBasicBlock::iterator I
= End
, E
= Begin
; I
!= E
; --Count
) {
528 MachineInstr
&MI
= *--I
;
529 // Kill instructions can define registers but are really nops, and there
530 // might be a real definition earlier that needs to be paired with uses
531 // dominated by this kill.
533 // FIXME: It may be possible to remove the isKill() restriction once PR18663
534 // has been properly fixed. There can be value in processing kills as seen
535 // in the AggressiveAntiDepBreaker class.
536 if (MI
.isDebugInstr() || MI
.isKill())
539 // Check if this instruction has a dependence on the critical path that
540 // is an anti-dependence that we may be able to break. If it is, set
541 // AntiDepReg to the non-zero register associated with the anti-dependence.
543 // We limit our attention to the critical path as a heuristic to avoid
544 // breaking anti-dependence edges that aren't going to significantly
545 // impact the overall schedule. There are a limited number of registers
546 // and we want to save them for the important edges.
548 // TODO: Instructions with multiple defs could have multiple
549 // anti-dependencies. The current code here only knows how to break one
550 // edge per instruction. Note that we'd have to be able to break all of
551 // the anti-dependencies in an instruction in order to be effective.
552 unsigned AntiDepReg
= 0;
553 if (&MI
== CriticalPathMI
) {
554 if (const SDep
*Edge
= CriticalPathStep(CriticalPathSU
)) {
555 const SUnit
*NextSU
= Edge
->getSUnit();
557 // Only consider anti-dependence edges.
558 if (Edge
->getKind() == SDep::Anti
) {
559 AntiDepReg
= Edge
->getReg();
560 assert(AntiDepReg
!= 0 && "Anti-dependence on reg0?");
561 if (!MRI
.isAllocatable(AntiDepReg
))
562 // Don't break anti-dependencies on non-allocatable registers.
564 else if (KeepRegs
.test(AntiDepReg
))
565 // Don't break anti-dependencies if a use down below requires
566 // this exact register.
569 // If the SUnit has other dependencies on the SUnit that it
570 // anti-depends on, don't bother breaking the anti-dependency
571 // since those edges would prevent such units from being
572 // scheduled past each other regardless.
574 // Also, if there are dependencies on other SUnits with the
575 // same register as the anti-dependency, don't attempt to
577 for (const SDep
&P
: CriticalPathSU
->Preds
)
578 if (P
.getSUnit() == NextSU
579 ? (P
.getKind() != SDep::Anti
|| P
.getReg() != AntiDepReg
)
580 : (P
.getKind() == SDep::Data
&&
581 P
.getReg() == AntiDepReg
)) {
587 CriticalPathSU
= NextSU
;
588 CriticalPathMI
= CriticalPathSU
->getInstr();
590 // We've reached the end of the critical path.
591 CriticalPathSU
= nullptr;
592 CriticalPathMI
= nullptr;
596 PrescanInstruction(MI
);
598 SmallVector
<unsigned, 2> ForbidRegs
;
600 // If MI's defs have a special allocation requirement, don't allow
601 // any def registers to be changed. Also assume all registers
602 // defined in a call must not be changed (ABI).
603 if (MI
.isCall() || MI
.hasExtraDefRegAllocReq() || TII
->isPredicated(MI
))
604 // If this instruction's defs have special allocation requirement, don't
605 // break this anti-dependency.
607 else if (AntiDepReg
) {
608 // If this instruction has a use of AntiDepReg, breaking it
609 // is invalid. If the instruction defines other registers,
610 // save a list of them so that we don't pick a new register
611 // that overlaps any of them.
612 for (const MachineOperand
&MO
: MI
.operands()) {
613 if (!MO
.isReg()) continue;
614 Register Reg
= MO
.getReg();
615 if (Reg
== 0) continue;
616 if (MO
.isUse() && TRI
->regsOverlap(AntiDepReg
, Reg
)) {
620 if (MO
.isDef() && Reg
!= AntiDepReg
)
621 ForbidRegs
.push_back(Reg
);
625 // Determine AntiDepReg's register class, if it is live and is
626 // consistently used within a single class.
627 const TargetRegisterClass
*RC
= AntiDepReg
!= 0 ? Classes
[AntiDepReg
]
629 assert((AntiDepReg
== 0 || RC
!= nullptr) &&
630 "Register should be live if it's causing an anti-dependence!");
631 if (RC
== reinterpret_cast<TargetRegisterClass
*>(-1))
634 // Look for a suitable register to use to break the anti-dependence.
636 // TODO: Instead of picking the first free register, consider which might
638 if (AntiDepReg
!= 0) {
639 std::pair
<std::multimap
<unsigned, MachineOperand
*>::iterator
,
640 std::multimap
<unsigned, MachineOperand
*>::iterator
>
641 Range
= RegRefs
.equal_range(AntiDepReg
);
642 if (unsigned NewReg
= findSuitableFreeRegister(Range
.first
, Range
.second
,
644 LastNewReg
[AntiDepReg
],
646 LLVM_DEBUG(dbgs() << "Breaking anti-dependence edge on "
647 << printReg(AntiDepReg
, TRI
) << " with "
648 << RegRefs
.count(AntiDepReg
) << " references"
649 << " using " << printReg(NewReg
, TRI
) << "!\n");
651 // Update the references to the old register to refer to the new
653 for (std::multimap
<unsigned, MachineOperand
*>::iterator
654 Q
= Range
.first
, QE
= Range
.second
; Q
!= QE
; ++Q
) {
655 Q
->second
->setReg(NewReg
);
656 // If the SU for the instruction being updated has debug information
657 // related to the anti-dependency register, make sure to update that
659 const SUnit
*SU
= MISUnitMap
[Q
->second
->getParent()];
661 UpdateDbgValues(DbgValues
, Q
->second
->getParent(),
665 // We just went back in time and modified history; the
666 // liveness information for the anti-dependence reg is now
667 // inconsistent. Set the state as if it were dead.
668 Classes
[NewReg
] = Classes
[AntiDepReg
];
669 DefIndices
[NewReg
] = DefIndices
[AntiDepReg
];
670 KillIndices
[NewReg
] = KillIndices
[AntiDepReg
];
671 assert(((KillIndices
[NewReg
] == ~0u) !=
672 (DefIndices
[NewReg
] == ~0u)) &&
673 "Kill and Def maps aren't consistent for NewReg!");
675 Classes
[AntiDepReg
] = nullptr;
676 DefIndices
[AntiDepReg
] = KillIndices
[AntiDepReg
];
677 KillIndices
[AntiDepReg
] = ~0u;
678 assert(((KillIndices
[AntiDepReg
] == ~0u) !=
679 (DefIndices
[AntiDepReg
] == ~0u)) &&
680 "Kill and Def maps aren't consistent for AntiDepReg!");
682 RegRefs
.erase(AntiDepReg
);
683 LastNewReg
[AntiDepReg
] = NewReg
;
688 ScanInstruction(MI
, Count
);
695 llvm::createCriticalAntiDepBreaker(MachineFunction
&MFi
,
696 const RegisterClassInfo
&RCI
) {
697 return new CriticalAntiDepBreaker(MFi
, RCI
);