Path: blob/main/contrib/llvm-project/llvm/lib/CodeGen/CriticalAntiDepBreaker.cpp
35233 views
//===- CriticalAntiDepBreaker.cpp - Anti-dep breaker ----------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the CriticalAntiDepBreaker class, which9// implements register anti-dependence breaking along a blocks10// critical path during post-RA scheduler.11//12//===----------------------------------------------------------------------===//1314#include "CriticalAntiDepBreaker.h"15#include "llvm/ADT/ArrayRef.h"16#include "llvm/ADT/DenseMap.h"17#include "llvm/ADT/SmallVector.h"18#include "llvm/CodeGen/MachineBasicBlock.h"19#include "llvm/CodeGen/MachineFrameInfo.h"20#include "llvm/CodeGen/MachineFunction.h"21#include "llvm/CodeGen/MachineInstr.h"22#include "llvm/CodeGen/MachineOperand.h"23#include "llvm/CodeGen/MachineRegisterInfo.h"24#include "llvm/CodeGen/RegisterClassInfo.h"25#include "llvm/CodeGen/ScheduleDAG.h"26#include "llvm/CodeGen/TargetInstrInfo.h"27#include "llvm/CodeGen/TargetRegisterInfo.h"28#include "llvm/CodeGen/TargetSubtargetInfo.h"29#include "llvm/MC/MCInstrDesc.h"30#include "llvm/MC/MCRegisterInfo.h"31#include "llvm/Support/Debug.h"32#include "llvm/Support/raw_ostream.h"33#include <cassert>34#include <utility>3536using namespace llvm;3738#define DEBUG_TYPE "post-RA-sched"3940CriticalAntiDepBreaker::CriticalAntiDepBreaker(MachineFunction &MFi,41const RegisterClassInfo &RCI)42: MF(MFi), MRI(MF.getRegInfo()), TII(MF.getSubtarget().getInstrInfo()),43TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RCI),44Classes(TRI->getNumRegs(), nullptr), KillIndices(TRI->getNumRegs(), 0),45DefIndices(TRI->getNumRegs(), 0), KeepRegs(TRI->getNumRegs(), false) {}4647CriticalAntiDepBreaker::~CriticalAntiDepBreaker() = default;4849void CriticalAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {50const unsigned BBSize = BB->size();51for (unsigned i = 1, e = TRI->getNumRegs(); i != e; ++i) {52// Clear out the register class data.53Classes[i] = nullptr;5455// Initialize the indices to indicate that no registers are live.56KillIndices[i] = ~0u;57DefIndices[i] = BBSize;58}5960// Clear "do not change" set.61KeepRegs.reset();6263bool IsReturnBlock = BB->isReturnBlock();6465// Examine the live-in regs of all successors.66for (const MachineBasicBlock *Succ : BB->successors())67for (const auto &LI : Succ->liveins()) {68for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI) {69unsigned Reg = *AI;70Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);71KillIndices[Reg] = BBSize;72DefIndices[Reg] = ~0u;73}74}7576// Mark live-out callee-saved registers. In a return block this is77// all callee-saved registers. In non-return this is any78// callee-saved register that is not saved in the prolog.79const MachineFrameInfo &MFI = MF.getFrameInfo();80BitVector Pristine = MFI.getPristineRegs(MF);81for (const MCPhysReg *I = MF.getRegInfo().getCalleeSavedRegs(); *I;82++I) {83unsigned Reg = *I;84if (!IsReturnBlock && !Pristine.test(Reg))85continue;86for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI) {87unsigned Reg = *AI;88Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);89KillIndices[Reg] = BBSize;90DefIndices[Reg] = ~0u;91}92}93}9495void CriticalAntiDepBreaker::FinishBlock() {96RegRefs.clear();97KeepRegs.reset();98}99100void CriticalAntiDepBreaker::Observe(MachineInstr &MI, unsigned Count,101unsigned InsertPosIndex) {102// Kill instructions can define registers but are really nops, and there might103// be a real definition earlier that needs to be paired with uses dominated by104// this kill.105106// FIXME: It may be possible to remove the isKill() restriction once PR18663107// has been properly fixed. There can be value in processing kills as seen in108// the AggressiveAntiDepBreaker class.109if (MI.isDebugInstr() || MI.isKill())110return;111assert(Count < InsertPosIndex && "Instruction index out of expected range!");112113for (unsigned Reg = 1; Reg != TRI->getNumRegs(); ++Reg) {114if (KillIndices[Reg] != ~0u) {115// If Reg is currently live, then mark that it can't be renamed as116// we don't know the extent of its live-range anymore (now that it117// has been scheduled).118Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);119KillIndices[Reg] = Count;120} else if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) {121// Any register which was defined within the previous scheduling region122// may have been rescheduled and its lifetime may overlap with registers123// in ways not reflected in our current liveness state. For each such124// register, adjust the liveness state to be conservatively correct.125Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);126127// Move the def index to the end of the previous region, to reflect128// that the def could theoretically have been scheduled at the end.129DefIndices[Reg] = InsertPosIndex;130}131}132133PrescanInstruction(MI);134ScanInstruction(MI, Count);135}136137/// CriticalPathStep - Return the next SUnit after SU on the bottom-up138/// critical path.139static const SDep *CriticalPathStep(const SUnit *SU) {140const SDep *Next = nullptr;141unsigned NextDepth = 0;142// Find the predecessor edge with the greatest depth.143for (const SDep &P : SU->Preds) {144const SUnit *PredSU = P.getSUnit();145unsigned PredLatency = P.getLatency();146unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;147// In the case of a latency tie, prefer an anti-dependency edge over148// other types of edges.149if (NextDepth < PredTotalLatency ||150(NextDepth == PredTotalLatency && P.getKind() == SDep::Anti)) {151NextDepth = PredTotalLatency;152Next = &P;153}154}155return Next;156}157158void CriticalAntiDepBreaker::PrescanInstruction(MachineInstr &MI) {159// It's not safe to change register allocation for source operands of160// instructions that have special allocation requirements. Also assume all161// registers used in a call must not be changed (ABI).162// FIXME: The issue with predicated instruction is more complex. We are being163// conservative here because the kill markers cannot be trusted after164// if-conversion:165// %r6 = LDR %sp, %reg0, 92, 14, %reg0; mem:LD4[FixedStack14]166// ...167// STR %r0, killed %r6, %reg0, 0, 0, %cpsr; mem:ST4[%395]168// %r6 = LDR %sp, %reg0, 100, 0, %cpsr; mem:LD4[FixedStack12]169// STR %r0, killed %r6, %reg0, 0, 14, %reg0; mem:ST4[%396](align=8)170//171// The first R6 kill is not really a kill since it's killed by a predicated172// instruction which may not be executed. The second R6 def may or may not173// re-define R6 so it's not safe to change it since the last R6 use cannot be174// changed.175bool Special =176MI.isCall() || MI.hasExtraSrcRegAllocReq() || TII->isPredicated(MI);177178// Scan the register operands for this instruction and update179// Classes and RegRefs.180for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {181MachineOperand &MO = MI.getOperand(i);182if (!MO.isReg()) continue;183Register Reg = MO.getReg();184if (Reg == 0) continue;185const TargetRegisterClass *NewRC = nullptr;186187if (i < MI.getDesc().getNumOperands())188NewRC = TII->getRegClass(MI.getDesc(), i, TRI, MF);189190// For now, only allow the register to be changed if its register191// class is consistent across all uses.192if (!Classes[Reg] && NewRC)193Classes[Reg] = NewRC;194else if (!NewRC || Classes[Reg] != NewRC)195Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);196197// Now check for aliases.198for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) {199// If an alias of the reg is used during the live range, give up.200// Note that this allows us to skip checking if AntiDepReg201// overlaps with any of the aliases, among other things.202unsigned AliasReg = *AI;203if (Classes[AliasReg]) {204Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);205Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);206}207}208209// If we're still willing to consider this register, note the reference.210if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))211RegRefs.insert(std::make_pair(Reg, &MO));212213if (MO.isUse() && Special) {214if (!KeepRegs.test(Reg)) {215for (MCPhysReg SubReg : TRI->subregs_inclusive(Reg))216KeepRegs.set(SubReg);217}218}219}220221for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {222const MachineOperand &MO = MI.getOperand(I);223if (!MO.isReg()) continue;224Register Reg = MO.getReg();225if (!Reg.isValid())226continue;227// If this reg is tied and live (Classes[Reg] is set to -1), we can't change228// it or any of its sub or super regs. We need to use KeepRegs to mark the229// reg because not all uses of the same reg within an instruction are230// necessarily tagged as tied.231// Example: an x86 "xor %eax, %eax" will have one source operand tied to the232// def register but not the second (see PR20020 for details).233// FIXME: can this check be relaxed to account for undef uses234// of a register? In the above 'xor' example, the uses of %eax are undef, so235// earlier instructions could still replace %eax even though the 'xor'236// itself can't be changed.237if (MI.isRegTiedToUseOperand(I) &&238Classes[Reg] == reinterpret_cast<TargetRegisterClass *>(-1)) {239for (MCPhysReg SubReg : TRI->subregs_inclusive(Reg)) {240KeepRegs.set(SubReg);241}242for (MCPhysReg SuperReg : TRI->superregs(Reg)) {243KeepRegs.set(SuperReg);244}245}246}247}248249void CriticalAntiDepBreaker::ScanInstruction(MachineInstr &MI, unsigned Count) {250// Update liveness.251// Proceeding upwards, registers that are defed but not used in this252// instruction are now dead.253assert(!MI.isKill() && "Attempting to scan a kill instruction");254255if (!TII->isPredicated(MI)) {256// Predicated defs are modeled as read + write, i.e. similar to two257// address updates.258for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {259MachineOperand &MO = MI.getOperand(i);260261if (MO.isRegMask()) {262auto ClobbersPhysRegAndSubRegs = [&](unsigned PhysReg) {263return all_of(TRI->subregs_inclusive(PhysReg),264[&](MCPhysReg SR) { return MO.clobbersPhysReg(SR); });265};266267for (unsigned i = 1, e = TRI->getNumRegs(); i != e; ++i) {268if (ClobbersPhysRegAndSubRegs(i)) {269DefIndices[i] = Count;270KillIndices[i] = ~0u;271KeepRegs.reset(i);272Classes[i] = nullptr;273RegRefs.erase(i);274}275}276}277278if (!MO.isReg()) continue;279Register Reg = MO.getReg();280if (Reg == 0) continue;281if (!MO.isDef()) continue;282283// Ignore two-addr defs.284if (MI.isRegTiedToUseOperand(i))285continue;286287// If we've already marked this reg as unchangeable, don't remove288// it or any of its subregs from KeepRegs.289bool Keep = KeepRegs.test(Reg);290291// For the reg itself and all subregs: update the def to current;292// reset the kill state, any restrictions, and references.293for (MCPhysReg SubregReg : TRI->subregs_inclusive(Reg)) {294DefIndices[SubregReg] = Count;295KillIndices[SubregReg] = ~0u;296Classes[SubregReg] = nullptr;297RegRefs.erase(SubregReg);298if (!Keep)299KeepRegs.reset(SubregReg);300}301// Conservatively mark super-registers as unusable.302for (MCPhysReg SR : TRI->superregs(Reg))303Classes[SR] = reinterpret_cast<TargetRegisterClass *>(-1);304}305}306for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {307MachineOperand &MO = MI.getOperand(i);308if (!MO.isReg()) continue;309Register Reg = MO.getReg();310if (Reg == 0) continue;311if (!MO.isUse()) continue;312313const TargetRegisterClass *NewRC = nullptr;314if (i < MI.getDesc().getNumOperands())315NewRC = TII->getRegClass(MI.getDesc(), i, TRI, MF);316317// For now, only allow the register to be changed if its register318// class is consistent across all uses.319if (!Classes[Reg] && NewRC)320Classes[Reg] = NewRC;321else if (!NewRC || Classes[Reg] != NewRC)322Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);323324RegRefs.insert(std::make_pair(Reg, &MO));325326// It wasn't previously live but now it is, this is a kill.327// Repeat for all aliases.328for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {329unsigned AliasReg = *AI;330if (KillIndices[AliasReg] == ~0u) {331KillIndices[AliasReg] = Count;332DefIndices[AliasReg] = ~0u;333}334}335}336}337338// Check all machine operands that reference the antidependent register and must339// be replaced by NewReg. Return true if any of their parent instructions may340// clobber the new register.341//342// Note: AntiDepReg may be referenced by a two-address instruction such that343// it's use operand is tied to a def operand. We guard against the case in which344// the two-address instruction also defines NewReg, as may happen with345// pre/postincrement loads. In this case, both the use and def operands are in346// RegRefs because the def is inserted by PrescanInstruction and not erased347// during ScanInstruction. So checking for an instruction with definitions of348// both NewReg and AntiDepReg covers it.349bool350CriticalAntiDepBreaker::isNewRegClobberedByRefs(RegRefIter RegRefBegin,351RegRefIter RegRefEnd,352unsigned NewReg) {353for (RegRefIter I = RegRefBegin; I != RegRefEnd; ++I ) {354MachineOperand *RefOper = I->second;355356// Don't allow the instruction defining AntiDepReg to earlyclobber its357// operands, in case they may be assigned to NewReg. In this case antidep358// breaking must fail, but it's too rare to bother optimizing.359if (RefOper->isDef() && RefOper->isEarlyClobber())360return true;361362// Handle cases in which this instruction defines NewReg.363MachineInstr *MI = RefOper->getParent();364for (const MachineOperand &CheckOper : MI->operands()) {365if (CheckOper.isRegMask() && CheckOper.clobbersPhysReg(NewReg))366return true;367368if (!CheckOper.isReg() || !CheckOper.isDef() ||369CheckOper.getReg() != NewReg)370continue;371372// Don't allow the instruction to define NewReg and AntiDepReg.373// When AntiDepReg is renamed it will be an illegal op.374if (RefOper->isDef())375return true;376377// Don't allow an instruction using AntiDepReg to be earlyclobbered by378// NewReg.379if (CheckOper.isEarlyClobber())380return true;381382// Don't allow inline asm to define NewReg at all. Who knows what it's383// doing with it.384if (MI->isInlineAsm())385return true;386}387}388return false;389}390391unsigned CriticalAntiDepBreaker::392findSuitableFreeRegister(RegRefIter RegRefBegin,393RegRefIter RegRefEnd,394unsigned AntiDepReg,395unsigned LastNewReg,396const TargetRegisterClass *RC,397SmallVectorImpl<unsigned> &Forbid) {398ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(RC);399for (unsigned NewReg : Order) {400// Don't replace a register with itself.401if (NewReg == AntiDepReg) continue;402// Don't replace a register with one that was recently used to repair403// an anti-dependence with this AntiDepReg, because that would404// re-introduce that anti-dependence.405if (NewReg == LastNewReg) continue;406// If any instructions that define AntiDepReg also define the NewReg, it's407// not suitable. For example, Instruction with multiple definitions can408// result in this condition.409if (isNewRegClobberedByRefs(RegRefBegin, RegRefEnd, NewReg)) continue;410// If NewReg is dead and NewReg's most recent def is not before411// AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.412assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u))413&& "Kill and Def maps aren't consistent for AntiDepReg!");414assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u))415&& "Kill and Def maps aren't consistent for NewReg!");416if (KillIndices[NewReg] != ~0u ||417Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) ||418KillIndices[AntiDepReg] > DefIndices[NewReg])419continue;420// If NewReg overlaps any of the forbidden registers, we can't use it.421bool Forbidden = false;422for (unsigned R : Forbid)423if (TRI->regsOverlap(NewReg, R)) {424Forbidden = true;425break;426}427if (Forbidden) continue;428return NewReg;429}430431// No registers are free and available!432return 0;433}434435unsigned CriticalAntiDepBreaker::436BreakAntiDependencies(const std::vector<SUnit> &SUnits,437MachineBasicBlock::iterator Begin,438MachineBasicBlock::iterator End,439unsigned InsertPosIndex,440DbgValueVector &DbgValues) {441// The code below assumes that there is at least one instruction,442// so just duck out immediately if the block is empty.443if (SUnits.empty()) return 0;444445// Keep a map of the MachineInstr*'s back to the SUnit representing them.446// This is used for updating debug information.447//448// FIXME: Replace this with the existing map in ScheduleDAGInstrs::MISUnitMap449DenseMap<MachineInstr *, const SUnit *> MISUnitMap;450451// Find the node at the bottom of the critical path.452const SUnit *Max = nullptr;453for (const SUnit &SU : SUnits) {454MISUnitMap[SU.getInstr()] = &SU;455if (!Max || SU.getDepth() + SU.Latency > Max->getDepth() + Max->Latency)456Max = &SU;457}458assert(Max && "Failed to find bottom of the critical path");459460#ifndef NDEBUG461{462LLVM_DEBUG(dbgs() << "Critical path has total latency "463<< (Max->getDepth() + Max->Latency) << "\n");464LLVM_DEBUG(dbgs() << "Available regs:");465for (unsigned Reg = 1; Reg < TRI->getNumRegs(); ++Reg) {466if (KillIndices[Reg] == ~0u)467LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI));468}469LLVM_DEBUG(dbgs() << '\n');470}471#endif472473// Track progress along the critical path through the SUnit graph as we walk474// the instructions.475const SUnit *CriticalPathSU = Max;476MachineInstr *CriticalPathMI = CriticalPathSU->getInstr();477478// Consider this pattern:479// A = ...480// ... = A481// A = ...482// ... = A483// A = ...484// ... = A485// A = ...486// ... = A487// There are three anti-dependencies here, and without special care,488// we'd break all of them using the same register:489// A = ...490// ... = A491// B = ...492// ... = B493// B = ...494// ... = B495// B = ...496// ... = B497// because at each anti-dependence, B is the first register that498// isn't A which is free. This re-introduces anti-dependencies499// at all but one of the original anti-dependencies that we were500// trying to break. To avoid this, keep track of the most recent501// register that each register was replaced with, avoid502// using it to repair an anti-dependence on the same register.503// This lets us produce this:504// A = ...505// ... = A506// B = ...507// ... = B508// C = ...509// ... = C510// B = ...511// ... = B512// This still has an anti-dependence on B, but at least it isn't on the513// original critical path.514//515// TODO: If we tracked more than one register here, we could potentially516// fix that remaining critical edge too. This is a little more involved,517// because unlike the most recent register, less recent registers should518// still be considered, though only if no other registers are available.519std::vector<unsigned> LastNewReg(TRI->getNumRegs(), 0);520521// Attempt to break anti-dependence edges on the critical path. Walk the522// instructions from the bottom up, tracking information about liveness523// as we go to help determine which registers are available.524unsigned Broken = 0;525unsigned Count = InsertPosIndex - 1;526for (MachineBasicBlock::iterator I = End, E = Begin; I != E; --Count) {527MachineInstr &MI = *--I;528// Kill instructions can define registers but are really nops, and there529// might be a real definition earlier that needs to be paired with uses530// dominated by this kill.531532// FIXME: It may be possible to remove the isKill() restriction once PR18663533// has been properly fixed. There can be value in processing kills as seen534// in the AggressiveAntiDepBreaker class.535if (MI.isDebugInstr() || MI.isKill())536continue;537538// Check if this instruction has a dependence on the critical path that539// is an anti-dependence that we may be able to break. If it is, set540// AntiDepReg to the non-zero register associated with the anti-dependence.541//542// We limit our attention to the critical path as a heuristic to avoid543// breaking anti-dependence edges that aren't going to significantly544// impact the overall schedule. There are a limited number of registers545// and we want to save them for the important edges.546//547// TODO: Instructions with multiple defs could have multiple548// anti-dependencies. The current code here only knows how to break one549// edge per instruction. Note that we'd have to be able to break all of550// the anti-dependencies in an instruction in order to be effective.551unsigned AntiDepReg = 0;552if (&MI == CriticalPathMI) {553if (const SDep *Edge = CriticalPathStep(CriticalPathSU)) {554const SUnit *NextSU = Edge->getSUnit();555556// Only consider anti-dependence edges.557if (Edge->getKind() == SDep::Anti) {558AntiDepReg = Edge->getReg();559assert(AntiDepReg != 0 && "Anti-dependence on reg0?");560if (!MRI.isAllocatable(AntiDepReg))561// Don't break anti-dependencies on non-allocatable registers.562AntiDepReg = 0;563else if (KeepRegs.test(AntiDepReg))564// Don't break anti-dependencies if a use down below requires565// this exact register.566AntiDepReg = 0;567else {568// If the SUnit has other dependencies on the SUnit that it569// anti-depends on, don't bother breaking the anti-dependency570// since those edges would prevent such units from being571// scheduled past each other regardless.572//573// Also, if there are dependencies on other SUnits with the574// same register as the anti-dependency, don't attempt to575// break it.576for (const SDep &P : CriticalPathSU->Preds)577if (P.getSUnit() == NextSU578? (P.getKind() != SDep::Anti || P.getReg() != AntiDepReg)579: (P.getKind() == SDep::Data &&580P.getReg() == AntiDepReg)) {581AntiDepReg = 0;582break;583}584}585}586CriticalPathSU = NextSU;587CriticalPathMI = CriticalPathSU->getInstr();588} else {589// We've reached the end of the critical path.590CriticalPathSU = nullptr;591CriticalPathMI = nullptr;592}593}594595PrescanInstruction(MI);596597SmallVector<unsigned, 2> ForbidRegs;598599// If MI's defs have a special allocation requirement, don't allow600// any def registers to be changed. Also assume all registers601// defined in a call must not be changed (ABI).602if (MI.isCall() || MI.hasExtraDefRegAllocReq() || TII->isPredicated(MI))603// If this instruction's defs have special allocation requirement, don't604// break this anti-dependency.605AntiDepReg = 0;606else if (AntiDepReg) {607// If this instruction has a use of AntiDepReg, breaking it608// is invalid. If the instruction defines other registers,609// save a list of them so that we don't pick a new register610// that overlaps any of them.611for (const MachineOperand &MO : MI.operands()) {612if (!MO.isReg()) continue;613Register Reg = MO.getReg();614if (Reg == 0) continue;615if (MO.isUse() && TRI->regsOverlap(AntiDepReg, Reg)) {616AntiDepReg = 0;617break;618}619if (MO.isDef() && Reg != AntiDepReg)620ForbidRegs.push_back(Reg);621}622}623624// Determine AntiDepReg's register class, if it is live and is625// consistently used within a single class.626const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg]627: nullptr;628assert((AntiDepReg == 0 || RC != nullptr) &&629"Register should be live if it's causing an anti-dependence!");630if (RC == reinterpret_cast<TargetRegisterClass *>(-1))631AntiDepReg = 0;632633// Look for a suitable register to use to break the anti-dependence.634//635// TODO: Instead of picking the first free register, consider which might636// be the best.637if (AntiDepReg != 0) {638std::pair<std::multimap<unsigned, MachineOperand *>::iterator,639std::multimap<unsigned, MachineOperand *>::iterator>640Range = RegRefs.equal_range(AntiDepReg);641if (unsigned NewReg = findSuitableFreeRegister(Range.first, Range.second,642AntiDepReg,643LastNewReg[AntiDepReg],644RC, ForbidRegs)) {645LLVM_DEBUG(dbgs() << "Breaking anti-dependence edge on "646<< printReg(AntiDepReg, TRI) << " with "647<< RegRefs.count(AntiDepReg) << " references"648<< " using " << printReg(NewReg, TRI) << "!\n");649650// Update the references to the old register to refer to the new651// register.652for (std::multimap<unsigned, MachineOperand *>::iterator653Q = Range.first, QE = Range.second; Q != QE; ++Q) {654Q->second->setReg(NewReg);655// If the SU for the instruction being updated has debug information656// related to the anti-dependency register, make sure to update that657// as well.658const SUnit *SU = MISUnitMap[Q->second->getParent()];659if (!SU) continue;660UpdateDbgValues(DbgValues, Q->second->getParent(),661AntiDepReg, NewReg);662}663664// We just went back in time and modified history; the665// liveness information for the anti-dependence reg is now666// inconsistent. Set the state as if it were dead.667Classes[NewReg] = Classes[AntiDepReg];668DefIndices[NewReg] = DefIndices[AntiDepReg];669KillIndices[NewReg] = KillIndices[AntiDepReg];670assert(((KillIndices[NewReg] == ~0u) !=671(DefIndices[NewReg] == ~0u)) &&672"Kill and Def maps aren't consistent for NewReg!");673674Classes[AntiDepReg] = nullptr;675DefIndices[AntiDepReg] = KillIndices[AntiDepReg];676KillIndices[AntiDepReg] = ~0u;677assert(((KillIndices[AntiDepReg] == ~0u) !=678(DefIndices[AntiDepReg] == ~0u)) &&679"Kill and Def maps aren't consistent for AntiDepReg!");680681RegRefs.erase(AntiDepReg);682LastNewReg[AntiDepReg] = NewReg;683++Broken;684}685}686687ScanInstruction(MI, Count);688}689690return Broken;691}692693AntiDepBreaker *694llvm::createCriticalAntiDepBreaker(MachineFunction &MFi,695const RegisterClassInfo &RCI) {696return new CriticalAntiDepBreaker(MFi, RCI);697}698699700