2012-02-16 02:52:27 +08:00
|
|
|
//===-- HexagonPeephole.cpp - Hexagon Peephole Optimiztions ---------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
// This peephole pass optimizes in the following cases.
|
|
|
|
// 1. Optimizes redundant sign extends for the following case
|
|
|
|
// Transform the following pattern
|
|
|
|
// %vreg170<def> = SXTW %vreg166
|
|
|
|
// ...
|
2016-11-10 00:19:08 +08:00
|
|
|
// %vreg176<def> = COPY %vreg170:isub_lo
|
2012-02-16 02:52:27 +08:00
|
|
|
//
|
|
|
|
// Into
|
|
|
|
// %vreg176<def> = COPY vreg166
|
|
|
|
//
|
|
|
|
// 2. Optimizes redundant negation of predicates.
|
|
|
|
// %vreg15<def> = CMPGTrr %vreg6, %vreg2
|
|
|
|
// ...
|
|
|
|
// %vreg16<def> = NOT_p %vreg15<kill>
|
|
|
|
// ...
|
|
|
|
// JMP_c %vreg16<kill>, <BB#1>, %PC<imp-def,dead>
|
|
|
|
//
|
|
|
|
// Into
|
|
|
|
// %vreg15<def> = CMPGTrr %vreg6, %vreg2;
|
|
|
|
// ...
|
|
|
|
// JMP_cNot %vreg15<kill>, <BB#1>, %PC<imp-def,dead>;
|
|
|
|
//
|
|
|
|
// Note: The peephole pass makes the instrucstions like
|
|
|
|
// %vreg170<def> = SXTW %vreg166 or %vreg16<def> = NOT_p %vreg15<kill>
|
2013-09-28 21:42:22 +08:00
|
|
|
// redundant and relies on some form of dead removal instructions, like
|
2012-02-16 02:52:27 +08:00
|
|
|
// DCE or DIE to actually eliminate them.
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-03-18 02:46:09 +08:00
|
|
|
#include "Hexagon.h"
|
|
|
|
#include "HexagonTargetMachine.h"
|
2012-02-16 02:52:27 +08:00
|
|
|
#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"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/PassSupport.h"
|
2012-03-18 02:46:09 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2012-02-16 02:52:27 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2012-02-16 02:52:27 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:41:26 +08:00
|
|
|
#define DEBUG_TYPE "hexagon-peephole"
|
|
|
|
|
2012-02-16 02:52:27 +08:00
|
|
|
static cl::opt<bool> DisableHexagonPeephole("disable-hexagon-peephole",
|
|
|
|
cl::Hidden, cl::ZeroOrMore, cl::init(false),
|
|
|
|
cl::desc("Disable Peephole Optimization"));
|
|
|
|
|
|
|
|
static cl::opt<bool> DisablePNotP("disable-hexagon-pnotp",
|
|
|
|
cl::Hidden, cl::ZeroOrMore, cl::init(false),
|
|
|
|
cl::desc("Disable Optimization of PNotP"));
|
|
|
|
|
|
|
|
static cl::opt<bool> DisableOptSZExt("disable-hexagon-optszext",
|
2016-04-20 05:36:24 +08:00
|
|
|
cl::Hidden, cl::ZeroOrMore, cl::init(true),
|
2012-02-16 02:52:27 +08:00
|
|
|
cl::desc("Disable Optimization of Sign/Zero Extends"));
|
|
|
|
|
2013-05-03 04:22:51 +08:00
|
|
|
static cl::opt<bool> DisableOptExtTo64("disable-hexagon-opt-ext-to-64",
|
2016-04-20 05:36:24 +08:00
|
|
|
cl::Hidden, cl::ZeroOrMore, cl::init(true),
|
2013-05-03 04:22:51 +08:00
|
|
|
cl::desc("Disable Optimization of extensions to i64."));
|
|
|
|
|
2013-05-07 05:58:00 +08:00
|
|
|
namespace llvm {
|
2015-06-16 03:05:35 +08:00
|
|
|
FunctionPass *createHexagonPeephole();
|
2013-05-07 05:58:00 +08:00
|
|
|
void initializeHexagonPeepholePass(PassRegistry&);
|
|
|
|
}
|
|
|
|
|
2012-02-16 02:52:27 +08:00
|
|
|
namespace {
|
|
|
|
struct HexagonPeephole : public MachineFunctionPass {
|
|
|
|
const HexagonInstrInfo *QII;
|
|
|
|
const HexagonRegisterInfo *QRI;
|
|
|
|
const MachineRegisterInfo *MRI;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
2013-05-07 05:58:00 +08:00
|
|
|
HexagonPeephole() : MachineFunctionPass(ID) {
|
|
|
|
initializeHexagonPeepholePass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2012-02-16 02:52:27 +08:00
|
|
|
|
2014-04-29 15:58:16 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2012-02-16 02:52:27 +08:00
|
|
|
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override {
|
2012-02-16 02:52:27 +08:00
|
|
|
return "Hexagon optimize redundant zero and size extends";
|
|
|
|
}
|
|
|
|
|
2014-04-29 15:58:16 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2012-02-16 02:52:27 +08:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2012-02-16 02:52:27 +08:00
|
|
|
|
|
|
|
char HexagonPeephole::ID = 0;
|
|
|
|
|
2013-05-07 05:58:00 +08:00
|
|
|
INITIALIZE_PASS(HexagonPeephole, "hexagon-peephole", "Hexagon Peephole",
|
|
|
|
false, false)
|
2012-02-16 02:52:27 +08:00
|
|
|
|
2013-05-07 05:58:00 +08:00
|
|
|
bool HexagonPeephole::runOnMachineFunction(MachineFunction &MF) {
|
2016-04-27 03:46:28 +08:00
|
|
|
if (skipFunction(*MF.getFunction()))
|
|
|
|
return false;
|
|
|
|
|
2014-08-05 10:39:49 +08:00
|
|
|
QII = static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo());
|
2015-02-03 06:40:56 +08:00
|
|
|
QRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
|
2012-02-16 02:52:27 +08:00
|
|
|
MRI = &MF.getRegInfo();
|
|
|
|
|
|
|
|
DenseMap<unsigned, unsigned> PeepholeMap;
|
2012-09-06 00:01:40 +08:00
|
|
|
DenseMap<unsigned, std::pair<unsigned, unsigned> > PeepholeDoubleRegsMap;
|
2012-02-16 02:52:27 +08:00
|
|
|
|
|
|
|
if (DisableHexagonPeephole) return false;
|
|
|
|
|
|
|
|
// Loop over all of the basic blocks.
|
|
|
|
for (MachineFunction::iterator MBBb = MF.begin(), MBBe = MF.end();
|
|
|
|
MBBb != MBBe; ++MBBb) {
|
Hexagon: Remove implicit ilist iterator conversions, NFC
There are two things out of the ordinary in this commit. First, I made
a loop obviously "infinite" in HexagonInstrInfo.cpp. After checking if
an instruction was at the beginning of a basic block (in which case,
`break`), the loop decremented and checked the iterator for `nullptr` as
the loop condition. This has never been possible (the prev pointers are
always been circular, so even with the weird ilist/iplist
implementation, this isn't been possible), so I removed the condition.
Second, in HexagonAsmPrinter.cpp there was another case of comparing a
`MachineBasicBlock::instr_iterator` against `MachineBasicBlock::end()`
(which returns `MachineBasicBlock::iterator`). While not incorrect,
it's fragile. I switched this to `::instr_end()`.
All that said, no functionality change intended here.
llvm-svn: 250778
2015-10-20 08:46:39 +08:00
|
|
|
MachineBasicBlock *MBB = &*MBBb;
|
2012-02-16 02:52:27 +08:00
|
|
|
PeepholeMap.clear();
|
2012-09-06 00:01:40 +08:00
|
|
|
PeepholeDoubleRegsMap.clear();
|
2012-02-16 02:52:27 +08:00
|
|
|
|
|
|
|
// Traverse the basic block.
|
2017-06-22 05:03:34 +08:00
|
|
|
for (auto I = MBB->begin(), E = MBB->end(), NextI = I; I != E; I = NextI) {
|
|
|
|
NextI = std::next(I);
|
|
|
|
MachineInstr &MI = *I;
|
2012-02-16 02:52:27 +08:00
|
|
|
// Look for sign extends:
|
|
|
|
// %vreg170<def> = SXTW %vreg166
|
2016-07-12 09:55:32 +08:00
|
|
|
if (!DisableOptSZExt && MI.getOpcode() == Hexagon::A2_sxtw) {
|
|
|
|
assert(MI.getNumOperands() == 2);
|
|
|
|
MachineOperand &Dst = MI.getOperand(0);
|
|
|
|
MachineOperand &Src = MI.getOperand(1);
|
2012-02-16 02:52:27 +08:00
|
|
|
unsigned DstReg = Dst.getReg();
|
|
|
|
unsigned SrcReg = Src.getReg();
|
|
|
|
// Just handle virtual registers.
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(DstReg) &&
|
|
|
|
TargetRegisterInfo::isVirtualRegister(SrcReg)) {
|
|
|
|
// Map the following:
|
|
|
|
// %vreg170<def> = SXTW %vreg166
|
|
|
|
// PeepholeMap[170] = vreg166
|
|
|
|
PeepholeMap[DstReg] = SrcReg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 04:22:51 +08:00
|
|
|
// Look for %vreg170<def> = COMBINE_ir_V4 (0, %vreg169)
|
|
|
|
// %vreg170:DoublRegs, %vreg169:IntRegs
|
2016-07-12 09:55:32 +08:00
|
|
|
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);
|
2013-05-03 04:22:51 +08:00
|
|
|
if (Src1.getImm() != 0)
|
|
|
|
continue;
|
|
|
|
unsigned DstReg = Dst.getReg();
|
|
|
|
unsigned SrcReg = Src2.getReg();
|
|
|
|
PeepholeMap[DstReg] = SrcReg;
|
|
|
|
}
|
|
|
|
|
2012-09-06 00:01:40 +08:00
|
|
|
// Look for this sequence below
|
|
|
|
// %vregDoubleReg1 = LSRd_ri %vregDoubleReg0, 32
|
2016-11-10 00:19:08 +08:00
|
|
|
// %vregIntReg = COPY %vregDoubleReg1:isub_lo.
|
2012-09-06 00:01:40 +08:00
|
|
|
// and convert into
|
2016-11-10 00:19:08 +08:00
|
|
|
// %vregIntReg = COPY %vregDoubleReg0:isub_hi.
|
2016-07-12 09:55:32 +08:00
|
|
|
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);
|
2012-09-06 00:01:40 +08:00
|
|
|
if (Src2.getImm() != 32)
|
|
|
|
continue;
|
|
|
|
unsigned DstReg = Dst.getReg();
|
|
|
|
unsigned SrcReg = Src1.getReg();
|
|
|
|
PeepholeDoubleRegsMap[DstReg] =
|
2016-11-10 00:19:08 +08:00
|
|
|
std::make_pair(*&SrcReg, Hexagon::isub_hi);
|
2012-09-06 00:01:40 +08:00
|
|
|
}
|
|
|
|
|
2012-02-16 02:52:27 +08:00
|
|
|
// Look for P=NOT(P).
|
2016-07-12 09:55:32 +08:00
|
|
|
if (!DisablePNotP && MI.getOpcode() == Hexagon::C2_not) {
|
|
|
|
assert(MI.getNumOperands() == 2);
|
|
|
|
MachineOperand &Dst = MI.getOperand(0);
|
|
|
|
MachineOperand &Src = MI.getOperand(1);
|
2012-02-16 02:52:27 +08:00
|
|
|
unsigned DstReg = Dst.getReg();
|
|
|
|
unsigned SrcReg = Src.getReg();
|
|
|
|
// Just handle virtual registers.
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(DstReg) &&
|
|
|
|
TargetRegisterInfo::isVirtualRegister(SrcReg)) {
|
|
|
|
// Map the following:
|
|
|
|
// %vreg170<def> = NOT_xx %vreg166
|
|
|
|
// PeepholeMap[170] = vreg166
|
|
|
|
PeepholeMap[DstReg] = SrcReg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for copy:
|
2016-11-10 00:19:08 +08:00
|
|
|
// %vreg176<def> = COPY %vreg170:isub_lo
|
2016-07-12 09:55:32 +08:00
|
|
|
if (!DisableOptSZExt && MI.isCopy()) {
|
|
|
|
assert(MI.getNumOperands() == 2);
|
|
|
|
MachineOperand &Dst = MI.getOperand(0);
|
|
|
|
MachineOperand &Src = MI.getOperand(1);
|
2012-02-16 02:52:27 +08:00
|
|
|
|
|
|
|
// Make sure we are copying the lower 32 bits.
|
2016-11-10 00:19:08 +08:00
|
|
|
if (Src.getSubReg() != Hexagon::isub_lo)
|
2012-02-16 02:52:27 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned DstReg = Dst.getReg();
|
|
|
|
unsigned SrcReg = Src.getReg();
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(DstReg) &&
|
|
|
|
TargetRegisterInfo::isVirtualRegister(SrcReg)) {
|
|
|
|
// Try to find in the map.
|
|
|
|
if (unsigned PeepholeSrc = PeepholeMap.lookup(SrcReg)) {
|
|
|
|
// Change the 1st operand.
|
2016-07-12 09:55:32 +08:00
|
|
|
MI.RemoveOperand(1);
|
|
|
|
MI.addOperand(MachineOperand::CreateReg(PeepholeSrc, false));
|
2012-09-06 00:01:40 +08:00
|
|
|
} else {
|
|
|
|
DenseMap<unsigned, std::pair<unsigned, unsigned> >::iterator DI =
|
|
|
|
PeepholeDoubleRegsMap.find(SrcReg);
|
|
|
|
if (DI != PeepholeDoubleRegsMap.end()) {
|
|
|
|
std::pair<unsigned,unsigned> PeepholeSrc = DI->second;
|
2016-07-12 09:55:32 +08:00
|
|
|
MI.RemoveOperand(1);
|
|
|
|
MI.addOperand(MachineOperand::CreateReg(
|
|
|
|
PeepholeSrc.first, false /*isDef*/, false /*isImp*/,
|
|
|
|
false /*isKill*/, false /*isDead*/, false /*isUndef*/,
|
|
|
|
false /*isEarlyClobber*/, PeepholeSrc.second));
|
2012-09-06 00:01:40 +08:00
|
|
|
}
|
2012-02-16 02:52:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for Predicated instructions.
|
|
|
|
if (!DisablePNotP) {
|
|
|
|
bool Done = false;
|
2016-07-12 09:55:32 +08:00
|
|
|
if (QII->isPredicated(MI)) {
|
|
|
|
MachineOperand &Op0 = MI.getOperand(0);
|
2012-02-16 02:52:27 +08:00
|
|
|
unsigned Reg0 = Op0.getReg();
|
|
|
|
const TargetRegisterClass *RC0 = MRI->getRegClass(Reg0);
|
|
|
|
if (RC0->getID() == Hexagon::PredRegsRegClassID) {
|
|
|
|
// Handle instructions that have a prediate register in op0
|
|
|
|
// (most cases of predicable instructions).
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(Reg0)) {
|
|
|
|
// Try to find in the map.
|
|
|
|
if (unsigned PeepholeSrc = PeepholeMap.lookup(Reg0)) {
|
|
|
|
// Change the 1st operand and, flip the opcode.
|
2016-07-12 09:55:32 +08:00
|
|
|
MI.getOperand(0).setReg(PeepholeSrc);
|
2016-08-04 22:17:16 +08:00
|
|
|
MRI->clearKillFlags(PeepholeSrc);
|
2016-07-12 09:55:32 +08:00
|
|
|
int NewOp = QII->getInvertedPredicatedOpcode(MI.getOpcode());
|
|
|
|
MI.setDesc(QII->get(NewOp));
|
2012-02-16 02:52:27 +08:00
|
|
|
Done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Done) {
|
|
|
|
// Handle special instructions.
|
2016-07-12 09:55:32 +08:00
|
|
|
unsigned Op = MI.getOpcode();
|
2012-02-16 02:52:27 +08:00
|
|
|
unsigned NewOp = 0;
|
|
|
|
unsigned PR = 1, S1 = 2, S2 = 3; // Operand indices.
|
|
|
|
|
|
|
|
switch (Op) {
|
2014-11-26 04:20:09 +08:00
|
|
|
case Hexagon::C2_mux:
|
2014-12-06 05:09:27 +08:00
|
|
|
case Hexagon::C2_muxii:
|
2012-02-16 02:52:27 +08:00
|
|
|
NewOp = Op;
|
|
|
|
break;
|
2014-12-06 05:09:27 +08:00
|
|
|
case Hexagon::C2_muxri:
|
|
|
|
NewOp = Hexagon::C2_muxir;
|
2012-02-16 02:52:27 +08:00
|
|
|
break;
|
2014-12-06 05:09:27 +08:00
|
|
|
case Hexagon::C2_muxir:
|
|
|
|
NewOp = Hexagon::C2_muxri;
|
2012-02-16 02:52:27 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (NewOp) {
|
2016-07-12 09:55:32 +08:00
|
|
|
unsigned PSrc = MI.getOperand(PR).getReg();
|
2012-02-16 02:52:27 +08:00
|
|
|
if (unsigned POrig = PeepholeMap.lookup(PSrc)) {
|
2017-06-22 05:03:34 +08:00
|
|
|
BuildMI(*MBB, MI.getIterator(), MI.getDebugLoc(),
|
|
|
|
QII->get(NewOp), MI.getOperand(0).getReg())
|
|
|
|
.addReg(POrig)
|
|
|
|
.add(MI.getOperand(S2))
|
|
|
|
.add(MI.getOperand(S1));
|
2016-08-04 22:17:16 +08:00
|
|
|
MRI->clearKillFlags(POrig);
|
2017-06-22 05:03:34 +08:00
|
|
|
MI.eraseFromParent();
|
2012-02-16 02:52:27 +08:00
|
|
|
}
|
|
|
|
} // if (NewOp)
|
|
|
|
} // if (!Done)
|
|
|
|
|
|
|
|
} // if (!DisablePNotP)
|
|
|
|
|
|
|
|
} // Instruction
|
|
|
|
} // Basic Block
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createHexagonPeephole() {
|
|
|
|
return new HexagonPeephole();
|
|
|
|
}
|