57#define DEBUG_TYPE "hexagon-nvj"
59STATISTIC(NumNVJGenerated,
"Number of New Value Jump Instructions created");
62 cl::desc(
"Maximum number of predicated jumps to be converted to "
66 cl::desc(
"Disable New Value Jumps"));
77 void getAnalysisUsage(AnalysisUsage &AU)
const override {
78 AU.
addRequired<MachineBranchProbabilityInfoWrapperPass>();
82 StringRef getPassName()
const override {
return "Hexagon NewValueJump"; }
86 MachineFunctionProperties getRequiredProperties()
const override {
87 return MachineFunctionProperties().setNoVRegs();
91 const HexagonInstrInfo *QII;
92 const HexagonRegisterInfo *QRI;
95 const MachineBranchProbabilityInfo *MBPI;
97 bool isNewValueJumpCandidate(
const MachineInstr &
MI)
const;
102char HexagonNewValueJump::ID = 0;
105 "Hexagon NewValueJump",
false,
false)
119 if (QII->isPredicated(*
II))
132 if (
II->getOpcode() == TargetOpcode::KILL)
135 if (
II->isImplicitDef())
138 if (QII->isSolo(*
II))
141 if (QII->isFloat(*
II))
147 if (!Op.isReg() || !Op.isDef())
152 if (!Hexagon::IntRegsRegClass.contains(Op.getReg()))
171 for (
unsigned i = 0; i <
II->getNumOperands(); ++i) {
172 if (
II->getOperand(i).isReg() &&
173 (
II->getOperand(i).isUse() ||
II->getOperand(i).isDef())) {
179 if (localBegin ==
skip)
181 if (localBegin->isDebugInstr())
184 if (localBegin->modifiesRegister(
Reg,
TRI) ||
185 localBegin->readsRegister(
Reg,
TRI))
218 if (MII->getOpcode() == TargetOpcode::KILL ||
219 MII->getOpcode() == TargetOpcode::PHI ||
220 MII->getOpcode() == TargetOpcode::COPY)
227 if (MII->getOpcode() == Hexagon::LDriw_pred ||
228 MII->getOpcode() == Hexagon::STriw_pred)
255 switch (
MI.getOpcode()) {
256 case Hexagon::C2_cmpeqi:
257 case Hexagon::C4_cmpneqi:
258 case Hexagon::C2_cmpgti:
259 case Hexagon::C4_cmpltei:
262 case Hexagon::C2_cmpgtui:
263 case Hexagon::C4_cmplteui:
266 case Hexagon::S2_tstbit_i:
267 case Hexagon::S4_ntstbit_i:
276 unsigned cmpReg1, cmpOp2 = 0;
277 cmpReg1 =
MI.getOperand(1).getReg();
280 cmpOp2 =
MI.getOperand(2).getReg();
284 if (cmpReg1 == cmpOp2)
293 if (def->
getOpcode() == TargetOpcode::COPY)
302 if (localII->isDebugInstr())
312 if (localII->modifiesRegister(pReg,
TRI) ||
313 localII->readsRegister(pReg,
TRI))
323 if (localII->modifiesRegister(cmpReg1,
TRI) ||
324 (secondReg && localII->modifiesRegister(cmpOp2,
TRI)))
333 bool secondRegNewified,
345 switch (
MI->getOpcode()) {
346 case Hexagon::C2_cmpeq:
347 return taken ? Hexagon::J4_cmpeq_t_jumpnv_t
348 : Hexagon::J4_cmpeq_t_jumpnv_nt;
350 case Hexagon::C2_cmpeqi:
352 return taken ? Hexagon::J4_cmpeqi_t_jumpnv_t
353 : Hexagon::J4_cmpeqi_t_jumpnv_nt;
354 return taken ? Hexagon::J4_cmpeqn1_t_jumpnv_t
355 : Hexagon::J4_cmpeqn1_t_jumpnv_nt;
357 case Hexagon::C4_cmpneqi:
359 return taken ? Hexagon::J4_cmpeqi_f_jumpnv_t
360 : Hexagon::J4_cmpeqi_f_jumpnv_nt;
361 return taken ? Hexagon::J4_cmpeqn1_f_jumpnv_t :
362 Hexagon::J4_cmpeqn1_f_jumpnv_nt;
364 case Hexagon::C2_cmpgt:
365 if (secondRegNewified)
366 return taken ? Hexagon::J4_cmplt_t_jumpnv_t
367 : Hexagon::J4_cmplt_t_jumpnv_nt;
368 return taken ? Hexagon::J4_cmpgt_t_jumpnv_t
369 : Hexagon::J4_cmpgt_t_jumpnv_nt;
371 case Hexagon::C2_cmpgti:
373 return taken ? Hexagon::J4_cmpgti_t_jumpnv_t
374 : Hexagon::J4_cmpgti_t_jumpnv_nt;
375 return taken ? Hexagon::J4_cmpgtn1_t_jumpnv_t
376 : Hexagon::J4_cmpgtn1_t_jumpnv_nt;
378 case Hexagon::C2_cmpgtu:
379 if (secondRegNewified)
380 return taken ? Hexagon::J4_cmpltu_t_jumpnv_t
381 : Hexagon::J4_cmpltu_t_jumpnv_nt;
382 return taken ? Hexagon::J4_cmpgtu_t_jumpnv_t
383 : Hexagon::J4_cmpgtu_t_jumpnv_nt;
385 case Hexagon::C2_cmpgtui:
386 return taken ? Hexagon::J4_cmpgtui_t_jumpnv_t
387 : Hexagon::J4_cmpgtui_t_jumpnv_nt;
389 case Hexagon::C4_cmpneq:
390 return taken ? Hexagon::J4_cmpeq_f_jumpnv_t
391 : Hexagon::J4_cmpeq_f_jumpnv_nt;
393 case Hexagon::C4_cmplte:
394 if (secondRegNewified)
395 return taken ? Hexagon::J4_cmplt_f_jumpnv_t
396 : Hexagon::J4_cmplt_f_jumpnv_nt;
397 return taken ? Hexagon::J4_cmpgt_f_jumpnv_t
398 : Hexagon::J4_cmpgt_f_jumpnv_nt;
400 case Hexagon::C4_cmplteu:
401 if (secondRegNewified)
402 return taken ? Hexagon::J4_cmpltu_f_jumpnv_t
403 : Hexagon::J4_cmpltu_f_jumpnv_nt;
404 return taken ? Hexagon::J4_cmpgtu_f_jumpnv_t
405 : Hexagon::J4_cmpgtu_f_jumpnv_nt;
407 case Hexagon::C4_cmpltei:
409 return taken ? Hexagon::J4_cmpgti_f_jumpnv_t
410 : Hexagon::J4_cmpgti_f_jumpnv_nt;
411 return taken ? Hexagon::J4_cmpgtn1_f_jumpnv_t
412 : Hexagon::J4_cmpgtn1_f_jumpnv_nt;
414 case Hexagon::C4_cmplteui:
415 return taken ? Hexagon::J4_cmpgtui_f_jumpnv_t
416 : Hexagon::J4_cmpgtui_f_jumpnv_nt;
425bool HexagonNewValueJump::isNewValueJumpCandidate(
427 switch (
MI.getOpcode()) {
428 case Hexagon::C2_cmpeq:
429 case Hexagon::C2_cmpeqi:
430 case Hexagon::C2_cmpgt:
431 case Hexagon::C2_cmpgti:
432 case Hexagon::C2_cmpgtu:
433 case Hexagon::C2_cmpgtui:
434 case Hexagon::C4_cmpneq:
435 case Hexagon::C4_cmpneqi:
436 case Hexagon::C4_cmplte:
437 case Hexagon::C4_cmplteu:
438 case Hexagon::C4_cmpltei:
439 case Hexagon::C4_cmplteui:
448 LLVM_DEBUG(
dbgs() <<
"********** Hexagon New Value Jump **********\n"
449 <<
"********** Function: " << MF.
getName() <<
"\n");
457 QII =
static_cast<const HexagonInstrInfo *
>(MF.
getSubtarget().getInstrInfo());
458 QRI =
static_cast<const HexagonRegisterInfo *
>(
460 MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
465 !MF.
getSubtarget<HexagonSubtarget>().useNewValueJumps())
469 int nvjGenerated = 0;
473 MBBb != MBBe; ++MBBb) {
474 MachineBasicBlock *
MBB = &*MBBb;
479 <<
"********** dumping instr bottom up **********\n");
480 bool foundJump =
false;
481 bool foundCompare =
false;
482 bool invertPredicate =
false;
483 unsigned predReg = 0;
484 unsigned cmpReg1 = 0;
488 MachineInstr *cmpInstr =
nullptr, *jmpInstr =
nullptr;
489 MachineBasicBlock *jmpTarget =
nullptr;
490 bool afterRA =
false;
491 bool isSecondOpReg =
false;
492 bool isSecondOpNewified =
false;
496 MachineInstr &
MI = *--MII;
497 if (
MI.isDebugInstr()) {
501 if ((nvjCount == 0) || (nvjCount > -1 && nvjCount <= nvjGenerated))
506 if (!foundJump && (
MI.getOpcode() == Hexagon::J2_jumpt ||
507 MI.getOpcode() == Hexagon::J2_jumptpt ||
508 MI.getOpcode() == Hexagon::J2_jumpf ||
509 MI.getOpcode() == Hexagon::J2_jumpfpt ||
510 MI.getOpcode() == Hexagon::J2_jumptnewpt ||
511 MI.getOpcode() == Hexagon::J2_jumptnew ||
512 MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
513 MI.getOpcode() == Hexagon::J2_jumpfnew)) {
518 predReg =
MI.getOperand(0).getReg();
519 afterRA = Register::isPhysicalRegister(predReg);
536 bool predLive =
false;
538 if (SuccMBB->isLiveIn(predReg))
543 if (!
MI.getOperand(1).isMBB())
545 jmpTarget =
MI.getOperand(1).getMBB();
547 if (
MI.getOpcode() == Hexagon::J2_jumpf ||
548 MI.getOpcode() == Hexagon::J2_jumpfnewpt ||
549 MI.getOpcode() == Hexagon::J2_jumpfnew) {
550 invertPredicate =
true;
558 if (foundJump &&
MI.getNumOperands() == 0)
561 if (foundJump && !foundCompare &&
MI.getOperand(0).isReg() &&
562 MI.getOperand(0).getReg() == predReg) {
564 if (isNewValueJumpCandidate(
MI)) {
566 (
MI.getDesc().isCompare()) &&
567 "Only compare instruction can be collapsed into New Value Jump");
568 isSecondOpReg =
MI.getOperand(2).isReg();
571 afterRA, jmpPos, MF))
580 cmpReg1 =
MI.getOperand(1).getReg();
583 cmpOp2 =
MI.getOperand(2).getReg();
585 cmpOp2 =
MI.getOperand(2).getImm();
590 if (foundCompare && foundJump) {
595 bool foundFeeder =
false;
597 if (
MI.getOperand(0).isReg() &&
MI.getOperand(0).isDef() &&
598 (
MI.getOperand(0).getReg() == cmpReg1 ||
600 MI.getOperand(0).getReg() == (
unsigned)cmpOp2))) {
602 Register feederReg =
MI.getOperand(0).getReg();
610 if (feederReg == cmpReg1) {
620 if (!foundFeeder && isSecondOpReg && feederReg == (
unsigned)cmpOp2)
628 if ((COp == Hexagon::C2_cmpeq || COp == Hexagon::C4_cmpneq) &&
629 (feederReg == (
unsigned)cmpOp2)) {
630 unsigned tmp = cmpReg1;
638 if (feederReg == (
unsigned)cmpOp2)
639 isSecondOpNewified =
true;
646 auto TransferKills = [jmpPos,cmpPos] (MachineInstr &
MI) {
647 for (MachineOperand &MO :
MI.operands()) {
648 if (!MO.isReg() || !MO.isUse())
651 for (
auto I = std::next(
MI.getIterator());
I != jmpPos; ++
I) {
654 for (MachineOperand &
Op :
I->operands()) {
655 if (!
Op.isReg() || !
Op.isUse() || !
Op.isKill())
657 if (
Op.getReg() != UseR)
669 TransferKills(*feederPos);
670 TransferKills(*cmpPos);
671 bool MO1IsKill = cmpPos->killsRegister(cmpReg1, QRI);
672 bool MO2IsKill = isSecondOpReg && cmpPos->killsRegister(cmpOp2, QRI);
678 for (
auto I = std::next(
MI.getIterator());
679 I != jmpInstr->getIterator(); ++
I)
680 if (
I->isDebugValue() && !
I->isDebugEntryValue() &&
681 I->readsRegister(feederReg, QRI))
682 I->setDebugValueUndef();
689 MachineBasicBlock &Blk) {
690 for (
auto I = Begin;
I != Blk.end(); ++
I) {
691 if (
I->isDebugValue() && !
I->isDebugEntryValue() &&
692 I->readsRegister(predReg, QRI))
693 I->setDebugValueUndef();
694 else if (!
I->isDebugInstr() &&
695 (
I->isCall() || (
I->modifiesRegister(predReg, QRI) &&
701 SmallVector<MachineBasicBlock *, 4> Todo;
702 SmallPtrSet<MachineBasicBlock *, 8> Visited;
703 if (!UndefPredicateReaders(std::next(cmpInstr->
getIterator()), *
MBB))
705 while (!Todo.
empty()) {
708 !UndefPredicateReaders(
Next->begin(), *
Next))
717 assert((isNewValueJumpCandidate(*cmpInstr)) &&
718 "This compare is not a New Value Jump candidate.");
726 NewMI =
BuildMI(*
MBB, jmpPos, dl, QII->get(opc))
732 NewMI =
BuildMI(*
MBB, jmpPos, dl, QII->get(opc))
737 assert(NewMI &&
"New Value Jump Instruction Not created!");
746 jmpInstr->eraseFromParent();
759 return new HexagonNewValueJump();
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static cl::opt< bool > DisableNewValueJumps("disable-nvjump", cl::Hidden, cl::desc("Disable New Value Jumps"))
static bool canCompareBeNewValueJump(const HexagonInstrInfo *QII, const TargetRegisterInfo *TRI, MachineBasicBlock::iterator II, unsigned pReg, bool secondReg, bool optLocation, MachineBasicBlock::iterator end, MachineFunction &MF)
static bool commonChecksToProhibitNewValueJump(bool afterRA, MachineBasicBlock::iterator MII)
cl::opt< bool > DisablePacketizer
hexagon Hexagon static false bool canBeFeederToNewValueJump(const HexagonInstrInfo *QII, const TargetRegisterInfo *TRI, MachineBasicBlock::iterator II, MachineBasicBlock::iterator end, MachineBasicBlock::iterator skip, MachineFunction &MF)
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 unsigned getNewValueJumpOpcode(MachineInstr *MI, int reg, bool secondRegNewified, MachineBasicBlock *jmpTarget, const MachineBranchProbabilityInfo *MBPI)
static bool skip(GsymDataExtractor &Data, uint64_t &Offset, bool SkippedRanges)
Skip an InlineInfo object in the specified data at the specified offset.
Register const TargetRegisterInfo * TRI
Promote Memory to Register
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
MachineRegisterInfo & MRI
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
AnalysisUsage & addRequired()
FunctionPass class - This class is used to implement most global optimizations.
bool isPredicated(const MachineInstr &MI) const override
Returns true if the instruction is already predicated.
unsigned getInvertedPredicatedOpcode(const int Opc) const
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
succ_iterator succ_begin()
LLVM_ABI void dump() const
iterator_range< succ_iterator > successors()
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI BranchProbability getEdgeProbability(const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
void setIsKill(bool Val=true)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Wrapper class representing virtual and physical registers.
static constexpr bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
self_iterator getIterator()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr RegState getKillRegState(bool B)
FunctionPass * createHexagonNewValueJump()
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
DWARFExpression::Operation Op
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next