#include "AArch64.h"
#include "AArch64Subtarget.h"
#include "llvm/ADT/Statistic.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/TargetInstrInfo.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
#define DEBUG_TYPE "aarch64-fix-cortex-a53-835769"
STATISTIC(NumNopsAdded, "Number of Nops added to work around erratum 835769");
static bool isFirstInstructionInSequence(MachineInstr *MI) {
switch (MI->getOpcode()) {
case AArch64::PRFMl:
case AArch64::PRFMroW:
case AArch64::PRFMroX:
case AArch64::PRFMui:
case AArch64::PRFUMi:
return true;
default:
return MI->mayLoadOrStore();
}
}
static bool isSecondInstructionInSequence(MachineInstr *MI) {
switch (MI->getOpcode()) {
case AArch64::MSUBXrrr:
case AArch64::MADDXrrr:
case AArch64::SMADDLrrr:
case AArch64::SMSUBLrrr:
case AArch64::UMADDLrrr:
case AArch64::UMSUBLrrr:
return MI->getOperand(3).getReg() != AArch64::XZR;
default:
return false;
}
}
namespace {
class AArch64A53Fix835769 : public MachineFunctionPass {
const TargetInstrInfo *TII;
public:
static char ID;
explicit AArch64A53Fix835769() : MachineFunctionPass(ID) {
initializeAArch64A53Fix835769Pass(*PassRegistry::getPassRegistry());
}
bool runOnMachineFunction(MachineFunction &F) override;
MachineFunctionProperties getRequiredProperties() const override {
return MachineFunctionProperties().set(
MachineFunctionProperties::Property::NoVRegs);
}
StringRef getPassName() const override {
return "Workaround A53 erratum 835769 pass";
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG();
MachineFunctionPass::getAnalysisUsage(AU);
}
private:
bool runOnBasicBlock(MachineBasicBlock &MBB);
};
char AArch64A53Fix835769::ID = 0;
}
INITIALIZE_PASS(AArch64A53Fix835769, "aarch64-fix-cortex-a53-835769-pass",
"AArch64 fix for A53 erratum 835769", false, false)
bool
AArch64A53Fix835769::runOnMachineFunction(MachineFunction &F) {
LLVM_DEBUG(dbgs() << "***** AArch64A53Fix835769 *****\n");
auto &STI = F.getSubtarget<AArch64Subtarget>();
if (!STI.fixCortexA53_835769())
return false;
bool Changed = false;
TII = STI.getInstrInfo();
for (auto &MBB : F) {
Changed |= runOnBasicBlock(MBB);
}
return Changed;
}
static MachineBasicBlock *getBBFallenThrough(MachineBasicBlock *MBB,
const TargetInstrInfo *TII) {
MachineFunction::iterator MBBI(MBB);
if (MBBI == MBB->getParent()->begin())
return nullptr;
MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
SmallVector<MachineOperand, 2> Cond;
MachineBasicBlock *PrevBB = &*std::prev(MBBI);
for (MachineBasicBlock *S : MBB->predecessors())
if (S == PrevBB && !TII->analyzeBranch(*PrevBB, TBB, FBB, Cond) && !TBB &&
!FBB)
return S;
return nullptr;
}
static MachineInstr *getLastNonPseudo(MachineBasicBlock &MBB,
const TargetInstrInfo *TII) {
MachineBasicBlock *FMBB = &MBB;
while ((FMBB = getBBFallenThrough(FMBB, TII))) {
for (MachineInstr &I : llvm::reverse(*FMBB))
if (!I.isPseudo())
return &I;
}
return nullptr;
}
static void insertNopBeforeInstruction(MachineBasicBlock &MBB, MachineInstr* MI,
const TargetInstrInfo *TII) {
if (MI == &MBB.front()) {
MachineInstr *I = getLastNonPseudo(MBB, TII);
assert(I && "Expected instruction");
DebugLoc DL = I->getDebugLoc();
BuildMI(I->getParent(), DL, TII->get(AArch64::HINT)).addImm(0);
}
else {
DebugLoc DL = MI->getDebugLoc();
BuildMI(MBB, MI, DL, TII->get(AArch64::HINT)).addImm(0);
}
++NumNopsAdded;
}
bool
AArch64A53Fix835769::runOnBasicBlock(MachineBasicBlock &MBB) {
bool Changed = false;
LLVM_DEBUG(dbgs() << "Running on MBB: " << MBB
<< " - scanning instructions...\n");
std::vector<MachineInstr*> Sequences;
unsigned Idx = 0;
MachineInstr *PrevInstr = nullptr;
PrevInstr = getLastNonPseudo(MBB, TII);
for (auto &MI : MBB) {
MachineInstr *CurrInstr = &MI;
LLVM_DEBUG(dbgs() << " Examining: " << MI);
if (PrevInstr) {
LLVM_DEBUG(dbgs() << " PrevInstr: " << *PrevInstr
<< " CurrInstr: " << *CurrInstr
<< " isFirstInstructionInSequence(PrevInstr): "
<< isFirstInstructionInSequence(PrevInstr) << "\n"
<< " isSecondInstructionInSequence(CurrInstr): "
<< isSecondInstructionInSequence(CurrInstr) << "\n");
if (isFirstInstructionInSequence(PrevInstr) &&
isSecondInstructionInSequence(CurrInstr)) {
LLVM_DEBUG(dbgs() << " ** pattern found at Idx " << Idx << "!\n");
(void) Idx;
Sequences.push_back(CurrInstr);
}
}
if (!CurrInstr->isPseudo())
PrevInstr = CurrInstr;
++Idx;
}
LLVM_DEBUG(dbgs() << "Scan complete, " << Sequences.size()
<< " occurrences of pattern found.\n");
for (auto &MI : Sequences) {
Changed = true;
insertNopBeforeInstruction(MBB, MI, TII);
}
return Changed;
}
FunctionPass *llvm::createAArch64A53Fix835769() {
return new AArch64A53Fix835769();
}