1 //===-- HexagonInstrInfo.cpp - Hexagon Instruction Information ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the Hexagon implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "HexagonInstrInfo.h"
16 #include "HexagonRegisterInfo.h"
17 #include "HexagonSubtarget.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/CodeGen/DFAPacketizer.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineMemOperand.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/PseudoSourceValue.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/raw_ostream.h"
32 #define DEBUG_TYPE "hexagon-instrinfo"
34 #define GET_INSTRINFO_CTOR_DTOR
35 #define GET_INSTRMAP_INFO
36 #include "HexagonGenInstrInfo.inc"
37 #include "HexagonGenDFAPacketizer.inc"
40 /// Constants for Hexagon instructions.
42 const int Hexagon_MEMW_OFFSET_MAX = 4095;
43 const int Hexagon_MEMW_OFFSET_MIN = -4096;
44 const int Hexagon_MEMD_OFFSET_MAX = 8191;
45 const int Hexagon_MEMD_OFFSET_MIN = -8192;
46 const int Hexagon_MEMH_OFFSET_MAX = 2047;
47 const int Hexagon_MEMH_OFFSET_MIN = -2048;
48 const int Hexagon_MEMB_OFFSET_MAX = 1023;
49 const int Hexagon_MEMB_OFFSET_MIN = -1024;
50 const int Hexagon_ADDI_OFFSET_MAX = 32767;
51 const int Hexagon_ADDI_OFFSET_MIN = -32768;
52 const int Hexagon_MEMD_AUTOINC_MAX = 56;
53 const int Hexagon_MEMD_AUTOINC_MIN = -64;
54 const int Hexagon_MEMW_AUTOINC_MAX = 28;
55 const int Hexagon_MEMW_AUTOINC_MIN = -32;
56 const int Hexagon_MEMH_AUTOINC_MAX = 14;
57 const int Hexagon_MEMH_AUTOINC_MIN = -16;
58 const int Hexagon_MEMB_AUTOINC_MAX = 7;
59 const int Hexagon_MEMB_AUTOINC_MIN = -8;
61 // Pin the vtable to this file.
62 void HexagonInstrInfo::anchor() {}
64 HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
65 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
66 RI(), Subtarget(ST) {}
68 /// isLoadFromStackSlot - If the specified machine instruction is a direct
69 /// load from a stack slot, return the virtual or physical register number of
70 /// the destination along with the FrameIndex of the loaded stack slot. If
71 /// not, return 0. This predicate must return 0 if the instruction has
72 /// any side effects other than loading from the stack slot.
73 unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
74 int &FrameIndex) const {
77 switch (MI->getOpcode()) {
79 case Hexagon::L2_loadri_io:
80 case Hexagon::L2_loadrd_io:
81 case Hexagon::L2_loadrh_io:
82 case Hexagon::L2_loadrb_io:
83 case Hexagon::L2_loadrub_io:
84 if (MI->getOperand(2).isFI() &&
85 MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
86 FrameIndex = MI->getOperand(2).getIndex();
87 return MI->getOperand(0).getReg();
95 /// isStoreToStackSlot - If the specified machine instruction is a direct
96 /// store to a stack slot, return the virtual or physical register number of
97 /// the source reg along with the FrameIndex of the loaded stack slot. If
98 /// not, return 0. This predicate must return 0 if the instruction has
99 /// any side effects other than storing to the stack slot.
100 unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
101 int &FrameIndex) const {
102 switch (MI->getOpcode()) {
104 case Hexagon::S2_storeri_io:
105 case Hexagon::S2_storerd_io:
106 case Hexagon::S2_storerh_io:
107 case Hexagon::S2_storerb_io:
108 if (MI->getOperand(2).isFI() &&
109 MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
110 FrameIndex = MI->getOperand(0).getIndex();
111 return MI->getOperand(2).getReg();
120 HexagonInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
121 MachineBasicBlock *FBB,
122 const SmallVectorImpl<MachineOperand> &Cond,
125 int BOpc = Hexagon::J2_jump;
126 int BccOpc = Hexagon::J2_jumpt;
128 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
131 // Check if ReverseBranchCondition has asked to reverse this branch
132 // If we want to reverse the branch an odd number of times, we want
134 if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
135 BccOpc = Hexagon::J2_jumpf;
141 // Due to a bug in TailMerging/CFG Optimization, we need to add a
142 // special case handling of a predicated jump followed by an
143 // unconditional jump. If not, Tail Merging and CFG Optimization go
144 // into an infinite loop.
145 MachineBasicBlock *NewTBB, *NewFBB;
146 SmallVector<MachineOperand, 4> Cond;
147 MachineInstr *Term = MBB.getFirstTerminator();
148 if (isPredicated(Term) && !AnalyzeBranch(MBB, NewTBB, NewFBB, Cond,
150 MachineBasicBlock *NextBB =
151 std::next(MachineFunction::iterator(&MBB));
152 if (NewTBB == NextBB) {
153 ReverseBranchCondition(Cond);
155 return InsertBranch(MBB, TBB, nullptr, Cond, DL);
158 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
160 // If Cond[0] is a basic block, insert ENDLOOP0.
162 BuildMI(&MBB, DL, get(Hexagon::ENDLOOP0)).addMBB(Cond[0].getMBB());
165 get(BccOpc)).addReg(Cond[regPos].getReg()).addMBB(TBB);
170 // We don't handle ENDLOOP0 with a conditional branch in AnalyzeBranch.
171 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[regPos].getReg()).addMBB(TBB);
172 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
177 bool HexagonInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
178 MachineBasicBlock *&TBB,
179 MachineBasicBlock *&FBB,
180 SmallVectorImpl<MachineOperand> &Cond,
181 bool AllowModify) const {
185 // If the block has no terminators, it just falls into the block after it.
186 MachineBasicBlock::instr_iterator I = MBB.instr_end();
187 if (I == MBB.instr_begin())
190 // A basic block may looks like this:
200 // It has two succs but does not have a terminator
201 // Don't know how to handle it.
206 } while (I != MBB.instr_begin());
211 while (I->isDebugValue()) {
212 if (I == MBB.instr_begin())
217 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
218 I->getOperand(0).isMBB();
219 // Delete the JMP if it's equivalent to a fall-through.
220 if (AllowModify && JumpToBlock &&
221 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
222 DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
223 I->eraseFromParent();
225 if (I == MBB.instr_begin())
229 if (!isUnpredicatedTerminator(I))
232 // Get the last instruction in the block.
233 MachineInstr *LastInst = I;
234 MachineInstr *SecondLastInst = nullptr;
235 // Find one more terminator if present.
237 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(I)) {
241 // This is a third branch.
244 if (I == MBB.instr_begin())
249 int LastOpcode = LastInst->getOpcode();
250 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
251 // If the branch target is not a basic block, it could be a tail call.
252 // (It is, if the target is a function.)
253 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
255 if (SecLastOpcode == Hexagon::J2_jump &&
256 !SecondLastInst->getOperand(0).isMBB())
259 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
260 bool LastOpcodeHasNot = PredOpcodeHasNot(LastOpcode);
262 // If there is only one terminator instruction, process it.
263 if (LastInst && !SecondLastInst) {
264 if (LastOpcode == Hexagon::J2_jump) {
265 TBB = LastInst->getOperand(0).getMBB();
268 if (LastOpcode == Hexagon::ENDLOOP0) {
269 TBB = LastInst->getOperand(0).getMBB();
270 Cond.push_back(LastInst->getOperand(0));
273 if (LastOpcodeHasJMP_c) {
274 TBB = LastInst->getOperand(1).getMBB();
275 if (LastOpcodeHasNot) {
276 Cond.push_back(MachineOperand::CreateImm(0));
278 Cond.push_back(LastInst->getOperand(0));
281 // Otherwise, don't know what this is.
285 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
286 bool SecLastOpcodeHasNot = PredOpcodeHasNot(SecLastOpcode);
287 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
288 TBB = SecondLastInst->getOperand(1).getMBB();
289 if (SecLastOpcodeHasNot)
290 Cond.push_back(MachineOperand::CreateImm(0));
291 Cond.push_back(SecondLastInst->getOperand(0));
292 FBB = LastInst->getOperand(0).getMBB();
296 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
297 // executed, so remove it.
298 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
299 TBB = SecondLastInst->getOperand(0).getMBB();
302 I->eraseFromParent();
306 // If the block ends with an ENDLOOP, and JMP, handle it.
307 if (SecLastOpcode == Hexagon::ENDLOOP0 &&
308 LastOpcode == Hexagon::J2_jump) {
309 TBB = SecondLastInst->getOperand(0).getMBB();
310 Cond.push_back(SecondLastInst->getOperand(0));
311 FBB = LastInst->getOperand(0).getMBB();
315 // Otherwise, can't handle this.
320 unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
321 MachineBasicBlock::iterator I = MBB.end();
322 if (I == MBB.begin()) return 0;
324 unsigned Opc1 = I->getOpcode();
326 case Hexagon::J2_jump:
327 case Hexagon::J2_jumpt:
328 case Hexagon::J2_jumpf:
329 case Hexagon::ENDLOOP0:
330 I->eraseFromParent();
338 if (I == MBB.begin()) return 1;
340 unsigned Opc2 = I->getOpcode();
342 case Hexagon::J2_jumpt:
343 case Hexagon::J2_jumpf:
344 case Hexagon::ENDLOOP0:
345 I->eraseFromParent();
353 /// \brief For a comparison instruction, return the source registers in
354 /// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
355 /// compares against in CmpValue. Return true if the comparison instruction
357 bool HexagonInstrInfo::analyzeCompare(const MachineInstr *MI,
358 unsigned &SrcReg, unsigned &SrcReg2,
359 int &Mask, int &Value) const {
360 unsigned Opc = MI->getOpcode();
362 // Set mask and the first source register.
364 case Hexagon::C2_cmpeqp:
365 case Hexagon::C2_cmpeqi:
366 case Hexagon::C2_cmpeq:
367 case Hexagon::C2_cmpgtp:
368 case Hexagon::C2_cmpgtup:
369 case Hexagon::C2_cmpgtui:
370 case Hexagon::C2_cmpgtu:
371 case Hexagon::C2_cmpgti:
372 case Hexagon::C2_cmpgt:
373 SrcReg = MI->getOperand(1).getReg();
376 case Hexagon::A4_cmpbeqi:
377 case Hexagon::A4_cmpbeq:
378 case Hexagon::A4_cmpbgtui:
379 case Hexagon::A4_cmpbgtu:
380 case Hexagon::A4_cmpbgt:
381 SrcReg = MI->getOperand(1).getReg();
384 case Hexagon::A4_cmpheqi:
385 case Hexagon::A4_cmpheq:
386 case Hexagon::A4_cmphgtui:
387 case Hexagon::A4_cmphgtu:
388 case Hexagon::A4_cmphgt:
389 SrcReg = MI->getOperand(1).getReg();
394 // Set the value/second source register.
396 case Hexagon::C2_cmpeqp:
397 case Hexagon::C2_cmpeq:
398 case Hexagon::C2_cmpgtp:
399 case Hexagon::C2_cmpgtup:
400 case Hexagon::C2_cmpgtu:
401 case Hexagon::C2_cmpgt:
402 case Hexagon::A4_cmpbeq:
403 case Hexagon::A4_cmpbgtu:
404 case Hexagon::A4_cmpbgt:
405 case Hexagon::A4_cmpheq:
406 case Hexagon::A4_cmphgtu:
407 case Hexagon::A4_cmphgt:
408 SrcReg2 = MI->getOperand(2).getReg();
411 case Hexagon::C2_cmpeqi:
412 case Hexagon::C2_cmpgtui:
413 case Hexagon::C2_cmpgti:
414 case Hexagon::A4_cmpbeqi:
415 case Hexagon::A4_cmpbgtui:
416 case Hexagon::A4_cmpheqi:
417 case Hexagon::A4_cmphgtui:
419 Value = MI->getOperand(2).getImm();
427 void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
428 MachineBasicBlock::iterator I, DebugLoc DL,
429 unsigned DestReg, unsigned SrcReg,
430 bool KillSrc) const {
431 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
432 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg).addReg(SrcReg);
435 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
436 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg).addReg(SrcReg);
439 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
440 // Map Pd = Ps to Pd = or(Ps, Ps).
441 BuildMI(MBB, I, DL, get(Hexagon::C2_or),
442 DestReg).addReg(SrcReg).addReg(SrcReg);
445 if (Hexagon::DoubleRegsRegClass.contains(DestReg) &&
446 Hexagon::IntRegsRegClass.contains(SrcReg)) {
447 // We can have an overlap between single and double reg: r1:0 = r0.
448 if(SrcReg == RI.getSubReg(DestReg, Hexagon::subreg_loreg)) {
450 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
451 Hexagon::subreg_hireg))).addImm(0);
453 // r1:0 = r1 or no overlap.
454 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), (RI.getSubReg(DestReg,
455 Hexagon::subreg_loreg))).addReg(SrcReg);
456 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
457 Hexagon::subreg_hireg))).addImm(0);
461 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
462 Hexagon::IntRegsRegClass.contains(SrcReg)) {
463 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg).addReg(SrcReg);
466 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
467 Hexagon::IntRegsRegClass.contains(DestReg)) {
468 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg).
469 addReg(SrcReg, getKillRegState(KillSrc));
472 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
473 Hexagon::PredRegsRegClass.contains(DestReg)) {
474 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg).
475 addReg(SrcReg, getKillRegState(KillSrc));
479 llvm_unreachable("Unimplemented");
483 void HexagonInstrInfo::
484 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
485 unsigned SrcReg, bool isKill, int FI,
486 const TargetRegisterClass *RC,
487 const TargetRegisterInfo *TRI) const {
489 DebugLoc DL = MBB.findDebugLoc(I);
490 MachineFunction &MF = *MBB.getParent();
491 MachineFrameInfo &MFI = *MF.getFrameInfo();
492 unsigned Align = MFI.getObjectAlignment(FI);
494 MachineMemOperand *MMO =
495 MF.getMachineMemOperand(
496 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
497 MachineMemOperand::MOStore,
498 MFI.getObjectSize(FI),
501 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
502 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
503 .addFrameIndex(FI).addImm(0)
504 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
505 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
506 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
507 .addFrameIndex(FI).addImm(0)
508 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
509 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
510 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
511 .addFrameIndex(FI).addImm(0)
512 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
514 llvm_unreachable("Unimplemented");
519 void HexagonInstrInfo::storeRegToAddr(
520 MachineFunction &MF, unsigned SrcReg,
522 SmallVectorImpl<MachineOperand> &Addr,
523 const TargetRegisterClass *RC,
524 SmallVectorImpl<MachineInstr*> &NewMIs) const
526 llvm_unreachable("Unimplemented");
530 void HexagonInstrInfo::
531 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
532 unsigned DestReg, int FI,
533 const TargetRegisterClass *RC,
534 const TargetRegisterInfo *TRI) const {
535 DebugLoc DL = MBB.findDebugLoc(I);
536 MachineFunction &MF = *MBB.getParent();
537 MachineFrameInfo &MFI = *MF.getFrameInfo();
538 unsigned Align = MFI.getObjectAlignment(FI);
540 MachineMemOperand *MMO =
541 MF.getMachineMemOperand(
542 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
543 MachineMemOperand::MOLoad,
544 MFI.getObjectSize(FI),
546 if (RC == &Hexagon::IntRegsRegClass) {
547 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
548 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
549 } else if (RC == &Hexagon::DoubleRegsRegClass) {
550 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
551 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
552 } else if (RC == &Hexagon::PredRegsRegClass) {
553 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
554 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
556 llvm_unreachable("Can't store this register to stack slot");
561 void HexagonInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
562 SmallVectorImpl<MachineOperand> &Addr,
563 const TargetRegisterClass *RC,
564 SmallVectorImpl<MachineInstr*> &NewMIs) const {
565 llvm_unreachable("Unimplemented");
568 HexagonInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
569 MachineBasicBlock &MBB = *MI->getParent();
570 DebugLoc DL = MI->getDebugLoc();
571 unsigned Opc = MI->getOpcode();
574 case Hexagon::TFR_PdTrue: {
575 unsigned Reg = MI->getOperand(0).getReg();
576 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
577 .addReg(Reg, RegState::Undef)
578 .addReg(Reg, RegState::Undef);
582 case Hexagon::TFR_PdFalse: {
583 unsigned Reg = MI->getOperand(0).getReg();
584 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
585 .addReg(Reg, RegState::Undef)
586 .addReg(Reg, RegState::Undef);
590 case Hexagon::TCRETURNi:
591 MI->setDesc(get(Hexagon::J2_jump));
593 case Hexagon::TCRETURNr:
594 MI->setDesc(get(Hexagon::J2_jumpr));
601 MachineInstr *HexagonInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
603 ArrayRef<unsigned> Ops,
605 // Hexagon_TODO: Implement.
609 unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
611 MachineRegisterInfo &RegInfo = MF->getRegInfo();
612 const TargetRegisterClass *TRC;
614 TRC = &Hexagon::PredRegsRegClass;
615 } else if (VT == MVT::i32 || VT == MVT::f32) {
616 TRC = &Hexagon::IntRegsRegClass;
617 } else if (VT == MVT::i64 || VT == MVT::f64) {
618 TRC = &Hexagon::DoubleRegsRegClass;
620 llvm_unreachable("Cannot handle this register class");
623 unsigned NewReg = RegInfo.createVirtualRegister(TRC);
627 bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
628 const MCInstrDesc &MID = MI->getDesc();
629 const uint64_t F = MID.TSFlags;
630 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
633 // TODO: This is largely obsolete now. Will need to be removed
634 // in consecutive patches.
635 switch(MI->getOpcode()) {
636 // TFR_FI Remains a special case.
637 case Hexagon::TFR_FI:
645 // This returns true in two cases:
646 // - The OP code itself indicates that this is an extended instruction.
647 // - One of MOs has been marked with HMOTF_ConstExtended flag.
648 bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
649 // First check if this is permanently extended op code.
650 const uint64_t F = MI->getDesc().TSFlags;
651 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
653 // Use MO operand flags to determine if one of MI's operands
654 // has HMOTF_ConstExtended flag set.
655 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
656 E = MI->operands_end(); I != E; ++I) {
657 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
663 bool HexagonInstrInfo::isBranch (const MachineInstr *MI) const {
664 return MI->getDesc().isBranch();
667 bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
668 if (isNewValueJump(MI))
671 if (isNewValueStore(MI))
677 bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
678 return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4;
681 bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
682 bool isPred = MI->getDesc().isPredicable();
687 const int Opc = MI->getOpcode();
690 case Hexagon::A2_tfrsi:
691 return (isOperandExtended(MI, 1) && isConstExtended(MI)) || isInt<12>(MI->getOperand(1).getImm());
693 case Hexagon::S2_storerd_io:
694 return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
696 case Hexagon::S2_storeri_io:
697 case Hexagon::S2_storerinew_io:
698 return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
700 case Hexagon::S2_storerh_io:
701 case Hexagon::S2_storerhnew_io:
702 return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
704 case Hexagon::S2_storerb_io:
705 case Hexagon::S2_storerbnew_io:
706 return isUInt<6>(MI->getOperand(1).getImm());
708 case Hexagon::L2_loadrd_io:
709 return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
711 case Hexagon::L2_loadri_io:
712 return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
714 case Hexagon::L2_loadrh_io:
715 case Hexagon::L2_loadruh_io:
716 return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
718 case Hexagon::L2_loadrb_io:
719 case Hexagon::L2_loadrub_io:
720 return isUInt<6>(MI->getOperand(2).getImm());
722 case Hexagon::L2_loadrd_pi:
723 return isShiftedInt<4,3>(MI->getOperand(3).getImm());
725 case Hexagon::L2_loadri_pi:
726 return isShiftedInt<4,2>(MI->getOperand(3).getImm());
728 case Hexagon::L2_loadrh_pi:
729 case Hexagon::L2_loadruh_pi:
730 return isShiftedInt<4,1>(MI->getOperand(3).getImm());
732 case Hexagon::L2_loadrb_pi:
733 case Hexagon::L2_loadrub_pi:
734 return isInt<4>(MI->getOperand(3).getImm());
736 case Hexagon::S4_storeirb_io:
737 case Hexagon::S4_storeirh_io:
738 case Hexagon::S4_storeiri_io:
739 return (isUInt<6>(MI->getOperand(1).getImm()) &&
740 isInt<6>(MI->getOperand(2).getImm()));
742 case Hexagon::A2_addi:
743 return isInt<8>(MI->getOperand(2).getImm());
745 case Hexagon::A2_aslh:
746 case Hexagon::A2_asrh:
747 case Hexagon::A2_sxtb:
748 case Hexagon::A2_sxth:
749 case Hexagon::A2_zxtb:
750 case Hexagon::A2_zxth:
757 // This function performs the following inversiones:
762 unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
764 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
765 : Hexagon::getTruePredOpcode(Opc);
766 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
767 return InvPredOpcode;
770 default: llvm_unreachable("Unexpected predicated instruction");
771 case Hexagon::C2_ccombinewt:
772 return Hexagon::C2_ccombinewf;
773 case Hexagon::C2_ccombinewf:
774 return Hexagon::C2_ccombinewt;
777 case Hexagon::L4_return_t:
778 return Hexagon::L4_return_f;
779 case Hexagon::L4_return_f:
780 return Hexagon::L4_return_t;
784 // New Value Store instructions.
785 bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
786 const uint64_t F = MI->getDesc().TSFlags;
788 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
791 bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
792 const uint64_t F = get(Opcode).TSFlags;
794 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
797 int HexagonInstrInfo::
798 getMatchingCondBranchOpcode(int Opc, bool invertPredicate) const {
799 enum Hexagon::PredSense inPredSense;
800 inPredSense = invertPredicate ? Hexagon::PredSense_false :
801 Hexagon::PredSense_true;
802 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
803 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
806 // This switch case will be removed once all the instructions have been
807 // modified to use relation maps.
809 case Hexagon::TFRI_f:
810 return !invertPredicate ? Hexagon::TFRI_cPt_f :
811 Hexagon::TFRI_cNotPt_f;
812 case Hexagon::A2_combinew:
813 return !invertPredicate ? Hexagon::C2_ccombinewt :
814 Hexagon::C2_ccombinewf;
817 case Hexagon::L4_return:
818 return !invertPredicate ? Hexagon::L4_return_t:
819 Hexagon::L4_return_f;
821 llvm_unreachable("Unexpected predicable instruction");
825 bool HexagonInstrInfo::
826 PredicateInstruction(MachineInstr *MI,
827 const SmallVectorImpl<MachineOperand> &Cond) const {
828 int Opc = MI->getOpcode();
829 assert (isPredicable(MI) && "Expected predicable instruction");
830 bool invertJump = (!Cond.empty() && Cond[0].isImm() &&
831 (Cond[0].getImm() == 0));
833 // This will change MI's opcode to its predicate version.
834 // However, its operand list is still the old one, i.e. the
835 // non-predicate one.
836 MI->setDesc(get(getMatchingCondBranchOpcode(Opc, invertJump)));
839 unsigned int GAIdx = 0;
841 // Indicates whether the current MI has a GlobalAddress operand
842 bool hasGAOpnd = false;
843 std::vector<MachineOperand> tmpOpnds;
845 // Indicates whether we need to shift operands to right.
846 bool needShift = true;
848 // The predicate is ALWAYS the FIRST input operand !!!
849 if (MI->getNumOperands() == 0) {
850 // The non-predicate version of MI does not take any operands,
851 // i.e. no outs and no ins. In this condition, the predicate
852 // operand will be directly placed at Operands[0]. No operand
858 else if ( MI->getOperand(MI->getNumOperands()-1).isReg()
859 && MI->getOperand(MI->getNumOperands()-1).isDef()
860 && !MI->getOperand(MI->getNumOperands()-1).isImplicit()) {
861 // The non-predicate version of MI does not have any input operands.
862 // In this condition, we extend the length of Operands[] by one and
863 // copy the original last operand to the newly allocated slot.
864 // At this moment, it is just a place holder. Later, we will put
865 // predicate operand directly into it. No operand shift is needed.
866 // Example: r0=BARRIER (this is a faked insn used here for illustration)
867 MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
869 oper = MI->getNumOperands() - 2;
872 // We need to right shift all input operands by one. Duplicate the
873 // last operand into the newly allocated slot.
874 MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
879 // Operands[ MI->getNumOperands() - 2 ] has been copied into
880 // Operands[ MI->getNumOperands() - 1 ], so we start from
881 // Operands[ MI->getNumOperands() - 3 ].
882 // oper is a signed int.
883 // It is ok if "MI->getNumOperands()-3" is -3, -2, or -1.
884 for (oper = MI->getNumOperands() - 3; oper >= 0; --oper)
886 MachineOperand &MO = MI->getOperand(oper);
888 // Opnd[0] Opnd[1] Opnd[2] Opnd[3] Opnd[4] Opnd[5] Opnd[6] Opnd[7]
889 // <Def0> <Def1> <Use0> <Use1> <ImpDef0> <ImpDef1> <ImpUse0> <ImpUse1>
893 // Predicate Operand here
894 if (MO.isReg() && !MO.isUse() && !MO.isImplicit()) {
898 MI->getOperand(oper+1).ChangeToRegister(MO.getReg(), MO.isDef(),
899 MO.isImplicit(), MO.isKill(),
900 MO.isDead(), MO.isUndef(),
903 else if (MO.isImm()) {
904 MI->getOperand(oper+1).ChangeToImmediate(MO.getImm());
906 else if (MO.isGlobal()) {
907 // MI can not have more than one GlobalAddress operand.
908 assert(hasGAOpnd == false && "MI can only have one GlobalAddress opnd");
910 // There is no member function called "ChangeToGlobalAddress" in the
911 // MachineOperand class (not like "ChangeToRegister" and
912 // "ChangeToImmediate"). So we have to remove them from Operands[] list
913 // first, and then add them back after we have inserted the predicate
914 // operand. tmpOpnds[] is to remember these operands before we remove
916 tmpOpnds.push_back(MO);
918 // Operands[oper] is a GlobalAddress operand;
919 // Operands[oper+1] has been copied into Operands[oper+2];
925 llvm_unreachable("Unexpected operand type");
930 int regPos = invertJump ? 1 : 0;
931 MachineOperand PredMO = Cond[regPos];
933 // [oper] now points to the last explicit Def. Predicate operand must be
934 // located at [oper+1]. See diagram above.
935 // This assumes that the predicate is always the first operand,
936 // i.e. Operands[0+numResults], in the set of inputs
937 // It is better to have an assert here to check this. But I don't know how
938 // to write this assert because findFirstPredOperandIdx() would return -1
939 if (oper < -1) oper = -1;
941 MI->getOperand(oper+1).ChangeToRegister(PredMO.getReg(), PredMO.isDef(),
942 PredMO.isImplicit(), false,
943 PredMO.isDead(), PredMO.isUndef(),
946 MachineRegisterInfo &RegInfo = MI->getParent()->getParent()->getRegInfo();
947 RegInfo.clearKillFlags(PredMO.getReg());
953 // Operands[GAIdx] is the original GlobalAddress operand, which is
954 // already copied into tmpOpnds[0].
955 // Operands[GAIdx] now stores a copy of Operands[GAIdx-1]
956 // Operands[GAIdx+1] has already been copied into Operands[GAIdx+2],
957 // so we start from [GAIdx+2]
958 for (i = GAIdx + 2; i < MI->getNumOperands(); ++i)
959 tmpOpnds.push_back(MI->getOperand(i));
961 // Remove all operands in range [ (GAIdx+1) ... (MI->getNumOperands()-1) ]
962 // It is very important that we always remove from the end of Operands[]
963 // MI->getNumOperands() is at least 2 if program goes to here.
964 for (i = MI->getNumOperands() - 1; i > GAIdx; --i)
965 MI->RemoveOperand(i);
967 for (i = 0; i < tmpOpnds.size(); ++i)
968 MI->addOperand(tmpOpnds[i]);
977 isProfitableToIfCvt(MachineBasicBlock &MBB,
979 unsigned ExtraPredCycles,
980 const BranchProbability &Probability) const {
987 isProfitableToIfCvt(MachineBasicBlock &TMBB,
989 unsigned ExtraTCycles,
990 MachineBasicBlock &FMBB,
992 unsigned ExtraFCycles,
993 const BranchProbability &Probability) const {
997 // Returns true if an instruction is predicated irrespective of the predicate
998 // sense. For example, all of the following will return true.
999 // if (p0) R1 = add(R2, R3)
1000 // if (!p0) R1 = add(R2, R3)
1001 // if (p0.new) R1 = add(R2, R3)
1002 // if (!p0.new) R1 = add(R2, R3)
1003 bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
1004 const uint64_t F = MI->getDesc().TSFlags;
1006 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
1009 bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
1010 const uint64_t F = get(Opcode).TSFlags;
1012 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
1015 bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr *MI) const {
1016 const uint64_t F = MI->getDesc().TSFlags;
1018 assert(isPredicated(MI));
1019 return (!((F >> HexagonII::PredicatedFalsePos) &
1020 HexagonII::PredicatedFalseMask));
1023 bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
1024 const uint64_t F = get(Opcode).TSFlags;
1026 // Make sure that the instruction is predicated.
1027 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
1028 return (!((F >> HexagonII::PredicatedFalsePos) &
1029 HexagonII::PredicatedFalseMask));
1032 bool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
1033 const uint64_t F = MI->getDesc().TSFlags;
1035 assert(isPredicated(MI));
1036 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1039 bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
1040 const uint64_t F = get(Opcode).TSFlags;
1042 assert(isPredicated(Opcode));
1043 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1046 // Returns true, if a ST insn can be promoted to a new-value store.
1047 bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
1048 const uint64_t F = MI->getDesc().TSFlags;
1050 return ((F >> HexagonII::mayNVStorePos) &
1051 HexagonII::mayNVStoreMask);
1055 HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
1056 std::vector<MachineOperand> &Pred) const {
1057 for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
1058 MachineOperand MO = MI->getOperand(oper);
1059 if (MO.isReg() && MO.isDef()) {
1060 const TargetRegisterClass* RC = RI.getMinimalPhysRegClass(MO.getReg());
1061 if (RC == &Hexagon::PredRegsRegClass) {
1073 SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
1074 const SmallVectorImpl<MachineOperand> &Pred2) const {
1081 // We indicate that we want to reverse the branch by
1082 // inserting a 0 at the beginning of the Cond vector.
1084 bool HexagonInstrInfo::
1085 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
1086 if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
1087 Cond.erase(Cond.begin());
1089 Cond.insert(Cond.begin(), MachineOperand::CreateImm(0));
1095 bool HexagonInstrInfo::
1096 isProfitableToDupForIfCvt(MachineBasicBlock &MBB,unsigned NumInstrs,
1097 const BranchProbability &Probability) const {
1098 return (NumInstrs <= 4);
1101 bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1102 switch (MI->getOpcode()) {
1103 default: return false;
1104 case Hexagon::L4_return:
1105 case Hexagon::L4_return_t:
1106 case Hexagon::L4_return_f:
1107 case Hexagon::L4_return_tnew_pnt:
1108 case Hexagon::L4_return_fnew_pnt:
1109 case Hexagon::L4_return_tnew_pt:
1110 case Hexagon::L4_return_fnew_pt:
1116 bool HexagonInstrInfo::
1117 isValidOffset(const int Opcode, const int Offset) const {
1118 // This function is to check whether the "Offset" is in the correct range of
1119 // the given "Opcode". If "Offset" is not in the correct range, "ADD_ri" is
1120 // inserted to calculate the final address. Due to this reason, the function
1121 // assumes that the "Offset" has correct alignment.
1122 // We used to assert if the offset was not properly aligned, however,
1123 // there are cases where a misaligned pointer recast can cause this
1124 // problem, and we need to allow for it. The front end warns of such
1125 // misaligns with respect to load size.
1129 case Hexagon::L2_loadri_io:
1130 case Hexagon::S2_storeri_io:
1131 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
1132 (Offset <= Hexagon_MEMW_OFFSET_MAX);
1134 case Hexagon::L2_loadrd_io:
1135 case Hexagon::S2_storerd_io:
1136 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
1137 (Offset <= Hexagon_MEMD_OFFSET_MAX);
1139 case Hexagon::L2_loadrh_io:
1140 case Hexagon::L2_loadruh_io:
1141 case Hexagon::S2_storerh_io:
1142 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
1143 (Offset <= Hexagon_MEMH_OFFSET_MAX);
1145 case Hexagon::L2_loadrb_io:
1146 case Hexagon::S2_storerb_io:
1147 case Hexagon::L2_loadrub_io:
1148 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
1149 (Offset <= Hexagon_MEMB_OFFSET_MAX);
1151 case Hexagon::A2_addi:
1152 case Hexagon::TFR_FI:
1153 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
1154 (Offset <= Hexagon_ADDI_OFFSET_MAX);
1156 case Hexagon::L4_iadd_memopw_io:
1157 case Hexagon::L4_isub_memopw_io:
1158 case Hexagon::L4_add_memopw_io:
1159 case Hexagon::L4_sub_memopw_io:
1160 case Hexagon::L4_and_memopw_io:
1161 case Hexagon::L4_or_memopw_io:
1162 return (0 <= Offset && Offset <= 255);
1164 case Hexagon::L4_iadd_memoph_io:
1165 case Hexagon::L4_isub_memoph_io:
1166 case Hexagon::L4_add_memoph_io:
1167 case Hexagon::L4_sub_memoph_io:
1168 case Hexagon::L4_and_memoph_io:
1169 case Hexagon::L4_or_memoph_io:
1170 return (0 <= Offset && Offset <= 127);
1172 case Hexagon::L4_iadd_memopb_io:
1173 case Hexagon::L4_isub_memopb_io:
1174 case Hexagon::L4_add_memopb_io:
1175 case Hexagon::L4_sub_memopb_io:
1176 case Hexagon::L4_and_memopb_io:
1177 case Hexagon::L4_or_memopb_io:
1178 return (0 <= Offset && Offset <= 63);
1180 // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
1181 // any size. Later pass knows how to handle it.
1182 case Hexagon::STriw_pred:
1183 case Hexagon::LDriw_pred:
1186 case Hexagon::J2_loop0i:
1187 return isUInt<10>(Offset);
1189 // INLINEASM is very special.
1190 case Hexagon::INLINEASM:
1194 llvm_unreachable("No offset range is defined for this opcode. "
1195 "Please define it in the above switch statement!");
1200 // Check if the Offset is a valid auto-inc imm by Load/Store Type.
1202 bool HexagonInstrInfo::
1203 isValidAutoIncImm(const EVT VT, const int Offset) const {
1205 if (VT == MVT::i64) {
1206 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
1207 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
1208 (Offset & 0x7) == 0);
1210 if (VT == MVT::i32) {
1211 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
1212 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
1213 (Offset & 0x3) == 0);
1215 if (VT == MVT::i16) {
1216 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
1217 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
1218 (Offset & 0x1) == 0);
1220 if (VT == MVT::i8) {
1221 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
1222 Offset <= Hexagon_MEMB_AUTOINC_MAX);
1224 llvm_unreachable("Not an auto-inc opc!");
1228 bool HexagonInstrInfo::
1229 isMemOp(const MachineInstr *MI) const {
1230 // return MI->getDesc().mayLoad() && MI->getDesc().mayStore();
1232 switch (MI->getOpcode())
1234 default: return false;
1235 case Hexagon::L4_iadd_memopw_io:
1236 case Hexagon::L4_isub_memopw_io:
1237 case Hexagon::L4_add_memopw_io:
1238 case Hexagon::L4_sub_memopw_io:
1239 case Hexagon::L4_and_memopw_io:
1240 case Hexagon::L4_or_memopw_io:
1241 case Hexagon::L4_iadd_memoph_io:
1242 case Hexagon::L4_isub_memoph_io:
1243 case Hexagon::L4_add_memoph_io:
1244 case Hexagon::L4_sub_memoph_io:
1245 case Hexagon::L4_and_memoph_io:
1246 case Hexagon::L4_or_memoph_io:
1247 case Hexagon::L4_iadd_memopb_io:
1248 case Hexagon::L4_isub_memopb_io:
1249 case Hexagon::L4_add_memopb_io:
1250 case Hexagon::L4_sub_memopb_io:
1251 case Hexagon::L4_and_memopb_io:
1252 case Hexagon::L4_or_memopb_io:
1253 case Hexagon::L4_ior_memopb_io:
1254 case Hexagon::L4_ior_memoph_io:
1255 case Hexagon::L4_ior_memopw_io:
1256 case Hexagon::L4_iand_memopb_io:
1257 case Hexagon::L4_iand_memoph_io:
1258 case Hexagon::L4_iand_memopw_io:
1265 bool HexagonInstrInfo::
1266 isSpillPredRegOp(const MachineInstr *MI) const {
1267 switch (MI->getOpcode()) {
1268 default: return false;
1269 case Hexagon::STriw_pred :
1270 case Hexagon::LDriw_pred :
1275 bool HexagonInstrInfo::isNewValueJumpCandidate(const MachineInstr *MI) const {
1276 switch (MI->getOpcode()) {
1277 default: return false;
1278 case Hexagon::C2_cmpeq:
1279 case Hexagon::C2_cmpeqi:
1280 case Hexagon::C2_cmpgt:
1281 case Hexagon::C2_cmpgti:
1282 case Hexagon::C2_cmpgtu:
1283 case Hexagon::C2_cmpgtui:
1288 bool HexagonInstrInfo::
1289 isConditionalTransfer (const MachineInstr *MI) const {
1290 switch (MI->getOpcode()) {
1291 default: return false;
1292 case Hexagon::A2_tfrt:
1293 case Hexagon::A2_tfrf:
1294 case Hexagon::C2_cmoveit:
1295 case Hexagon::C2_cmoveif:
1296 case Hexagon::A2_tfrtnew:
1297 case Hexagon::A2_tfrfnew:
1298 case Hexagon::C2_cmovenewit:
1299 case Hexagon::C2_cmovenewif:
1304 bool HexagonInstrInfo::isConditionalALU32 (const MachineInstr* MI) const {
1305 switch (MI->getOpcode())
1307 default: return false;
1308 case Hexagon::A2_paddf:
1309 case Hexagon::A2_paddfnew:
1310 case Hexagon::A2_paddt:
1311 case Hexagon::A2_paddtnew:
1312 case Hexagon::A2_pandf:
1313 case Hexagon::A2_pandfnew:
1314 case Hexagon::A2_pandt:
1315 case Hexagon::A2_pandtnew:
1316 case Hexagon::A4_paslhf:
1317 case Hexagon::A4_paslhfnew:
1318 case Hexagon::A4_paslht:
1319 case Hexagon::A4_paslhtnew:
1320 case Hexagon::A4_pasrhf:
1321 case Hexagon::A4_pasrhfnew:
1322 case Hexagon::A4_pasrht:
1323 case Hexagon::A4_pasrhtnew:
1324 case Hexagon::A2_porf:
1325 case Hexagon::A2_porfnew:
1326 case Hexagon::A2_port:
1327 case Hexagon::A2_portnew:
1328 case Hexagon::A2_psubf:
1329 case Hexagon::A2_psubfnew:
1330 case Hexagon::A2_psubt:
1331 case Hexagon::A2_psubtnew:
1332 case Hexagon::A2_pxorf:
1333 case Hexagon::A2_pxorfnew:
1334 case Hexagon::A2_pxort:
1335 case Hexagon::A2_pxortnew:
1336 case Hexagon::A4_psxthf:
1337 case Hexagon::A4_psxthfnew:
1338 case Hexagon::A4_psxtht:
1339 case Hexagon::A4_psxthtnew:
1340 case Hexagon::A4_psxtbf:
1341 case Hexagon::A4_psxtbfnew:
1342 case Hexagon::A4_psxtbt:
1343 case Hexagon::A4_psxtbtnew:
1344 case Hexagon::A4_pzxtbf:
1345 case Hexagon::A4_pzxtbfnew:
1346 case Hexagon::A4_pzxtbt:
1347 case Hexagon::A4_pzxtbtnew:
1348 case Hexagon::A4_pzxthf:
1349 case Hexagon::A4_pzxthfnew:
1350 case Hexagon::A4_pzxtht:
1351 case Hexagon::A4_pzxthtnew:
1352 case Hexagon::A2_paddit:
1353 case Hexagon::A2_paddif:
1354 case Hexagon::C2_ccombinewt:
1355 case Hexagon::C2_ccombinewf:
1360 bool HexagonInstrInfo::
1361 isConditionalLoad (const MachineInstr* MI) const {
1362 switch (MI->getOpcode())
1364 default: return false;
1365 case Hexagon::L2_ploadrdt_io :
1366 case Hexagon::L2_ploadrdf_io:
1367 case Hexagon::L2_ploadrit_io:
1368 case Hexagon::L2_ploadrif_io:
1369 case Hexagon::L2_ploadrht_io:
1370 case Hexagon::L2_ploadrhf_io:
1371 case Hexagon::L2_ploadrbt_io:
1372 case Hexagon::L2_ploadrbf_io:
1373 case Hexagon::L2_ploadruht_io:
1374 case Hexagon::L2_ploadruhf_io:
1375 case Hexagon::L2_ploadrubt_io:
1376 case Hexagon::L2_ploadrubf_io:
1377 case Hexagon::L2_ploadrdt_pi:
1378 case Hexagon::L2_ploadrdf_pi:
1379 case Hexagon::L2_ploadrit_pi:
1380 case Hexagon::L2_ploadrif_pi:
1381 case Hexagon::L2_ploadrht_pi:
1382 case Hexagon::L2_ploadrhf_pi:
1383 case Hexagon::L2_ploadrbt_pi:
1384 case Hexagon::L2_ploadrbf_pi:
1385 case Hexagon::L2_ploadruht_pi:
1386 case Hexagon::L2_ploadruhf_pi:
1387 case Hexagon::L2_ploadrubt_pi:
1388 case Hexagon::L2_ploadrubf_pi:
1389 case Hexagon::L4_ploadrdt_rr:
1390 case Hexagon::L4_ploadrdf_rr:
1391 case Hexagon::L4_ploadrbt_rr:
1392 case Hexagon::L4_ploadrbf_rr:
1393 case Hexagon::L4_ploadrubt_rr:
1394 case Hexagon::L4_ploadrubf_rr:
1395 case Hexagon::L4_ploadrht_rr:
1396 case Hexagon::L4_ploadrhf_rr:
1397 case Hexagon::L4_ploadruht_rr:
1398 case Hexagon::L4_ploadruhf_rr:
1399 case Hexagon::L4_ploadrit_rr:
1400 case Hexagon::L4_ploadrif_rr:
1405 // Returns true if an instruction is a conditional store.
1407 // Note: It doesn't include conditional new-value stores as they can't be
1408 // converted to .new predicate.
1410 // p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
1412 // / \ (not OK. it will cause new-value store to be
1413 // / X conditional on p0.new while R2 producer is
1416 // p.new store p.old NV store
1417 // [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
1423 // [if (p0)memw(R0+#0)=R2]
1425 // The above diagram shows the steps involoved in the conversion of a predicated
1426 // store instruction to its .new predicated new-value form.
1428 // The following set of instructions further explains the scenario where
1429 // conditional new-value store becomes invalid when promoted to .new predicate
1432 // { 1) if (p0) r0 = add(r1, r2)
1433 // 2) p0 = cmp.eq(r3, #0) }
1435 // 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
1436 // the first two instructions because in instr 1, r0 is conditional on old value
1437 // of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
1438 // is not valid for new-value stores.
1439 bool HexagonInstrInfo::
1440 isConditionalStore (const MachineInstr* MI) const {
1441 switch (MI->getOpcode())
1443 default: return false;
1444 case Hexagon::S4_storeirbt_io:
1445 case Hexagon::S4_storeirbf_io:
1446 case Hexagon::S4_pstorerbt_rr:
1447 case Hexagon::S4_pstorerbf_rr:
1448 case Hexagon::S2_pstorerbt_io:
1449 case Hexagon::S2_pstorerbf_io:
1450 case Hexagon::S2_pstorerbt_pi:
1451 case Hexagon::S2_pstorerbf_pi:
1452 case Hexagon::S2_pstorerdt_io:
1453 case Hexagon::S2_pstorerdf_io:
1454 case Hexagon::S4_pstorerdt_rr:
1455 case Hexagon::S4_pstorerdf_rr:
1456 case Hexagon::S2_pstorerdt_pi:
1457 case Hexagon::S2_pstorerdf_pi:
1458 case Hexagon::S2_pstorerht_io:
1459 case Hexagon::S2_pstorerhf_io:
1460 case Hexagon::S4_storeirht_io:
1461 case Hexagon::S4_storeirhf_io:
1462 case Hexagon::S4_pstorerht_rr:
1463 case Hexagon::S4_pstorerhf_rr:
1464 case Hexagon::S2_pstorerht_pi:
1465 case Hexagon::S2_pstorerhf_pi:
1466 case Hexagon::S2_pstorerit_io:
1467 case Hexagon::S2_pstorerif_io:
1468 case Hexagon::S4_storeirit_io:
1469 case Hexagon::S4_storeirif_io:
1470 case Hexagon::S4_pstorerit_rr:
1471 case Hexagon::S4_pstorerif_rr:
1472 case Hexagon::S2_pstorerit_pi:
1473 case Hexagon::S2_pstorerif_pi:
1475 // V4 global address store before promoting to dot new.
1476 case Hexagon::S4_pstorerdt_abs:
1477 case Hexagon::S4_pstorerdf_abs:
1478 case Hexagon::S4_pstorerbt_abs:
1479 case Hexagon::S4_pstorerbf_abs:
1480 case Hexagon::S4_pstorerht_abs:
1481 case Hexagon::S4_pstorerhf_abs:
1482 case Hexagon::S4_pstorerit_abs:
1483 case Hexagon::S4_pstorerif_abs:
1486 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1487 // from the "Conditional Store" list. Because a predicated new value store
1488 // would NOT be promoted to a double dot new store. See diagram below:
1489 // This function returns yes for those stores that are predicated but not
1490 // yet promoted to predicate dot new instructions.
1492 // +---------------------+
1493 // /-----| if (p0) memw(..)=r0 |---------\~
1494 // || +---------------------+ ||
1495 // promote || /\ /\ || promote
1497 // \||/ demote || \||/
1499 // +-------------------------+ || +-------------------------+
1500 // | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
1501 // +-------------------------+ || +-------------------------+
1504 // promote || \/ NOT possible
1508 // +-----------------------------+
1509 // | if (p0.new) memw(..)=r0.new |
1510 // +-----------------------------+
1511 // Double Dot New Store
1517 bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
1518 if (isNewValue(MI) && isBranch(MI))
1523 bool HexagonInstrInfo::isPostIncrement (const MachineInstr* MI) const {
1524 return (getAddrMode(MI) == HexagonII::PostInc);
1527 bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
1528 const uint64_t F = MI->getDesc().TSFlags;
1529 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
1532 // Returns true, if any one of the operands is a dot new
1533 // insn, whether it is predicated dot new or register dot new.
1534 bool HexagonInstrInfo::isDotNewInst (const MachineInstr* MI) const {
1535 return (isNewValueInst(MI) ||
1536 (isPredicated(MI) && isPredicatedNew(MI)));
1539 // Returns the most basic instruction for the .new predicated instructions and
1540 // new-value stores.
1541 // For example, all of the following instructions will be converted back to the
1542 // same instruction:
1543 // 1) if (p0.new) memw(R0+#0) = R1.new --->
1544 // 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
1545 // 3) if (p0.new) memw(R0+#0) = R1 --->
1548 int HexagonInstrInfo::GetDotOldOp(const int opc) const {
1550 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
1551 NewOp = Hexagon::getPredOldOpcode(NewOp);
1552 assert(NewOp >= 0 &&
1553 "Couldn't change predicate new instruction to its old form.");
1556 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
1557 NewOp = Hexagon::getNonNVStore(NewOp);
1558 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
1563 // Return the new value instruction for a given store.
1564 int HexagonInstrInfo::GetDotNewOp(const MachineInstr* MI) const {
1565 int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
1566 if (NVOpcode >= 0) // Valid new-value store instruction.
1569 switch (MI->getOpcode()) {
1570 default: llvm_unreachable("Unknown .new type");
1571 case Hexagon::S4_storerb_ur:
1572 return Hexagon::S4_storerbnew_ur;
1574 case Hexagon::S4_storerh_ur:
1575 return Hexagon::S4_storerhnew_ur;
1577 case Hexagon::S4_storeri_ur:
1578 return Hexagon::S4_storerinew_ur;
1580 case Hexagon::S2_storerb_pci:
1581 return Hexagon::S2_storerb_pci;
1583 case Hexagon::S2_storeri_pci:
1584 return Hexagon::S2_storeri_pci;
1586 case Hexagon::S2_storerh_pci:
1587 return Hexagon::S2_storerh_pci;
1589 case Hexagon::S2_storerd_pci:
1590 return Hexagon::S2_storerd_pci;
1592 case Hexagon::S2_storerf_pci:
1593 return Hexagon::S2_storerf_pci;
1598 // Return .new predicate version for an instruction.
1599 int HexagonInstrInfo::GetDotNewPredOp(MachineInstr *MI,
1600 const MachineBranchProbabilityInfo
1603 int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1604 if (NewOpcode >= 0) // Valid predicate new instruction
1607 switch (MI->getOpcode()) {
1608 default: llvm_unreachable("Unknown .new type");
1610 case Hexagon::J2_jumpt:
1611 case Hexagon::J2_jumpf:
1612 return getDotNewPredJumpOp(MI, MBPI);
1614 case Hexagon::J2_jumprt:
1615 return Hexagon::J2_jumptnewpt;
1617 case Hexagon::J2_jumprf:
1618 return Hexagon::J2_jumprfnewpt;
1620 case Hexagon::JMPrett:
1621 return Hexagon::J2_jumprtnewpt;
1623 case Hexagon::JMPretf:
1624 return Hexagon::J2_jumprfnewpt;
1627 // Conditional combine
1628 case Hexagon::C2_ccombinewt:
1629 return Hexagon::C2_ccombinewnewt;
1630 case Hexagon::C2_ccombinewf:
1631 return Hexagon::C2_ccombinewnewf;
1636 unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
1637 const uint64_t F = MI->getDesc().TSFlags;
1639 return((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
1642 /// immediateExtend - Changes the instruction in place to one using an immediate
1644 void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
1645 assert((isExtendable(MI)||isConstExtended(MI)) &&
1646 "Instruction must be extendable");
1647 // Find which operand is extendable.
1648 short ExtOpNum = getCExtOpNum(MI);
1649 MachineOperand &MO = MI->getOperand(ExtOpNum);
1650 // This needs to be something we understand.
1651 assert((MO.isMBB() || MO.isImm()) &&
1652 "Branch with unknown extendable field type");
1653 // Mark given operand as extended.
1654 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
1657 DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1658 const TargetSubtargetInfo &STI) const {
1659 const InstrItineraryData *II = STI.getInstrItineraryData();
1660 return static_cast<const HexagonSubtarget &>(STI).createDFAPacketizer(II);
1663 bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1664 const MachineBasicBlock *MBB,
1665 const MachineFunction &MF) const {
1666 // Debug info is never a scheduling boundary. It's necessary to be explicit
1667 // due to the special treatment of IT instructions below, otherwise a
1668 // dbg_value followed by an IT will result in the IT instruction being
1669 // considered a scheduling hazard, which is wrong. It should be the actual
1670 // instruction preceding the dbg_value instruction(s), just like it is
1671 // when debug info is not present.
1672 if (MI->isDebugValue())
1675 // Terminators and labels can't be scheduled around.
1676 if (MI->getDesc().isTerminator() || MI->isPosition() || MI->isInlineAsm())
1682 bool HexagonInstrInfo::isConstExtended(MachineInstr *MI) const {
1683 const uint64_t F = MI->getDesc().TSFlags;
1684 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1685 if (isExtended) // Instruction must be extended.
1688 unsigned isExtendable = (F >> HexagonII::ExtendablePos)
1689 & HexagonII::ExtendableMask;
1693 short ExtOpNum = getCExtOpNum(MI);
1694 const MachineOperand &MO = MI->getOperand(ExtOpNum);
1695 // Use MO operand flags to determine if MO
1696 // has the HMOTF_ConstExtended flag set.
1697 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1699 // If this is a Machine BB address we are talking about, and it is
1700 // not marked as extended, say so.
1704 // We could be using an instruction with an extendable immediate and shoehorn
1705 // a global address into it. If it is a global address it will be constant
1706 // extended. We do this for COMBINE.
1707 // We currently only handle isGlobal() because it is the only kind of
1708 // object we are going to end up with here for now.
1709 // In the future we probably should add isSymbol(), etc.
1710 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress())
1713 // If the extendable operand is not 'Immediate' type, the instruction should
1714 // have 'isExtended' flag set.
1715 assert(MO.isImm() && "Extendable operand must be Immediate type");
1717 int MinValue = getMinValue(MI);
1718 int MaxValue = getMaxValue(MI);
1719 int ImmValue = MO.getImm();
1721 return (ImmValue < MinValue || ImmValue > MaxValue);
1724 // Returns the opcode to use when converting MI, which is a conditional jump,
1725 // into a conditional instruction which uses the .new value of the predicate.
1726 // We also use branch probabilities to add a hint to the jump.
1728 HexagonInstrInfo::getDotNewPredJumpOp(MachineInstr *MI,
1730 MachineBranchProbabilityInfo *MBPI) const {
1732 // We assume that block can have at most two successors.
1734 MachineBasicBlock *Src = MI->getParent();
1735 MachineOperand *BrTarget = &MI->getOperand(1);
1736 MachineBasicBlock *Dst = BrTarget->getMBB();
1738 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
1739 if (Prediction >= BranchProbability(1,2))
1742 switch (MI->getOpcode()) {
1743 case Hexagon::J2_jumpt:
1744 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
1745 case Hexagon::J2_jumpf:
1746 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
1749 llvm_unreachable("Unexpected jump instruction.");
1752 // Returns true if a particular operand is extendable for an instruction.
1753 bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
1754 unsigned short OperandNum) const {
1755 const uint64_t F = MI->getDesc().TSFlags;
1757 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
1761 // Returns Operand Index for the constant extended instruction.
1762 unsigned short HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
1763 const uint64_t F = MI->getDesc().TSFlags;
1764 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
1767 // Returns the min value that doesn't need to be extended.
1768 int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
1769 const uint64_t F = MI->getDesc().TSFlags;
1770 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1771 & HexagonII::ExtentSignedMask;
1772 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1773 & HexagonII::ExtentBitsMask;
1775 if (isSigned) // if value is signed
1776 return -1U << (bits - 1);
1781 // Returns the max value that doesn't need to be extended.
1782 int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
1783 const uint64_t F = MI->getDesc().TSFlags;
1784 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1785 & HexagonII::ExtentSignedMask;
1786 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1787 & HexagonII::ExtentBitsMask;
1789 if (isSigned) // if value is signed
1790 return ~(-1U << (bits - 1));
1792 return ~(-1U << bits);
1795 // Returns true if an instruction can be converted into a non-extended
1796 // equivalent instruction.
1797 bool HexagonInstrInfo::NonExtEquivalentExists (const MachineInstr *MI) const {
1800 // Check if the instruction has a register form that uses register in place
1801 // of the extended operand, if so return that as the non-extended form.
1802 if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
1805 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
1806 // Check addressing mode and retrieve non-ext equivalent instruction.
1808 switch (getAddrMode(MI)) {
1809 case HexagonII::Absolute :
1810 // Load/store with absolute addressing mode can be converted into
1811 // base+offset mode.
1812 NonExtOpcode = Hexagon::getBasedWithImmOffset(MI->getOpcode());
1814 case HexagonII::BaseImmOffset :
1815 // Load/store with base+offset addressing mode can be converted into
1816 // base+register offset addressing mode. However left shift operand should
1818 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
1823 if (NonExtOpcode < 0)
1830 // Returns opcode of the non-extended equivalent instruction.
1831 short HexagonInstrInfo::getNonExtOpcode (const MachineInstr *MI) const {
1833 // Check if the instruction has a register form that uses register in place
1834 // of the extended operand, if so return that as the non-extended form.
1835 short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
1836 if (NonExtOpcode >= 0)
1837 return NonExtOpcode;
1839 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
1840 // Check addressing mode and retrieve non-ext equivalent instruction.
1841 switch (getAddrMode(MI)) {
1842 case HexagonII::Absolute :
1843 return Hexagon::getBasedWithImmOffset(MI->getOpcode());
1844 case HexagonII::BaseImmOffset :
1845 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
1853 bool HexagonInstrInfo::PredOpcodeHasJMP_c(Opcode_t Opcode) const {
1854 return (Opcode == Hexagon::J2_jumpt) ||
1855 (Opcode == Hexagon::J2_jumpf) ||
1856 (Opcode == Hexagon::J2_jumptnewpt) ||
1857 (Opcode == Hexagon::J2_jumpfnewpt) ||
1858 (Opcode == Hexagon::J2_jumpt) ||
1859 (Opcode == Hexagon::J2_jumpf);
1862 bool HexagonInstrInfo::PredOpcodeHasNot(Opcode_t Opcode) const {
1863 return (Opcode == Hexagon::J2_jumpf) ||
1864 (Opcode == Hexagon::J2_jumpfnewpt) ||
1865 (Opcode == Hexagon::J2_jumpfnew);