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 MachineRegisterInfo *MRI;
102 const TargetRegisterInfo *TRI;
103 RegisterClassInfo *RCI =
nullptr;
106 bool colorChainSet(std::vector<Chain *> GV, MachineBasicBlock &
MBB,
108 bool colorChain(Chain *
G, Color
C, MachineBasicBlock &
MBB);
109 int scavengeRegister(Chain *
G, Color
C, MachineBasicBlock &
MBB);
110 void scanInstruction(MachineInstr *
MI,
unsigned Idx,
111 std::map<unsigned, Chain *> &Active,
112 std::vector<std::unique_ptr<Chain>> &AllChains);
113 void maybeKillChain(MachineInstr &
MI, MachineOperand &MO,
unsigned Idx,
114 std::map<unsigned, Chain *> &RegChains);
116 Chain *getAndEraseNext(Color PreferredColor, std::vector<Chain *> &L);
122 explicit AArch64A57FPLoadBalancingLegacy() : MachineFunctionPass(ID) {}
126 MachineFunctionProperties getRequiredProperties()
const override {
127 return MachineFunctionProperties().setNoVRegs();
130 StringRef getPassName()
const override {
131 return "A57 FP Anti-dependency breaker";
134 void getAnalysisUsage(AnalysisUsage &AU)
const override {
136 AU.
addRequired<MachineRegisterClassInfoWrapperPass>();
142char AArch64A57FPLoadBalancingLegacy::ID = 0;
145 "AArch64 A57 FP Load-Balancing",
false,
false)
208 "Chain: broken invariant. A Chain can only be killed after its last "
229 "Chain: broken invariant. A Chain can only be killed after its last "
252 return Override == 1 ? Color::Even : Color::Odd;
259 unsigned OtherEnd =
Other.KillInst ?
302 if (!
ST.balanceFPOps())
304 CLOpts = &
ST.getCLOpts();
312 for (
auto &
MBB : MF) {
319bool AArch64A57FPLoadBalancingLegacy::runOnMachineFunction(
323 RegisterClassInfo *RCI =
324 &getAnalysis<MachineRegisterClassInfoWrapperPass>().getRCI();
325 return AArch64A57FPLoadBalancingImpl(RCI).run(MF);
332 if (AArch64A57FPLoadBalancingImpl(RCI).
run(MF)) {
343 <<
" - scanning instructions...\n");
350 std::map<unsigned, Chain*> ActiveChains;
351 std::vector<std::unique_ptr<Chain>> AllChains;
354 scanInstruction(&
MI, Idx++, ActiveChains, AllChains);
357 <<
" chains created.\n");
367 for (
auto &
I : AllChains)
370 for (
auto &
I : AllChains)
371 for (
auto &J : AllChains)
372 if (
I != J &&
I->rangeOverlapsWith(*J))
373 EC.unionSets(
I.get(), J.get());
374 LLVM_DEBUG(
dbgs() <<
"Created " << EC.getNumClasses() <<
" disjoint sets.\n");
380 std::vector<std::vector<Chain*> > V;
381 for (
const auto &E : EC) {
384 std::vector<Chain *> Cs(EC.member_begin(*E), EC.member_end());
385 if (Cs.empty())
continue;
386 V.push_back(std::move(Cs));
392 [](
const std::vector<Chain *> &
A,
const std::vector<Chain *> &
B) {
393 return A.front()->startsBefore(
B.front());
410 Changed |= colorChainSet(std::move(
I),
MBB, Parity);
415Chain *AArch64A57FPLoadBalancingImpl::getAndEraseNext(Color PreferredColor,
416 std::vector<Chain *> &L) {
428 const unsigned SizeFuzz = 1;
429 unsigned MinSize =
L.front()->size() - SizeFuzz;
430 for (
auto I =
L.begin(),
E =
L.end();
I !=
E; ++
I) {
431 if ((*I)->size() <= MinSize) {
438 if ((*I)->getPreferredColor(CLOpts->a57_fp_load_balancing_override) ==
447 Chain *Ch =
L.front();
452bool AArch64A57FPLoadBalancingImpl::colorChainSet(std::vector<Chain *> GV,
453 MachineBasicBlock &
MBB,
456 LLVM_DEBUG(
dbgs() <<
"colorChainSet(): #sets=" << GV.size() <<
"\n");
467 llvm::sort(GV, [](
const Chain *G1,
const Chain *G2) {
468 if (G1->size() != G2->size())
469 return G1->size() > G2->size();
470 if (G1->requiresFixup() != G2->requiresFixup())
471 return G1->requiresFixup() > G2->requiresFixup();
473 assert((G1 == G2 || (G1->startsBefore(G2) ^ G2->startsBefore(G1))) &&
474 "Starts before not total order!");
475 return G1->startsBefore(G2);
478 unsigned Override = CLOpts->a57_fp_load_balancing_override;
479 Color PreferredColor = Parity < 0 ? Color::Even : Color::Odd;
480 while (Chain *
G = getAndEraseNext(PreferredColor, GV)) {
482 Color
C = PreferredColor;
485 C =
G->getPreferredColor(Override);
488 <<
", Color=" << ColorNames[(
int)
C] <<
"\n");
493 if (
G->requiresFixup() &&
C !=
G->getPreferredColor(Override)) {
494 C =
G->getPreferredColor(Override);
496 <<
" - not worthwhile changing; "
498 << ColorNames[(
int)
C] <<
"\n");
503 Parity += (
C == Color::Even) ?
G->size() : -
G->size();
504 PreferredColor = Parity < 0 ? Color::Even : Color::Odd;
510int AArch64A57FPLoadBalancingImpl::scavengeRegister(Chain *
G, Color
C,
511 MachineBasicBlock &
MBB) {
514 LiveRegUnits Units(*
TRI);
515 Units.addLiveOuts(
MBB);
518 while (
I != ChainEnd) {
520 if (!
I->isDebugInstr())
521 Units.stepBackward(*
I);
526 assert(ChainBegin != ChainEnd &&
"Chain should contain instructions");
529 Units.accumulate(*
I);
530 }
while (
I != ChainBegin);
533 unsigned RegClassID = ChainBegin->getDesc().operands()[0].RegClass;
534 auto Ord = RCI->
getOrder(
TRI->getRegClass(RegClassID));
535 for (
auto Reg : Ord) {
536 if (!Units.available(
Reg))
538 if (
C == getColor(
Reg))
545bool AArch64A57FPLoadBalancingImpl::colorChain(Chain *
G, Color
C,
546 MachineBasicBlock &
MBB) {
549 << ColorNames[(
int)
C] <<
")\n");
553 int Reg = scavengeRegister(
G,
C,
MBB);
560 std::map<unsigned, unsigned> Substs;
561 for (MachineInstr &
I : *
G) {
562 if (!
G->contains(
I) && (&
I !=
G->getKill() ||
G->isKillImmutable()))
567 std::vector<unsigned> ToErase;
568 for (
auto &U :
I.operands()) {
569 if (
U.isReg() &&
U.isUse() && Substs.find(
U.getReg()) != Substs.end()) {
571 U.setReg(Substs[OrigReg]);
575 ToErase.push_back(OrigReg);
576 }
else if (
U.isRegMask()) {
577 for (
auto J : Substs) {
578 if (
U.clobbersPhysReg(J.first))
579 ToErase.push_back(J.first);
584 for (
auto J : ToErase)
588 if (&
I !=
G->getKill()) {
589 MachineOperand &MO =
I.getOperand(0);
592 CLOpts->a57_fp_load_balancing_force_all || getColor(MO.
getReg()) !=
C;
593 if (
G->requiresFixup() && &
I ==
G->getLast())
604 assert(Substs.size() == 0 &&
"No substitutions should be left active!");
616void AArch64A57FPLoadBalancingImpl::scanInstruction(
617 MachineInstr *
MI,
unsigned Idx, std::map<unsigned, Chain *> &ActiveChains,
618 std::vector<std::unique_ptr<Chain>> &AllChains) {
623 for (
auto &
I :
MI->uses())
624 maybeKillChain(*
MI,
I, Idx, ActiveChains);
625 for (
auto &
I :
MI->defs())
626 maybeKillChain(*
MI,
I, Idx, ActiveChains);
630 Register DestReg =
MI->getOperand(0).getReg();
635 auto G = std::make_unique<Chain>(
MI, Idx, getColor(DestReg));
636 ActiveChains[DestReg] =
G.get();
637 AllChains.push_back(std::move(
G));
643 Register DestReg =
MI->getOperand(0).getReg();
644 Register AccumReg =
MI->getOperand(3).getReg();
646 maybeKillChain(*
MI,
MI->getOperand(1), Idx, ActiveChains);
647 maybeKillChain(*
MI,
MI->getOperand(2), Idx, ActiveChains);
648 if (DestReg != AccumReg)
649 maybeKillChain(*
MI,
MI->getOperand(0), Idx, ActiveChains);
651 if (ActiveChains.find(AccumReg) != ActiveChains.end()) {
660 if (
MI->getOperand(3).isKill()) {
662 LLVM_DEBUG(
dbgs() <<
"Instruction was successfully added to chain.\n");
663 ActiveChains[AccumReg]->add(
MI, Idx, getColor(DestReg));
665 if (DestReg != AccumReg) {
666 ActiveChains[DestReg] = ActiveChains[AccumReg];
667 ActiveChains.erase(AccumReg);
673 dbgs() <<
"Cannot add to chain because accumulator operand wasn't "
674 <<
"marked <kill>!\n");
675 maybeKillChain(*
MI,
MI->getOperand(3), Idx, ActiveChains);
680 auto G = std::make_unique<Chain>(
MI, Idx, getColor(DestReg));
681 ActiveChains[DestReg] =
G.get();
682 AllChains.push_back(std::move(
G));
688 for (
auto &
I :
MI->uses())
689 maybeKillChain(*
MI,
I, Idx, ActiveChains);
690 for (
auto &
I :
MI->defs())
691 maybeKillChain(*
MI,
I, Idx, ActiveChains);
695void AArch64A57FPLoadBalancingImpl::maybeKillChain(
696 MachineInstr &
MI, MachineOperand &MO,
unsigned Idx,
697 std::map<unsigned, Chain *> &ActiveChains) {
703 if (MO.
isKill() && ActiveChains.find(MO.
getReg()) != ActiveChains.end()) {
708 ActiveChains.erase(MO.
getReg());
712 for (
auto I = ActiveChains.begin(),
E = ActiveChains.end();
717 I->second->setKill(&
MI, Idx,
true);
718 ActiveChains.erase(
I++);
726Color AArch64A57FPLoadBalancingImpl::getColor(
unsigned Reg) {
727 if ((
TRI->getEncodingValue(
Reg) % 2) == 0)
735 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.
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.