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(ST), Subtarget(ST) {
70 /// isLoadFromStackSlot - If the specified machine instruction is a direct
71 /// load from a stack slot, return the virtual or physical register number of
72 /// the destination along with the FrameIndex of the loaded stack slot. If
73 /// not, return 0. This predicate must return 0 if the instruction has
74 /// any side effects other than loading from the stack slot.
75 unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
76 int &FrameIndex) const {
79 switch (MI->getOpcode()) {
81 case Hexagon::L2_loadri_io:
82 case Hexagon::L2_loadrd_io:
83 case Hexagon::L2_loadrh_io:
84 case Hexagon::L2_loadrb_io:
85 case Hexagon::L2_loadrub_io:
86 if (MI->getOperand(2).isFI() &&
87 MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
88 FrameIndex = MI->getOperand(2).getIndex();
89 return MI->getOperand(0).getReg();
97 /// isStoreToStackSlot - If the specified machine instruction is a direct
98 /// store to a stack slot, return the virtual or physical register number of
99 /// the source reg along with the FrameIndex of the loaded stack slot. If
100 /// not, return 0. This predicate must return 0 if the instruction has
101 /// any side effects other than storing to the stack slot.
102 unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
103 int &FrameIndex) const {
104 switch (MI->getOpcode()) {
106 case Hexagon::S2_storeri_io:
107 case Hexagon::S2_storerd_io:
108 case Hexagon::S2_storerh_io:
109 case Hexagon::S2_storerb_io:
110 if (MI->getOperand(2).isFI() &&
111 MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
112 FrameIndex = MI->getOperand(0).getIndex();
113 return MI->getOperand(2).getReg();
122 HexagonInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
123 MachineBasicBlock *FBB,
124 const SmallVectorImpl<MachineOperand> &Cond,
127 int BOpc = Hexagon::J2_jump;
128 int BccOpc = Hexagon::J2_jumpt;
130 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
133 // Check if ReverseBranchCondition has asked to reverse this branch
134 // If we want to reverse the branch an odd number of times, we want
136 if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
137 BccOpc = Hexagon::J2_jumpf;
143 // Due to a bug in TailMerging/CFG Optimization, we need to add a
144 // special case handling of a predicated jump followed by an
145 // unconditional jump. If not, Tail Merging and CFG Optimization go
146 // into an infinite loop.
147 MachineBasicBlock *NewTBB, *NewFBB;
148 SmallVector<MachineOperand, 4> Cond;
149 MachineInstr *Term = MBB.getFirstTerminator();
150 if (isPredicated(Term) && !AnalyzeBranch(MBB, NewTBB, NewFBB, Cond,
152 MachineBasicBlock *NextBB =
153 std::next(MachineFunction::iterator(&MBB));
154 if (NewTBB == NextBB) {
155 ReverseBranchCondition(Cond);
157 return InsertBranch(MBB, TBB, nullptr, Cond, DL);
160 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
163 get(BccOpc)).addReg(Cond[regPos].getReg()).addMBB(TBB);
168 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[regPos].getReg()).addMBB(TBB);
169 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
175 bool HexagonInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
176 MachineBasicBlock *&TBB,
177 MachineBasicBlock *&FBB,
178 SmallVectorImpl<MachineOperand> &Cond,
179 bool AllowModify) const {
183 // If the block has no terminators, it just falls into the block after it.
184 MachineBasicBlock::instr_iterator I = MBB.instr_end();
185 if (I == MBB.instr_begin())
188 // A basic block may looks like this:
198 // It has two succs but does not have a terminator
199 // Don't know how to handle it.
204 } while (I != MBB.instr_begin());
209 while (I->isDebugValue()) {
210 if (I == MBB.instr_begin())
215 // Delete the JMP if it's equivalent to a fall-through.
216 if (AllowModify && I->getOpcode() == Hexagon::J2_jump &&
217 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
218 DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
219 I->eraseFromParent();
221 if (I == MBB.instr_begin())
225 if (!isUnpredicatedTerminator(I))
228 // Get the last instruction in the block.
229 MachineInstr *LastInst = I;
230 MachineInstr *SecondLastInst = nullptr;
231 // Find one more terminator if present.
233 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(I)) {
237 // This is a third branch.
240 if (I == MBB.instr_begin())
245 int LastOpcode = LastInst->getOpcode();
247 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
248 bool LastOpcodeHasNot = PredOpcodeHasNot(LastOpcode);
250 // If there is only one terminator instruction, process it.
251 if (LastInst && !SecondLastInst) {
252 if (LastOpcode == Hexagon::J2_jump) {
253 TBB = LastInst->getOperand(0).getMBB();
256 if (LastOpcode == Hexagon::ENDLOOP0) {
257 TBB = LastInst->getOperand(0).getMBB();
258 Cond.push_back(LastInst->getOperand(0));
261 if (LastOpcodeHasJMP_c) {
262 TBB = LastInst->getOperand(1).getMBB();
263 if (LastOpcodeHasNot) {
264 Cond.push_back(MachineOperand::CreateImm(0));
266 Cond.push_back(LastInst->getOperand(0));
269 // Otherwise, don't know what this is.
273 int SecLastOpcode = SecondLastInst->getOpcode();
275 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
276 bool SecLastOpcodeHasNot = PredOpcodeHasNot(SecLastOpcode);
277 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
278 TBB = SecondLastInst->getOperand(1).getMBB();
279 if (SecLastOpcodeHasNot)
280 Cond.push_back(MachineOperand::CreateImm(0));
281 Cond.push_back(SecondLastInst->getOperand(0));
282 FBB = LastInst->getOperand(0).getMBB();
286 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
287 // executed, so remove it.
288 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
289 TBB = SecondLastInst->getOperand(0).getMBB();
292 I->eraseFromParent();
296 // If the block ends with an ENDLOOP, and JMP, handle it.
297 if (SecLastOpcode == Hexagon::ENDLOOP0 &&
298 LastOpcode == Hexagon::J2_jump) {
299 TBB = SecondLastInst->getOperand(0).getMBB();
300 Cond.push_back(SecondLastInst->getOperand(0));
301 FBB = LastInst->getOperand(0).getMBB();
305 // Otherwise, can't handle this.
310 unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
311 int BOpc = Hexagon::J2_jump;
312 int BccOpc = Hexagon::J2_jumpt;
313 int BccOpcNot = Hexagon::J2_jumpf;
315 MachineBasicBlock::iterator I = MBB.end();
316 if (I == MBB.begin()) return 0;
318 if (I->getOpcode() != BOpc && I->getOpcode() != BccOpc &&
319 I->getOpcode() != BccOpcNot)
322 // Remove the branch.
323 I->eraseFromParent();
327 if (I == MBB.begin()) return 1;
329 if (I->getOpcode() != BccOpc && I->getOpcode() != BccOpcNot)
332 // Remove the branch.
333 I->eraseFromParent();
338 /// \brief For a comparison instruction, return the source registers in
339 /// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
340 /// compares against in CmpValue. Return true if the comparison instruction
342 bool HexagonInstrInfo::analyzeCompare(const MachineInstr *MI,
343 unsigned &SrcReg, unsigned &SrcReg2,
344 int &Mask, int &Value) const {
345 unsigned Opc = MI->getOpcode();
347 // Set mask and the first source register.
349 case Hexagon::C2_cmpeqp:
350 case Hexagon::C2_cmpeqi:
351 case Hexagon::C2_cmpeq:
352 case Hexagon::C2_cmpgtp:
353 case Hexagon::C2_cmpgtup:
354 case Hexagon::C2_cmpgtui:
355 case Hexagon::C2_cmpgtu:
356 case Hexagon::C2_cmpgti:
357 case Hexagon::C2_cmpgt:
358 SrcReg = MI->getOperand(1).getReg();
361 case Hexagon::A4_cmpbeqi:
362 case Hexagon::A4_cmpbeq:
363 case Hexagon::A4_cmpbgtui:
364 case Hexagon::A4_cmpbgtu:
365 case Hexagon::A4_cmpbgt:
366 SrcReg = MI->getOperand(1).getReg();
369 case Hexagon::A4_cmpheqi:
370 case Hexagon::A4_cmpheq:
371 case Hexagon::A4_cmphgtui:
372 case Hexagon::A4_cmphgtu:
373 case Hexagon::A4_cmphgt:
374 SrcReg = MI->getOperand(1).getReg();
379 // Set the value/second source register.
381 case Hexagon::C2_cmpeqp:
382 case Hexagon::C2_cmpeq:
383 case Hexagon::C2_cmpgtp:
384 case Hexagon::C2_cmpgtup:
385 case Hexagon::C2_cmpgtu:
386 case Hexagon::C2_cmpgt:
387 case Hexagon::A4_cmpbeq:
388 case Hexagon::A4_cmpbgtu:
389 case Hexagon::A4_cmpbgt:
390 case Hexagon::A4_cmpheq:
391 case Hexagon::A4_cmphgtu:
392 case Hexagon::A4_cmphgt:
393 SrcReg2 = MI->getOperand(2).getReg();
396 case Hexagon::C2_cmpeqi:
397 case Hexagon::C2_cmpgtui:
398 case Hexagon::C2_cmpgti:
399 case Hexagon::A4_cmpbeqi:
400 case Hexagon::A4_cmpbgtui:
401 case Hexagon::A4_cmpheqi:
402 case Hexagon::A4_cmphgtui:
404 Value = MI->getOperand(2).getImm();
412 void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
413 MachineBasicBlock::iterator I, DebugLoc DL,
414 unsigned DestReg, unsigned SrcReg,
415 bool KillSrc) const {
416 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
417 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg).addReg(SrcReg);
420 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
421 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg).addReg(SrcReg);
424 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
425 // Map Pd = Ps to Pd = or(Ps, Ps).
426 BuildMI(MBB, I, DL, get(Hexagon::C2_or),
427 DestReg).addReg(SrcReg).addReg(SrcReg);
430 if (Hexagon::DoubleRegsRegClass.contains(DestReg) &&
431 Hexagon::IntRegsRegClass.contains(SrcReg)) {
432 // We can have an overlap between single and double reg: r1:0 = r0.
433 if(SrcReg == RI.getSubReg(DestReg, Hexagon::subreg_loreg)) {
435 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
436 Hexagon::subreg_hireg))).addImm(0);
438 // r1:0 = r1 or no overlap.
439 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), (RI.getSubReg(DestReg,
440 Hexagon::subreg_loreg))).addReg(SrcReg);
441 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
442 Hexagon::subreg_hireg))).addImm(0);
446 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
447 Hexagon::IntRegsRegClass.contains(SrcReg)) {
448 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg).addReg(SrcReg);
451 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
452 Hexagon::IntRegsRegClass.contains(DestReg)) {
453 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg).
454 addReg(SrcReg, getKillRegState(KillSrc));
457 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
458 Hexagon::PredRegsRegClass.contains(DestReg)) {
459 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg).
460 addReg(SrcReg, getKillRegState(KillSrc));
464 llvm_unreachable("Unimplemented");
468 void HexagonInstrInfo::
469 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
470 unsigned SrcReg, bool isKill, int FI,
471 const TargetRegisterClass *RC,
472 const TargetRegisterInfo *TRI) const {
474 DebugLoc DL = MBB.findDebugLoc(I);
475 MachineFunction &MF = *MBB.getParent();
476 MachineFrameInfo &MFI = *MF.getFrameInfo();
477 unsigned Align = MFI.getObjectAlignment(FI);
479 MachineMemOperand *MMO =
480 MF.getMachineMemOperand(
481 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
482 MachineMemOperand::MOStore,
483 MFI.getObjectSize(FI),
486 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
487 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
488 .addFrameIndex(FI).addImm(0)
489 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
490 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
491 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
492 .addFrameIndex(FI).addImm(0)
493 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
494 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
495 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
496 .addFrameIndex(FI).addImm(0)
497 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
499 llvm_unreachable("Unimplemented");
504 void HexagonInstrInfo::storeRegToAddr(
505 MachineFunction &MF, unsigned SrcReg,
507 SmallVectorImpl<MachineOperand> &Addr,
508 const TargetRegisterClass *RC,
509 SmallVectorImpl<MachineInstr*> &NewMIs) const
511 llvm_unreachable("Unimplemented");
515 void HexagonInstrInfo::
516 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
517 unsigned DestReg, int FI,
518 const TargetRegisterClass *RC,
519 const TargetRegisterInfo *TRI) const {
520 DebugLoc DL = MBB.findDebugLoc(I);
521 MachineFunction &MF = *MBB.getParent();
522 MachineFrameInfo &MFI = *MF.getFrameInfo();
523 unsigned Align = MFI.getObjectAlignment(FI);
525 MachineMemOperand *MMO =
526 MF.getMachineMemOperand(
527 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
528 MachineMemOperand::MOLoad,
529 MFI.getObjectSize(FI),
531 if (RC == &Hexagon::IntRegsRegClass) {
532 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
533 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
534 } else if (RC == &Hexagon::DoubleRegsRegClass) {
535 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
536 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
537 } else if (RC == &Hexagon::PredRegsRegClass) {
538 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
539 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
541 llvm_unreachable("Can't store this register to stack slot");
546 void HexagonInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
547 SmallVectorImpl<MachineOperand> &Addr,
548 const TargetRegisterClass *RC,
549 SmallVectorImpl<MachineInstr*> &NewMIs) const {
550 llvm_unreachable("Unimplemented");
554 MachineInstr *HexagonInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
556 const SmallVectorImpl<unsigned> &Ops,
558 // Hexagon_TODO: Implement.
562 unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
564 MachineRegisterInfo &RegInfo = MF->getRegInfo();
565 const TargetRegisterClass *TRC;
567 TRC = &Hexagon::PredRegsRegClass;
568 } else if (VT == MVT::i32 || VT == MVT::f32) {
569 TRC = &Hexagon::IntRegsRegClass;
570 } else if (VT == MVT::i64 || VT == MVT::f64) {
571 TRC = &Hexagon::DoubleRegsRegClass;
573 llvm_unreachable("Cannot handle this register class");
576 unsigned NewReg = RegInfo.createVirtualRegister(TRC);
580 bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
581 // Constant extenders are allowed only for V4 and above.
582 if (!Subtarget.hasV4TOps())
585 const MCInstrDesc &MID = MI->getDesc();
586 const uint64_t F = MID.TSFlags;
587 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
590 // TODO: This is largely obsolete now. Will need to be removed
591 // in consecutive patches.
592 switch(MI->getOpcode()) {
593 // TFR_FI Remains a special case.
594 case Hexagon::TFR_FI:
602 // This returns true in two cases:
603 // - The OP code itself indicates that this is an extended instruction.
604 // - One of MOs has been marked with HMOTF_ConstExtended flag.
605 bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
606 // First check if this is permanently extended op code.
607 const uint64_t F = MI->getDesc().TSFlags;
608 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
610 // Use MO operand flags to determine if one of MI's operands
611 // has HMOTF_ConstExtended flag set.
612 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
613 E = MI->operands_end(); I != E; ++I) {
614 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
620 bool HexagonInstrInfo::isBranch (const MachineInstr *MI) const {
621 return MI->getDesc().isBranch();
624 bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
625 if (isNewValueJump(MI))
628 if (isNewValueStore(MI))
634 bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
635 return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4;
638 bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
639 bool isPred = MI->getDesc().isPredicable();
644 const int Opc = MI->getOpcode();
647 case Hexagon::A2_tfrsi:
648 return isInt<12>(MI->getOperand(1).getImm());
650 case Hexagon::S2_storerd_io:
651 return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
653 case Hexagon::S2_storeri_io:
654 case Hexagon::S2_storerinew_io:
655 return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
657 case Hexagon::S2_storerh_io:
658 case Hexagon::S2_storerhnew_io:
659 return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
661 case Hexagon::S2_storerb_io:
662 case Hexagon::S2_storerbnew_io:
663 return isUInt<6>(MI->getOperand(1).getImm());
665 case Hexagon::L2_loadrd_io:
666 return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
668 case Hexagon::L2_loadri_io:
669 return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
671 case Hexagon::L2_loadrh_io:
672 case Hexagon::L2_loadruh_io:
673 return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
675 case Hexagon::L2_loadrb_io:
676 case Hexagon::L2_loadrub_io:
677 return isUInt<6>(MI->getOperand(2).getImm());
679 case Hexagon::L2_loadrd_pi:
680 return isShiftedInt<4,3>(MI->getOperand(3).getImm());
682 case Hexagon::L2_loadri_pi:
683 return isShiftedInt<4,2>(MI->getOperand(3).getImm());
685 case Hexagon::L2_loadrh_pi:
686 case Hexagon::L2_loadruh_pi:
687 return isShiftedInt<4,1>(MI->getOperand(3).getImm());
689 case Hexagon::L2_loadrb_pi:
690 case Hexagon::L2_loadrub_pi:
691 return isInt<4>(MI->getOperand(3).getImm());
693 case Hexagon::S4_storeirb_io:
694 case Hexagon::S4_storeirh_io:
695 case Hexagon::S4_storeiri_io:
696 return (isUInt<6>(MI->getOperand(1).getImm()) &&
697 isInt<6>(MI->getOperand(2).getImm()));
699 case Hexagon::A2_addi:
700 return isInt<8>(MI->getOperand(2).getImm());
702 case Hexagon::A2_aslh:
703 case Hexagon::A2_asrh:
704 case Hexagon::A2_sxtb:
705 case Hexagon::A2_sxth:
706 case Hexagon::A2_zxtb:
707 case Hexagon::A2_zxth:
708 return Subtarget.hasV4TOps();
714 // This function performs the following inversiones:
719 unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
721 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
722 : Hexagon::getTruePredOpcode(Opc);
723 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
724 return InvPredOpcode;
727 default: llvm_unreachable("Unexpected predicated instruction");
728 case Hexagon::C2_ccombinewt:
729 return Hexagon::C2_ccombinewf;
730 case Hexagon::C2_ccombinewf:
731 return Hexagon::C2_ccombinewt;
734 case Hexagon::L4_return_t:
735 return Hexagon::L4_return_f;
736 case Hexagon::L4_return_f:
737 return Hexagon::L4_return_t;
741 // New Value Store instructions.
742 bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
743 const uint64_t F = MI->getDesc().TSFlags;
745 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
748 bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
749 const uint64_t F = get(Opcode).TSFlags;
751 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
754 int HexagonInstrInfo::
755 getMatchingCondBranchOpcode(int Opc, bool invertPredicate) const {
756 enum Hexagon::PredSense inPredSense;
757 inPredSense = invertPredicate ? Hexagon::PredSense_false :
758 Hexagon::PredSense_true;
759 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
760 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
763 // This switch case will be removed once all the instructions have been
764 // modified to use relation maps.
766 case Hexagon::TFRI_f:
767 return !invertPredicate ? Hexagon::TFRI_cPt_f :
768 Hexagon::TFRI_cNotPt_f;
769 case Hexagon::A2_combinew:
770 return !invertPredicate ? Hexagon::C2_ccombinewt :
771 Hexagon::C2_ccombinewf;
774 case Hexagon::L4_return:
775 return !invertPredicate ? Hexagon::L4_return_t:
776 Hexagon::L4_return_f;
778 llvm_unreachable("Unexpected predicable instruction");
782 bool HexagonInstrInfo::
783 PredicateInstruction(MachineInstr *MI,
784 const SmallVectorImpl<MachineOperand> &Cond) const {
785 int Opc = MI->getOpcode();
786 assert (isPredicable(MI) && "Expected predicable instruction");
787 bool invertJump = (!Cond.empty() && Cond[0].isImm() &&
788 (Cond[0].getImm() == 0));
790 // This will change MI's opcode to its predicate version.
791 // However, its operand list is still the old one, i.e. the
792 // non-predicate one.
793 MI->setDesc(get(getMatchingCondBranchOpcode(Opc, invertJump)));
796 unsigned int GAIdx = 0;
798 // Indicates whether the current MI has a GlobalAddress operand
799 bool hasGAOpnd = false;
800 std::vector<MachineOperand> tmpOpnds;
802 // Indicates whether we need to shift operands to right.
803 bool needShift = true;
805 // The predicate is ALWAYS the FIRST input operand !!!
806 if (MI->getNumOperands() == 0) {
807 // The non-predicate version of MI does not take any operands,
808 // i.e. no outs and no ins. In this condition, the predicate
809 // operand will be directly placed at Operands[0]. No operand
815 else if ( MI->getOperand(MI->getNumOperands()-1).isReg()
816 && MI->getOperand(MI->getNumOperands()-1).isDef()
817 && !MI->getOperand(MI->getNumOperands()-1).isImplicit()) {
818 // The non-predicate version of MI does not have any input operands.
819 // In this condition, we extend the length of Operands[] by one and
820 // copy the original last operand to the newly allocated slot.
821 // At this moment, it is just a place holder. Later, we will put
822 // predicate operand directly into it. No operand shift is needed.
823 // Example: r0=BARRIER (this is a faked insn used here for illustration)
824 MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
826 oper = MI->getNumOperands() - 2;
829 // We need to right shift all input operands by one. Duplicate the
830 // last operand into the newly allocated slot.
831 MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
836 // Operands[ MI->getNumOperands() - 2 ] has been copied into
837 // Operands[ MI->getNumOperands() - 1 ], so we start from
838 // Operands[ MI->getNumOperands() - 3 ].
839 // oper is a signed int.
840 // It is ok if "MI->getNumOperands()-3" is -3, -2, or -1.
841 for (oper = MI->getNumOperands() - 3; oper >= 0; --oper)
843 MachineOperand &MO = MI->getOperand(oper);
845 // Opnd[0] Opnd[1] Opnd[2] Opnd[3] Opnd[4] Opnd[5] Opnd[6] Opnd[7]
846 // <Def0> <Def1> <Use0> <Use1> <ImpDef0> <ImpDef1> <ImpUse0> <ImpUse1>
850 // Predicate Operand here
851 if (MO.isReg() && !MO.isUse() && !MO.isImplicit()) {
855 MI->getOperand(oper+1).ChangeToRegister(MO.getReg(), MO.isDef(),
856 MO.isImplicit(), MO.isKill(),
857 MO.isDead(), MO.isUndef(),
860 else if (MO.isImm()) {
861 MI->getOperand(oper+1).ChangeToImmediate(MO.getImm());
863 else if (MO.isGlobal()) {
864 // MI can not have more than one GlobalAddress operand.
865 assert(hasGAOpnd == false && "MI can only have one GlobalAddress opnd");
867 // There is no member function called "ChangeToGlobalAddress" in the
868 // MachineOperand class (not like "ChangeToRegister" and
869 // "ChangeToImmediate"). So we have to remove them from Operands[] list
870 // first, and then add them back after we have inserted the predicate
871 // operand. tmpOpnds[] is to remember these operands before we remove
873 tmpOpnds.push_back(MO);
875 // Operands[oper] is a GlobalAddress operand;
876 // Operands[oper+1] has been copied into Operands[oper+2];
882 llvm_unreachable("Unexpected operand type");
887 int regPos = invertJump ? 1 : 0;
888 MachineOperand PredMO = Cond[regPos];
890 // [oper] now points to the last explicit Def. Predicate operand must be
891 // located at [oper+1]. See diagram above.
892 // This assumes that the predicate is always the first operand,
893 // i.e. Operands[0+numResults], in the set of inputs
894 // It is better to have an assert here to check this. But I don't know how
895 // to write this assert because findFirstPredOperandIdx() would return -1
896 if (oper < -1) oper = -1;
898 MI->getOperand(oper+1).ChangeToRegister(PredMO.getReg(), PredMO.isDef(),
899 PredMO.isImplicit(), false,
900 PredMO.isDead(), PredMO.isUndef(),
903 MachineRegisterInfo &RegInfo = MI->getParent()->getParent()->getRegInfo();
904 RegInfo.clearKillFlags(PredMO.getReg());
910 // Operands[GAIdx] is the original GlobalAddress operand, which is
911 // already copied into tmpOpnds[0].
912 // Operands[GAIdx] now stores a copy of Operands[GAIdx-1]
913 // Operands[GAIdx+1] has already been copied into Operands[GAIdx+2],
914 // so we start from [GAIdx+2]
915 for (i = GAIdx + 2; i < MI->getNumOperands(); ++i)
916 tmpOpnds.push_back(MI->getOperand(i));
918 // Remove all operands in range [ (GAIdx+1) ... (MI->getNumOperands()-1) ]
919 // It is very important that we always remove from the end of Operands[]
920 // MI->getNumOperands() is at least 2 if program goes to here.
921 for (i = MI->getNumOperands() - 1; i > GAIdx; --i)
922 MI->RemoveOperand(i);
924 for (i = 0; i < tmpOpnds.size(); ++i)
925 MI->addOperand(tmpOpnds[i]);
934 isProfitableToIfCvt(MachineBasicBlock &MBB,
936 unsigned ExtraPredCycles,
937 const BranchProbability &Probability) const {
944 isProfitableToIfCvt(MachineBasicBlock &TMBB,
946 unsigned ExtraTCycles,
947 MachineBasicBlock &FMBB,
949 unsigned ExtraFCycles,
950 const BranchProbability &Probability) const {
954 // Returns true if an instruction is predicated irrespective of the predicate
955 // sense. For example, all of the following will return true.
956 // if (p0) R1 = add(R2, R3)
957 // if (!p0) R1 = add(R2, R3)
958 // if (p0.new) R1 = add(R2, R3)
959 // if (!p0.new) R1 = add(R2, R3)
960 bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
961 const uint64_t F = MI->getDesc().TSFlags;
963 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
966 bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
967 const uint64_t F = get(Opcode).TSFlags;
969 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
972 bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr *MI) const {
973 const uint64_t F = MI->getDesc().TSFlags;
975 assert(isPredicated(MI));
976 return (!((F >> HexagonII::PredicatedFalsePos) &
977 HexagonII::PredicatedFalseMask));
980 bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
981 const uint64_t F = get(Opcode).TSFlags;
983 // Make sure that the instruction is predicated.
984 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
985 return (!((F >> HexagonII::PredicatedFalsePos) &
986 HexagonII::PredicatedFalseMask));
989 bool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
990 const uint64_t F = MI->getDesc().TSFlags;
992 assert(isPredicated(MI));
993 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
996 bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
997 const uint64_t F = get(Opcode).TSFlags;
999 assert(isPredicated(Opcode));
1000 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1003 // Returns true, if a ST insn can be promoted to a new-value store.
1004 bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
1005 const HexagonRegisterInfo& QRI = getRegisterInfo();
1006 const uint64_t F = MI->getDesc().TSFlags;
1008 return ((F >> HexagonII::mayNVStorePos) &
1009 HexagonII::mayNVStoreMask &
1010 QRI.Subtarget.hasV4TOps());
1014 HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
1015 std::vector<MachineOperand> &Pred) const {
1016 for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
1017 MachineOperand MO = MI->getOperand(oper);
1018 if (MO.isReg() && MO.isDef()) {
1019 const TargetRegisterClass* RC = RI.getMinimalPhysRegClass(MO.getReg());
1020 if (RC == &Hexagon::PredRegsRegClass) {
1032 SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
1033 const SmallVectorImpl<MachineOperand> &Pred2) const {
1040 // We indicate that we want to reverse the branch by
1041 // inserting a 0 at the beginning of the Cond vector.
1043 bool HexagonInstrInfo::
1044 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
1045 if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
1046 Cond.erase(Cond.begin());
1048 Cond.insert(Cond.begin(), MachineOperand::CreateImm(0));
1054 bool HexagonInstrInfo::
1055 isProfitableToDupForIfCvt(MachineBasicBlock &MBB,unsigned NumInstrs,
1056 const BranchProbability &Probability) const {
1057 return (NumInstrs <= 4);
1060 bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1061 switch (MI->getOpcode()) {
1062 default: return false;
1063 case Hexagon::L4_return:
1064 case Hexagon::L4_return_t:
1065 case Hexagon::L4_return_f:
1066 case Hexagon::L4_return_tnew_pnt:
1067 case Hexagon::L4_return_fnew_pnt:
1068 case Hexagon::L4_return_tnew_pt:
1069 case Hexagon::L4_return_fnew_pt:
1075 bool HexagonInstrInfo::
1076 isValidOffset(const int Opcode, const int Offset) const {
1077 // This function is to check whether the "Offset" is in the correct range of
1078 // the given "Opcode". If "Offset" is not in the correct range, "ADD_ri" is
1079 // inserted to calculate the final address. Due to this reason, the function
1080 // assumes that the "Offset" has correct alignment.
1081 // We used to assert if the offset was not properly aligned, however,
1082 // there are cases where a misaligned pointer recast can cause this
1083 // problem, and we need to allow for it. The front end warns of such
1084 // misaligns with respect to load size.
1088 case Hexagon::L2_loadri_io:
1089 case Hexagon::S2_storeri_io:
1090 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
1091 (Offset <= Hexagon_MEMW_OFFSET_MAX);
1093 case Hexagon::L2_loadrd_io:
1094 case Hexagon::S2_storerd_io:
1095 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
1096 (Offset <= Hexagon_MEMD_OFFSET_MAX);
1098 case Hexagon::L2_loadrh_io:
1099 case Hexagon::L2_loadruh_io:
1100 case Hexagon::S2_storerh_io:
1101 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
1102 (Offset <= Hexagon_MEMH_OFFSET_MAX);
1104 case Hexagon::L2_loadrb_io:
1105 case Hexagon::S2_storerb_io:
1106 case Hexagon::L2_loadrub_io:
1107 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
1108 (Offset <= Hexagon_MEMB_OFFSET_MAX);
1110 case Hexagon::A2_addi:
1111 case Hexagon::TFR_FI:
1112 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
1113 (Offset <= Hexagon_ADDI_OFFSET_MAX);
1115 case Hexagon::L4_iadd_memopw_io:
1116 case Hexagon::L4_isub_memopw_io:
1117 case Hexagon::L4_add_memopw_io:
1118 case Hexagon::L4_sub_memopw_io:
1119 case Hexagon::L4_and_memopw_io:
1120 case Hexagon::L4_or_memopw_io:
1121 return (0 <= Offset && Offset <= 255);
1123 case Hexagon::L4_iadd_memoph_io:
1124 case Hexagon::L4_isub_memoph_io:
1125 case Hexagon::L4_add_memoph_io:
1126 case Hexagon::L4_sub_memoph_io:
1127 case Hexagon::L4_and_memoph_io:
1128 case Hexagon::L4_or_memoph_io:
1129 return (0 <= Offset && Offset <= 127);
1131 case Hexagon::L4_iadd_memopb_io:
1132 case Hexagon::L4_isub_memopb_io:
1133 case Hexagon::L4_add_memopb_io:
1134 case Hexagon::L4_sub_memopb_io:
1135 case Hexagon::L4_and_memopb_io:
1136 case Hexagon::L4_or_memopb_io:
1137 return (0 <= Offset && Offset <= 63);
1139 // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
1140 // any size. Later pass knows how to handle it.
1141 case Hexagon::STriw_pred:
1142 case Hexagon::LDriw_pred:
1145 case Hexagon::J2_loop0i:
1146 return isUInt<10>(Offset);
1148 // INLINEASM is very special.
1149 case Hexagon::INLINEASM:
1153 llvm_unreachable("No offset range is defined for this opcode. "
1154 "Please define it in the above switch statement!");
1159 // Check if the Offset is a valid auto-inc imm by Load/Store Type.
1161 bool HexagonInstrInfo::
1162 isValidAutoIncImm(const EVT VT, const int Offset) const {
1164 if (VT == MVT::i64) {
1165 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
1166 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
1167 (Offset & 0x7) == 0);
1169 if (VT == MVT::i32) {
1170 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
1171 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
1172 (Offset & 0x3) == 0);
1174 if (VT == MVT::i16) {
1175 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
1176 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
1177 (Offset & 0x1) == 0);
1179 if (VT == MVT::i8) {
1180 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
1181 Offset <= Hexagon_MEMB_AUTOINC_MAX);
1183 llvm_unreachable("Not an auto-inc opc!");
1187 bool HexagonInstrInfo::
1188 isMemOp(const MachineInstr *MI) const {
1189 // return MI->getDesc().mayLoad() && MI->getDesc().mayStore();
1191 switch (MI->getOpcode())
1193 default: return false;
1194 case Hexagon::L4_iadd_memopw_io:
1195 case Hexagon::L4_isub_memopw_io:
1196 case Hexagon::L4_add_memopw_io:
1197 case Hexagon::L4_sub_memopw_io:
1198 case Hexagon::L4_and_memopw_io:
1199 case Hexagon::L4_or_memopw_io:
1200 case Hexagon::L4_iadd_memoph_io:
1201 case Hexagon::L4_isub_memoph_io:
1202 case Hexagon::L4_add_memoph_io:
1203 case Hexagon::L4_sub_memoph_io:
1204 case Hexagon::L4_and_memoph_io:
1205 case Hexagon::L4_or_memoph_io:
1206 case Hexagon::L4_iadd_memopb_io:
1207 case Hexagon::L4_isub_memopb_io:
1208 case Hexagon::L4_add_memopb_io:
1209 case Hexagon::L4_sub_memopb_io:
1210 case Hexagon::L4_and_memopb_io:
1211 case Hexagon::L4_or_memopb_io:
1212 case Hexagon::L4_ior_memopb_io:
1213 case Hexagon::L4_ior_memoph_io:
1214 case Hexagon::L4_ior_memopw_io:
1215 case Hexagon::L4_iand_memopb_io:
1216 case Hexagon::L4_iand_memoph_io:
1217 case Hexagon::L4_iand_memopw_io:
1224 bool HexagonInstrInfo::
1225 isSpillPredRegOp(const MachineInstr *MI) const {
1226 switch (MI->getOpcode()) {
1227 default: return false;
1228 case Hexagon::STriw_pred :
1229 case Hexagon::LDriw_pred :
1234 bool HexagonInstrInfo::isNewValueJumpCandidate(const MachineInstr *MI) const {
1235 switch (MI->getOpcode()) {
1236 default: return false;
1237 case Hexagon::C2_cmpeq:
1238 case Hexagon::C2_cmpeqi:
1239 case Hexagon::C2_cmpgt:
1240 case Hexagon::C2_cmpgti:
1241 case Hexagon::C2_cmpgtu:
1242 case Hexagon::C2_cmpgtui:
1247 bool HexagonInstrInfo::
1248 isConditionalTransfer (const MachineInstr *MI) const {
1249 switch (MI->getOpcode()) {
1250 default: return false;
1251 case Hexagon::A2_tfrt:
1252 case Hexagon::A2_tfrf:
1253 case Hexagon::C2_cmoveit:
1254 case Hexagon::C2_cmoveif:
1255 case Hexagon::A2_tfrtnew:
1256 case Hexagon::A2_tfrfnew:
1257 case Hexagon::C2_cmovenewit:
1258 case Hexagon::C2_cmovenewif:
1263 bool HexagonInstrInfo::isConditionalALU32 (const MachineInstr* MI) const {
1264 switch (MI->getOpcode())
1266 default: return false;
1267 case Hexagon::A2_paddf:
1268 case Hexagon::A2_paddfnew:
1269 case Hexagon::A2_paddt:
1270 case Hexagon::A2_paddtnew:
1271 case Hexagon::A2_pandf:
1272 case Hexagon::A2_pandfnew:
1273 case Hexagon::A2_pandt:
1274 case Hexagon::A2_pandtnew:
1275 case Hexagon::A4_paslhf:
1276 case Hexagon::A4_paslhfnew:
1277 case Hexagon::A4_paslht:
1278 case Hexagon::A4_paslhtnew:
1279 case Hexagon::A4_pasrhf:
1280 case Hexagon::A4_pasrhfnew:
1281 case Hexagon::A4_pasrht:
1282 case Hexagon::A4_pasrhtnew:
1283 case Hexagon::A2_porf:
1284 case Hexagon::A2_porfnew:
1285 case Hexagon::A2_port:
1286 case Hexagon::A2_portnew:
1287 case Hexagon::A2_psubf:
1288 case Hexagon::A2_psubfnew:
1289 case Hexagon::A2_psubt:
1290 case Hexagon::A2_psubtnew:
1291 case Hexagon::A2_pxorf:
1292 case Hexagon::A2_pxorfnew:
1293 case Hexagon::A2_pxort:
1294 case Hexagon::A2_pxortnew:
1295 case Hexagon::A4_psxthf:
1296 case Hexagon::A4_psxthfnew:
1297 case Hexagon::A4_psxtht:
1298 case Hexagon::A4_psxthtnew:
1299 case Hexagon::A4_psxtbf:
1300 case Hexagon::A4_psxtbfnew:
1301 case Hexagon::A4_psxtbt:
1302 case Hexagon::A4_psxtbtnew:
1303 case Hexagon::A4_pzxtbf:
1304 case Hexagon::A4_pzxtbfnew:
1305 case Hexagon::A4_pzxtbt:
1306 case Hexagon::A4_pzxtbtnew:
1307 case Hexagon::A4_pzxthf:
1308 case Hexagon::A4_pzxthfnew:
1309 case Hexagon::A4_pzxtht:
1310 case Hexagon::A4_pzxthtnew:
1311 case Hexagon::A2_paddit:
1312 case Hexagon::A2_paddif:
1313 case Hexagon::C2_ccombinewt:
1314 case Hexagon::C2_ccombinewf:
1319 bool HexagonInstrInfo::
1320 isConditionalLoad (const MachineInstr* MI) const {
1321 const HexagonRegisterInfo& QRI = getRegisterInfo();
1322 switch (MI->getOpcode())
1324 default: return false;
1325 case Hexagon::L2_ploadrdt_io :
1326 case Hexagon::L2_ploadrdf_io:
1327 case Hexagon::L2_ploadrit_io:
1328 case Hexagon::L2_ploadrif_io:
1329 case Hexagon::L2_ploadrht_io:
1330 case Hexagon::L2_ploadrhf_io:
1331 case Hexagon::L2_ploadrbt_io:
1332 case Hexagon::L2_ploadrbf_io:
1333 case Hexagon::L2_ploadruht_io:
1334 case Hexagon::L2_ploadruhf_io:
1335 case Hexagon::L2_ploadrubt_io:
1336 case Hexagon::L2_ploadrubf_io:
1338 case Hexagon::L2_ploadrdt_pi:
1339 case Hexagon::L2_ploadrdf_pi:
1340 case Hexagon::L2_ploadrit_pi:
1341 case Hexagon::L2_ploadrif_pi:
1342 case Hexagon::L2_ploadrht_pi:
1343 case Hexagon::L2_ploadrhf_pi:
1344 case Hexagon::L2_ploadrbt_pi:
1345 case Hexagon::L2_ploadrbf_pi:
1346 case Hexagon::L2_ploadruht_pi:
1347 case Hexagon::L2_ploadruhf_pi:
1348 case Hexagon::L2_ploadrubt_pi:
1349 case Hexagon::L2_ploadrubf_pi:
1350 return QRI.Subtarget.hasV4TOps();
1351 case Hexagon::L4_ploadrdt_rr:
1352 case Hexagon::L4_ploadrdf_rr:
1353 case Hexagon::L4_ploadrbt_rr:
1354 case Hexagon::L4_ploadrbf_rr:
1355 case Hexagon::L4_ploadrubt_rr:
1356 case Hexagon::L4_ploadrubf_rr:
1357 case Hexagon::L4_ploadrht_rr:
1358 case Hexagon::L4_ploadrhf_rr:
1359 case Hexagon::L4_ploadruht_rr:
1360 case Hexagon::L4_ploadruhf_rr:
1361 case Hexagon::L4_ploadrit_rr:
1362 case Hexagon::L4_ploadrif_rr:
1363 return QRI.Subtarget.hasV4TOps();
1367 // Returns true if an instruction is a conditional store.
1369 // Note: It doesn't include conditional new-value stores as they can't be
1370 // converted to .new predicate.
1372 // p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
1374 // / \ (not OK. it will cause new-value store to be
1375 // / X conditional on p0.new while R2 producer is
1378 // p.new store p.old NV store
1379 // [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
1385 // [if (p0)memw(R0+#0)=R2]
1387 // The above diagram shows the steps involoved in the conversion of a predicated
1388 // store instruction to its .new predicated new-value form.
1390 // The following set of instructions further explains the scenario where
1391 // conditional new-value store becomes invalid when promoted to .new predicate
1394 // { 1) if (p0) r0 = add(r1, r2)
1395 // 2) p0 = cmp.eq(r3, #0) }
1397 // 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
1398 // the first two instructions because in instr 1, r0 is conditional on old value
1399 // of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
1400 // is not valid for new-value stores.
1401 bool HexagonInstrInfo::
1402 isConditionalStore (const MachineInstr* MI) const {
1403 const HexagonRegisterInfo& QRI = getRegisterInfo();
1404 switch (MI->getOpcode())
1406 default: return false;
1407 case Hexagon::S4_storeirbt_io:
1408 case Hexagon::S4_storeirbf_io:
1409 case Hexagon::S4_pstorerbt_rr:
1410 case Hexagon::S4_pstorerbf_rr:
1411 case Hexagon::S2_pstorerbt_io:
1412 case Hexagon::S2_pstorerbf_io:
1413 case Hexagon::S2_pstorerbt_pi:
1414 case Hexagon::S2_pstorerbf_pi:
1415 case Hexagon::S2_pstorerdt_io:
1416 case Hexagon::S2_pstorerdf_io:
1417 case Hexagon::S4_pstorerdt_rr:
1418 case Hexagon::S4_pstorerdf_rr:
1419 case Hexagon::S2_pstorerdt_pi:
1420 case Hexagon::S2_pstorerdf_pi:
1421 case Hexagon::S2_pstorerht_io:
1422 case Hexagon::S2_pstorerhf_io:
1423 case Hexagon::S4_storeirht_io:
1424 case Hexagon::S4_storeirhf_io:
1425 case Hexagon::S4_pstorerht_rr:
1426 case Hexagon::S4_pstorerhf_rr:
1427 case Hexagon::S2_pstorerht_pi:
1428 case Hexagon::S2_pstorerhf_pi:
1429 case Hexagon::S2_pstorerit_io:
1430 case Hexagon::S2_pstorerif_io:
1431 case Hexagon::S4_storeirit_io:
1432 case Hexagon::S4_storeirif_io:
1433 case Hexagon::S4_pstorerit_rr:
1434 case Hexagon::S4_pstorerif_rr:
1435 case Hexagon::S2_pstorerit_pi:
1436 case Hexagon::S2_pstorerif_pi:
1437 return QRI.Subtarget.hasV4TOps();
1439 // V4 global address store before promoting to dot new.
1440 case Hexagon::S4_pstorerdt_abs:
1441 case Hexagon::S4_pstorerdf_abs:
1442 case Hexagon::S4_pstorerbt_abs:
1443 case Hexagon::S4_pstorerbf_abs:
1444 case Hexagon::S4_pstorerht_abs:
1445 case Hexagon::S4_pstorerhf_abs:
1446 case Hexagon::S4_pstorerit_abs:
1447 case Hexagon::S4_pstorerif_abs:
1448 return QRI.Subtarget.hasV4TOps();
1450 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1451 // from the "Conditional Store" list. Because a predicated new value store
1452 // would NOT be promoted to a double dot new store. See diagram below:
1453 // This function returns yes for those stores that are predicated but not
1454 // yet promoted to predicate dot new instructions.
1456 // +---------------------+
1457 // /-----| if (p0) memw(..)=r0 |---------\~
1458 // || +---------------------+ ||
1459 // promote || /\ /\ || promote
1461 // \||/ demote || \||/
1463 // +-------------------------+ || +-------------------------+
1464 // | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
1465 // +-------------------------+ || +-------------------------+
1468 // promote || \/ NOT possible
1472 // +-----------------------------+
1473 // | if (p0.new) memw(..)=r0.new |
1474 // +-----------------------------+
1475 // Double Dot New Store
1481 bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
1482 if (isNewValue(MI) && isBranch(MI))
1487 bool HexagonInstrInfo::isPostIncrement (const MachineInstr* MI) const {
1488 return (getAddrMode(MI) == HexagonII::PostInc);
1491 bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
1492 const uint64_t F = MI->getDesc().TSFlags;
1493 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
1496 // Returns true, if any one of the operands is a dot new
1497 // insn, whether it is predicated dot new or register dot new.
1498 bool HexagonInstrInfo::isDotNewInst (const MachineInstr* MI) const {
1499 return (isNewValueInst(MI) ||
1500 (isPredicated(MI) && isPredicatedNew(MI)));
1503 // Returns the most basic instruction for the .new predicated instructions and
1504 // new-value stores.
1505 // For example, all of the following instructions will be converted back to the
1506 // same instruction:
1507 // 1) if (p0.new) memw(R0+#0) = R1.new --->
1508 // 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
1509 // 3) if (p0.new) memw(R0+#0) = R1 --->
1512 int HexagonInstrInfo::GetDotOldOp(const int opc) const {
1514 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
1515 NewOp = Hexagon::getPredOldOpcode(NewOp);
1516 assert(NewOp >= 0 &&
1517 "Couldn't change predicate new instruction to its old form.");
1520 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
1521 NewOp = Hexagon::getNonNVStore(NewOp);
1522 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
1527 // Return the new value instruction for a given store.
1528 int HexagonInstrInfo::GetDotNewOp(const MachineInstr* MI) const {
1529 int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
1530 if (NVOpcode >= 0) // Valid new-value store instruction.
1533 switch (MI->getOpcode()) {
1534 default: llvm_unreachable("Unknown .new type");
1535 // store new value byte
1536 case Hexagon::S4_storerb_ur:
1537 return Hexagon::S4_storerbnew_ur;
1539 case Hexagon::S4_storerh_ur:
1540 return Hexagon::S4_storerhnew_ur;
1542 case Hexagon::S4_storeri_ur:
1543 return Hexagon::S4_storerinew_ur;
1549 // Return .new predicate version for an instruction.
1550 int HexagonInstrInfo::GetDotNewPredOp(MachineInstr *MI,
1551 const MachineBranchProbabilityInfo
1554 int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1555 if (NewOpcode >= 0) // Valid predicate new instruction
1558 switch (MI->getOpcode()) {
1559 default: llvm_unreachable("Unknown .new type");
1561 case Hexagon::J2_jumpt:
1562 case Hexagon::J2_jumpf:
1563 return getDotNewPredJumpOp(MI, MBPI);
1565 case Hexagon::J2_jumprt:
1566 return Hexagon::J2_jumptnewpt;
1568 case Hexagon::J2_jumprf:
1569 return Hexagon::J2_jumprfnewpt;
1571 case Hexagon::JMPrett:
1572 return Hexagon::J2_jumprtnewpt;
1574 case Hexagon::JMPretf:
1575 return Hexagon::J2_jumprfnewpt;
1578 // Conditional combine
1579 case Hexagon::C2_ccombinewt:
1580 return Hexagon::C2_ccombinewnewt;
1581 case Hexagon::C2_ccombinewf:
1582 return Hexagon::C2_ccombinewnewf;
1587 unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
1588 const uint64_t F = MI->getDesc().TSFlags;
1590 return((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
1593 /// immediateExtend - Changes the instruction in place to one using an immediate
1595 void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
1596 assert((isExtendable(MI)||isConstExtended(MI)) &&
1597 "Instruction must be extendable");
1598 // Find which operand is extendable.
1599 short ExtOpNum = getCExtOpNum(MI);
1600 MachineOperand &MO = MI->getOperand(ExtOpNum);
1601 // This needs to be something we understand.
1602 assert((MO.isMBB() || MO.isImm()) &&
1603 "Branch with unknown extendable field type");
1604 // Mark given operand as extended.
1605 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
1608 DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1609 const TargetSubtargetInfo &STI) const {
1610 const InstrItineraryData *II = STI.getInstrItineraryData();
1611 return static_cast<const HexagonSubtarget &>(STI).createDFAPacketizer(II);
1614 bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1615 const MachineBasicBlock *MBB,
1616 const MachineFunction &MF) const {
1617 // Debug info is never a scheduling boundary. It's necessary to be explicit
1618 // due to the special treatment of IT instructions below, otherwise a
1619 // dbg_value followed by an IT will result in the IT instruction being
1620 // considered a scheduling hazard, which is wrong. It should be the actual
1621 // instruction preceding the dbg_value instruction(s), just like it is
1622 // when debug info is not present.
1623 if (MI->isDebugValue())
1626 // Terminators and labels can't be scheduled around.
1627 if (MI->getDesc().isTerminator() || MI->isPosition() || MI->isInlineAsm())
1633 bool HexagonInstrInfo::isConstExtended(MachineInstr *MI) const {
1635 // Constant extenders are allowed only for V4 and above.
1636 if (!Subtarget.hasV4TOps())
1639 const uint64_t F = MI->getDesc().TSFlags;
1640 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1641 if (isExtended) // Instruction must be extended.
1644 unsigned isExtendable = (F >> HexagonII::ExtendablePos)
1645 & HexagonII::ExtendableMask;
1649 short ExtOpNum = getCExtOpNum(MI);
1650 const MachineOperand &MO = MI->getOperand(ExtOpNum);
1651 // Use MO operand flags to determine if MO
1652 // has the HMOTF_ConstExtended flag set.
1653 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1655 // If this is a Machine BB address we are talking about, and it is
1656 // not marked as extended, say so.
1660 // We could be using an instruction with an extendable immediate and shoehorn
1661 // a global address into it. If it is a global address it will be constant
1662 // extended. We do this for COMBINE.
1663 // We currently only handle isGlobal() because it is the only kind of
1664 // object we are going to end up with here for now.
1665 // In the future we probably should add isSymbol(), etc.
1666 if (MO.isGlobal() || MO.isSymbol())
1669 // If the extendable operand is not 'Immediate' type, the instruction should
1670 // have 'isExtended' flag set.
1671 assert(MO.isImm() && "Extendable operand must be Immediate type");
1673 int MinValue = getMinValue(MI);
1674 int MaxValue = getMaxValue(MI);
1675 int ImmValue = MO.getImm();
1677 return (ImmValue < MinValue || ImmValue > MaxValue);
1680 // Returns the opcode to use when converting MI, which is a conditional jump,
1681 // into a conditional instruction which uses the .new value of the predicate.
1682 // We also use branch probabilities to add a hint to the jump.
1684 HexagonInstrInfo::getDotNewPredJumpOp(MachineInstr *MI,
1686 MachineBranchProbabilityInfo *MBPI) const {
1688 // We assume that block can have at most two successors.
1690 MachineBasicBlock *Src = MI->getParent();
1691 MachineOperand *BrTarget = &MI->getOperand(1);
1692 MachineBasicBlock *Dst = BrTarget->getMBB();
1694 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
1695 if (Prediction >= BranchProbability(1,2))
1698 switch (MI->getOpcode()) {
1699 case Hexagon::J2_jumpt:
1700 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
1701 case Hexagon::J2_jumpf:
1702 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
1705 llvm_unreachable("Unexpected jump instruction.");
1708 // Returns true if a particular operand is extendable for an instruction.
1709 bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
1710 unsigned short OperandNum) const {
1711 // Constant extenders are allowed only for V4 and above.
1712 if (!Subtarget.hasV4TOps())
1715 const uint64_t F = MI->getDesc().TSFlags;
1717 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
1721 // Returns Operand Index for the constant extended instruction.
1722 unsigned short HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
1723 const uint64_t F = MI->getDesc().TSFlags;
1724 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
1727 // Returns the min value that doesn't need to be extended.
1728 int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
1729 const uint64_t F = MI->getDesc().TSFlags;
1730 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1731 & HexagonII::ExtentSignedMask;
1732 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1733 & HexagonII::ExtentBitsMask;
1735 if (isSigned) // if value is signed
1736 return -1U << (bits - 1);
1741 // Returns the max value that doesn't need to be extended.
1742 int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
1743 const uint64_t F = MI->getDesc().TSFlags;
1744 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1745 & HexagonII::ExtentSignedMask;
1746 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1747 & HexagonII::ExtentBitsMask;
1749 if (isSigned) // if value is signed
1750 return ~(-1U << (bits - 1));
1752 return ~(-1U << bits);
1755 // Returns true if an instruction can be converted into a non-extended
1756 // equivalent instruction.
1757 bool HexagonInstrInfo::NonExtEquivalentExists (const MachineInstr *MI) const {
1760 // Check if the instruction has a register form that uses register in place
1761 // of the extended operand, if so return that as the non-extended form.
1762 if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
1765 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
1766 // Check addressing mode and retrieve non-ext equivalent instruction.
1768 switch (getAddrMode(MI)) {
1769 case HexagonII::Absolute :
1770 // Load/store with absolute addressing mode can be converted into
1771 // base+offset mode.
1772 NonExtOpcode = Hexagon::getBasedWithImmOffset(MI->getOpcode());
1774 case HexagonII::BaseImmOffset :
1775 // Load/store with base+offset addressing mode can be converted into
1776 // base+register offset addressing mode. However left shift operand should
1778 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
1783 if (NonExtOpcode < 0)
1790 // Returns opcode of the non-extended equivalent instruction.
1791 short HexagonInstrInfo::getNonExtOpcode (const MachineInstr *MI) const {
1793 // Check if the instruction has a register form that uses register in place
1794 // of the extended operand, if so return that as the non-extended form.
1795 short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
1796 if (NonExtOpcode >= 0)
1797 return NonExtOpcode;
1799 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
1800 // Check addressing mode and retrieve non-ext equivalent instruction.
1801 switch (getAddrMode(MI)) {
1802 case HexagonII::Absolute :
1803 return Hexagon::getBasedWithImmOffset(MI->getOpcode());
1804 case HexagonII::BaseImmOffset :
1805 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
1813 bool HexagonInstrInfo::PredOpcodeHasJMP_c(Opcode_t Opcode) const {
1814 return (Opcode == Hexagon::J2_jumpt) ||
1815 (Opcode == Hexagon::J2_jumpf) ||
1816 (Opcode == Hexagon::J2_jumptnewpt) ||
1817 (Opcode == Hexagon::J2_jumpfnewpt) ||
1818 (Opcode == Hexagon::J2_jumpt) ||
1819 (Opcode == Hexagon::J2_jumpf);
1822 bool HexagonInstrInfo::PredOpcodeHasNot(Opcode_t Opcode) const {
1823 return (Opcode == Hexagon::J2_jumpf) ||
1824 (Opcode == Hexagon::J2_jumpfnewpt) ||
1825 (Opcode == Hexagon::J2_jumpfnew);