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 ?
272 return StartInstIdx <= OtherEnd && Other.
StartInstIdx <= End;
282 return (getKill() && isKillImmutable()) || !getKill();
291 StartInst->
print(OS,
true);
293 LastInst->
print(OS,
true);
296 KillInst->
print(OS,
true);
317 bool Changed =
false;
322 RCI.runOnMachineFunction(F);
324 for (
auto &MBB : F) {
332 bool Changed =
false;
334 <<
" - scanning instructions...\n");
341 std::map<unsigned, Chain*> ActiveChains;
342 std::vector<std::unique_ptr<Chain>> AllChains;
345 scanInstruction(&
MI, Idx++, ActiveChains, AllChains);
348 <<
" chains created.\n");
358 for (
auto &
I : AllChains)
361 for (
auto &
I : AllChains)
362 for (
auto &J : AllChains)
363 if (
I != J &&
I->rangeOverlapsWith(*J))
364 EC.unionSets(
I.get(), J.get());
365 LLVM_DEBUG(
dbgs() <<
"Created " << EC.getNumClasses() <<
" disjoint sets.\n");
371 std::vector<std::vector<Chain*> > V;
372 for (
auto I = EC.begin(),
E = EC.end();
I !=
E; ++
I) {
373 std::vector<Chain*> Cs(EC.member_begin(
I), EC.member_end());
374 if (Cs.empty())
continue;
375 V.push_back(std::move(Cs));
381 [](
const std::vector<Chain *> &A,
const std::vector<Chain *> &
B) {
382 return A.front()->startsBefore(B.front());
399 Changed |= colorChainSet(std::move(
I), MBB, Parity);
404 Chain *AArch64A57FPLoadBalancing::getAndEraseNext(
Color PreferredColor,
405 std::vector<Chain*> &L) {
417 const unsigned SizeFuzz = 1;
418 unsigned MinSize = L.front()->
size() - SizeFuzz;
419 for (
auto I = L.begin(),
E = L.end();
I !=
E; ++
I) {
420 if ((*I)->size() <= MinSize) {
427 if ((*I)->getPreferredColor() == PreferredColor) {
435 Chain *Ch = L.front();
440 bool AArch64A57FPLoadBalancing::colorChainSet(std::vector<Chain*> GV,
443 bool Changed =
false;
444 LLVM_DEBUG(
dbgs() <<
"colorChainSet(): #sets=" << GV.size() <<
"\n");
462 "Starts before not total order!");
466 Color PreferredColor = Parity < 0 ? Color::Even : Color::Odd;
467 while (
Chain *
G = getAndEraseNext(PreferredColor, GV)) {
472 C =
G->getPreferredColor();
475 <<
", Color=" << ColorNames[(
int)C] <<
"\n");
480 if (
G->requiresFixup() && C !=
G->getPreferredColor()) {
481 C =
G->getPreferredColor();
483 <<
" - not worthwhile changing; " 485 << ColorNames[(int)C] <<
"\n");
488 Changed |= colorChain(
G, C, MBB);
490 Parity += (C == Color::Even) ?
G->size() : -
G->size();
491 PreferredColor = Parity < 0 ? Color::Even : Color::Odd;
497 int AArch64A57FPLoadBalancing::scavengeRegister(
Chain *
G,
Color C,
505 while (I != ChainEnd) {
512 assert(ChainBegin != ChainEnd &&
"Chain should contain instructions");
516 }
while (I != ChainBegin);
519 unsigned RegClassID = ChainBegin->getDesc().OpInfo[0].RegClass;
520 auto Ord = RCI.getOrder(
TRI->getRegClass(RegClassID));
521 for (
auto Reg : Ord) {
524 if (C == getColor(
Reg))
531 bool AArch64A57FPLoadBalancing::colorChain(
Chain *G,
Color C,
533 bool Changed =
false;
535 << ColorNames[(int)C] <<
")\n");
539 int Reg = scavengeRegister(G, C, MBB);
546 std::map<unsigned, unsigned> Substs;
548 if (!G->contains(
I) && (&
I != G->getKill() || G->isKillImmutable()))
553 std::vector<unsigned> ToErase;
554 for (
auto &U :
I.operands()) {
555 if (U.isReg() && U.isUse() && Substs.find(U.getReg()) != Substs.end()) {
556 unsigned OrigReg = U.getReg();
557 U.setReg(Substs[OrigReg]);
561 ToErase.push_back(OrigReg);
562 }
else if (U.isRegMask()) {
563 for (
auto J : Substs) {
564 if (U.clobbersPhysReg(J.first))
565 ToErase.push_back(J.first);
570 for (
auto J : ToErase)
574 if (&
I != G->getKill()) {
578 if (G->requiresFixup() && &
I == G->getLast())
589 assert(Substs.size() == 0 &&
"No substitutions should be left active!");
601 void AArch64A57FPLoadBalancing::scanInstruction(
602 MachineInstr *
MI,
unsigned Idx, std::map<unsigned, Chain *> &ActiveChains,
603 std::vector<std::unique_ptr<Chain>> &AllChains) {
608 for (
auto &
I : MI->
uses())
609 maybeKillChain(
I, Idx, ActiveChains);
610 for (
auto &
I : MI->
defs())
611 maybeKillChain(
I, Idx, ActiveChains);
620 auto G = llvm::make_unique<Chain>(
MI, Idx, getColor(DestReg));
621 ActiveChains[DestReg] = G.get();
622 AllChains.push_back(std::move(G));
624 }
else if (
isMla(MI)) {
631 maybeKillChain(MI->
getOperand(1), Idx, ActiveChains);
632 maybeKillChain(MI->
getOperand(2), Idx, ActiveChains);
633 if (DestReg != AccumReg)
634 maybeKillChain(MI->
getOperand(0), Idx, ActiveChains);
636 if (ActiveChains.find(AccumReg) != ActiveChains.end()) {
647 LLVM_DEBUG(
dbgs() <<
"Instruction was successfully added to chain.\n");
648 ActiveChains[AccumReg]->add(MI, Idx, getColor(DestReg));
650 if (DestReg != AccumReg) {
651 ActiveChains[DestReg] = ActiveChains[AccumReg];
652 ActiveChains.erase(AccumReg);
658 dbgs() <<
"Cannot add to chain because accumulator operand wasn't " 659 <<
"marked <kill>!\n");
660 maybeKillChain(MI->
getOperand(3), Idx, ActiveChains);
665 auto G = llvm::make_unique<Chain>(
MI, Idx, getColor(DestReg));
666 ActiveChains[DestReg] = G.get();
667 AllChains.push_back(std::move(G));
673 for (
auto &
I : MI->
uses())
674 maybeKillChain(
I, Idx, ActiveChains);
675 for (
auto &
I : MI->
defs())
676 maybeKillChain(
I, Idx, ActiveChains);
681 void AArch64A57FPLoadBalancing::
683 std::map<unsigned, Chain*> &ActiveChains) {
691 if (MO.
isKill() && ActiveChains.find(MO.
getReg()) != ActiveChains.end()) {
694 ActiveChains[MO.
getReg()]->setKill(MI, Idx, MO.
isTied());
696 ActiveChains.erase(MO.
getReg());
700 for (
auto I = ActiveChains.begin(),
E = ActiveChains.end();
705 I->second->setKill(MI, Idx,
true);
706 ActiveChains.erase(
I++);
714 Color AArch64A57FPLoadBalancing::getColor(
unsigned Reg) {
715 if ((
TRI->getEncodingValue(Reg) % 2) == 0)
723 return new AArch64A57FPLoadBalancing();
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
MachineInstr * getKill() const
Return the "kill" instruction (as set with setKill()) or NULL.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
MachineInstr * StartInst
The important (marker) instructions.
This class represents lattice values for constants.
iterator_range< mop_iterator > uses()
Returns a range that includes all operands that are register uses.
bool requiresFixup() const
Return true if the group will require a fixup MOV at the end.
unsigned getReg() const
getReg - Returns the register number.
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)
unsigned const TargetRegisterInfo * TRI
bool rangeOverlapsWith(const Chain &Other) const
Return true if this chain (StartInst..KillInst) overlaps with Other.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void addLiveOuts(const MachineBasicBlock &MBB)
Adds registers living out of block MBB.
Printable printReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
bool startsBefore(const Chain *Other) const
Return true if this chain starts before Other.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
std::string str() const
Return a simple string representation of the chain.
bool isKillImmutable() const
Can the Kill instruction (assuming one exists) be modified?
FunctionPass * createAArch64A57FPLoadBalancing()
bool available(MCPhysReg Reg) const
Returns true if no part of physical register Reg is live.
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.
void initializeAArch64A57FPLoadBalancingPass(PassRegistry &)
initializer< Ty > init(const Ty &Val)
MachineInstrBundleIterator< MachineInstr > iterator
This file declares the machine register scavenger class.
const TargetRegisterInfo * getTargetRegisterInfo() const
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
bool balanceFPOps() const
EquivalenceClasses - This represents a collection of equivalence classes and supports three efficient...
Represent the analysis usage information of a pass.
void stepBackward(const MachineInstr &MI)
Updates liveness when stepping backwards over the instruction MI.
iterator_range< mop_iterator > defs()
Returns a range over all explicit operands that are register definitions.
FunctionPass class - This class is used to implement most global optimizations.
AArch64 A57 FP Load Balancing
void print(raw_ostream &OS, bool IsStandalone=true, bool SkipOpers=false, bool SkipDebugLoc=false, bool AddNewLine=true, const TargetInstrInfo *TII=nullptr) const
Print this MI to OS.
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.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
Color LastColor
The "color" of LastInst.
void add(MachineInstr *MI, unsigned Idx, Color C)
Add a new instruction into the chain.
void sort(IteratorTy Start, IteratorTy End)
Color
A "color", which is either even or odd.
MachineOperand class - Representation of each machine instruction operand.
Promote Memory to Register
static bool isMla(MachineInstr *MI)
MachineBasicBlock::iterator begin() const
void setPreservesCFG()
This function should be called by the pass, iff they do not:
MachineInstr * getStart() const
Return the first instruction in the chain.
const Function & getFunction() const
Return the LLVM function that this machine code represents.
void accumulate(const MachineInstr &MI)
Adds all register units used, defined or clobbered in MI.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
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.
static bool runOnBasicBlock(MachineBasicBlock *MBB, std::vector< StringRef > &bbNames, std::vector< unsigned > &renamedInOtherBB, unsigned &basicBlockNum, unsigned &VRegGapIndex, NamedVRegCursor &NVC)
MachineInstr * getLast() const
Return the last instruction in 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.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
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.
INITIALIZE_PASS_BEGIN(AArch64A57FPLoadBalancing, DEBUG_TYPE, "AArch64 A57 FP Load-Balancing", false, false) INITIALIZE_PASS_END(AArch64A57FPLoadBalancing
Color getPreferredColor()
Return the preferred color of this chain.
static bool isMul(MachineInstr *MI)
StringRef - Represent a constant reference to a string, i.e.
const MachineOperand & getOperand(unsigned i) const
MachineBasicBlock::iterator end() const
Return an instruction that can be used as an iterator for the end of the chain.
Properties which a MachineFunction may have at a given point in time.
unsigned size() const
Return the number of instructions in the chain.
Chain(MachineInstr *MI, unsigned Idx, Color C)