[Alignment][NFC] migrate DataLayout internal struct to llvm::Align
[llvm-core.git] / lib / Target / Hexagon / HexagonInstrInfo.cpp
blobf9d464b0797b8fe7bbcfb72ef7d19d3a211a0f13
1 //===- HexagonInstrInfo.cpp - Hexagon Instruction Information -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the Hexagon implementation of the TargetInstrInfo class.
11 //===----------------------------------------------------------------------===//
13 #include "HexagonInstrInfo.h"
14 #include "Hexagon.h"
15 #include "HexagonFrameLowering.h"
16 #include "HexagonHazardRecognizer.h"
17 #include "HexagonRegisterInfo.h"
18 #include "HexagonSubtarget.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/CodeGen/DFAPacketizer.h"
24 #include "llvm/CodeGen/LivePhysRegs.h"
25 #include "llvm/CodeGen/MachineBasicBlock.h"
26 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "llvm/CodeGen/MachineInstrBuilder.h"
31 #include "llvm/CodeGen/MachineInstrBundle.h"
32 #include "llvm/CodeGen/MachineLoopInfo.h"
33 #include "llvm/CodeGen/MachineMemOperand.h"
34 #include "llvm/CodeGen/MachineOperand.h"
35 #include "llvm/CodeGen/MachineRegisterInfo.h"
36 #include "llvm/CodeGen/ScheduleDAG.h"
37 #include "llvm/CodeGen/TargetInstrInfo.h"
38 #include "llvm/CodeGen/TargetOpcodes.h"
39 #include "llvm/CodeGen/TargetRegisterInfo.h"
40 #include "llvm/CodeGen/TargetSubtargetInfo.h"
41 #include "llvm/IR/DebugLoc.h"
42 #include "llvm/MC/MCAsmInfo.h"
43 #include "llvm/MC/MCInstrDesc.h"
44 #include "llvm/MC/MCInstrItineraries.h"
45 #include "llvm/MC/MCRegisterInfo.h"
46 #include "llvm/Support/BranchProbability.h"
47 #include "llvm/Support/CommandLine.h"
48 #include "llvm/Support/Debug.h"
49 #include "llvm/Support/ErrorHandling.h"
50 #include "llvm/Support/MachineValueType.h"
51 #include "llvm/Support/MathExtras.h"
52 #include "llvm/Support/raw_ostream.h"
53 #include "llvm/Target/TargetMachine.h"
54 #include <cassert>
55 #include <cctype>
56 #include <cstdint>
57 #include <cstring>
58 #include <iterator>
59 #include <string>
60 #include <utility>
62 using namespace llvm;
64 #define DEBUG_TYPE "hexagon-instrinfo"
66 #define GET_INSTRINFO_CTOR_DTOR
67 #define GET_INSTRMAP_INFO
68 #include "HexagonDepTimingClasses.h"
69 #include "HexagonGenDFAPacketizer.inc"
70 #include "HexagonGenInstrInfo.inc"
72 cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
73 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
74 "packetization boundary."));
76 static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
77 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
79 static cl::opt<bool> DisableNVSchedule("disable-hexagon-nv-schedule",
80 cl::Hidden, cl::ZeroOrMore, cl::init(false),
81 cl::desc("Disable schedule adjustment for new value stores."));
83 static cl::opt<bool> EnableTimingClassLatency(
84 "enable-timing-class-latency", cl::Hidden, cl::init(false),
85 cl::desc("Enable timing class latency"));
87 static cl::opt<bool> EnableALUForwarding(
88 "enable-alu-forwarding", cl::Hidden, cl::init(true),
89 cl::desc("Enable vec alu forwarding"));
91 static cl::opt<bool> EnableACCForwarding(
92 "enable-acc-forwarding", cl::Hidden, cl::init(true),
93 cl::desc("Enable vec acc forwarding"));
95 static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
96 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
98 static cl::opt<bool> UseDFAHazardRec("dfa-hazard-rec",
99 cl::init(true), cl::Hidden, cl::ZeroOrMore,
100 cl::desc("Use the DFA based hazard recognizer."));
102 /// Constants for Hexagon instructions.
103 const int Hexagon_MEMW_OFFSET_MAX = 4095;
104 const int Hexagon_MEMW_OFFSET_MIN = -4096;
105 const int Hexagon_MEMD_OFFSET_MAX = 8191;
106 const int Hexagon_MEMD_OFFSET_MIN = -8192;
107 const int Hexagon_MEMH_OFFSET_MAX = 2047;
108 const int Hexagon_MEMH_OFFSET_MIN = -2048;
109 const int Hexagon_MEMB_OFFSET_MAX = 1023;
110 const int Hexagon_MEMB_OFFSET_MIN = -1024;
111 const int Hexagon_ADDI_OFFSET_MAX = 32767;
112 const int Hexagon_ADDI_OFFSET_MIN = -32768;
114 // Pin the vtable to this file.
115 void HexagonInstrInfo::anchor() {}
117 HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
118 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
119 Subtarget(ST) {}
121 static bool isIntRegForSubInst(unsigned Reg) {
122 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
123 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
126 static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
127 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_lo)) &&
128 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_hi));
131 /// Calculate number of instructions excluding the debug instructions.
132 static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
133 MachineBasicBlock::const_instr_iterator MIE) {
134 unsigned Count = 0;
135 for (; MIB != MIE; ++MIB) {
136 if (!MIB->isDebugInstr())
137 ++Count;
139 return Count;
142 /// Find the hardware loop instruction used to set-up the specified loop.
143 /// On Hexagon, we have two instructions used to set-up the hardware loop
144 /// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
145 /// to indicate the end of a loop.
146 MachineInstr *HexagonInstrInfo::findLoopInstr(MachineBasicBlock *BB,
147 unsigned EndLoopOp, MachineBasicBlock *TargetBB,
148 SmallPtrSet<MachineBasicBlock *, 8> &Visited) const {
149 unsigned LOOPi;
150 unsigned LOOPr;
151 if (EndLoopOp == Hexagon::ENDLOOP0) {
152 LOOPi = Hexagon::J2_loop0i;
153 LOOPr = Hexagon::J2_loop0r;
154 } else { // EndLoopOp == Hexagon::EndLOOP1
155 LOOPi = Hexagon::J2_loop1i;
156 LOOPr = Hexagon::J2_loop1r;
159 // The loop set-up instruction will be in a predecessor block
160 for (MachineBasicBlock *PB : BB->predecessors()) {
161 // If this has been visited, already skip it.
162 if (!Visited.insert(PB).second)
163 continue;
164 if (PB == BB)
165 continue;
166 for (auto I = PB->instr_rbegin(), E = PB->instr_rend(); I != E; ++I) {
167 unsigned Opc = I->getOpcode();
168 if (Opc == LOOPi || Opc == LOOPr)
169 return &*I;
170 // We've reached a different loop, which means the loop01 has been
171 // removed.
172 if (Opc == EndLoopOp && I->getOperand(0).getMBB() != TargetBB)
173 return nullptr;
175 // Check the predecessors for the LOOP instruction.
176 if (MachineInstr *Loop = findLoopInstr(PB, EndLoopOp, TargetBB, Visited))
177 return Loop;
179 return nullptr;
182 /// Gather register def/uses from MI.
183 /// This treats possible (predicated) defs as actually happening ones
184 /// (conservatively).
185 static inline void parseOperands(const MachineInstr &MI,
186 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
187 Defs.clear();
188 Uses.clear();
190 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
191 const MachineOperand &MO = MI.getOperand(i);
193 if (!MO.isReg())
194 continue;
196 Register Reg = MO.getReg();
197 if (!Reg)
198 continue;
200 if (MO.isUse())
201 Uses.push_back(MO.getReg());
203 if (MO.isDef())
204 Defs.push_back(MO.getReg());
208 // Position dependent, so check twice for swap.
209 static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
210 switch (Ga) {
211 case HexagonII::HSIG_None:
212 default:
213 return false;
214 case HexagonII::HSIG_L1:
215 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
216 case HexagonII::HSIG_L2:
217 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
218 Gb == HexagonII::HSIG_A);
219 case HexagonII::HSIG_S1:
220 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
221 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
222 case HexagonII::HSIG_S2:
223 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
224 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
225 Gb == HexagonII::HSIG_A);
226 case HexagonII::HSIG_A:
227 return (Gb == HexagonII::HSIG_A);
228 case HexagonII::HSIG_Compound:
229 return (Gb == HexagonII::HSIG_Compound);
231 return false;
234 /// isLoadFromStackSlot - If the specified machine instruction is a direct
235 /// load from a stack slot, return the virtual or physical register number of
236 /// the destination along with the FrameIndex of the loaded stack slot. If
237 /// not, return 0. This predicate must return 0 if the instruction has
238 /// any side effects other than loading from the stack slot.
239 unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
240 int &FrameIndex) const {
241 switch (MI.getOpcode()) {
242 default:
243 break;
244 case Hexagon::L2_loadri_io:
245 case Hexagon::L2_loadrd_io:
246 case Hexagon::V6_vL32b_ai:
247 case Hexagon::V6_vL32b_nt_ai:
248 case Hexagon::V6_vL32Ub_ai:
249 case Hexagon::LDriw_pred:
250 case Hexagon::LDriw_ctr:
251 case Hexagon::PS_vloadrq_ai:
252 case Hexagon::PS_vloadrw_ai:
253 case Hexagon::PS_vloadrw_nt_ai: {
254 const MachineOperand OpFI = MI.getOperand(1);
255 if (!OpFI.isFI())
256 return 0;
257 const MachineOperand OpOff = MI.getOperand(2);
258 if (!OpOff.isImm() || OpOff.getImm() != 0)
259 return 0;
260 FrameIndex = OpFI.getIndex();
261 return MI.getOperand(0).getReg();
264 case Hexagon::L2_ploadrit_io:
265 case Hexagon::L2_ploadrif_io:
266 case Hexagon::L2_ploadrdt_io:
267 case Hexagon::L2_ploadrdf_io: {
268 const MachineOperand OpFI = MI.getOperand(2);
269 if (!OpFI.isFI())
270 return 0;
271 const MachineOperand OpOff = MI.getOperand(3);
272 if (!OpOff.isImm() || OpOff.getImm() != 0)
273 return 0;
274 FrameIndex = OpFI.getIndex();
275 return MI.getOperand(0).getReg();
279 return 0;
282 /// isStoreToStackSlot - If the specified machine instruction is a direct
283 /// store to a stack slot, return the virtual or physical register number of
284 /// the source reg along with the FrameIndex of the loaded stack slot. If
285 /// not, return 0. This predicate must return 0 if the instruction has
286 /// any side effects other than storing to the stack slot.
287 unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
288 int &FrameIndex) const {
289 switch (MI.getOpcode()) {
290 default:
291 break;
292 case Hexagon::S2_storerb_io:
293 case Hexagon::S2_storerh_io:
294 case Hexagon::S2_storeri_io:
295 case Hexagon::S2_storerd_io:
296 case Hexagon::V6_vS32b_ai:
297 case Hexagon::V6_vS32Ub_ai:
298 case Hexagon::STriw_pred:
299 case Hexagon::STriw_ctr:
300 case Hexagon::PS_vstorerq_ai:
301 case Hexagon::PS_vstorerw_ai: {
302 const MachineOperand &OpFI = MI.getOperand(0);
303 if (!OpFI.isFI())
304 return 0;
305 const MachineOperand &OpOff = MI.getOperand(1);
306 if (!OpOff.isImm() || OpOff.getImm() != 0)
307 return 0;
308 FrameIndex = OpFI.getIndex();
309 return MI.getOperand(2).getReg();
312 case Hexagon::S2_pstorerbt_io:
313 case Hexagon::S2_pstorerbf_io:
314 case Hexagon::S2_pstorerht_io:
315 case Hexagon::S2_pstorerhf_io:
316 case Hexagon::S2_pstorerit_io:
317 case Hexagon::S2_pstorerif_io:
318 case Hexagon::S2_pstorerdt_io:
319 case Hexagon::S2_pstorerdf_io: {
320 const MachineOperand &OpFI = MI.getOperand(1);
321 if (!OpFI.isFI())
322 return 0;
323 const MachineOperand &OpOff = MI.getOperand(2);
324 if (!OpOff.isImm() || OpOff.getImm() != 0)
325 return 0;
326 FrameIndex = OpFI.getIndex();
327 return MI.getOperand(3).getReg();
331 return 0;
334 /// This function checks if the instruction or bundle of instructions
335 /// has load from stack slot and returns frameindex and machine memory
336 /// operand of that instruction if true.
337 bool HexagonInstrInfo::hasLoadFromStackSlot(
338 const MachineInstr &MI,
339 SmallVectorImpl<const MachineMemOperand *> &Accesses) const {
340 if (MI.isBundle()) {
341 const MachineBasicBlock *MBB = MI.getParent();
342 MachineBasicBlock::const_instr_iterator MII = MI.getIterator();
343 for (++MII; MII != MBB->instr_end() && MII->isInsideBundle(); ++MII)
344 if (TargetInstrInfo::hasLoadFromStackSlot(*MII, Accesses))
345 return true;
346 return false;
349 return TargetInstrInfo::hasLoadFromStackSlot(MI, Accesses);
352 /// This function checks if the instruction or bundle of instructions
353 /// has store to stack slot and returns frameindex and machine memory
354 /// operand of that instruction if true.
355 bool HexagonInstrInfo::hasStoreToStackSlot(
356 const MachineInstr &MI,
357 SmallVectorImpl<const MachineMemOperand *> &Accesses) const {
358 if (MI.isBundle()) {
359 const MachineBasicBlock *MBB = MI.getParent();
360 MachineBasicBlock::const_instr_iterator MII = MI.getIterator();
361 for (++MII; MII != MBB->instr_end() && MII->isInsideBundle(); ++MII)
362 if (TargetInstrInfo::hasStoreToStackSlot(*MII, Accesses))
363 return true;
364 return false;
367 return TargetInstrInfo::hasStoreToStackSlot(MI, Accesses);
370 /// This function can analyze one/two way branching only and should (mostly) be
371 /// called by target independent side.
372 /// First entry is always the opcode of the branching instruction, except when
373 /// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
374 /// BB with only unconditional jump. Subsequent entries depend upon the opcode,
375 /// e.g. Jump_c p will have
376 /// Cond[0] = Jump_c
377 /// Cond[1] = p
378 /// HW-loop ENDLOOP:
379 /// Cond[0] = ENDLOOP
380 /// Cond[1] = MBB
381 /// New value jump:
382 /// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
383 /// Cond[1] = R
384 /// Cond[2] = Imm
385 bool HexagonInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
386 MachineBasicBlock *&TBB,
387 MachineBasicBlock *&FBB,
388 SmallVectorImpl<MachineOperand> &Cond,
389 bool AllowModify) const {
390 TBB = nullptr;
391 FBB = nullptr;
392 Cond.clear();
394 // If the block has no terminators, it just falls into the block after it.
395 MachineBasicBlock::instr_iterator I = MBB.instr_end();
396 if (I == MBB.instr_begin())
397 return false;
399 // A basic block may looks like this:
401 // [ insn
402 // EH_LABEL
403 // insn
404 // insn
405 // insn
406 // EH_LABEL
407 // insn ]
409 // It has two succs but does not have a terminator
410 // Don't know how to handle it.
411 do {
412 --I;
413 if (I->isEHLabel())
414 // Don't analyze EH branches.
415 return true;
416 } while (I != MBB.instr_begin());
418 I = MBB.instr_end();
419 --I;
421 while (I->isDebugInstr()) {
422 if (I == MBB.instr_begin())
423 return false;
424 --I;
427 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
428 I->getOperand(0).isMBB();
429 // Delete the J2_jump if it's equivalent to a fall-through.
430 if (AllowModify && JumpToBlock &&
431 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
432 LLVM_DEBUG(dbgs() << "\nErasing the jump to successor block\n";);
433 I->eraseFromParent();
434 I = MBB.instr_end();
435 if (I == MBB.instr_begin())
436 return false;
437 --I;
439 if (!isUnpredicatedTerminator(*I))
440 return false;
442 // Get the last instruction in the block.
443 MachineInstr *LastInst = &*I;
444 MachineInstr *SecondLastInst = nullptr;
445 // Find one more terminator if present.
446 while (true) {
447 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
448 if (!SecondLastInst)
449 SecondLastInst = &*I;
450 else
451 // This is a third branch.
452 return true;
454 if (I == MBB.instr_begin())
455 break;
456 --I;
459 int LastOpcode = LastInst->getOpcode();
460 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
461 // If the branch target is not a basic block, it could be a tail call.
462 // (It is, if the target is a function.)
463 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
464 return true;
465 if (SecLastOpcode == Hexagon::J2_jump &&
466 !SecondLastInst->getOperand(0).isMBB())
467 return true;
469 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
470 bool LastOpcodeHasNVJump = isNewValueJump(*LastInst);
472 if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
473 return true;
475 // If there is only one terminator instruction, process it.
476 if (LastInst && !SecondLastInst) {
477 if (LastOpcode == Hexagon::J2_jump) {
478 TBB = LastInst->getOperand(0).getMBB();
479 return false;
481 if (isEndLoopN(LastOpcode)) {
482 TBB = LastInst->getOperand(0).getMBB();
483 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
484 Cond.push_back(LastInst->getOperand(0));
485 return false;
487 if (LastOpcodeHasJMP_c) {
488 TBB = LastInst->getOperand(1).getMBB();
489 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
490 Cond.push_back(LastInst->getOperand(0));
491 return false;
493 // Only supporting rr/ri versions of new-value jumps.
494 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
495 TBB = LastInst->getOperand(2).getMBB();
496 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
497 Cond.push_back(LastInst->getOperand(0));
498 Cond.push_back(LastInst->getOperand(1));
499 return false;
501 LLVM_DEBUG(dbgs() << "\nCant analyze " << printMBBReference(MBB)
502 << " with one jump\n";);
503 // Otherwise, don't know what this is.
504 return true;
507 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
508 bool SecLastOpcodeHasNVJump = isNewValueJump(*SecondLastInst);
509 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
510 if (!SecondLastInst->getOperand(1).isMBB())
511 return true;
512 TBB = SecondLastInst->getOperand(1).getMBB();
513 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
514 Cond.push_back(SecondLastInst->getOperand(0));
515 FBB = LastInst->getOperand(0).getMBB();
516 return false;
519 // Only supporting rr/ri versions of new-value jumps.
520 if (SecLastOpcodeHasNVJump &&
521 (SecondLastInst->getNumExplicitOperands() == 3) &&
522 (LastOpcode == Hexagon::J2_jump)) {
523 TBB = SecondLastInst->getOperand(2).getMBB();
524 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
525 Cond.push_back(SecondLastInst->getOperand(0));
526 Cond.push_back(SecondLastInst->getOperand(1));
527 FBB = LastInst->getOperand(0).getMBB();
528 return false;
531 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
532 // executed, so remove it.
533 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
534 TBB = SecondLastInst->getOperand(0).getMBB();
535 I = LastInst->getIterator();
536 if (AllowModify)
537 I->eraseFromParent();
538 return false;
541 // If the block ends with an ENDLOOP, and J2_jump, handle it.
542 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
543 TBB = SecondLastInst->getOperand(0).getMBB();
544 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
545 Cond.push_back(SecondLastInst->getOperand(0));
546 FBB = LastInst->getOperand(0).getMBB();
547 return false;
549 LLVM_DEBUG(dbgs() << "\nCant analyze " << printMBBReference(MBB)
550 << " with two jumps";);
551 // Otherwise, can't handle this.
552 return true;
555 unsigned HexagonInstrInfo::removeBranch(MachineBasicBlock &MBB,
556 int *BytesRemoved) const {
557 assert(!BytesRemoved && "code size not handled");
559 LLVM_DEBUG(dbgs() << "\nRemoving branches out of " << printMBBReference(MBB));
560 MachineBasicBlock::iterator I = MBB.end();
561 unsigned Count = 0;
562 while (I != MBB.begin()) {
563 --I;
564 if (I->isDebugInstr())
565 continue;
566 // Only removing branches from end of MBB.
567 if (!I->isBranch())
568 return Count;
569 if (Count && (I->getOpcode() == Hexagon::J2_jump))
570 llvm_unreachable("Malformed basic block: unconditional branch not last");
571 MBB.erase(&MBB.back());
572 I = MBB.end();
573 ++Count;
575 return Count;
578 unsigned HexagonInstrInfo::insertBranch(MachineBasicBlock &MBB,
579 MachineBasicBlock *TBB,
580 MachineBasicBlock *FBB,
581 ArrayRef<MachineOperand> Cond,
582 const DebugLoc &DL,
583 int *BytesAdded) const {
584 unsigned BOpc = Hexagon::J2_jump;
585 unsigned BccOpc = Hexagon::J2_jumpt;
586 assert(validateBranchCond(Cond) && "Invalid branching condition");
587 assert(TBB && "insertBranch must not be told to insert a fallthrough");
588 assert(!BytesAdded && "code size not handled");
590 // Check if reverseBranchCondition has asked to reverse this branch
591 // If we want to reverse the branch an odd number of times, we want
592 // J2_jumpf.
593 if (!Cond.empty() && Cond[0].isImm())
594 BccOpc = Cond[0].getImm();
596 if (!FBB) {
597 if (Cond.empty()) {
598 // Due to a bug in TailMerging/CFG Optimization, we need to add a
599 // special case handling of a predicated jump followed by an
600 // unconditional jump. If not, Tail Merging and CFG Optimization go
601 // into an infinite loop.
602 MachineBasicBlock *NewTBB, *NewFBB;
603 SmallVector<MachineOperand, 4> Cond;
604 auto Term = MBB.getFirstTerminator();
605 if (Term != MBB.end() && isPredicated(*Term) &&
606 !analyzeBranch(MBB, NewTBB, NewFBB, Cond, false) &&
607 MachineFunction::iterator(NewTBB) == ++MBB.getIterator()) {
608 reverseBranchCondition(Cond);
609 removeBranch(MBB);
610 return insertBranch(MBB, TBB, nullptr, Cond, DL);
612 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
613 } else if (isEndLoopN(Cond[0].getImm())) {
614 int EndLoopOp = Cond[0].getImm();
615 assert(Cond[1].isMBB());
616 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
617 // Check for it, and change the BB target if needed.
618 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
619 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
620 VisitedBBs);
621 assert(Loop != nullptr && "Inserting an ENDLOOP without a LOOP");
622 Loop->getOperand(0).setMBB(TBB);
623 // Add the ENDLOOP after the finding the LOOP0.
624 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
625 } else if (isNewValueJump(Cond[0].getImm())) {
626 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
627 // New value jump
628 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
629 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
630 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
631 LLVM_DEBUG(dbgs() << "\nInserting NVJump for "
632 << printMBBReference(MBB););
633 if (Cond[2].isReg()) {
634 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
635 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
636 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
637 } else if(Cond[2].isImm()) {
638 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
639 addImm(Cond[2].getImm()).addMBB(TBB);
640 } else
641 llvm_unreachable("Invalid condition for branching");
642 } else {
643 assert((Cond.size() == 2) && "Malformed cond vector");
644 const MachineOperand &RO = Cond[1];
645 unsigned Flags = getUndefRegState(RO.isUndef());
646 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
648 return 1;
650 assert((!Cond.empty()) &&
651 "Cond. cannot be empty when multiple branchings are required");
652 assert((!isNewValueJump(Cond[0].getImm())) &&
653 "NV-jump cannot be inserted with another branch");
654 // Special case for hardware loops. The condition is a basic block.
655 if (isEndLoopN(Cond[0].getImm())) {
656 int EndLoopOp = Cond[0].getImm();
657 assert(Cond[1].isMBB());
658 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
659 // Check for it, and change the BB target if needed.
660 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
661 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
662 VisitedBBs);
663 assert(Loop != nullptr && "Inserting an ENDLOOP without a LOOP");
664 Loop->getOperand(0).setMBB(TBB);
665 // Add the ENDLOOP after the finding the LOOP0.
666 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
667 } else {
668 const MachineOperand &RO = Cond[1];
669 unsigned Flags = getUndefRegState(RO.isUndef());
670 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
672 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
674 return 2;
677 class HexagonPipelinerLoopInfo : public TargetInstrInfo::PipelinerLoopInfo {
678 MachineInstr *Loop, *EndLoop;
679 MachineFunction *MF;
680 const HexagonInstrInfo *TII;
682 public:
683 HexagonPipelinerLoopInfo(MachineInstr *Loop, MachineInstr *EndLoop)
684 : Loop(Loop), EndLoop(EndLoop), MF(Loop->getParent()->getParent()),
685 TII(MF->getSubtarget<HexagonSubtarget>().getInstrInfo()) {}
687 bool shouldIgnoreForPipelining(const MachineInstr *MI) const override {
688 // Only ignore the terminator.
689 return MI == EndLoop;
692 Optional<bool>
693 createTripCountGreaterCondition(int TC, MachineBasicBlock &MBB,
694 SmallVectorImpl<MachineOperand> &Cond) override {
695 if (Loop->getOpcode() == Hexagon::J2_loop0r) {
696 Register LoopCount = Loop->getOperand(1).getReg();
697 // Check if we're done with the loop.
698 unsigned Done = TII->createVR(MF, MVT::i1);
699 MachineInstr *NewCmp = BuildMI(&MBB, Loop->getDebugLoc(),
700 TII->get(Hexagon::C2_cmpgtui), Done)
701 .addReg(LoopCount)
702 .addImm(TC);
703 Cond.push_back(MachineOperand::CreateImm(Hexagon::J2_jumpf));
704 Cond.push_back(NewCmp->getOperand(0));
705 return {};
708 int64_t TripCount = Loop->getOperand(1).getImm();
709 return TripCount > TC;
712 void setPreheader(MachineBasicBlock *NewPreheader) override {
713 NewPreheader->splice(NewPreheader->getFirstTerminator(), Loop->getParent(),
714 Loop);
717 void adjustTripCount(int TripCountAdjust) override {
718 // If the loop trip count is a compile-time value, then just change the
719 // value.
720 if (Loop->getOpcode() == Hexagon::J2_loop0i ||
721 Loop->getOpcode() == Hexagon::J2_loop1i) {
722 int64_t TripCount = Loop->getOperand(1).getImm() + TripCountAdjust;
723 assert(TripCount > 0 && "Can't create an empty or negative loop!");
724 Loop->getOperand(1).setImm(TripCount);
725 return;
728 // The loop trip count is a run-time value. We generate code to subtract
729 // one from the trip count, and update the loop instruction.
730 Register LoopCount = Loop->getOperand(1).getReg();
731 Register NewLoopCount = TII->createVR(MF, MVT::i32);
732 BuildMI(*Loop->getParent(), Loop, Loop->getDebugLoc(),
733 TII->get(Hexagon::A2_addi), NewLoopCount)
734 .addReg(LoopCount)
735 .addImm(TripCountAdjust);
736 Loop->getOperand(1).setReg(NewLoopCount);
739 void disposed() override { Loop->eraseFromParent(); }
742 std::unique_ptr<TargetInstrInfo::PipelinerLoopInfo>
743 HexagonInstrInfo::analyzeLoopForPipelining(MachineBasicBlock *LoopBB) const {
744 // We really "analyze" only hardware loops right now.
745 MachineBasicBlock::iterator I = LoopBB->getFirstTerminator();
747 if (I != LoopBB->end() && isEndLoopN(I->getOpcode())) {
748 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
749 MachineInstr *LoopInst = findLoopInstr(
750 LoopBB, I->getOpcode(), I->getOperand(0).getMBB(), VisitedBBs);
751 if (LoopInst)
752 return std::make_unique<HexagonPipelinerLoopInfo>(LoopInst, &*I);
754 return nullptr;
757 bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
758 unsigned NumCycles, unsigned ExtraPredCycles,
759 BranchProbability Probability) const {
760 return nonDbgBBSize(&MBB) <= 3;
763 bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
764 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
765 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
766 const {
767 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
770 bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
771 unsigned NumInstrs, BranchProbability Probability) const {
772 return NumInstrs <= 4;
775 void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
776 MachineBasicBlock::iterator I,
777 const DebugLoc &DL, unsigned DestReg,
778 unsigned SrcReg, bool KillSrc) const {
779 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
780 unsigned KillFlag = getKillRegState(KillSrc);
782 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
783 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg)
784 .addReg(SrcReg, KillFlag);
785 return;
787 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
788 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg)
789 .addReg(SrcReg, KillFlag);
790 return;
792 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
793 // Map Pd = Ps to Pd = or(Ps, Ps).
794 BuildMI(MBB, I, DL, get(Hexagon::C2_or), DestReg)
795 .addReg(SrcReg).addReg(SrcReg, KillFlag);
796 return;
798 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
799 Hexagon::IntRegsRegClass.contains(SrcReg)) {
800 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
801 .addReg(SrcReg, KillFlag);
802 return;
804 if (Hexagon::IntRegsRegClass.contains(DestReg) &&
805 Hexagon::CtrRegsRegClass.contains(SrcReg)) {
806 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrcrr), DestReg)
807 .addReg(SrcReg, KillFlag);
808 return;
810 if (Hexagon::ModRegsRegClass.contains(DestReg) &&
811 Hexagon::IntRegsRegClass.contains(SrcReg)) {
812 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
813 .addReg(SrcReg, KillFlag);
814 return;
816 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
817 Hexagon::IntRegsRegClass.contains(DestReg)) {
818 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
819 .addReg(SrcReg, KillFlag);
820 return;
822 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
823 Hexagon::PredRegsRegClass.contains(DestReg)) {
824 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg)
825 .addReg(SrcReg, KillFlag);
826 return;
828 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
829 Hexagon::IntRegsRegClass.contains(DestReg)) {
830 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
831 .addReg(SrcReg, KillFlag);
832 return;
834 if (Hexagon::HvxVRRegClass.contains(SrcReg, DestReg)) {
835 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
836 addReg(SrcReg, KillFlag);
837 return;
839 if (Hexagon::HvxWRRegClass.contains(SrcReg, DestReg)) {
840 Register LoSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
841 Register HiSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
842 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
843 .addReg(HiSrc, KillFlag)
844 .addReg(LoSrc, KillFlag);
845 return;
847 if (Hexagon::HvxQRRegClass.contains(SrcReg, DestReg)) {
848 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
849 .addReg(SrcReg)
850 .addReg(SrcReg, KillFlag);
851 return;
853 if (Hexagon::HvxQRRegClass.contains(SrcReg) &&
854 Hexagon::HvxVRRegClass.contains(DestReg)) {
855 llvm_unreachable("Unimplemented pred to vec");
856 return;
858 if (Hexagon::HvxQRRegClass.contains(DestReg) &&
859 Hexagon::HvxVRRegClass.contains(SrcReg)) {
860 llvm_unreachable("Unimplemented vec to pred");
861 return;
864 #ifndef NDEBUG
865 // Show the invalid registers to ease debugging.
866 dbgs() << "Invalid registers for copy in " << printMBBReference(MBB) << ": "
867 << printReg(DestReg, &HRI) << " = " << printReg(SrcReg, &HRI) << '\n';
868 #endif
869 llvm_unreachable("Unimplemented");
872 void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
873 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
874 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
875 DebugLoc DL = MBB.findDebugLoc(I);
876 MachineFunction &MF = *MBB.getParent();
877 MachineFrameInfo &MFI = MF.getFrameInfo();
878 unsigned SlotAlign = MFI.getObjectAlignment(FI);
879 unsigned RegAlign = TRI->getSpillAlignment(*RC);
880 unsigned KillFlag = getKillRegState(isKill);
881 bool HasAlloca = MFI.hasVarSizedObjects();
882 const HexagonFrameLowering &HFI = *Subtarget.getFrameLowering();
884 MachineMemOperand *MMO = MF.getMachineMemOperand(
885 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
886 MFI.getObjectSize(FI), SlotAlign);
888 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
889 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
890 .addFrameIndex(FI).addImm(0)
891 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
892 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
893 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
894 .addFrameIndex(FI).addImm(0)
895 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
896 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
897 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
898 .addFrameIndex(FI).addImm(0)
899 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
900 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
901 BuildMI(MBB, I, DL, get(Hexagon::STriw_ctr))
902 .addFrameIndex(FI).addImm(0)
903 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
904 } else if (Hexagon::HvxQRRegClass.hasSubClassEq(RC)) {
905 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai))
906 .addFrameIndex(FI).addImm(0)
907 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
908 } else if (Hexagon::HvxVRRegClass.hasSubClassEq(RC)) {
909 // If there are variable-sized objects, spills will not be aligned.
910 if (HasAlloca)
911 SlotAlign = HFI.getStackAlignment();
912 unsigned Opc = SlotAlign < RegAlign ? Hexagon::V6_vS32Ub_ai
913 : Hexagon::V6_vS32b_ai;
914 MachineMemOperand *MMOA = MF.getMachineMemOperand(
915 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
916 MFI.getObjectSize(FI), SlotAlign);
917 BuildMI(MBB, I, DL, get(Opc))
918 .addFrameIndex(FI).addImm(0)
919 .addReg(SrcReg, KillFlag).addMemOperand(MMOA);
920 } else if (Hexagon::HvxWRRegClass.hasSubClassEq(RC)) {
921 // If there are variable-sized objects, spills will not be aligned.
922 if (HasAlloca)
923 SlotAlign = HFI.getStackAlignment();
924 unsigned Opc = SlotAlign < RegAlign ? Hexagon::PS_vstorerwu_ai
925 : Hexagon::PS_vstorerw_ai;
926 MachineMemOperand *MMOA = MF.getMachineMemOperand(
927 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
928 MFI.getObjectSize(FI), SlotAlign);
929 BuildMI(MBB, I, DL, get(Opc))
930 .addFrameIndex(FI).addImm(0)
931 .addReg(SrcReg, KillFlag).addMemOperand(MMOA);
932 } else {
933 llvm_unreachable("Unimplemented");
937 void HexagonInstrInfo::loadRegFromStackSlot(
938 MachineBasicBlock &MBB, MachineBasicBlock::iterator I, unsigned DestReg,
939 int FI, const TargetRegisterClass *RC,
940 const TargetRegisterInfo *TRI) const {
941 DebugLoc DL = MBB.findDebugLoc(I);
942 MachineFunction &MF = *MBB.getParent();
943 MachineFrameInfo &MFI = MF.getFrameInfo();
944 unsigned SlotAlign = MFI.getObjectAlignment(FI);
945 unsigned RegAlign = TRI->getSpillAlignment(*RC);
946 bool HasAlloca = MFI.hasVarSizedObjects();
947 const HexagonFrameLowering &HFI = *Subtarget.getFrameLowering();
949 MachineMemOperand *MMO = MF.getMachineMemOperand(
950 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
951 MFI.getObjectSize(FI), SlotAlign);
953 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
954 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
955 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
956 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
957 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
958 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
959 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
960 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
961 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
962 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
963 BuildMI(MBB, I, DL, get(Hexagon::LDriw_ctr), DestReg)
964 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
965 } else if (Hexagon::HvxQRRegClass.hasSubClassEq(RC)) {
966 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai), DestReg)
967 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
968 } else if (Hexagon::HvxVRRegClass.hasSubClassEq(RC)) {
969 // If there are variable-sized objects, spills will not be aligned.
970 if (HasAlloca)
971 SlotAlign = HFI.getStackAlignment();
972 unsigned Opc = SlotAlign < RegAlign ? Hexagon::V6_vL32Ub_ai
973 : Hexagon::V6_vL32b_ai;
974 MachineMemOperand *MMOA = MF.getMachineMemOperand(
975 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
976 MFI.getObjectSize(FI), SlotAlign);
977 BuildMI(MBB, I, DL, get(Opc), DestReg)
978 .addFrameIndex(FI).addImm(0).addMemOperand(MMOA);
979 } else if (Hexagon::HvxWRRegClass.hasSubClassEq(RC)) {
980 // If there are variable-sized objects, spills will not be aligned.
981 if (HasAlloca)
982 SlotAlign = HFI.getStackAlignment();
983 unsigned Opc = SlotAlign < RegAlign ? Hexagon::PS_vloadrwu_ai
984 : Hexagon::PS_vloadrw_ai;
985 MachineMemOperand *MMOA = MF.getMachineMemOperand(
986 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
987 MFI.getObjectSize(FI), SlotAlign);
988 BuildMI(MBB, I, DL, get(Opc), DestReg)
989 .addFrameIndex(FI).addImm(0).addMemOperand(MMOA);
990 } else {
991 llvm_unreachable("Can't store this register to stack slot");
995 static void getLiveRegsAt(LivePhysRegs &Regs, const MachineInstr &MI) {
996 const MachineBasicBlock &B = *MI.getParent();
997 Regs.addLiveOuts(B);
998 auto E = ++MachineBasicBlock::const_iterator(MI.getIterator()).getReverse();
999 for (auto I = B.rbegin(); I != E; ++I)
1000 Regs.stepBackward(*I);
1003 /// expandPostRAPseudo - This function is called for all pseudo instructions
1004 /// that remain after register allocation. Many pseudo instructions are
1005 /// created to help register allocation. This is the place to convert them
1006 /// into real instructions. The target can edit MI in place, or it can insert
1007 /// new instructions and erase MI. The function should return true if
1008 /// anything was changed.
1009 bool HexagonInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1010 MachineBasicBlock &MBB = *MI.getParent();
1011 MachineFunction &MF = *MBB.getParent();
1012 MachineRegisterInfo &MRI = MF.getRegInfo();
1013 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
1014 DebugLoc DL = MI.getDebugLoc();
1015 unsigned Opc = MI.getOpcode();
1017 auto RealCirc = [&](unsigned Opc, bool HasImm, unsigned MxOp) {
1018 Register Mx = MI.getOperand(MxOp).getReg();
1019 unsigned CSx = (Mx == Hexagon::M0 ? Hexagon::CS0 : Hexagon::CS1);
1020 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrrcr), CSx)
1021 .add(MI.getOperand((HasImm ? 5 : 4)));
1022 auto MIB = BuildMI(MBB, MI, DL, get(Opc)).add(MI.getOperand(0))
1023 .add(MI.getOperand(1)).add(MI.getOperand(2)).add(MI.getOperand(3));
1024 if (HasImm)
1025 MIB.add(MI.getOperand(4));
1026 MIB.addReg(CSx, RegState::Implicit);
1027 MBB.erase(MI);
1028 return true;
1031 switch (Opc) {
1032 case TargetOpcode::COPY: {
1033 MachineOperand &MD = MI.getOperand(0);
1034 MachineOperand &MS = MI.getOperand(1);
1035 MachineBasicBlock::iterator MBBI = MI.getIterator();
1036 if (MD.getReg() != MS.getReg() && !MS.isUndef()) {
1037 copyPhysReg(MBB, MI, DL, MD.getReg(), MS.getReg(), MS.isKill());
1038 std::prev(MBBI)->copyImplicitOps(*MBB.getParent(), MI);
1040 MBB.erase(MBBI);
1041 return true;
1043 case Hexagon::PS_aligna:
1044 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI.getOperand(0).getReg())
1045 .addReg(HRI.getFrameRegister())
1046 .addImm(-MI.getOperand(1).getImm());
1047 MBB.erase(MI);
1048 return true;
1049 case Hexagon::V6_vassignp: {
1050 Register SrcReg = MI.getOperand(1).getReg();
1051 Register DstReg = MI.getOperand(0).getReg();
1052 unsigned Kill = getKillRegState(MI.getOperand(1).isKill());
1053 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcombine), DstReg)
1054 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_hi), Kill)
1055 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_lo), Kill);
1056 MBB.erase(MI);
1057 return true;
1059 case Hexagon::V6_lo: {
1060 Register SrcReg = MI.getOperand(1).getReg();
1061 Register DstReg = MI.getOperand(0).getReg();
1062 Register SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
1063 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
1064 MBB.erase(MI);
1065 MRI.clearKillFlags(SrcSubLo);
1066 return true;
1068 case Hexagon::V6_hi: {
1069 Register SrcReg = MI.getOperand(1).getReg();
1070 Register DstReg = MI.getOperand(0).getReg();
1071 Register SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
1072 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
1073 MBB.erase(MI);
1074 MRI.clearKillFlags(SrcSubHi);
1075 return true;
1077 case Hexagon::PS_vstorerw_ai:
1078 case Hexagon::PS_vstorerwu_ai: {
1079 bool Aligned = Opc == Hexagon::PS_vstorerw_ai;
1080 Register SrcReg = MI.getOperand(2).getReg();
1081 Register SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
1082 Register SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
1083 unsigned NewOpc = Aligned ? Hexagon::V6_vS32b_ai : Hexagon::V6_vS32Ub_ai;
1084 unsigned Offset = HRI.getSpillSize(Hexagon::HvxVRRegClass);
1086 MachineInstr *MI1New = BuildMI(MBB, MI, DL, get(NewOpc))
1087 .add(MI.getOperand(0))
1088 .addImm(MI.getOperand(1).getImm())
1089 .addReg(SrcSubLo)
1090 .cloneMemRefs(MI);
1091 MI1New->getOperand(0).setIsKill(false);
1092 BuildMI(MBB, MI, DL, get(NewOpc))
1093 .add(MI.getOperand(0))
1094 // The Vectors are indexed in multiples of vector size.
1095 .addImm(MI.getOperand(1).getImm() + Offset)
1096 .addReg(SrcSubHi)
1097 .cloneMemRefs(MI);
1098 MBB.erase(MI);
1099 return true;
1101 case Hexagon::PS_vloadrw_ai:
1102 case Hexagon::PS_vloadrwu_ai: {
1103 bool Aligned = Opc == Hexagon::PS_vloadrw_ai;
1104 Register DstReg = MI.getOperand(0).getReg();
1105 unsigned NewOpc = Aligned ? Hexagon::V6_vL32b_ai : Hexagon::V6_vL32Ub_ai;
1106 unsigned Offset = HRI.getSpillSize(Hexagon::HvxVRRegClass);
1108 MachineInstr *MI1New = BuildMI(MBB, MI, DL, get(NewOpc),
1109 HRI.getSubReg(DstReg, Hexagon::vsub_lo))
1110 .add(MI.getOperand(1))
1111 .addImm(MI.getOperand(2).getImm())
1112 .cloneMemRefs(MI);
1113 MI1New->getOperand(1).setIsKill(false);
1114 BuildMI(MBB, MI, DL, get(NewOpc), HRI.getSubReg(DstReg, Hexagon::vsub_hi))
1115 .add(MI.getOperand(1))
1116 // The Vectors are indexed in multiples of vector size.
1117 .addImm(MI.getOperand(2).getImm() + Offset)
1118 .cloneMemRefs(MI);
1119 MBB.erase(MI);
1120 return true;
1122 case Hexagon::PS_true: {
1123 Register Reg = MI.getOperand(0).getReg();
1124 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1125 .addReg(Reg, RegState::Undef)
1126 .addReg(Reg, RegState::Undef);
1127 MBB.erase(MI);
1128 return true;
1130 case Hexagon::PS_false: {
1131 Register Reg = MI.getOperand(0).getReg();
1132 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1133 .addReg(Reg, RegState::Undef)
1134 .addReg(Reg, RegState::Undef);
1135 MBB.erase(MI);
1136 return true;
1138 case Hexagon::PS_qtrue: {
1139 BuildMI(MBB, MI, DL, get(Hexagon::V6_veqw), MI.getOperand(0).getReg())
1140 .addReg(Hexagon::V0, RegState::Undef)
1141 .addReg(Hexagon::V0, RegState::Undef);
1142 MBB.erase(MI);
1143 return true;
1145 case Hexagon::PS_qfalse: {
1146 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgtw), MI.getOperand(0).getReg())
1147 .addReg(Hexagon::V0, RegState::Undef)
1148 .addReg(Hexagon::V0, RegState::Undef);
1149 MBB.erase(MI);
1150 return true;
1152 case Hexagon::PS_vdd0: {
1153 Register Vd = MI.getOperand(0).getReg();
1154 BuildMI(MBB, MI, DL, get(Hexagon::V6_vsubw_dv), Vd)
1155 .addReg(Vd, RegState::Undef)
1156 .addReg(Vd, RegState::Undef);
1157 MBB.erase(MI);
1158 return true;
1160 case Hexagon::PS_vmulw: {
1161 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
1162 Register DstReg = MI.getOperand(0).getReg();
1163 Register Src1Reg = MI.getOperand(1).getReg();
1164 Register Src2Reg = MI.getOperand(2).getReg();
1165 Register Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1166 Register Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1167 Register Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1168 Register Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
1169 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
1170 HRI.getSubReg(DstReg, Hexagon::isub_hi))
1171 .addReg(Src1SubHi)
1172 .addReg(Src2SubHi);
1173 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
1174 HRI.getSubReg(DstReg, Hexagon::isub_lo))
1175 .addReg(Src1SubLo)
1176 .addReg(Src2SubLo);
1177 MBB.erase(MI);
1178 MRI.clearKillFlags(Src1SubHi);
1179 MRI.clearKillFlags(Src1SubLo);
1180 MRI.clearKillFlags(Src2SubHi);
1181 MRI.clearKillFlags(Src2SubLo);
1182 return true;
1184 case Hexagon::PS_vmulw_acc: {
1185 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
1186 Register DstReg = MI.getOperand(0).getReg();
1187 Register Src1Reg = MI.getOperand(1).getReg();
1188 Register Src2Reg = MI.getOperand(2).getReg();
1189 Register Src3Reg = MI.getOperand(3).getReg();
1190 Register Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1191 Register Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1192 Register Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1193 Register Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
1194 Register Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::isub_hi);
1195 Register Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::isub_lo);
1196 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
1197 HRI.getSubReg(DstReg, Hexagon::isub_hi))
1198 .addReg(Src1SubHi)
1199 .addReg(Src2SubHi)
1200 .addReg(Src3SubHi);
1201 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
1202 HRI.getSubReg(DstReg, Hexagon::isub_lo))
1203 .addReg(Src1SubLo)
1204 .addReg(Src2SubLo)
1205 .addReg(Src3SubLo);
1206 MBB.erase(MI);
1207 MRI.clearKillFlags(Src1SubHi);
1208 MRI.clearKillFlags(Src1SubLo);
1209 MRI.clearKillFlags(Src2SubHi);
1210 MRI.clearKillFlags(Src2SubLo);
1211 MRI.clearKillFlags(Src3SubHi);
1212 MRI.clearKillFlags(Src3SubLo);
1213 return true;
1215 case Hexagon::PS_pselect: {
1216 const MachineOperand &Op0 = MI.getOperand(0);
1217 const MachineOperand &Op1 = MI.getOperand(1);
1218 const MachineOperand &Op2 = MI.getOperand(2);
1219 const MachineOperand &Op3 = MI.getOperand(3);
1220 Register Rd = Op0.getReg();
1221 Register Pu = Op1.getReg();
1222 Register Rs = Op2.getReg();
1223 Register Rt = Op3.getReg();
1224 DebugLoc DL = MI.getDebugLoc();
1225 unsigned K1 = getKillRegState(Op1.isKill());
1226 unsigned K2 = getKillRegState(Op2.isKill());
1227 unsigned K3 = getKillRegState(Op3.isKill());
1228 if (Rd != Rs)
1229 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1230 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1231 .addReg(Rs, K2);
1232 if (Rd != Rt)
1233 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1234 .addReg(Pu, K1)
1235 .addReg(Rt, K3);
1236 MBB.erase(MI);
1237 return true;
1239 case Hexagon::PS_vselect: {
1240 const MachineOperand &Op0 = MI.getOperand(0);
1241 const MachineOperand &Op1 = MI.getOperand(1);
1242 const MachineOperand &Op2 = MI.getOperand(2);
1243 const MachineOperand &Op3 = MI.getOperand(3);
1244 LivePhysRegs LiveAtMI(HRI);
1245 getLiveRegsAt(LiveAtMI, MI);
1246 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
1247 Register PReg = Op1.getReg();
1248 assert(Op1.getSubReg() == 0);
1249 unsigned PState = getRegState(Op1);
1251 if (Op0.getReg() != Op2.getReg()) {
1252 unsigned S = Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill
1253 : PState;
1254 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
1255 .add(Op0)
1256 .addReg(PReg, S)
1257 .add(Op2);
1258 if (IsDestLive)
1259 T.addReg(Op0.getReg(), RegState::Implicit);
1260 IsDestLive = true;
1262 if (Op0.getReg() != Op3.getReg()) {
1263 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
1264 .add(Op0)
1265 .addReg(PReg, PState)
1266 .add(Op3);
1267 if (IsDestLive)
1268 T.addReg(Op0.getReg(), RegState::Implicit);
1270 MBB.erase(MI);
1271 return true;
1273 case Hexagon::PS_wselect: {
1274 MachineOperand &Op0 = MI.getOperand(0);
1275 MachineOperand &Op1 = MI.getOperand(1);
1276 MachineOperand &Op2 = MI.getOperand(2);
1277 MachineOperand &Op3 = MI.getOperand(3);
1278 LivePhysRegs LiveAtMI(HRI);
1279 getLiveRegsAt(LiveAtMI, MI);
1280 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
1281 Register PReg = Op1.getReg();
1282 assert(Op1.getSubReg() == 0);
1283 unsigned PState = getRegState(Op1);
1285 if (Op0.getReg() != Op2.getReg()) {
1286 unsigned S = Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill
1287 : PState;
1288 Register SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_lo);
1289 Register SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_hi);
1290 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
1291 .add(Op0)
1292 .addReg(PReg, S)
1293 .addReg(SrcHi)
1294 .addReg(SrcLo);
1295 if (IsDestLive)
1296 T.addReg(Op0.getReg(), RegState::Implicit);
1297 IsDestLive = true;
1299 if (Op0.getReg() != Op3.getReg()) {
1300 Register SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_lo);
1301 Register SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_hi);
1302 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
1303 .add(Op0)
1304 .addReg(PReg, PState)
1305 .addReg(SrcHi)
1306 .addReg(SrcLo);
1307 if (IsDestLive)
1308 T.addReg(Op0.getReg(), RegState::Implicit);
1310 MBB.erase(MI);
1311 return true;
1314 case Hexagon::PS_crash: {
1315 // Generate a misaligned load that is guaranteed to cause a crash.
1316 class CrashPseudoSourceValue : public PseudoSourceValue {
1317 public:
1318 CrashPseudoSourceValue(const TargetInstrInfo &TII)
1319 : PseudoSourceValue(TargetCustom, TII) {}
1321 bool isConstant(const MachineFrameInfo *) const override {
1322 return false;
1324 bool isAliased(const MachineFrameInfo *) const override {
1325 return false;
1327 bool mayAlias(const MachineFrameInfo *) const override {
1328 return false;
1330 void printCustom(raw_ostream &OS) const override {
1331 OS << "MisalignedCrash";
1335 static const CrashPseudoSourceValue CrashPSV(*this);
1336 MachineMemOperand *MMO = MF.getMachineMemOperand(
1337 MachinePointerInfo(&CrashPSV),
1338 MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 8, 1);
1339 BuildMI(MBB, MI, DL, get(Hexagon::PS_loadrdabs), Hexagon::D13)
1340 .addImm(0xBADC0FEE) // Misaligned load.
1341 .addMemOperand(MMO);
1342 MBB.erase(MI);
1343 return true;
1346 case Hexagon::PS_tailcall_i:
1347 MI.setDesc(get(Hexagon::J2_jump));
1348 return true;
1349 case Hexagon::PS_tailcall_r:
1350 case Hexagon::PS_jmpret:
1351 MI.setDesc(get(Hexagon::J2_jumpr));
1352 return true;
1353 case Hexagon::PS_jmprett:
1354 MI.setDesc(get(Hexagon::J2_jumprt));
1355 return true;
1356 case Hexagon::PS_jmpretf:
1357 MI.setDesc(get(Hexagon::J2_jumprf));
1358 return true;
1359 case Hexagon::PS_jmprettnewpt:
1360 MI.setDesc(get(Hexagon::J2_jumprtnewpt));
1361 return true;
1362 case Hexagon::PS_jmpretfnewpt:
1363 MI.setDesc(get(Hexagon::J2_jumprfnewpt));
1364 return true;
1365 case Hexagon::PS_jmprettnew:
1366 MI.setDesc(get(Hexagon::J2_jumprtnew));
1367 return true;
1368 case Hexagon::PS_jmpretfnew:
1369 MI.setDesc(get(Hexagon::J2_jumprfnew));
1370 return true;
1372 case Hexagon::PS_loadrub_pci:
1373 return RealCirc(Hexagon::L2_loadrub_pci, /*HasImm*/true, /*MxOp*/4);
1374 case Hexagon::PS_loadrb_pci:
1375 return RealCirc(Hexagon::L2_loadrb_pci, /*HasImm*/true, /*MxOp*/4);
1376 case Hexagon::PS_loadruh_pci:
1377 return RealCirc(Hexagon::L2_loadruh_pci, /*HasImm*/true, /*MxOp*/4);
1378 case Hexagon::PS_loadrh_pci:
1379 return RealCirc(Hexagon::L2_loadrh_pci, /*HasImm*/true, /*MxOp*/4);
1380 case Hexagon::PS_loadri_pci:
1381 return RealCirc(Hexagon::L2_loadri_pci, /*HasImm*/true, /*MxOp*/4);
1382 case Hexagon::PS_loadrd_pci:
1383 return RealCirc(Hexagon::L2_loadrd_pci, /*HasImm*/true, /*MxOp*/4);
1384 case Hexagon::PS_loadrub_pcr:
1385 return RealCirc(Hexagon::L2_loadrub_pcr, /*HasImm*/false, /*MxOp*/3);
1386 case Hexagon::PS_loadrb_pcr:
1387 return RealCirc(Hexagon::L2_loadrb_pcr, /*HasImm*/false, /*MxOp*/3);
1388 case Hexagon::PS_loadruh_pcr:
1389 return RealCirc(Hexagon::L2_loadruh_pcr, /*HasImm*/false, /*MxOp*/3);
1390 case Hexagon::PS_loadrh_pcr:
1391 return RealCirc(Hexagon::L2_loadrh_pcr, /*HasImm*/false, /*MxOp*/3);
1392 case Hexagon::PS_loadri_pcr:
1393 return RealCirc(Hexagon::L2_loadri_pcr, /*HasImm*/false, /*MxOp*/3);
1394 case Hexagon::PS_loadrd_pcr:
1395 return RealCirc(Hexagon::L2_loadrd_pcr, /*HasImm*/false, /*MxOp*/3);
1396 case Hexagon::PS_storerb_pci:
1397 return RealCirc(Hexagon::S2_storerb_pci, /*HasImm*/true, /*MxOp*/3);
1398 case Hexagon::PS_storerh_pci:
1399 return RealCirc(Hexagon::S2_storerh_pci, /*HasImm*/true, /*MxOp*/3);
1400 case Hexagon::PS_storerf_pci:
1401 return RealCirc(Hexagon::S2_storerf_pci, /*HasImm*/true, /*MxOp*/3);
1402 case Hexagon::PS_storeri_pci:
1403 return RealCirc(Hexagon::S2_storeri_pci, /*HasImm*/true, /*MxOp*/3);
1404 case Hexagon::PS_storerd_pci:
1405 return RealCirc(Hexagon::S2_storerd_pci, /*HasImm*/true, /*MxOp*/3);
1406 case Hexagon::PS_storerb_pcr:
1407 return RealCirc(Hexagon::S2_storerb_pcr, /*HasImm*/false, /*MxOp*/2);
1408 case Hexagon::PS_storerh_pcr:
1409 return RealCirc(Hexagon::S2_storerh_pcr, /*HasImm*/false, /*MxOp*/2);
1410 case Hexagon::PS_storerf_pcr:
1411 return RealCirc(Hexagon::S2_storerf_pcr, /*HasImm*/false, /*MxOp*/2);
1412 case Hexagon::PS_storeri_pcr:
1413 return RealCirc(Hexagon::S2_storeri_pcr, /*HasImm*/false, /*MxOp*/2);
1414 case Hexagon::PS_storerd_pcr:
1415 return RealCirc(Hexagon::S2_storerd_pcr, /*HasImm*/false, /*MxOp*/2);
1418 return false;
1421 MachineBasicBlock::instr_iterator
1422 HexagonInstrInfo::expandVGatherPseudo(MachineInstr &MI) const {
1423 MachineBasicBlock &MBB = *MI.getParent();
1424 const DebugLoc &DL = MI.getDebugLoc();
1425 unsigned Opc = MI.getOpcode();
1426 MachineBasicBlock::iterator First;
1428 switch (Opc) {
1429 case Hexagon::V6_vgathermh_pseudo:
1430 First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermh))
1431 .add(MI.getOperand(1))
1432 .add(MI.getOperand(2))
1433 .add(MI.getOperand(3));
1434 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1435 .add(MI.getOperand(0))
1436 .addImm(0)
1437 .addReg(Hexagon::VTMP);
1438 MBB.erase(MI);
1439 return First.getInstrIterator();
1441 case Hexagon::V6_vgathermw_pseudo:
1442 First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermw))
1443 .add(MI.getOperand(1))
1444 .add(MI.getOperand(2))
1445 .add(MI.getOperand(3));
1446 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1447 .add(MI.getOperand(0))
1448 .addImm(0)
1449 .addReg(Hexagon::VTMP);
1450 MBB.erase(MI);
1451 return First.getInstrIterator();
1453 case Hexagon::V6_vgathermhw_pseudo:
1454 First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhw))
1455 .add(MI.getOperand(1))
1456 .add(MI.getOperand(2))
1457 .add(MI.getOperand(3));
1458 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1459 .add(MI.getOperand(0))
1460 .addImm(0)
1461 .addReg(Hexagon::VTMP);
1462 MBB.erase(MI);
1463 return First.getInstrIterator();
1465 case Hexagon::V6_vgathermhq_pseudo:
1466 First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhq))
1467 .add(MI.getOperand(1))
1468 .add(MI.getOperand(2))
1469 .add(MI.getOperand(3))
1470 .add(MI.getOperand(4));
1471 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1472 .add(MI.getOperand(0))
1473 .addImm(0)
1474 .addReg(Hexagon::VTMP);
1475 MBB.erase(MI);
1476 return First.getInstrIterator();
1478 case Hexagon::V6_vgathermwq_pseudo:
1479 First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermwq))
1480 .add(MI.getOperand(1))
1481 .add(MI.getOperand(2))
1482 .add(MI.getOperand(3))
1483 .add(MI.getOperand(4));
1484 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1485 .add(MI.getOperand(0))
1486 .addImm(0)
1487 .addReg(Hexagon::VTMP);
1488 MBB.erase(MI);
1489 return First.getInstrIterator();
1491 case Hexagon::V6_vgathermhwq_pseudo:
1492 First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhwq))
1493 .add(MI.getOperand(1))
1494 .add(MI.getOperand(2))
1495 .add(MI.getOperand(3))
1496 .add(MI.getOperand(4));
1497 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1498 .add(MI.getOperand(0))
1499 .addImm(0)
1500 .addReg(Hexagon::VTMP);
1501 MBB.erase(MI);
1502 return First.getInstrIterator();
1505 return MI.getIterator();
1508 // We indicate that we want to reverse the branch by
1509 // inserting the reversed branching opcode.
1510 bool HexagonInstrInfo::reverseBranchCondition(
1511 SmallVectorImpl<MachineOperand> &Cond) const {
1512 if (Cond.empty())
1513 return true;
1514 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1515 unsigned opcode = Cond[0].getImm();
1516 //unsigned temp;
1517 assert(get(opcode).isBranch() && "Should be a branching condition.");
1518 if (isEndLoopN(opcode))
1519 return true;
1520 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1521 Cond[0].setImm(NewOpcode);
1522 return false;
1525 void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1526 MachineBasicBlock::iterator MI) const {
1527 DebugLoc DL;
1528 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1531 bool HexagonInstrInfo::isPostIncrement(const MachineInstr &MI) const {
1532 return getAddrMode(MI) == HexagonII::PostInc;
1535 // Returns true if an instruction is predicated irrespective of the predicate
1536 // sense. For example, all of the following will return true.
1537 // if (p0) R1 = add(R2, R3)
1538 // if (!p0) R1 = add(R2, R3)
1539 // if (p0.new) R1 = add(R2, R3)
1540 // if (!p0.new) R1 = add(R2, R3)
1541 // Note: New-value stores are not included here as in the current
1542 // implementation, we don't need to check their predicate sense.
1543 bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1544 const uint64_t F = MI.getDesc().TSFlags;
1545 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
1548 bool HexagonInstrInfo::PredicateInstruction(
1549 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
1550 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1551 isEndLoopN(Cond[0].getImm())) {
1552 LLVM_DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
1553 return false;
1555 int Opc = MI.getOpcode();
1556 assert (isPredicable(MI) && "Expected predicable instruction");
1557 bool invertJump = predOpcodeHasNot(Cond);
1559 // We have to predicate MI "in place", i.e. after this function returns,
1560 // MI will need to be transformed into a predicated form. To avoid com-
1561 // plicated manipulations with the operands (handling tied operands,
1562 // etc.), build a new temporary instruction, then overwrite MI with it.
1564 MachineBasicBlock &B = *MI.getParent();
1565 DebugLoc DL = MI.getDebugLoc();
1566 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1567 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
1568 unsigned NOp = 0, NumOps = MI.getNumOperands();
1569 while (NOp < NumOps) {
1570 MachineOperand &Op = MI.getOperand(NOp);
1571 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1572 break;
1573 T.add(Op);
1574 NOp++;
1577 unsigned PredReg, PredRegPos, PredRegFlags;
1578 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1579 (void)GotPredReg;
1580 assert(GotPredReg);
1581 T.addReg(PredReg, PredRegFlags);
1582 while (NOp < NumOps)
1583 T.add(MI.getOperand(NOp++));
1585 MI.setDesc(get(PredOpc));
1586 while (unsigned n = MI.getNumOperands())
1587 MI.RemoveOperand(n-1);
1588 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
1589 MI.addOperand(T->getOperand(i));
1591 MachineBasicBlock::instr_iterator TI = T->getIterator();
1592 B.erase(TI);
1594 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1595 MRI.clearKillFlags(PredReg);
1596 return true;
1599 bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1600 ArrayRef<MachineOperand> Pred2) const {
1601 // TODO: Fix this
1602 return false;
1605 bool HexagonInstrInfo::DefinesPredicate(MachineInstr &MI,
1606 std::vector<MachineOperand> &Pred) const {
1607 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
1609 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1610 MachineOperand MO = MI.getOperand(oper);
1611 if (MO.isReg()) {
1612 if (!MO.isDef())
1613 continue;
1614 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1615 if (RC == &Hexagon::PredRegsRegClass) {
1616 Pred.push_back(MO);
1617 return true;
1619 continue;
1620 } else if (MO.isRegMask()) {
1621 for (unsigned PR : Hexagon::PredRegsRegClass) {
1622 if (!MI.modifiesRegister(PR, &HRI))
1623 continue;
1624 Pred.push_back(MO);
1625 return true;
1629 return false;
1632 bool HexagonInstrInfo::isPredicable(const MachineInstr &MI) const {
1633 if (!MI.getDesc().isPredicable())
1634 return false;
1636 if (MI.isCall() || isTailCall(MI)) {
1637 if (!Subtarget.usePredicatedCalls())
1638 return false;
1641 // HVX loads are not predicable on v60, but are on v62.
1642 if (!Subtarget.hasV62Ops()) {
1643 switch (MI.getOpcode()) {
1644 case Hexagon::V6_vL32b_ai:
1645 case Hexagon::V6_vL32b_pi:
1646 case Hexagon::V6_vL32b_ppu:
1647 case Hexagon::V6_vL32b_cur_ai:
1648 case Hexagon::V6_vL32b_cur_pi:
1649 case Hexagon::V6_vL32b_cur_ppu:
1650 case Hexagon::V6_vL32b_nt_ai:
1651 case Hexagon::V6_vL32b_nt_pi:
1652 case Hexagon::V6_vL32b_nt_ppu:
1653 case Hexagon::V6_vL32b_tmp_ai:
1654 case Hexagon::V6_vL32b_tmp_pi:
1655 case Hexagon::V6_vL32b_tmp_ppu:
1656 case Hexagon::V6_vL32b_nt_cur_ai:
1657 case Hexagon::V6_vL32b_nt_cur_pi:
1658 case Hexagon::V6_vL32b_nt_cur_ppu:
1659 case Hexagon::V6_vL32b_nt_tmp_ai:
1660 case Hexagon::V6_vL32b_nt_tmp_pi:
1661 case Hexagon::V6_vL32b_nt_tmp_ppu:
1662 return false;
1665 return true;
1668 bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1669 const MachineBasicBlock *MBB,
1670 const MachineFunction &MF) const {
1671 // Debug info is never a scheduling boundary. It's necessary to be explicit
1672 // due to the special treatment of IT instructions below, otherwise a
1673 // dbg_value followed by an IT will result in the IT instruction being
1674 // considered a scheduling hazard, which is wrong. It should be the actual
1675 // instruction preceding the dbg_value instruction(s), just like it is
1676 // when debug info is not present.
1677 if (MI.isDebugInstr())
1678 return false;
1680 // Throwing call is a boundary.
1681 if (MI.isCall()) {
1682 // Don't mess around with no return calls.
1683 if (doesNotReturn(MI))
1684 return true;
1685 // If any of the block's successors is a landing pad, this could be a
1686 // throwing call.
1687 for (auto I : MBB->successors())
1688 if (I->isEHPad())
1689 return true;
1692 // Terminators and labels can't be scheduled around.
1693 if (MI.getDesc().isTerminator() || MI.isPosition())
1694 return true;
1696 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1697 return true;
1699 return false;
1702 /// Measure the specified inline asm to determine an approximation of its
1703 /// length.
1704 /// Comments (which run till the next SeparatorString or newline) do not
1705 /// count as an instruction.
1706 /// Any other non-whitespace text is considered an instruction, with
1707 /// multiple instructions separated by SeparatorString or newlines.
1708 /// Variable-length instructions are not handled here; this function
1709 /// may be overloaded in the target code to do that.
1710 /// Hexagon counts the number of ##'s and adjust for that many
1711 /// constant exenders.
1712 unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1713 const MCAsmInfo &MAI,
1714 const TargetSubtargetInfo *STI) const {
1715 StringRef AStr(Str);
1716 // Count the number of instructions in the asm.
1717 bool atInsnStart = true;
1718 unsigned Length = 0;
1719 const unsigned MaxInstLength = MAI.getMaxInstLength(STI);
1720 for (; *Str; ++Str) {
1721 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1722 strlen(MAI.getSeparatorString())) == 0)
1723 atInsnStart = true;
1724 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1725 Length += MaxInstLength;
1726 atInsnStart = false;
1728 if (atInsnStart && strncmp(Str, MAI.getCommentString().data(),
1729 MAI.getCommentString().size()) == 0)
1730 atInsnStart = false;
1733 // Add to size number of constant extenders seen * 4.
1734 StringRef Occ("##");
1735 Length += AStr.count(Occ)*4;
1736 return Length;
1739 ScheduleHazardRecognizer*
1740 HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1741 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
1742 if (UseDFAHazardRec)
1743 return new HexagonHazardRecognizer(II, this, Subtarget);
1744 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1747 /// For a comparison instruction, return the source registers in
1748 /// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1749 /// compares against in CmpValue. Return true if the comparison instruction
1750 /// can be analyzed.
1751 bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1752 unsigned &SrcReg2, int &Mask,
1753 int &Value) const {
1754 unsigned Opc = MI.getOpcode();
1756 // Set mask and the first source register.
1757 switch (Opc) {
1758 case Hexagon::C2_cmpeq:
1759 case Hexagon::C2_cmpeqp:
1760 case Hexagon::C2_cmpgt:
1761 case Hexagon::C2_cmpgtp:
1762 case Hexagon::C2_cmpgtu:
1763 case Hexagon::C2_cmpgtup:
1764 case Hexagon::C4_cmpneq:
1765 case Hexagon::C4_cmplte:
1766 case Hexagon::C4_cmplteu:
1767 case Hexagon::C2_cmpeqi:
1768 case Hexagon::C2_cmpgti:
1769 case Hexagon::C2_cmpgtui:
1770 case Hexagon::C4_cmpneqi:
1771 case Hexagon::C4_cmplteui:
1772 case Hexagon::C4_cmpltei:
1773 SrcReg = MI.getOperand(1).getReg();
1774 Mask = ~0;
1775 break;
1776 case Hexagon::A4_cmpbeq:
1777 case Hexagon::A4_cmpbgt:
1778 case Hexagon::A4_cmpbgtu:
1779 case Hexagon::A4_cmpbeqi:
1780 case Hexagon::A4_cmpbgti:
1781 case Hexagon::A4_cmpbgtui:
1782 SrcReg = MI.getOperand(1).getReg();
1783 Mask = 0xFF;
1784 break;
1785 case Hexagon::A4_cmpheq:
1786 case Hexagon::A4_cmphgt:
1787 case Hexagon::A4_cmphgtu:
1788 case Hexagon::A4_cmpheqi:
1789 case Hexagon::A4_cmphgti:
1790 case Hexagon::A4_cmphgtui:
1791 SrcReg = MI.getOperand(1).getReg();
1792 Mask = 0xFFFF;
1793 break;
1796 // Set the value/second source register.
1797 switch (Opc) {
1798 case Hexagon::C2_cmpeq:
1799 case Hexagon::C2_cmpeqp:
1800 case Hexagon::C2_cmpgt:
1801 case Hexagon::C2_cmpgtp:
1802 case Hexagon::C2_cmpgtu:
1803 case Hexagon::C2_cmpgtup:
1804 case Hexagon::A4_cmpbeq:
1805 case Hexagon::A4_cmpbgt:
1806 case Hexagon::A4_cmpbgtu:
1807 case Hexagon::A4_cmpheq:
1808 case Hexagon::A4_cmphgt:
1809 case Hexagon::A4_cmphgtu:
1810 case Hexagon::C4_cmpneq:
1811 case Hexagon::C4_cmplte:
1812 case Hexagon::C4_cmplteu:
1813 SrcReg2 = MI.getOperand(2).getReg();
1814 return true;
1816 case Hexagon::C2_cmpeqi:
1817 case Hexagon::C2_cmpgtui:
1818 case Hexagon::C2_cmpgti:
1819 case Hexagon::C4_cmpneqi:
1820 case Hexagon::C4_cmplteui:
1821 case Hexagon::C4_cmpltei:
1822 case Hexagon::A4_cmpbeqi:
1823 case Hexagon::A4_cmpbgti:
1824 case Hexagon::A4_cmpbgtui:
1825 case Hexagon::A4_cmpheqi:
1826 case Hexagon::A4_cmphgti:
1827 case Hexagon::A4_cmphgtui: {
1828 SrcReg2 = 0;
1829 const MachineOperand &Op2 = MI.getOperand(2);
1830 if (!Op2.isImm())
1831 return false;
1832 Value = MI.getOperand(2).getImm();
1833 return true;
1837 return false;
1840 unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
1841 const MachineInstr &MI,
1842 unsigned *PredCost) const {
1843 return getInstrTimingClassLatency(ItinData, MI);
1846 DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1847 const TargetSubtargetInfo &STI) const {
1848 const InstrItineraryData *II = STI.getInstrItineraryData();
1849 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1852 // Inspired by this pair:
1853 // %r13 = L2_loadri_io %r29, 136; mem:LD4[FixedStack0]
1854 // S2_storeri_io %r29, 132, killed %r1; flags: mem:ST4[FixedStack1]
1855 // Currently AA considers the addresses in these instructions to be aliasing.
1856 bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1857 const MachineInstr &MIa, const MachineInstr &MIb,
1858 AliasAnalysis *AA) const {
1859 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1860 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
1861 return false;
1863 // Instructions that are pure loads, not loads and stores like memops are not
1864 // dependent.
1865 if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
1866 return true;
1868 // Get the base register in MIa.
1869 unsigned BasePosA, OffsetPosA;
1870 if (!getBaseAndOffsetPosition(MIa, BasePosA, OffsetPosA))
1871 return false;
1872 const MachineOperand &BaseA = MIa.getOperand(BasePosA);
1873 Register BaseRegA = BaseA.getReg();
1874 unsigned BaseSubA = BaseA.getSubReg();
1876 // Get the base register in MIb.
1877 unsigned BasePosB, OffsetPosB;
1878 if (!getBaseAndOffsetPosition(MIb, BasePosB, OffsetPosB))
1879 return false;
1880 const MachineOperand &BaseB = MIb.getOperand(BasePosB);
1881 Register BaseRegB = BaseB.getReg();
1882 unsigned BaseSubB = BaseB.getSubReg();
1884 if (BaseRegA != BaseRegB || BaseSubA != BaseSubB)
1885 return false;
1887 // Get the access sizes.
1888 unsigned SizeA = getMemAccessSize(MIa);
1889 unsigned SizeB = getMemAccessSize(MIb);
1891 // Get the offsets. Handle immediates only for now.
1892 const MachineOperand &OffA = MIa.getOperand(OffsetPosA);
1893 const MachineOperand &OffB = MIb.getOperand(OffsetPosB);
1894 if (!MIa.getOperand(OffsetPosA).isImm() ||
1895 !MIb.getOperand(OffsetPosB).isImm())
1896 return false;
1897 int OffsetA = isPostIncrement(MIa) ? 0 : OffA.getImm();
1898 int OffsetB = isPostIncrement(MIb) ? 0 : OffB.getImm();
1900 // This is a mem access with the same base register and known offsets from it.
1901 // Reason about it.
1902 if (OffsetA > OffsetB) {
1903 uint64_t OffDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1904 return SizeB <= OffDiff;
1906 if (OffsetA < OffsetB) {
1907 uint64_t OffDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1908 return SizeA <= OffDiff;
1911 return false;
1914 /// If the instruction is an increment of a constant value, return the amount.
1915 bool HexagonInstrInfo::getIncrementValue(const MachineInstr &MI,
1916 int &Value) const {
1917 if (isPostIncrement(MI)) {
1918 unsigned BasePos = 0, OffsetPos = 0;
1919 if (!getBaseAndOffsetPosition(MI, BasePos, OffsetPos))
1920 return false;
1921 const MachineOperand &OffsetOp = MI.getOperand(OffsetPos);
1922 if (OffsetOp.isImm()) {
1923 Value = OffsetOp.getImm();
1924 return true;
1926 } else if (MI.getOpcode() == Hexagon::A2_addi) {
1927 const MachineOperand &AddOp = MI.getOperand(2);
1928 if (AddOp.isImm()) {
1929 Value = AddOp.getImm();
1930 return true;
1934 return false;
1937 std::pair<unsigned, unsigned>
1938 HexagonInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
1939 return std::make_pair(TF & ~HexagonII::MO_Bitmasks,
1940 TF & HexagonII::MO_Bitmasks);
1943 ArrayRef<std::pair<unsigned, const char*>>
1944 HexagonInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
1945 using namespace HexagonII;
1947 static const std::pair<unsigned, const char*> Flags[] = {
1948 {MO_PCREL, "hexagon-pcrel"},
1949 {MO_GOT, "hexagon-got"},
1950 {MO_LO16, "hexagon-lo16"},
1951 {MO_HI16, "hexagon-hi16"},
1952 {MO_GPREL, "hexagon-gprel"},
1953 {MO_GDGOT, "hexagon-gdgot"},
1954 {MO_GDPLT, "hexagon-gdplt"},
1955 {MO_IE, "hexagon-ie"},
1956 {MO_IEGOT, "hexagon-iegot"},
1957 {MO_TPREL, "hexagon-tprel"}
1959 return makeArrayRef(Flags);
1962 ArrayRef<std::pair<unsigned, const char*>>
1963 HexagonInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
1964 using namespace HexagonII;
1966 static const std::pair<unsigned, const char*> Flags[] = {
1967 {HMOTF_ConstExtended, "hexagon-ext"}
1969 return makeArrayRef(Flags);
1972 unsigned HexagonInstrInfo::createVR(MachineFunction *MF, MVT VT) const {
1973 MachineRegisterInfo &MRI = MF->getRegInfo();
1974 const TargetRegisterClass *TRC;
1975 if (VT == MVT::i1) {
1976 TRC = &Hexagon::PredRegsRegClass;
1977 } else if (VT == MVT::i32 || VT == MVT::f32) {
1978 TRC = &Hexagon::IntRegsRegClass;
1979 } else if (VT == MVT::i64 || VT == MVT::f64) {
1980 TRC = &Hexagon::DoubleRegsRegClass;
1981 } else {
1982 llvm_unreachable("Cannot handle this register class");
1985 Register NewReg = MRI.createVirtualRegister(TRC);
1986 return NewReg;
1989 bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr &MI) const {
1990 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1993 bool HexagonInstrInfo::isAccumulator(const MachineInstr &MI) const {
1994 const uint64_t F = MI.getDesc().TSFlags;
1995 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1998 bool HexagonInstrInfo::isBaseImmOffset(const MachineInstr &MI) const {
1999 return getAddrMode(MI) == HexagonII::BaseImmOffset;
2002 bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
2003 return !isTC1(MI) && !isTC2Early(MI) && !MI.getDesc().mayLoad() &&
2004 !MI.getDesc().mayStore() &&
2005 MI.getDesc().getOpcode() != Hexagon::S2_allocframe &&
2006 MI.getDesc().getOpcode() != Hexagon::L2_deallocframe &&
2007 !isMemOp(MI) && !MI.isBranch() && !MI.isReturn() && !MI.isCall();
2010 // Return true if the instruction is a compund branch instruction.
2011 bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
2012 return getType(MI) == HexagonII::TypeCJ && MI.isBranch();
2015 // TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
2016 // isFPImm and later getFPImm as well.
2017 bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
2018 const uint64_t F = MI.getDesc().TSFlags;
2019 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
2020 if (isExtended) // Instruction must be extended.
2021 return true;
2023 unsigned isExtendable =
2024 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
2025 if (!isExtendable)
2026 return false;
2028 if (MI.isCall())
2029 return false;
2031 short ExtOpNum = getCExtOpNum(MI);
2032 const MachineOperand &MO = MI.getOperand(ExtOpNum);
2033 // Use MO operand flags to determine if MO
2034 // has the HMOTF_ConstExtended flag set.
2035 if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
2036 return true;
2037 // If this is a Machine BB address we are talking about, and it is
2038 // not marked as extended, say so.
2039 if (MO.isMBB())
2040 return false;
2042 // We could be using an instruction with an extendable immediate and shoehorn
2043 // a global address into it. If it is a global address it will be constant
2044 // extended. We do this for COMBINE.
2045 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
2046 MO.isJTI() || MO.isCPI() || MO.isFPImm())
2047 return true;
2049 // If the extendable operand is not 'Immediate' type, the instruction should
2050 // have 'isExtended' flag set.
2051 assert(MO.isImm() && "Extendable operand must be Immediate type");
2053 int MinValue = getMinValue(MI);
2054 int MaxValue = getMaxValue(MI);
2055 int ImmValue = MO.getImm();
2057 return (ImmValue < MinValue || ImmValue > MaxValue);
2060 bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
2061 switch (MI.getOpcode()) {
2062 case Hexagon::L4_return:
2063 case Hexagon::L4_return_t:
2064 case Hexagon::L4_return_f:
2065 case Hexagon::L4_return_tnew_pnt:
2066 case Hexagon::L4_return_fnew_pnt:
2067 case Hexagon::L4_return_tnew_pt:
2068 case Hexagon::L4_return_fnew_pt:
2069 return true;
2071 return false;
2074 // Return true when ConsMI uses a register defined by ProdMI.
2075 bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
2076 const MachineInstr &ConsMI) const {
2077 if (!ProdMI.getDesc().getNumDefs())
2078 return false;
2079 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
2081 SmallVector<unsigned, 4> DefsA;
2082 SmallVector<unsigned, 4> DefsB;
2083 SmallVector<unsigned, 8> UsesA;
2084 SmallVector<unsigned, 8> UsesB;
2086 parseOperands(ProdMI, DefsA, UsesA);
2087 parseOperands(ConsMI, DefsB, UsesB);
2089 for (auto &RegA : DefsA)
2090 for (auto &RegB : UsesB) {
2091 // True data dependency.
2092 if (RegA == RegB)
2093 return true;
2095 if (Register::isPhysicalRegister(RegA))
2096 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
2097 if (RegB == *SubRegs)
2098 return true;
2100 if (Register::isPhysicalRegister(RegB))
2101 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
2102 if (RegA == *SubRegs)
2103 return true;
2106 return false;
2109 // Returns true if the instruction is alread a .cur.
2110 bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
2111 switch (MI.getOpcode()) {
2112 case Hexagon::V6_vL32b_cur_pi:
2113 case Hexagon::V6_vL32b_cur_ai:
2114 return true;
2116 return false;
2119 // Returns true, if any one of the operands is a dot new
2120 // insn, whether it is predicated dot new or register dot new.
2121 bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
2122 if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
2123 return true;
2125 return false;
2128 /// Symmetrical. See if these two instructions are fit for duplex pair.
2129 bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
2130 const MachineInstr &MIb) const {
2131 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
2132 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
2133 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
2136 bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
2137 if (MI.mayLoad() || MI.mayStore() || MI.isCompare())
2138 return true;
2140 // Multiply
2141 unsigned SchedClass = MI.getDesc().getSchedClass();
2142 return is_TC4x(SchedClass) || is_TC3x(SchedClass);
2145 bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
2146 return (Opcode == Hexagon::ENDLOOP0 ||
2147 Opcode == Hexagon::ENDLOOP1);
2150 bool HexagonInstrInfo::isExpr(unsigned OpType) const {
2151 switch(OpType) {
2152 case MachineOperand::MO_MachineBasicBlock:
2153 case MachineOperand::MO_GlobalAddress:
2154 case MachineOperand::MO_ExternalSymbol:
2155 case MachineOperand::MO_JumpTableIndex:
2156 case MachineOperand::MO_ConstantPoolIndex:
2157 case MachineOperand::MO_BlockAddress:
2158 return true;
2159 default:
2160 return false;
2164 bool HexagonInstrInfo::isExtendable(const MachineInstr &MI) const {
2165 const MCInstrDesc &MID = MI.getDesc();
2166 const uint64_t F = MID.TSFlags;
2167 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
2168 return true;
2170 // TODO: This is largely obsolete now. Will need to be removed
2171 // in consecutive patches.
2172 switch (MI.getOpcode()) {
2173 // PS_fi and PS_fia remain special cases.
2174 case Hexagon::PS_fi:
2175 case Hexagon::PS_fia:
2176 return true;
2177 default:
2178 return false;
2180 return false;
2183 // This returns true in two cases:
2184 // - The OP code itself indicates that this is an extended instruction.
2185 // - One of MOs has been marked with HMOTF_ConstExtended flag.
2186 bool HexagonInstrInfo::isExtended(const MachineInstr &MI) const {
2187 // First check if this is permanently extended op code.
2188 const uint64_t F = MI.getDesc().TSFlags;
2189 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2190 return true;
2191 // Use MO operand flags to determine if one of MI's operands
2192 // has HMOTF_ConstExtended flag set.
2193 for (const MachineOperand &MO : MI.operands())
2194 if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
2195 return true;
2196 return false;
2199 bool HexagonInstrInfo::isFloat(const MachineInstr &MI) const {
2200 unsigned Opcode = MI.getOpcode();
2201 const uint64_t F = get(Opcode).TSFlags;
2202 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2205 // No V60 HVX VMEM with A_INDIRECT.
2206 bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr &I,
2207 const MachineInstr &J) const {
2208 if (!isHVXVec(I))
2209 return false;
2210 if (!I.mayLoad() && !I.mayStore())
2211 return false;
2212 return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
2215 bool HexagonInstrInfo::isIndirectCall(const MachineInstr &MI) const {
2216 switch (MI.getOpcode()) {
2217 case Hexagon::J2_callr:
2218 case Hexagon::J2_callrf:
2219 case Hexagon::J2_callrt:
2220 case Hexagon::PS_call_nr:
2221 return true;
2223 return false;
2226 bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr &MI) const {
2227 switch (MI.getOpcode()) {
2228 case Hexagon::L4_return:
2229 case Hexagon::L4_return_t:
2230 case Hexagon::L4_return_f:
2231 case Hexagon::L4_return_fnew_pnt:
2232 case Hexagon::L4_return_fnew_pt:
2233 case Hexagon::L4_return_tnew_pnt:
2234 case Hexagon::L4_return_tnew_pt:
2235 return true;
2237 return false;
2240 bool HexagonInstrInfo::isJumpR(const MachineInstr &MI) const {
2241 switch (MI.getOpcode()) {
2242 case Hexagon::J2_jumpr:
2243 case Hexagon::J2_jumprt:
2244 case Hexagon::J2_jumprf:
2245 case Hexagon::J2_jumprtnewpt:
2246 case Hexagon::J2_jumprfnewpt:
2247 case Hexagon::J2_jumprtnew:
2248 case Hexagon::J2_jumprfnew:
2249 return true;
2251 return false;
2254 // Return true if a given MI can accommodate given offset.
2255 // Use abs estimate as oppose to the exact number.
2256 // TODO: This will need to be changed to use MC level
2257 // definition of instruction extendable field size.
2258 bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr &MI,
2259 unsigned offset) const {
2260 // This selection of jump instructions matches to that what
2261 // analyzeBranch can parse, plus NVJ.
2262 if (isNewValueJump(MI)) // r9:2
2263 return isInt<11>(offset);
2265 switch (MI.getOpcode()) {
2266 // Still missing Jump to address condition on register value.
2267 default:
2268 return false;
2269 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2270 case Hexagon::J2_call:
2271 case Hexagon::PS_call_nr:
2272 return isInt<24>(offset);
2273 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2274 case Hexagon::J2_jumpf:
2275 case Hexagon::J2_jumptnew:
2276 case Hexagon::J2_jumptnewpt:
2277 case Hexagon::J2_jumpfnew:
2278 case Hexagon::J2_jumpfnewpt:
2279 case Hexagon::J2_callt:
2280 case Hexagon::J2_callf:
2281 return isInt<17>(offset);
2282 case Hexagon::J2_loop0i:
2283 case Hexagon::J2_loop0iext:
2284 case Hexagon::J2_loop0r:
2285 case Hexagon::J2_loop0rext:
2286 case Hexagon::J2_loop1i:
2287 case Hexagon::J2_loop1iext:
2288 case Hexagon::J2_loop1r:
2289 case Hexagon::J2_loop1rext:
2290 return isInt<9>(offset);
2291 // TODO: Add all the compound branches here. Can we do this in Relation model?
2292 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2293 case Hexagon::J4_cmpeqi_tp1_jump_nt:
2294 case Hexagon::J4_cmpeqn1_tp0_jump_nt:
2295 case Hexagon::J4_cmpeqn1_tp1_jump_nt:
2296 return isInt<11>(offset);
2300 bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr &LRMI,
2301 const MachineInstr &ESMI) const {
2302 bool isLate = isLateResultInstr(LRMI);
2303 bool isEarly = isEarlySourceInstr(ESMI);
2305 LLVM_DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
2306 LLVM_DEBUG(LRMI.dump());
2307 LLVM_DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
2308 LLVM_DEBUG(ESMI.dump());
2310 if (isLate && isEarly) {
2311 LLVM_DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2312 return true;
2315 return false;
2318 bool HexagonInstrInfo::isLateResultInstr(const MachineInstr &MI) const {
2319 switch (MI.getOpcode()) {
2320 case TargetOpcode::EXTRACT_SUBREG:
2321 case TargetOpcode::INSERT_SUBREG:
2322 case TargetOpcode::SUBREG_TO_REG:
2323 case TargetOpcode::REG_SEQUENCE:
2324 case TargetOpcode::IMPLICIT_DEF:
2325 case TargetOpcode::COPY:
2326 case TargetOpcode::INLINEASM:
2327 case TargetOpcode::PHI:
2328 return false;
2329 default:
2330 break;
2333 unsigned SchedClass = MI.getDesc().getSchedClass();
2334 return !is_TC1(SchedClass);
2337 bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
2338 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2339 // resource, but all operands can be received late like an ALU instruction.
2340 return getType(MI) == HexagonII::TypeCVI_VX_LATE;
2343 bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2344 unsigned Opcode = MI.getOpcode();
2345 return Opcode == Hexagon::J2_loop0i ||
2346 Opcode == Hexagon::J2_loop0r ||
2347 Opcode == Hexagon::J2_loop0iext ||
2348 Opcode == Hexagon::J2_loop0rext ||
2349 Opcode == Hexagon::J2_loop1i ||
2350 Opcode == Hexagon::J2_loop1r ||
2351 Opcode == Hexagon::J2_loop1iext ||
2352 Opcode == Hexagon::J2_loop1rext;
2355 bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2356 switch (MI.getOpcode()) {
2357 default: return false;
2358 case Hexagon::L4_iadd_memopw_io:
2359 case Hexagon::L4_isub_memopw_io:
2360 case Hexagon::L4_add_memopw_io:
2361 case Hexagon::L4_sub_memopw_io:
2362 case Hexagon::L4_and_memopw_io:
2363 case Hexagon::L4_or_memopw_io:
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 case Hexagon::L4_iadd_memopb_io:
2371 case Hexagon::L4_isub_memopb_io:
2372 case Hexagon::L4_add_memopb_io:
2373 case Hexagon::L4_sub_memopb_io:
2374 case Hexagon::L4_and_memopb_io:
2375 case Hexagon::L4_or_memopb_io:
2376 case Hexagon::L4_ior_memopb_io:
2377 case Hexagon::L4_ior_memoph_io:
2378 case Hexagon::L4_ior_memopw_io:
2379 case Hexagon::L4_iand_memopb_io:
2380 case Hexagon::L4_iand_memoph_io:
2381 case Hexagon::L4_iand_memopw_io:
2382 return true;
2384 return false;
2387 bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2388 const uint64_t F = MI.getDesc().TSFlags;
2389 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2392 bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2393 const uint64_t F = get(Opcode).TSFlags;
2394 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2397 bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
2398 return isNewValueJump(MI) || isNewValueStore(MI);
2401 bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2402 return isNewValue(MI) && MI.isBranch();
2405 bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2406 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2409 bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2410 const uint64_t F = MI.getDesc().TSFlags;
2411 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2414 bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2415 const uint64_t F = get(Opcode).TSFlags;
2416 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2419 // Returns true if a particular operand is extendable for an instruction.
2420 bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
2421 unsigned OperandNum) const {
2422 const uint64_t F = MI.getDesc().TSFlags;
2423 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2424 == OperandNum;
2427 bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2428 const uint64_t F = MI.getDesc().TSFlags;
2429 assert(isPredicated(MI));
2430 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2433 bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2434 const uint64_t F = get(Opcode).TSFlags;
2435 assert(isPredicated(Opcode));
2436 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2439 bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2440 const uint64_t F = MI.getDesc().TSFlags;
2441 return !((F >> HexagonII::PredicatedFalsePos) &
2442 HexagonII::PredicatedFalseMask);
2445 bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2446 const uint64_t F = get(Opcode).TSFlags;
2447 // Make sure that the instruction is predicated.
2448 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2449 return !((F >> HexagonII::PredicatedFalsePos) &
2450 HexagonII::PredicatedFalseMask);
2453 bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2454 const uint64_t F = get(Opcode).TSFlags;
2455 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2458 bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2459 const uint64_t F = get(Opcode).TSFlags;
2460 return (F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2463 bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2464 const uint64_t F = get(Opcode).TSFlags;
2465 assert(get(Opcode).isBranch() &&
2466 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2467 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2470 bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2471 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2472 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2473 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2474 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
2477 bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2478 switch (MI.getOpcode()) {
2479 // Byte
2480 case Hexagon::L2_loadrb_io:
2481 case Hexagon::L4_loadrb_ur:
2482 case Hexagon::L4_loadrb_ap:
2483 case Hexagon::L2_loadrb_pr:
2484 case Hexagon::L2_loadrb_pbr:
2485 case Hexagon::L2_loadrb_pi:
2486 case Hexagon::L2_loadrb_pci:
2487 case Hexagon::L2_loadrb_pcr:
2488 case Hexagon::L2_loadbsw2_io:
2489 case Hexagon::L4_loadbsw2_ur:
2490 case Hexagon::L4_loadbsw2_ap:
2491 case Hexagon::L2_loadbsw2_pr:
2492 case Hexagon::L2_loadbsw2_pbr:
2493 case Hexagon::L2_loadbsw2_pi:
2494 case Hexagon::L2_loadbsw2_pci:
2495 case Hexagon::L2_loadbsw2_pcr:
2496 case Hexagon::L2_loadbsw4_io:
2497 case Hexagon::L4_loadbsw4_ur:
2498 case Hexagon::L4_loadbsw4_ap:
2499 case Hexagon::L2_loadbsw4_pr:
2500 case Hexagon::L2_loadbsw4_pbr:
2501 case Hexagon::L2_loadbsw4_pi:
2502 case Hexagon::L2_loadbsw4_pci:
2503 case Hexagon::L2_loadbsw4_pcr:
2504 case Hexagon::L4_loadrb_rr:
2505 case Hexagon::L2_ploadrbt_io:
2506 case Hexagon::L2_ploadrbt_pi:
2507 case Hexagon::L2_ploadrbf_io:
2508 case Hexagon::L2_ploadrbf_pi:
2509 case Hexagon::L2_ploadrbtnew_io:
2510 case Hexagon::L2_ploadrbfnew_io:
2511 case Hexagon::L4_ploadrbt_rr:
2512 case Hexagon::L4_ploadrbf_rr:
2513 case Hexagon::L4_ploadrbtnew_rr:
2514 case Hexagon::L4_ploadrbfnew_rr:
2515 case Hexagon::L2_ploadrbtnew_pi:
2516 case Hexagon::L2_ploadrbfnew_pi:
2517 case Hexagon::L4_ploadrbt_abs:
2518 case Hexagon::L4_ploadrbf_abs:
2519 case Hexagon::L4_ploadrbtnew_abs:
2520 case Hexagon::L4_ploadrbfnew_abs:
2521 case Hexagon::L2_loadrbgp:
2522 // Half
2523 case Hexagon::L2_loadrh_io:
2524 case Hexagon::L4_loadrh_ur:
2525 case Hexagon::L4_loadrh_ap:
2526 case Hexagon::L2_loadrh_pr:
2527 case Hexagon::L2_loadrh_pbr:
2528 case Hexagon::L2_loadrh_pi:
2529 case Hexagon::L2_loadrh_pci:
2530 case Hexagon::L2_loadrh_pcr:
2531 case Hexagon::L4_loadrh_rr:
2532 case Hexagon::L2_ploadrht_io:
2533 case Hexagon::L2_ploadrht_pi:
2534 case Hexagon::L2_ploadrhf_io:
2535 case Hexagon::L2_ploadrhf_pi:
2536 case Hexagon::L2_ploadrhtnew_io:
2537 case Hexagon::L2_ploadrhfnew_io:
2538 case Hexagon::L4_ploadrht_rr:
2539 case Hexagon::L4_ploadrhf_rr:
2540 case Hexagon::L4_ploadrhtnew_rr:
2541 case Hexagon::L4_ploadrhfnew_rr:
2542 case Hexagon::L2_ploadrhtnew_pi:
2543 case Hexagon::L2_ploadrhfnew_pi:
2544 case Hexagon::L4_ploadrht_abs:
2545 case Hexagon::L4_ploadrhf_abs:
2546 case Hexagon::L4_ploadrhtnew_abs:
2547 case Hexagon::L4_ploadrhfnew_abs:
2548 case Hexagon::L2_loadrhgp:
2549 return true;
2550 default:
2551 return false;
2555 bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2556 const uint64_t F = MI.getDesc().TSFlags;
2557 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2560 bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2561 switch (MI.getOpcode()) {
2562 case Hexagon::STriw_pred:
2563 case Hexagon::LDriw_pred:
2564 return true;
2565 default:
2566 return false;
2570 bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2571 if (!MI.isBranch())
2572 return false;
2574 for (auto &Op : MI.operands())
2575 if (Op.isGlobal() || Op.isSymbol())
2576 return true;
2577 return false;
2580 // Returns true when SU has a timing class TC1.
2581 bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2582 unsigned SchedClass = MI.getDesc().getSchedClass();
2583 return is_TC1(SchedClass);
2586 bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2587 unsigned SchedClass = MI.getDesc().getSchedClass();
2588 return is_TC2(SchedClass);
2591 bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2592 unsigned SchedClass = MI.getDesc().getSchedClass();
2593 return is_TC2early(SchedClass);
2596 bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2597 unsigned SchedClass = MI.getDesc().getSchedClass();
2598 return is_TC4x(SchedClass);
2601 // Schedule this ASAP.
2602 bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2603 const MachineInstr &MI2) const {
2604 if (mayBeCurLoad(MI1)) {
2605 // if (result of SU is used in Next) return true;
2606 Register DstReg = MI1.getOperand(0).getReg();
2607 int N = MI2.getNumOperands();
2608 for (int I = 0; I < N; I++)
2609 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
2610 return true;
2612 if (mayBeNewStore(MI2))
2613 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2614 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2615 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
2616 return true;
2617 return false;
2620 bool HexagonInstrInfo::isHVXVec(const MachineInstr &MI) const {
2621 const uint64_t V = getType(MI);
2622 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2625 // Check if the Offset is a valid auto-inc imm by Load/Store Type.
2626 bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, int Offset) const {
2627 int Size = VT.getSizeInBits() / 8;
2628 if (Offset % Size != 0)
2629 return false;
2630 int Count = Offset / Size;
2632 switch (VT.getSimpleVT().SimpleTy) {
2633 // For scalars the auto-inc is s4
2634 case MVT::i8:
2635 case MVT::i16:
2636 case MVT::i32:
2637 case MVT::i64:
2638 case MVT::f32:
2639 case MVT::f64:
2640 case MVT::v2i16:
2641 case MVT::v2i32:
2642 case MVT::v4i8:
2643 case MVT::v4i16:
2644 case MVT::v8i8:
2645 return isInt<4>(Count);
2646 // For HVX vectors the auto-inc is s3
2647 case MVT::v64i8:
2648 case MVT::v32i16:
2649 case MVT::v16i32:
2650 case MVT::v8i64:
2651 case MVT::v128i8:
2652 case MVT::v64i16:
2653 case MVT::v32i32:
2654 case MVT::v16i64:
2655 return isInt<3>(Count);
2656 default:
2657 break;
2660 llvm_unreachable("Not an valid type!");
2663 bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2664 const TargetRegisterInfo *TRI, bool Extend) const {
2665 // This function is to check whether the "Offset" is in the correct range of
2666 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
2667 // inserted to calculate the final address. Due to this reason, the function
2668 // assumes that the "Offset" has correct alignment.
2669 // We used to assert if the offset was not properly aligned, however,
2670 // there are cases where a misaligned pointer recast can cause this
2671 // problem, and we need to allow for it. The front end warns of such
2672 // misaligns with respect to load size.
2673 switch (Opcode) {
2674 case Hexagon::PS_vstorerq_ai:
2675 case Hexagon::PS_vstorerw_ai:
2676 case Hexagon::PS_vstorerw_nt_ai:
2677 case Hexagon::PS_vloadrq_ai:
2678 case Hexagon::PS_vloadrw_ai:
2679 case Hexagon::PS_vloadrw_nt_ai:
2680 case Hexagon::V6_vL32b_ai:
2681 case Hexagon::V6_vS32b_ai:
2682 case Hexagon::V6_vL32b_nt_ai:
2683 case Hexagon::V6_vS32b_nt_ai:
2684 case Hexagon::V6_vL32Ub_ai:
2685 case Hexagon::V6_vS32Ub_ai: {
2686 unsigned VectorSize = TRI->getSpillSize(Hexagon::HvxVRRegClass);
2687 assert(isPowerOf2_32(VectorSize));
2688 if (Offset & (VectorSize-1))
2689 return false;
2690 return isInt<4>(Offset >> Log2_32(VectorSize));
2693 case Hexagon::J2_loop0i:
2694 case Hexagon::J2_loop1i:
2695 return isUInt<10>(Offset);
2697 case Hexagon::S4_storeirb_io:
2698 case Hexagon::S4_storeirbt_io:
2699 case Hexagon::S4_storeirbf_io:
2700 return isUInt<6>(Offset);
2702 case Hexagon::S4_storeirh_io:
2703 case Hexagon::S4_storeirht_io:
2704 case Hexagon::S4_storeirhf_io:
2705 return isShiftedUInt<6,1>(Offset);
2707 case Hexagon::S4_storeiri_io:
2708 case Hexagon::S4_storeirit_io:
2709 case Hexagon::S4_storeirif_io:
2710 return isShiftedUInt<6,2>(Offset);
2713 if (Extend)
2714 return true;
2716 switch (Opcode) {
2717 case Hexagon::L2_loadri_io:
2718 case Hexagon::S2_storeri_io:
2719 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2720 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2722 case Hexagon::L2_loadrd_io:
2723 case Hexagon::S2_storerd_io:
2724 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2725 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2727 case Hexagon::L2_loadrh_io:
2728 case Hexagon::L2_loadruh_io:
2729 case Hexagon::S2_storerh_io:
2730 case Hexagon::S2_storerf_io:
2731 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2732 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2734 case Hexagon::L2_loadrb_io:
2735 case Hexagon::L2_loadrub_io:
2736 case Hexagon::S2_storerb_io:
2737 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2738 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2740 case Hexagon::A2_addi:
2741 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2742 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2744 case Hexagon::L4_iadd_memopw_io:
2745 case Hexagon::L4_isub_memopw_io:
2746 case Hexagon::L4_add_memopw_io:
2747 case Hexagon::L4_sub_memopw_io:
2748 case Hexagon::L4_and_memopw_io:
2749 case Hexagon::L4_or_memopw_io:
2750 return (0 <= Offset && Offset <= 255);
2752 case Hexagon::L4_iadd_memoph_io:
2753 case Hexagon::L4_isub_memoph_io:
2754 case Hexagon::L4_add_memoph_io:
2755 case Hexagon::L4_sub_memoph_io:
2756 case Hexagon::L4_and_memoph_io:
2757 case Hexagon::L4_or_memoph_io:
2758 return (0 <= Offset && Offset <= 127);
2760 case Hexagon::L4_iadd_memopb_io:
2761 case Hexagon::L4_isub_memopb_io:
2762 case Hexagon::L4_add_memopb_io:
2763 case Hexagon::L4_sub_memopb_io:
2764 case Hexagon::L4_and_memopb_io:
2765 case Hexagon::L4_or_memopb_io:
2766 return (0 <= Offset && Offset <= 63);
2768 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
2769 // any size. Later pass knows how to handle it.
2770 case Hexagon::STriw_pred:
2771 case Hexagon::LDriw_pred:
2772 case Hexagon::STriw_ctr:
2773 case Hexagon::LDriw_ctr:
2774 return true;
2776 case Hexagon::PS_fi:
2777 case Hexagon::PS_fia:
2778 case Hexagon::INLINEASM:
2779 return true;
2781 case Hexagon::L2_ploadrbt_io:
2782 case Hexagon::L2_ploadrbf_io:
2783 case Hexagon::L2_ploadrubt_io:
2784 case Hexagon::L2_ploadrubf_io:
2785 case Hexagon::S2_pstorerbt_io:
2786 case Hexagon::S2_pstorerbf_io:
2787 return isUInt<6>(Offset);
2789 case Hexagon::L2_ploadrht_io:
2790 case Hexagon::L2_ploadrhf_io:
2791 case Hexagon::L2_ploadruht_io:
2792 case Hexagon::L2_ploadruhf_io:
2793 case Hexagon::S2_pstorerht_io:
2794 case Hexagon::S2_pstorerhf_io:
2795 return isShiftedUInt<6,1>(Offset);
2797 case Hexagon::L2_ploadrit_io:
2798 case Hexagon::L2_ploadrif_io:
2799 case Hexagon::S2_pstorerit_io:
2800 case Hexagon::S2_pstorerif_io:
2801 return isShiftedUInt<6,2>(Offset);
2803 case Hexagon::L2_ploadrdt_io:
2804 case Hexagon::L2_ploadrdf_io:
2805 case Hexagon::S2_pstorerdt_io:
2806 case Hexagon::S2_pstorerdf_io:
2807 return isShiftedUInt<6,3>(Offset);
2808 } // switch
2810 llvm_unreachable("No offset range is defined for this opcode. "
2811 "Please define it in the above switch statement!");
2814 bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
2815 return isHVXVec(MI) && isAccumulator(MI);
2818 bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2819 const uint64_t F = get(MI.getOpcode()).TSFlags;
2820 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2821 return
2822 V == HexagonII::TypeCVI_VA ||
2823 V == HexagonII::TypeCVI_VA_DV;
2826 bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2827 const MachineInstr &ConsMI) const {
2828 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2829 return true;
2831 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2832 return true;
2834 if (mayBeNewStore(ConsMI))
2835 return true;
2837 return false;
2840 bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2841 switch (MI.getOpcode()) {
2842 // Byte
2843 case Hexagon::L2_loadrub_io:
2844 case Hexagon::L4_loadrub_ur:
2845 case Hexagon::L4_loadrub_ap:
2846 case Hexagon::L2_loadrub_pr:
2847 case Hexagon::L2_loadrub_pbr:
2848 case Hexagon::L2_loadrub_pi:
2849 case Hexagon::L2_loadrub_pci:
2850 case Hexagon::L2_loadrub_pcr:
2851 case Hexagon::L2_loadbzw2_io:
2852 case Hexagon::L4_loadbzw2_ur:
2853 case Hexagon::L4_loadbzw2_ap:
2854 case Hexagon::L2_loadbzw2_pr:
2855 case Hexagon::L2_loadbzw2_pbr:
2856 case Hexagon::L2_loadbzw2_pi:
2857 case Hexagon::L2_loadbzw2_pci:
2858 case Hexagon::L2_loadbzw2_pcr:
2859 case Hexagon::L2_loadbzw4_io:
2860 case Hexagon::L4_loadbzw4_ur:
2861 case Hexagon::L4_loadbzw4_ap:
2862 case Hexagon::L2_loadbzw4_pr:
2863 case Hexagon::L2_loadbzw4_pbr:
2864 case Hexagon::L2_loadbzw4_pi:
2865 case Hexagon::L2_loadbzw4_pci:
2866 case Hexagon::L2_loadbzw4_pcr:
2867 case Hexagon::L4_loadrub_rr:
2868 case Hexagon::L2_ploadrubt_io:
2869 case Hexagon::L2_ploadrubt_pi:
2870 case Hexagon::L2_ploadrubf_io:
2871 case Hexagon::L2_ploadrubf_pi:
2872 case Hexagon::L2_ploadrubtnew_io:
2873 case Hexagon::L2_ploadrubfnew_io:
2874 case Hexagon::L4_ploadrubt_rr:
2875 case Hexagon::L4_ploadrubf_rr:
2876 case Hexagon::L4_ploadrubtnew_rr:
2877 case Hexagon::L4_ploadrubfnew_rr:
2878 case Hexagon::L2_ploadrubtnew_pi:
2879 case Hexagon::L2_ploadrubfnew_pi:
2880 case Hexagon::L4_ploadrubt_abs:
2881 case Hexagon::L4_ploadrubf_abs:
2882 case Hexagon::L4_ploadrubtnew_abs:
2883 case Hexagon::L4_ploadrubfnew_abs:
2884 case Hexagon::L2_loadrubgp:
2885 // Half
2886 case Hexagon::L2_loadruh_io:
2887 case Hexagon::L4_loadruh_ur:
2888 case Hexagon::L4_loadruh_ap:
2889 case Hexagon::L2_loadruh_pr:
2890 case Hexagon::L2_loadruh_pbr:
2891 case Hexagon::L2_loadruh_pi:
2892 case Hexagon::L2_loadruh_pci:
2893 case Hexagon::L2_loadruh_pcr:
2894 case Hexagon::L4_loadruh_rr:
2895 case Hexagon::L2_ploadruht_io:
2896 case Hexagon::L2_ploadruht_pi:
2897 case Hexagon::L2_ploadruhf_io:
2898 case Hexagon::L2_ploadruhf_pi:
2899 case Hexagon::L2_ploadruhtnew_io:
2900 case Hexagon::L2_ploadruhfnew_io:
2901 case Hexagon::L4_ploadruht_rr:
2902 case Hexagon::L4_ploadruhf_rr:
2903 case Hexagon::L4_ploadruhtnew_rr:
2904 case Hexagon::L4_ploadruhfnew_rr:
2905 case Hexagon::L2_ploadruhtnew_pi:
2906 case Hexagon::L2_ploadruhfnew_pi:
2907 case Hexagon::L4_ploadruht_abs:
2908 case Hexagon::L4_ploadruhf_abs:
2909 case Hexagon::L4_ploadruhtnew_abs:
2910 case Hexagon::L4_ploadruhfnew_abs:
2911 case Hexagon::L2_loadruhgp:
2912 return true;
2913 default:
2914 return false;
2918 // Add latency to instruction.
2919 bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2920 const MachineInstr &MI2) const {
2921 if (isHVXVec(MI1) && isHVXVec(MI2))
2922 if (!isVecUsableNextPacket(MI1, MI2))
2923 return true;
2924 return false;
2927 /// Get the base register and byte offset of a load/store instr.
2928 bool HexagonInstrInfo::getMemOperandWithOffset(
2929 const MachineInstr &LdSt, const MachineOperand *&BaseOp, int64_t &Offset,
2930 const TargetRegisterInfo *TRI) const {
2931 unsigned AccessSize = 0;
2932 BaseOp = getBaseAndOffset(LdSt, Offset, AccessSize);
2933 assert((!BaseOp || BaseOp->isReg()) &&
2934 "getMemOperandWithOffset only supports base "
2935 "operands of type register.");
2936 return BaseOp != nullptr;
2939 /// Can these instructions execute at the same time in a bundle.
2940 bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2941 const MachineInstr &Second) const {
2942 if (Second.mayStore() && First.getOpcode() == Hexagon::S2_allocframe) {
2943 const MachineOperand &Op = Second.getOperand(0);
2944 if (Op.isReg() && Op.isUse() && Op.getReg() == Hexagon::R29)
2945 return true;
2947 if (DisableNVSchedule)
2948 return false;
2949 if (mayBeNewStore(Second)) {
2950 // Make sure the definition of the first instruction is the value being
2951 // stored.
2952 const MachineOperand &Stored =
2953 Second.getOperand(Second.getNumOperands() - 1);
2954 if (!Stored.isReg())
2955 return false;
2956 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
2957 const MachineOperand &Op = First.getOperand(i);
2958 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2959 return true;
2962 return false;
2965 bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
2966 unsigned Opc = CallMI.getOpcode();
2967 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
2970 bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2971 for (auto &I : *B)
2972 if (I.isEHLabel())
2973 return true;
2974 return false;
2977 // Returns true if an instruction can be converted into a non-extended
2978 // equivalent instruction.
2979 bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
2980 short NonExtOpcode;
2981 // Check if the instruction has a register form that uses register in place
2982 // of the extended operand, if so return that as the non-extended form.
2983 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
2984 return true;
2986 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
2987 // Check addressing mode and retrieve non-ext equivalent instruction.
2989 switch (getAddrMode(MI)) {
2990 case HexagonII::Absolute:
2991 // Load/store with absolute addressing mode can be converted into
2992 // base+offset mode.
2993 NonExtOpcode = Hexagon::changeAddrMode_abs_io(MI.getOpcode());
2994 break;
2995 case HexagonII::BaseImmOffset:
2996 // Load/store with base+offset addressing mode can be converted into
2997 // base+register offset addressing mode. However left shift operand should
2998 // be set to 0.
2999 NonExtOpcode = Hexagon::changeAddrMode_io_rr(MI.getOpcode());
3000 break;
3001 case HexagonII::BaseLongOffset:
3002 NonExtOpcode = Hexagon::changeAddrMode_ur_rr(MI.getOpcode());
3003 break;
3004 default:
3005 return false;
3007 if (NonExtOpcode < 0)
3008 return false;
3009 return true;
3011 return false;
3014 bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
3015 return Hexagon::getRealHWInstr(MI.getOpcode(),
3016 Hexagon::InstrType_Pseudo) >= 0;
3019 bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
3020 const {
3021 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
3022 while (I != E) {
3023 if (I->isBarrier())
3024 return true;
3025 ++I;
3027 return false;
3030 // Returns true, if a LD insn can be promoted to a cur load.
3031 bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
3032 const uint64_t F = MI.getDesc().TSFlags;
3033 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
3034 Subtarget.hasV60Ops();
3037 // Returns true, if a ST insn can be promoted to a new-value store.
3038 bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
3039 if (MI.mayStore() && !Subtarget.useNewValueStores())
3040 return false;
3042 const uint64_t F = MI.getDesc().TSFlags;
3043 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
3046 bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
3047 const MachineInstr &ConsMI) const {
3048 // There is no stall when ProdMI is not a V60 vector.
3049 if (!isHVXVec(ProdMI))
3050 return false;
3052 // There is no stall when ProdMI and ConsMI are not dependent.
3053 if (!isDependent(ProdMI, ConsMI))
3054 return false;
3056 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
3057 // are scheduled in consecutive packets.
3058 if (isVecUsableNextPacket(ProdMI, ConsMI))
3059 return false;
3061 return true;
3064 bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
3065 MachineBasicBlock::const_instr_iterator BII) const {
3066 // There is no stall when I is not a V60 vector.
3067 if (!isHVXVec(MI))
3068 return false;
3070 MachineBasicBlock::const_instr_iterator MII = BII;
3071 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
3073 if (!MII->isBundle())
3074 return producesStall(*MII, MI);
3076 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
3077 const MachineInstr &J = *MII;
3078 if (producesStall(J, MI))
3079 return true;
3081 return false;
3084 bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
3085 unsigned PredReg) const {
3086 for (const MachineOperand &MO : MI.operands()) {
3087 // Predicate register must be explicitly defined.
3088 if (MO.isRegMask() && MO.clobbersPhysReg(PredReg))
3089 return false;
3090 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
3091 return false;
3094 // Instruction that produce late predicate cannot be used as sources of
3095 // dot-new.
3096 switch (MI.getOpcode()) {
3097 case Hexagon::A4_addp_c:
3098 case Hexagon::A4_subp_c:
3099 case Hexagon::A4_tlbmatch:
3100 case Hexagon::A5_ACS:
3101 case Hexagon::F2_sfinvsqrta:
3102 case Hexagon::F2_sfrecipa:
3103 case Hexagon::J2_endloop0:
3104 case Hexagon::J2_endloop01:
3105 case Hexagon::J2_ploop1si:
3106 case Hexagon::J2_ploop1sr:
3107 case Hexagon::J2_ploop2si:
3108 case Hexagon::J2_ploop2sr:
3109 case Hexagon::J2_ploop3si:
3110 case Hexagon::J2_ploop3sr:
3111 case Hexagon::S2_cabacdecbin:
3112 case Hexagon::S2_storew_locked:
3113 case Hexagon::S4_stored_locked:
3114 return false;
3116 return true;
3119 bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
3120 return Opcode == Hexagon::J2_jumpt ||
3121 Opcode == Hexagon::J2_jumptpt ||
3122 Opcode == Hexagon::J2_jumpf ||
3123 Opcode == Hexagon::J2_jumpfpt ||
3124 Opcode == Hexagon::J2_jumptnew ||
3125 Opcode == Hexagon::J2_jumpfnew ||
3126 Opcode == Hexagon::J2_jumptnewpt ||
3127 Opcode == Hexagon::J2_jumpfnewpt;
3130 bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
3131 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
3132 return false;
3133 return !isPredicatedTrue(Cond[0].getImm());
3136 unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
3137 const uint64_t F = MI.getDesc().TSFlags;
3138 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3141 // Returns the base register in a memory access (load/store). The offset is
3142 // returned in Offset and the access size is returned in AccessSize.
3143 // If the base operand has a subregister or the offset field does not contain
3144 // an immediate value, return nullptr.
3145 MachineOperand *HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
3146 int64_t &Offset,
3147 unsigned &AccessSize) const {
3148 // Return if it is not a base+offset type instruction or a MemOp.
3149 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3150 getAddrMode(MI) != HexagonII::BaseLongOffset &&
3151 !isMemOp(MI) && !isPostIncrement(MI))
3152 return nullptr;
3154 AccessSize = getMemAccessSize(MI);
3156 unsigned BasePos = 0, OffsetPos = 0;
3157 if (!getBaseAndOffsetPosition(MI, BasePos, OffsetPos))
3158 return nullptr;
3160 // Post increment updates its EA after the mem access,
3161 // so we need to treat its offset as zero.
3162 if (isPostIncrement(MI)) {
3163 Offset = 0;
3164 } else {
3165 const MachineOperand &OffsetOp = MI.getOperand(OffsetPos);
3166 if (!OffsetOp.isImm())
3167 return nullptr;
3168 Offset = OffsetOp.getImm();
3171 const MachineOperand &BaseOp = MI.getOperand(BasePos);
3172 if (BaseOp.getSubReg() != 0)
3173 return nullptr;
3174 return &const_cast<MachineOperand&>(BaseOp);
3177 /// Return the position of the base and offset operands for this instruction.
3178 bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
3179 unsigned &BasePos, unsigned &OffsetPos) const {
3180 if (!isAddrModeWithOffset(MI) && !isPostIncrement(MI))
3181 return false;
3183 // Deal with memops first.
3184 if (isMemOp(MI)) {
3185 BasePos = 0;
3186 OffsetPos = 1;
3187 } else if (MI.mayStore()) {
3188 BasePos = 0;
3189 OffsetPos = 1;
3190 } else if (MI.mayLoad()) {
3191 BasePos = 1;
3192 OffsetPos = 2;
3193 } else
3194 return false;
3196 if (isPredicated(MI)) {
3197 BasePos++;
3198 OffsetPos++;
3200 if (isPostIncrement(MI)) {
3201 BasePos++;
3202 OffsetPos++;
3205 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
3206 return false;
3208 return true;
3211 // Inserts branching instructions in reverse order of their occurrence.
3212 // e.g. jump_t t1 (i1)
3213 // jump t2 (i2)
3214 // Jumpers = {i2, i1}
3215 SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3216 MachineBasicBlock& MBB) const {
3217 SmallVector<MachineInstr*, 2> Jumpers;
3218 // If the block has no terminators, it just falls into the block after it.
3219 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3220 if (I == MBB.instr_begin())
3221 return Jumpers;
3223 // A basic block may looks like this:
3225 // [ insn
3226 // EH_LABEL
3227 // insn
3228 // insn
3229 // insn
3230 // EH_LABEL
3231 // insn ]
3233 // It has two succs but does not have a terminator
3234 // Don't know how to handle it.
3235 do {
3236 --I;
3237 if (I->isEHLabel())
3238 return Jumpers;
3239 } while (I != MBB.instr_begin());
3241 I = MBB.instr_end();
3242 --I;
3244 while (I->isDebugInstr()) {
3245 if (I == MBB.instr_begin())
3246 return Jumpers;
3247 --I;
3249 if (!isUnpredicatedTerminator(*I))
3250 return Jumpers;
3252 // Get the last instruction in the block.
3253 MachineInstr *LastInst = &*I;
3254 Jumpers.push_back(LastInst);
3255 MachineInstr *SecondLastInst = nullptr;
3256 // Find one more terminator if present.
3257 do {
3258 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
3259 if (!SecondLastInst) {
3260 SecondLastInst = &*I;
3261 Jumpers.push_back(SecondLastInst);
3262 } else // This is a third branch.
3263 return Jumpers;
3265 if (I == MBB.instr_begin())
3266 break;
3267 --I;
3268 } while (true);
3269 return Jumpers;
3272 // Returns Operand Index for the constant extended instruction.
3273 unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
3274 const uint64_t F = MI.getDesc().TSFlags;
3275 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3278 // See if instruction could potentially be a duplex candidate.
3279 // If so, return its group. Zero otherwise.
3280 HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
3281 const MachineInstr &MI) const {
3282 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3284 switch (MI.getOpcode()) {
3285 default:
3286 return HexagonII::HCG_None;
3288 // Compound pairs.
3289 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3290 // "Rd16=#U6 ; jump #r9:2"
3291 // "Rd16=Rs16 ; jump #r9:2"
3293 case Hexagon::C2_cmpeq:
3294 case Hexagon::C2_cmpgt:
3295 case Hexagon::C2_cmpgtu:
3296 DstReg = MI.getOperand(0).getReg();
3297 Src1Reg = MI.getOperand(1).getReg();
3298 Src2Reg = MI.getOperand(2).getReg();
3299 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3300 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3301 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3302 return HexagonII::HCG_A;
3303 break;
3304 case Hexagon::C2_cmpeqi:
3305 case Hexagon::C2_cmpgti:
3306 case Hexagon::C2_cmpgtui:
3307 // P0 = cmp.eq(Rs,#u2)
3308 DstReg = MI.getOperand(0).getReg();
3309 SrcReg = MI.getOperand(1).getReg();
3310 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3311 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3312 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3313 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3314 (MI.getOperand(2).getImm() == -1)))
3315 return HexagonII::HCG_A;
3316 break;
3317 case Hexagon::A2_tfr:
3318 // Rd = Rs
3319 DstReg = MI.getOperand(0).getReg();
3320 SrcReg = MI.getOperand(1).getReg();
3321 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3322 return HexagonII::HCG_A;
3323 break;
3324 case Hexagon::A2_tfrsi:
3325 // Rd = #u6
3326 // Do not test for #u6 size since the const is getting extended
3327 // regardless and compound could be formed.
3328 DstReg = MI.getOperand(0).getReg();
3329 if (isIntRegForSubInst(DstReg))
3330 return HexagonII::HCG_A;
3331 break;
3332 case Hexagon::S2_tstbit_i:
3333 DstReg = MI.getOperand(0).getReg();
3334 Src1Reg = MI.getOperand(1).getReg();
3335 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3336 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3337 MI.getOperand(2).isImm() &&
3338 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
3339 return HexagonII::HCG_A;
3340 break;
3341 // The fact that .new form is used pretty much guarantees
3342 // that predicate register will match. Nevertheless,
3343 // there could be some false positives without additional
3344 // checking.
3345 case Hexagon::J2_jumptnew:
3346 case Hexagon::J2_jumpfnew:
3347 case Hexagon::J2_jumptnewpt:
3348 case Hexagon::J2_jumpfnewpt:
3349 Src1Reg = MI.getOperand(0).getReg();
3350 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3351 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3352 return HexagonII::HCG_B;
3353 break;
3354 // Transfer and jump:
3355 // Rd=#U6 ; jump #r9:2
3356 // Rd=Rs ; jump #r9:2
3357 // Do not test for jump range here.
3358 case Hexagon::J2_jump:
3359 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3360 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
3361 return HexagonII::HCG_C;
3364 return HexagonII::HCG_None;
3367 // Returns -1 when there is no opcode found.
3368 unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3369 const MachineInstr &GB) const {
3370 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3371 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
3372 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3373 (GB.getOpcode() != Hexagon::J2_jumptnew))
3374 return -1u;
3375 Register DestReg = GA.getOperand(0).getReg();
3376 if (!GB.readsRegister(DestReg))
3377 return -1u;
3378 if (DestReg != Hexagon::P0 && DestReg != Hexagon::P1)
3379 return -1u;
3380 // The value compared against must be either u5 or -1.
3381 const MachineOperand &CmpOp = GA.getOperand(2);
3382 if (!CmpOp.isImm())
3383 return -1u;
3384 int V = CmpOp.getImm();
3385 if (V == -1)
3386 return DestReg == Hexagon::P0 ? Hexagon::J4_cmpeqn1_tp0_jump_nt
3387 : Hexagon::J4_cmpeqn1_tp1_jump_nt;
3388 if (!isUInt<5>(V))
3389 return -1u;
3390 return DestReg == Hexagon::P0 ? Hexagon::J4_cmpeqi_tp0_jump_nt
3391 : Hexagon::J4_cmpeqi_tp1_jump_nt;
3394 int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3395 enum Hexagon::PredSense inPredSense;
3396 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3397 Hexagon::PredSense_true;
3398 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3399 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3400 return CondOpcode;
3402 llvm_unreachable("Unexpected predicable instruction");
3405 // Return the cur value instruction for a given store.
3406 int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3407 switch (MI.getOpcode()) {
3408 default: llvm_unreachable("Unknown .cur type");
3409 case Hexagon::V6_vL32b_pi:
3410 return Hexagon::V6_vL32b_cur_pi;
3411 case Hexagon::V6_vL32b_ai:
3412 return Hexagon::V6_vL32b_cur_ai;
3413 case Hexagon::V6_vL32b_nt_pi:
3414 return Hexagon::V6_vL32b_nt_cur_pi;
3415 case Hexagon::V6_vL32b_nt_ai:
3416 return Hexagon::V6_vL32b_nt_cur_ai;
3418 return 0;
3421 // Return the regular version of the .cur instruction.
3422 int HexagonInstrInfo::getNonDotCurOp(const MachineInstr &MI) const {
3423 switch (MI.getOpcode()) {
3424 default: llvm_unreachable("Unknown .cur type");
3425 case Hexagon::V6_vL32b_cur_pi:
3426 return Hexagon::V6_vL32b_pi;
3427 case Hexagon::V6_vL32b_cur_ai:
3428 return Hexagon::V6_vL32b_ai;
3429 case Hexagon::V6_vL32b_nt_cur_pi:
3430 return Hexagon::V6_vL32b_nt_pi;
3431 case Hexagon::V6_vL32b_nt_cur_ai:
3432 return Hexagon::V6_vL32b_nt_ai;
3434 return 0;
3437 // The diagram below shows the steps involved in the conversion of a predicated
3438 // store instruction to its .new predicated new-value form.
3440 // Note: It doesn't include conditional new-value stores as they can't be
3441 // converted to .new predicate.
3443 // p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3444 // ^ ^
3445 // / \ (not OK. it will cause new-value store to be
3446 // / X conditional on p0.new while R2 producer is
3447 // / \ on p0)
3448 // / \.
3449 // p.new store p.old NV store
3450 // [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3451 // ^ ^
3452 // \ /
3453 // \ /
3454 // \ /
3455 // p.old store
3456 // [if (p0)memw(R0+#0)=R2]
3458 // The following set of instructions further explains the scenario where
3459 // conditional new-value store becomes invalid when promoted to .new predicate
3460 // form.
3462 // { 1) if (p0) r0 = add(r1, r2)
3463 // 2) p0 = cmp.eq(r3, #0) }
3465 // 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3466 // the first two instructions because in instr 1, r0 is conditional on old value
3467 // of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3468 // is not valid for new-value stores.
3469 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3470 // from the "Conditional Store" list. Because a predicated new value store
3471 // would NOT be promoted to a double dot new store. See diagram below:
3472 // This function returns yes for those stores that are predicated but not
3473 // yet promoted to predicate dot new instructions.
3475 // +---------------------+
3476 // /-----| if (p0) memw(..)=r0 |---------\~
3477 // || +---------------------+ ||
3478 // promote || /\ /\ || promote
3479 // || /||\ /||\ ||
3480 // \||/ demote || \||/
3481 // \/ || || \/
3482 // +-------------------------+ || +-------------------------+
3483 // | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3484 // +-------------------------+ || +-------------------------+
3485 // || || ||
3486 // || demote \||/
3487 // promote || \/ NOT possible
3488 // || || /\~
3489 // \||/ || /||\~
3490 // \/ || ||
3491 // +-----------------------------+
3492 // | if (p0.new) memw(..)=r0.new |
3493 // +-----------------------------+
3494 // Double Dot New Store
3496 // Returns the most basic instruction for the .new predicated instructions and
3497 // new-value stores.
3498 // For example, all of the following instructions will be converted back to the
3499 // same instruction:
3500 // 1) if (p0.new) memw(R0+#0) = R1.new --->
3501 // 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3502 // 3) if (p0.new) memw(R0+#0) = R1 --->
3504 // To understand the translation of instruction 1 to its original form, consider
3505 // a packet with 3 instructions.
3506 // { p0 = cmp.eq(R0,R1)
3507 // if (p0.new) R2 = add(R3, R4)
3508 // R5 = add (R3, R1)
3509 // }
3510 // if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3512 // This instruction can be part of the previous packet only if both p0 and R2
3513 // are promoted to .new values. This promotion happens in steps, first
3514 // predicate register is promoted to .new and in the next iteration R2 is
3515 // promoted. Therefore, in case of dependence check failure (due to R5) during
3516 // next iteration, it should be converted back to its most basic form.
3518 // Return the new value instruction for a given store.
3519 int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3520 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
3521 if (NVOpcode >= 0) // Valid new-value store instruction.
3522 return NVOpcode;
3524 switch (MI.getOpcode()) {
3525 default:
3526 report_fatal_error(std::string("Unknown .new type: ") +
3527 std::to_string(MI.getOpcode()));
3528 case Hexagon::S4_storerb_ur:
3529 return Hexagon::S4_storerbnew_ur;
3531 case Hexagon::S2_storerb_pci:
3532 return Hexagon::S2_storerb_pci;
3534 case Hexagon::S2_storeri_pci:
3535 return Hexagon::S2_storeri_pci;
3537 case Hexagon::S2_storerh_pci:
3538 return Hexagon::S2_storerh_pci;
3540 case Hexagon::S2_storerd_pci:
3541 return Hexagon::S2_storerd_pci;
3543 case Hexagon::S2_storerf_pci:
3544 return Hexagon::S2_storerf_pci;
3546 case Hexagon::V6_vS32b_ai:
3547 return Hexagon::V6_vS32b_new_ai;
3549 case Hexagon::V6_vS32b_pi:
3550 return Hexagon::V6_vS32b_new_pi;
3552 return 0;
3555 // Returns the opcode to use when converting MI, which is a conditional jump,
3556 // into a conditional instruction which uses the .new value of the predicate.
3557 // We also use branch probabilities to add a hint to the jump.
3558 // If MBPI is null, all edges will be treated as equally likely for the
3559 // purposes of establishing a predication hint.
3560 int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
3561 const MachineBranchProbabilityInfo *MBPI) const {
3562 // We assume that block can have at most two successors.
3563 const MachineBasicBlock *Src = MI.getParent();
3564 const MachineOperand &BrTarget = MI.getOperand(1);
3565 bool Taken = false;
3566 const BranchProbability OneHalf(1, 2);
3568 auto getEdgeProbability = [MBPI] (const MachineBasicBlock *Src,
3569 const MachineBasicBlock *Dst) {
3570 if (MBPI)
3571 return MBPI->getEdgeProbability(Src, Dst);
3572 return BranchProbability(1, Src->succ_size());
3575 if (BrTarget.isMBB()) {
3576 const MachineBasicBlock *Dst = BrTarget.getMBB();
3577 Taken = getEdgeProbability(Src, Dst) >= OneHalf;
3578 } else {
3579 // The branch target is not a basic block (most likely a function).
3580 // Since BPI only gives probabilities for targets that are basic blocks,
3581 // try to identify another target of this branch (potentially a fall-
3582 // -through) and check the probability of that target.
3584 // The only handled branch combinations are:
3585 // - one conditional branch,
3586 // - one conditional branch followed by one unconditional branch.
3587 // Otherwise, assume not-taken.
3588 assert(MI.isConditionalBranch());
3589 const MachineBasicBlock &B = *MI.getParent();
3590 bool SawCond = false, Bad = false;
3591 for (const MachineInstr &I : B) {
3592 if (!I.isBranch())
3593 continue;
3594 if (I.isConditionalBranch()) {
3595 SawCond = true;
3596 if (&I != &MI) {
3597 Bad = true;
3598 break;
3601 if (I.isUnconditionalBranch() && !SawCond) {
3602 Bad = true;
3603 break;
3606 if (!Bad) {
3607 MachineBasicBlock::const_instr_iterator It(MI);
3608 MachineBasicBlock::const_instr_iterator NextIt = std::next(It);
3609 if (NextIt == B.instr_end()) {
3610 // If this branch is the last, look for the fall-through block.
3611 for (const MachineBasicBlock *SB : B.successors()) {
3612 if (!B.isLayoutSuccessor(SB))
3613 continue;
3614 Taken = getEdgeProbability(Src, SB) < OneHalf;
3615 break;
3617 } else {
3618 assert(NextIt->isUnconditionalBranch());
3619 // Find the first MBB operand and assume it's the target.
3620 const MachineBasicBlock *BT = nullptr;
3621 for (const MachineOperand &Op : NextIt->operands()) {
3622 if (!Op.isMBB())
3623 continue;
3624 BT = Op.getMBB();
3625 break;
3627 Taken = BT && getEdgeProbability(Src, BT) < OneHalf;
3629 } // if (!Bad)
3632 // The Taken flag should be set to something reasonable by this point.
3634 switch (MI.getOpcode()) {
3635 case Hexagon::J2_jumpt:
3636 return Taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3637 case Hexagon::J2_jumpf:
3638 return Taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3640 default:
3641 llvm_unreachable("Unexpected jump instruction.");
3645 // Return .new predicate version for an instruction.
3646 int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
3647 const MachineBranchProbabilityInfo *MBPI) const {
3648 switch (MI.getOpcode()) {
3649 // Condtional Jumps
3650 case Hexagon::J2_jumpt:
3651 case Hexagon::J2_jumpf:
3652 return getDotNewPredJumpOp(MI, MBPI);
3655 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
3656 if (NewOpcode >= 0)
3657 return NewOpcode;
3658 return 0;
3661 int HexagonInstrInfo::getDotOldOp(const MachineInstr &MI) const {
3662 int NewOp = MI.getOpcode();
3663 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3664 NewOp = Hexagon::getPredOldOpcode(NewOp);
3665 // All Hexagon architectures have prediction bits on dot-new branches,
3666 // but only Hexagon V60+ has prediction bits on dot-old ones. Make sure
3667 // to pick the right opcode when converting back to dot-old.
3668 if (!Subtarget.getFeatureBits()[Hexagon::ArchV60]) {
3669 switch (NewOp) {
3670 case Hexagon::J2_jumptpt:
3671 NewOp = Hexagon::J2_jumpt;
3672 break;
3673 case Hexagon::J2_jumpfpt:
3674 NewOp = Hexagon::J2_jumpf;
3675 break;
3676 case Hexagon::J2_jumprtpt:
3677 NewOp = Hexagon::J2_jumprt;
3678 break;
3679 case Hexagon::J2_jumprfpt:
3680 NewOp = Hexagon::J2_jumprf;
3681 break;
3684 assert(NewOp >= 0 &&
3685 "Couldn't change predicate new instruction to its old form.");
3688 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3689 NewOp = Hexagon::getNonNVStore(NewOp);
3690 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3693 if (Subtarget.hasV60Ops())
3694 return NewOp;
3696 // Subtargets prior to V60 didn't support 'taken' forms of predicated jumps.
3697 switch (NewOp) {
3698 case Hexagon::J2_jumpfpt:
3699 return Hexagon::J2_jumpf;
3700 case Hexagon::J2_jumptpt:
3701 return Hexagon::J2_jumpt;
3702 case Hexagon::J2_jumprfpt:
3703 return Hexagon::J2_jumprf;
3704 case Hexagon::J2_jumprtpt:
3705 return Hexagon::J2_jumprt;
3707 return NewOp;
3710 // See if instruction could potentially be a duplex candidate.
3711 // If so, return its group. Zero otherwise.
3712 HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
3713 const MachineInstr &MI) const {
3714 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3715 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
3717 switch (MI.getOpcode()) {
3718 default:
3719 return HexagonII::HSIG_None;
3721 // Group L1:
3723 // Rd = memw(Rs+#u4:2)
3724 // Rd = memub(Rs+#u4:0)
3725 case Hexagon::L2_loadri_io:
3726 DstReg = MI.getOperand(0).getReg();
3727 SrcReg = MI.getOperand(1).getReg();
3728 // Special case this one from Group L2.
3729 // Rd = memw(r29+#u5:2)
3730 if (isIntRegForSubInst(DstReg)) {
3731 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3732 HRI.getStackRegister() == SrcReg &&
3733 MI.getOperand(2).isImm() &&
3734 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
3735 return HexagonII::HSIG_L2;
3736 // Rd = memw(Rs+#u4:2)
3737 if (isIntRegForSubInst(SrcReg) &&
3738 (MI.getOperand(2).isImm() &&
3739 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
3740 return HexagonII::HSIG_L1;
3742 break;
3743 case Hexagon::L2_loadrub_io:
3744 // Rd = memub(Rs+#u4:0)
3745 DstReg = MI.getOperand(0).getReg();
3746 SrcReg = MI.getOperand(1).getReg();
3747 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3748 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
3749 return HexagonII::HSIG_L1;
3750 break;
3752 // Group L2:
3754 // Rd = memh/memuh(Rs+#u3:1)
3755 // Rd = memb(Rs+#u3:0)
3756 // Rd = memw(r29+#u5:2) - Handled above.
3757 // Rdd = memd(r29+#u5:3)
3758 // deallocframe
3759 // [if ([!]p0[.new])] dealloc_return
3760 // [if ([!]p0[.new])] jumpr r31
3761 case Hexagon::L2_loadrh_io:
3762 case Hexagon::L2_loadruh_io:
3763 // Rd = memh/memuh(Rs+#u3:1)
3764 DstReg = MI.getOperand(0).getReg();
3765 SrcReg = MI.getOperand(1).getReg();
3766 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3767 MI.getOperand(2).isImm() &&
3768 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
3769 return HexagonII::HSIG_L2;
3770 break;
3771 case Hexagon::L2_loadrb_io:
3772 // Rd = memb(Rs+#u3:0)
3773 DstReg = MI.getOperand(0).getReg();
3774 SrcReg = MI.getOperand(1).getReg();
3775 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3776 MI.getOperand(2).isImm() &&
3777 isUInt<3>(MI.getOperand(2).getImm()))
3778 return HexagonII::HSIG_L2;
3779 break;
3780 case Hexagon::L2_loadrd_io:
3781 // Rdd = memd(r29+#u5:3)
3782 DstReg = MI.getOperand(0).getReg();
3783 SrcReg = MI.getOperand(1).getReg();
3784 if (isDblRegForSubInst(DstReg, HRI) &&
3785 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3786 HRI.getStackRegister() == SrcReg &&
3787 MI.getOperand(2).isImm() &&
3788 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
3789 return HexagonII::HSIG_L2;
3790 break;
3791 // dealloc_return is not documented in Hexagon Manual, but marked
3792 // with A_SUBINSN attribute in iset_v4classic.py.
3793 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3794 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
3795 case Hexagon::L4_return:
3796 case Hexagon::L2_deallocframe:
3797 return HexagonII::HSIG_L2;
3798 case Hexagon::EH_RETURN_JMPR:
3799 case Hexagon::PS_jmpret:
3800 case Hexagon::SL2_jumpr31:
3801 // jumpr r31
3802 // Actual form JMPR implicit-def %pc, implicit %r31, implicit internal %r0
3803 DstReg = MI.getOperand(0).getReg();
3804 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3805 return HexagonII::HSIG_L2;
3806 break;
3807 case Hexagon::PS_jmprett:
3808 case Hexagon::PS_jmpretf:
3809 case Hexagon::PS_jmprettnewpt:
3810 case Hexagon::PS_jmpretfnewpt:
3811 case Hexagon::PS_jmprettnew:
3812 case Hexagon::PS_jmpretfnew:
3813 case Hexagon::SL2_jumpr31_t:
3814 case Hexagon::SL2_jumpr31_f:
3815 case Hexagon::SL2_jumpr31_tnew:
3816 DstReg = MI.getOperand(1).getReg();
3817 SrcReg = MI.getOperand(0).getReg();
3818 // [if ([!]p0[.new])] jumpr r31
3819 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3820 (Hexagon::P0 == SrcReg)) &&
3821 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3822 return HexagonII::HSIG_L2;
3823 break;
3824 case Hexagon::L4_return_t:
3825 case Hexagon::L4_return_f:
3826 case Hexagon::L4_return_tnew_pnt:
3827 case Hexagon::L4_return_fnew_pnt:
3828 case Hexagon::L4_return_tnew_pt:
3829 case Hexagon::L4_return_fnew_pt:
3830 // [if ([!]p0[.new])] dealloc_return
3831 SrcReg = MI.getOperand(0).getReg();
3832 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3833 return HexagonII::HSIG_L2;
3834 break;
3836 // Group S1:
3838 // memw(Rs+#u4:2) = Rt
3839 // memb(Rs+#u4:0) = Rt
3840 case Hexagon::S2_storeri_io:
3841 // Special case this one from Group S2.
3842 // memw(r29+#u5:2) = Rt
3843 Src1Reg = MI.getOperand(0).getReg();
3844 Src2Reg = MI.getOperand(2).getReg();
3845 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3846 isIntRegForSubInst(Src2Reg) &&
3847 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3848 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
3849 return HexagonII::HSIG_S2;
3850 // memw(Rs+#u4:2) = Rt
3851 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3852 MI.getOperand(1).isImm() &&
3853 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
3854 return HexagonII::HSIG_S1;
3855 break;
3856 case Hexagon::S2_storerb_io:
3857 // memb(Rs+#u4:0) = Rt
3858 Src1Reg = MI.getOperand(0).getReg();
3859 Src2Reg = MI.getOperand(2).getReg();
3860 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3861 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
3862 return HexagonII::HSIG_S1;
3863 break;
3865 // Group S2:
3867 // memh(Rs+#u3:1) = Rt
3868 // memw(r29+#u5:2) = Rt
3869 // memd(r29+#s6:3) = Rtt
3870 // memw(Rs+#u4:2) = #U1
3871 // memb(Rs+#u4) = #U1
3872 // allocframe(#u5:3)
3873 case Hexagon::S2_storerh_io:
3874 // memh(Rs+#u3:1) = Rt
3875 Src1Reg = MI.getOperand(0).getReg();
3876 Src2Reg = MI.getOperand(2).getReg();
3877 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3878 MI.getOperand(1).isImm() &&
3879 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
3880 return HexagonII::HSIG_S1;
3881 break;
3882 case Hexagon::S2_storerd_io:
3883 // memd(r29+#s6:3) = Rtt
3884 Src1Reg = MI.getOperand(0).getReg();
3885 Src2Reg = MI.getOperand(2).getReg();
3886 if (isDblRegForSubInst(Src2Reg, HRI) &&
3887 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3888 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3889 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
3890 return HexagonII::HSIG_S2;
3891 break;
3892 case Hexagon::S4_storeiri_io:
3893 // memw(Rs+#u4:2) = #U1
3894 Src1Reg = MI.getOperand(0).getReg();
3895 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
3896 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
3897 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
3898 return HexagonII::HSIG_S2;
3899 break;
3900 case Hexagon::S4_storeirb_io:
3901 // memb(Rs+#u4) = #U1
3902 Src1Reg = MI.getOperand(0).getReg();
3903 if (isIntRegForSubInst(Src1Reg) &&
3904 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
3905 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
3906 return HexagonII::HSIG_S2;
3907 break;
3908 case Hexagon::S2_allocframe:
3909 if (MI.getOperand(2).isImm() &&
3910 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
3911 return HexagonII::HSIG_S1;
3912 break;
3914 // Group A:
3916 // Rx = add(Rx,#s7)
3917 // Rd = Rs
3918 // Rd = #u6
3919 // Rd = #-1
3920 // if ([!]P0[.new]) Rd = #0
3921 // Rd = add(r29,#u6:2)
3922 // Rx = add(Rx,Rs)
3923 // P0 = cmp.eq(Rs,#u2)
3924 // Rdd = combine(#0,Rs)
3925 // Rdd = combine(Rs,#0)
3926 // Rdd = combine(#u2,#U2)
3927 // Rd = add(Rs,#1)
3928 // Rd = add(Rs,#-1)
3929 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3930 // Rd = and(Rs,#1)
3931 case Hexagon::A2_addi:
3932 DstReg = MI.getOperand(0).getReg();
3933 SrcReg = MI.getOperand(1).getReg();
3934 if (isIntRegForSubInst(DstReg)) {
3935 // Rd = add(r29,#u6:2)
3936 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3937 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
3938 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
3939 return HexagonII::HSIG_A;
3940 // Rx = add(Rx,#s7)
3941 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
3942 isInt<7>(MI.getOperand(2).getImm()))
3943 return HexagonII::HSIG_A;
3944 // Rd = add(Rs,#1)
3945 // Rd = add(Rs,#-1)
3946 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3947 ((MI.getOperand(2).getImm() == 1) ||
3948 (MI.getOperand(2).getImm() == -1)))
3949 return HexagonII::HSIG_A;
3951 break;
3952 case Hexagon::A2_add:
3953 // Rx = add(Rx,Rs)
3954 DstReg = MI.getOperand(0).getReg();
3955 Src1Reg = MI.getOperand(1).getReg();
3956 Src2Reg = MI.getOperand(2).getReg();
3957 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3958 isIntRegForSubInst(Src2Reg))
3959 return HexagonII::HSIG_A;
3960 break;
3961 case Hexagon::A2_andir:
3962 // Same as zxtb.
3963 // Rd16=and(Rs16,#255)
3964 // Rd16=and(Rs16,#1)
3965 DstReg = MI.getOperand(0).getReg();
3966 SrcReg = MI.getOperand(1).getReg();
3967 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3968 MI.getOperand(2).isImm() &&
3969 ((MI.getOperand(2).getImm() == 1) ||
3970 (MI.getOperand(2).getImm() == 255)))
3971 return HexagonII::HSIG_A;
3972 break;
3973 case Hexagon::A2_tfr:
3974 // Rd = Rs
3975 DstReg = MI.getOperand(0).getReg();
3976 SrcReg = MI.getOperand(1).getReg();
3977 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3978 return HexagonII::HSIG_A;
3979 break;
3980 case Hexagon::A2_tfrsi:
3981 // Rd = #u6
3982 // Do not test for #u6 size since the const is getting extended
3983 // regardless and compound could be formed.
3984 // Rd = #-1
3985 DstReg = MI.getOperand(0).getReg();
3986 if (isIntRegForSubInst(DstReg))
3987 return HexagonII::HSIG_A;
3988 break;
3989 case Hexagon::C2_cmoveit:
3990 case Hexagon::C2_cmovenewit:
3991 case Hexagon::C2_cmoveif:
3992 case Hexagon::C2_cmovenewif:
3993 // if ([!]P0[.new]) Rd = #0
3994 // Actual form:
3995 // %r16 = C2_cmovenewit internal %p0, 0, implicit undef %r16;
3996 DstReg = MI.getOperand(0).getReg();
3997 SrcReg = MI.getOperand(1).getReg();
3998 if (isIntRegForSubInst(DstReg) &&
3999 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
4000 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
4001 return HexagonII::HSIG_A;
4002 break;
4003 case Hexagon::C2_cmpeqi:
4004 // P0 = cmp.eq(Rs,#u2)
4005 DstReg = MI.getOperand(0).getReg();
4006 SrcReg = MI.getOperand(1).getReg();
4007 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
4008 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
4009 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
4010 return HexagonII::HSIG_A;
4011 break;
4012 case Hexagon::A2_combineii:
4013 case Hexagon::A4_combineii:
4014 // Rdd = combine(#u2,#U2)
4015 DstReg = MI.getOperand(0).getReg();
4016 if (isDblRegForSubInst(DstReg, HRI) &&
4017 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
4018 (MI.getOperand(1).isGlobal() &&
4019 isUInt<2>(MI.getOperand(1).getOffset()))) &&
4020 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
4021 (MI.getOperand(2).isGlobal() &&
4022 isUInt<2>(MI.getOperand(2).getOffset()))))
4023 return HexagonII::HSIG_A;
4024 break;
4025 case Hexagon::A4_combineri:
4026 // Rdd = combine(Rs,#0)
4027 DstReg = MI.getOperand(0).getReg();
4028 SrcReg = MI.getOperand(1).getReg();
4029 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
4030 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
4031 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
4032 return HexagonII::HSIG_A;
4033 break;
4034 case Hexagon::A4_combineir:
4035 // Rdd = combine(#0,Rs)
4036 DstReg = MI.getOperand(0).getReg();
4037 SrcReg = MI.getOperand(2).getReg();
4038 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
4039 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
4040 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
4041 return HexagonII::HSIG_A;
4042 break;
4043 case Hexagon::A2_sxtb:
4044 case Hexagon::A2_sxth:
4045 case Hexagon::A2_zxtb:
4046 case Hexagon::A2_zxth:
4047 // Rd = sxth/sxtb/zxtb/zxth(Rs)
4048 DstReg = MI.getOperand(0).getReg();
4049 SrcReg = MI.getOperand(1).getReg();
4050 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
4051 return HexagonII::HSIG_A;
4052 break;
4055 return HexagonII::HSIG_None;
4058 short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
4059 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
4062 unsigned HexagonInstrInfo::getInstrTimingClassLatency(
4063 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
4064 // Default to one cycle for no itinerary. However, an "empty" itinerary may
4065 // still have a MinLatency property, which getStageLatency checks.
4066 if (!ItinData)
4067 return getInstrLatency(ItinData, MI);
4069 if (MI.isTransient())
4070 return 0;
4071 return ItinData->getStageLatency(MI.getDesc().getSchedClass());
4074 /// getOperandLatency - Compute and return the use operand latency of a given
4075 /// pair of def and use.
4076 /// In most cases, the static scheduling itinerary was enough to determine the
4077 /// operand latency. But it may not be possible for instructions with variable
4078 /// number of defs / uses.
4080 /// This is a raw interface to the itinerary that may be directly overriden by
4081 /// a target. Use computeOperandLatency to get the best estimate of latency.
4082 int HexagonInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
4083 const MachineInstr &DefMI,
4084 unsigned DefIdx,
4085 const MachineInstr &UseMI,
4086 unsigned UseIdx) const {
4087 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
4089 // Get DefIdx and UseIdx for super registers.
4090 const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
4092 if (DefMO.isReg() && Register::isPhysicalRegister(DefMO.getReg())) {
4093 if (DefMO.isImplicit()) {
4094 for (MCSuperRegIterator SR(DefMO.getReg(), &HRI); SR.isValid(); ++SR) {
4095 int Idx = DefMI.findRegisterDefOperandIdx(*SR, false, false, &HRI);
4096 if (Idx != -1) {
4097 DefIdx = Idx;
4098 break;
4103 const MachineOperand &UseMO = UseMI.getOperand(UseIdx);
4104 if (UseMO.isImplicit()) {
4105 for (MCSuperRegIterator SR(UseMO.getReg(), &HRI); SR.isValid(); ++SR) {
4106 int Idx = UseMI.findRegisterUseOperandIdx(*SR, false, &HRI);
4107 if (Idx != -1) {
4108 UseIdx = Idx;
4109 break;
4115 int Latency = TargetInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
4116 UseMI, UseIdx);
4117 if (!Latency)
4118 // We should never have 0 cycle latency between two instructions unless
4119 // they can be packetized together. However, this decision can't be made
4120 // here.
4121 Latency = 1;
4122 return Latency;
4125 // inverts the predication logic.
4126 // p -> NotP
4127 // NotP -> P
4128 bool HexagonInstrInfo::getInvertedPredSense(
4129 SmallVectorImpl<MachineOperand> &Cond) const {
4130 if (Cond.empty())
4131 return false;
4132 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
4133 Cond[0].setImm(Opc);
4134 return true;
4137 unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
4138 int InvPredOpcode;
4139 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
4140 : Hexagon::getTruePredOpcode(Opc);
4141 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
4142 return InvPredOpcode;
4144 llvm_unreachable("Unexpected predicated instruction");
4147 // Returns the max value that doesn't need to be extended.
4148 int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
4149 const uint64_t F = MI.getDesc().TSFlags;
4150 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4151 & HexagonII::ExtentSignedMask;
4152 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4153 & HexagonII::ExtentBitsMask;
4155 if (isSigned) // if value is signed
4156 return ~(-1U << (bits - 1));
4157 else
4158 return ~(-1U << bits);
4162 bool HexagonInstrInfo::isAddrModeWithOffset(const MachineInstr &MI) const {
4163 switch (MI.getOpcode()) {
4164 case Hexagon::L2_loadrbgp:
4165 case Hexagon::L2_loadrdgp:
4166 case Hexagon::L2_loadrhgp:
4167 case Hexagon::L2_loadrigp:
4168 case Hexagon::L2_loadrubgp:
4169 case Hexagon::L2_loadruhgp:
4170 case Hexagon::S2_storerbgp:
4171 case Hexagon::S2_storerbnewgp:
4172 case Hexagon::S2_storerhgp:
4173 case Hexagon::S2_storerhnewgp:
4174 case Hexagon::S2_storerigp:
4175 case Hexagon::S2_storerinewgp:
4176 case Hexagon::S2_storerdgp:
4177 case Hexagon::S2_storerfgp:
4178 return true;
4180 const uint64_t F = MI.getDesc().TSFlags;
4181 unsigned addrMode =
4182 ((F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask);
4183 // Disallow any base+offset instruction. The assembler does not yet reorder
4184 // based up any zero offset instruction.
4185 return (addrMode == HexagonII::BaseRegOffset ||
4186 addrMode == HexagonII::BaseImmOffset ||
4187 addrMode == HexagonII::BaseLongOffset);
4190 unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
4191 using namespace HexagonII;
4193 const uint64_t F = MI.getDesc().TSFlags;
4194 unsigned S = (F >> MemAccessSizePos) & MemAccesSizeMask;
4195 unsigned Size = getMemAccessSizeInBytes(MemAccessSize(S));
4196 if (Size != 0)
4197 return Size;
4199 // Handle vector access sizes.
4200 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
4201 switch (S) {
4202 case HexagonII::HVXVectorAccess:
4203 return HRI.getSpillSize(Hexagon::HvxVRRegClass);
4204 default:
4205 llvm_unreachable("Unexpected instruction");
4209 // Returns the min value that doesn't need to be extended.
4210 int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
4211 const uint64_t F = MI.getDesc().TSFlags;
4212 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4213 & HexagonII::ExtentSignedMask;
4214 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4215 & HexagonII::ExtentBitsMask;
4217 if (isSigned) // if value is signed
4218 return -1U << (bits - 1);
4219 else
4220 return 0;
4223 // Returns opcode of the non-extended equivalent instruction.
4224 short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
4225 // Check if the instruction has a register form that uses register in place
4226 // of the extended operand, if so return that as the non-extended form.
4227 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
4228 if (NonExtOpcode >= 0)
4229 return NonExtOpcode;
4231 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
4232 // Check addressing mode and retrieve non-ext equivalent instruction.
4233 switch (getAddrMode(MI)) {
4234 case HexagonII::Absolute:
4235 return Hexagon::changeAddrMode_abs_io(MI.getOpcode());
4236 case HexagonII::BaseImmOffset:
4237 return Hexagon::changeAddrMode_io_rr(MI.getOpcode());
4238 case HexagonII::BaseLongOffset:
4239 return Hexagon::changeAddrMode_ur_rr(MI.getOpcode());
4241 default:
4242 return -1;
4245 return -1;
4248 bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
4249 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
4250 if (Cond.empty())
4251 return false;
4252 assert(Cond.size() == 2);
4253 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
4254 LLVM_DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
4255 return false;
4257 PredReg = Cond[1].getReg();
4258 PredRegPos = 1;
4259 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4260 PredRegFlags = 0;
4261 if (Cond[1].isImplicit())
4262 PredRegFlags = RegState::Implicit;
4263 if (Cond[1].isUndef())
4264 PredRegFlags |= RegState::Undef;
4265 return true;
4268 short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
4269 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
4272 short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
4273 return Hexagon::getRegForm(MI.getOpcode());
4276 // Return the number of bytes required to encode the instruction.
4277 // Hexagon instructions are fixed length, 4 bytes, unless they
4278 // use a constant extender, which requires another 4 bytes.
4279 // For debug instructions and prolog labels, return 0.
4280 unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
4281 if (MI.isDebugInstr() || MI.isPosition())
4282 return 0;
4284 unsigned Size = MI.getDesc().getSize();
4285 if (!Size)
4286 // Assume the default insn size in case it cannot be determined
4287 // for whatever reason.
4288 Size = HEXAGON_INSTR_SIZE;
4290 if (isConstExtended(MI) || isExtended(MI))
4291 Size += HEXAGON_INSTR_SIZE;
4293 // Try and compute number of instructions in asm.
4294 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4295 const MachineBasicBlock &MBB = *MI.getParent();
4296 const MachineFunction *MF = MBB.getParent();
4297 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4299 // Count the number of register definitions to find the asm string.
4300 unsigned NumDefs = 0;
4301 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
4302 ++NumDefs)
4303 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
4305 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
4306 // Disassemble the AsmStr and approximate number of instructions.
4307 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
4308 Size = getInlineAsmLength(AsmStr, *MAI);
4311 return Size;
4314 uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
4315 const uint64_t F = MI.getDesc().TSFlags;
4316 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4319 unsigned HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
4320 const InstrItineraryData &II = *Subtarget.getInstrItineraryData();
4321 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
4323 return IS.getUnits();
4326 // Calculate size of the basic block without debug instructions.
4327 unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4328 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4331 unsigned HexagonInstrInfo::nonDbgBundleSize(
4332 MachineBasicBlock::const_iterator BundleHead) const {
4333 assert(BundleHead->isBundle() && "Not a bundle header");
4334 auto MII = BundleHead.getInstrIterator();
4335 // Skip the bundle header.
4336 return nonDbgMICount(++MII, getBundleEnd(BundleHead.getInstrIterator()));
4339 /// immediateExtend - Changes the instruction in place to one using an immediate
4340 /// extender.
4341 void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
4342 assert((isExtendable(MI)||isConstExtended(MI)) &&
4343 "Instruction must be extendable");
4344 // Find which operand is extendable.
4345 short ExtOpNum = getCExtOpNum(MI);
4346 MachineOperand &MO = MI.getOperand(ExtOpNum);
4347 // This needs to be something we understand.
4348 assert((MO.isMBB() || MO.isImm()) &&
4349 "Branch with unknown extendable field type");
4350 // Mark given operand as extended.
4351 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4354 bool HexagonInstrInfo::invertAndChangeJumpTarget(
4355 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
4356 LLVM_DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to "
4357 << printMBBReference(*NewTarget);
4358 MI.dump(););
4359 assert(MI.isBranch());
4360 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4361 int TargetPos = MI.getNumOperands() - 1;
4362 // In general branch target is the last operand,
4363 // but some implicit defs added at the end might change it.
4364 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
4365 --TargetPos;
4366 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4367 MI.getOperand(TargetPos).setMBB(NewTarget);
4368 if (EnableBranchPrediction && isPredicatedNew(MI)) {
4369 NewOpcode = reversePrediction(NewOpcode);
4371 MI.setDesc(get(NewOpcode));
4372 return true;
4375 void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4376 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4377 MachineFunction::iterator A = MF.begin();
4378 MachineBasicBlock &B = *A;
4379 MachineBasicBlock::iterator I = B.begin();
4380 DebugLoc DL = I->getDebugLoc();
4381 MachineInstr *NewMI;
4383 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4384 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
4385 NewMI = BuildMI(B, I, DL, get(insn));
4386 LLVM_DEBUG(dbgs() << "\n"
4387 << getName(NewMI->getOpcode())
4388 << " Class: " << NewMI->getDesc().getSchedClass());
4389 NewMI->eraseFromParent();
4391 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4394 // inverts the predication logic.
4395 // p -> NotP
4396 // NotP -> P
4397 bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
4398 LLVM_DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
4399 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
4400 return true;
4403 // Reverse the branch prediction.
4404 unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4405 int PredRevOpcode = -1;
4406 if (isPredictedTaken(Opcode))
4407 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4408 else
4409 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4410 assert(PredRevOpcode > 0);
4411 return PredRevOpcode;
4414 // TODO: Add more rigorous validation.
4415 bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4416 const {
4417 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4420 void HexagonInstrInfo::
4421 setBundleNoShuf(MachineBasicBlock::instr_iterator MIB) const {
4422 assert(MIB->isBundle());
4423 MachineOperand &Operand = MIB->getOperand(0);
4424 if (Operand.isImm())
4425 Operand.setImm(Operand.getImm() | memShufDisabledMask);
4426 else
4427 MIB->addOperand(MachineOperand::CreateImm(memShufDisabledMask));
4430 bool HexagonInstrInfo::getBundleNoShuf(const MachineInstr &MIB) const {
4431 assert(MIB.isBundle());
4432 const MachineOperand &Operand = MIB.getOperand(0);
4433 return (Operand.isImm() && (Operand.getImm() & memShufDisabledMask) != 0);
4436 // Addressing mode relations.
4437 short HexagonInstrInfo::changeAddrMode_abs_io(short Opc) const {
4438 return Opc >= 0 ? Hexagon::changeAddrMode_abs_io(Opc) : Opc;
4441 short HexagonInstrInfo::changeAddrMode_io_abs(short Opc) const {
4442 return Opc >= 0 ? Hexagon::changeAddrMode_io_abs(Opc) : Opc;
4445 short HexagonInstrInfo::changeAddrMode_io_pi(short Opc) const {
4446 return Opc >= 0 ? Hexagon::changeAddrMode_io_pi(Opc) : Opc;
4449 short HexagonInstrInfo::changeAddrMode_io_rr(short Opc) const {
4450 return Opc >= 0 ? Hexagon::changeAddrMode_io_rr(Opc) : Opc;
4453 short HexagonInstrInfo::changeAddrMode_pi_io(short Opc) const {
4454 return Opc >= 0 ? Hexagon::changeAddrMode_pi_io(Opc) : Opc;
4457 short HexagonInstrInfo::changeAddrMode_rr_io(short Opc) const {
4458 return Opc >= 0 ? Hexagon::changeAddrMode_rr_io(Opc) : Opc;
4461 short HexagonInstrInfo::changeAddrMode_rr_ur(short Opc) const {
4462 return Opc >= 0 ? Hexagon::changeAddrMode_rr_ur(Opc) : Opc;
4465 short HexagonInstrInfo::changeAddrMode_ur_rr(short Opc) const {
4466 return Opc >= 0 ? Hexagon::changeAddrMode_ur_rr(Opc) : Opc;