#include "Hexagon.h"
#include "HexagonTargetMachine.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"
#include "llvm/IR/Constants.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
#include <algorithm>
using namespace llvm;
#define DEBUG_TYPE "hexagon-peephole"
static cl::opt<bool>
DisableHexagonPeephole("disable-hexagon-peephole", cl::Hidden,
cl::desc("Disable Peephole Optimization"));
static cl::opt<bool> DisablePNotP("disable-hexagon-pnotp", cl::Hidden,
cl::desc("Disable Optimization of PNotP"));
static cl::opt<bool>
DisableOptSZExt("disable-hexagon-optszext", cl::Hidden, cl::init(true),
cl::desc("Disable Optimization of Sign/Zero Extends"));
static cl::opt<bool>
DisableOptExtTo64("disable-hexagon-opt-ext-to-64", cl::Hidden,
cl::init(true),
cl::desc("Disable Optimization of extensions to i64."));
namespace llvm {
FunctionPass *createHexagonPeephole();
void initializeHexagonPeepholePass(PassRegistry&);
}
namespace {
struct HexagonPeephole : public MachineFunctionPass {
const HexagonInstrInfo *QII;
const HexagonRegisterInfo *QRI;
const MachineRegisterInfo *MRI;
public:
static char ID;
HexagonPeephole() : MachineFunctionPass(ID) {
initializeHexagonPeepholePass(*PassRegistry::getPassRegistry());
}
bool runOnMachineFunction(MachineFunction &MF) override;
StringRef getPassName() const override {
return "Hexagon optimize redundant zero and size extends";
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
MachineFunctionPass::getAnalysisUsage(AU);
}
};
}
char HexagonPeephole::ID = 0;
INITIALIZE_PASS(HexagonPeephole, "hexagon-peephole", "Hexagon Peephole",
false, false)
bool HexagonPeephole::runOnMachineFunction(MachineFunction &MF) {
if (skipFunction(MF.getFunction()))
return false;
QII = static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo());
QRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
MRI = &MF.getRegInfo();
DenseMap<unsigned, unsigned> PeepholeMap;
DenseMap<unsigned, std::pair<unsigned, unsigned> > PeepholeDoubleRegsMap;
if (DisableHexagonPeephole) return false;
for (MachineBasicBlock &MBB : MF) {
PeepholeMap.clear();
PeepholeDoubleRegsMap.clear();
for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
if (!DisableOptSZExt && MI.getOpcode() == Hexagon::A2_sxtw) {
assert(MI.getNumOperands() == 2);
MachineOperand &Dst = MI.getOperand(0);
MachineOperand &Src = MI.getOperand(1);
Register DstReg = Dst.getReg();
Register SrcReg = Src.getReg();
if (DstReg.isVirtual() && SrcReg.isVirtual()) {
PeepholeMap[DstReg] = SrcReg;
}
}
if (!DisableOptExtTo64 && MI.getOpcode() == Hexagon::A4_combineir) {
assert(MI.getNumOperands() == 3);
MachineOperand &Dst = MI.getOperand(0);
MachineOperand &Src1 = MI.getOperand(1);
MachineOperand &Src2 = MI.getOperand(2);
if (Src1.getImm() != 0)
continue;
Register DstReg = Dst.getReg();
Register SrcReg = Src2.getReg();
PeepholeMap[DstReg] = SrcReg;
}
if (MI.getOpcode() == Hexagon::S2_lsr_i_p) {
assert(MI.getNumOperands() == 3);
MachineOperand &Dst = MI.getOperand(0);
MachineOperand &Src1 = MI.getOperand(1);
MachineOperand &Src2 = MI.getOperand(2);
if (Src2.getImm() != 32)
continue;
Register DstReg = Dst.getReg();
Register SrcReg = Src1.getReg();
PeepholeDoubleRegsMap[DstReg] =
std::make_pair(*&SrcReg, Hexagon::isub_hi);
}
if (!DisablePNotP && MI.getOpcode() == Hexagon::C2_not) {
assert(MI.getNumOperands() == 2);
MachineOperand &Dst = MI.getOperand(0);
MachineOperand &Src = MI.getOperand(1);
Register DstReg = Dst.getReg();
Register SrcReg = Src.getReg();
if (DstReg.isVirtual() && SrcReg.isVirtual()) {
PeepholeMap[DstReg] = SrcReg;
}
}
if (!DisableOptSZExt && MI.isCopy()) {
assert(MI.getNumOperands() == 2);
MachineOperand &Dst = MI.getOperand(0);
MachineOperand &Src = MI.getOperand(1);
if (Src.getSubReg() != Hexagon::isub_lo)
continue;
Register DstReg = Dst.getReg();
Register SrcReg = Src.getReg();
if (DstReg.isVirtual() && SrcReg.isVirtual()) {
if (unsigned PeepholeSrc = PeepholeMap.lookup(SrcReg)) {
MI.removeOperand(1);
MI.addOperand(MachineOperand::CreateReg(PeepholeSrc, false));
} else {
DenseMap<unsigned, std::pair<unsigned, unsigned> >::iterator DI =
PeepholeDoubleRegsMap.find(SrcReg);
if (DI != PeepholeDoubleRegsMap.end()) {
std::pair<unsigned,unsigned> PeepholeSrc = DI->second;
MI.removeOperand(1);
MI.addOperand(MachineOperand::CreateReg(
PeepholeSrc.first, false , false ,
false , false , false ,
false , PeepholeSrc.second));
}
}
}
}
if (!DisablePNotP) {
bool Done = false;
if (QII->isPredicated(MI)) {
MachineOperand &Op0 = MI.getOperand(0);
Register Reg0 = Op0.getReg();
const TargetRegisterClass *RC0 = MRI->getRegClass(Reg0);
if (RC0->getID() == Hexagon::PredRegsRegClassID) {
if (Reg0.isVirtual()) {
if (unsigned PeepholeSrc = PeepholeMap.lookup(Reg0)) {
MI.getOperand(0).setReg(PeepholeSrc);
MRI->clearKillFlags(PeepholeSrc);
int NewOp = QII->getInvertedPredicatedOpcode(MI.getOpcode());
MI.setDesc(QII->get(NewOp));
Done = true;
}
}
}
}
if (!Done) {
unsigned Op = MI.getOpcode();
unsigned NewOp = 0;
unsigned PR = 1, S1 = 2, S2 = 3;
switch (Op) {
case Hexagon::C2_mux:
case Hexagon::C2_muxii:
NewOp = Op;
break;
case Hexagon::C2_muxri:
NewOp = Hexagon::C2_muxir;
break;
case Hexagon::C2_muxir:
NewOp = Hexagon::C2_muxri;
break;
}
if (NewOp) {
Register PSrc = MI.getOperand(PR).getReg();
if (unsigned POrig = PeepholeMap.lookup(PSrc)) {
BuildMI(MBB, MI.getIterator(), MI.getDebugLoc(), QII->get(NewOp),
MI.getOperand(0).getReg())
.addReg(POrig)
.add(MI.getOperand(S2))
.add(MI.getOperand(S1));
MRI->clearKillFlags(POrig);
MI.eraseFromParent();
}
} }
}
} } return true;
}
FunctionPass *llvm::createHexagonPeephole() {
return new HexagonPeephole();
}