Path: blob/main/contrib/llvm-project/llvm/lib/CodeGen/AggressiveAntiDepBreaker.cpp
35233 views
//===- AggressiveAntiDepBreaker.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 AggressiveAntiDepBreaker class, which9// implements register anti-dependence breaking during post-RA10// scheduling. It attempts to break all anti-dependencies within a11// block.12//13//===----------------------------------------------------------------------===//1415#include "AggressiveAntiDepBreaker.h"16#include "llvm/ADT/ArrayRef.h"17#include "llvm/ADT/SmallSet.h"18#include "llvm/ADT/iterator_range.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/CodeGenTypes/MachineValueType.h"30#include "llvm/MC/MCInstrDesc.h"31#include "llvm/MC/MCRegisterInfo.h"32#include "llvm/Support/CommandLine.h"33#include "llvm/Support/Debug.h"34#include "llvm/Support/raw_ostream.h"35#include <cassert>36#include <utility>3738using namespace llvm;3940#define DEBUG_TYPE "post-RA-sched"4142// If DebugDiv > 0 then only break antidep with (ID % DebugDiv) == DebugMod43static cl::opt<int>44DebugDiv("agg-antidep-debugdiv",45cl::desc("Debug control for aggressive anti-dep breaker"),46cl::init(0), cl::Hidden);4748static cl::opt<int>49DebugMod("agg-antidep-debugmod",50cl::desc("Debug control for aggressive anti-dep breaker"),51cl::init(0), cl::Hidden);5253AggressiveAntiDepState::AggressiveAntiDepState(const unsigned TargetRegs,54MachineBasicBlock *BB)55: NumTargetRegs(TargetRegs), GroupNodes(TargetRegs, 0),56GroupNodeIndices(TargetRegs, 0), KillIndices(TargetRegs, 0),57DefIndices(TargetRegs, 0) {58const unsigned BBSize = BB->size();59for (unsigned i = 0; i < NumTargetRegs; ++i) {60// Initialize all registers to be in their own group. Initially we61// assign the register to the same-indexed GroupNode.62GroupNodeIndices[i] = i;63// Initialize the indices to indicate that no registers are live.64KillIndices[i] = ~0u;65DefIndices[i] = BBSize;66}67}6869unsigned AggressiveAntiDepState::GetGroup(unsigned Reg) {70unsigned Node = GroupNodeIndices[Reg];71while (GroupNodes[Node] != Node)72Node = GroupNodes[Node];7374return Node;75}7677void AggressiveAntiDepState::GetGroupRegs(78unsigned Group,79std::vector<unsigned> &Regs,80std::multimap<unsigned, AggressiveAntiDepState::RegisterReference> *RegRefs)81{82for (unsigned Reg = 0; Reg != NumTargetRegs; ++Reg) {83if ((GetGroup(Reg) == Group) && (RegRefs->count(Reg) > 0))84Regs.push_back(Reg);85}86}8788unsigned AggressiveAntiDepState::UnionGroups(unsigned Reg1, unsigned Reg2) {89assert(GroupNodes[0] == 0 && "GroupNode 0 not parent!");90assert(GroupNodeIndices[0] == 0 && "Reg 0 not in Group 0!");9192// find group for each register93unsigned Group1 = GetGroup(Reg1);94unsigned Group2 = GetGroup(Reg2);9596// if either group is 0, then that must become the parent97unsigned Parent = (Group1 == 0) ? Group1 : Group2;98unsigned Other = (Parent == Group1) ? Group2 : Group1;99GroupNodes.at(Other) = Parent;100return Parent;101}102103unsigned AggressiveAntiDepState::LeaveGroup(unsigned Reg) {104// Create a new GroupNode for Reg. Reg's existing GroupNode must105// stay as is because there could be other GroupNodes referring to106// it.107unsigned idx = GroupNodes.size();108GroupNodes.push_back(idx);109GroupNodeIndices[Reg] = idx;110return idx;111}112113bool AggressiveAntiDepState::IsLive(unsigned Reg) {114// KillIndex must be defined and DefIndex not defined for a register115// to be live.116return((KillIndices[Reg] != ~0u) && (DefIndices[Reg] == ~0u));117}118119AggressiveAntiDepBreaker::AggressiveAntiDepBreaker(120MachineFunction &MFi, const RegisterClassInfo &RCI,121TargetSubtargetInfo::RegClassVector &CriticalPathRCs)122: MF(MFi), MRI(MF.getRegInfo()), TII(MF.getSubtarget().getInstrInfo()),123TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RCI) {124/* Collect a bitset of all registers that are only broken if they125are on the critical path. */126for (const TargetRegisterClass *RC : CriticalPathRCs) {127BitVector CPSet = TRI->getAllocatableSet(MF, RC);128if (CriticalPathSet.none())129CriticalPathSet = CPSet;130else131CriticalPathSet |= CPSet;132}133134LLVM_DEBUG(dbgs() << "AntiDep Critical-Path Registers:");135LLVM_DEBUG(for (unsigned r136: CriticalPathSet.set_bits()) dbgs()137<< " " << printReg(r, TRI));138LLVM_DEBUG(dbgs() << '\n');139}140141AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() {142delete State;143}144145void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {146assert(!State);147State = new AggressiveAntiDepState(TRI->getNumRegs(), BB);148149bool IsReturnBlock = BB->isReturnBlock();150std::vector<unsigned> &KillIndices = State->GetKillIndices();151std::vector<unsigned> &DefIndices = State->GetDefIndices();152153// Examine the live-in regs of all successors.154for (MachineBasicBlock *Succ : BB->successors())155for (const auto &LI : Succ->liveins()) {156for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI) {157unsigned Reg = *AI;158State->UnionGroups(Reg, 0);159KillIndices[Reg] = BB->size();160DefIndices[Reg] = ~0u;161}162}163164// Mark live-out callee-saved registers. In a return block this is165// all callee-saved registers. In non-return this is any166// callee-saved register that is not saved in the prolog.167const MachineFrameInfo &MFI = MF.getFrameInfo();168BitVector Pristine = MFI.getPristineRegs(MF);169for (const MCPhysReg *I = MF.getRegInfo().getCalleeSavedRegs(); *I;170++I) {171unsigned Reg = *I;172if (!IsReturnBlock && !Pristine.test(Reg))173continue;174for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {175unsigned AliasReg = *AI;176State->UnionGroups(AliasReg, 0);177KillIndices[AliasReg] = BB->size();178DefIndices[AliasReg] = ~0u;179}180}181}182183void AggressiveAntiDepBreaker::FinishBlock() {184delete State;185State = nullptr;186}187188void AggressiveAntiDepBreaker::Observe(MachineInstr &MI, unsigned Count,189unsigned InsertPosIndex) {190assert(Count < InsertPosIndex && "Instruction index out of expected range!");191192std::set<unsigned> PassthruRegs;193GetPassthruRegs(MI, PassthruRegs);194PrescanInstruction(MI, Count, PassthruRegs);195ScanInstruction(MI, Count);196197LLVM_DEBUG(dbgs() << "Observe: ");198LLVM_DEBUG(MI.dump());199LLVM_DEBUG(dbgs() << "\tRegs:");200201std::vector<unsigned> &DefIndices = State->GetDefIndices();202for (unsigned Reg = 1; Reg != TRI->getNumRegs(); ++Reg) {203// If Reg is current live, then mark that it can't be renamed as204// we don't know the extent of its live-range anymore (now that it205// has been scheduled). If it is not live but was defined in the206// previous schedule region, then set its def index to the most207// conservative location (i.e. the beginning of the previous208// schedule region).209if (State->IsLive(Reg)) {210LLVM_DEBUG(if (State->GetGroup(Reg) != 0) dbgs()211<< " " << printReg(Reg, TRI) << "=g" << State->GetGroup(Reg)212<< "->g0(region live-out)");213State->UnionGroups(Reg, 0);214} else if ((DefIndices[Reg] < InsertPosIndex)215&& (DefIndices[Reg] >= Count)) {216DefIndices[Reg] = Count;217}218}219LLVM_DEBUG(dbgs() << '\n');220}221222bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr &MI,223MachineOperand &MO) {224if (!MO.isReg() || !MO.isImplicit())225return false;226227Register Reg = MO.getReg();228if (Reg == 0)229return false;230231MachineOperand *Op = nullptr;232if (MO.isDef())233Op = MI.findRegisterUseOperand(Reg, /*TRI=*/nullptr, true);234else235Op = MI.findRegisterDefOperand(Reg, /*TRI=*/nullptr);236237return(Op && Op->isImplicit());238}239240void AggressiveAntiDepBreaker::GetPassthruRegs(241MachineInstr &MI, std::set<unsigned> &PassthruRegs) {242for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {243MachineOperand &MO = MI.getOperand(i);244if (!MO.isReg()) continue;245if ((MO.isDef() && MI.isRegTiedToUseOperand(i)) ||246IsImplicitDefUse(MI, MO)) {247const Register Reg = MO.getReg();248for (MCPhysReg SubReg : TRI->subregs_inclusive(Reg))249PassthruRegs.insert(SubReg);250}251}252}253254/// AntiDepEdges - Return in Edges the anti- and output- dependencies255/// in SU that we want to consider for breaking.256static void AntiDepEdges(const SUnit *SU, std::vector<const SDep *> &Edges) {257SmallSet<unsigned, 4> RegSet;258for (const SDep &Pred : SU->Preds) {259if ((Pred.getKind() == SDep::Anti) || (Pred.getKind() == SDep::Output)) {260if (RegSet.insert(Pred.getReg()).second)261Edges.push_back(&Pred);262}263}264}265266/// CriticalPathStep - Return the next SUnit after SU on the bottom-up267/// critical path.268static const SUnit *CriticalPathStep(const SUnit *SU) {269const SDep *Next = nullptr;270unsigned NextDepth = 0;271// Find the predecessor edge with the greatest depth.272if (SU) {273for (const SDep &Pred : SU->Preds) {274const SUnit *PredSU = Pred.getSUnit();275unsigned PredLatency = Pred.getLatency();276unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;277// In the case of a latency tie, prefer an anti-dependency edge over278// other types of edges.279if (NextDepth < PredTotalLatency ||280(NextDepth == PredTotalLatency && Pred.getKind() == SDep::Anti)) {281NextDepth = PredTotalLatency;282Next = &Pred;283}284}285}286287return (Next) ? Next->getSUnit() : nullptr;288}289290void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx,291const char *tag,292const char *header,293const char *footer) {294std::vector<unsigned> &KillIndices = State->GetKillIndices();295std::vector<unsigned> &DefIndices = State->GetDefIndices();296std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&297RegRefs = State->GetRegRefs();298299// FIXME: We must leave subregisters of live super registers as live, so that300// we don't clear out the register tracking information for subregisters of301// super registers we're still tracking (and with which we're unioning302// subregister definitions).303for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)304if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI)) {305LLVM_DEBUG(if (!header && footer) dbgs() << footer);306return;307}308309if (!State->IsLive(Reg)) {310KillIndices[Reg] = KillIdx;311DefIndices[Reg] = ~0u;312RegRefs.erase(Reg);313State->LeaveGroup(Reg);314LLVM_DEBUG(if (header) {315dbgs() << header << printReg(Reg, TRI);316header = nullptr;317});318LLVM_DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag);319// Repeat for subregisters. Note that we only do this if the superregister320// was not live because otherwise, regardless whether we have an explicit321// use of the subregister, the subregister's contents are needed for the322// uses of the superregister.323for (MCPhysReg SubregReg : TRI->subregs(Reg)) {324if (!State->IsLive(SubregReg)) {325KillIndices[SubregReg] = KillIdx;326DefIndices[SubregReg] = ~0u;327RegRefs.erase(SubregReg);328State->LeaveGroup(SubregReg);329LLVM_DEBUG(if (header) {330dbgs() << header << printReg(Reg, TRI);331header = nullptr;332});333LLVM_DEBUG(dbgs() << " " << printReg(SubregReg, TRI) << "->g"334<< State->GetGroup(SubregReg) << tag);335}336}337}338339LLVM_DEBUG(if (!header && footer) dbgs() << footer);340}341342void AggressiveAntiDepBreaker::PrescanInstruction(343MachineInstr &MI, unsigned Count, std::set<unsigned> &PassthruRegs) {344std::vector<unsigned> &DefIndices = State->GetDefIndices();345std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&346RegRefs = State->GetRegRefs();347348// Handle dead defs by simulating a last-use of the register just349// after the def. A dead def can occur because the def is truly350// dead, or because only a subregister is live at the def. If we351// don't do this the dead def will be incorrectly merged into the352// previous def.353for (const MachineOperand &MO : MI.all_defs()) {354Register Reg = MO.getReg();355if (Reg == 0) continue;356357HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n");358}359360LLVM_DEBUG(dbgs() << "\tDef Groups:");361for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {362MachineOperand &MO = MI.getOperand(i);363if (!MO.isReg() || !MO.isDef()) continue;364Register Reg = MO.getReg();365if (Reg == 0) continue;366367LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI) << "=g"368<< State->GetGroup(Reg));369370// If MI's defs have a special allocation requirement, don't allow371// any def registers to be changed. Also assume all registers372// defined in a call must not be changed (ABI). Inline assembly may373// reference either system calls or the register directly. Skip it until we374// can tell user specified registers from compiler-specified.375if (MI.isCall() || MI.hasExtraDefRegAllocReq() || TII->isPredicated(MI) ||376MI.isInlineAsm()) {377LLVM_DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");378State->UnionGroups(Reg, 0);379}380381// Any aliased that are live at this point are completely or382// partially defined here, so group those aliases with Reg.383for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) {384unsigned AliasReg = *AI;385if (State->IsLive(AliasReg)) {386State->UnionGroups(Reg, AliasReg);387LLVM_DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via "388<< printReg(AliasReg, TRI) << ")");389}390}391392// Note register reference...393const TargetRegisterClass *RC = nullptr;394if (i < MI.getDesc().getNumOperands())395RC = TII->getRegClass(MI.getDesc(), i, TRI, MF);396AggressiveAntiDepState::RegisterReference RR = { &MO, RC };397RegRefs.insert(std::make_pair(Reg, RR));398}399400LLVM_DEBUG(dbgs() << '\n');401402// Scan the register defs for this instruction and update403// live-ranges.404for (const MachineOperand &MO : MI.operands()) {405if (!MO.isReg() || !MO.isDef()) continue;406Register Reg = MO.getReg();407if (Reg == 0) continue;408// Ignore KILLs and passthru registers for liveness...409if (MI.isKill() || (PassthruRegs.count(Reg) != 0))410continue;411412// Update def for Reg and aliases.413for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {414// We need to be careful here not to define already-live super registers.415// If the super register is already live, then this definition is not416// a definition of the whole super register (just a partial insertion417// into it). Earlier subregister definitions (which we've not yet visited418// because we're iterating bottom-up) need to be linked to the same group419// as this definition.420if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI))421continue;422423DefIndices[*AI] = Count;424}425}426}427428void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr &MI,429unsigned Count) {430LLVM_DEBUG(dbgs() << "\tUse Groups:");431std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&432RegRefs = State->GetRegRefs();433434// If MI's uses have special allocation requirement, don't allow435// any use registers to be changed. Also assume all registers436// used in a call must not be changed (ABI).437// Inline Assembly register uses also cannot be safely changed.438// FIXME: The issue with predicated instruction is more complex. We are being439// conservatively here because the kill markers cannot be trusted after440// if-conversion:441// %r6 = LDR %sp, %reg0, 92, 14, %reg0; mem:LD4[FixedStack14]442// ...443// STR %r0, killed %r6, %reg0, 0, 0, %cpsr; mem:ST4[%395]444// %r6 = LDR %sp, %reg0, 100, 0, %cpsr; mem:LD4[FixedStack12]445// STR %r0, killed %r6, %reg0, 0, 14, %reg0; mem:ST4[%396](align=8)446//447// The first R6 kill is not really a kill since it's killed by a predicated448// instruction which may not be executed. The second R6 def may or may not449// re-define R6 so it's not safe to change it since the last R6 use cannot be450// changed.451bool Special = MI.isCall() || MI.hasExtraSrcRegAllocReq() ||452TII->isPredicated(MI) || MI.isInlineAsm();453454// Scan the register uses for this instruction and update455// live-ranges, groups and RegRefs.456for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {457MachineOperand &MO = MI.getOperand(i);458if (!MO.isReg() || !MO.isUse()) continue;459Register Reg = MO.getReg();460if (Reg == 0) continue;461462LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI) << "=g"463<< State->GetGroup(Reg));464465// It wasn't previously live but now it is, this is a kill. Forget466// the previous live-range information and start a new live-range467// for the register.468HandleLastUse(Reg, Count, "(last-use)");469470if (Special) {471LLVM_DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");472State->UnionGroups(Reg, 0);473}474475// Note register reference...476const TargetRegisterClass *RC = nullptr;477if (i < MI.getDesc().getNumOperands())478RC = TII->getRegClass(MI.getDesc(), i, TRI, MF);479AggressiveAntiDepState::RegisterReference RR = { &MO, RC };480RegRefs.insert(std::make_pair(Reg, RR));481}482483LLVM_DEBUG(dbgs() << '\n');484485// Form a group of all defs and uses of a KILL instruction to ensure486// that all registers are renamed as a group.487if (MI.isKill()) {488LLVM_DEBUG(dbgs() << "\tKill Group:");489490unsigned FirstReg = 0;491for (const MachineOperand &MO : MI.operands()) {492if (!MO.isReg()) continue;493Register Reg = MO.getReg();494if (Reg == 0) continue;495496if (FirstReg != 0) {497LLVM_DEBUG(dbgs() << "=" << printReg(Reg, TRI));498State->UnionGroups(FirstReg, Reg);499} else {500LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI));501FirstReg = Reg;502}503}504505LLVM_DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n');506}507}508509BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) {510BitVector BV(TRI->getNumRegs(), false);511bool first = true;512513// Check all references that need rewriting for Reg. For each, use514// the corresponding register class to narrow the set of registers515// that are appropriate for renaming.516for (const auto &Q : make_range(State->GetRegRefs().equal_range(Reg))) {517const TargetRegisterClass *RC = Q.second.RC;518if (!RC) continue;519520BitVector RCBV = TRI->getAllocatableSet(MF, RC);521if (first) {522BV |= RCBV;523first = false;524} else {525BV &= RCBV;526}527528LLVM_DEBUG(dbgs() << " " << TRI->getRegClassName(RC));529}530531return BV;532}533534bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(535unsigned SuperReg, unsigned AntiDepGroupIndex, RenameOrderType &RenameOrder,536std::map<unsigned, unsigned> &RenameMap) {537std::vector<unsigned> &KillIndices = State->GetKillIndices();538std::vector<unsigned> &DefIndices = State->GetDefIndices();539std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&540RegRefs = State->GetRegRefs();541542// Collect all referenced registers in the same group as543// AntiDepReg. These all need to be renamed together if we are to544// break the anti-dependence.545std::vector<unsigned> Regs;546State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs);547assert(!Regs.empty() && "Empty register group!");548if (Regs.empty())549return false;550551// Collect the BitVector of registers that can be used to rename552// each register.553LLVM_DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex554<< ":\n");555std::map<unsigned, BitVector> RenameRegisterMap;556for (unsigned Reg : Regs) {557// If Reg has any references, then collect possible rename regs558if (RegRefs.count(Reg) > 0) {559LLVM_DEBUG(dbgs() << "\t\t" << printReg(Reg, TRI) << ":");560561BitVector &BV = RenameRegisterMap[Reg];562assert(BV.empty());563BV = GetRenameRegisters(Reg);564565LLVM_DEBUG({566dbgs() << " ::";567for (unsigned r : BV.set_bits())568dbgs() << " " << printReg(r, TRI);569dbgs() << "\n";570});571}572}573574// All group registers should be a subreg of SuperReg.575for (unsigned Reg : Regs) {576if (Reg == SuperReg) continue;577bool IsSub = TRI->isSubRegister(SuperReg, Reg);578// FIXME: remove this once PR18663 has been properly fixed. For now,579// return a conservative answer:580// assert(IsSub && "Expecting group subregister");581if (!IsSub)582return false;583}584585#ifndef NDEBUG586// If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod587if (DebugDiv > 0) {588static int renamecnt = 0;589if (renamecnt++ % DebugDiv != DebugMod)590return false;591592dbgs() << "*** Performing rename " << printReg(SuperReg, TRI)593<< " for debug ***\n";594}595#endif596597// Check each possible rename register for SuperReg in round-robin598// order. If that register is available, and the corresponding599// registers are available for the other group subregisters, then we600// can use those registers to rename.601602// FIXME: Using getMinimalPhysRegClass is very conservative. We should603// check every use of the register and find the largest register class604// that can be used in all of them.605const TargetRegisterClass *SuperRC =606TRI->getMinimalPhysRegClass(SuperReg, MVT::Other);607608ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(SuperRC);609if (Order.empty()) {610LLVM_DEBUG(dbgs() << "\tEmpty Super Regclass!!\n");611return false;612}613614LLVM_DEBUG(dbgs() << "\tFind Registers:");615616RenameOrder.insert(RenameOrderType::value_type(SuperRC, Order.size()));617618unsigned OrigR = RenameOrder[SuperRC];619unsigned EndR = ((OrigR == Order.size()) ? 0 : OrigR);620unsigned R = OrigR;621do {622if (R == 0) R = Order.size();623--R;624const unsigned NewSuperReg = Order[R];625// Don't consider non-allocatable registers626if (!MRI.isAllocatable(NewSuperReg)) continue;627// Don't replace a register with itself.628if (NewSuperReg == SuperReg) continue;629630LLVM_DEBUG(dbgs() << " [" << printReg(NewSuperReg, TRI) << ':');631RenameMap.clear();632633// For each referenced group register (which must be a SuperReg or634// a subregister of SuperReg), find the corresponding subregister635// of NewSuperReg and make sure it is free to be renamed.636for (unsigned Reg : Regs) {637unsigned NewReg = 0;638if (Reg == SuperReg) {639NewReg = NewSuperReg;640} else {641unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg);642if (NewSubRegIdx != 0)643NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx);644}645646LLVM_DEBUG(dbgs() << " " << printReg(NewReg, TRI));647648// Check if Reg can be renamed to NewReg.649if (!RenameRegisterMap[Reg].test(NewReg)) {650LLVM_DEBUG(dbgs() << "(no rename)");651goto next_super_reg;652}653654// If NewReg is dead and NewReg's most recent def is not before655// Regs's kill, it's safe to replace Reg with NewReg. We656// must also check all aliases of NewReg, because we can't define a657// register when any sub or super is already live.658if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) {659LLVM_DEBUG(dbgs() << "(live)");660goto next_super_reg;661} else {662bool found = false;663for (MCRegAliasIterator AI(NewReg, TRI, false); AI.isValid(); ++AI) {664unsigned AliasReg = *AI;665if (State->IsLive(AliasReg) ||666(KillIndices[Reg] > DefIndices[AliasReg])) {667LLVM_DEBUG(dbgs()668<< "(alias " << printReg(AliasReg, TRI) << " live)");669found = true;670break;671}672}673if (found)674goto next_super_reg;675}676677// We cannot rename 'Reg' to 'NewReg' if one of the uses of 'Reg' also678// defines 'NewReg' via an early-clobber operand.679for (const auto &Q : make_range(RegRefs.equal_range(Reg))) {680MachineInstr *UseMI = Q.second.Operand->getParent();681int Idx = UseMI->findRegisterDefOperandIdx(NewReg, TRI, false, true);682if (Idx == -1)683continue;684685if (UseMI->getOperand(Idx).isEarlyClobber()) {686LLVM_DEBUG(dbgs() << "(ec)");687goto next_super_reg;688}689}690691// Also, we cannot rename 'Reg' to 'NewReg' if the instruction defining692// 'Reg' is an early-clobber define and that instruction also uses693// 'NewReg'.694for (const auto &Q : make_range(RegRefs.equal_range(Reg))) {695if (!Q.second.Operand->isDef() || !Q.second.Operand->isEarlyClobber())696continue;697698MachineInstr *DefMI = Q.second.Operand->getParent();699if (DefMI->readsRegister(NewReg, TRI)) {700LLVM_DEBUG(dbgs() << "(ec)");701goto next_super_reg;702}703}704705// Record that 'Reg' can be renamed to 'NewReg'.706RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg));707}708709// If we fall-out here, then every register in the group can be710// renamed, as recorded in RenameMap.711RenameOrder.erase(SuperRC);712RenameOrder.insert(RenameOrderType::value_type(SuperRC, R));713LLVM_DEBUG(dbgs() << "]\n");714return true;715716next_super_reg:717LLVM_DEBUG(dbgs() << ']');718} while (R != EndR);719720LLVM_DEBUG(dbgs() << '\n');721722// No registers are free and available!723return false;724}725726/// BreakAntiDependencies - Identifiy anti-dependencies within the727/// ScheduleDAG and break them by renaming registers.728unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(729const std::vector<SUnit> &SUnits,730MachineBasicBlock::iterator Begin,731MachineBasicBlock::iterator End,732unsigned InsertPosIndex,733DbgValueVector &DbgValues) {734std::vector<unsigned> &KillIndices = State->GetKillIndices();735std::vector<unsigned> &DefIndices = State->GetDefIndices();736std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&737RegRefs = State->GetRegRefs();738739// The code below assumes that there is at least one instruction,740// so just duck out immediately if the block is empty.741if (SUnits.empty()) return 0;742743// For each regclass the next register to use for renaming.744RenameOrderType RenameOrder;745746// ...need a map from MI to SUnit.747std::map<MachineInstr *, const SUnit *> MISUnitMap;748for (const SUnit &SU : SUnits)749MISUnitMap.insert(std::make_pair(SU.getInstr(), &SU));750751// Track progress along the critical path through the SUnit graph as752// we walk the instructions. This is needed for regclasses that only753// break critical-path anti-dependencies.754const SUnit *CriticalPathSU = nullptr;755MachineInstr *CriticalPathMI = nullptr;756if (CriticalPathSet.any()) {757for (const SUnit &SU : SUnits) {758if (!CriticalPathSU ||759((SU.getDepth() + SU.Latency) >760(CriticalPathSU->getDepth() + CriticalPathSU->Latency))) {761CriticalPathSU = &SU;762}763}764assert(CriticalPathSU && "Failed to find SUnit critical path");765CriticalPathMI = CriticalPathSU->getInstr();766}767768#ifndef NDEBUG769LLVM_DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n");770LLVM_DEBUG(dbgs() << "Available regs:");771for (unsigned Reg = 1; Reg < TRI->getNumRegs(); ++Reg) {772if (!State->IsLive(Reg))773LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI));774}775LLVM_DEBUG(dbgs() << '\n');776#endif777778BitVector RegAliases(TRI->getNumRegs());779780// Attempt to break anti-dependence edges. Walk the instructions781// from the bottom up, tracking information about liveness as we go782// to help determine which registers are available.783unsigned Broken = 0;784unsigned Count = InsertPosIndex - 1;785for (MachineBasicBlock::iterator I = End, E = Begin;786I != E; --Count) {787MachineInstr &MI = *--I;788789if (MI.isDebugInstr())790continue;791792LLVM_DEBUG(dbgs() << "Anti: ");793LLVM_DEBUG(MI.dump());794795std::set<unsigned> PassthruRegs;796GetPassthruRegs(MI, PassthruRegs);797798// Process the defs in MI...799PrescanInstruction(MI, Count, PassthruRegs);800801// The dependence edges that represent anti- and output-802// dependencies that are candidates for breaking.803std::vector<const SDep *> Edges;804const SUnit *PathSU = MISUnitMap[&MI];805AntiDepEdges(PathSU, Edges);806807// If MI is not on the critical path, then we don't rename808// registers in the CriticalPathSet.809BitVector *ExcludeRegs = nullptr;810if (&MI == CriticalPathMI) {811CriticalPathSU = CriticalPathStep(CriticalPathSU);812CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : nullptr;813} else if (CriticalPathSet.any()) {814ExcludeRegs = &CriticalPathSet;815}816817// Ignore KILL instructions (they form a group in ScanInstruction818// but don't cause any anti-dependence breaking themselves)819if (!MI.isKill()) {820// Attempt to break each anti-dependency...821for (const SDep *Edge : Edges) {822SUnit *NextSU = Edge->getSUnit();823824if ((Edge->getKind() != SDep::Anti) &&825(Edge->getKind() != SDep::Output)) continue;826827unsigned AntiDepReg = Edge->getReg();828LLVM_DEBUG(dbgs() << "\tAntidep reg: " << printReg(AntiDepReg, TRI));829assert(AntiDepReg != 0 && "Anti-dependence on reg0?");830831if (!MRI.isAllocatable(AntiDepReg)) {832// Don't break anti-dependencies on non-allocatable registers.833LLVM_DEBUG(dbgs() << " (non-allocatable)\n");834continue;835} else if (ExcludeRegs && ExcludeRegs->test(AntiDepReg)) {836// Don't break anti-dependencies for critical path registers837// if not on the critical path838LLVM_DEBUG(dbgs() << " (not critical-path)\n");839continue;840} else if (PassthruRegs.count(AntiDepReg) != 0) {841// If the anti-dep register liveness "passes-thru", then842// don't try to change it. It will be changed along with843// the use if required to break an earlier antidep.844LLVM_DEBUG(dbgs() << " (passthru)\n");845continue;846} else {847// No anti-dep breaking for implicit deps848MachineOperand *AntiDepOp =849MI.findRegisterDefOperand(AntiDepReg, /*TRI=*/nullptr);850assert(AntiDepOp && "Can't find index for defined register operand");851if (!AntiDepOp || AntiDepOp->isImplicit()) {852LLVM_DEBUG(dbgs() << " (implicit)\n");853continue;854}855856// If the SUnit has other dependencies on the SUnit that857// it anti-depends on, don't bother breaking the858// anti-dependency since those edges would prevent such859// units from being scheduled past each other860// regardless.861//862// Also, if there are dependencies on other SUnits with the863// same register as the anti-dependency, don't attempt to864// break it.865for (const SDep &Pred : PathSU->Preds) {866if (Pred.getSUnit() == NextSU ? (Pred.getKind() != SDep::Anti ||867Pred.getReg() != AntiDepReg)868: (Pred.getKind() == SDep::Data &&869Pred.getReg() == AntiDepReg)) {870AntiDepReg = 0;871break;872}873}874for (const SDep &Pred : PathSU->Preds) {875if ((Pred.getSUnit() == NextSU) && (Pred.getKind() != SDep::Anti) &&876(Pred.getKind() != SDep::Output)) {877LLVM_DEBUG(dbgs() << " (real dependency)\n");878AntiDepReg = 0;879break;880} else if ((Pred.getSUnit() != NextSU) &&881(Pred.getKind() == SDep::Data) &&882(Pred.getReg() == AntiDepReg)) {883LLVM_DEBUG(dbgs() << " (other dependency)\n");884AntiDepReg = 0;885break;886}887}888889if (AntiDepReg == 0)890continue;891}892893assert(AntiDepReg != 0);894895// Determine AntiDepReg's register group.896const unsigned GroupIndex = State->GetGroup(AntiDepReg);897if (GroupIndex == 0) {898LLVM_DEBUG(dbgs() << " (zero group)\n");899continue;900}901902LLVM_DEBUG(dbgs() << '\n');903904// Look for a suitable register to use to break the anti-dependence.905std::map<unsigned, unsigned> RenameMap;906if (FindSuitableFreeRegisters(AntiDepReg, GroupIndex, RenameOrder,907RenameMap)) {908LLVM_DEBUG(dbgs() << "\tBreaking anti-dependence edge on "909<< printReg(AntiDepReg, TRI) << ":");910911// Handle each group register...912for (const auto &P : RenameMap) {913unsigned CurrReg = P.first;914unsigned NewReg = P.second;915916LLVM_DEBUG(dbgs() << " " << printReg(CurrReg, TRI) << "->"917<< printReg(NewReg, TRI) << "("918<< RegRefs.count(CurrReg) << " refs)");919920// Update the references to the old register CurrReg to921// refer to the new register NewReg.922for (const auto &Q : make_range(RegRefs.equal_range(CurrReg))) {923Q.second.Operand->setReg(NewReg);924// If the SU for the instruction being updated has debug925// information related to the anti-dependency register, make926// sure to update that as well.927const SUnit *SU = MISUnitMap[Q.second.Operand->getParent()];928if (!SU) continue;929UpdateDbgValues(DbgValues, Q.second.Operand->getParent(),930AntiDepReg, NewReg);931}932933// We just went back in time and modified history; the934// liveness information for CurrReg is now inconsistent. Set935// the state as if it were dead.936State->UnionGroups(NewReg, 0);937RegRefs.erase(NewReg);938DefIndices[NewReg] = DefIndices[CurrReg];939KillIndices[NewReg] = KillIndices[CurrReg];940941State->UnionGroups(CurrReg, 0);942RegRefs.erase(CurrReg);943DefIndices[CurrReg] = KillIndices[CurrReg];944KillIndices[CurrReg] = ~0u;945assert(((KillIndices[CurrReg] == ~0u) !=946(DefIndices[CurrReg] == ~0u)) &&947"Kill and Def maps aren't consistent for AntiDepReg!");948}949950++Broken;951LLVM_DEBUG(dbgs() << '\n');952}953}954}955956ScanInstruction(MI, Count);957}958959return Broken;960}961962AntiDepBreaker *llvm::createAggressiveAntiDepBreaker(963MachineFunction &MFi, const RegisterClassInfo &RCI,964TargetSubtargetInfo::RegClassVector &CriticalPathRCs) {965return new AggressiveAntiDepBreaker(MFi, RCI, CriticalPathRCs);966}967968969