46#define DEBUG_TYPE "aarch64-a57-fp-load-balancing"
53 switch (
MI->getOpcode()) {
54 case AArch64::FMULSrr:
55 case AArch64::FNMULSrr:
56 case AArch64::FMULDrr:
57 case AArch64::FNMULDrr:
66 switch (
MI->getOpcode()) {
67 case AArch64::FMSUBSrrr:
68 case AArch64::FMADDSrrr:
69 case AArch64::FNMSUBSrrr:
70 case AArch64::FNMADDSrrr:
71 case AArch64::FMSUBDrrr:
72 case AArch64::FMADDDrrr:
73 case AArch64::FNMSUBDrrr:
74 case AArch64::FNMADDDrrr:
86enum class Color { Even, Odd };
88static const char *ColorNames[2] = {
"Even",
"Odd" };
93class AArch64A57FPLoadBalancingImpl {
95 explicit AArch64A57FPLoadBalancingImpl(RegisterClassInfo *RCI) : RCI(RCI) {}
100 const AArch64Options *CLOpts;
101 const TargetRegisterInfo *TRI;
102 RegisterClassInfo *RCI =
nullptr;
105 bool colorChainSet(std::vector<Chain *> GV, MachineBasicBlock &
MBB,
107 bool colorChain(Chain *
G, Color
C, MachineBasicBlock &
MBB);
108 int scavengeRegister(Chain *
G, Color
C, MachineBasicBlock &
MBB);
109 void scanInstruction(MachineInstr *
MI,
unsigned Idx,
110 std::map<unsigned, Chain *> &Active,
111 std::vector<std::unique_ptr<Chain>> &AllChains);
112 void maybeKillChain(MachineInstr &
MI, MachineOperand &MO,
unsigned Idx,
113 std::map<unsigned, Chain *> &RegChains);
115 Chain *getAndEraseNext(Color PreferredColor, std::vector<Chain *> &L);
121 explicit AArch64A57FPLoadBalancingLegacy() : MachineFunctionPass(ID) {}
125 MachineFunctionProperties getRequiredProperties()
const override {
126 return MachineFunctionProperties().setNoVRegs();
129 StringRef getPassName()
const override {
130 return "A57 FP Anti-dependency breaker";
133 void getAnalysisUsage(AnalysisUsage &AU)
const override {
135 AU.
addRequired<MachineRegisterClassInfoWrapperPass>();
141char AArch64A57FPLoadBalancingLegacy::ID = 0;
144 "AArch64 A57 FP Load-Balancing",
false,
false)
207 "Chain: broken invariant. A Chain can only be killed after its last "
228 "Chain: broken invariant. A Chain can only be killed after its last "
251 return Override == 1 ? Color::Even : Color::Odd;
258 unsigned OtherEnd =
Other.KillInst ?
301 if (!
ST.balanceFPOps())
303 CLOpts = &
ST.getCLOpts();
310 for (
auto &
MBB : MF) {
317bool AArch64A57FPLoadBalancingLegacy::runOnMachineFunction(
321 RegisterClassInfo *RCI =
322 &getAnalysis<MachineRegisterClassInfoWrapperPass>().getRCI();
323 return AArch64A57FPLoadBalancingImpl(RCI).run(MF);
330 if (AArch64A57FPLoadBalancingImpl(RCI).
run(MF)) {
341 <<
" - scanning instructions...\n");
348 std::map<unsigned, Chain*> ActiveChains;
349 std::vector<std::unique_ptr<Chain>> AllChains;
352 scanInstruction(&
MI, Idx++, ActiveChains, AllChains);
355 <<
" chains created.\n");
365 for (
auto &
I : AllChains)
368 for (
auto &
I : AllChains)
369 for (
auto &J : AllChains)
370 if (
I != J &&
I->rangeOverlapsWith(*J))
371 EC.unionSets(
I.get(), J.get());
372 LLVM_DEBUG(
dbgs() <<
"Created " << EC.getNumClasses() <<
" disjoint sets.\n");
378 std::vector<std::vector<Chain*> > V;
379 for (
const auto &E : EC) {
382 std::vector<Chain *> Cs(EC.member_begin(*E), EC.member_end());
383 if (Cs.empty())
continue;
384 V.push_back(std::move(Cs));
390 [](
const std::vector<Chain *> &
A,
const std::vector<Chain *> &
B) {
391 return A.front()->startsBefore(
B.front());
408 Changed |= colorChainSet(std::move(
I),
MBB, Parity);
413Chain *AArch64A57FPLoadBalancingImpl::getAndEraseNext(Color PreferredColor,
414 std::vector<Chain *> &L) {
426 const unsigned SizeFuzz = 1;
427 unsigned MinSize =
L.front()->size() - SizeFuzz;
428 for (
auto I =
L.begin(),
E =
L.end();
I !=
E; ++
I) {
429 if ((*I)->size() <= MinSize) {
436 if ((*I)->getPreferredColor(CLOpts->a57_fp_load_balancing_override) ==
445 Chain *Ch =
L.front();
450bool AArch64A57FPLoadBalancingImpl::colorChainSet(std::vector<Chain *> GV,
451 MachineBasicBlock &
MBB,
454 LLVM_DEBUG(
dbgs() <<
"colorChainSet(): #sets=" << GV.size() <<
"\n");
465 llvm::sort(GV, [](
const Chain *G1,
const Chain *G2) {
466 if (G1->size() != G2->size())
467 return G1->size() > G2->size();
468 if (G1->requiresFixup() != G2->requiresFixup())
469 return G1->requiresFixup() > G2->requiresFixup();
471 assert((G1 == G2 || (G1->startsBefore(G2) ^ G2->startsBefore(G1))) &&
472 "Starts before not total order!");
473 return G1->startsBefore(G2);
476 unsigned Override = CLOpts->a57_fp_load_balancing_override;
477 Color PreferredColor = Parity < 0 ? Color::Even : Color::Odd;
478 while (Chain *
G = getAndEraseNext(PreferredColor, GV)) {
480 Color
C = PreferredColor;
483 C =
G->getPreferredColor(Override);
486 <<
", Color=" << ColorNames[(
int)
C] <<
"\n");
491 if (
G->requiresFixup() &&
C !=
G->getPreferredColor(Override)) {
492 C =
G->getPreferredColor(Override);
494 <<
" - not worthwhile changing; "
496 << ColorNames[(
int)
C] <<
"\n");
501 Parity += (
C == Color::Even) ?
G->size() : -
G->size();
502 PreferredColor = Parity < 0 ? Color::Even : Color::Odd;
508int AArch64A57FPLoadBalancingImpl::scavengeRegister(Chain *
G, Color
C,
509 MachineBasicBlock &
MBB) {
512 LiveRegUnits Units(*
TRI);
513 Units.addLiveOuts(
MBB);
516 while (
I != ChainEnd) {
518 if (!
I->isDebugInstr())
519 Units.stepBackward(*
I);
524 assert(ChainBegin != ChainEnd &&
"Chain should contain instructions");
527 Units.accumulate(*
I);
528 }
while (
I != ChainBegin);
531 unsigned RegClassID = ChainBegin->getDesc().operands()[0].RegClass;
532 auto Ord = RCI->
getOrder(
TRI->getRegClass(RegClassID));
533 for (
auto Reg : Ord) {
534 if (!Units.available(
Reg))
536 if (
C == getColor(
Reg))
543bool AArch64A57FPLoadBalancingImpl::colorChain(Chain *
G, Color
C,
544 MachineBasicBlock &
MBB) {
547 << ColorNames[(
int)
C] <<
")\n");
551 int Reg = scavengeRegister(
G,
C,
MBB);
558 std::map<unsigned, unsigned> Substs;
559 for (MachineInstr &
I : *
G) {
560 if (!
G->contains(
I) && (&
I !=
G->getKill() ||
G->isKillImmutable()))
565 std::vector<unsigned> ToErase;
566 for (
auto &U :
I.operands()) {
567 if (
U.isReg() &&
U.isUse() && Substs.find(
U.getReg()) != Substs.end()) {
569 U.setReg(Substs[OrigReg]);
573 ToErase.push_back(OrigReg);
574 }
else if (
U.isRegMask()) {
575 for (
auto J : Substs) {
576 if (
U.clobbersPhysReg(J.first))
577 ToErase.push_back(J.first);
582 for (
auto J : ToErase)
586 if (&
I !=
G->getKill()) {
587 MachineOperand &MO =
I.getOperand(0);
590 CLOpts->a57_fp_load_balancing_force_all || getColor(MO.
getReg()) !=
C;
591 if (
G->requiresFixup() && &
I ==
G->getLast())
602 assert(Substs.size() == 0 &&
"No substitutions should be left active!");
614void AArch64A57FPLoadBalancingImpl::scanInstruction(
615 MachineInstr *
MI,
unsigned Idx, std::map<unsigned, Chain *> &ActiveChains,
616 std::vector<std::unique_ptr<Chain>> &AllChains) {
621 for (
auto &
I :
MI->uses())
622 maybeKillChain(*
MI,
I, Idx, ActiveChains);
623 for (
auto &
I :
MI->defs())
624 maybeKillChain(*
MI,
I, Idx, ActiveChains);
628 Register DestReg =
MI->getOperand(0).getReg();
633 auto G = std::make_unique<Chain>(
MI, Idx, getColor(DestReg));
634 ActiveChains[DestReg] =
G.get();
635 AllChains.push_back(std::move(
G));
641 Register DestReg =
MI->getOperand(0).getReg();
642 Register AccumReg =
MI->getOperand(3).getReg();
644 maybeKillChain(*
MI,
MI->getOperand(1), Idx, ActiveChains);
645 maybeKillChain(*
MI,
MI->getOperand(2), Idx, ActiveChains);
646 if (DestReg != AccumReg)
647 maybeKillChain(*
MI,
MI->getOperand(0), Idx, ActiveChains);
649 if (ActiveChains.find(AccumReg) != ActiveChains.end()) {
658 if (
MI->getOperand(3).isKill()) {
660 LLVM_DEBUG(
dbgs() <<
"Instruction was successfully added to chain.\n");
661 ActiveChains[AccumReg]->add(
MI, Idx, getColor(DestReg));
663 if (DestReg != AccumReg) {
664 ActiveChains[DestReg] = ActiveChains[AccumReg];
665 ActiveChains.erase(AccumReg);
671 dbgs() <<
"Cannot add to chain because accumulator operand wasn't "
672 <<
"marked <kill>!\n");
673 maybeKillChain(*
MI,
MI->getOperand(3), Idx, ActiveChains);
678 auto G = std::make_unique<Chain>(
MI, Idx, getColor(DestReg));
679 ActiveChains[DestReg] =
G.get();
680 AllChains.push_back(std::move(
G));
686 for (
auto &
I :
MI->uses())
687 maybeKillChain(*
MI,
I, Idx, ActiveChains);
688 for (
auto &
I :
MI->defs())
689 maybeKillChain(*
MI,
I, Idx, ActiveChains);
693void AArch64A57FPLoadBalancingImpl::maybeKillChain(
694 MachineInstr &
MI, MachineOperand &MO,
unsigned Idx,
695 std::map<unsigned, Chain *> &ActiveChains) {
701 if (MO.
isKill() && ActiveChains.find(MO.
getReg()) != ActiveChains.end()) {
706 ActiveChains.erase(MO.
getReg());
710 for (
auto I = ActiveChains.begin(),
E = ActiveChains.end();
715 I->second->setKill(&
MI, Idx,
true);
716 ActiveChains.erase(
I++);
724Color AArch64A57FPLoadBalancingImpl::getColor(
unsigned Reg) {
725 if ((
TRI->getEncodingValue(
Reg) % 2) == 0)
733 return new AArch64A57FPLoadBalancingLegacy();
static bool isMul(MachineInstr *MI)
static bool isMla(MachineInstr *MI)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Generic implementation of equivalence classes through the use Tarjan's efficient union-find algorithm...
static bool runOnBasicBlock(MachineBasicBlock *MBB, unsigned BasicBlockNum, VRegRenamer &Renamer)
Register const TargetRegisterInfo * TRI
Promote Memory to Register
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file declares the machine register scavenger class.
MachineInstr * StartInst
The important (marker) instructions.
MachineBasicBlock::iterator begin() const
bool isKillImmutable() const
Can the Kill instruction (assuming one exists) be modified?
void add(MachineInstr *MI, unsigned Idx, Color C)
Add a new instruction into the chain.
bool contains(MachineInstr &MI)
Return true if MI is a member of the chain.
Color LastColor
The "color" of LastInst.
bool requiresFixup() const
Return true if the group will require a fixup MOV at the end.
MachineInstr * getLast() const
Return the last instruction in the chain.
bool KillIsImmutable
True if KillInst cannot be modified.
bool rangeOverlapsWith(const Chain &Other) const
Return true if this chain (StartInst..KillInst) overlaps with Other.
MachineInstr * getStart() const
Return the first instruction in the chain.
unsigned size() const
Return the number of instructions in the chain.
MachineBasicBlock::iterator end() const
Return an instruction that can be used as an iterator for the end of the chain.
void setKill(MachineInstr *MI, unsigned Idx, bool Immutable)
Inform the chain that its last active register (the dest register of LastInst) is killed by MI with n...
MachineInstr * getKill() const
Return the "kill" instruction (as set with setKill()) or NULL.
unsigned StartInstIdx
The index, from the start of the basic block, that each marker appears.
std::string str() const
Return a simple string representation of the chain.
Color getPreferredColor(unsigned Override)
Return the preferred color of this chain.
std::set< MachineInstr * > Insts
All instructions in the chain.
Chain(MachineInstr *MI, unsigned Idx, Color C)
bool startsBefore(const Chain *Other) const
Return true if this chain starts before Other.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represents analyses that only rely on functions' control flow.
This represents a collection of equivalence classes and supports three efficient operations: insert a...
FunctionPass class - This class is used to implement most global optimizations.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
Register getReg() const
getReg - Returns the register number.
static bool clobbersPhysReg(const uint32_t *RegMask, MCRegister PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
const TargetRegisterInfo * getTargetRegisterInfo() const
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
ArrayRef< MCPhysReg > getOrder(const TargetRegisterClass *RC) const
getOrder - Returns the preferred allocation order for RC.
A raw_ostream that writes to an std::string.
std::string & str()
Returns the string's reference.
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
FunctionPass * createAArch64A57FPLoadBalancingLegacyPass()
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
void sort(IteratorTy Start, IteratorTy End)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
@ End
Run the machine verifier once, at the end of the pipeline.
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.