#include "AArch64.h"
#include "AArch64InstrInfo.h"
#include "AArch64Subtarget.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/ADT/EquivalenceClasses.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/RegisterClassInfo.h"
#include "llvm/CodeGen/RegisterScavenging.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
#define DEBUG_TYPE "aarch64-a57-fp-load-balancing"
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);
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);
static bool isMul(MachineInstr *MI) {
switch (MI->getOpcode()) {
case AArch64::FMULSrr:
case AArch64::FNMULSrr:
case AArch64::FMULDrr:
case AArch64::FNMULDrr:
return true;
default:
return false;
}
}
static bool isMla(MachineInstr *MI) {
switch (MI->getOpcode()) {
case AArch64::FMSUBSrrr:
case AArch64::FMADDSrrr:
case AArch64::FNMSUBSrrr:
case AArch64::FNMADDSrrr:
case AArch64::FMSUBDrrr:
case AArch64::FMADDDrrr:
case AArch64::FNMSUBDrrr:
case AArch64::FNMADDDrrr:
return true;
default:
return false;
}
}
namespace {
enum class Color { Even, Odd };
#ifndef NDEBUG
static const char *ColorNames[2] = { "Even", "Odd" };
#endif
class Chain;
class AArch64A57FPLoadBalancing : public MachineFunctionPass {
MachineRegisterInfo *MRI;
const TargetRegisterInfo *TRI;
RegisterClassInfo RCI;
public:
static char ID;
explicit AArch64A57FPLoadBalancing() : MachineFunctionPass(ID) {
initializeAArch64A57FPLoadBalancingPass(*PassRegistry::getPassRegistry());
}
bool runOnMachineFunction(MachineFunction &F) override;
MachineFunctionProperties getRequiredProperties() const override {
return MachineFunctionProperties().set(
MachineFunctionProperties::Property::NoVRegs);
}
StringRef getPassName() const override {
return "A57 FP Anti-dependency breaker";
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG();
MachineFunctionPass::getAnalysisUsage(AU);
}
private:
bool runOnBasicBlock(MachineBasicBlock &MBB);
bool colorChainSet(std::vector<Chain*> GV, MachineBasicBlock &MBB,
int &Balance);
bool colorChain(Chain *G, Color C, MachineBasicBlock &MBB);
int scavengeRegister(Chain *G, Color C, MachineBasicBlock &MBB);
void scanInstruction(MachineInstr *MI, unsigned Idx,
std::map<unsigned, Chain*> &Active,
std::vector<std::unique_ptr<Chain>> &AllChains);
void maybeKillChain(MachineOperand &MO, unsigned Idx,
std::map<unsigned, Chain*> &RegChains);
Color getColor(unsigned Register);
Chain *getAndEraseNext(Color PreferredColor, std::vector<Chain*> &L);
};
}
char AArch64A57FPLoadBalancing::ID = 0;
INITIALIZE_PASS_BEGIN(AArch64A57FPLoadBalancing, DEBUG_TYPE,
"AArch64 A57 FP Load-Balancing", false, false)
INITIALIZE_PASS_END(AArch64A57FPLoadBalancing, DEBUG_TYPE,
"AArch64 A57 FP Load-Balancing", false, false)
namespace {
class Chain {
public:
MachineInstr *StartInst, *LastInst, *KillInst;
unsigned StartInstIdx, LastInstIdx, KillInstIdx;
std::set<MachineInstr*> Insts;
bool KillIsImmutable;
Color LastColor;
Chain(MachineInstr *MI, unsigned Idx, Color C)
: StartInst(MI), LastInst(MI), KillInst(nullptr),
StartInstIdx(Idx), LastInstIdx(Idx), KillInstIdx(0),
LastColor(C) {
Insts.insert(MI);
}
void add(MachineInstr *MI, unsigned Idx, Color C) {
LastInst = MI;
LastInstIdx = Idx;
LastColor = C;
assert((KillInstIdx == 0 || LastInstIdx < KillInstIdx) &&
"Chain: broken invariant. A Chain can only be killed after its last "
"def");
Insts.insert(MI);
}
bool contains(MachineInstr &MI) { return Insts.count(&MI) > 0; }
unsigned size() const {
return Insts.size();
}
void setKill(MachineInstr *MI, unsigned Idx, bool Immutable) {
KillInst = MI;
KillInstIdx = Idx;
KillIsImmutable = Immutable;
assert((KillInstIdx == 0 || LastInstIdx < KillInstIdx) &&
"Chain: broken invariant. A Chain can only be killed after its last "
"def");
}
MachineInstr *getStart() const { return StartInst; }
MachineInstr *getLast() const { return LastInst; }
MachineInstr *getKill() const { return KillInst; }
MachineBasicBlock::iterator end() const {
return ++MachineBasicBlock::iterator(KillInst ? KillInst : LastInst);
}
MachineBasicBlock::iterator begin() const { return getStart(); }
bool isKillImmutable() const { return KillIsImmutable; }
Color getPreferredColor() {
if (OverrideBalance != 0)
return OverrideBalance == 1 ? Color::Even : Color::Odd;
return LastColor;
}
bool rangeOverlapsWith(const Chain &Other) const {
unsigned End = KillInst ? KillInstIdx : LastInstIdx;
unsigned OtherEnd = Other.KillInst ?
Other.KillInstIdx : Other.LastInstIdx;
return StartInstIdx <= OtherEnd && Other.StartInstIdx <= End;
}
bool startsBefore(const Chain *Other) const {
return StartInstIdx < Other->StartInstIdx;
}
bool requiresFixup() const {
return (getKill() && isKillImmutable()) || !getKill();
}
std::string str() const {
std::string S;
raw_string_ostream OS(S);
OS << "{";
StartInst->print(OS, true);
OS << " -> ";
LastInst->print(OS, true);
if (KillInst) {
OS << " (kill @ ";
KillInst->print(OS, true);
OS << ")";
}
OS << "}";
return OS.str();
}
};
}
bool AArch64A57FPLoadBalancing::runOnMachineFunction(MachineFunction &F) {
if (skipFunction(F.getFunction()))
return false;
if (!F.getSubtarget<AArch64Subtarget>().balanceFPOps())
return false;
bool Changed = false;
LLVM_DEBUG(dbgs() << "***** AArch64A57FPLoadBalancing *****\n");
MRI = &F.getRegInfo();
TRI = F.getRegInfo().getTargetRegisterInfo();
RCI.runOnMachineFunction(F);
for (auto &MBB : F) {
Changed |= runOnBasicBlock(MBB);
}
return Changed;
}
bool AArch64A57FPLoadBalancing::runOnBasicBlock(MachineBasicBlock &MBB) {
bool Changed = false;
LLVM_DEBUG(dbgs() << "Running on MBB: " << MBB
<< " - scanning instructions...\n");
std::map<unsigned, Chain*> ActiveChains;
std::vector<std::unique_ptr<Chain>> AllChains;
unsigned Idx = 0;
for (auto &MI : MBB)
scanInstruction(&MI, Idx++, ActiveChains, AllChains);
LLVM_DEBUG(dbgs() << "Scan complete, " << AllChains.size()
<< " chains created.\n");
EquivalenceClasses<Chain*> EC;
for (auto &I : AllChains)
EC.insert(I.get());
for (auto &I : AllChains)
for (auto &J : AllChains)
if (I != J && I->rangeOverlapsWith(*J))
EC.unionSets(I.get(), J.get());
LLVM_DEBUG(dbgs() << "Created " << EC.getNumClasses() << " disjoint sets.\n");
std::vector<std::vector<Chain*> > V;
for (auto I = EC.begin(), E = EC.end(); I != E; ++I) {
std::vector<Chain*> Cs(EC.member_begin(I), EC.member_end());
if (Cs.empty()) continue;
V.push_back(std::move(Cs));
}
llvm::sort(V,
[](const std::vector<Chain *> &A, const std::vector<Chain *> &B) {
return A.front()->startsBefore(B.front());
});
int Parity = 0;
for (auto &I : V)
Changed |= colorChainSet(std::move(I), MBB, Parity);
return Changed;
}
Chain *AArch64A57FPLoadBalancing::getAndEraseNext(Color PreferredColor,
std::vector<Chain*> &L) {
if (L.empty())
return nullptr;
const unsigned SizeFuzz = 1;
unsigned MinSize = L.front()->size() - SizeFuzz;
for (auto I = L.begin(), E = L.end(); I != E; ++I) {
if ((*I)->size() <= MinSize) {
Chain *Ch = *--I;
L.erase(I);
return Ch;
}
if ((*I)->getPreferredColor() == PreferredColor) {
Chain *Ch = *I;
L.erase(I);
return Ch;
}
}
Chain *Ch = L.front();
L.erase(L.begin());
return Ch;
}
bool AArch64A57FPLoadBalancing::colorChainSet(std::vector<Chain*> GV,
MachineBasicBlock &MBB,
int &Parity) {
bool Changed = false;
LLVM_DEBUG(dbgs() << "colorChainSet(): #sets=" << GV.size() << "\n");
llvm::sort(GV, [](const Chain *G1, const Chain *G2) {
if (G1->size() != G2->size())
return G1->size() > G2->size();
if (G1->requiresFixup() != G2->requiresFixup())
return G1->requiresFixup() > G2->requiresFixup();
assert((G1 == G2 || (G1->startsBefore(G2) ^ G2->startsBefore(G1))) &&
"Starts before not total order!");
return G1->startsBefore(G2);
});
Color PreferredColor = Parity < 0 ? Color::Even : Color::Odd;
while (Chain *G = getAndEraseNext(PreferredColor, GV)) {
Color C = PreferredColor;
if (Parity == 0)
C = G->getPreferredColor();
LLVM_DEBUG(dbgs() << " - Parity=" << Parity
<< ", Color=" << ColorNames[(int)C] << "\n");
if (G->requiresFixup() && C != G->getPreferredColor()) {
C = G->getPreferredColor();
LLVM_DEBUG(dbgs() << " - " << G->str()
<< " - not worthwhile changing; "
"color remains "
<< ColorNames[(int)C] << "\n");
}
Changed |= colorChain(G, C, MBB);
Parity += (C == Color::Even) ? G->size() : -G->size();
PreferredColor = Parity < 0 ? Color::Even : Color::Odd;
}
return Changed;
}
int AArch64A57FPLoadBalancing::scavengeRegister(Chain *G, Color C,
MachineBasicBlock &MBB) {
LiveRegUnits Units(*TRI);
Units.addLiveOuts(MBB);
MachineBasicBlock::iterator I = MBB.end();
MachineBasicBlock::iterator ChainEnd = G->end();
while (I != ChainEnd) {
--I;
Units.stepBackward(*I);
}
MachineBasicBlock::iterator ChainBegin = G->begin();
assert(ChainBegin != ChainEnd && "Chain should contain instructions");
do {
--I;
Units.accumulate(*I);
} while (I != ChainBegin);
unsigned RegClassID = ChainBegin->getDesc().OpInfo[0].RegClass;
auto Ord = RCI.getOrder(TRI->getRegClass(RegClassID));
for (auto Reg : Ord) {
if (!Units.available(Reg))
continue;
if (C == getColor(Reg))
return Reg;
}
return -1;
}
bool AArch64A57FPLoadBalancing::colorChain(Chain *G, Color C,
MachineBasicBlock &MBB) {
bool Changed = false;
LLVM_DEBUG(dbgs() << " - colorChain(" << G->str() << ", "
<< ColorNames[(int)C] << ")\n");
int Reg = scavengeRegister(G, C, MBB);
if (Reg == -1) {
LLVM_DEBUG(dbgs() << "Scavenging (thus coloring) failed!\n");
return false;
}
LLVM_DEBUG(dbgs() << " - Scavenged register: " << printReg(Reg, TRI) << "\n");
std::map<unsigned, unsigned> Substs;
for (MachineInstr &I : *G) {
if (!G->contains(I) && (&I != G->getKill() || G->isKillImmutable()))
continue;
std::vector<unsigned> ToErase;
for (auto &U : I.operands()) {
if (U.isReg() && U.isUse() && Substs.find(U.getReg()) != Substs.end()) {
Register OrigReg = U.getReg();
U.setReg(Substs[OrigReg]);
if (U.isKill())
ToErase.push_back(OrigReg);
} else if (U.isRegMask()) {
for (auto J : Substs) {
if (U.clobbersPhysReg(J.first))
ToErase.push_back(J.first);
}
}
}
for (auto J : ToErase)
Substs.erase(J);
if (&I != G->getKill()) {
MachineOperand &MO = I.getOperand(0);
bool Change = TransformAll || getColor(MO.getReg()) != C;
if (G->requiresFixup() && &I == G->getLast())
Change = false;
if (Change) {
Substs[MO.getReg()] = Reg;
MO.setReg(Reg);
Changed = true;
}
}
}
assert(Substs.size() == 0 && "No substitutions should be left active!");
if (G->getKill()) {
LLVM_DEBUG(dbgs() << " - Kill instruction seen.\n");
} else {
LLVM_DEBUG(dbgs() << " - Destination register not changed.\n");
}
return Changed;
}
void AArch64A57FPLoadBalancing::scanInstruction(
MachineInstr *MI, unsigned Idx, std::map<unsigned, Chain *> &ActiveChains,
std::vector<std::unique_ptr<Chain>> &AllChains) {
if (isMul(MI)) {
for (auto &I : MI->uses())
maybeKillChain(I, Idx, ActiveChains);
for (auto &I : MI->defs())
maybeKillChain(I, Idx, ActiveChains);
Register DestReg = MI->getOperand(0).getReg();
LLVM_DEBUG(dbgs() << "New chain started for register "
<< printReg(DestReg, TRI) << " at " << *MI);
auto G = std::make_unique<Chain>(MI, Idx, getColor(DestReg));
ActiveChains[DestReg] = G.get();
AllChains.push_back(std::move(G));
} else if (isMla(MI)) {
Register DestReg = MI->getOperand(0).getReg();
Register AccumReg = MI->getOperand(3).getReg();
maybeKillChain(MI->getOperand(1), Idx, ActiveChains);
maybeKillChain(MI->getOperand(2), Idx, ActiveChains);
if (DestReg != AccumReg)
maybeKillChain(MI->getOperand(0), Idx, ActiveChains);
if (ActiveChains.find(AccumReg) != ActiveChains.end()) {
LLVM_DEBUG(dbgs() << "Chain found for accumulator register "
<< printReg(AccumReg, TRI) << " in MI " << *MI);
if (MI->getOperand(3).isKill()) {
LLVM_DEBUG(dbgs() << "Instruction was successfully added to chain.\n");
ActiveChains[AccumReg]->add(MI, Idx, getColor(DestReg));
if (DestReg != AccumReg) {
ActiveChains[DestReg] = ActiveChains[AccumReg];
ActiveChains.erase(AccumReg);
}
return;
}
LLVM_DEBUG(
dbgs() << "Cannot add to chain because accumulator operand wasn't "
<< "marked <kill>!\n");
maybeKillChain(MI->getOperand(3), Idx, ActiveChains);
}
LLVM_DEBUG(dbgs() << "Creating new chain for dest register "
<< printReg(DestReg, TRI) << "\n");
auto G = std::make_unique<Chain>(MI, Idx, getColor(DestReg));
ActiveChains[DestReg] = G.get();
AllChains.push_back(std::move(G));
} else {
for (auto &I : MI->uses())
maybeKillChain(I, Idx, ActiveChains);
for (auto &I : MI->defs())
maybeKillChain(I, Idx, ActiveChains);
}
}
void AArch64A57FPLoadBalancing::
maybeKillChain(MachineOperand &MO, unsigned Idx,
std::map<unsigned, Chain*> &ActiveChains) {
MachineInstr *MI = MO.getParent();
if (MO.isReg()) {
if (MO.isKill() && ActiveChains.find(MO.getReg()) != ActiveChains.end()) {
LLVM_DEBUG(dbgs() << "Kill seen for chain " << printReg(MO.getReg(), TRI)
<< "\n");
ActiveChains[MO.getReg()]->setKill(MI, Idx, MO.isTied());
}
ActiveChains.erase(MO.getReg());
} else if (MO.isRegMask()) {
for (auto I = ActiveChains.begin(), E = ActiveChains.end();
I != E;) {
if (MO.clobbersPhysReg(I->first)) {
LLVM_DEBUG(dbgs() << "Kill (regmask) seen for chain "
<< printReg(I->first, TRI) << "\n");
I->second->setKill(MI, Idx, true);
ActiveChains.erase(I++);
} else
++I;
}
}
}
Color AArch64A57FPLoadBalancing::getColor(unsigned Reg) {
if ((TRI->getEncodingValue(Reg) % 2) == 0)
return Color::Even;
else
return Color::Odd;
}
FunctionPass *llvm::createAArch64A57FPLoadBalancing() {
return new AArch64A57FPLoadBalancing();
}