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,
382 const std::vector<Chain*> &
B) {
383 return A.front()->startsBefore(
B.front());
400 Changed |= colorChainSet(std::move(
I), MBB, Parity);
405 Chain *AArch64A57FPLoadBalancing::getAndEraseNext(
Color PreferredColor,
406 std::vector<Chain*> &L) {
418 const unsigned SizeFuzz = 1;
419 unsigned MinSize = L.front()->
size() - SizeFuzz;
420 for (
auto I = L.begin(),
E = L.end();
I !=
E; ++
I) {
421 if ((*I)->size() <= MinSize) {
428 if ((*I)->getPreferredColor() == PreferredColor) {
436 Chain *Ch = L.front();
441 bool AArch64A57FPLoadBalancing::colorChainSet(std::vector<Chain*> GV,
444 bool Changed =
false;
445 LLVM_DEBUG(
dbgs() <<
"colorChainSet(): #sets=" << GV.size() <<
"\n");
457 if (G1->
size() != G2->size())
458 return G1->
size() > G2->size();
463 "Starts before not total order!");
467 Color PreferredColor = Parity < 0 ? Color::Even : Color::Odd;
468 while (
Chain *
G = getAndEraseNext(PreferredColor, GV)) {
473 C =
G->getPreferredColor();
476 <<
", Color=" << ColorNames[(
int)C] <<
"\n");
481 if (
G->requiresFixup() && C !=
G->getPreferredColor()) {
482 C =
G->getPreferredColor();
484 <<
" - not worthwhile changing; " 486 << ColorNames[(int)C] <<
"\n");
489 Changed |= colorChain(
G, C, MBB);
491 Parity += (C == Color::Even) ?
G->size() : -
G->size();
492 PreferredColor = Parity < 0 ? Color::Even : Color::Odd;
498 int AArch64A57FPLoadBalancing::scavengeRegister(
Chain *
G,
Color C,
506 while (I != ChainEnd) {
513 assert(ChainBegin != ChainEnd &&
"Chain should contain instructions");
517 }
while (I != ChainBegin);
520 unsigned RegClassID = ChainBegin->getDesc().OpInfo[0].RegClass;
521 auto Ord = RCI.getOrder(
TRI->getRegClass(RegClassID));
522 for (
auto Reg : Ord) {
525 if (C == getColor(
Reg))
532 bool AArch64A57FPLoadBalancing::colorChain(
Chain *G,
Color C,
534 bool Changed =
false;
536 << ColorNames[(int)C] <<
")\n");
540 int Reg = scavengeRegister(G, C, MBB);
547 std::map<unsigned, unsigned> Substs;
549 if (!G->contains(
I) && (&
I != G->getKill() || G->isKillImmutable()))
554 std::vector<unsigned> ToErase;
555 for (
auto &U :
I.operands()) {
556 if (U.isReg() && U.isUse() && Substs.find(U.getReg()) != Substs.end()) {
557 unsigned OrigReg = U.getReg();
558 U.setReg(Substs[OrigReg]);
562 ToErase.push_back(OrigReg);
563 }
else if (U.isRegMask()) {
564 for (
auto J : Substs) {
565 if (U.clobbersPhysReg(J.first))
566 ToErase.push_back(J.first);
571 for (
auto J : ToErase)
575 if (&
I != G->getKill()) {
579 if (G->requiresFixup() && &
I == G->getLast())
590 assert(Substs.size() == 0 &&
"No substitutions should be left active!");
602 void AArch64A57FPLoadBalancing::scanInstruction(
603 MachineInstr *
MI,
unsigned Idx, std::map<unsigned, Chain *> &ActiveChains,
604 std::vector<std::unique_ptr<Chain>> &AllChains) {
609 for (
auto &
I : MI->
uses())
610 maybeKillChain(
I, Idx, ActiveChains);
611 for (
auto &
I : MI->
defs())
612 maybeKillChain(
I, Idx, ActiveChains);
621 auto G = llvm::make_unique<Chain>(
MI, Idx, getColor(DestReg));
622 ActiveChains[DestReg] = G.get();
623 AllChains.push_back(std::move(G));
625 }
else if (
isMla(MI)) {
632 maybeKillChain(MI->
getOperand(1), Idx, ActiveChains);
633 maybeKillChain(MI->
getOperand(2), Idx, ActiveChains);
634 if (DestReg != AccumReg)
635 maybeKillChain(MI->
getOperand(0), Idx, ActiveChains);
637 if (ActiveChains.find(AccumReg) != ActiveChains.end()) {
648 LLVM_DEBUG(
dbgs() <<
"Instruction was successfully added to chain.\n");
649 ActiveChains[AccumReg]->add(MI, Idx, getColor(DestReg));
651 if (DestReg != AccumReg) {
652 ActiveChains[DestReg] = ActiveChains[AccumReg];
653 ActiveChains.erase(AccumReg);
659 dbgs() <<
"Cannot add to chain because accumulator operand wasn't " 660 <<
"marked <kill>!\n");
661 maybeKillChain(MI->
getOperand(3), Idx, ActiveChains);
666 auto G = llvm::make_unique<Chain>(
MI, Idx, getColor(DestReg));
667 ActiveChains[DestReg] = G.get();
668 AllChains.push_back(std::move(G));
674 for (
auto &
I : MI->
uses())
675 maybeKillChain(
I, Idx, ActiveChains);
676 for (
auto &
I : MI->
defs())
677 maybeKillChain(
I, Idx, ActiveChains);
682 void AArch64A57FPLoadBalancing::
684 std::map<unsigned, Chain*> &ActiveChains) {
692 if (MO.
isKill() && ActiveChains.find(MO.
getReg()) != ActiveChains.end()) {
695 ActiveChains[MO.
getReg()]->setKill(MI, Idx, MO.
isTied());
697 ActiveChains.erase(MO.
getReg());
701 for (
auto I = ActiveChains.begin(),
E = ActiveChains.end();
706 I->second->setKill(MI, Idx,
true);
707 ActiveChains.erase(
I++);
715 Color AArch64A57FPLoadBalancing::getColor(
unsigned Reg) {
716 if ((
TRI->getEncodingValue(Reg) % 2) == 0)
724 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.
Compute iterated dominance frontiers using a linear time algorithm.
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()
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.
bool available(unsigned Reg) const
Returns true if no part of physical register Reg is live.
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)