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/MC/MCAsmInfo.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/MathExtras.h"
29 #include "llvm/Support/raw_ostream.h"
34 #define DEBUG_TYPE "hexagon-instrinfo"
36 #define GET_INSTRINFO_CTOR_DTOR
37 #define GET_INSTRMAP_INFO
38 #include "HexagonGenInstrInfo.inc"
39 #include "HexagonGenDFAPacketizer.inc"
43 static cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
44 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
45 "packetization boundary."));
47 static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
48 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
50 static cl::opt<bool> EnableTimingClassLatency(
51 "enable-timing-class-latency", cl::Hidden, cl::init(false),
52 cl::desc("Enable timing class latency"));
54 static cl::opt<bool> EnableALUForwarding(
55 "enable-alu-forwarding", cl::Hidden, cl::init(true),
56 cl::desc("Enable vec alu forwarding"));
58 static cl::opt<bool> EnableACCForwarding(
59 "enable-acc-forwarding", cl::Hidden, cl::init(true),
60 cl::desc("Enable vec acc forwarding"));
62 static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
63 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
66 /// Constants for Hexagon instructions.
68 const int Hexagon_MEMV_OFFSET_MAX_128B = 2047; // #s7
69 const int Hexagon_MEMV_OFFSET_MIN_128B = -2048; // #s7
70 const int Hexagon_MEMV_OFFSET_MAX = 1023; // #s6
71 const int Hexagon_MEMV_OFFSET_MIN = -1024; // #s6
72 const int Hexagon_MEMW_OFFSET_MAX = 4095;
73 const int Hexagon_MEMW_OFFSET_MIN = -4096;
74 const int Hexagon_MEMD_OFFSET_MAX = 8191;
75 const int Hexagon_MEMD_OFFSET_MIN = -8192;
76 const int Hexagon_MEMH_OFFSET_MAX = 2047;
77 const int Hexagon_MEMH_OFFSET_MIN = -2048;
78 const int Hexagon_MEMB_OFFSET_MAX = 1023;
79 const int Hexagon_MEMB_OFFSET_MIN = -1024;
80 const int Hexagon_ADDI_OFFSET_MAX = 32767;
81 const int Hexagon_ADDI_OFFSET_MIN = -32768;
82 const int Hexagon_MEMD_AUTOINC_MAX = 56;
83 const int Hexagon_MEMD_AUTOINC_MIN = -64;
84 const int Hexagon_MEMW_AUTOINC_MAX = 28;
85 const int Hexagon_MEMW_AUTOINC_MIN = -32;
86 const int Hexagon_MEMH_AUTOINC_MAX = 14;
87 const int Hexagon_MEMH_AUTOINC_MIN = -16;
88 const int Hexagon_MEMB_AUTOINC_MAX = 7;
89 const int Hexagon_MEMB_AUTOINC_MIN = -8;
90 const int Hexagon_MEMV_AUTOINC_MAX = 192;
91 const int Hexagon_MEMV_AUTOINC_MIN = -256;
92 const int Hexagon_MEMV_AUTOINC_MAX_128B = 384;
93 const int Hexagon_MEMV_AUTOINC_MIN_128B = -512;
95 // Pin the vtable to this file.
96 void HexagonInstrInfo::anchor() {}
98 HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
99 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
103 static bool isIntRegForSubInst(unsigned Reg) {
104 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
105 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
109 static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
110 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::subreg_loreg)) &&
111 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::subreg_hireg));
115 /// Calculate number of instructions excluding the debug instructions.
116 static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
117 MachineBasicBlock::const_instr_iterator MIE) {
119 for (; MIB != MIE; ++MIB) {
120 if (!MIB->isDebugValue())
127 /// Find the hardware loop instruction used to set-up the specified loop.
128 /// On Hexagon, we have two instructions used to set-up the hardware loop
129 /// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
130 /// to indicate the end of a loop.
131 static MachineInstr *findLoopInstr(MachineBasicBlock *BB, int EndLoopOp,
132 SmallPtrSet<MachineBasicBlock *, 8> &Visited) {
135 if (EndLoopOp == Hexagon::ENDLOOP0) {
136 LOOPi = Hexagon::J2_loop0i;
137 LOOPr = Hexagon::J2_loop0r;
138 } else { // EndLoopOp == Hexagon::EndLOOP1
139 LOOPi = Hexagon::J2_loop1i;
140 LOOPr = Hexagon::J2_loop1r;
143 // The loop set-up instruction will be in a predecessor block
144 for (MachineBasicBlock::pred_iterator PB = BB->pred_begin(),
145 PE = BB->pred_end(); PB != PE; ++PB) {
146 // If this has been visited, already skip it.
147 if (!Visited.insert(*PB).second)
151 for (MachineBasicBlock::reverse_instr_iterator I = (*PB)->instr_rbegin(),
152 E = (*PB)->instr_rend(); I != E; ++I) {
153 int Opc = I->getOpcode();
154 if (Opc == LOOPi || Opc == LOOPr)
156 // We've reached a different loop, which means the loop0 has been removed.
157 if (Opc == EndLoopOp)
160 // Check the predecessors for the LOOP instruction.
161 MachineInstr *loop = findLoopInstr(*PB, EndLoopOp, Visited);
169 /// Gather register def/uses from MI.
170 /// This treats possible (predicated) defs as actually happening ones
171 /// (conservatively).
172 static inline void parseOperands(const MachineInstr *MI,
173 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
177 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
178 const MachineOperand &MO = MI->getOperand(i);
183 unsigned Reg = MO.getReg();
188 Uses.push_back(MO.getReg());
191 Defs.push_back(MO.getReg());
196 // Position dependent, so check twice for swap.
197 static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
199 case HexagonII::HSIG_None:
202 case HexagonII::HSIG_L1:
203 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
204 case HexagonII::HSIG_L2:
205 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
206 Gb == HexagonII::HSIG_A);
207 case HexagonII::HSIG_S1:
208 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
209 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
210 case HexagonII::HSIG_S2:
211 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
212 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
213 Gb == HexagonII::HSIG_A);
214 case HexagonII::HSIG_A:
215 return (Gb == HexagonII::HSIG_A);
216 case HexagonII::HSIG_Compound:
217 return (Gb == HexagonII::HSIG_Compound);
224 /// isLoadFromStackSlot - If the specified machine instruction is a direct
225 /// load from a stack slot, return the virtual or physical register number of
226 /// the destination along with the FrameIndex of the loaded stack slot. If
227 /// not, return 0. This predicate must return 0 if the instruction has
228 /// any side effects other than loading from the stack slot.
229 unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
230 int &FrameIndex) const {
231 switch (MI->getOpcode()) {
233 case Hexagon::L2_loadri_io:
234 case Hexagon::L2_loadrd_io:
235 case Hexagon::L2_loadrh_io:
236 case Hexagon::L2_loadrb_io:
237 case Hexagon::L2_loadrub_io:
238 if (MI->getOperand(2).isFI() &&
239 MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
240 FrameIndex = MI->getOperand(2).getIndex();
241 return MI->getOperand(0).getReg();
249 /// isStoreToStackSlot - If the specified machine instruction is a direct
250 /// store to a stack slot, return the virtual or physical register number of
251 /// the source reg along with the FrameIndex of the loaded stack slot. If
252 /// not, return 0. This predicate must return 0 if the instruction has
253 /// any side effects other than storing to the stack slot.
254 unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
255 int &FrameIndex) const {
256 switch (MI->getOpcode()) {
258 case Hexagon::S2_storeri_io:
259 case Hexagon::S2_storerd_io:
260 case Hexagon::S2_storerh_io:
261 case Hexagon::S2_storerb_io:
262 if (MI->getOperand(2).isFI() &&
263 MI->getOperand(1).isImm() && (MI->getOperand(1).getImm() == 0)) {
264 FrameIndex = MI->getOperand(0).getIndex();
265 return MI->getOperand(2).getReg();
273 /// This function can analyze one/two way branching only and should (mostly) be
274 /// called by target independent side.
275 /// First entry is always the opcode of the branching instruction, except when
276 /// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
277 /// BB with only unconditional jump. Subsequent entries depend upon the opcode,
278 /// e.g. Jump_c p will have
282 /// Cond[0] = ENDLOOP
285 /// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
289 bool HexagonInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
290 MachineBasicBlock *&TBB,
291 MachineBasicBlock *&FBB,
292 SmallVectorImpl<MachineOperand> &Cond,
293 bool AllowModify) const {
298 // If the block has no terminators, it just falls into the block after it.
299 MachineBasicBlock::instr_iterator I = MBB.instr_end();
300 if (I == MBB.instr_begin())
303 // A basic block may looks like this:
313 // It has two succs but does not have a terminator
314 // Don't know how to handle it.
318 // Don't analyze EH branches.
320 } while (I != MBB.instr_begin());
325 while (I->isDebugValue()) {
326 if (I == MBB.instr_begin())
331 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
332 I->getOperand(0).isMBB();
333 // Delete the J2_jump if it's equivalent to a fall-through.
334 if (AllowModify && JumpToBlock &&
335 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
336 DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
337 I->eraseFromParent();
339 if (I == MBB.instr_begin())
343 if (!isUnpredicatedTerminator(&*I))
346 // Get the last instruction in the block.
347 MachineInstr *LastInst = &*I;
348 MachineInstr *SecondLastInst = nullptr;
349 // Find one more terminator if present.
351 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(&*I)) {
353 SecondLastInst = &*I;
355 // This is a third branch.
358 if (I == MBB.instr_begin())
363 int LastOpcode = LastInst->getOpcode();
364 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
365 // If the branch target is not a basic block, it could be a tail call.
366 // (It is, if the target is a function.)
367 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
369 if (SecLastOpcode == Hexagon::J2_jump &&
370 !SecondLastInst->getOperand(0).isMBB())
373 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
374 bool LastOpcodeHasNVJump = isNewValueJump(LastInst);
376 // If there is only one terminator instruction, process it.
377 if (LastInst && !SecondLastInst) {
378 if (LastOpcode == Hexagon::J2_jump) {
379 TBB = LastInst->getOperand(0).getMBB();
382 if (isEndLoopN(LastOpcode)) {
383 TBB = LastInst->getOperand(0).getMBB();
384 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
385 Cond.push_back(LastInst->getOperand(0));
388 if (LastOpcodeHasJMP_c) {
389 TBB = LastInst->getOperand(1).getMBB();
390 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
391 Cond.push_back(LastInst->getOperand(0));
394 // Only supporting rr/ri versions of new-value jumps.
395 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
396 TBB = LastInst->getOperand(2).getMBB();
397 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
398 Cond.push_back(LastInst->getOperand(0));
399 Cond.push_back(LastInst->getOperand(1));
402 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
403 << " with one jump\n";);
404 // Otherwise, don't know what this is.
408 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
409 bool SecLastOpcodeHasNVJump = isNewValueJump(SecondLastInst);
410 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
411 TBB = SecondLastInst->getOperand(1).getMBB();
412 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
413 Cond.push_back(SecondLastInst->getOperand(0));
414 FBB = LastInst->getOperand(0).getMBB();
418 // Only supporting rr/ri versions of new-value jumps.
419 if (SecLastOpcodeHasNVJump &&
420 (SecondLastInst->getNumExplicitOperands() == 3) &&
421 (LastOpcode == Hexagon::J2_jump)) {
422 TBB = SecondLastInst->getOperand(2).getMBB();
423 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
424 Cond.push_back(SecondLastInst->getOperand(0));
425 Cond.push_back(SecondLastInst->getOperand(1));
426 FBB = LastInst->getOperand(0).getMBB();
430 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
431 // executed, so remove it.
432 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
433 TBB = SecondLastInst->getOperand(0).getMBB();
434 I = LastInst->getIterator();
436 I->eraseFromParent();
440 // If the block ends with an ENDLOOP, and J2_jump, handle it.
441 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
442 TBB = SecondLastInst->getOperand(0).getMBB();
443 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
444 Cond.push_back(SecondLastInst->getOperand(0));
445 FBB = LastInst->getOperand(0).getMBB();
448 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
449 << " with two jumps";);
450 // Otherwise, can't handle this.
455 unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
456 DEBUG(dbgs() << "\nRemoving branches out of BB#" << MBB.getNumber());
457 MachineBasicBlock::iterator I = MBB.end();
459 while (I != MBB.begin()) {
461 if (I->isDebugValue())
463 // Only removing branches from end of MBB.
466 if (Count && (I->getOpcode() == Hexagon::J2_jump))
467 llvm_unreachable("Malformed basic block: unconditional branch not last");
468 MBB.erase(&MBB.back());
476 unsigned HexagonInstrInfo::InsertBranch(MachineBasicBlock &MBB,
477 MachineBasicBlock *TBB, MachineBasicBlock *FBB,
478 ArrayRef<MachineOperand> Cond, DebugLoc DL) const {
479 unsigned BOpc = Hexagon::J2_jump;
480 unsigned BccOpc = Hexagon::J2_jumpt;
481 assert(validateBranchCond(Cond) && "Invalid branching condition");
482 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
484 // Check if ReverseBranchCondition has asked to reverse this branch
485 // If we want to reverse the branch an odd number of times, we want
487 if (!Cond.empty() && Cond[0].isImm())
488 BccOpc = Cond[0].getImm();
492 // Due to a bug in TailMerging/CFG Optimization, we need to add a
493 // special case handling of a predicated jump followed by an
494 // unconditional jump. If not, Tail Merging and CFG Optimization go
495 // into an infinite loop.
496 MachineBasicBlock *NewTBB, *NewFBB;
497 SmallVector<MachineOperand, 4> Cond;
498 MachineInstr *Term = MBB.getFirstTerminator();
499 if (Term != MBB.end() && isPredicated(Term) &&
500 !AnalyzeBranch(MBB, NewTBB, NewFBB, Cond, false)) {
501 MachineBasicBlock *NextBB = &*++MBB.getIterator();
502 if (NewTBB == NextBB) {
503 ReverseBranchCondition(Cond);
505 return InsertBranch(MBB, TBB, nullptr, Cond, DL);
508 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
509 } else if (isEndLoopN(Cond[0].getImm())) {
510 int EndLoopOp = Cond[0].getImm();
511 assert(Cond[1].isMBB());
512 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
513 // Check for it, and change the BB target if needed.
514 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
515 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
516 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
517 Loop->getOperand(0).setMBB(TBB);
518 // Add the ENDLOOP after the finding the LOOP0.
519 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
520 } else if (isNewValueJump(Cond[0].getImm())) {
521 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
523 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
524 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
525 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
526 DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
527 if (Cond[2].isReg()) {
528 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
529 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
530 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
531 } else if(Cond[2].isImm()) {
532 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
533 addImm(Cond[2].getImm()).addMBB(TBB);
535 llvm_unreachable("Invalid condition for branching");
537 assert((Cond.size() == 2) && "Malformed cond vector");
538 const MachineOperand &RO = Cond[1];
539 unsigned Flags = getUndefRegState(RO.isUndef());
540 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
544 assert((!Cond.empty()) &&
545 "Cond. cannot be empty when multiple branchings are required");
546 assert((!isNewValueJump(Cond[0].getImm())) &&
547 "NV-jump cannot be inserted with another branch");
548 // Special case for hardware loops. The condition is a basic block.
549 if (isEndLoopN(Cond[0].getImm())) {
550 int EndLoopOp = Cond[0].getImm();
551 assert(Cond[1].isMBB());
552 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
553 // Check for it, and change the BB target if needed.
554 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
555 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
556 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
557 Loop->getOperand(0).setMBB(TBB);
558 // Add the ENDLOOP after the finding the LOOP0.
559 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
561 const MachineOperand &RO = Cond[1];
562 unsigned Flags = getUndefRegState(RO.isUndef());
563 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
565 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
571 bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
572 unsigned NumCycles, unsigned ExtraPredCycles,
573 BranchProbability Probability) const {
574 return nonDbgBBSize(&MBB) <= 3;
578 bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
579 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
580 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
582 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
586 bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
587 unsigned NumInstrs, BranchProbability Probability) const {
588 return NumInstrs <= 4;
592 void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
593 MachineBasicBlock::iterator I, DebugLoc DL, unsigned DestReg,
594 unsigned SrcReg, bool KillSrc) const {
595 auto &HRI = getRegisterInfo();
596 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
597 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg).addReg(SrcReg);
600 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
601 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg).addReg(SrcReg);
604 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
605 // Map Pd = Ps to Pd = or(Ps, Ps).
606 BuildMI(MBB, I, DL, get(Hexagon::C2_or),
607 DestReg).addReg(SrcReg).addReg(SrcReg);
610 if (Hexagon::DoubleRegsRegClass.contains(DestReg) &&
611 Hexagon::IntRegsRegClass.contains(SrcReg)) {
612 // We can have an overlap between single and double reg: r1:0 = r0.
613 if(SrcReg == RI.getSubReg(DestReg, Hexagon::subreg_loreg)) {
615 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
616 Hexagon::subreg_hireg))).addImm(0);
618 // r1:0 = r1 or no overlap.
619 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), (RI.getSubReg(DestReg,
620 Hexagon::subreg_loreg))).addReg(SrcReg);
621 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrsi), (RI.getSubReg(DestReg,
622 Hexagon::subreg_hireg))).addImm(0);
626 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
627 Hexagon::IntRegsRegClass.contains(SrcReg)) {
628 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg).addReg(SrcReg);
631 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
632 Hexagon::IntRegsRegClass.contains(DestReg)) {
633 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg).
634 addReg(SrcReg, getKillRegState(KillSrc));
637 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
638 Hexagon::PredRegsRegClass.contains(DestReg)) {
639 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg).
640 addReg(SrcReg, getKillRegState(KillSrc));
643 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
644 Hexagon::IntRegsRegClass.contains(DestReg)) {
645 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg).
646 addReg(SrcReg, getKillRegState(KillSrc));
649 if (Hexagon::VectorRegsRegClass.contains(SrcReg, DestReg)) {
650 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
651 addReg(SrcReg, getKillRegState(KillSrc));
654 if (Hexagon::VecDblRegsRegClass.contains(SrcReg, DestReg)) {
655 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg).
656 addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg),
657 getKillRegState(KillSrc)).
658 addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg),
659 getKillRegState(KillSrc));
662 if (Hexagon::VecPredRegsRegClass.contains(SrcReg, DestReg)) {
663 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg).
665 addReg(SrcReg, getKillRegState(KillSrc));
668 if (Hexagon::VecPredRegsRegClass.contains(SrcReg) &&
669 Hexagon::VectorRegsRegClass.contains(DestReg)) {
670 llvm_unreachable("Unimplemented pred to vec");
673 if (Hexagon::VecPredRegsRegClass.contains(DestReg) &&
674 Hexagon::VectorRegsRegClass.contains(SrcReg)) {
675 llvm_unreachable("Unimplemented vec to pred");
678 if (Hexagon::VecPredRegs128BRegClass.contains(SrcReg, DestReg)) {
679 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and),
680 HRI.getSubReg(DestReg, Hexagon::subreg_hireg)).
681 addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg),
682 getKillRegState(KillSrc));
683 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and),
684 HRI.getSubReg(DestReg, Hexagon::subreg_loreg)).
685 addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg),
686 getKillRegState(KillSrc));
691 // Show the invalid registers to ease debugging.
692 dbgs() << "Invalid registers for copy in BB#" << MBB.getNumber()
693 << ": " << PrintReg(DestReg, &HRI)
694 << " = " << PrintReg(SrcReg, &HRI) << '\n';
696 llvm_unreachable("Unimplemented");
700 void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
701 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
702 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
703 DebugLoc DL = MBB.findDebugLoc(I);
704 MachineFunction &MF = *MBB.getParent();
705 MachineFrameInfo &MFI = *MF.getFrameInfo();
706 unsigned Align = MFI.getObjectAlignment(FI);
708 MachineMemOperand *MMO = MF.getMachineMemOperand(
709 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
710 MFI.getObjectSize(FI), Align);
712 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
713 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
714 .addFrameIndex(FI).addImm(0)
715 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
716 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
717 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
718 .addFrameIndex(FI).addImm(0)
719 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
720 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
721 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
722 .addFrameIndex(FI).addImm(0)
723 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
725 llvm_unreachable("Unimplemented");
730 void HexagonInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
731 MachineBasicBlock::iterator I, unsigned DestReg, int FI,
732 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
733 DebugLoc DL = MBB.findDebugLoc(I);
734 MachineFunction &MF = *MBB.getParent();
735 MachineFrameInfo &MFI = *MF.getFrameInfo();
736 unsigned Align = MFI.getObjectAlignment(FI);
738 MachineMemOperand *MMO = MF.getMachineMemOperand(
739 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
740 MFI.getObjectSize(FI), Align);
741 if (RC == &Hexagon::IntRegsRegClass) {
742 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
743 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
744 } else if (RC == &Hexagon::DoubleRegsRegClass) {
745 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
746 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
747 } else if (RC == &Hexagon::PredRegsRegClass) {
748 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
749 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
751 llvm_unreachable("Can't store this register to stack slot");
756 /// expandPostRAPseudo - This function is called for all pseudo instructions
757 /// that remain after register allocation. Many pseudo instructions are
758 /// created to help register allocation. This is the place to convert them
759 /// into real instructions. The target can edit MI in place, or it can insert
760 /// new instructions and erase MI. The function should return true if
761 /// anything was changed.
762 bool HexagonInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI)
764 const HexagonRegisterInfo &HRI = getRegisterInfo();
765 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
766 MachineBasicBlock &MBB = *MI->getParent();
767 DebugLoc DL = MI->getDebugLoc();
768 unsigned Opc = MI->getOpcode();
769 const unsigned VecOffset = 1;
773 case Hexagon::ALIGNA:
774 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI->getOperand(0).getReg())
775 .addReg(HRI.getFrameRegister())
776 .addImm(-MI->getOperand(1).getImm());
779 case Hexagon::STrivv_indexed_128B:
781 case Hexagon::STrivv_indexed: {
782 unsigned SrcReg = MI->getOperand(2).getReg();
783 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::subreg_hireg);
784 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::subreg_loreg);
785 unsigned NewOpcd = Is128B ? Hexagon::V6_vS32b_ai_128B
786 : Hexagon::V6_vS32b_ai;
787 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
788 MachineInstr *MI1New = BuildMI(MBB, MI, DL, get(NewOpcd))
789 .addOperand(MI->getOperand(0))
790 .addImm(MI->getOperand(1).getImm())
792 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
793 MI1New->getOperand(0).setIsKill(false);
794 BuildMI(MBB, MI, DL, get(NewOpcd))
795 .addOperand(MI->getOperand(0))
796 // The Vectors are indexed in multiples of vector size.
797 .addImm(MI->getOperand(1).getImm()+Offset)
799 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
803 case Hexagon::LDrivv_pseudo_V6_128B:
804 case Hexagon::LDrivv_indexed_128B:
806 case Hexagon::LDrivv_pseudo_V6:
807 case Hexagon::LDrivv_indexed: {
808 unsigned NewOpcd = Is128B ? Hexagon::V6_vL32b_ai_128B
809 : Hexagon::V6_vL32b_ai;
810 unsigned DstReg = MI->getOperand(0).getReg();
811 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
812 MachineInstr *MI1New =
813 BuildMI(MBB, MI, DL, get(NewOpcd),
814 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
815 .addOperand(MI->getOperand(1))
816 .addImm(MI->getOperand(2).getImm());
817 MI1New->getOperand(1).setIsKill(false);
818 BuildMI(MBB, MI, DL, get(NewOpcd),
819 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
820 .addOperand(MI->getOperand(1))
821 // The Vectors are indexed in multiples of vector size.
822 .addImm(MI->getOperand(2).getImm() + Offset)
823 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
827 case Hexagon::LDriv_pseudo_V6_128B:
829 case Hexagon::LDriv_pseudo_V6: {
830 unsigned DstReg = MI->getOperand(0).getReg();
831 unsigned NewOpc = Is128B ? Hexagon::V6_vL32b_ai_128B
832 : Hexagon::V6_vL32b_ai;
833 int32_t Off = MI->getOperand(2).getImm();
835 BuildMI(MBB, MI, DL, get(NewOpc), DstReg)
836 .addOperand(MI->getOperand(1))
838 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
842 case Hexagon::STriv_pseudo_V6_128B:
844 case Hexagon::STriv_pseudo_V6: {
845 unsigned NewOpc = Is128B ? Hexagon::V6_vS32b_ai_128B
846 : Hexagon::V6_vS32b_ai;
847 int32_t Off = MI->getOperand(1).getImm();
848 int32_t Idx = Is128B ? (Off >> 7) : (Off >> 6);
849 BuildMI(MBB, MI, DL, get(NewOpc))
850 .addOperand(MI->getOperand(0))
852 .addOperand(MI->getOperand(2))
853 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
857 case Hexagon::TFR_PdTrue: {
858 unsigned Reg = MI->getOperand(0).getReg();
859 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
860 .addReg(Reg, RegState::Undef)
861 .addReg(Reg, RegState::Undef);
865 case Hexagon::TFR_PdFalse: {
866 unsigned Reg = MI->getOperand(0).getReg();
867 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
868 .addReg(Reg, RegState::Undef)
869 .addReg(Reg, RegState::Undef);
873 case Hexagon::VMULW: {
874 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
875 unsigned DstReg = MI->getOperand(0).getReg();
876 unsigned Src1Reg = MI->getOperand(1).getReg();
877 unsigned Src2Reg = MI->getOperand(2).getReg();
878 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
879 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
880 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
881 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
882 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_mpyi),
883 HRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(Src1SubHi)
885 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_mpyi),
886 HRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(Src1SubLo)
889 MRI.clearKillFlags(Src1SubHi);
890 MRI.clearKillFlags(Src1SubLo);
891 MRI.clearKillFlags(Src2SubHi);
892 MRI.clearKillFlags(Src2SubLo);
895 case Hexagon::VMULW_ACC: {
896 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
897 unsigned DstReg = MI->getOperand(0).getReg();
898 unsigned Src1Reg = MI->getOperand(1).getReg();
899 unsigned Src2Reg = MI->getOperand(2).getReg();
900 unsigned Src3Reg = MI->getOperand(3).getReg();
901 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
902 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
903 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
904 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
905 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::subreg_hireg);
906 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::subreg_loreg);
907 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_maci),
908 HRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(Src1SubHi)
909 .addReg(Src2SubHi).addReg(Src3SubHi);
910 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_maci),
911 HRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(Src1SubLo)
912 .addReg(Src2SubLo).addReg(Src3SubLo);
914 MRI.clearKillFlags(Src1SubHi);
915 MRI.clearKillFlags(Src1SubLo);
916 MRI.clearKillFlags(Src2SubHi);
917 MRI.clearKillFlags(Src2SubLo);
918 MRI.clearKillFlags(Src3SubHi);
919 MRI.clearKillFlags(Src3SubLo);
922 case Hexagon::MUX64_rr: {
923 const MachineOperand &Op0 = MI->getOperand(0);
924 const MachineOperand &Op1 = MI->getOperand(1);
925 const MachineOperand &Op2 = MI->getOperand(2);
926 const MachineOperand &Op3 = MI->getOperand(3);
927 unsigned Rd = Op0.getReg();
928 unsigned Pu = Op1.getReg();
929 unsigned Rs = Op2.getReg();
930 unsigned Rt = Op3.getReg();
931 DebugLoc DL = MI->getDebugLoc();
932 unsigned K1 = getKillRegState(Op1.isKill());
933 unsigned K2 = getKillRegState(Op2.isKill());
934 unsigned K3 = getKillRegState(Op3.isKill());
936 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
937 .addReg(Pu, (Rd == Rt) ? K1 : 0)
940 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
946 case Hexagon::TCRETURNi:
947 MI->setDesc(get(Hexagon::J2_jump));
949 case Hexagon::TCRETURNr:
950 MI->setDesc(get(Hexagon::J2_jumpr));
952 case Hexagon::TFRI_f:
953 case Hexagon::TFRI_cPt_f:
954 case Hexagon::TFRI_cNotPt_f: {
955 unsigned Opx = (Opc == Hexagon::TFRI_f) ? 1 : 2;
956 APFloat FVal = MI->getOperand(Opx).getFPImm()->getValueAPF();
957 APInt IVal = FVal.bitcastToAPInt();
958 MI->RemoveOperand(Opx);
959 unsigned NewOpc = (Opc == Hexagon::TFRI_f) ? Hexagon::A2_tfrsi :
960 (Opc == Hexagon::TFRI_cPt_f) ? Hexagon::C2_cmoveit :
962 MI->setDesc(get(NewOpc));
963 MI->addOperand(MachineOperand::CreateImm(IVal.getZExtValue()));
972 // We indicate that we want to reverse the branch by
973 // inserting the reversed branching opcode.
974 bool HexagonInstrInfo::ReverseBranchCondition(
975 SmallVectorImpl<MachineOperand> &Cond) const {
978 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
979 unsigned opcode = Cond[0].getImm();
981 assert(get(opcode).isBranch() && "Should be a branching condition.");
982 if (isEndLoopN(opcode))
984 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
985 Cond[0].setImm(NewOpcode);
990 void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
991 MachineBasicBlock::iterator MI) const {
993 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
997 // Returns true if an instruction is predicated irrespective of the predicate
998 // sense. For example, all of the following will return true.
999 // if (p0) R1 = add(R2, R3)
1000 // if (!p0) R1 = add(R2, R3)
1001 // if (p0.new) R1 = add(R2, R3)
1002 // if (!p0.new) R1 = add(R2, R3)
1003 // Note: New-value stores are not included here as in the current
1004 // implementation, we don't need to check their predicate sense.
1005 bool HexagonInstrInfo::isPredicated(const MachineInstr *MI) const {
1006 const uint64_t F = MI->getDesc().TSFlags;
1007 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
1011 bool HexagonInstrInfo::PredicateInstruction(MachineInstr *MI,
1012 ArrayRef<MachineOperand> Cond) const {
1013 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1014 isEndLoopN(Cond[0].getImm())) {
1015 DEBUG(dbgs() << "\nCannot predicate:"; MI->dump(););
1018 int Opc = MI->getOpcode();
1019 assert (isPredicable(MI) && "Expected predicable instruction");
1020 bool invertJump = predOpcodeHasNot(Cond);
1022 // We have to predicate MI "in place", i.e. after this function returns,
1023 // MI will need to be transformed into a predicated form. To avoid com-
1024 // plicated manipulations with the operands (handling tied operands,
1025 // etc.), build a new temporary instruction, then overwrite MI with it.
1027 MachineBasicBlock &B = *MI->getParent();
1028 DebugLoc DL = MI->getDebugLoc();
1029 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1030 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
1031 unsigned NOp = 0, NumOps = MI->getNumOperands();
1032 while (NOp < NumOps) {
1033 MachineOperand &Op = MI->getOperand(NOp);
1034 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1040 unsigned PredReg, PredRegPos, PredRegFlags;
1041 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1044 T.addReg(PredReg, PredRegFlags);
1045 while (NOp < NumOps)
1046 T.addOperand(MI->getOperand(NOp++));
1048 MI->setDesc(get(PredOpc));
1049 while (unsigned n = MI->getNumOperands())
1050 MI->RemoveOperand(n-1);
1051 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
1052 MI->addOperand(T->getOperand(i));
1054 MachineBasicBlock::instr_iterator TI = T->getIterator();
1057 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1058 MRI.clearKillFlags(PredReg);
1063 bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1064 ArrayRef<MachineOperand> Pred2) const {
1070 bool HexagonInstrInfo::DefinesPredicate(MachineInstr *MI,
1071 std::vector<MachineOperand> &Pred) const {
1072 auto &HRI = getRegisterInfo();
1073 for (unsigned oper = 0; oper < MI->getNumOperands(); ++oper) {
1074 MachineOperand MO = MI->getOperand(oper);
1075 if (MO.isReg() && MO.isDef()) {
1076 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1077 if (RC == &Hexagon::PredRegsRegClass) {
1086 bool HexagonInstrInfo::isPredicable(MachineInstr *MI) const {
1087 bool isPred = MI->getDesc().isPredicable();
1092 const int Opc = MI->getOpcode();
1093 int NumOperands = MI->getNumOperands();
1095 // Keep a flag for upto 4 operands in the instructions, to indicate if
1096 // that operand has been constant extended.
1097 bool OpCExtended[4];
1098 if (NumOperands > 4)
1101 for (int i = 0; i < NumOperands; i++)
1102 OpCExtended[i] = (isOperandExtended(MI, i) && isConstExtended(MI));
1105 case Hexagon::A2_tfrsi:
1106 return (isOperandExtended(MI, 1) && isConstExtended(MI)) ||
1107 isInt<12>(MI->getOperand(1).getImm());
1109 case Hexagon::S2_storerd_io:
1110 return isShiftedUInt<6,3>(MI->getOperand(1).getImm());
1112 case Hexagon::S2_storeri_io:
1113 case Hexagon::S2_storerinew_io:
1114 return isShiftedUInt<6,2>(MI->getOperand(1).getImm());
1116 case Hexagon::S2_storerh_io:
1117 case Hexagon::S2_storerhnew_io:
1118 return isShiftedUInt<6,1>(MI->getOperand(1).getImm());
1120 case Hexagon::S2_storerb_io:
1121 case Hexagon::S2_storerbnew_io:
1122 return isUInt<6>(MI->getOperand(1).getImm());
1124 case Hexagon::L2_loadrd_io:
1125 return isShiftedUInt<6,3>(MI->getOperand(2).getImm());
1127 case Hexagon::L2_loadri_io:
1128 return isShiftedUInt<6,2>(MI->getOperand(2).getImm());
1130 case Hexagon::L2_loadrh_io:
1131 case Hexagon::L2_loadruh_io:
1132 return isShiftedUInt<6,1>(MI->getOperand(2).getImm());
1134 case Hexagon::L2_loadrb_io:
1135 case Hexagon::L2_loadrub_io:
1136 return isUInt<6>(MI->getOperand(2).getImm());
1138 case Hexagon::L2_loadrd_pi:
1139 return isShiftedInt<4,3>(MI->getOperand(3).getImm());
1141 case Hexagon::L2_loadri_pi:
1142 return isShiftedInt<4,2>(MI->getOperand(3).getImm());
1144 case Hexagon::L2_loadrh_pi:
1145 case Hexagon::L2_loadruh_pi:
1146 return isShiftedInt<4,1>(MI->getOperand(3).getImm());
1148 case Hexagon::L2_loadrb_pi:
1149 case Hexagon::L2_loadrub_pi:
1150 return isInt<4>(MI->getOperand(3).getImm());
1152 case Hexagon::S4_storeirb_io:
1153 case Hexagon::S4_storeirh_io:
1154 case Hexagon::S4_storeiri_io:
1155 return (OpCExtended[1] || isUInt<6>(MI->getOperand(1).getImm())) &&
1156 (OpCExtended[2] || isInt<6>(MI->getOperand(2).getImm()));
1158 case Hexagon::A2_addi:
1159 return isInt<8>(MI->getOperand(2).getImm());
1161 case Hexagon::A2_aslh:
1162 case Hexagon::A2_asrh:
1163 case Hexagon::A2_sxtb:
1164 case Hexagon::A2_sxth:
1165 case Hexagon::A2_zxtb:
1166 case Hexagon::A2_zxth:
1174 bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1175 const MachineBasicBlock *MBB, const MachineFunction &MF) const {
1176 // Debug info is never a scheduling boundary. It's necessary to be explicit
1177 // due to the special treatment of IT instructions below, otherwise a
1178 // dbg_value followed by an IT will result in the IT instruction being
1179 // considered a scheduling hazard, which is wrong. It should be the actual
1180 // instruction preceding the dbg_value instruction(s), just like it is
1181 // when debug info is not present.
1182 if (MI->isDebugValue())
1185 // Throwing call is a boundary.
1187 // If any of the block's successors is a landing pad, this could be a
1189 for (auto I : MBB->successors())
1194 // Don't mess around with no return calls.
1195 if (MI->getOpcode() == Hexagon::CALLv3nr)
1198 // Terminators and labels can't be scheduled around.
1199 if (MI->getDesc().isTerminator() || MI->isPosition())
1202 if (MI->isInlineAsm() && !ScheduleInlineAsm)
1209 /// Measure the specified inline asm to determine an approximation of its
1211 /// Comments (which run till the next SeparatorString or newline) do not
1212 /// count as an instruction.
1213 /// Any other non-whitespace text is considered an instruction, with
1214 /// multiple instructions separated by SeparatorString or newlines.
1215 /// Variable-length instructions are not handled here; this function
1216 /// may be overloaded in the target code to do that.
1217 /// Hexagon counts the number of ##'s and adjust for that many
1218 /// constant exenders.
1219 unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1220 const MCAsmInfo &MAI) const {
1221 StringRef AStr(Str);
1222 // Count the number of instructions in the asm.
1223 bool atInsnStart = true;
1224 unsigned Length = 0;
1225 for (; *Str; ++Str) {
1226 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1227 strlen(MAI.getSeparatorString())) == 0)
1229 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1230 Length += MAI.getMaxInstLength();
1231 atInsnStart = false;
1233 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
1234 strlen(MAI.getCommentString())) == 0)
1235 atInsnStart = false;
1238 // Add to size number of constant extenders seen * 4.
1239 StringRef Occ("##");
1240 Length += AStr.count(Occ)*4;
1245 ScheduleHazardRecognizer*
1246 HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1247 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
1248 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1252 /// \brief For a comparison instruction, return the source registers in
1253 /// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1254 /// compares against in CmpValue. Return true if the comparison instruction
1255 /// can be analyzed.
1256 bool HexagonInstrInfo::analyzeCompare(const MachineInstr *MI,
1257 unsigned &SrcReg, unsigned &SrcReg2, int &Mask, int &Value) const {
1258 unsigned Opc = MI->getOpcode();
1260 // Set mask and the first source register.
1262 case Hexagon::C2_cmpeq:
1263 case Hexagon::C2_cmpeqp:
1264 case Hexagon::C2_cmpgt:
1265 case Hexagon::C2_cmpgtp:
1266 case Hexagon::C2_cmpgtu:
1267 case Hexagon::C2_cmpgtup:
1268 case Hexagon::C4_cmpneq:
1269 case Hexagon::C4_cmplte:
1270 case Hexagon::C4_cmplteu:
1271 case Hexagon::C2_cmpeqi:
1272 case Hexagon::C2_cmpgti:
1273 case Hexagon::C2_cmpgtui:
1274 case Hexagon::C4_cmpneqi:
1275 case Hexagon::C4_cmplteui:
1276 case Hexagon::C4_cmpltei:
1277 SrcReg = MI->getOperand(1).getReg();
1280 case Hexagon::A4_cmpbeq:
1281 case Hexagon::A4_cmpbgt:
1282 case Hexagon::A4_cmpbgtu:
1283 case Hexagon::A4_cmpbeqi:
1284 case Hexagon::A4_cmpbgti:
1285 case Hexagon::A4_cmpbgtui:
1286 SrcReg = MI->getOperand(1).getReg();
1289 case Hexagon::A4_cmpheq:
1290 case Hexagon::A4_cmphgt:
1291 case Hexagon::A4_cmphgtu:
1292 case Hexagon::A4_cmpheqi:
1293 case Hexagon::A4_cmphgti:
1294 case Hexagon::A4_cmphgtui:
1295 SrcReg = MI->getOperand(1).getReg();
1300 // Set the value/second source register.
1302 case Hexagon::C2_cmpeq:
1303 case Hexagon::C2_cmpeqp:
1304 case Hexagon::C2_cmpgt:
1305 case Hexagon::C2_cmpgtp:
1306 case Hexagon::C2_cmpgtu:
1307 case Hexagon::C2_cmpgtup:
1308 case Hexagon::A4_cmpbeq:
1309 case Hexagon::A4_cmpbgt:
1310 case Hexagon::A4_cmpbgtu:
1311 case Hexagon::A4_cmpheq:
1312 case Hexagon::A4_cmphgt:
1313 case Hexagon::A4_cmphgtu:
1314 case Hexagon::C4_cmpneq:
1315 case Hexagon::C4_cmplte:
1316 case Hexagon::C4_cmplteu:
1317 SrcReg2 = MI->getOperand(2).getReg();
1320 case Hexagon::C2_cmpeqi:
1321 case Hexagon::C2_cmpgtui:
1322 case Hexagon::C2_cmpgti:
1323 case Hexagon::C4_cmpneqi:
1324 case Hexagon::C4_cmplteui:
1325 case Hexagon::C4_cmpltei:
1326 case Hexagon::A4_cmpbeqi:
1327 case Hexagon::A4_cmpbgti:
1328 case Hexagon::A4_cmpbgtui:
1329 case Hexagon::A4_cmpheqi:
1330 case Hexagon::A4_cmphgti:
1331 case Hexagon::A4_cmphgtui:
1333 Value = MI->getOperand(2).getImm();
1341 unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
1342 const MachineInstr *MI, unsigned *PredCost) const {
1343 return getInstrTimingClassLatency(ItinData, MI);
1347 DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1348 const TargetSubtargetInfo &STI) const {
1349 const InstrItineraryData *II = STI.getInstrItineraryData();
1350 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1354 // Inspired by this pair:
1355 // %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1356 // S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1357 // Currently AA considers the addresses in these instructions to be aliasing.
1358 bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(MachineInstr *MIa,
1359 MachineInstr *MIb, AliasAnalysis *AA) const {
1360 int OffsetA = 0, OffsetB = 0;
1361 unsigned SizeA = 0, SizeB = 0;
1363 if (MIa->hasUnmodeledSideEffects() || MIb->hasUnmodeledSideEffects() ||
1364 MIa->hasOrderedMemoryRef() || MIa->hasOrderedMemoryRef())
1367 // Instructions that are pure loads, not loads and stores like memops are not
1369 if (MIa->mayLoad() && !isMemOp(MIa) && MIb->mayLoad() && !isMemOp(MIb))
1372 // Get base, offset, and access size in MIa.
1373 unsigned BaseRegA = getBaseAndOffset(MIa, OffsetA, SizeA);
1374 if (!BaseRegA || !SizeA)
1377 // Get base, offset, and access size in MIb.
1378 unsigned BaseRegB = getBaseAndOffset(MIb, OffsetB, SizeB);
1379 if (!BaseRegB || !SizeB)
1382 if (BaseRegA != BaseRegB)
1385 // This is a mem access with the same base register and known offsets from it.
1387 if (OffsetA > OffsetB) {
1388 uint64_t offDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1389 return (SizeB <= offDiff);
1390 } else if (OffsetA < OffsetB) {
1391 uint64_t offDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1392 return (SizeA <= offDiff);
1399 unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
1400 MachineRegisterInfo &MRI = MF->getRegInfo();
1401 const TargetRegisterClass *TRC;
1402 if (VT == MVT::i1) {
1403 TRC = &Hexagon::PredRegsRegClass;
1404 } else if (VT == MVT::i32 || VT == MVT::f32) {
1405 TRC = &Hexagon::IntRegsRegClass;
1406 } else if (VT == MVT::i64 || VT == MVT::f64) {
1407 TRC = &Hexagon::DoubleRegsRegClass;
1409 llvm_unreachable("Cannot handle this register class");
1412 unsigned NewReg = MRI.createVirtualRegister(TRC);
1417 bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr* MI) const {
1418 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1422 bool HexagonInstrInfo::isAccumulator(const MachineInstr *MI) const {
1423 const uint64_t F = MI->getDesc().TSFlags;
1424 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1428 bool HexagonInstrInfo::isComplex(const MachineInstr *MI) const {
1429 const MachineFunction *MF = MI->getParent()->getParent();
1430 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1431 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1434 && !(QII->isTC2Early(MI))
1435 && !(MI->getDesc().mayLoad())
1436 && !(MI->getDesc().mayStore())
1437 && (MI->getDesc().getOpcode() != Hexagon::S2_allocframe)
1438 && (MI->getDesc().getOpcode() != Hexagon::L2_deallocframe)
1439 && !(QII->isMemOp(MI))
1440 && !(MI->isBranch())
1441 && !(MI->isReturn())
1449 // Return true if the the instruction is a compund branch instruction.
1450 bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr *MI) const {
1451 return (getType(MI) == HexagonII::TypeCOMPOUND && MI->isBranch());
1455 bool HexagonInstrInfo::isCondInst(const MachineInstr *MI) const {
1456 return (MI->isBranch() && isPredicated(MI)) ||
1457 isConditionalTransfer(MI) ||
1458 isConditionalALU32(MI) ||
1459 isConditionalLoad(MI) ||
1460 // Predicated stores which don't have a .new on any operands.
1461 (MI->mayStore() && isPredicated(MI) && !isNewValueStore(MI) &&
1462 !isPredicatedNew(MI));
1466 bool HexagonInstrInfo::isConditionalALU32(const MachineInstr* MI) const {
1467 switch (MI->getOpcode()) {
1468 case Hexagon::A2_paddf:
1469 case Hexagon::A2_paddfnew:
1470 case Hexagon::A2_paddif:
1471 case Hexagon::A2_paddifnew:
1472 case Hexagon::A2_paddit:
1473 case Hexagon::A2_padditnew:
1474 case Hexagon::A2_paddt:
1475 case Hexagon::A2_paddtnew:
1476 case Hexagon::A2_pandf:
1477 case Hexagon::A2_pandfnew:
1478 case Hexagon::A2_pandt:
1479 case Hexagon::A2_pandtnew:
1480 case Hexagon::A2_porf:
1481 case Hexagon::A2_porfnew:
1482 case Hexagon::A2_port:
1483 case Hexagon::A2_portnew:
1484 case Hexagon::A2_psubf:
1485 case Hexagon::A2_psubfnew:
1486 case Hexagon::A2_psubt:
1487 case Hexagon::A2_psubtnew:
1488 case Hexagon::A2_pxorf:
1489 case Hexagon::A2_pxorfnew:
1490 case Hexagon::A2_pxort:
1491 case Hexagon::A2_pxortnew:
1492 case Hexagon::A4_paslhf:
1493 case Hexagon::A4_paslhfnew:
1494 case Hexagon::A4_paslht:
1495 case Hexagon::A4_paslhtnew:
1496 case Hexagon::A4_pasrhf:
1497 case Hexagon::A4_pasrhfnew:
1498 case Hexagon::A4_pasrht:
1499 case Hexagon::A4_pasrhtnew:
1500 case Hexagon::A4_psxtbf:
1501 case Hexagon::A4_psxtbfnew:
1502 case Hexagon::A4_psxtbt:
1503 case Hexagon::A4_psxtbtnew:
1504 case Hexagon::A4_psxthf:
1505 case Hexagon::A4_psxthfnew:
1506 case Hexagon::A4_psxtht:
1507 case Hexagon::A4_psxthtnew:
1508 case Hexagon::A4_pzxtbf:
1509 case Hexagon::A4_pzxtbfnew:
1510 case Hexagon::A4_pzxtbt:
1511 case Hexagon::A4_pzxtbtnew:
1512 case Hexagon::A4_pzxthf:
1513 case Hexagon::A4_pzxthfnew:
1514 case Hexagon::A4_pzxtht:
1515 case Hexagon::A4_pzxthtnew:
1516 case Hexagon::C2_ccombinewf:
1517 case Hexagon::C2_ccombinewt:
1524 // FIXME - Function name and it's functionality don't match.
1525 // It should be renamed to hasPredNewOpcode()
1526 bool HexagonInstrInfo::isConditionalLoad(const MachineInstr* MI) const {
1527 if (!MI->getDesc().mayLoad() || !isPredicated(MI))
1530 int PNewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1531 // Instruction with valid predicated-new opcode can be promoted to .new.
1532 return PNewOpcode >= 0;
1536 // Returns true if an instruction is a conditional store.
1538 // Note: It doesn't include conditional new-value stores as they can't be
1539 // converted to .new predicate.
1540 bool HexagonInstrInfo::isConditionalStore(const MachineInstr* MI) const {
1541 switch (MI->getOpcode()) {
1542 default: return false;
1543 case Hexagon::S4_storeirbt_io:
1544 case Hexagon::S4_storeirbf_io:
1545 case Hexagon::S4_pstorerbt_rr:
1546 case Hexagon::S4_pstorerbf_rr:
1547 case Hexagon::S2_pstorerbt_io:
1548 case Hexagon::S2_pstorerbf_io:
1549 case Hexagon::S2_pstorerbt_pi:
1550 case Hexagon::S2_pstorerbf_pi:
1551 case Hexagon::S2_pstorerdt_io:
1552 case Hexagon::S2_pstorerdf_io:
1553 case Hexagon::S4_pstorerdt_rr:
1554 case Hexagon::S4_pstorerdf_rr:
1555 case Hexagon::S2_pstorerdt_pi:
1556 case Hexagon::S2_pstorerdf_pi:
1557 case Hexagon::S2_pstorerht_io:
1558 case Hexagon::S2_pstorerhf_io:
1559 case Hexagon::S4_storeirht_io:
1560 case Hexagon::S4_storeirhf_io:
1561 case Hexagon::S4_pstorerht_rr:
1562 case Hexagon::S4_pstorerhf_rr:
1563 case Hexagon::S2_pstorerht_pi:
1564 case Hexagon::S2_pstorerhf_pi:
1565 case Hexagon::S2_pstorerit_io:
1566 case Hexagon::S2_pstorerif_io:
1567 case Hexagon::S4_storeirit_io:
1568 case Hexagon::S4_storeirif_io:
1569 case Hexagon::S4_pstorerit_rr:
1570 case Hexagon::S4_pstorerif_rr:
1571 case Hexagon::S2_pstorerit_pi:
1572 case Hexagon::S2_pstorerif_pi:
1574 // V4 global address store before promoting to dot new.
1575 case Hexagon::S4_pstorerdt_abs:
1576 case Hexagon::S4_pstorerdf_abs:
1577 case Hexagon::S4_pstorerbt_abs:
1578 case Hexagon::S4_pstorerbf_abs:
1579 case Hexagon::S4_pstorerht_abs:
1580 case Hexagon::S4_pstorerhf_abs:
1581 case Hexagon::S4_pstorerit_abs:
1582 case Hexagon::S4_pstorerif_abs:
1585 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1586 // from the "Conditional Store" list. Because a predicated new value store
1587 // would NOT be promoted to a double dot new store.
1588 // This function returns yes for those stores that are predicated but not
1589 // yet promoted to predicate dot new instructions.
1594 bool HexagonInstrInfo::isConditionalTransfer(const MachineInstr *MI) const {
1595 switch (MI->getOpcode()) {
1596 case Hexagon::A2_tfrt:
1597 case Hexagon::A2_tfrf:
1598 case Hexagon::C2_cmoveit:
1599 case Hexagon::C2_cmoveif:
1600 case Hexagon::A2_tfrtnew:
1601 case Hexagon::A2_tfrfnew:
1602 case Hexagon::C2_cmovenewit:
1603 case Hexagon::C2_cmovenewif:
1604 case Hexagon::A2_tfrpt:
1605 case Hexagon::A2_tfrpf:
1615 // TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1616 // isFPImm and later getFPImm as well.
1617 bool HexagonInstrInfo::isConstExtended(const MachineInstr *MI) const {
1618 const uint64_t F = MI->getDesc().TSFlags;
1619 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1620 if (isExtended) // Instruction must be extended.
1623 unsigned isExtendable =
1624 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1631 short ExtOpNum = getCExtOpNum(MI);
1632 const MachineOperand &MO = MI->getOperand(ExtOpNum);
1633 // Use MO operand flags to determine if MO
1634 // has the HMOTF_ConstExtended flag set.
1635 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1637 // If this is a Machine BB address we are talking about, and it is
1638 // not marked as extended, say so.
1642 // We could be using an instruction with an extendable immediate and shoehorn
1643 // a global address into it. If it is a global address it will be constant
1644 // extended. We do this for COMBINE.
1645 // We currently only handle isGlobal() because it is the only kind of
1646 // object we are going to end up with here for now.
1647 // In the future we probably should add isSymbol(), etc.
1648 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
1649 MO.isJTI() || MO.isCPI())
1652 // If the extendable operand is not 'Immediate' type, the instruction should
1653 // have 'isExtended' flag set.
1654 assert(MO.isImm() && "Extendable operand must be Immediate type");
1656 int MinValue = getMinValue(MI);
1657 int MaxValue = getMaxValue(MI);
1658 int ImmValue = MO.getImm();
1660 return (ImmValue < MinValue || ImmValue > MaxValue);
1664 bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1665 switch (MI->getOpcode()) {
1666 case Hexagon::L4_return :
1667 case Hexagon::L4_return_t :
1668 case Hexagon::L4_return_f :
1669 case Hexagon::L4_return_tnew_pnt :
1670 case Hexagon::L4_return_fnew_pnt :
1671 case Hexagon::L4_return_tnew_pt :
1672 case Hexagon::L4_return_fnew_pt :
1679 // Return true when ConsMI uses a register defined by ProdMI.
1680 bool HexagonInstrInfo::isDependent(const MachineInstr *ProdMI,
1681 const MachineInstr *ConsMI) const {
1682 const MCInstrDesc &ProdMCID = ProdMI->getDesc();
1683 if (!ProdMCID.getNumDefs())
1686 auto &HRI = getRegisterInfo();
1688 SmallVector<unsigned, 4> DefsA;
1689 SmallVector<unsigned, 4> DefsB;
1690 SmallVector<unsigned, 8> UsesA;
1691 SmallVector<unsigned, 8> UsesB;
1693 parseOperands(ProdMI, DefsA, UsesA);
1694 parseOperands(ConsMI, DefsB, UsesB);
1696 for (auto &RegA : DefsA)
1697 for (auto &RegB : UsesB) {
1698 // True data dependency.
1702 if (Hexagon::DoubleRegsRegClass.contains(RegA))
1703 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1704 if (RegB == *SubRegs)
1707 if (Hexagon::DoubleRegsRegClass.contains(RegB))
1708 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1709 if (RegA == *SubRegs)
1717 // Returns true if the instruction is alread a .cur.
1718 bool HexagonInstrInfo::isDotCurInst(const MachineInstr* MI) const {
1719 switch (MI->getOpcode()) {
1720 case Hexagon::V6_vL32b_cur_pi:
1721 case Hexagon::V6_vL32b_cur_ai:
1722 case Hexagon::V6_vL32b_cur_pi_128B:
1723 case Hexagon::V6_vL32b_cur_ai_128B:
1730 // Returns true, if any one of the operands is a dot new
1731 // insn, whether it is predicated dot new or register dot new.
1732 bool HexagonInstrInfo::isDotNewInst(const MachineInstr* MI) const {
1733 if (isNewValueInst(MI) ||
1734 (isPredicated(MI) && isPredicatedNew(MI)))
1741 /// Symmetrical. See if these two instructions are fit for duplex pair.
1742 bool HexagonInstrInfo::isDuplexPair(const MachineInstr *MIa,
1743 const MachineInstr *MIb) const {
1744 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
1745 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
1746 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
1750 bool HexagonInstrInfo::isEarlySourceInstr(MachineInstr *MI) const {
1754 if (MI->mayLoad() || MI->mayStore() || MI->isCompare())
1758 unsigned SchedClass = MI->getDesc().getSchedClass();
1759 if (SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23)
1765 bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
1766 return (Opcode == Hexagon::ENDLOOP0 ||
1767 Opcode == Hexagon::ENDLOOP1);
1771 bool HexagonInstrInfo::isExpr(unsigned OpType) const {
1773 case MachineOperand::MO_MachineBasicBlock:
1774 case MachineOperand::MO_GlobalAddress:
1775 case MachineOperand::MO_ExternalSymbol:
1776 case MachineOperand::MO_JumpTableIndex:
1777 case MachineOperand::MO_ConstantPoolIndex:
1778 case MachineOperand::MO_BlockAddress:
1786 bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
1787 const MCInstrDesc &MID = MI->getDesc();
1788 const uint64_t F = MID.TSFlags;
1789 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
1792 // TODO: This is largely obsolete now. Will need to be removed
1793 // in consecutive patches.
1794 switch(MI->getOpcode()) {
1795 // TFR_FI Remains a special case.
1796 case Hexagon::TFR_FI:
1805 // This returns true in two cases:
1806 // - The OP code itself indicates that this is an extended instruction.
1807 // - One of MOs has been marked with HMOTF_ConstExtended flag.
1808 bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
1809 // First check if this is permanently extended op code.
1810 const uint64_t F = MI->getDesc().TSFlags;
1811 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
1813 // Use MO operand flags to determine if one of MI's operands
1814 // has HMOTF_ConstExtended flag set.
1815 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
1816 E = MI->operands_end(); I != E; ++I) {
1817 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
1824 bool HexagonInstrInfo::isFloat(MachineInstr *MI) const {
1825 unsigned Opcode = MI->getOpcode();
1826 const uint64_t F = get(Opcode).TSFlags;
1827 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
1831 bool HexagonInstrInfo::isIndirectCall(const MachineInstr *MI) const {
1832 switch (MI->getOpcode()) {
1833 case Hexagon::J2_callr :
1834 case Hexagon::J2_callrf :
1835 case Hexagon::J2_callrt :
1842 bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr *MI) const {
1843 switch (MI->getOpcode()) {
1844 case Hexagon::L4_return :
1845 case Hexagon::L4_return_t :
1846 case Hexagon::L4_return_f :
1847 case Hexagon::L4_return_fnew_pnt :
1848 case Hexagon::L4_return_fnew_pt :
1849 case Hexagon::L4_return_tnew_pnt :
1850 case Hexagon::L4_return_tnew_pt :
1857 bool HexagonInstrInfo::isJumpR(const MachineInstr *MI) const {
1858 switch (MI->getOpcode()) {
1859 case Hexagon::J2_jumpr :
1860 case Hexagon::J2_jumprt :
1861 case Hexagon::J2_jumprf :
1862 case Hexagon::J2_jumprtnewpt :
1863 case Hexagon::J2_jumprfnewpt :
1864 case Hexagon::J2_jumprtnew :
1865 case Hexagon::J2_jumprfnew :
1872 // Return true if a given MI can accomodate given offset.
1873 // Use abs estimate as oppose to the exact number.
1874 // TODO: This will need to be changed to use MC level
1875 // definition of instruction extendable field size.
1876 bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr *MI,
1877 unsigned offset) const {
1878 // This selection of jump instructions matches to that what
1879 // AnalyzeBranch can parse, plus NVJ.
1880 if (isNewValueJump(MI)) // r9:2
1881 return isInt<11>(offset);
1883 switch (MI->getOpcode()) {
1884 // Still missing Jump to address condition on register value.
1887 case Hexagon::J2_jump: // bits<24> dst; // r22:2
1888 case Hexagon::J2_call:
1889 case Hexagon::CALLv3nr:
1890 return isInt<24>(offset);
1891 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
1892 case Hexagon::J2_jumpf:
1893 case Hexagon::J2_jumptnew:
1894 case Hexagon::J2_jumptnewpt:
1895 case Hexagon::J2_jumpfnew:
1896 case Hexagon::J2_jumpfnewpt:
1897 case Hexagon::J2_callt:
1898 case Hexagon::J2_callf:
1899 return isInt<17>(offset);
1900 case Hexagon::J2_loop0i:
1901 case Hexagon::J2_loop0iext:
1902 case Hexagon::J2_loop0r:
1903 case Hexagon::J2_loop0rext:
1904 case Hexagon::J2_loop1i:
1905 case Hexagon::J2_loop1iext:
1906 case Hexagon::J2_loop1r:
1907 case Hexagon::J2_loop1rext:
1908 return isInt<9>(offset);
1909 // TODO: Add all the compound branches here. Can we do this in Relation model?
1910 case Hexagon::J4_cmpeqi_tp0_jump_nt:
1911 case Hexagon::J4_cmpeqi_tp1_jump_nt:
1912 return isInt<11>(offset);
1917 bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(MachineInstr *LRMI,
1918 MachineInstr *ESMI) const {
1922 bool isLate = isLateResultInstr(LRMI);
1923 bool isEarly = isEarlySourceInstr(ESMI);
1925 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
1926 DEBUG(LRMI->dump());
1927 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
1928 DEBUG(ESMI->dump());
1930 if (isLate && isEarly) {
1931 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
1939 bool HexagonInstrInfo::isLateResultInstr(MachineInstr *MI) const {
1943 switch (MI->getOpcode()) {
1944 case TargetOpcode::EXTRACT_SUBREG:
1945 case TargetOpcode::INSERT_SUBREG:
1946 case TargetOpcode::SUBREG_TO_REG:
1947 case TargetOpcode::REG_SEQUENCE:
1948 case TargetOpcode::IMPLICIT_DEF:
1949 case TargetOpcode::COPY:
1950 case TargetOpcode::INLINEASM:
1951 case TargetOpcode::PHI:
1957 unsigned SchedClass = MI->getDesc().getSchedClass();
1959 switch (SchedClass) {
1960 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
1961 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
1962 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
1963 case Hexagon::Sched::ALU64_tc_1_SLOT23:
1964 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
1965 case Hexagon::Sched::S_2op_tc_1_SLOT23:
1966 case Hexagon::Sched::S_3op_tc_1_SLOT23:
1967 case Hexagon::Sched::V2LDST_tc_ld_SLOT01:
1968 case Hexagon::Sched::V2LDST_tc_st_SLOT0:
1969 case Hexagon::Sched::V2LDST_tc_st_SLOT01:
1970 case Hexagon::Sched::V4LDST_tc_ld_SLOT01:
1971 case Hexagon::Sched::V4LDST_tc_st_SLOT0:
1972 case Hexagon::Sched::V4LDST_tc_st_SLOT01:
1979 bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr *MI) const {
1983 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
1984 // resource, but all operands can be received late like an ALU instruction.
1985 return MI->getDesc().getSchedClass() == Hexagon::Sched::CVI_VX_LATE;
1989 bool HexagonInstrInfo::isLoopN(unsigned Opcode) const {
1990 return (Opcode == Hexagon::J2_loop0i ||
1991 Opcode == Hexagon::J2_loop0r ||
1992 Opcode == Hexagon::J2_loop0iext ||
1993 Opcode == Hexagon::J2_loop0rext ||
1994 Opcode == Hexagon::J2_loop1i ||
1995 Opcode == Hexagon::J2_loop1r ||
1996 Opcode == Hexagon::J2_loop1iext ||
1997 Opcode == Hexagon::J2_loop1rext);
2001 bool HexagonInstrInfo::isMemOp(const MachineInstr *MI) const {
2002 switch (MI->getOpcode()) {
2003 default: return false;
2004 case Hexagon::L4_iadd_memopw_io :
2005 case Hexagon::L4_isub_memopw_io :
2006 case Hexagon::L4_add_memopw_io :
2007 case Hexagon::L4_sub_memopw_io :
2008 case Hexagon::L4_and_memopw_io :
2009 case Hexagon::L4_or_memopw_io :
2010 case Hexagon::L4_iadd_memoph_io :
2011 case Hexagon::L4_isub_memoph_io :
2012 case Hexagon::L4_add_memoph_io :
2013 case Hexagon::L4_sub_memoph_io :
2014 case Hexagon::L4_and_memoph_io :
2015 case Hexagon::L4_or_memoph_io :
2016 case Hexagon::L4_iadd_memopb_io :
2017 case Hexagon::L4_isub_memopb_io :
2018 case Hexagon::L4_add_memopb_io :
2019 case Hexagon::L4_sub_memopb_io :
2020 case Hexagon::L4_and_memopb_io :
2021 case Hexagon::L4_or_memopb_io :
2022 case Hexagon::L4_ior_memopb_io:
2023 case Hexagon::L4_ior_memoph_io:
2024 case Hexagon::L4_ior_memopw_io:
2025 case Hexagon::L4_iand_memopb_io:
2026 case Hexagon::L4_iand_memoph_io:
2027 case Hexagon::L4_iand_memopw_io:
2034 bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
2035 const uint64_t F = MI->getDesc().TSFlags;
2036 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2040 bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2041 const uint64_t F = get(Opcode).TSFlags;
2042 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2046 bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
2047 return isNewValueJump(MI) || isNewValueStore(MI);
2051 bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
2052 return isNewValue(MI) && MI->isBranch();
2056 bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2057 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2061 bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
2062 const uint64_t F = MI->getDesc().TSFlags;
2063 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2067 bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2068 const uint64_t F = get(Opcode).TSFlags;
2069 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2073 // Returns true if a particular operand is extendable for an instruction.
2074 bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
2075 unsigned OperandNum) const {
2076 const uint64_t F = MI->getDesc().TSFlags;
2077 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2082 bool HexagonInstrInfo::isPostIncrement(const MachineInstr* MI) const {
2083 return getAddrMode(MI) == HexagonII::PostInc;
2087 bool HexagonInstrInfo::isPredicatedNew(const MachineInstr *MI) const {
2088 const uint64_t F = MI->getDesc().TSFlags;
2089 assert(isPredicated(MI));
2090 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2094 bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2095 const uint64_t F = get(Opcode).TSFlags;
2096 assert(isPredicated(Opcode));
2097 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2101 bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr *MI) const {
2102 const uint64_t F = MI->getDesc().TSFlags;
2103 return !((F >> HexagonII::PredicatedFalsePos) &
2104 HexagonII::PredicatedFalseMask);
2108 bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2109 const uint64_t F = get(Opcode).TSFlags;
2110 // Make sure that the instruction is predicated.
2111 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2112 return !((F >> HexagonII::PredicatedFalsePos) &
2113 HexagonII::PredicatedFalseMask);
2117 bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2118 const uint64_t F = get(Opcode).TSFlags;
2119 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2123 bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2124 const uint64_t F = get(Opcode).TSFlags;
2125 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2129 bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2130 const uint64_t F = get(Opcode).TSFlags;
2131 assert(get(Opcode).isBranch() &&
2132 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2133 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2137 bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
2138 return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2139 MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT;
2143 bool HexagonInstrInfo::isSolo(const MachineInstr* MI) const {
2144 const uint64_t F = MI->getDesc().TSFlags;
2145 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2149 bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr *MI) const {
2150 switch (MI->getOpcode()) {
2151 case Hexagon::STriw_pred :
2152 case Hexagon::LDriw_pred :
2160 // Returns true when SU has a timing class TC1.
2161 bool HexagonInstrInfo::isTC1(const MachineInstr *MI) const {
2162 unsigned SchedClass = MI->getDesc().getSchedClass();
2163 switch (SchedClass) {
2164 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2165 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2166 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2167 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2168 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2169 //case Hexagon::Sched::M_tc_1_SLOT23:
2170 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2171 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2180 bool HexagonInstrInfo::isTC2(const MachineInstr *MI) const {
2181 unsigned SchedClass = MI->getDesc().getSchedClass();
2182 switch (SchedClass) {
2183 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
2184 case Hexagon::Sched::ALU64_tc_2_SLOT23:
2185 case Hexagon::Sched::CR_tc_2_SLOT3:
2186 case Hexagon::Sched::M_tc_2_SLOT23:
2187 case Hexagon::Sched::S_2op_tc_2_SLOT23:
2188 case Hexagon::Sched::S_3op_tc_2_SLOT23:
2197 bool HexagonInstrInfo::isTC2Early(const MachineInstr *MI) const {
2198 unsigned SchedClass = MI->getDesc().getSchedClass();
2199 switch (SchedClass) {
2200 case Hexagon::Sched::ALU32_2op_tc_2early_SLOT0123:
2201 case Hexagon::Sched::ALU32_3op_tc_2early_SLOT0123:
2202 case Hexagon::Sched::ALU64_tc_2early_SLOT23:
2203 case Hexagon::Sched::CR_tc_2early_SLOT23:
2204 case Hexagon::Sched::CR_tc_2early_SLOT3:
2205 case Hexagon::Sched::J_tc_2early_SLOT0123:
2206 case Hexagon::Sched::J_tc_2early_SLOT2:
2207 case Hexagon::Sched::J_tc_2early_SLOT23:
2208 case Hexagon::Sched::S_2op_tc_2early_SLOT23:
2209 case Hexagon::Sched::S_3op_tc_2early_SLOT23:
2218 bool HexagonInstrInfo::isTC4x(const MachineInstr *MI) const {
2222 unsigned SchedClass = MI->getDesc().getSchedClass();
2223 return SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23;
2227 bool HexagonInstrInfo::isV60VectorInstruction(const MachineInstr *MI) const {
2231 const uint64_t V = getType(MI);
2232 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2236 // Check if the Offset is a valid auto-inc imm by Load/Store Type.
2238 bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2239 if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2240 VT == MVT::v32i16 || VT == MVT::v64i8) {
2241 return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2242 Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2243 (Offset & 0x3f) == 0);
2246 if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2247 VT == MVT::v64i16 || VT == MVT::v128i8) {
2248 return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2249 Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2250 (Offset & 0x7f) == 0);
2252 if (VT == MVT::i64) {
2253 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2254 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2255 (Offset & 0x7) == 0);
2257 if (VT == MVT::i32) {
2258 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2259 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2260 (Offset & 0x3) == 0);
2262 if (VT == MVT::i16) {
2263 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2264 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2265 (Offset & 0x1) == 0);
2267 if (VT == MVT::i8) {
2268 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2269 Offset <= Hexagon_MEMB_AUTOINC_MAX);
2271 llvm_unreachable("Not an auto-inc opc!");
2275 bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2276 bool Extend) const {
2277 // This function is to check whether the "Offset" is in the correct range of
2278 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
2279 // inserted to calculate the final address. Due to this reason, the function
2280 // assumes that the "Offset" has correct alignment.
2281 // We used to assert if the offset was not properly aligned, however,
2282 // there are cases where a misaligned pointer recast can cause this
2283 // problem, and we need to allow for it. The front end warns of such
2284 // misaligns with respect to load size.
2287 case Hexagon::STriq_pred_V6:
2288 case Hexagon::STriq_pred_vec_V6:
2289 case Hexagon::STriv_pseudo_V6:
2290 case Hexagon::STrivv_pseudo_V6:
2291 case Hexagon::LDriq_pred_V6:
2292 case Hexagon::LDriq_pred_vec_V6:
2293 case Hexagon::LDriv_pseudo_V6:
2294 case Hexagon::LDrivv_pseudo_V6:
2295 case Hexagon::LDrivv_indexed:
2296 case Hexagon::STrivv_indexed:
2297 case Hexagon::V6_vL32b_ai:
2298 case Hexagon::V6_vS32b_ai:
2299 case Hexagon::V6_vL32Ub_ai:
2300 case Hexagon::V6_vS32Ub_ai:
2301 return (Offset >= Hexagon_MEMV_OFFSET_MIN) &&
2302 (Offset <= Hexagon_MEMV_OFFSET_MAX);
2304 case Hexagon::STriq_pred_V6_128B:
2305 case Hexagon::STriq_pred_vec_V6_128B:
2306 case Hexagon::STriv_pseudo_V6_128B:
2307 case Hexagon::STrivv_pseudo_V6_128B:
2308 case Hexagon::LDriq_pred_V6_128B:
2309 case Hexagon::LDriq_pred_vec_V6_128B:
2310 case Hexagon::LDriv_pseudo_V6_128B:
2311 case Hexagon::LDrivv_pseudo_V6_128B:
2312 case Hexagon::LDrivv_indexed_128B:
2313 case Hexagon::STrivv_indexed_128B:
2314 case Hexagon::V6_vL32b_ai_128B:
2315 case Hexagon::V6_vS32b_ai_128B:
2316 case Hexagon::V6_vL32Ub_ai_128B:
2317 case Hexagon::V6_vS32Ub_ai_128B:
2318 return (Offset >= Hexagon_MEMV_OFFSET_MIN_128B) &&
2319 (Offset <= Hexagon_MEMV_OFFSET_MAX_128B);
2321 case Hexagon::J2_loop0i:
2322 case Hexagon::J2_loop1i:
2323 return isUInt<10>(Offset);
2330 case Hexagon::L2_loadri_io:
2331 case Hexagon::S2_storeri_io:
2332 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2333 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2335 case Hexagon::L2_loadrd_io:
2336 case Hexagon::S2_storerd_io:
2337 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2338 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2340 case Hexagon::L2_loadrh_io:
2341 case Hexagon::L2_loadruh_io:
2342 case Hexagon::S2_storerh_io:
2343 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2344 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2346 case Hexagon::L2_loadrb_io:
2347 case Hexagon::L2_loadrub_io:
2348 case Hexagon::S2_storerb_io:
2349 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2350 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2352 case Hexagon::A2_addi:
2353 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2354 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2356 case Hexagon::L4_iadd_memopw_io :
2357 case Hexagon::L4_isub_memopw_io :
2358 case Hexagon::L4_add_memopw_io :
2359 case Hexagon::L4_sub_memopw_io :
2360 case Hexagon::L4_and_memopw_io :
2361 case Hexagon::L4_or_memopw_io :
2362 return (0 <= Offset && Offset <= 255);
2364 case Hexagon::L4_iadd_memoph_io :
2365 case Hexagon::L4_isub_memoph_io :
2366 case Hexagon::L4_add_memoph_io :
2367 case Hexagon::L4_sub_memoph_io :
2368 case Hexagon::L4_and_memoph_io :
2369 case Hexagon::L4_or_memoph_io :
2370 return (0 <= Offset && Offset <= 127);
2372 case Hexagon::L4_iadd_memopb_io :
2373 case Hexagon::L4_isub_memopb_io :
2374 case Hexagon::L4_add_memopb_io :
2375 case Hexagon::L4_sub_memopb_io :
2376 case Hexagon::L4_and_memopb_io :
2377 case Hexagon::L4_or_memopb_io :
2378 return (0 <= Offset && Offset <= 63);
2380 // LDri_pred and STriw_pred are pseudo operations, so it has to take offset of
2381 // any size. Later pass knows how to handle it.
2382 case Hexagon::STriw_pred:
2383 case Hexagon::LDriw_pred:
2386 case Hexagon::TFR_FI:
2387 case Hexagon::TFR_FIA:
2388 case Hexagon::INLINEASM:
2391 case Hexagon::L2_ploadrbt_io:
2392 case Hexagon::L2_ploadrbf_io:
2393 case Hexagon::L2_ploadrubt_io:
2394 case Hexagon::L2_ploadrubf_io:
2395 case Hexagon::S2_pstorerbt_io:
2396 case Hexagon::S2_pstorerbf_io:
2397 case Hexagon::S4_storeirb_io:
2398 case Hexagon::S4_storeirbt_io:
2399 case Hexagon::S4_storeirbf_io:
2400 return isUInt<6>(Offset);
2402 case Hexagon::L2_ploadrht_io:
2403 case Hexagon::L2_ploadrhf_io:
2404 case Hexagon::L2_ploadruht_io:
2405 case Hexagon::L2_ploadruhf_io:
2406 case Hexagon::S2_pstorerht_io:
2407 case Hexagon::S2_pstorerhf_io:
2408 case Hexagon::S4_storeirh_io:
2409 case Hexagon::S4_storeirht_io:
2410 case Hexagon::S4_storeirhf_io:
2411 return isShiftedUInt<6,1>(Offset);
2413 case Hexagon::L2_ploadrit_io:
2414 case Hexagon::L2_ploadrif_io:
2415 case Hexagon::S2_pstorerit_io:
2416 case Hexagon::S2_pstorerif_io:
2417 case Hexagon::S4_storeiri_io:
2418 case Hexagon::S4_storeirit_io:
2419 case Hexagon::S4_storeirif_io:
2420 return isShiftedUInt<6,2>(Offset);
2422 case Hexagon::L2_ploadrdt_io:
2423 case Hexagon::L2_ploadrdf_io:
2424 case Hexagon::S2_pstorerdt_io:
2425 case Hexagon::S2_pstorerdf_io:
2426 return isShiftedUInt<6,3>(Offset);
2429 llvm_unreachable("No offset range is defined for this opcode. "
2430 "Please define it in the above switch statement!");
2434 bool HexagonInstrInfo::isVecAcc(const MachineInstr *MI) const {
2435 return MI && isV60VectorInstruction(MI) && isAccumulator(MI);
2439 bool HexagonInstrInfo::isVecALU(const MachineInstr *MI) const {
2442 const uint64_t F = get(MI->getOpcode()).TSFlags;
2443 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2445 V == HexagonII::TypeCVI_VA ||
2446 V == HexagonII::TypeCVI_VA_DV;
2450 bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr *ProdMI,
2451 const MachineInstr *ConsMI) const {
2452 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2455 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2458 if (mayBeNewStore(ConsMI))
2465 bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2473 // Returns true if an instruction can be converted into a non-extended
2474 // equivalent instruction.
2475 bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr *MI) const {
2477 // Check if the instruction has a register form that uses register in place
2478 // of the extended operand, if so return that as the non-extended form.
2479 if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
2482 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
2483 // Check addressing mode and retrieve non-ext equivalent instruction.
2485 switch (getAddrMode(MI)) {
2486 case HexagonII::Absolute :
2487 // Load/store with absolute addressing mode can be converted into
2488 // base+offset mode.
2489 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI->getOpcode());
2491 case HexagonII::BaseImmOffset :
2492 // Load/store with base+offset addressing mode can be converted into
2493 // base+register offset addressing mode. However left shift operand should
2495 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
2497 case HexagonII::BaseLongOffset:
2498 NonExtOpcode = Hexagon::getRegShlForm(MI->getOpcode());
2503 if (NonExtOpcode < 0)
2511 bool HexagonInstrInfo::hasPseudoInstrPair(MachineInstr *MI) const {
2512 return Hexagon::getRealHWInstr(MI->getOpcode(),
2513 Hexagon::InstrType_Pseudo) >= 0;
2517 bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2519 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2529 // Returns true, if a LD insn can be promoted to a cur load.
2530 bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr *MI) const {
2531 auto &HST = MI->getParent()->getParent()->getSubtarget<HexagonSubtarget>();
2532 const uint64_t F = MI->getDesc().TSFlags;
2533 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
2538 // Returns true, if a ST insn can be promoted to a new-value store.
2539 bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
2540 const uint64_t F = MI->getDesc().TSFlags;
2541 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
2545 bool HexagonInstrInfo::producesStall(const MachineInstr *ProdMI,
2546 const MachineInstr *ConsMI) const {
2547 // There is no stall when ProdMI is not a V60 vector.
2548 if (!isV60VectorInstruction(ProdMI))
2551 // There is no stall when ProdMI and ConsMI are not dependent.
2552 if (!isDependent(ProdMI, ConsMI))
2555 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
2556 // are scheduled in consecutive packets.
2557 if (isVecUsableNextPacket(ProdMI, ConsMI))
2564 bool HexagonInstrInfo::producesStall(const MachineInstr *MI,
2565 MachineBasicBlock::const_instr_iterator BII) const {
2566 // There is no stall when I is not a V60 vector.
2567 if (!isV60VectorInstruction(MI))
2570 MachineBasicBlock::const_instr_iterator MII = BII;
2571 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
2573 if (!(*MII).isBundle()) {
2574 const MachineInstr *J = &*MII;
2575 if (!isV60VectorInstruction(J))
2577 else if (isVecUsableNextPacket(J, MI))
2582 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
2583 const MachineInstr *J = &*MII;
2584 if (producesStall(J, MI))
2591 bool HexagonInstrInfo::predCanBeUsedAsDotNew(MachineInstr *MI,
2592 unsigned PredReg) const {
2593 for (unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++) {
2594 MachineOperand &MO = MI->getOperand(opNum);
2595 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
2596 return false; // Predicate register must be explicitly defined.
2599 // Hexagon Programmer's Reference says that decbin, memw_locked, and
2600 // memd_locked cannot be used as .new as well,
2601 // but we don't seem to have these instructions defined.
2602 return MI->getOpcode() != Hexagon::A4_tlbmatch;
2606 bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
2607 return (Opcode == Hexagon::J2_jumpt) ||
2608 (Opcode == Hexagon::J2_jumpf) ||
2609 (Opcode == Hexagon::J2_jumptnew) ||
2610 (Opcode == Hexagon::J2_jumpfnew) ||
2611 (Opcode == Hexagon::J2_jumptnewpt) ||
2612 (Opcode == Hexagon::J2_jumpfnewpt);
2616 bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
2617 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
2619 return !isPredicatedTrue(Cond[0].getImm());
2623 unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
2624 const uint64_t F = MI->getDesc().TSFlags;
2625 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
2629 // Returns the base register in a memory access (load/store). The offset is
2630 // returned in Offset and the access size is returned in AccessSize.
2631 unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr *MI,
2632 int &Offset, unsigned &AccessSize) const {
2633 // Return if it is not a base+offset type instruction or a MemOp.
2634 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
2635 getAddrMode(MI) != HexagonII::BaseLongOffset &&
2636 !isMemOp(MI) && !isPostIncrement(MI))
2639 // Since it is a memory access instruction, getMemAccessSize() should never
2641 assert (getMemAccessSize(MI) &&
2642 "BaseImmOffset or BaseLongOffset or MemOp without accessSize");
2644 // Return Values of getMemAccessSize() are
2645 // 0 - Checked in the assert above.
2646 // 1, 2, 3, 4 & 7, 8 - The statement below is correct for all these.
2647 // MemAccessSize is represented as 1+log2(N) where N is size in bits.
2648 AccessSize = (1U << (getMemAccessSize(MI) - 1));
2650 unsigned basePos = 0, offsetPos = 0;
2651 if (!getBaseAndOffsetPosition(MI, basePos, offsetPos))
2654 // Post increment updates its EA after the mem access,
2655 // so we need to treat its offset as zero.
2656 if (isPostIncrement(MI))
2659 Offset = MI->getOperand(offsetPos).getImm();
2662 return MI->getOperand(basePos).getReg();
2666 /// Return the position of the base and offset operands for this instruction.
2667 bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr *MI,
2668 unsigned &BasePos, unsigned &OffsetPos) const {
2669 // Deal with memops first.
2671 assert (MI->getOperand(0).isReg() && MI->getOperand(1).isImm() &&
2675 } else if (MI->mayStore()) {
2678 } else if (MI->mayLoad()) {
2684 if (isPredicated(MI)) {
2688 if (isPostIncrement(MI)) {
2693 if (!MI->getOperand(BasePos).isReg() || !MI->getOperand(OffsetPos).isImm())
2700 // Inserts branching instructions in reverse order of their occurence.
2701 // e.g. jump_t t1 (i1)
2703 // Jumpers = {i2, i1}
2704 SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
2705 MachineBasicBlock& MBB) const {
2706 SmallVector<MachineInstr*, 2> Jumpers;
2707 // If the block has no terminators, it just falls into the block after it.
2708 MachineBasicBlock::instr_iterator I = MBB.instr_end();
2709 if (I == MBB.instr_begin())
2712 // A basic block may looks like this:
2722 // It has two succs but does not have a terminator
2723 // Don't know how to handle it.
2728 } while (I != MBB.instr_begin());
2730 I = MBB.instr_end();
2733 while (I->isDebugValue()) {
2734 if (I == MBB.instr_begin())
2738 if (!isUnpredicatedTerminator(&*I))
2741 // Get the last instruction in the block.
2742 MachineInstr *LastInst = &*I;
2743 Jumpers.push_back(LastInst);
2744 MachineInstr *SecondLastInst = nullptr;
2745 // Find one more terminator if present.
2747 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(&*I)) {
2748 if (!SecondLastInst) {
2749 SecondLastInst = &*I;
2750 Jumpers.push_back(SecondLastInst);
2751 } else // This is a third branch.
2754 if (I == MBB.instr_begin())
2762 // Returns Operand Index for the constant extended instruction.
2763 unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
2764 const uint64_t F = MI->getDesc().TSFlags;
2765 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
2768 // See if instruction could potentially be a duplex candidate.
2769 // If so, return its group. Zero otherwise.
2770 HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
2771 const MachineInstr *MI) const {
2772 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
2774 switch (MI->getOpcode()) {
2776 return HexagonII::HCG_None;
2779 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
2780 // "Rd16=#U6 ; jump #r9:2"
2781 // "Rd16=Rs16 ; jump #r9:2"
2783 case Hexagon::C2_cmpeq:
2784 case Hexagon::C2_cmpgt:
2785 case Hexagon::C2_cmpgtu:
2786 DstReg = MI->getOperand(0).getReg();
2787 Src1Reg = MI->getOperand(1).getReg();
2788 Src2Reg = MI->getOperand(2).getReg();
2789 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
2790 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
2791 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
2792 return HexagonII::HCG_A;
2794 case Hexagon::C2_cmpeqi:
2795 case Hexagon::C2_cmpgti:
2796 case Hexagon::C2_cmpgtui:
2797 // P0 = cmp.eq(Rs,#u2)
2798 DstReg = MI->getOperand(0).getReg();
2799 SrcReg = MI->getOperand(1).getReg();
2800 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
2801 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
2802 isIntRegForSubInst(SrcReg) && MI->getOperand(2).isImm() &&
2803 ((isUInt<5>(MI->getOperand(2).getImm())) ||
2804 (MI->getOperand(2).getImm() == -1)))
2805 return HexagonII::HCG_A;
2807 case Hexagon::A2_tfr:
2809 DstReg = MI->getOperand(0).getReg();
2810 SrcReg = MI->getOperand(1).getReg();
2811 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
2812 return HexagonII::HCG_A;
2814 case Hexagon::A2_tfrsi:
2816 // Do not test for #u6 size since the const is getting extended
2817 // regardless and compound could be formed.
2818 DstReg = MI->getOperand(0).getReg();
2819 if (isIntRegForSubInst(DstReg))
2820 return HexagonII::HCG_A;
2822 case Hexagon::S2_tstbit_i:
2823 DstReg = MI->getOperand(0).getReg();
2824 Src1Reg = MI->getOperand(1).getReg();
2825 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
2826 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
2827 MI->getOperand(2).isImm() &&
2828 isIntRegForSubInst(Src1Reg) && (MI->getOperand(2).getImm() == 0))
2829 return HexagonII::HCG_A;
2831 // The fact that .new form is used pretty much guarantees
2832 // that predicate register will match. Nevertheless,
2833 // there could be some false positives without additional
2835 case Hexagon::J2_jumptnew:
2836 case Hexagon::J2_jumpfnew:
2837 case Hexagon::J2_jumptnewpt:
2838 case Hexagon::J2_jumpfnewpt:
2839 Src1Reg = MI->getOperand(0).getReg();
2840 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
2841 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
2842 return HexagonII::HCG_B;
2844 // Transfer and jump:
2845 // Rd=#U6 ; jump #r9:2
2846 // Rd=Rs ; jump #r9:2
2847 // Do not test for jump range here.
2848 case Hexagon::J2_jump:
2849 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
2850 return HexagonII::HCG_C;
2854 return HexagonII::HCG_None;
2858 // Returns -1 when there is no opcode found.
2859 unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr *GA,
2860 const MachineInstr *GB) const {
2861 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
2862 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
2863 if ((GA->getOpcode() != Hexagon::C2_cmpeqi) ||
2864 (GB->getOpcode() != Hexagon::J2_jumptnew))
2866 unsigned DestReg = GA->getOperand(0).getReg();
2867 if (!GB->readsRegister(DestReg))
2869 if (DestReg == Hexagon::P0)
2870 return Hexagon::J4_cmpeqi_tp0_jump_nt;
2871 if (DestReg == Hexagon::P1)
2872 return Hexagon::J4_cmpeqi_tp1_jump_nt;
2877 int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
2878 enum Hexagon::PredSense inPredSense;
2879 inPredSense = invertPredicate ? Hexagon::PredSense_false :
2880 Hexagon::PredSense_true;
2881 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
2882 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
2885 // This switch case will be removed once all the instructions have been
2886 // modified to use relation maps.
2888 case Hexagon::TFRI_f:
2889 return !invertPredicate ? Hexagon::TFRI_cPt_f :
2890 Hexagon::TFRI_cNotPt_f;
2893 llvm_unreachable("Unexpected predicable instruction");
2897 // Return the cur value instruction for a given store.
2898 int HexagonInstrInfo::getDotCurOp(const MachineInstr* MI) const {
2899 switch (MI->getOpcode()) {
2900 default: llvm_unreachable("Unknown .cur type");
2901 case Hexagon::V6_vL32b_pi:
2902 return Hexagon::V6_vL32b_cur_pi;
2903 case Hexagon::V6_vL32b_ai:
2904 return Hexagon::V6_vL32b_cur_ai;
2906 case Hexagon::V6_vL32b_pi_128B:
2907 return Hexagon::V6_vL32b_cur_pi_128B;
2908 case Hexagon::V6_vL32b_ai_128B:
2909 return Hexagon::V6_vL32b_cur_ai_128B;
2916 // The diagram below shows the steps involved in the conversion of a predicated
2917 // store instruction to its .new predicated new-value form.
2919 // p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
2921 // / \ (not OK. it will cause new-value store to be
2922 // / X conditional on p0.new while R2 producer is
2925 // p.new store p.old NV store
2926 // [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
2932 // [if (p0)memw(R0+#0)=R2]
2935 // The following set of instructions further explains the scenario where
2936 // conditional new-value store becomes invalid when promoted to .new predicate
2939 // { 1) if (p0) r0 = add(r1, r2)
2940 // 2) p0 = cmp.eq(r3, #0) }
2942 // 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
2943 // the first two instructions because in instr 1, r0 is conditional on old value
2944 // of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
2945 // is not valid for new-value stores.
2946 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
2947 // from the "Conditional Store" list. Because a predicated new value store
2948 // would NOT be promoted to a double dot new store. See diagram below:
2949 // This function returns yes for those stores that are predicated but not
2950 // yet promoted to predicate dot new instructions.
2952 // +---------------------+
2953 // /-----| if (p0) memw(..)=r0 |---------\~
2954 // || +---------------------+ ||
2955 // promote || /\ /\ || promote
2957 // \||/ demote || \||/
2959 // +-------------------------+ || +-------------------------+
2960 // | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
2961 // +-------------------------+ || +-------------------------+
2964 // promote || \/ NOT possible
2968 // +-----------------------------+
2969 // | if (p0.new) memw(..)=r0.new |
2970 // +-----------------------------+
2971 // Double Dot New Store
2973 // Returns the most basic instruction for the .new predicated instructions and
2974 // new-value stores.
2975 // For example, all of the following instructions will be converted back to the
2976 // same instruction:
2977 // 1) if (p0.new) memw(R0+#0) = R1.new --->
2978 // 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
2979 // 3) if (p0.new) memw(R0+#0) = R1 --->
2981 // To understand the translation of instruction 1 to its original form, consider
2982 // a packet with 3 instructions.
2983 // { p0 = cmp.eq(R0,R1)
2984 // if (p0.new) R2 = add(R3, R4)
2985 // R5 = add (R3, R1)
2987 // if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
2989 // This instruction can be part of the previous packet only if both p0 and R2
2990 // are promoted to .new values. This promotion happens in steps, first
2991 // predicate register is promoted to .new and in the next iteration R2 is
2992 // promoted. Therefore, in case of dependence check failure (due to R5) during
2993 // next iteration, it should be converted back to its most basic form.
2996 // Return the new value instruction for a given store.
2997 int HexagonInstrInfo::getDotNewOp(const MachineInstr* MI) const {
2998 int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
2999 if (NVOpcode >= 0) // Valid new-value store instruction.
3002 switch (MI->getOpcode()) {
3003 default: llvm_unreachable("Unknown .new type");
3004 case Hexagon::S4_storerb_ur:
3005 return Hexagon::S4_storerbnew_ur;
3007 case Hexagon::S2_storerb_pci:
3008 return Hexagon::S2_storerb_pci;
3010 case Hexagon::S2_storeri_pci:
3011 return Hexagon::S2_storeri_pci;
3013 case Hexagon::S2_storerh_pci:
3014 return Hexagon::S2_storerh_pci;
3016 case Hexagon::S2_storerd_pci:
3017 return Hexagon::S2_storerd_pci;
3019 case Hexagon::S2_storerf_pci:
3020 return Hexagon::S2_storerf_pci;
3022 case Hexagon::V6_vS32b_ai:
3023 return Hexagon::V6_vS32b_new_ai;
3025 case Hexagon::V6_vS32b_pi:
3026 return Hexagon::V6_vS32b_new_pi;
3029 case Hexagon::V6_vS32b_ai_128B:
3030 return Hexagon::V6_vS32b_new_ai_128B;
3032 case Hexagon::V6_vS32b_pi_128B:
3033 return Hexagon::V6_vS32b_new_pi_128B;
3038 // Returns the opcode to use when converting MI, which is a conditional jump,
3039 // into a conditional instruction which uses the .new value of the predicate.
3040 // We also use branch probabilities to add a hint to the jump.
3041 int HexagonInstrInfo::getDotNewPredJumpOp(MachineInstr *MI,
3042 const MachineBranchProbabilityInfo *MBPI) const {
3043 // We assume that block can have at most two successors.
3045 MachineBasicBlock *Src = MI->getParent();
3046 MachineOperand *BrTarget = &MI->getOperand(1);
3047 MachineBasicBlock *Dst = BrTarget->getMBB();
3049 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
3050 if (Prediction >= BranchProbability(1,2))
3053 switch (MI->getOpcode()) {
3054 case Hexagon::J2_jumpt:
3055 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3056 case Hexagon::J2_jumpf:
3057 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3060 llvm_unreachable("Unexpected jump instruction.");
3065 // Return .new predicate version for an instruction.
3066 int HexagonInstrInfo::getDotNewPredOp(MachineInstr *MI,
3067 const MachineBranchProbabilityInfo *MBPI) const {
3068 int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
3069 if (NewOpcode >= 0) // Valid predicate new instruction
3072 switch (MI->getOpcode()) {
3074 case Hexagon::J2_jumpt:
3075 case Hexagon::J2_jumpf:
3076 return getDotNewPredJumpOp(MI, MBPI);
3079 assert(0 && "Unknown .new type");
3085 int HexagonInstrInfo::getDotOldOp(const int opc) const {
3087 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3088 NewOp = Hexagon::getPredOldOpcode(NewOp);
3089 assert(NewOp >= 0 &&
3090 "Couldn't change predicate new instruction to its old form.");
3093 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3094 NewOp = Hexagon::getNonNVStore(NewOp);
3095 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3101 // See if instruction could potentially be a duplex candidate.
3102 // If so, return its group. Zero otherwise.
3103 HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
3104 const MachineInstr *MI) const {
3105 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3106 auto &HRI = getRegisterInfo();
3108 switch (MI->getOpcode()) {
3110 return HexagonII::HSIG_None;
3114 // Rd = memw(Rs+#u4:2)
3115 // Rd = memub(Rs+#u4:0)
3116 case Hexagon::L2_loadri_io:
3117 DstReg = MI->getOperand(0).getReg();
3118 SrcReg = MI->getOperand(1).getReg();
3119 // Special case this one from Group L2.
3120 // Rd = memw(r29+#u5:2)
3121 if (isIntRegForSubInst(DstReg)) {
3122 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3123 HRI.getStackRegister() == SrcReg &&
3124 MI->getOperand(2).isImm() &&
3125 isShiftedUInt<5,2>(MI->getOperand(2).getImm()))
3126 return HexagonII::HSIG_L2;
3127 // Rd = memw(Rs+#u4:2)
3128 if (isIntRegForSubInst(SrcReg) &&
3129 (MI->getOperand(2).isImm() &&
3130 isShiftedUInt<4,2>(MI->getOperand(2).getImm())))
3131 return HexagonII::HSIG_L1;
3134 case Hexagon::L2_loadrub_io:
3135 // Rd = memub(Rs+#u4:0)
3136 DstReg = MI->getOperand(0).getReg();
3137 SrcReg = MI->getOperand(1).getReg();
3138 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3139 MI->getOperand(2).isImm() && isUInt<4>(MI->getOperand(2).getImm()))
3140 return HexagonII::HSIG_L1;
3145 // Rd = memh/memuh(Rs+#u3:1)
3146 // Rd = memb(Rs+#u3:0)
3147 // Rd = memw(r29+#u5:2) - Handled above.
3148 // Rdd = memd(r29+#u5:3)
3150 // [if ([!]p0[.new])] dealloc_return
3151 // [if ([!]p0[.new])] jumpr r31
3152 case Hexagon::L2_loadrh_io:
3153 case Hexagon::L2_loadruh_io:
3154 // Rd = memh/memuh(Rs+#u3:1)
3155 DstReg = MI->getOperand(0).getReg();
3156 SrcReg = MI->getOperand(1).getReg();
3157 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3158 MI->getOperand(2).isImm() &&
3159 isShiftedUInt<3,1>(MI->getOperand(2).getImm()))
3160 return HexagonII::HSIG_L2;
3162 case Hexagon::L2_loadrb_io:
3163 // Rd = memb(Rs+#u3:0)
3164 DstReg = MI->getOperand(0).getReg();
3165 SrcReg = MI->getOperand(1).getReg();
3166 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3167 MI->getOperand(2).isImm() &&
3168 isUInt<3>(MI->getOperand(2).getImm()))
3169 return HexagonII::HSIG_L2;
3171 case Hexagon::L2_loadrd_io:
3172 // Rdd = memd(r29+#u5:3)
3173 DstReg = MI->getOperand(0).getReg();
3174 SrcReg = MI->getOperand(1).getReg();
3175 if (isDblRegForSubInst(DstReg, HRI) &&
3176 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3177 HRI.getStackRegister() == SrcReg &&
3178 MI->getOperand(2).isImm() &&
3179 isShiftedUInt<5,3>(MI->getOperand(2).getImm()))
3180 return HexagonII::HSIG_L2;
3182 // dealloc_return is not documented in Hexagon Manual, but marked
3183 // with A_SUBINSN attribute in iset_v4classic.py.
3184 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3185 case Hexagon::L4_return:
3186 case Hexagon::L2_deallocframe:
3187 return HexagonII::HSIG_L2;
3188 case Hexagon::EH_RETURN_JMPR:
3189 case Hexagon::JMPret :
3191 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
3192 DstReg = MI->getOperand(0).getReg();
3193 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3194 return HexagonII::HSIG_L2;
3196 case Hexagon::JMPrett:
3197 case Hexagon::JMPretf:
3198 case Hexagon::JMPrettnewpt:
3199 case Hexagon::JMPretfnewpt :
3200 case Hexagon::JMPrettnew :
3201 case Hexagon::JMPretfnew :
3202 DstReg = MI->getOperand(1).getReg();
3203 SrcReg = MI->getOperand(0).getReg();
3204 // [if ([!]p0[.new])] jumpr r31
3205 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3206 (Hexagon::P0 == SrcReg)) &&
3207 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3208 return HexagonII::HSIG_L2;
3210 case Hexagon::L4_return_t :
3211 case Hexagon::L4_return_f :
3212 case Hexagon::L4_return_tnew_pnt :
3213 case Hexagon::L4_return_fnew_pnt :
3214 case Hexagon::L4_return_tnew_pt :
3215 case Hexagon::L4_return_fnew_pt :
3216 // [if ([!]p0[.new])] dealloc_return
3217 SrcReg = MI->getOperand(0).getReg();
3218 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3219 return HexagonII::HSIG_L2;
3224 // memw(Rs+#u4:2) = Rt
3225 // memb(Rs+#u4:0) = Rt
3226 case Hexagon::S2_storeri_io:
3227 // Special case this one from Group S2.
3228 // memw(r29+#u5:2) = Rt
3229 Src1Reg = MI->getOperand(0).getReg();
3230 Src2Reg = MI->getOperand(2).getReg();
3231 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3232 isIntRegForSubInst(Src2Reg) &&
3233 HRI.getStackRegister() == Src1Reg && MI->getOperand(1).isImm() &&
3234 isShiftedUInt<5,2>(MI->getOperand(1).getImm()))
3235 return HexagonII::HSIG_S2;
3236 // memw(Rs+#u4:2) = Rt
3237 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3238 MI->getOperand(1).isImm() &&
3239 isShiftedUInt<4,2>(MI->getOperand(1).getImm()))
3240 return HexagonII::HSIG_S1;
3242 case Hexagon::S2_storerb_io:
3243 // memb(Rs+#u4:0) = Rt
3244 Src1Reg = MI->getOperand(0).getReg();
3245 Src2Reg = MI->getOperand(2).getReg();
3246 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3247 MI->getOperand(1).isImm() && isUInt<4>(MI->getOperand(1).getImm()))
3248 return HexagonII::HSIG_S1;
3253 // memh(Rs+#u3:1) = Rt
3254 // memw(r29+#u5:2) = Rt
3255 // memd(r29+#s6:3) = Rtt
3256 // memw(Rs+#u4:2) = #U1
3257 // memb(Rs+#u4) = #U1
3258 // allocframe(#u5:3)
3259 case Hexagon::S2_storerh_io:
3260 // memh(Rs+#u3:1) = Rt
3261 Src1Reg = MI->getOperand(0).getReg();
3262 Src2Reg = MI->getOperand(2).getReg();
3263 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3264 MI->getOperand(1).isImm() &&
3265 isShiftedUInt<3,1>(MI->getOperand(1).getImm()))
3266 return HexagonII::HSIG_S1;
3268 case Hexagon::S2_storerd_io:
3269 // memd(r29+#s6:3) = Rtt
3270 Src1Reg = MI->getOperand(0).getReg();
3271 Src2Reg = MI->getOperand(2).getReg();
3272 if (isDblRegForSubInst(Src2Reg, HRI) &&
3273 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3274 HRI.getStackRegister() == Src1Reg && MI->getOperand(1).isImm() &&
3275 isShiftedInt<6,3>(MI->getOperand(1).getImm()))
3276 return HexagonII::HSIG_S2;
3278 case Hexagon::S4_storeiri_io:
3279 // memw(Rs+#u4:2) = #U1
3280 Src1Reg = MI->getOperand(0).getReg();
3281 if (isIntRegForSubInst(Src1Reg) && MI->getOperand(1).isImm() &&
3282 isShiftedUInt<4,2>(MI->getOperand(1).getImm()) &&
3283 MI->getOperand(2).isImm() && isUInt<1>(MI->getOperand(2).getImm()))
3284 return HexagonII::HSIG_S2;
3286 case Hexagon::S4_storeirb_io:
3287 // memb(Rs+#u4) = #U1
3288 Src1Reg = MI->getOperand(0).getReg();
3289 if (isIntRegForSubInst(Src1Reg) && MI->getOperand(1).isImm() &&
3290 isUInt<4>(MI->getOperand(1).getImm()) && MI->getOperand(2).isImm() &&
3291 MI->getOperand(2).isImm() && isUInt<1>(MI->getOperand(2).getImm()))
3292 return HexagonII::HSIG_S2;
3294 case Hexagon::S2_allocframe:
3295 if (MI->getOperand(0).isImm() &&
3296 isShiftedUInt<5,3>(MI->getOperand(0).getImm()))
3297 return HexagonII::HSIG_S1;
3306 // if ([!]P0[.new]) Rd = #0
3307 // Rd = add(r29,#u6:2)
3309 // P0 = cmp.eq(Rs,#u2)
3310 // Rdd = combine(#0,Rs)
3311 // Rdd = combine(Rs,#0)
3312 // Rdd = combine(#u2,#U2)
3315 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3317 case Hexagon::A2_addi:
3318 DstReg = MI->getOperand(0).getReg();
3319 SrcReg = MI->getOperand(1).getReg();
3320 if (isIntRegForSubInst(DstReg)) {
3321 // Rd = add(r29,#u6:2)
3322 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3323 HRI.getStackRegister() == SrcReg && MI->getOperand(2).isImm() &&
3324 isShiftedUInt<6,2>(MI->getOperand(2).getImm()))
3325 return HexagonII::HSIG_A;
3327 if ((DstReg == SrcReg) && MI->getOperand(2).isImm() &&
3328 isInt<7>(MI->getOperand(2).getImm()))
3329 return HexagonII::HSIG_A;
3332 if (isIntRegForSubInst(SrcReg) && MI->getOperand(2).isImm() &&
3333 ((MI->getOperand(2).getImm() == 1) ||
3334 (MI->getOperand(2).getImm() == -1)))
3335 return HexagonII::HSIG_A;
3338 case Hexagon::A2_add:
3340 DstReg = MI->getOperand(0).getReg();
3341 Src1Reg = MI->getOperand(1).getReg();
3342 Src2Reg = MI->getOperand(2).getReg();
3343 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3344 isIntRegForSubInst(Src2Reg))
3345 return HexagonII::HSIG_A;
3347 case Hexagon::A2_andir:
3349 // Rd16=and(Rs16,#255)
3350 // Rd16=and(Rs16,#1)
3351 DstReg = MI->getOperand(0).getReg();
3352 SrcReg = MI->getOperand(1).getReg();
3353 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3354 MI->getOperand(2).isImm() &&
3355 ((MI->getOperand(2).getImm() == 1) ||
3356 (MI->getOperand(2).getImm() == 255)))
3357 return HexagonII::HSIG_A;
3359 case Hexagon::A2_tfr:
3361 DstReg = MI->getOperand(0).getReg();
3362 SrcReg = MI->getOperand(1).getReg();
3363 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3364 return HexagonII::HSIG_A;
3366 case Hexagon::A2_tfrsi:
3368 // Do not test for #u6 size since the const is getting extended
3369 // regardless and compound could be formed.
3371 DstReg = MI->getOperand(0).getReg();
3372 if (isIntRegForSubInst(DstReg))
3373 return HexagonII::HSIG_A;
3375 case Hexagon::C2_cmoveit:
3376 case Hexagon::C2_cmovenewit:
3377 case Hexagon::C2_cmoveif:
3378 case Hexagon::C2_cmovenewif:
3379 // if ([!]P0[.new]) Rd = #0
3381 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
3382 DstReg = MI->getOperand(0).getReg();
3383 SrcReg = MI->getOperand(1).getReg();
3384 if (isIntRegForSubInst(DstReg) &&
3385 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
3386 MI->getOperand(2).isImm() && MI->getOperand(2).getImm() == 0)
3387 return HexagonII::HSIG_A;
3389 case Hexagon::C2_cmpeqi:
3390 // P0 = cmp.eq(Rs,#u2)
3391 DstReg = MI->getOperand(0).getReg();
3392 SrcReg = MI->getOperand(1).getReg();
3393 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3394 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
3395 MI->getOperand(2).isImm() && isUInt<2>(MI->getOperand(2).getImm()))
3396 return HexagonII::HSIG_A;
3398 case Hexagon::A2_combineii:
3399 case Hexagon::A4_combineii:
3400 // Rdd = combine(#u2,#U2)
3401 DstReg = MI->getOperand(0).getReg();
3402 if (isDblRegForSubInst(DstReg, HRI) &&
3403 ((MI->getOperand(1).isImm() && isUInt<2>(MI->getOperand(1).getImm())) ||
3404 (MI->getOperand(1).isGlobal() &&
3405 isUInt<2>(MI->getOperand(1).getOffset()))) &&
3406 ((MI->getOperand(2).isImm() && isUInt<2>(MI->getOperand(2).getImm())) ||
3407 (MI->getOperand(2).isGlobal() &&
3408 isUInt<2>(MI->getOperand(2).getOffset()))))
3409 return HexagonII::HSIG_A;
3411 case Hexagon::A4_combineri:
3412 // Rdd = combine(Rs,#0)
3413 DstReg = MI->getOperand(0).getReg();
3414 SrcReg = MI->getOperand(1).getReg();
3415 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
3416 ((MI->getOperand(2).isImm() && MI->getOperand(2).getImm() == 0) ||
3417 (MI->getOperand(2).isGlobal() && MI->getOperand(2).getOffset() == 0)))
3418 return HexagonII::HSIG_A;
3420 case Hexagon::A4_combineir:
3421 // Rdd = combine(#0,Rs)
3422 DstReg = MI->getOperand(0).getReg();
3423 SrcReg = MI->getOperand(2).getReg();
3424 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
3425 ((MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0) ||
3426 (MI->getOperand(1).isGlobal() && MI->getOperand(1).getOffset() == 0)))
3427 return HexagonII::HSIG_A;
3429 case Hexagon::A2_sxtb:
3430 case Hexagon::A2_sxth:
3431 case Hexagon::A2_zxtb:
3432 case Hexagon::A2_zxth:
3433 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3434 DstReg = MI->getOperand(0).getReg();
3435 SrcReg = MI->getOperand(1).getReg();
3436 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3437 return HexagonII::HSIG_A;
3441 return HexagonII::HSIG_None;
3445 short HexagonInstrInfo::getEquivalentHWInstr(MachineInstr *MI) const {
3446 return Hexagon::getRealHWInstr(MI->getOpcode(), Hexagon::InstrType_Real);
3450 // Return first non-debug instruction in the basic block.
3451 MachineInstr *HexagonInstrInfo::getFirstNonDbgInst(MachineBasicBlock *BB)
3453 for (auto MII = BB->instr_begin(), End = BB->instr_end(); MII != End; MII++) {
3454 MachineInstr *MI = &*MII;
3455 if (MI->isDebugValue())
3463 unsigned HexagonInstrInfo::getInstrTimingClassLatency(
3464 const InstrItineraryData *ItinData, const MachineInstr *MI) const {
3465 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3466 // still have a MinLatency property, which getStageLatency checks.
3468 return getInstrLatency(ItinData, MI);
3470 // Get the latency embedded in the itinerary. If we're not using timing class
3471 // latencies or if we using BSB scheduling, then restrict the maximum latency
3472 // to 1 (that is, either 0 or 1).
3473 if (MI->isTransient())
3475 unsigned Latency = ItinData->getStageLatency(MI->getDesc().getSchedClass());
3476 if (!EnableTimingClassLatency ||
3477 MI->getParent()->getParent()->getSubtarget<HexagonSubtarget>().
3485 // inverts the predication logic.
3488 bool HexagonInstrInfo::getInvertedPredSense(
3489 SmallVectorImpl<MachineOperand> &Cond) const {
3492 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
3493 Cond[0].setImm(Opc);
3498 unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
3500 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
3501 : Hexagon::getTruePredOpcode(Opc);
3502 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
3503 return InvPredOpcode;
3505 llvm_unreachable("Unexpected predicated instruction");
3509 // Returns the max value that doesn't need to be extended.
3510 int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
3511 const uint64_t F = MI->getDesc().TSFlags;
3512 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3513 & HexagonII::ExtentSignedMask;
3514 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3515 & HexagonII::ExtentBitsMask;
3517 if (isSigned) // if value is signed
3518 return ~(-1U << (bits - 1));
3520 return ~(-1U << bits);
3524 unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr* MI) const {
3525 const uint64_t F = MI->getDesc().TSFlags;
3526 return (F >> HexagonII::MemAccessSizePos) & HexagonII::MemAccesSizeMask;
3530 // Returns the min value that doesn't need to be extended.
3531 int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
3532 const uint64_t F = MI->getDesc().TSFlags;
3533 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3534 & HexagonII::ExtentSignedMask;
3535 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3536 & HexagonII::ExtentBitsMask;
3538 if (isSigned) // if value is signed
3539 return -1U << (bits - 1);
3545 // Returns opcode of the non-extended equivalent instruction.
3546 short HexagonInstrInfo::getNonExtOpcode(const MachineInstr *MI) const {
3547 // Check if the instruction has a register form that uses register in place
3548 // of the extended operand, if so return that as the non-extended form.
3549 short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
3550 if (NonExtOpcode >= 0)
3551 return NonExtOpcode;
3553 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
3554 // Check addressing mode and retrieve non-ext equivalent instruction.
3555 switch (getAddrMode(MI)) {
3556 case HexagonII::Absolute :
3557 return Hexagon::getBaseWithImmOffset(MI->getOpcode());
3558 case HexagonII::BaseImmOffset :
3559 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
3560 case HexagonII::BaseLongOffset:
3561 return Hexagon::getRegShlForm(MI->getOpcode());
3571 bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
3572 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
3575 assert(Cond.size() == 2);
3576 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
3577 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
3580 PredReg = Cond[1].getReg();
3582 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
3584 if (Cond[1].isImplicit())
3585 PredRegFlags = RegState::Implicit;
3586 if (Cond[1].isUndef())
3587 PredRegFlags |= RegState::Undef;
3592 short HexagonInstrInfo::getPseudoInstrPair(MachineInstr *MI) const {
3593 return Hexagon::getRealHWInstr(MI->getOpcode(), Hexagon::InstrType_Pseudo);
3597 short HexagonInstrInfo::getRegForm(const MachineInstr *MI) const {
3598 return Hexagon::getRegForm(MI->getOpcode());
3602 // Return the number of bytes required to encode the instruction.
3603 // Hexagon instructions are fixed length, 4 bytes, unless they
3604 // use a constant extender, which requires another 4 bytes.
3605 // For debug instructions and prolog labels, return 0.
3606 unsigned HexagonInstrInfo::getSize(const MachineInstr *MI) const {
3607 if (MI->isDebugValue() || MI->isPosition())
3610 unsigned Size = MI->getDesc().getSize();
3612 // Assume the default insn size in case it cannot be determined
3613 // for whatever reason.
3614 Size = HEXAGON_INSTR_SIZE;
3616 if (isConstExtended(MI) || isExtended(MI))
3617 Size += HEXAGON_INSTR_SIZE;
3619 // Try and compute number of instructions in asm.
3620 if (BranchRelaxAsmLarge && MI->getOpcode() == Hexagon::INLINEASM) {
3621 const MachineBasicBlock &MBB = *MI->getParent();
3622 const MachineFunction *MF = MBB.getParent();
3623 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
3625 // Count the number of register definitions to find the asm string.
3626 unsigned NumDefs = 0;
3627 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
3629 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
3631 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
3632 // Disassemble the AsmStr and approximate number of instructions.
3633 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
3634 Size = getInlineAsmLength(AsmStr, *MAI);
3641 uint64_t HexagonInstrInfo::getType(const MachineInstr* MI) const {
3642 const uint64_t F = MI->getDesc().TSFlags;
3643 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
3647 unsigned HexagonInstrInfo::getUnits(const MachineInstr* MI) const {
3648 const TargetSubtargetInfo &ST = MI->getParent()->getParent()->getSubtarget();
3649 const InstrItineraryData &II = *ST.getInstrItineraryData();
3650 const InstrStage &IS = *II.beginStage(MI->getDesc().getSchedClass());
3652 return IS.getUnits();
3656 unsigned HexagonInstrInfo::getValidSubTargets(const unsigned Opcode) const {
3657 const uint64_t F = get(Opcode).TSFlags;
3658 return (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask;
3662 // Calculate size of the basic block without debug instructions.
3663 unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
3664 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
3668 unsigned HexagonInstrInfo::nonDbgBundleSize(
3669 MachineBasicBlock::const_iterator BundleHead) const {
3670 assert(BundleHead->isBundle() && "Not a bundle header");
3671 auto MII = BundleHead.getInstrIterator();
3672 // Skip the bundle header.
3673 return nonDbgMICount(++MII, getBundleEnd(BundleHead));
3677 /// immediateExtend - Changes the instruction in place to one using an immediate
3679 void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
3680 assert((isExtendable(MI)||isConstExtended(MI)) &&
3681 "Instruction must be extendable");
3682 // Find which operand is extendable.
3683 short ExtOpNum = getCExtOpNum(MI);
3684 MachineOperand &MO = MI->getOperand(ExtOpNum);
3685 // This needs to be something we understand.
3686 assert((MO.isMBB() || MO.isImm()) &&
3687 "Branch with unknown extendable field type");
3688 // Mark given operand as extended.
3689 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
3693 bool HexagonInstrInfo::invertAndChangeJumpTarget(
3694 MachineInstr* MI, MachineBasicBlock* NewTarget) const {
3695 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
3696 << NewTarget->getNumber(); MI->dump(););
3697 assert(MI->isBranch());
3698 unsigned NewOpcode = getInvertedPredicatedOpcode(MI->getOpcode());
3699 int TargetPos = MI->getNumOperands() - 1;
3700 // In general branch target is the last operand,
3701 // but some implicit defs added at the end might change it.
3702 while ((TargetPos > -1) && !MI->getOperand(TargetPos).isMBB())
3704 assert((TargetPos >= 0) && MI->getOperand(TargetPos).isMBB());
3705 MI->getOperand(TargetPos).setMBB(NewTarget);
3706 if (EnableBranchPrediction && isPredicatedNew(MI)) {
3707 NewOpcode = reversePrediction(NewOpcode);
3709 MI->setDesc(get(NewOpcode));
3714 void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
3715 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
3716 MachineFunction::iterator A = MF.begin();
3717 MachineBasicBlock &B = *A;
3718 MachineBasicBlock::iterator I = B.begin();
3719 MachineInstr *MI = &*I;
3720 DebugLoc DL = MI->getDebugLoc();
3721 MachineInstr *NewMI;
3723 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
3724 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
3725 NewMI = BuildMI(B, MI, DL, get(insn));
3726 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
3727 " Class: " << NewMI->getDesc().getSchedClass());
3728 NewMI->eraseFromParent();
3730 /* --- The code above is used to generate complete set of Hexagon Insn --- */
3734 // inverts the predication logic.
3737 bool HexagonInstrInfo::reversePredSense(MachineInstr* MI) const {
3738 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI->dump());
3739 MI->setDesc(get(getInvertedPredicatedOpcode(MI->getOpcode())));
3744 // Reverse the branch prediction.
3745 unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
3746 int PredRevOpcode = -1;
3747 if (isPredictedTaken(Opcode))
3748 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
3750 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
3751 assert(PredRevOpcode > 0);
3752 return PredRevOpcode;
3756 // TODO: Add more rigorous validation.
3757 bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
3759 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));