#include "ARM.h"
#include "ARMBaseInstrInfo.h"
#include "ARMBaseRegisterInfo.h"
#include "ARMSubtarget.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/TargetRegisterInfo.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include <map>
#include <set>
using namespace llvm;
#define DEBUG_TYPE "a15-sd-optimizer"
namespace {
struct A15SDOptimizer : public MachineFunctionPass {
static char ID;
A15SDOptimizer() : MachineFunctionPass(ID) {}
bool runOnMachineFunction(MachineFunction &Fn) override;
StringRef getPassName() const override { return "ARM A15 S->D optimizer"; }
private:
const ARMBaseInstrInfo *TII;
const TargetRegisterInfo *TRI;
MachineRegisterInfo *MRI;
bool runOnInstruction(MachineInstr *MI);
unsigned createDupLane(MachineBasicBlock &MBB,
MachineBasicBlock::iterator InsertBefore,
const DebugLoc &DL, unsigned Reg, unsigned Lane,
bool QPR = false);
unsigned createExtractSubreg(MachineBasicBlock &MBB,
MachineBasicBlock::iterator InsertBefore,
const DebugLoc &DL, unsigned DReg,
unsigned Lane, const TargetRegisterClass *TRC);
unsigned createVExt(MachineBasicBlock &MBB,
MachineBasicBlock::iterator InsertBefore,
const DebugLoc &DL, unsigned Ssub0, unsigned Ssub1);
unsigned createRegSequence(MachineBasicBlock &MBB,
MachineBasicBlock::iterator InsertBefore,
const DebugLoc &DL, unsigned Reg1,
unsigned Reg2);
unsigned createInsertSubreg(MachineBasicBlock &MBB,
MachineBasicBlock::iterator InsertBefore,
const DebugLoc &DL, unsigned DReg,
unsigned Lane, unsigned ToInsert);
unsigned createImplicitDef(MachineBasicBlock &MBB,
MachineBasicBlock::iterator InsertBefore,
const DebugLoc &DL);
bool usesRegClass(MachineOperand &MO, const TargetRegisterClass *TRC);
bool hasPartialWrite(MachineInstr *MI);
SmallVector<unsigned, 8> getReadDPRs(MachineInstr *MI);
unsigned getDPRLaneFromSPR(unsigned SReg);
MachineInstr *elideCopies(MachineInstr *MI);
void elideCopiesAndPHIs(MachineInstr *MI,
SmallVectorImpl<MachineInstr*> &Outs);
unsigned optimizeAllLanesPattern(MachineInstr *MI, unsigned Reg);
unsigned optimizeSDPattern(MachineInstr *MI);
unsigned getPrefSPRLane(unsigned SReg);
void eraseInstrWithNoUses(MachineInstr *MI);
std::map<MachineInstr*, unsigned> Replacements;
std::set<MachineInstr *> DeadInstr;
};
char A15SDOptimizer::ID = 0;
}
bool A15SDOptimizer::usesRegClass(MachineOperand &MO,
const TargetRegisterClass *TRC) {
if (!MO.isReg())
return false;
Register Reg = MO.getReg();
if (Register::isVirtualRegister(Reg))
return MRI->getRegClass(Reg)->hasSuperClassEq(TRC);
else
return TRC->contains(Reg);
}
unsigned A15SDOptimizer::getDPRLaneFromSPR(unsigned SReg) {
unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1,
&ARM::DPRRegClass);
if (DReg != ARM::NoRegister) return ARM::ssub_1;
return ARM::ssub_0;
}
unsigned A15SDOptimizer::getPrefSPRLane(unsigned SReg) {
if (!Register::isVirtualRegister(SReg))
return getDPRLaneFromSPR(SReg);
MachineInstr *MI = MRI->getVRegDef(SReg);
if (!MI) return ARM::ssub_0;
MachineOperand *MO = MI->findRegisterDefOperand(SReg);
if (!MO) return ARM::ssub_0;
assert(MO->isReg() && "Non-register operand found!");
if (MI->isCopy() && usesRegClass(MI->getOperand(1),
&ARM::SPRRegClass)) {
SReg = MI->getOperand(1).getReg();
}
if (Register::isVirtualRegister(SReg)) {
if (MO->getSubReg() == ARM::ssub_1) return ARM::ssub_1;
return ARM::ssub_0;
}
return getDPRLaneFromSPR(SReg);
}
void A15SDOptimizer::eraseInstrWithNoUses(MachineInstr *MI) {
SmallVector<MachineInstr *, 8> Front;
DeadInstr.insert(MI);
LLVM_DEBUG(dbgs() << "Deleting base instruction " << *MI << "\n");
Front.push_back(MI);
while (Front.size() != 0) {
MI = Front.pop_back_val();
for (MachineOperand &MO : MI->operands()) {
if ((!MO.isReg()) || (!MO.isUse()))
continue;
Register Reg = MO.getReg();
if (!Register::isVirtualRegister(Reg))
continue;
MachineOperand *Op = MI->findRegisterDefOperand(Reg);
if (!Op)
continue;
MachineInstr *Def = Op->getParent();
if (DeadInstr.find(Def) != DeadInstr.end())
continue;
bool IsDead = true;
for (MachineOperand &MODef : Def->operands()) {
if ((!MODef.isReg()) || (!MODef.isDef()))
continue;
Register DefReg = MODef.getReg();
if (!Register::isVirtualRegister(DefReg)) {
IsDead = false;
break;
}
for (MachineInstr &Use : MRI->use_instructions(Reg)) {
if (&Use == Def)
continue;
if (DeadInstr.find(&Use) == DeadInstr.end()) {
IsDead = false;
break;
}
}
}
if (!IsDead) continue;
LLVM_DEBUG(dbgs() << "Deleting instruction " << *Def << "\n");
DeadInstr.insert(Def);
}
}
}
unsigned A15SDOptimizer::optimizeSDPattern(MachineInstr *MI) {
if (MI->isCopy()) {
return optimizeAllLanesPattern(MI, MI->getOperand(1).getReg());
}
if (MI->isInsertSubreg()) {
Register DPRReg = MI->getOperand(1).getReg();
Register SPRReg = MI->getOperand(2).getReg();
if (Register::isVirtualRegister(DPRReg) && Register::isVirtualRegister(SPRReg)) {
MachineInstr *DPRMI = MRI->getVRegDef(MI->getOperand(1).getReg());
MachineInstr *SPRMI = MRI->getVRegDef(MI->getOperand(2).getReg());
if (DPRMI && SPRMI) {
MachineInstr *ECDef = elideCopies(DPRMI);
if (ECDef && ECDef->isImplicitDef()) {
MachineInstr *EC = elideCopies(SPRMI);
if (EC && EC->isCopy() &&
EC->getOperand(1).getSubReg() == ARM::ssub_0) {
LLVM_DEBUG(dbgs() << "Found a subreg copy: " << *SPRMI);
Register FullReg = SPRMI->getOperand(1).getReg();
const TargetRegisterClass *TRC =
MRI->getRegClass(MI->getOperand(1).getReg());
if (TRC->hasSuperClassEq(MRI->getRegClass(FullReg))) {
LLVM_DEBUG(dbgs() << "Subreg copy is compatible - returning ");
LLVM_DEBUG(dbgs() << printReg(FullReg) << "\n");
eraseInstrWithNoUses(MI);
return FullReg;
}
}
return optimizeAllLanesPattern(MI, MI->getOperand(2).getReg());
}
}
}
return optimizeAllLanesPattern(MI, MI->getOperand(0).getReg());
}
if (MI->isRegSequence() && usesRegClass(MI->getOperand(1),
&ARM::SPRRegClass)) {
unsigned NumImplicit = 0, NumTotal = 0;
unsigned NonImplicitReg = ~0U;
for (unsigned I = 1; I < MI->getNumExplicitOperands(); ++I) {
if (!MI->getOperand(I).isReg())
continue;
++NumTotal;
Register OpReg = MI->getOperand(I).getReg();
if (!Register::isVirtualRegister(OpReg))
break;
MachineInstr *Def = MRI->getVRegDef(OpReg);
if (!Def)
break;
if (Def->isImplicitDef())
++NumImplicit;
else
NonImplicitReg = MI->getOperand(I).getReg();
}
if (NumImplicit == NumTotal - 1)
return optimizeAllLanesPattern(MI, NonImplicitReg);
else
return optimizeAllLanesPattern(MI, MI->getOperand(0).getReg());
}
llvm_unreachable("Unhandled update pattern!");
}
bool A15SDOptimizer::hasPartialWrite(MachineInstr *MI) {
if (MI->isCopy() && usesRegClass(MI->getOperand(1), &ARM::SPRRegClass))
return true;
if (MI->isInsertSubreg() && usesRegClass(MI->getOperand(2),
&ARM::SPRRegClass))
return true;
if (MI->isRegSequence() && usesRegClass(MI->getOperand(1), &ARM::SPRRegClass))
return true;
return false;
}
MachineInstr *A15SDOptimizer::elideCopies(MachineInstr *MI) {
if (!MI->isFullCopy())
return MI;
if (!Register::isVirtualRegister(MI->getOperand(1).getReg()))
return nullptr;
MachineInstr *Def = MRI->getVRegDef(MI->getOperand(1).getReg());
if (!Def)
return nullptr;
return elideCopies(Def);
}
void A15SDOptimizer::elideCopiesAndPHIs(MachineInstr *MI,
SmallVectorImpl<MachineInstr*> &Outs) {
std::set<MachineInstr *> Reached;
SmallVector<MachineInstr *, 8> Front;
Front.push_back(MI);
while (Front.size() != 0) {
MI = Front.pop_back_val();
if (!Reached.insert(MI).second)
continue;
if (MI->isPHI()) {
for (unsigned I = 1, E = MI->getNumOperands(); I != E; I += 2) {
Register Reg = MI->getOperand(I).getReg();
if (!Register::isVirtualRegister(Reg)) {
continue;
}
MachineInstr *NewMI = MRI->getVRegDef(Reg);
if (!NewMI)
continue;
Front.push_back(NewMI);
}
} else if (MI->isFullCopy()) {
if (!Register::isVirtualRegister(MI->getOperand(1).getReg()))
continue;
MachineInstr *NewMI = MRI->getVRegDef(MI->getOperand(1).getReg());
if (!NewMI)
continue;
Front.push_back(NewMI);
} else {
LLVM_DEBUG(dbgs() << "Found partial copy" << *MI << "\n");
Outs.push_back(MI);
}
}
}
SmallVector<unsigned, 8> A15SDOptimizer::getReadDPRs(MachineInstr *MI) {
if (MI->isCopyLike() || MI->isInsertSubreg() || MI->isRegSequence() ||
MI->isKill())
return SmallVector<unsigned, 8>();
SmallVector<unsigned, 8> Defs;
for (MachineOperand &MO : MI->operands()) {
if (!MO.isReg() || !MO.isUse())
continue;
if (!usesRegClass(MO, &ARM::DPRRegClass) &&
!usesRegClass(MO, &ARM::QPRRegClass) &&
!usesRegClass(MO, &ARM::DPairRegClass)) continue;
Defs.push_back(MO.getReg());
}
return Defs;
}
unsigned A15SDOptimizer::createDupLane(MachineBasicBlock &MBB,
MachineBasicBlock::iterator InsertBefore,
const DebugLoc &DL, unsigned Reg,
unsigned Lane, bool QPR) {
Register Out =
MRI->createVirtualRegister(QPR ? &ARM::QPRRegClass : &ARM::DPRRegClass);
BuildMI(MBB, InsertBefore, DL,
TII->get(QPR ? ARM::VDUPLN32q : ARM::VDUPLN32d), Out)
.addReg(Reg)
.addImm(Lane)
.add(predOps(ARMCC::AL));
return Out;
}
unsigned A15SDOptimizer::createExtractSubreg(
MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
const DebugLoc &DL, unsigned DReg, unsigned Lane,
const TargetRegisterClass *TRC) {
Register Out = MRI->createVirtualRegister(TRC);
BuildMI(MBB,
InsertBefore,
DL,
TII->get(TargetOpcode::COPY), Out)
.addReg(DReg, 0, Lane);
return Out;
}
unsigned A15SDOptimizer::createRegSequence(
MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
const DebugLoc &DL, unsigned Reg1, unsigned Reg2) {
Register Out = MRI->createVirtualRegister(&ARM::QPRRegClass);
BuildMI(MBB,
InsertBefore,
DL,
TII->get(TargetOpcode::REG_SEQUENCE), Out)
.addReg(Reg1)
.addImm(ARM::dsub_0)
.addReg(Reg2)
.addImm(ARM::dsub_1);
return Out;
}
unsigned A15SDOptimizer::createVExt(MachineBasicBlock &MBB,
MachineBasicBlock::iterator InsertBefore,
const DebugLoc &DL, unsigned Ssub0,
unsigned Ssub1) {
Register Out = MRI->createVirtualRegister(&ARM::DPRRegClass);
BuildMI(MBB, InsertBefore, DL, TII->get(ARM::VEXTd32), Out)
.addReg(Ssub0)
.addReg(Ssub1)
.addImm(1)
.add(predOps(ARMCC::AL));
return Out;
}
unsigned A15SDOptimizer::createInsertSubreg(
MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
const DebugLoc &DL, unsigned DReg, unsigned Lane, unsigned ToInsert) {
Register Out = MRI->createVirtualRegister(&ARM::DPR_VFP2RegClass);
BuildMI(MBB,
InsertBefore,
DL,
TII->get(TargetOpcode::INSERT_SUBREG), Out)
.addReg(DReg)
.addReg(ToInsert)
.addImm(Lane);
return Out;
}
unsigned
A15SDOptimizer::createImplicitDef(MachineBasicBlock &MBB,
MachineBasicBlock::iterator InsertBefore,
const DebugLoc &DL) {
Register Out = MRI->createVirtualRegister(&ARM::DPRRegClass);
BuildMI(MBB,
InsertBefore,
DL,
TII->get(TargetOpcode::IMPLICIT_DEF), Out);
return Out;
}
unsigned
A15SDOptimizer::optimizeAllLanesPattern(MachineInstr *MI, unsigned Reg) {
MachineBasicBlock::iterator InsertPt(MI);
DebugLoc DL = MI->getDebugLoc();
MachineBasicBlock &MBB = *MI->getParent();
InsertPt++;
unsigned Out;
if (MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::QPRRegClass) ||
MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::DPairRegClass)) {
unsigned DSub0 = createExtractSubreg(MBB, InsertPt, DL, Reg,
ARM::dsub_0, &ARM::DPRRegClass);
unsigned DSub1 = createExtractSubreg(MBB, InsertPt, DL, Reg,
ARM::dsub_1, &ARM::DPRRegClass);
unsigned Out1 = createDupLane(MBB, InsertPt, DL, DSub0, 0);
unsigned Out2 = createDupLane(MBB, InsertPt, DL, DSub0, 1);
Out = createVExt(MBB, InsertPt, DL, Out1, Out2);
unsigned Out3 = createDupLane(MBB, InsertPt, DL, DSub1, 0);
unsigned Out4 = createDupLane(MBB, InsertPt, DL, DSub1, 1);
Out2 = createVExt(MBB, InsertPt, DL, Out3, Out4);
Out = createRegSequence(MBB, InsertPt, DL, Out, Out2);
} else if (MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::DPRRegClass)) {
unsigned Out1 = createDupLane(MBB, InsertPt, DL, Reg, 0);
unsigned Out2 = createDupLane(MBB, InsertPt, DL, Reg, 1);
Out = createVExt(MBB, InsertPt, DL, Out1, Out2);
} else {
assert(MRI->getRegClass(Reg)->hasSuperClassEq(&ARM::SPRRegClass) &&
"Found unexpected regclass!");
unsigned PrefLane = getPrefSPRLane(Reg);
unsigned Lane;
switch (PrefLane) {
case ARM::ssub_0: Lane = 0; break;
case ARM::ssub_1: Lane = 1; break;
default: llvm_unreachable("Unknown preferred lane!");
}
bool UsesQPR = usesRegClass(MI->getOperand(0), &ARM::QPRRegClass) ||
usesRegClass(MI->getOperand(0), &ARM::DPairRegClass);
Out = createImplicitDef(MBB, InsertPt, DL);
Out = createInsertSubreg(MBB, InsertPt, DL, Out, PrefLane, Reg);
Out = createDupLane(MBB, InsertPt, DL, Out, Lane, UsesQPR);
eraseInstrWithNoUses(MI);
}
return Out;
}
bool A15SDOptimizer::runOnInstruction(MachineInstr *MI) {
SmallVector<unsigned, 8> Defs = getReadDPRs(MI);
bool Modified = false;
for (unsigned I : Defs) {
SmallVector<MachineInstr *, 8> DefSrcs;
if (!Register::isVirtualRegister(I))
continue;
MachineInstr *Def = MRI->getVRegDef(I);
if (!Def)
continue;
elideCopiesAndPHIs(Def, DefSrcs);
for (MachineInstr *MI : DefSrcs) {
if (Replacements.find(MI) != Replacements.end())
continue;
if (!hasPartialWrite(MI))
continue;
SmallVector<MachineOperand*, 8> Uses;
Register DPRDefReg = MI->getOperand(0).getReg();
for (MachineOperand &MO : MRI->use_operands(DPRDefReg))
Uses.push_back(&MO);
unsigned NewReg = optimizeSDPattern(MI);
if (NewReg != 0) {
Modified = true;
for (MachineOperand *Use : Uses) {
MRI->constrainRegClass(NewReg, MRI->getRegClass(Use->getReg()));
LLVM_DEBUG(dbgs() << "Replacing operand " << *Use << " with "
<< printReg(NewReg) << "\n");
Use->substVirtReg(NewReg, 0, *TRI);
}
}
Replacements[MI] = NewReg;
}
}
return Modified;
}
bool A15SDOptimizer::runOnMachineFunction(MachineFunction &Fn) {
if (skipFunction(Fn.getFunction()))
return false;
const ARMSubtarget &STI = Fn.getSubtarget<ARMSubtarget>();
if (!(STI.useSplatVFPToNeon() && STI.hasNEON()))
return false;
TII = STI.getInstrInfo();
TRI = STI.getRegisterInfo();
MRI = &Fn.getRegInfo();
bool Modified = false;
LLVM_DEBUG(dbgs() << "Running on function " << Fn.getName() << "\n");
DeadInstr.clear();
Replacements.clear();
for (MachineBasicBlock &MBB : Fn) {
for (MachineInstr &MI : MBB) {
Modified |= runOnInstruction(&MI);
}
}
for (MachineInstr *MI : DeadInstr) {
MI->eraseFromParent();
}
return Modified;
}
FunctionPass *llvm::createA15SDOptimizerPass() {
return new A15SDOptimizer();
}