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 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
216 I->getOperand(0).isMBB();
217 // Delete the JMP if it's equivalent to a fall-through.
218 if (AllowModify && JumpToBlock &&
219 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
220 DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
221 I->eraseFromParent();
223 if (I == MBB.instr_begin())
227 if (!isUnpredicatedTerminator(I))
230 // Get the last instruction in the block.
231 MachineInstr *LastInst = I;
232 MachineInstr *SecondLastInst = nullptr;
233 // Find one more terminator if present.
235 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(I)) {
239 // This is a third branch.
242 if (I == MBB.instr_begin())
247 int LastOpcode = LastInst->getOpcode();
248 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
249 // If the branch target is not a basic block, it could be a tail call.
250 // (It is, if the target is a function.)
251 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
253 if (SecLastOpcode == Hexagon::J2_jump &&
254 !SecondLastInst->getOperand(0).isMBB())
257 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
258 bool LastOpcodeHasNot = PredOpcodeHasNot(LastOpcode);
260 // If there is only one terminator instruction, process it.
261 if (LastInst && !SecondLastInst) {
262 if (LastOpcode == Hexagon::J2_jump) {
263 TBB = LastInst->getOperand(0).getMBB();
266 if (LastOpcode == Hexagon::ENDLOOP0) {
267 TBB = LastInst->getOperand(0).getMBB();
268 Cond.push_back(LastInst->getOperand(0));
271 if (LastOpcodeHasJMP_c) {
272 TBB = LastInst->getOperand(1).getMBB();
273 if (LastOpcodeHasNot) {
274 Cond.push_back(MachineOperand::CreateImm(0));
276 Cond.push_back(LastInst->getOperand(0));
279 // Otherwise, don't know what this is.
283 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
284 bool SecLastOpcodeHasNot = PredOpcodeHasNot(SecLastOpcode);
285 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
286 TBB = SecondLastInst->getOperand(1).getMBB();
287 if (SecLastOpcodeHasNot)
288 Cond.push_back(MachineOperand::CreateImm(0));
289 Cond.push_back(SecondLastInst->getOperand(0));
290 FBB = LastInst->getOperand(0).getMBB();
294 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
295 // executed, so remove it.
296 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
297 TBB = SecondLastInst->getOperand(0).getMBB();
300 I->eraseFromParent();
304 // If the block ends with an ENDLOOP, and JMP, handle it.
305 if (SecLastOpcode == Hexagon::ENDLOOP0 &&
306 LastOpcode == Hexagon::J2_jump) {
307 TBB = SecondLastInst->getOperand(0).getMBB();
308 Cond.push_back(SecondLastInst->getOperand(0));
309 FBB = LastInst->getOperand(0).getMBB();
313 // Otherwise, can't handle this.
318 unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
319 int BOpc = Hexagon::J2_jump;
320 int BccOpc = Hexagon::J2_jumpt;
321 int BccOpcNot = Hexagon::J2_jumpf;
323 MachineBasicBlock::iterator I = MBB.end();
324 if (I == MBB.begin()) return 0;
326 if (I->getOpcode() != BOpc && I->getOpcode() != BccOpc &&
327 I->getOpcode() != BccOpcNot)
330 // Remove the branch.
331 I->eraseFromParent();
335 if (I == MBB.begin()) return 1;
337 if (I->getOpcode() != BccOpc && I->getOpcode() != BccOpcNot)
340 // Remove the branch.
341 I->eraseFromParent();
346 /// \brief For a comparison instruction, return the source registers in
347 /// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
348 /// compares against in CmpValue. Return true if the comparison instruction
350 bool HexagonInstrInfo::analyzeCompare(const MachineInstr *MI,
351 unsigned &SrcReg, unsigned &SrcReg2,
352 int &Mask, int &Value) const {
353 unsigned Opc = MI->getOpcode();
355 // Set mask and the first source register.
357 case Hexagon::C2_cmpeqp:
358 case Hexagon::C2_cmpeqi:
359 case Hexagon::C2_cmpeq:
360 case Hexagon::C2_cmpgtp:
361 case Hexagon::C2_cmpgtup:
362 case Hexagon::C2_cmpgtui:
363 case Hexagon::C2_cmpgtu:
364 case Hexagon::C2_cmpgti:
365 case Hexagon::C2_cmpgt:
366 SrcReg = MI->getOperand(1).getReg();
369 case Hexagon::A4_cmpbeqi:
370 case Hexagon::A4_cmpbeq:
371 case Hexagon::A4_cmpbgtui:
372 case Hexagon::A4_cmpbgtu:
373 case Hexagon::A4_cmpbgt:
374 SrcReg = MI->getOperand(1).getReg();
377 case Hexagon::A4_cmpheqi:
378 case Hexagon::A4_cmpheq:
379 case Hexagon::A4_cmphgtui:
380 case Hexagon::A4_cmphgtu:
381 case Hexagon::A4_cmphgt:
382 SrcReg = MI->getOperand(1).getReg();
387 // Set the value/second source register.
389 case Hexagon::C2_cmpeqp:
390 case Hexagon::C2_cmpeq:
391 case Hexagon::C2_cmpgtp:
392 case Hexagon::C2_cmpgtup:
393 case Hexagon::C2_cmpgtu:
394 case Hexagon::C2_cmpgt:
395 case Hexagon::A4_cmpbeq:
396 case Hexagon::A4_cmpbgtu:
397 case Hexagon::A4_cmpbgt:
398 case Hexagon::A4_cmpheq:
399 case Hexagon::A4_cmphgtu:
400 case Hexagon::A4_cmphgt:
401 SrcReg2 = MI->getOperand(2).getReg();
404 case Hexagon::C2_cmpeqi:
405 case Hexagon::C2_cmpgtui:
406 case Hexagon::C2_cmpgti:
407 case Hexagon::A4_cmpbeqi:
408 case Hexagon::A4_cmpbgtui:
409 case Hexagon::A4_cmpheqi:
410 case Hexagon::A4_cmphgtui:
412 Value = MI->getOperand(2).getImm();
420 void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
421 MachineBasicBlock::iterator I, DebugLoc DL,
422 unsigned DestReg, unsigned SrcReg,
423 bool KillSrc) const {
424 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
425 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg).addReg(SrcReg);
428 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
429 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg).addReg(SrcReg);
432 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
433 // Map Pd = Ps to Pd = or(Ps, Ps).
434 BuildMI(MBB, I, DL, get(Hexagon::C2_or),
435 DestReg).addReg(SrcReg).addReg(SrcReg);
438 if (Hexagon::DoubleRegsRegClass.contains(DestReg) &&
439 Hexagon::IntRegsRegClass.contains(SrcReg)) {
440 // We can have an overlap between single and double reg: r1:0 = r0.
441 if(SrcReg == RI.getSubReg(DestReg, Hexagon::subreg_loreg)) {
443 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
444 Hexagon::subreg_hireg))).addImm(0);
446 // r1:0 = r1 or no overlap.
447 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), (RI.getSubReg(DestReg,
448 Hexagon::subreg_loreg))).addReg(SrcReg);
449 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
450 Hexagon::subreg_hireg))).addImm(0);
454 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
455 Hexagon::IntRegsRegClass.contains(SrcReg)) {
456 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg).addReg(SrcReg);
459 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
460 Hexagon::IntRegsRegClass.contains(DestReg)) {
461 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg).
462 addReg(SrcReg, getKillRegState(KillSrc));
465 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
466 Hexagon::PredRegsRegClass.contains(DestReg)) {
467 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg).
468 addReg(SrcReg, getKillRegState(KillSrc));
472 llvm_unreachable("Unimplemented");
476 void HexagonInstrInfo::
477 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
478 unsigned SrcReg, bool isKill, int FI,
479 const TargetRegisterClass *RC,
480 const TargetRegisterInfo *TRI) const {
482 DebugLoc DL = MBB.findDebugLoc(I);
483 MachineFunction &MF = *MBB.getParent();
484 MachineFrameInfo &MFI = *MF.getFrameInfo();
485 unsigned Align = MFI.getObjectAlignment(FI);
487 MachineMemOperand *MMO =
488 MF.getMachineMemOperand(
489 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
490 MachineMemOperand::MOStore,
491 MFI.getObjectSize(FI),
494 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
495 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
496 .addFrameIndex(FI).addImm(0)
497 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
498 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
499 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
500 .addFrameIndex(FI).addImm(0)
501 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
502 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
503 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
504 .addFrameIndex(FI).addImm(0)
505 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
507 llvm_unreachable("Unimplemented");
512 void HexagonInstrInfo::storeRegToAddr(
513 MachineFunction &MF, unsigned SrcReg,
515 SmallVectorImpl<MachineOperand> &Addr,
516 const TargetRegisterClass *RC,
517 SmallVectorImpl<MachineInstr*> &NewMIs) const
519 llvm_unreachable("Unimplemented");
523 void HexagonInstrInfo::
524 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
525 unsigned DestReg, int FI,
526 const TargetRegisterClass *RC,
527 const TargetRegisterInfo *TRI) const {
528 DebugLoc DL = MBB.findDebugLoc(I);
529 MachineFunction &MF = *MBB.getParent();
530 MachineFrameInfo &MFI = *MF.getFrameInfo();
531 unsigned Align = MFI.getObjectAlignment(FI);
533 MachineMemOperand *MMO =
534 MF.getMachineMemOperand(
535 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
536 MachineMemOperand::MOLoad,
537 MFI.getObjectSize(FI),
539 if (RC == &Hexagon::IntRegsRegClass) {
540 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
541 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
542 } else if (RC == &Hexagon::DoubleRegsRegClass) {
543 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
544 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
545 } else if (RC == &Hexagon::PredRegsRegClass) {
546 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
547 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
549 llvm_unreachable("Can't store this register to stack slot");
554 void HexagonInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
555 SmallVectorImpl<MachineOperand> &Addr,
556 const TargetRegisterClass *RC,
557 SmallVectorImpl<MachineInstr*> &NewMIs) const {
558 llvm_unreachable("Unimplemented");
561 HexagonInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
562 unsigned Opc = MI->getOpcode();
565 case Hexagon::TCRETURNi:
566 MI->setDesc(get(Hexagon::J2_jump));
568 case Hexagon::TCRETURNr:
569 MI->setDesc(get(Hexagon::J2_jumpr));
576 MachineInstr *HexagonInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
578 ArrayRef<unsigned> Ops,
580 // Hexagon_TODO: Implement.
584 unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
586 MachineRegisterInfo &RegInfo = MF->getRegInfo();
587 const TargetRegisterClass *TRC;
589 TRC = &Hexagon::PredRegsRegClass;
590 } else if (VT == MVT::i32 || VT == MVT::f32) {
591 TRC = &Hexagon::IntRegsRegClass;
592 } else if (VT == MVT::i64 || VT == MVT::f64) {
593 TRC = &Hexagon::DoubleRegsRegClass;
595 llvm_unreachable("Cannot handle this register class");
598 unsigned NewReg = RegInfo.createVirtualRegister(TRC);
602 bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
603 const MCInstrDesc &MID = MI->getDesc();
604 const uint64_t F = MID.TSFlags;
605 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
608 // TODO: This is largely obsolete now. Will need to be removed
609 // in consecutive patches.
610 switch(MI->getOpcode()) {
611 // TFR_FI Remains a special case.
612 case Hexagon::TFR_FI:
620 // This returns true in two cases:
621 // - The OP code itself indicates that this is an extended instruction.
622 // - One of MOs has been marked with HMOTF_ConstExtended flag.
623 bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
624 // First check if this is permanently extended op code.
625 const uint64_t F = MI->getDesc().TSFlags;
626 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
628 // Use MO operand flags to determine if one of MI's operands
629 // has HMOTF_ConstExtended flag set.
630 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
631 E = MI->operands_end(); I != E; ++I) {
632 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
638 bool HexagonInstrInfo::isBranch (const MachineInstr *MI) const {
639 return MI->getDesc().isBranch();
642 bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
643 if (isNewValueJump(MI))
646 if (isNewValueStore(MI))
652 bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
653 return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4;
656 bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
657 bool isPred = MI->getDesc().isPredicable();
662 const int Opc = MI->getOpcode();
665 case Hexagon::A2_tfrsi:
666 return (isOperandExtended(MI, 1) && isConstExtended(MI)) || isInt<12>(MI->getOperand(1).getImm());
668 case Hexagon::S2_storerd_io:
669 return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
671 case Hexagon::S2_storeri_io:
672 case Hexagon::S2_storerinew_io:
673 return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
675 case Hexagon::S2_storerh_io:
676 case Hexagon::S2_storerhnew_io:
677 return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
679 case Hexagon::S2_storerb_io:
680 case Hexagon::S2_storerbnew_io:
681 return isUInt<6>(MI->getOperand(1).getImm());
683 case Hexagon::L2_loadrd_io:
684 return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
686 case Hexagon::L2_loadri_io:
687 return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
689 case Hexagon::L2_loadrh_io:
690 case Hexagon::L2_loadruh_io:
691 return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
693 case Hexagon::L2_loadrb_io:
694 case Hexagon::L2_loadrub_io:
695 return isUInt<6>(MI->getOperand(2).getImm());
697 case Hexagon::L2_loadrd_pi:
698 return isShiftedInt<4,3>(MI->getOperand(3).getImm());
700 case Hexagon::L2_loadri_pi:
701 return isShiftedInt<4,2>(MI->getOperand(3).getImm());
703 case Hexagon::L2_loadrh_pi:
704 case Hexagon::L2_loadruh_pi:
705 return isShiftedInt<4,1>(MI->getOperand(3).getImm());
707 case Hexagon::L2_loadrb_pi:
708 case Hexagon::L2_loadrub_pi:
709 return isInt<4>(MI->getOperand(3).getImm());
711 case Hexagon::S4_storeirb_io:
712 case Hexagon::S4_storeirh_io:
713 case Hexagon::S4_storeiri_io:
714 return (isUInt<6>(MI->getOperand(1).getImm()) &&
715 isInt<6>(MI->getOperand(2).getImm()));
717 case Hexagon::A2_addi:
718 return isInt<8>(MI->getOperand(2).getImm());
720 case Hexagon::A2_aslh:
721 case Hexagon::A2_asrh:
722 case Hexagon::A2_sxtb:
723 case Hexagon::A2_sxth:
724 case Hexagon::A2_zxtb:
725 case Hexagon::A2_zxth:
732 // This function performs the following inversiones:
737 unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
739 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
740 : Hexagon::getTruePredOpcode(Opc);
741 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
742 return InvPredOpcode;
745 default: llvm_unreachable("Unexpected predicated instruction");
746 case Hexagon::C2_ccombinewt:
747 return Hexagon::C2_ccombinewf;
748 case Hexagon::C2_ccombinewf:
749 return Hexagon::C2_ccombinewt;
752 case Hexagon::L4_return_t:
753 return Hexagon::L4_return_f;
754 case Hexagon::L4_return_f:
755 return Hexagon::L4_return_t;
759 // New Value Store instructions.
760 bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
761 const uint64_t F = MI->getDesc().TSFlags;
763 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
766 bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
767 const uint64_t F = get(Opcode).TSFlags;
769 return ((F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask);
772 int HexagonInstrInfo::
773 getMatchingCondBranchOpcode(int Opc, bool invertPredicate) const {
774 enum Hexagon::PredSense inPredSense;
775 inPredSense = invertPredicate ? Hexagon::PredSense_false :
776 Hexagon::PredSense_true;
777 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
778 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
781 // This switch case will be removed once all the instructions have been
782 // modified to use relation maps.
784 case Hexagon::TFRI_f:
785 return !invertPredicate ? Hexagon::TFRI_cPt_f :
786 Hexagon::TFRI_cNotPt_f;
787 case Hexagon::A2_combinew:
788 return !invertPredicate ? Hexagon::C2_ccombinewt :
789 Hexagon::C2_ccombinewf;
792 case Hexagon::L4_return:
793 return !invertPredicate ? Hexagon::L4_return_t:
794 Hexagon::L4_return_f;
796 llvm_unreachable("Unexpected predicable instruction");
800 bool HexagonInstrInfo::
801 PredicateInstruction(MachineInstr *MI,
802 const SmallVectorImpl<MachineOperand> &Cond) const {
803 int Opc = MI->getOpcode();
804 assert (isPredicable(MI) && "Expected predicable instruction");
805 bool invertJump = (!Cond.empty() && Cond[0].isImm() &&
806 (Cond[0].getImm() == 0));
808 // This will change MI's opcode to its predicate version.
809 // However, its operand list is still the old one, i.e. the
810 // non-predicate one.
811 MI->setDesc(get(getMatchingCondBranchOpcode(Opc, invertJump)));
814 unsigned int GAIdx = 0;
816 // Indicates whether the current MI has a GlobalAddress operand
817 bool hasGAOpnd = false;
818 std::vector<MachineOperand> tmpOpnds;
820 // Indicates whether we need to shift operands to right.
821 bool needShift = true;
823 // The predicate is ALWAYS the FIRST input operand !!!
824 if (MI->getNumOperands() == 0) {
825 // The non-predicate version of MI does not take any operands,
826 // i.e. no outs and no ins. In this condition, the predicate
827 // operand will be directly placed at Operands[0]. No operand
833 else if ( MI->getOperand(MI->getNumOperands()-1).isReg()
834 && MI->getOperand(MI->getNumOperands()-1).isDef()
835 && !MI->getOperand(MI->getNumOperands()-1).isImplicit()) {
836 // The non-predicate version of MI does not have any input operands.
837 // In this condition, we extend the length of Operands[] by one and
838 // copy the original last operand to the newly allocated slot.
839 // At this moment, it is just a place holder. Later, we will put
840 // predicate operand directly into it. No operand shift is needed.
841 // Example: r0=BARRIER (this is a faked insn used here for illustration)
842 MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
844 oper = MI->getNumOperands() - 2;
847 // We need to right shift all input operands by one. Duplicate the
848 // last operand into the newly allocated slot.
849 MI->addOperand(MI->getOperand(MI->getNumOperands()-1));
854 // Operands[ MI->getNumOperands() - 2 ] has been copied into
855 // Operands[ MI->getNumOperands() - 1 ], so we start from
856 // Operands[ MI->getNumOperands() - 3 ].
857 // oper is a signed int.
858 // It is ok if "MI->getNumOperands()-3" is -3, -2, or -1.
859 for (oper = MI->getNumOperands() - 3; oper >= 0; --oper)
861 MachineOperand &MO = MI->getOperand(oper);
863 // Opnd[0] Opnd[1] Opnd[2] Opnd[3] Opnd[4] Opnd[5] Opnd[6] Opnd[7]
864 // <Def0> <Def1> <Use0> <Use1> <ImpDef0> <ImpDef1> <ImpUse0> <ImpUse1>
868 // Predicate Operand here
869 if (MO.isReg() && !MO.isUse() && !MO.isImplicit()) {
873 MI->getOperand(oper+1).ChangeToRegister(MO.getReg(), MO.isDef(),
874 MO.isImplicit(), MO.isKill(),
875 MO.isDead(), MO.isUndef(),
878 else if (MO.isImm()) {
879 MI->getOperand(oper+1).ChangeToImmediate(MO.getImm());
881 else if (MO.isGlobal()) {
882 // MI can not have more than one GlobalAddress operand.
883 assert(hasGAOpnd == false && "MI can only have one GlobalAddress opnd");
885 // There is no member function called "ChangeToGlobalAddress" in the
886 // MachineOperand class (not like "ChangeToRegister" and
887 // "ChangeToImmediate"). So we have to remove them from Operands[] list
888 // first, and then add them back after we have inserted the predicate
889 // operand. tmpOpnds[] is to remember these operands before we remove
891 tmpOpnds.push_back(MO);
893 // Operands[oper] is a GlobalAddress operand;
894 // Operands[oper+1] has been copied into Operands[oper+2];
900 llvm_unreachable("Unexpected operand type");
905 int regPos = invertJump ? 1 : 0;
906 MachineOperand PredMO = Cond[regPos];
908 // [oper] now points to the last explicit Def. Predicate operand must be
909 // located at [oper+1]. See diagram above.
910 // This assumes that the predicate is always the first operand,
911 // i.e. Operands[0+numResults], in the set of inputs
912 // It is better to have an assert here to check this. But I don't know how
913 // to write this assert because findFirstPredOperandIdx() would return -1
914 if (oper < -1) oper = -1;
916 MI->getOperand(oper+1).ChangeToRegister(PredMO.getReg(), PredMO.isDef(),
917 PredMO.isImplicit(), false,
918 PredMO.isDead(), PredMO.isUndef(),
921 MachineRegisterInfo &RegInfo = MI->getParent()->getParent()->getRegInfo();
922 RegInfo.clearKillFlags(PredMO.getReg());
928 // Operands[GAIdx] is the original GlobalAddress operand, which is
929 // already copied into tmpOpnds[0].
930 // Operands[GAIdx] now stores a copy of Operands[GAIdx-1]
931 // Operands[GAIdx+1] has already been copied into Operands[GAIdx+2],
932 // so we start from [GAIdx+2]
933 for (i = GAIdx + 2; i < MI->getNumOperands(); ++i)
934 tmpOpnds.push_back(MI->getOperand(i));
936 // Remove all operands in range [ (GAIdx+1) ... (MI->getNumOperands()-1) ]
937 // It is very important that we always remove from the end of Operands[]
938 // MI->getNumOperands() is at least 2 if program goes to here.
939 for (i = MI->getNumOperands() - 1; i > GAIdx; --i)
940 MI->RemoveOperand(i);
942 for (i = 0; i < tmpOpnds.size(); ++i)
943 MI->addOperand(tmpOpnds[i]);
952 isProfitableToIfCvt(MachineBasicBlock &MBB,
954 unsigned ExtraPredCycles,
955 const BranchProbability &Probability) const {
962 isProfitableToIfCvt(MachineBasicBlock &TMBB,
964 unsigned ExtraTCycles,
965 MachineBasicBlock &FMBB,
967 unsigned ExtraFCycles,
968 const BranchProbability &Probability) const {
972 // Returns true if an instruction is predicated irrespective of the predicate
973 // sense. For example, all of the following will return true.
974 // if (p0) R1 = add(R2, R3)
975 // if (!p0) R1 = add(R2, R3)
976 // if (p0.new) R1 = add(R2, R3)
977 // if (!p0.new) R1 = add(R2, R3)
978 bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
979 const uint64_t F = MI->getDesc().TSFlags;
981 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
984 bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
985 const uint64_t F = get(Opcode).TSFlags;
987 return ((F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
990 bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr *MI) const {
991 const uint64_t F = MI->getDesc().TSFlags;
993 assert(isPredicated(MI));
994 return (!((F >> HexagonII::PredicatedFalsePos) &
995 HexagonII::PredicatedFalseMask));
998 bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
999 const uint64_t F = get(Opcode).TSFlags;
1001 // Make sure that the instruction is predicated.
1002 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
1003 return (!((F >> HexagonII::PredicatedFalsePos) &
1004 HexagonII::PredicatedFalseMask));
1007 bool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
1008 const uint64_t F = MI->getDesc().TSFlags;
1010 assert(isPredicated(MI));
1011 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1014 bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
1015 const uint64_t F = get(Opcode).TSFlags;
1017 assert(isPredicated(Opcode));
1018 return ((F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask);
1021 // Returns true, if a ST insn can be promoted to a new-value store.
1022 bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
1023 const uint64_t F = MI->getDesc().TSFlags;
1025 return ((F >> HexagonII::mayNVStorePos) &
1026 HexagonII::mayNVStoreMask);
1030 HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
1031 std::vector<MachineOperand> &Pred) const {
1032 for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
1033 MachineOperand MO = MI->getOperand(oper);
1034 if (MO.isReg() && MO.isDef()) {
1035 const TargetRegisterClass* RC = RI.getMinimalPhysRegClass(MO.getReg());
1036 if (RC == &Hexagon::PredRegsRegClass) {
1048 SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
1049 const SmallVectorImpl<MachineOperand> &Pred2) const {
1056 // We indicate that we want to reverse the branch by
1057 // inserting a 0 at the beginning of the Cond vector.
1059 bool HexagonInstrInfo::
1060 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
1061 if (!Cond.empty() && Cond[0].isImm() && Cond[0].getImm() == 0) {
1062 Cond.erase(Cond.begin());
1064 Cond.insert(Cond.begin(), MachineOperand::CreateImm(0));
1070 bool HexagonInstrInfo::
1071 isProfitableToDupForIfCvt(MachineBasicBlock &MBB,unsigned NumInstrs,
1072 const BranchProbability &Probability) const {
1073 return (NumInstrs <= 4);
1076 bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1077 switch (MI->getOpcode()) {
1078 default: return false;
1079 case Hexagon::L4_return:
1080 case Hexagon::L4_return_t:
1081 case Hexagon::L4_return_f:
1082 case Hexagon::L4_return_tnew_pnt:
1083 case Hexagon::L4_return_fnew_pnt:
1084 case Hexagon::L4_return_tnew_pt:
1085 case Hexagon::L4_return_fnew_pt:
1091 bool HexagonInstrInfo::
1092 isValidOffset(const int Opcode, const int Offset) const {
1093 // This function is to check whether the "Offset" is in the correct range of
1094 // the given "Opcode". If "Offset" is not in the correct range, "ADD_ri" is
1095 // inserted to calculate the final address. Due to this reason, the function
1096 // assumes that the "Offset" has correct alignment.
1097 // We used to assert if the offset was not properly aligned, however,
1098 // there are cases where a misaligned pointer recast can cause this
1099 // problem, and we need to allow for it. The front end warns of such
1100 // misaligns with respect to load size.
1104 case Hexagon::L2_loadri_io:
1105 case Hexagon::S2_storeri_io:
1106 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
1107 (Offset <= Hexagon_MEMW_OFFSET_MAX);
1109 case Hexagon::L2_loadrd_io:
1110 case Hexagon::S2_storerd_io:
1111 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
1112 (Offset <= Hexagon_MEMD_OFFSET_MAX);
1114 case Hexagon::L2_loadrh_io:
1115 case Hexagon::L2_loadruh_io:
1116 case Hexagon::S2_storerh_io:
1117 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
1118 (Offset <= Hexagon_MEMH_OFFSET_MAX);
1120 case Hexagon::L2_loadrb_io:
1121 case Hexagon::S2_storerb_io:
1122 case Hexagon::L2_loadrub_io:
1123 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
1124 (Offset <= Hexagon_MEMB_OFFSET_MAX);
1126 case Hexagon::A2_addi:
1127 case Hexagon::TFR_FI:
1128 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
1129 (Offset <= Hexagon_ADDI_OFFSET_MAX);
1131 case Hexagon::L4_iadd_memopw_io:
1132 case Hexagon::L4_isub_memopw_io:
1133 case Hexagon::L4_add_memopw_io:
1134 case Hexagon::L4_sub_memopw_io:
1135 case Hexagon::L4_and_memopw_io:
1136 case Hexagon::L4_or_memopw_io:
1137 return (0 <= Offset && Offset <= 255);
1139 case Hexagon::L4_iadd_memoph_io:
1140 case Hexagon::L4_isub_memoph_io:
1141 case Hexagon::L4_add_memoph_io:
1142 case Hexagon::L4_sub_memoph_io:
1143 case Hexagon::L4_and_memoph_io:
1144 case Hexagon::L4_or_memoph_io:
1145 return (0 <= Offset && Offset <= 127);
1147 case Hexagon::L4_iadd_memopb_io:
1148 case Hexagon::L4_isub_memopb_io:
1149 case Hexagon::L4_add_memopb_io:
1150 case Hexagon::L4_sub_memopb_io:
1151 case Hexagon::L4_and_memopb_io:
1152 case Hexagon::L4_or_memopb_io:
1153 return (0 <= Offset && Offset <= 63);
1155 // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
1156 // any size. Later pass knows how to handle it.
1157 case Hexagon::STriw_pred:
1158 case Hexagon::LDriw_pred:
1161 case Hexagon::J2_loop0i:
1162 return isUInt<10>(Offset);
1164 // INLINEASM is very special.
1165 case Hexagon::INLINEASM:
1169 llvm_unreachable("No offset range is defined for this opcode. "
1170 "Please define it in the above switch statement!");
1175 // Check if the Offset is a valid auto-inc imm by Load/Store Type.
1177 bool HexagonInstrInfo::
1178 isValidAutoIncImm(const EVT VT, const int Offset) const {
1180 if (VT == MVT::i64) {
1181 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
1182 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
1183 (Offset & 0x7) == 0);
1185 if (VT == MVT::i32) {
1186 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
1187 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
1188 (Offset & 0x3) == 0);
1190 if (VT == MVT::i16) {
1191 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
1192 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
1193 (Offset & 0x1) == 0);
1195 if (VT == MVT::i8) {
1196 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
1197 Offset <= Hexagon_MEMB_AUTOINC_MAX);
1199 llvm_unreachable("Not an auto-inc opc!");
1203 bool HexagonInstrInfo::
1204 isMemOp(const MachineInstr *MI) const {
1205 // return MI->getDesc().mayLoad() && MI->getDesc().mayStore();
1207 switch (MI->getOpcode())
1209 default: return false;
1210 case Hexagon::L4_iadd_memopw_io:
1211 case Hexagon::L4_isub_memopw_io:
1212 case Hexagon::L4_add_memopw_io:
1213 case Hexagon::L4_sub_memopw_io:
1214 case Hexagon::L4_and_memopw_io:
1215 case Hexagon::L4_or_memopw_io:
1216 case Hexagon::L4_iadd_memoph_io:
1217 case Hexagon::L4_isub_memoph_io:
1218 case Hexagon::L4_add_memoph_io:
1219 case Hexagon::L4_sub_memoph_io:
1220 case Hexagon::L4_and_memoph_io:
1221 case Hexagon::L4_or_memoph_io:
1222 case Hexagon::L4_iadd_memopb_io:
1223 case Hexagon::L4_isub_memopb_io:
1224 case Hexagon::L4_add_memopb_io:
1225 case Hexagon::L4_sub_memopb_io:
1226 case Hexagon::L4_and_memopb_io:
1227 case Hexagon::L4_or_memopb_io:
1228 case Hexagon::L4_ior_memopb_io:
1229 case Hexagon::L4_ior_memoph_io:
1230 case Hexagon::L4_ior_memopw_io:
1231 case Hexagon::L4_iand_memopb_io:
1232 case Hexagon::L4_iand_memoph_io:
1233 case Hexagon::L4_iand_memopw_io:
1240 bool HexagonInstrInfo::
1241 isSpillPredRegOp(const MachineInstr *MI) const {
1242 switch (MI->getOpcode()) {
1243 default: return false;
1244 case Hexagon::STriw_pred :
1245 case Hexagon::LDriw_pred :
1250 bool HexagonInstrInfo::isNewValueJumpCandidate(const MachineInstr *MI) const {
1251 switch (MI->getOpcode()) {
1252 default: return false;
1253 case Hexagon::C2_cmpeq:
1254 case Hexagon::C2_cmpeqi:
1255 case Hexagon::C2_cmpgt:
1256 case Hexagon::C2_cmpgti:
1257 case Hexagon::C2_cmpgtu:
1258 case Hexagon::C2_cmpgtui:
1263 bool HexagonInstrInfo::
1264 isConditionalTransfer (const MachineInstr *MI) const {
1265 switch (MI->getOpcode()) {
1266 default: return false;
1267 case Hexagon::A2_tfrt:
1268 case Hexagon::A2_tfrf:
1269 case Hexagon::C2_cmoveit:
1270 case Hexagon::C2_cmoveif:
1271 case Hexagon::A2_tfrtnew:
1272 case Hexagon::A2_tfrfnew:
1273 case Hexagon::C2_cmovenewit:
1274 case Hexagon::C2_cmovenewif:
1279 bool HexagonInstrInfo::isConditionalALU32 (const MachineInstr* MI) const {
1280 switch (MI->getOpcode())
1282 default: return false;
1283 case Hexagon::A2_paddf:
1284 case Hexagon::A2_paddfnew:
1285 case Hexagon::A2_paddt:
1286 case Hexagon::A2_paddtnew:
1287 case Hexagon::A2_pandf:
1288 case Hexagon::A2_pandfnew:
1289 case Hexagon::A2_pandt:
1290 case Hexagon::A2_pandtnew:
1291 case Hexagon::A4_paslhf:
1292 case Hexagon::A4_paslhfnew:
1293 case Hexagon::A4_paslht:
1294 case Hexagon::A4_paslhtnew:
1295 case Hexagon::A4_pasrhf:
1296 case Hexagon::A4_pasrhfnew:
1297 case Hexagon::A4_pasrht:
1298 case Hexagon::A4_pasrhtnew:
1299 case Hexagon::A2_porf:
1300 case Hexagon::A2_porfnew:
1301 case Hexagon::A2_port:
1302 case Hexagon::A2_portnew:
1303 case Hexagon::A2_psubf:
1304 case Hexagon::A2_psubfnew:
1305 case Hexagon::A2_psubt:
1306 case Hexagon::A2_psubtnew:
1307 case Hexagon::A2_pxorf:
1308 case Hexagon::A2_pxorfnew:
1309 case Hexagon::A2_pxort:
1310 case Hexagon::A2_pxortnew:
1311 case Hexagon::A4_psxthf:
1312 case Hexagon::A4_psxthfnew:
1313 case Hexagon::A4_psxtht:
1314 case Hexagon::A4_psxthtnew:
1315 case Hexagon::A4_psxtbf:
1316 case Hexagon::A4_psxtbfnew:
1317 case Hexagon::A4_psxtbt:
1318 case Hexagon::A4_psxtbtnew:
1319 case Hexagon::A4_pzxtbf:
1320 case Hexagon::A4_pzxtbfnew:
1321 case Hexagon::A4_pzxtbt:
1322 case Hexagon::A4_pzxtbtnew:
1323 case Hexagon::A4_pzxthf:
1324 case Hexagon::A4_pzxthfnew:
1325 case Hexagon::A4_pzxtht:
1326 case Hexagon::A4_pzxthtnew:
1327 case Hexagon::A2_paddit:
1328 case Hexagon::A2_paddif:
1329 case Hexagon::C2_ccombinewt:
1330 case Hexagon::C2_ccombinewf:
1335 bool HexagonInstrInfo::
1336 isConditionalLoad (const MachineInstr* MI) const {
1337 switch (MI->getOpcode())
1339 default: return false;
1340 case Hexagon::L2_ploadrdt_io :
1341 case Hexagon::L2_ploadrdf_io:
1342 case Hexagon::L2_ploadrit_io:
1343 case Hexagon::L2_ploadrif_io:
1344 case Hexagon::L2_ploadrht_io:
1345 case Hexagon::L2_ploadrhf_io:
1346 case Hexagon::L2_ploadrbt_io:
1347 case Hexagon::L2_ploadrbf_io:
1348 case Hexagon::L2_ploadruht_io:
1349 case Hexagon::L2_ploadruhf_io:
1350 case Hexagon::L2_ploadrubt_io:
1351 case Hexagon::L2_ploadrubf_io:
1352 case Hexagon::L2_ploadrdt_pi:
1353 case Hexagon::L2_ploadrdf_pi:
1354 case Hexagon::L2_ploadrit_pi:
1355 case Hexagon::L2_ploadrif_pi:
1356 case Hexagon::L2_ploadrht_pi:
1357 case Hexagon::L2_ploadrhf_pi:
1358 case Hexagon::L2_ploadrbt_pi:
1359 case Hexagon::L2_ploadrbf_pi:
1360 case Hexagon::L2_ploadruht_pi:
1361 case Hexagon::L2_ploadruhf_pi:
1362 case Hexagon::L2_ploadrubt_pi:
1363 case Hexagon::L2_ploadrubf_pi:
1364 case Hexagon::L4_ploadrdt_rr:
1365 case Hexagon::L4_ploadrdf_rr:
1366 case Hexagon::L4_ploadrbt_rr:
1367 case Hexagon::L4_ploadrbf_rr:
1368 case Hexagon::L4_ploadrubt_rr:
1369 case Hexagon::L4_ploadrubf_rr:
1370 case Hexagon::L4_ploadrht_rr:
1371 case Hexagon::L4_ploadrhf_rr:
1372 case Hexagon::L4_ploadruht_rr:
1373 case Hexagon::L4_ploadruhf_rr:
1374 case Hexagon::L4_ploadrit_rr:
1375 case Hexagon::L4_ploadrif_rr:
1380 // Returns true if an instruction is a conditional store.
1382 // Note: It doesn't include conditional new-value stores as they can't be
1383 // converted to .new predicate.
1385 // p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
1387 // / \ (not OK. it will cause new-value store to be
1388 // / X conditional on p0.new while R2 producer is
1391 // p.new store p.old NV store
1392 // [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
1398 // [if (p0)memw(R0+#0)=R2]
1400 // The above diagram shows the steps involoved in the conversion of a predicated
1401 // store instruction to its .new predicated new-value form.
1403 // The following set of instructions further explains the scenario where
1404 // conditional new-value store becomes invalid when promoted to .new predicate
1407 // { 1) if (p0) r0 = add(r1, r2)
1408 // 2) p0 = cmp.eq(r3, #0) }
1410 // 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
1411 // the first two instructions because in instr 1, r0 is conditional on old value
1412 // of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
1413 // is not valid for new-value stores.
1414 bool HexagonInstrInfo::
1415 isConditionalStore (const MachineInstr* MI) const {
1416 switch (MI->getOpcode())
1418 default: return false;
1419 case Hexagon::S4_storeirbt_io:
1420 case Hexagon::S4_storeirbf_io:
1421 case Hexagon::S4_pstorerbt_rr:
1422 case Hexagon::S4_pstorerbf_rr:
1423 case Hexagon::S2_pstorerbt_io:
1424 case Hexagon::S2_pstorerbf_io:
1425 case Hexagon::S2_pstorerbt_pi:
1426 case Hexagon::S2_pstorerbf_pi:
1427 case Hexagon::S2_pstorerdt_io:
1428 case Hexagon::S2_pstorerdf_io:
1429 case Hexagon::S4_pstorerdt_rr:
1430 case Hexagon::S4_pstorerdf_rr:
1431 case Hexagon::S2_pstorerdt_pi:
1432 case Hexagon::S2_pstorerdf_pi:
1433 case Hexagon::S2_pstorerht_io:
1434 case Hexagon::S2_pstorerhf_io:
1435 case Hexagon::S4_storeirht_io:
1436 case Hexagon::S4_storeirhf_io:
1437 case Hexagon::S4_pstorerht_rr:
1438 case Hexagon::S4_pstorerhf_rr:
1439 case Hexagon::S2_pstorerht_pi:
1440 case Hexagon::S2_pstorerhf_pi:
1441 case Hexagon::S2_pstorerit_io:
1442 case Hexagon::S2_pstorerif_io:
1443 case Hexagon::S4_storeirit_io:
1444 case Hexagon::S4_storeirif_io:
1445 case Hexagon::S4_pstorerit_rr:
1446 case Hexagon::S4_pstorerif_rr:
1447 case Hexagon::S2_pstorerit_pi:
1448 case Hexagon::S2_pstorerif_pi:
1450 // V4 global address store before promoting to dot new.
1451 case Hexagon::S4_pstorerdt_abs:
1452 case Hexagon::S4_pstorerdf_abs:
1453 case Hexagon::S4_pstorerbt_abs:
1454 case Hexagon::S4_pstorerbf_abs:
1455 case Hexagon::S4_pstorerht_abs:
1456 case Hexagon::S4_pstorerhf_abs:
1457 case Hexagon::S4_pstorerit_abs:
1458 case Hexagon::S4_pstorerif_abs:
1461 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1462 // from the "Conditional Store" list. Because a predicated new value store
1463 // would NOT be promoted to a double dot new store. See diagram below:
1464 // This function returns yes for those stores that are predicated but not
1465 // yet promoted to predicate dot new instructions.
1467 // +---------------------+
1468 // /-----| if (p0) memw(..)=r0 |---------\~
1469 // || +---------------------+ ||
1470 // promote || /\ /\ || promote
1472 // \||/ demote || \||/
1474 // +-------------------------+ || +-------------------------+
1475 // | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
1476 // +-------------------------+ || +-------------------------+
1479 // promote || \/ NOT possible
1483 // +-----------------------------+
1484 // | if (p0.new) memw(..)=r0.new |
1485 // +-----------------------------+
1486 // Double Dot New Store
1492 bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
1493 if (isNewValue(MI) && isBranch(MI))
1498 bool HexagonInstrInfo::isPostIncrement (const MachineInstr* MI) const {
1499 return (getAddrMode(MI) == HexagonII::PostInc);
1502 bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
1503 const uint64_t F = MI->getDesc().TSFlags;
1504 return ((F >> HexagonII::NewValuePos) & HexagonII::NewValueMask);
1507 // Returns true, if any one of the operands is a dot new
1508 // insn, whether it is predicated dot new or register dot new.
1509 bool HexagonInstrInfo::isDotNewInst (const MachineInstr* MI) const {
1510 return (isNewValueInst(MI) ||
1511 (isPredicated(MI) && isPredicatedNew(MI)));
1514 // Returns the most basic instruction for the .new predicated instructions and
1515 // new-value stores.
1516 // For example, all of the following instructions will be converted back to the
1517 // same instruction:
1518 // 1) if (p0.new) memw(R0+#0) = R1.new --->
1519 // 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
1520 // 3) if (p0.new) memw(R0+#0) = R1 --->
1523 int HexagonInstrInfo::GetDotOldOp(const int opc) const {
1525 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
1526 NewOp = Hexagon::getPredOldOpcode(NewOp);
1527 assert(NewOp >= 0 &&
1528 "Couldn't change predicate new instruction to its old form.");
1531 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
1532 NewOp = Hexagon::getNonNVStore(NewOp);
1533 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
1538 // Return the new value instruction for a given store.
1539 int HexagonInstrInfo::GetDotNewOp(const MachineInstr* MI) const {
1540 int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
1541 if (NVOpcode >= 0) // Valid new-value store instruction.
1544 switch (MI->getOpcode()) {
1545 default: llvm_unreachable("Unknown .new type");
1546 // store new value byte
1547 case Hexagon::S4_storerb_ur:
1548 return Hexagon::S4_storerbnew_ur;
1550 case Hexagon::S4_storerh_ur:
1551 return Hexagon::S4_storerhnew_ur;
1553 case Hexagon::S4_storeri_ur:
1554 return Hexagon::S4_storerinew_ur;
1560 // Return .new predicate version for an instruction.
1561 int HexagonInstrInfo::GetDotNewPredOp(MachineInstr *MI,
1562 const MachineBranchProbabilityInfo
1565 int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1566 if (NewOpcode >= 0) // Valid predicate new instruction
1569 switch (MI->getOpcode()) {
1570 default: llvm_unreachable("Unknown .new type");
1572 case Hexagon::J2_jumpt:
1573 case Hexagon::J2_jumpf:
1574 return getDotNewPredJumpOp(MI, MBPI);
1576 case Hexagon::J2_jumprt:
1577 return Hexagon::J2_jumptnewpt;
1579 case Hexagon::J2_jumprf:
1580 return Hexagon::J2_jumprfnewpt;
1582 case Hexagon::JMPrett:
1583 return Hexagon::J2_jumprtnewpt;
1585 case Hexagon::JMPretf:
1586 return Hexagon::J2_jumprfnewpt;
1589 // Conditional combine
1590 case Hexagon::C2_ccombinewt:
1591 return Hexagon::C2_ccombinewnewt;
1592 case Hexagon::C2_ccombinewf:
1593 return Hexagon::C2_ccombinewnewf;
1598 unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
1599 const uint64_t F = MI->getDesc().TSFlags;
1601 return((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
1604 /// immediateExtend - Changes the instruction in place to one using an immediate
1606 void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
1607 assert((isExtendable(MI)||isConstExtended(MI)) &&
1608 "Instruction must be extendable");
1609 // Find which operand is extendable.
1610 short ExtOpNum = getCExtOpNum(MI);
1611 MachineOperand &MO = MI->getOperand(ExtOpNum);
1612 // This needs to be something we understand.
1613 assert((MO.isMBB() || MO.isImm()) &&
1614 "Branch with unknown extendable field type");
1615 // Mark given operand as extended.
1616 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
1619 DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1620 const TargetSubtargetInfo &STI) const {
1621 const InstrItineraryData *II = STI.getInstrItineraryData();
1622 return static_cast<const HexagonSubtarget &>(STI).createDFAPacketizer(II);
1625 bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1626 const MachineBasicBlock *MBB,
1627 const MachineFunction &MF) const {
1628 // Debug info is never a scheduling boundary. It's necessary to be explicit
1629 // due to the special treatment of IT instructions below, otherwise a
1630 // dbg_value followed by an IT will result in the IT instruction being
1631 // considered a scheduling hazard, which is wrong. It should be the actual
1632 // instruction preceding the dbg_value instruction(s), just like it is
1633 // when debug info is not present.
1634 if (MI->isDebugValue())
1637 // Terminators and labels can't be scheduled around.
1638 if (MI->getDesc().isTerminator() || MI->isPosition() || MI->isInlineAsm())
1644 bool HexagonInstrInfo::isConstExtended(MachineInstr *MI) const {
1645 const uint64_t F = MI->getDesc().TSFlags;
1646 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1647 if (isExtended) // Instruction must be extended.
1650 unsigned isExtendable = (F >> HexagonII::ExtendablePos)
1651 & HexagonII::ExtendableMask;
1655 short ExtOpNum = getCExtOpNum(MI);
1656 const MachineOperand &MO = MI->getOperand(ExtOpNum);
1657 // Use MO operand flags to determine if MO
1658 // has the HMOTF_ConstExtended flag set.
1659 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1661 // If this is a Machine BB address we are talking about, and it is
1662 // not marked as extended, say so.
1666 // We could be using an instruction with an extendable immediate and shoehorn
1667 // a global address into it. If it is a global address it will be constant
1668 // extended. We do this for COMBINE.
1669 // We currently only handle isGlobal() because it is the only kind of
1670 // object we are going to end up with here for now.
1671 // In the future we probably should add isSymbol(), etc.
1672 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress())
1675 // If the extendable operand is not 'Immediate' type, the instruction should
1676 // have 'isExtended' flag set.
1677 assert(MO.isImm() && "Extendable operand must be Immediate type");
1679 int MinValue = getMinValue(MI);
1680 int MaxValue = getMaxValue(MI);
1681 int ImmValue = MO.getImm();
1683 return (ImmValue < MinValue || ImmValue > MaxValue);
1686 // Returns the opcode to use when converting MI, which is a conditional jump,
1687 // into a conditional instruction which uses the .new value of the predicate.
1688 // We also use branch probabilities to add a hint to the jump.
1690 HexagonInstrInfo::getDotNewPredJumpOp(MachineInstr *MI,
1692 MachineBranchProbabilityInfo *MBPI) const {
1694 // We assume that block can have at most two successors.
1696 MachineBasicBlock *Src = MI->getParent();
1697 MachineOperand *BrTarget = &MI->getOperand(1);
1698 MachineBasicBlock *Dst = BrTarget->getMBB();
1700 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
1701 if (Prediction >= BranchProbability(1,2))
1704 switch (MI->getOpcode()) {
1705 case Hexagon::J2_jumpt:
1706 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
1707 case Hexagon::J2_jumpf:
1708 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
1711 llvm_unreachable("Unexpected jump instruction.");
1714 // Returns true if a particular operand is extendable for an instruction.
1715 bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
1716 unsigned short OperandNum) const {
1717 const uint64_t F = MI->getDesc().TSFlags;
1719 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
1723 // Returns Operand Index for the constant extended instruction.
1724 unsigned short HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
1725 const uint64_t F = MI->getDesc().TSFlags;
1726 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask);
1729 // Returns the min value that doesn't need to be extended.
1730 int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
1731 const uint64_t F = MI->getDesc().TSFlags;
1732 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1733 & HexagonII::ExtentSignedMask;
1734 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1735 & HexagonII::ExtentBitsMask;
1737 if (isSigned) // if value is signed
1738 return -1U << (bits - 1);
1743 // Returns the max value that doesn't need to be extended.
1744 int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
1745 const uint64_t F = MI->getDesc().TSFlags;
1746 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
1747 & HexagonII::ExtentSignedMask;
1748 unsigned bits = (F >> HexagonII::ExtentBitsPos)
1749 & HexagonII::ExtentBitsMask;
1751 if (isSigned) // if value is signed
1752 return ~(-1U << (bits - 1));
1754 return ~(-1U << bits);
1757 // Returns true if an instruction can be converted into a non-extended
1758 // equivalent instruction.
1759 bool HexagonInstrInfo::NonExtEquivalentExists (const MachineInstr *MI) const {
1762 // Check if the instruction has a register form that uses register in place
1763 // of the extended operand, if so return that as the non-extended form.
1764 if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
1767 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
1768 // Check addressing mode and retrieve non-ext equivalent instruction.
1770 switch (getAddrMode(MI)) {
1771 case HexagonII::Absolute :
1772 // Load/store with absolute addressing mode can be converted into
1773 // base+offset mode.
1774 NonExtOpcode = Hexagon::getBasedWithImmOffset(MI->getOpcode());
1776 case HexagonII::BaseImmOffset :
1777 // Load/store with base+offset addressing mode can be converted into
1778 // base+register offset addressing mode. However left shift operand should
1780 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
1785 if (NonExtOpcode < 0)
1792 // Returns opcode of the non-extended equivalent instruction.
1793 short HexagonInstrInfo::getNonExtOpcode (const MachineInstr *MI) const {
1795 // Check if the instruction has a register form that uses register in place
1796 // of the extended operand, if so return that as the non-extended form.
1797 short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
1798 if (NonExtOpcode >= 0)
1799 return NonExtOpcode;
1801 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
1802 // Check addressing mode and retrieve non-ext equivalent instruction.
1803 switch (getAddrMode(MI)) {
1804 case HexagonII::Absolute :
1805 return Hexagon::getBasedWithImmOffset(MI->getOpcode());
1806 case HexagonII::BaseImmOffset :
1807 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
1815 bool HexagonInstrInfo::PredOpcodeHasJMP_c(Opcode_t Opcode) const {
1816 return (Opcode == Hexagon::J2_jumpt) ||
1817 (Opcode == Hexagon::J2_jumpf) ||
1818 (Opcode == Hexagon::J2_jumptnewpt) ||
1819 (Opcode == Hexagon::J2_jumpfnewpt) ||
1820 (Opcode == Hexagon::J2_jumpt) ||
1821 (Opcode == Hexagon::J2_jumpf);
1824 bool HexagonInstrInfo::PredOpcodeHasNot(Opcode_t Opcode) const {
1825 return (Opcode == Hexagon::J2_jumpf) ||
1826 (Opcode == Hexagon::J2_jumpfnewpt) ||
1827 (Opcode == Hexagon::J2_jumpfnew);