2012-05-12 13:10:30 +08:00
|
|
|
//===----- HexagonNewValueJump.cpp - Hexagon Backend New Value Jump -------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This implements NewValueJump pass in Hexagon.
|
|
|
|
// Ideally, we should merge this as a Peephole pass prior to register
|
2012-06-02 18:20:22 +08:00
|
|
|
// allocation, but because we have a spill in between the feeder and new value
|
2012-05-12 13:10:30 +08:00
|
|
|
// jump instructions, we are forced to write after register allocation.
|
2012-06-02 18:20:22 +08:00
|
|
|
// Having said that, we should re-attempt to pull this earlier at some point
|
2012-05-12 13:10:30 +08:00
|
|
|
// in future.
|
|
|
|
|
|
|
|
// The basic approach looks for sequence of predicated jump, compare instruciton
|
|
|
|
// that genereates the predicate and, the feeder to the predicate. Once it finds
|
|
|
|
// all, it collapses compare and jump instruction into a new valu jump
|
|
|
|
// intstructions.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "Hexagon.h"
|
|
|
|
#include "HexagonInstrInfo.h"
|
|
|
|
#include "HexagonMachineFunctionInfo.h"
|
|
|
|
#include "HexagonRegisterInfo.h"
|
|
|
|
#include "HexagonSubtarget.h"
|
|
|
|
#include "HexagonTargetMachine.h"
|
2012-05-12 13:10:30 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2013-05-07 02:49:23 +08:00
|
|
|
#include "llvm/CodeGen/LiveVariables.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/CodeGen/ScheduleDAGInstrs.h"
|
2016-04-18 17:17:29 +08:00
|
|
|
#include "llvm/PassSupport.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2015-03-24 03:32:43 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-05-07 02:49:23 +08:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2012-05-12 13:10:30 +08:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:41:26 +08:00
|
|
|
#define DEBUG_TYPE "hexagon-nvj"
|
|
|
|
|
2012-05-12 13:10:30 +08:00
|
|
|
STATISTIC(NumNVJGenerated, "Number of New Value Jump Instructions created");
|
|
|
|
|
|
|
|
static cl::opt<int>
|
|
|
|
DbgNVJCount("nvj-count", cl::init(-1), cl::Hidden, cl::desc(
|
|
|
|
"Maximum number of predicated jumps to be converted to New Value Jump"));
|
|
|
|
|
|
|
|
static cl::opt<bool> DisableNewValueJumps("disable-nvjump", cl::Hidden,
|
|
|
|
cl::ZeroOrMore, cl::init(false),
|
|
|
|
cl::desc("Disable New Value Jumps"));
|
|
|
|
|
2013-05-07 02:49:23 +08:00
|
|
|
namespace llvm {
|
2015-06-16 03:05:35 +08:00
|
|
|
FunctionPass *createHexagonNewValueJump();
|
2013-05-07 02:49:23 +08:00
|
|
|
void initializeHexagonNewValueJumpPass(PassRegistry&);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-12 13:10:30 +08:00
|
|
|
namespace {
|
|
|
|
struct HexagonNewValueJump : public MachineFunctionPass {
|
|
|
|
const HexagonInstrInfo *QII;
|
|
|
|
const HexagonRegisterInfo *QRI;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
2013-05-07 02:49:23 +08:00
|
|
|
HexagonNewValueJump() : MachineFunctionPass(ID) {
|
|
|
|
initializeHexagonNewValueJumpPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2012-05-12 13:10:30 +08:00
|
|
|
|
2014-04-29 15:58:16 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2013-05-02 23:39:30 +08:00
|
|
|
AU.addRequired<MachineBranchProbabilityInfo>();
|
2012-05-12 13:10:30 +08:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override { return "Hexagon NewValueJump"; }
|
2012-05-12 13:10:30 +08:00
|
|
|
|
2014-04-29 15:58:16 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &Fn) override;
|
2016-04-05 01:09:25 +08:00
|
|
|
MachineFunctionProperties getRequiredProperties() const override {
|
|
|
|
return MachineFunctionProperties().set(
|
2016-08-25 09:27:13 +08:00
|
|
|
MachineFunctionProperties::Property::NoVRegs);
|
2016-04-05 01:09:25 +08:00
|
|
|
}
|
2012-05-12 13:10:30 +08:00
|
|
|
|
|
|
|
private:
|
2013-05-02 23:39:30 +08:00
|
|
|
/// \brief A handle to the branch probability pass.
|
|
|
|
const MachineBranchProbabilityInfo *MBPI;
|
2012-05-12 13:10:30 +08:00
|
|
|
|
2016-07-12 09:55:32 +08:00
|
|
|
bool isNewValueJumpCandidate(const MachineInstr &MI) const;
|
2012-05-12 13:10:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
char HexagonNewValueJump::ID = 0;
|
|
|
|
|
2013-05-07 02:49:23 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(HexagonNewValueJump, "hexagon-nvj",
|
|
|
|
"Hexagon NewValueJump", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
|
|
|
|
INITIALIZE_PASS_END(HexagonNewValueJump, "hexagon-nvj",
|
|
|
|
"Hexagon NewValueJump", false, false)
|
|
|
|
|
|
|
|
|
2012-05-12 13:10:30 +08:00
|
|
|
// We have identified this II could be feeder to NVJ,
|
|
|
|
// verify that it can be.
|
|
|
|
static bool canBeFeederToNewValueJump(const HexagonInstrInfo *QII,
|
|
|
|
const TargetRegisterInfo *TRI,
|
|
|
|
MachineBasicBlock::iterator II,
|
|
|
|
MachineBasicBlock::iterator end,
|
|
|
|
MachineBasicBlock::iterator skip,
|
|
|
|
MachineFunction &MF) {
|
|
|
|
|
|
|
|
// Predicated instruction can not be feeder to NVJ.
|
2016-02-23 10:46:52 +08:00
|
|
|
if (QII->isPredicated(*II))
|
2012-05-12 13:10:30 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Bail out if feederReg is a paired register (double regs in
|
|
|
|
// our case). One would think that we can check to see if a given
|
|
|
|
// register cmpReg1 or cmpReg2 is a sub register of feederReg
|
|
|
|
// using -- if (QRI->isSubRegister(feederReg, cmpReg1) logic
|
|
|
|
// before the callsite of this function
|
|
|
|
// But we can not as it comes in the following fashion.
|
|
|
|
// %D0<def> = Hexagon_S2_lsr_r_p %D0<kill>, %R2<kill>
|
|
|
|
// %R0<def> = KILL %R0, %D0<imp-use,kill>
|
|
|
|
// %P0<def> = CMPEQri %R0<kill>, 0
|
|
|
|
// Hence, we need to check if it's a KILL instruction.
|
|
|
|
if (II->getOpcode() == TargetOpcode::KILL)
|
|
|
|
return false;
|
|
|
|
|
2017-02-24 01:47:34 +08:00
|
|
|
if (II->isImplicitDef())
|
|
|
|
return false;
|
2012-05-12 13:10:30 +08:00
|
|
|
|
|
|
|
// Make sure there there is no 'def' or 'use' of any of the uses of
|
|
|
|
// feeder insn between it's definition, this MI and jump, jmpInst
|
|
|
|
// skipping compare, cmpInst.
|
|
|
|
// Here's the example.
|
|
|
|
// r21=memub(r22+r24<<#0)
|
|
|
|
// p0 = cmp.eq(r21, #0)
|
|
|
|
// r4=memub(r3+r21<<#0)
|
|
|
|
// if (p0.new) jump:t .LBB29_45
|
|
|
|
// Without this check, it will be converted into
|
|
|
|
// r4=memub(r3+r21<<#0)
|
|
|
|
// r21=memub(r22+r24<<#0)
|
|
|
|
// p0 = cmp.eq(r21, #0)
|
|
|
|
// if (p0.new) jump:t .LBB29_45
|
|
|
|
// and result WAR hazards if converted to New Value Jump.
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < II->getNumOperands(); ++i) {
|
|
|
|
if (II->getOperand(i).isReg() &&
|
|
|
|
(II->getOperand(i).isUse() || II->getOperand(i).isDef())) {
|
|
|
|
MachineBasicBlock::iterator localII = II;
|
|
|
|
++localII;
|
|
|
|
unsigned Reg = II->getOperand(i).getReg();
|
|
|
|
for (MachineBasicBlock::iterator localBegin = localII;
|
|
|
|
localBegin != end; ++localBegin) {
|
|
|
|
if (localBegin == skip ) continue;
|
|
|
|
// Check for Subregisters too.
|
|
|
|
if (localBegin->modifiesRegister(Reg, TRI) ||
|
|
|
|
localBegin->readsRegister(Reg, TRI))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// These are the common checks that need to performed
|
|
|
|
// to determine if
|
|
|
|
// 1. compare instruction can be moved before jump.
|
|
|
|
// 2. feeder to the compare instruction can be moved before jump.
|
|
|
|
static bool commonChecksToProhibitNewValueJump(bool afterRA,
|
|
|
|
MachineBasicBlock::iterator MII) {
|
|
|
|
|
|
|
|
// If store in path, bail out.
|
|
|
|
if (MII->getDesc().mayStore())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// if call in path, bail out.
|
2016-08-20 01:54:49 +08:00
|
|
|
if (MII->isCall())
|
2012-05-12 13:10:30 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// if NVJ is running prior to RA, do the following checks.
|
|
|
|
if (!afterRA) {
|
|
|
|
// The following Target Opcode instructions are spurious
|
|
|
|
// to new value jump. If they are in the path, bail out.
|
|
|
|
// KILL sets kill flag on the opcode. It also sets up a
|
|
|
|
// single register, out of pair.
|
2016-08-20 01:54:49 +08:00
|
|
|
// %D0<def> = S2_lsr_r_p %D0<kill>, %R2<kill>
|
2012-05-12 13:10:30 +08:00
|
|
|
// %R0<def> = KILL %R0, %D0<imp-use,kill>
|
2016-08-20 01:54:49 +08:00
|
|
|
// %P0<def> = C2_cmpeqi %R0<kill>, 0
|
2012-05-12 13:10:30 +08:00
|
|
|
// PHI can be anything after RA.
|
|
|
|
// COPY can remateriaze things in between feeder, compare and nvj.
|
|
|
|
if (MII->getOpcode() == TargetOpcode::KILL ||
|
|
|
|
MII->getOpcode() == TargetOpcode::PHI ||
|
|
|
|
MII->getOpcode() == TargetOpcode::COPY)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// The following pseudo Hexagon instructions sets "use" and "def"
|
|
|
|
// of registers by individual passes in the backend. At this time,
|
|
|
|
// we don't know the scope of usage and definitions of these
|
|
|
|
// instructions.
|
2016-08-20 01:54:49 +08:00
|
|
|
if (MII->getOpcode() == Hexagon::LDriw_pred ||
|
2012-05-12 13:10:30 +08:00
|
|
|
MII->getOpcode() == Hexagon::STriw_pred)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool canCompareBeNewValueJump(const HexagonInstrInfo *QII,
|
|
|
|
const TargetRegisterInfo *TRI,
|
|
|
|
MachineBasicBlock::iterator II,
|
|
|
|
unsigned pReg,
|
|
|
|
bool secondReg,
|
|
|
|
bool optLocation,
|
|
|
|
MachineBasicBlock::iterator end,
|
|
|
|
MachineFunction &MF) {
|
|
|
|
|
2016-07-12 09:55:32 +08:00
|
|
|
MachineInstr &MI = *II;
|
2012-05-12 13:10:30 +08:00
|
|
|
|
|
|
|
// If the second operand of the compare is an imm, make sure it's in the
|
|
|
|
// range specified by the arch.
|
|
|
|
if (!secondReg) {
|
2016-07-12 09:55:32 +08:00
|
|
|
int64_t v = MI.getOperand(2).getImm();
|
2016-08-20 01:54:49 +08:00
|
|
|
bool Valid = false;
|
2012-05-12 13:10:30 +08:00
|
|
|
|
2016-08-20 01:54:49 +08:00
|
|
|
switch (MI.getOpcode()) {
|
|
|
|
case Hexagon::C2_cmpeqi:
|
|
|
|
case Hexagon::C2_cmpgti:
|
|
|
|
Valid = (isUInt<5>(v) || v == -1);
|
|
|
|
break;
|
|
|
|
case Hexagon::C2_cmpgtui:
|
|
|
|
Valid = isUInt<5>(v);
|
|
|
|
break;
|
|
|
|
case Hexagon::S2_tstbit_i:
|
|
|
|
case Hexagon::S4_ntstbit_i:
|
|
|
|
Valid = (v == 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Valid)
|
2012-05-12 13:10:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-07 02:49:23 +08:00
|
|
|
unsigned cmpReg1, cmpOp2 = 0; // cmpOp2 assignment silences compiler warning.
|
2016-07-12 09:55:32 +08:00
|
|
|
cmpReg1 = MI.getOperand(1).getReg();
|
2012-05-12 13:10:30 +08:00
|
|
|
|
|
|
|
if (secondReg) {
|
2016-07-12 09:55:32 +08:00
|
|
|
cmpOp2 = MI.getOperand(2).getReg();
|
2012-05-12 13:10:30 +08:00
|
|
|
|
2016-08-20 01:54:49 +08:00
|
|
|
// If the same register appears as both operands, we cannot generate a new
|
|
|
|
// value compare. Only one operand may use the .new suffix.
|
|
|
|
if (cmpReg1 == cmpOp2)
|
|
|
|
return false;
|
|
|
|
|
2012-05-12 13:10:30 +08:00
|
|
|
// Make sure that that second register is not from COPY
|
|
|
|
// At machine code level, we don't need this, but if we decide
|
|
|
|
// to move new value jump prior to RA, we would be needing this.
|
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
if (secondReg && !TargetRegisterInfo::isPhysicalRegister(cmpOp2)) {
|
|
|
|
MachineInstr *def = MRI.getVRegDef(cmpOp2);
|
|
|
|
if (def->getOpcode() == TargetOpcode::COPY)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk the instructions after the compare (predicate def) to the jump,
|
|
|
|
// and satisfy the following conditions.
|
|
|
|
++II ;
|
|
|
|
for (MachineBasicBlock::iterator localII = II; localII != end;
|
|
|
|
++localII) {
|
2016-08-20 01:54:49 +08:00
|
|
|
if (localII->isDebugValue())
|
|
|
|
continue;
|
2012-05-12 13:10:30 +08:00
|
|
|
|
|
|
|
// Check 1.
|
|
|
|
// If "common" checks fail, bail out.
|
|
|
|
if (!commonChecksToProhibitNewValueJump(optLocation, localII))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check 2.
|
|
|
|
// If there is a def or use of predicate (result of compare), bail out.
|
|
|
|
if (localII->modifiesRegister(pReg, TRI) ||
|
|
|
|
localII->readsRegister(pReg, TRI))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check 3.
|
|
|
|
// If there is a def of any of the use of the compare (operands of compare),
|
|
|
|
// bail out.
|
|
|
|
// Eg.
|
|
|
|
// p0 = cmp.eq(r2, r0)
|
|
|
|
// r2 = r4
|
|
|
|
// if (p0.new) jump:t .LBB28_3
|
|
|
|
if (localII->modifiesRegister(cmpReg1, TRI) ||
|
|
|
|
(secondReg && localII->modifiesRegister(cmpOp2, TRI)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-24 22:55:26 +08:00
|
|
|
|
|
|
|
// Given a compare operator, return a matching New Value Jump compare operator.
|
|
|
|
// Make sure that MI here is included in isNewValueJumpCandidate.
|
2013-05-02 23:39:30 +08:00
|
|
|
static unsigned getNewValueJumpOpcode(MachineInstr *MI, int reg,
|
|
|
|
bool secondRegNewified,
|
|
|
|
MachineBasicBlock *jmpTarget,
|
|
|
|
const MachineBranchProbabilityInfo
|
|
|
|
*MBPI) {
|
|
|
|
bool taken = false;
|
|
|
|
MachineBasicBlock *Src = MI->getParent();
|
|
|
|
const BranchProbability Prediction =
|
|
|
|
MBPI->getEdgeProbability(Src, jmpTarget);
|
|
|
|
|
|
|
|
if (Prediction >= BranchProbability(1,2))
|
|
|
|
taken = true;
|
|
|
|
|
2012-05-12 13:10:30 +08:00
|
|
|
switch (MI->getOpcode()) {
|
2014-11-26 02:20:52 +08:00
|
|
|
case Hexagon::C2_cmpeq:
|
2015-02-06 06:03:32 +08:00
|
|
|
return taken ? Hexagon::J4_cmpeq_t_jumpnv_t
|
|
|
|
: Hexagon::J4_cmpeq_t_jumpnv_nt;
|
2012-05-12 13:10:30 +08:00
|
|
|
|
2014-11-27 03:43:12 +08:00
|
|
|
case Hexagon::C2_cmpeqi: {
|
2012-05-12 13:10:30 +08:00
|
|
|
if (reg >= 0)
|
2015-02-06 06:03:32 +08:00
|
|
|
return taken ? Hexagon::J4_cmpeqi_t_jumpnv_t
|
|
|
|
: Hexagon::J4_cmpeqi_t_jumpnv_nt;
|
2012-05-12 13:10:30 +08:00
|
|
|
else
|
2015-02-06 06:03:32 +08:00
|
|
|
return taken ? Hexagon::J4_cmpeqn1_t_jumpnv_t
|
|
|
|
: Hexagon::J4_cmpeqn1_t_jumpnv_nt;
|
2012-05-12 13:10:30 +08:00
|
|
|
}
|
|
|
|
|
2014-11-26 02:20:52 +08:00
|
|
|
case Hexagon::C2_cmpgt: {
|
2012-05-12 13:10:30 +08:00
|
|
|
if (secondRegNewified)
|
2015-02-06 06:03:32 +08:00
|
|
|
return taken ? Hexagon::J4_cmplt_t_jumpnv_t
|
|
|
|
: Hexagon::J4_cmplt_t_jumpnv_nt;
|
2012-05-12 13:10:30 +08:00
|
|
|
else
|
2015-02-06 06:03:32 +08:00
|
|
|
return taken ? Hexagon::J4_cmpgt_t_jumpnv_t
|
|
|
|
: Hexagon::J4_cmpgt_t_jumpnv_nt;
|
2012-05-12 13:10:30 +08:00
|
|
|
}
|
|
|
|
|
2014-11-27 03:43:12 +08:00
|
|
|
case Hexagon::C2_cmpgti: {
|
2012-05-12 13:10:30 +08:00
|
|
|
if (reg >= 0)
|
2015-02-06 06:03:32 +08:00
|
|
|
return taken ? Hexagon::J4_cmpgti_t_jumpnv_t
|
|
|
|
: Hexagon::J4_cmpgti_t_jumpnv_nt;
|
2012-05-12 13:10:30 +08:00
|
|
|
else
|
2015-02-06 06:03:32 +08:00
|
|
|
return taken ? Hexagon::J4_cmpgtn1_t_jumpnv_t
|
|
|
|
: Hexagon::J4_cmpgtn1_t_jumpnv_nt;
|
2012-05-12 13:10:30 +08:00
|
|
|
}
|
|
|
|
|
2014-11-26 02:20:52 +08:00
|
|
|
case Hexagon::C2_cmpgtu: {
|
2012-05-12 13:10:30 +08:00
|
|
|
if (secondRegNewified)
|
2015-02-06 06:03:32 +08:00
|
|
|
return taken ? Hexagon::J4_cmpltu_t_jumpnv_t
|
|
|
|
: Hexagon::J4_cmpltu_t_jumpnv_nt;
|
2012-05-12 13:10:30 +08:00
|
|
|
else
|
2015-02-06 06:03:32 +08:00
|
|
|
return taken ? Hexagon::J4_cmpgtu_t_jumpnv_t
|
|
|
|
: Hexagon::J4_cmpgtu_t_jumpnv_nt;
|
2012-05-12 13:10:30 +08:00
|
|
|
}
|
|
|
|
|
2014-11-27 03:43:12 +08:00
|
|
|
case Hexagon::C2_cmpgtui:
|
2015-02-06 06:03:32 +08:00
|
|
|
return taken ? Hexagon::J4_cmpgtui_t_jumpnv_t
|
|
|
|
: Hexagon::J4_cmpgtui_t_jumpnv_nt;
|
2012-05-12 13:10:30 +08:00
|
|
|
|
2015-12-09 00:28:32 +08:00
|
|
|
case Hexagon::C4_cmpneq:
|
|
|
|
return taken ? Hexagon::J4_cmpeq_f_jumpnv_t
|
|
|
|
: Hexagon::J4_cmpeq_f_jumpnv_nt;
|
|
|
|
|
|
|
|
case Hexagon::C4_cmplte:
|
|
|
|
if (secondRegNewified)
|
|
|
|
return taken ? Hexagon::J4_cmplt_f_jumpnv_t
|
|
|
|
: Hexagon::J4_cmplt_f_jumpnv_nt;
|
|
|
|
return taken ? Hexagon::J4_cmpgt_f_jumpnv_t
|
|
|
|
: Hexagon::J4_cmpgt_f_jumpnv_nt;
|
|
|
|
|
|
|
|
case Hexagon::C4_cmplteu:
|
|
|
|
if (secondRegNewified)
|
|
|
|
return taken ? Hexagon::J4_cmpltu_f_jumpnv_t
|
|
|
|
: Hexagon::J4_cmpltu_f_jumpnv_nt;
|
|
|
|
return taken ? Hexagon::J4_cmpgtu_f_jumpnv_t
|
|
|
|
: Hexagon::J4_cmpgtu_f_jumpnv_nt;
|
|
|
|
|
2012-05-12 13:10:30 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Could not find matching New Value Jump instruction.");
|
|
|
|
}
|
|
|
|
// return *some value* to avoid compiler warning
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-12 09:55:32 +08:00
|
|
|
bool HexagonNewValueJump::isNewValueJumpCandidate(
|
|
|
|
const MachineInstr &MI) const {
|
|
|
|
switch (MI.getOpcode()) {
|
|
|
|
case Hexagon::C2_cmpeq:
|
|
|
|
case Hexagon::C2_cmpeqi:
|
|
|
|
case Hexagon::C2_cmpgt:
|
|
|
|
case Hexagon::C2_cmpgti:
|
|
|
|
case Hexagon::C2_cmpgtu:
|
|
|
|
case Hexagon::C2_cmpgtui:
|
|
|
|
case Hexagon::C4_cmpneq:
|
|
|
|
case Hexagon::C4_cmplte:
|
|
|
|
case Hexagon::C4_cmplteu:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
2015-11-24 22:55:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-12 13:10:30 +08:00
|
|
|
bool HexagonNewValueJump::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "********** Hexagon New Value Jump **********\n"
|
|
|
|
<< "********** Function: "
|
2012-08-22 14:07:19 +08:00
|
|
|
<< MF.getName() << "\n");
|
2012-05-12 13:10:30 +08:00
|
|
|
|
2016-04-27 03:46:28 +08:00
|
|
|
if (skipFunction(*MF.getFunction()))
|
|
|
|
return false;
|
|
|
|
|
2015-02-03 06:11:42 +08:00
|
|
|
// If we move NewValueJump before register allocation we'll need live variable
|
|
|
|
// analysis here too.
|
2012-05-12 13:10:30 +08:00
|
|
|
|
2014-08-05 10:39:49 +08:00
|
|
|
QII = static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo());
|
2014-08-05 05:25:23 +08:00
|
|
|
QRI = static_cast<const HexagonRegisterInfo *>(
|
2014-08-05 10:39:49 +08:00
|
|
|
MF.getSubtarget().getRegisterInfo());
|
2013-05-02 23:39:30 +08:00
|
|
|
MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
|
2012-05-12 13:10:30 +08:00
|
|
|
|
2015-02-10 05:56:37 +08:00
|
|
|
if (DisableNewValueJumps) {
|
2012-05-12 13:10:30 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nvjCount = DbgNVJCount;
|
|
|
|
int nvjGenerated = 0;
|
|
|
|
|
|
|
|
// Loop through all the bb's of the function
|
|
|
|
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-05-12 13:10:30 +08:00
|
|
|
|
|
|
|
DEBUG(dbgs() << "** dumping bb ** "
|
|
|
|
<< MBB->getNumber() << "\n");
|
|
|
|
DEBUG(MBB->dump());
|
|
|
|
DEBUG(dbgs() << "\n" << "********** dumping instr bottom up **********\n");
|
|
|
|
bool foundJump = false;
|
|
|
|
bool foundCompare = false;
|
|
|
|
bool invertPredicate = false;
|
|
|
|
unsigned predReg = 0; // predicate reg of the jump.
|
|
|
|
unsigned cmpReg1 = 0;
|
|
|
|
int cmpOp2 = 0;
|
|
|
|
bool MO1IsKill = false;
|
|
|
|
bool MO2IsKill = false;
|
|
|
|
MachineBasicBlock::iterator jmpPos;
|
|
|
|
MachineBasicBlock::iterator cmpPos;
|
2014-04-25 13:30:21 +08:00
|
|
|
MachineInstr *cmpInstr = nullptr, *jmpInstr = nullptr;
|
|
|
|
MachineBasicBlock *jmpTarget = nullptr;
|
2012-05-12 13:10:30 +08:00
|
|
|
bool afterRA = false;
|
|
|
|
bool isSecondOpReg = false;
|
|
|
|
bool isSecondOpNewified = false;
|
|
|
|
// Traverse the basic block - bottom up
|
|
|
|
for (MachineBasicBlock::iterator MII = MBB->end(), E = MBB->begin();
|
|
|
|
MII != E;) {
|
2016-07-12 09:55:32 +08:00
|
|
|
MachineInstr &MI = *--MII;
|
|
|
|
if (MI.isDebugValue()) {
|
2012-05-12 13:10:30 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((nvjCount == 0) || (nvjCount > -1 && nvjCount <= nvjGenerated))
|
|
|
|
break;
|
|
|
|
|
2016-07-12 09:55:32 +08:00
|
|
|
DEBUG(dbgs() << "Instr: "; MI.dump(); dbgs() << "\n");
|
2012-05-12 13:10:30 +08:00
|
|
|
|
2016-07-12 09:55:32 +08:00
|
|
|
if (!foundJump && (MI.getOpcode() == Hexagon::J2_jumpt ||
|
2016-08-19 22:14:09 +08:00
|
|
|
MI.getOpcode() == Hexagon::J2_jumptpt ||
|
2016-07-12 09:55:32 +08:00
|
|
|
MI.getOpcode() == Hexagon::J2_jumpf ||
|
2016-08-19 22:14:09 +08:00
|
|
|
MI.getOpcode() == Hexagon::J2_jumpfpt ||
|
2016-07-12 09:55:32 +08:00
|
|
|
MI.getOpcode() == Hexagon::J2_jumptnewpt ||
|
|
|
|
MI.getOpcode() == Hexagon::J2_jumptnew ||
|
|
|
|
MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
|
|
|
|
MI.getOpcode() == Hexagon::J2_jumpfnew)) {
|
2012-05-12 13:10:30 +08:00
|
|
|
// This is where you would insert your compare and
|
|
|
|
// instr that feeds compare
|
|
|
|
jmpPos = MII;
|
2016-07-12 09:55:32 +08:00
|
|
|
jmpInstr = &MI;
|
|
|
|
predReg = MI.getOperand(0).getReg();
|
2012-05-12 13:10:30 +08:00
|
|
|
afterRA = TargetRegisterInfo::isPhysicalRegister(predReg);
|
|
|
|
|
|
|
|
// If ifconverter had not messed up with the kill flags of the
|
|
|
|
// operands, the following check on the kill flag would suffice.
|
|
|
|
// if(!jmpInstr->getOperand(0).isKill()) break;
|
|
|
|
|
|
|
|
// This predicate register is live out out of BB
|
|
|
|
// this would only work if we can actually use Live
|
|
|
|
// variable analysis on phy regs - but LLVM does not
|
|
|
|
// provide LV analysis on phys regs.
|
|
|
|
//if(LVs.isLiveOut(predReg, *MBB)) break;
|
|
|
|
|
|
|
|
// Get all the successors of this block - which will always
|
2016-08-20 01:54:49 +08:00
|
|
|
// be 2. Check if the predicate register is live-in in those
|
2012-05-12 13:10:30 +08:00
|
|
|
// successor. If yes, we can not delete the predicate -
|
|
|
|
// I am doing this only because LLVM does not provide LiveOut
|
|
|
|
// at the BB level.
|
|
|
|
bool predLive = false;
|
|
|
|
for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
|
|
|
|
SIE = MBB->succ_end(); SI != SIE; ++SI) {
|
|
|
|
MachineBasicBlock* succMBB = *SI;
|
|
|
|
if (succMBB->isLiveIn(predReg)) {
|
|
|
|
predLive = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (predLive)
|
|
|
|
break;
|
|
|
|
|
2016-07-12 09:55:32 +08:00
|
|
|
if (!MI.getOperand(1).isMBB())
|
2016-01-14 23:05:27 +08:00
|
|
|
continue;
|
2016-07-12 09:55:32 +08:00
|
|
|
jmpTarget = MI.getOperand(1).getMBB();
|
2012-05-12 13:10:30 +08:00
|
|
|
foundJump = true;
|
2016-07-12 09:55:32 +08:00
|
|
|
if (MI.getOpcode() == Hexagon::J2_jumpf ||
|
|
|
|
MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
|
|
|
|
MI.getOpcode() == Hexagon::J2_jumpfnew) {
|
2012-05-12 13:10:30 +08:00
|
|
|
invertPredicate = true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No new value jump if there is a barrier. A barrier has to be in its
|
|
|
|
// own packet. A barrier has zero operands. We conservatively bail out
|
|
|
|
// here if we see any instruction with zero operands.
|
2016-07-12 09:55:32 +08:00
|
|
|
if (foundJump && MI.getNumOperands() == 0)
|
2012-05-12 13:10:30 +08:00
|
|
|
break;
|
|
|
|
|
2016-07-12 09:55:32 +08:00
|
|
|
if (foundJump && !foundCompare && MI.getOperand(0).isReg() &&
|
|
|
|
MI.getOperand(0).getReg() == predReg) {
|
2012-05-12 13:10:30 +08:00
|
|
|
|
|
|
|
// Not all compares can be new value compare. Arch Spec: 7.6.1.1
|
2015-11-24 22:55:26 +08:00
|
|
|
if (isNewValueJumpCandidate(MI)) {
|
2012-05-12 13:10:30 +08:00
|
|
|
|
2016-07-12 09:55:32 +08:00
|
|
|
assert(
|
|
|
|
(MI.getDesc().isCompare()) &&
|
2012-05-12 13:10:30 +08:00
|
|
|
"Only compare instruction can be collapsed into New Value Jump");
|
2016-07-12 09:55:32 +08:00
|
|
|
isSecondOpReg = MI.getOperand(2).isReg();
|
2012-05-12 13:10:30 +08:00
|
|
|
|
|
|
|
if (!canCompareBeNewValueJump(QII, QRI, MII, predReg, isSecondOpReg,
|
|
|
|
afterRA, jmpPos, MF))
|
|
|
|
break;
|
|
|
|
|
2016-07-12 09:55:32 +08:00
|
|
|
cmpInstr = &MI;
|
2012-05-12 13:10:30 +08:00
|
|
|
cmpPos = MII;
|
|
|
|
foundCompare = true;
|
|
|
|
|
|
|
|
// We need cmpReg1 and cmpOp2(imm or reg) while building
|
|
|
|
// new value jump instruction.
|
2016-07-12 09:55:32 +08:00
|
|
|
cmpReg1 = MI.getOperand(1).getReg();
|
|
|
|
if (MI.getOperand(1).isKill())
|
2012-05-12 13:10:30 +08:00
|
|
|
MO1IsKill = true;
|
|
|
|
|
|
|
|
if (isSecondOpReg) {
|
2016-07-12 09:55:32 +08:00
|
|
|
cmpOp2 = MI.getOperand(2).getReg();
|
|
|
|
if (MI.getOperand(2).isKill())
|
2012-05-12 13:10:30 +08:00
|
|
|
MO2IsKill = true;
|
|
|
|
} else
|
2016-07-12 09:55:32 +08:00
|
|
|
cmpOp2 = MI.getOperand(2).getImm();
|
2012-05-12 13:10:30 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundCompare && foundJump) {
|
|
|
|
|
|
|
|
// If "common" checks fail, bail out on this BB.
|
|
|
|
if (!commonChecksToProhibitNewValueJump(afterRA, MII))
|
|
|
|
break;
|
|
|
|
|
|
|
|
bool foundFeeder = false;
|
|
|
|
MachineBasicBlock::iterator feederPos = MII;
|
2016-07-12 09:55:32 +08:00
|
|
|
if (MI.getOperand(0).isReg() && MI.getOperand(0).isDef() &&
|
|
|
|
(MI.getOperand(0).getReg() == cmpReg1 ||
|
|
|
|
(isSecondOpReg &&
|
|
|
|
MI.getOperand(0).getReg() == (unsigned)cmpOp2))) {
|
2012-05-12 13:10:30 +08:00
|
|
|
|
2016-07-12 09:55:32 +08:00
|
|
|
unsigned feederReg = MI.getOperand(0).getReg();
|
2012-05-12 13:10:30 +08:00
|
|
|
|
|
|
|
// First try to see if we can get the feeder from the first operand
|
|
|
|
// of the compare. If we can not, and if secondOpReg is true
|
|
|
|
// (second operand of the compare is also register), try that one.
|
|
|
|
// TODO: Try to come up with some heuristic to figure out which
|
|
|
|
// feeder would benefit.
|
|
|
|
|
|
|
|
if (feederReg == cmpReg1) {
|
|
|
|
if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF)) {
|
|
|
|
if (!isSecondOpReg)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
foundFeeder = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!foundFeeder &&
|
|
|
|
isSecondOpReg &&
|
|
|
|
feederReg == (unsigned) cmpOp2)
|
|
|
|
if (!canBeFeederToNewValueJump(QII, QRI, MII, jmpPos, cmpPos, MF))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (isSecondOpReg) {
|
|
|
|
// In case of CMPLT, or CMPLTU, or EQ with the second register
|
|
|
|
// to newify, swap the operands.
|
2016-08-20 01:54:49 +08:00
|
|
|
unsigned COp = cmpInstr->getOpcode();
|
|
|
|
if ((COp == Hexagon::C2_cmpeq || COp == Hexagon::C4_cmpneq) &&
|
|
|
|
(feederReg == (unsigned) cmpOp2)) {
|
2012-05-12 13:10:30 +08:00
|
|
|
unsigned tmp = cmpReg1;
|
|
|
|
bool tmpIsKill = MO1IsKill;
|
|
|
|
cmpReg1 = cmpOp2;
|
|
|
|
MO1IsKill = MO2IsKill;
|
|
|
|
cmpOp2 = tmp;
|
|
|
|
MO2IsKill = tmpIsKill;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we have swapped the operands, all we need to check is,
|
|
|
|
// if the second operand (after swap) is the feeder.
|
|
|
|
// And if it is, make a note.
|
|
|
|
if (feederReg == (unsigned)cmpOp2)
|
|
|
|
isSecondOpNewified = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we are moving feeder close the jump,
|
|
|
|
// make sure we are respecting the kill values of
|
|
|
|
// the operands of the feeder.
|
|
|
|
|
|
|
|
bool updatedIsKill = false;
|
2016-07-12 09:55:32 +08:00
|
|
|
for (unsigned i = 0; i < MI.getNumOperands(); i++) {
|
|
|
|
MachineOperand &MO = MI.getOperand(i);
|
2012-05-12 13:10:30 +08:00
|
|
|
if (MO.isReg() && MO.isUse()) {
|
|
|
|
unsigned feederReg = MO.getReg();
|
|
|
|
for (MachineBasicBlock::iterator localII = feederPos,
|
2017-06-23 05:11:44 +08:00
|
|
|
end = cmpInstr->getIterator(); localII != end; localII++) {
|
2016-07-12 09:55:32 +08:00
|
|
|
MachineInstr &localMI = *localII;
|
|
|
|
for (unsigned j = 0; j < localMI.getNumOperands(); j++) {
|
|
|
|
MachineOperand &localMO = localMI.getOperand(j);
|
2012-05-12 13:10:30 +08:00
|
|
|
if (localMO.isReg() && localMO.isUse() &&
|
|
|
|
localMO.isKill() && feederReg == localMO.getReg()) {
|
|
|
|
// We found that there is kill of a use register
|
|
|
|
// Set up a kill flag on the register
|
|
|
|
localMO.setIsKill(false);
|
|
|
|
MO.setIsKill();
|
|
|
|
updatedIsKill = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (updatedIsKill) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (updatedIsKill) break;
|
|
|
|
}
|
|
|
|
|
2016-07-12 09:55:32 +08:00
|
|
|
MBB->splice(jmpPos, MI.getParent(), MI);
|
|
|
|
MBB->splice(jmpPos, MI.getParent(), cmpInstr);
|
|
|
|
DebugLoc dl = MI.getDebugLoc();
|
2012-05-12 13:10:30 +08:00
|
|
|
MachineInstr *NewMI;
|
|
|
|
|
2016-07-12 09:55:32 +08:00
|
|
|
assert((isNewValueJumpCandidate(*cmpInstr)) &&
|
2015-11-24 22:55:26 +08:00
|
|
|
"This compare is not a New Value Jump candidate.");
|
2012-05-12 13:10:30 +08:00
|
|
|
unsigned opc = getNewValueJumpOpcode(cmpInstr, cmpOp2,
|
2013-05-02 23:39:30 +08:00
|
|
|
isSecondOpNewified,
|
|
|
|
jmpTarget, MBPI);
|
2012-05-12 13:10:30 +08:00
|
|
|
if (invertPredicate)
|
|
|
|
opc = QII->getInvertedPredicatedOpcode(opc);
|
|
|
|
|
2013-04-24 03:15:55 +08:00
|
|
|
if (isSecondOpReg)
|
2012-05-12 13:10:30 +08:00
|
|
|
NewMI = BuildMI(*MBB, jmpPos, dl,
|
|
|
|
QII->get(opc))
|
|
|
|
.addReg(cmpReg1, getKillRegState(MO1IsKill))
|
|
|
|
.addReg(cmpOp2, getKillRegState(MO2IsKill))
|
|
|
|
.addMBB(jmpTarget);
|
2013-04-24 03:15:55 +08:00
|
|
|
|
|
|
|
else
|
2012-05-12 13:10:30 +08:00
|
|
|
NewMI = BuildMI(*MBB, jmpPos, dl,
|
|
|
|
QII->get(opc))
|
|
|
|
.addReg(cmpReg1, getKillRegState(MO1IsKill))
|
|
|
|
.addImm(cmpOp2)
|
|
|
|
.addMBB(jmpTarget);
|
|
|
|
|
|
|
|
assert(NewMI && "New Value Jump Instruction Not created!");
|
2013-05-13 15:50:47 +08:00
|
|
|
(void)NewMI;
|
2012-05-12 13:10:30 +08:00
|
|
|
if (cmpInstr->getOperand(0).isReg() &&
|
|
|
|
cmpInstr->getOperand(0).isKill())
|
|
|
|
cmpInstr->getOperand(0).setIsKill(false);
|
|
|
|
if (cmpInstr->getOperand(1).isReg() &&
|
|
|
|
cmpInstr->getOperand(1).isKill())
|
|
|
|
cmpInstr->getOperand(1).setIsKill(false);
|
|
|
|
cmpInstr->eraseFromParent();
|
|
|
|
jmpInstr->eraseFromParent();
|
|
|
|
++nvjGenerated;
|
|
|
|
++NumNVJGenerated;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createHexagonNewValueJump() {
|
|
|
|
return new HexagonNewValueJump();
|
|
|
|
}
|