48 #define DEBUG_TYPE "aarch64-a57-fp-load-balancing"
54 cl::desc(
"Always modify dest registers regardless of color"),
61 cl::desc(
"Ignore balance information, always return "
62 "(1: Even, 2: Odd)."),
71 case AArch64::FMULSrr:
72 case AArch64::FNMULSrr:
73 case AArch64::FMULDrr:
74 case AArch64::FNMULDrr:
84 case AArch64::FMSUBSrrr:
85 case AArch64::FMADDSrrr:
86 case AArch64::FNMSUBSrrr:
87 case AArch64::FNMADDSrrr:
88 case AArch64::FMSUBDrrr:
89 case AArch64::FMADDDrrr:
90 case AArch64::FNMSUBDrrr:
91 case AArch64::FNMADDDrrr:
105 static const char *ColorNames[2] = {
"Even",
"Odd" };
129 return "A57 FP Anti-dependency breaker";
144 std::map<unsigned, Chain*> &Active,
145 std::vector<std::unique_ptr<Chain>> &AllChains);
147 std::map<unsigned, Chain*> &RegChains);
149 Chain *getAndEraseNext(
Color PreferredColor, std::vector<Chain*> &
L);
156 "AArch64 A57 FP Load-Balancing",
false,
false)
205 : StartInst(MI), LastInst(MI), KillInst(nullptr),
206 StartInstIdx(Idx), LastInstIdx(Idx), KillInstIdx(0),
217 assert((KillInstIdx == 0 || LastInstIdx < KillInstIdx) &&
218 "Chain: broken invariant. A Chain can only be killed after its last "
237 KillIsImmutable = Immutable;
238 assert((KillInstIdx == 0 || LastInstIdx < KillInstIdx) &&
239 "Chain: broken invariant. A Chain can only be killed after its last "
268 unsigned End = KillInst ? KillInstIdx : LastInstIdx;
269 unsigned OtherEnd = Other.
KillInst ?
282 return (getKill() && isKillImmutable()) || !getKill();
291 StartInst->print(OS,
true);
293 LastInst->print(OS,
true);
296 KillInst->print(OS,
true);
317 bool Changed =
false;
318 DEBUG(
dbgs() <<
"***** AArch64A57FPLoadBalancing *****\n");
322 RCI.runOnMachineFunction(F);
324 for (
auto &MBB : F) {
332 bool Changed =
false;
333 DEBUG(
dbgs() <<
"Running on MBB: " << MBB <<
" - scanning instructions...\n");
340 std::map<unsigned, Chain*> ActiveChains;
341 std::vector<std::unique_ptr<Chain>> AllChains;
344 scanInstruction(&
MI, Idx++, ActiveChains, AllChains);
346 DEBUG(
dbgs() <<
"Scan complete, "<< AllChains.size() <<
" chains created.\n");
356 for (
auto &
I : AllChains)
359 for (
auto &
I : AllChains)
360 for (
auto &J : AllChains)
361 if (
I != J &&
I->rangeOverlapsWith(*J))
362 EC.unionSets(
I.get(), J.get());
363 DEBUG(
dbgs() <<
"Created " << EC.getNumClasses() <<
" disjoint sets.\n");
369 std::vector<std::vector<Chain*> > V;
370 for (
auto I = EC.begin(),
E = EC.end();
I !=
E; ++
I) {
371 std::vector<Chain*> Cs(EC.member_begin(
I), EC.member_end());
372 if (Cs.empty())
continue;
373 V.push_back(std::move(Cs));
379 [](
const std::vector<Chain*> &
A,
380 const std::vector<Chain*> &
B) {
381 return A.front()->startsBefore(
B.front());
398 Changed |= colorChainSet(std::move(
I), MBB, Parity);
403 Chain *AArch64A57FPLoadBalancing::getAndEraseNext(
Color PreferredColor,
404 std::vector<Chain*> &
L) {
416 const unsigned SizeFuzz = 1;
417 unsigned MinSize = L.front()->size() - SizeFuzz;
418 for (
auto I = L.begin(),
E = L.end();
I !=
E; ++
I) {
419 if ((*I)->size() <= MinSize) {
426 if ((*I)->getPreferredColor() == PreferredColor) {
434 Chain *Ch = L.front();
439 bool AArch64A57FPLoadBalancing::colorChainSet(std::vector<Chain*> GV,
442 bool Changed =
false;
443 DEBUG(
dbgs() <<
"colorChainSet(): #sets=" << GV.size() <<
"\n");
454 std::sort(GV.begin(), GV.end(), [](
const Chain *G1,
const Chain *G2) {
455 if (G1->size() != G2->size())
456 return G1->size() > G2->size();
457 if (G1->requiresFixup() != G2->requiresFixup())
458 return G1->requiresFixup() > G2->requiresFixup();
460 assert((G1 == G2 || (G1->startsBefore(G2) ^ G2->startsBefore(G1))) &&
461 "Starts before not total order!");
462 return G1->startsBefore(G2);
465 Color PreferredColor = Parity < 0 ? Color::Even : Color::Odd;
466 while (Chain *
G = getAndEraseNext(PreferredColor, GV)) {
471 C =
G->getPreferredColor();
473 DEBUG(
dbgs() <<
" - Parity=" << Parity <<
", Color="
474 << ColorNames[(
int)C] <<
"\n");
479 if (
G->requiresFixup() && C !=
G->getPreferredColor()) {
480 C =
G->getPreferredColor();
481 DEBUG(
dbgs() <<
" - " <<
G->str() <<
" - not worthwhile changing; "
482 "color remains " << ColorNames[(int)C] <<
"\n");
485 Changed |= colorChain(
G, C, MBB);
487 Parity += (C == Color::Even) ?
G->size() : -
G->size();
488 PreferredColor = Parity < 0 ? Color::Even : Color::Odd;
494 int AArch64A57FPLoadBalancing::scavengeRegister(Chain *
G,
Color C,
499 Units.addLiveOuts(MBB);
502 while (I != ChainEnd) {
504 Units.stepBackward(*I);
509 assert(ChainBegin != ChainEnd &&
"Chain should contain instructions");
512 Units.accumulate(*I);
513 }
while (I != ChainBegin);
516 unsigned RegClassID = ChainBegin->getDesc().OpInfo[0].RegClass;
517 auto Ord = RCI.getOrder(TRI->getRegClass(RegClassID));
518 for (
auto Reg : Ord) {
519 if (!Units.available(
Reg))
521 if (C == getColor(
Reg))
528 bool AArch64A57FPLoadBalancing::colorChain(Chain *G,
Color C,
530 bool Changed =
false;
531 DEBUG(
dbgs() <<
" - colorChain(" << G->str() <<
", "
532 << ColorNames[(int)C] <<
")\n");
536 int Reg = scavengeRegister(G, C, MBB);
538 DEBUG(
dbgs() <<
"Scavenging (thus coloring) failed!\n");
541 DEBUG(
dbgs() <<
" - Scavenged register: " << TRI->getName(Reg) <<
"\n");
543 std::map<unsigned, unsigned> Substs;
545 if (!G->contains(I) && (&I != G->getKill() || G->isKillImmutable()))
550 std::vector<unsigned> ToErase;
551 for (
auto &U : I.operands()) {
552 if (U.isReg() && U.isUse() && Substs.find(U.getReg()) != Substs.end()) {
553 unsigned OrigReg = U.getReg();
554 U.setReg(Substs[OrigReg]);
558 ToErase.push_back(OrigReg);
559 }
else if (U.isRegMask()) {
560 for (
auto J : Substs) {
561 if (U.clobbersPhysReg(J.first))
562 ToErase.push_back(J.first);
567 for (
auto J : ToErase)
571 if (&I != G->getKill()) {
575 if (G->requiresFixup() && &I == G->getLast())
586 assert(Substs.size() == 0 &&
"No substitutions should be left active!");
589 DEBUG(
dbgs() <<
" - Kill instruction seen.\n");
593 DEBUG(
dbgs() <<
" - Destination register not changed.\n");
598 void AArch64A57FPLoadBalancing::scanInstruction(
599 MachineInstr *
MI,
unsigned Idx, std::map<unsigned, Chain *> &ActiveChains,
600 std::vector<std::unique_ptr<Chain>> &AllChains) {
605 for (
auto &I : MI->
uses())
606 maybeKillChain(I, Idx, ActiveChains);
607 for (
auto &I : MI->
defs())
608 maybeKillChain(I, Idx, ActiveChains);
614 DEBUG(
dbgs() <<
"New chain started for register "
615 << TRI->getName(DestReg) <<
" at " << *
MI);
617 auto G = llvm::make_unique<Chain>(
MI, Idx, getColor(DestReg));
618 ActiveChains[DestReg] = G.get();
619 AllChains.push_back(std::move(G));
621 }
else if (
isMla(MI)) {
625 unsigned DestReg = MI->getOperand(0).getReg();
626 unsigned AccumReg = MI->getOperand(3).getReg();
628 maybeKillChain(MI->getOperand(1), Idx, ActiveChains);
629 maybeKillChain(MI->getOperand(2), Idx, ActiveChains);
630 if (DestReg != AccumReg)
631 maybeKillChain(MI->getOperand(0), Idx, ActiveChains);
633 if (ActiveChains.find(AccumReg) != ActiveChains.end()) {
634 DEBUG(
dbgs() <<
"Chain found for accumulator register "
635 << TRI->getName(AccumReg) <<
" in MI " << *
MI);
642 if (MI->getOperand(3).isKill()) {
644 DEBUG(
dbgs() <<
"Instruction was successfully added to chain.\n");
645 ActiveChains[AccumReg]->add(MI, Idx, getColor(DestReg));
647 if (DestReg != AccumReg) {
648 ActiveChains[DestReg] = ActiveChains[AccumReg];
649 ActiveChains.erase(AccumReg);
654 DEBUG(
dbgs() <<
"Cannot add to chain because accumulator operand wasn't "
655 <<
"marked <kill>!\n");
656 maybeKillChain(MI->getOperand(3), Idx, ActiveChains);
659 DEBUG(
dbgs() <<
"Creating new chain for dest register "
660 << TRI->getName(DestReg) <<
"\n");
661 auto G = llvm::make_unique<Chain>(
MI, Idx, getColor(DestReg));
662 ActiveChains[DestReg] = G.get();
663 AllChains.push_back(std::move(G));
669 for (
auto &I : MI->uses())
670 maybeKillChain(I, Idx, ActiveChains);
671 for (
auto &I : MI->defs())
672 maybeKillChain(I, Idx, ActiveChains);
677 void AArch64A57FPLoadBalancing::
679 std::map<unsigned, Chain*> &ActiveChains) {
687 if (MO.
isKill() && ActiveChains.find(MO.
getReg()) != ActiveChains.end()) {
690 ActiveChains[MO.
getReg()]->setKill(MI, Idx, MO.
isTied());
692 ActiveChains.erase(MO.
getReg());
696 for (
auto I = ActiveChains.begin(),
E = ActiveChains.end();
699 DEBUG(
dbgs() <<
"Kill (regmask) seen for chain "
700 << TRI->getName(I->first) <<
"\n");
701 I->second->setKill(MI, Idx,
true);
702 ActiveChains.erase(I++);
710 Color AArch64A57FPLoadBalancing::getColor(
unsigned Reg) {
711 if ((TRI->getEncodingValue(Reg) % 2) == 0)
719 return new AArch64A57FPLoadBalancing();
bool startsBefore(const Chain *Other) const
Return true if this chain starts before Other.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
MachineInstr * StartInst
The important (marker) instructions.
iterator_range< mop_iterator > uses()
Returns a range that includes all operands that are register uses.
static bool runOnBasicBlock(BasicBlock &BB)
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetRegisterInfo * getTargetRegisterInfo() const
bool balanceFPOps() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Reg
All possible values of the reg field in the ModR/M byte.
std::string str() const
Return a simple string representation of the chain.
INITIALIZE_PASS_BEGIN(AArch64A57FPLoadBalancing, DEBUG_TYPE,"AArch64 A57 FP Load-Balancing", false, false) INITIALIZE_PASS_END(AArch64A57FPLoadBalancing
MachineBasicBlock::iterator begin() const
FunctionPass * createAArch64A57FPLoadBalancing()
AArch64 A57 FP Load false
bool requiresFixup() const
Return true if the group will require a fixup MOV at the end.
static cl::opt< bool > TransformAll("aarch64-a57-fp-load-balancing-force-all", cl::desc("Always modify dest registers regardless of color"), cl::init(false), cl::Hidden)
bool contains(MachineInstr &MI)
Return true if MI is a member of the chain.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
void initializeAArch64A57FPLoadBalancingPass(PassRegistry &)
MachineInstr * getKill() const
Return the "kill" instruction (as set with setKill()) or NULL.
initializer< Ty > init(const Ty &Val)
MachineInstrBundleIterator< MachineInstr > iterator
This file declares the machine register scavenger class.
unsigned const MachineRegisterInfo * MRI
const MachineOperand & getOperand(unsigned i) const
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
EquivalenceClasses - This represents a collection of equivalence classes and supports three efficient...
Represent the analysis usage information of a pass.
iterator_range< mop_iterator > defs()
Returns a range over all explicit operands that are register definitions.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
bool isKillImmutable() const
Can the Kill instruction (assuming one exists) be modified?
static const unsigned End
bool rangeOverlapsWith(const Chain &Other) const
Return true if this chain (StartInst..KillInst) overlaps with Other.
FunctionPass class - This class is used to implement most global optimizations.
AArch64 A57 FP Load Balancing
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
Color LastColor
The "color" of LastInst.
void add(MachineInstr *MI, unsigned Idx, Color C)
Add a new instruction into the chain.
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
Color
A "color", which is either even or odd.
MachineOperand class - Representation of each machine instruction operand.
unsigned size() const
Return the number of instructions in the chain.
Promote Memory to Register
static bool isMla(MachineInstr *MI)
void setPreservesCFG()
This function should be called by the pass, iff they do not:
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
MachineInstr * getLast() const
Return the last instruction in the chain.
static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
std::set< MachineInstr * > Insts
All instructions in the chain.
MachineBasicBlock::iterator end() const
Return an instruction that can be used as an iterator for the end of the chain.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
bool KillIsImmutable
True if KillInst cannot be modified.
A Chain is a sequence of instructions that are linked together by an accumulation operand...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void setReg(unsigned Reg)
Change the register this operand corresponds to.
A set of register units used to track register liveness.
unsigned getReg() const
getReg - Returns the register number.
unsigned StartInstIdx
The index, from the start of the basic block, that each marker appears.
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...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
static cl::opt< unsigned > OverrideBalance("aarch64-a57-fp-load-balancing-override", cl::desc("Ignore balance information, always return ""(1: Even, 2: Odd)."), cl::init(0), cl::Hidden)
Color getPreferredColor()
Return the preferred color of this chain.
static bool isMul(MachineInstr *MI)
StringRef - Represent a constant reference to a string, i.e.
MachineInstr * getStart() const
Return the first instruction in the chain.
void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
Properties which a MachineFunction may have at a given point in time.
Chain(MachineInstr *MI, unsigned Idx, Color C)