LLVM 24.0.0git
HexagonInstrInfo.cpp
Go to the documentation of this file.
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.
10//
11//===----------------------------------------------------------------------===//
12
13#include "HexagonInstrInfo.h"
17#include "HexagonRegisterInfo.h"
18#include "HexagonSubtarget.h"
19#include "llvm/ADT/ArrayRef.h"
23#include "llvm/ADT/StringRef.h"
44#include "llvm/IR/DebugLoc.h"
46#include "llvm/MC/MCAsmInfo.h"
48#include "llvm/MC/MCInstrDesc.h"
52#include "llvm/Support/Debug.h"
57#include <cassert>
58#include <cstdint>
59#include <cstring>
60#include <iterator>
61#include <optional>
62#include <string>
63#include <utility>
64
65using namespace llvm;
66
67#define DEBUG_TYPE "hexagon-instrinfo"
68
69#define GET_INSTRINFO_CTOR_DTOR
70#define GET_INSTRMAP_INFO
72#include "HexagonGenDFAPacketizer.inc"
73#include "HexagonGenInstrInfo.inc"
74
75cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
76 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
77 "packetization boundary."));
78
79static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
80 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
81
83 "disable-hexagon-nv-schedule", cl::Hidden,
84 cl::desc("Disable schedule adjustment for new value stores."));
85
87 "enable-timing-class-latency", cl::Hidden, cl::init(false),
88 cl::desc("Enable timing class latency"));
89
91 "enable-alu-forwarding", cl::Hidden, cl::init(true),
92 cl::desc("Enable vec alu forwarding"));
93
95 "enable-acc-forwarding", cl::Hidden, cl::init(true),
96 cl::desc("Enable vec acc forwarding"));
97
98static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
99 cl::init(true), cl::Hidden,
100 cl::desc("branch relax asm"));
101
102static cl::opt<bool>
103 UseDFAHazardRec("dfa-hazard-rec", cl::init(true), cl::Hidden,
104 cl::desc("Use the DFA based hazard recognizer."));
105
106/// Constants for Hexagon instructions.
107const int Hexagon_MEMW_OFFSET_MAX = 4095;
108const int Hexagon_MEMW_OFFSET_MIN = -4096;
109const int Hexagon_MEMD_OFFSET_MAX = 8191;
110const int Hexagon_MEMD_OFFSET_MIN = -8192;
111const int Hexagon_MEMH_OFFSET_MAX = 2047;
112const int Hexagon_MEMH_OFFSET_MIN = -2048;
113const int Hexagon_MEMB_OFFSET_MAX = 1023;
114const int Hexagon_MEMB_OFFSET_MIN = -1024;
115const int Hexagon_ADDI_OFFSET_MAX = 32767;
116const int Hexagon_ADDI_OFFSET_MIN = -32768;
117
118// Pin the vtable to this file.
119void HexagonInstrInfo::anchor() {}
120
122 : HexagonGenInstrInfo(ST, RegInfo, Hexagon::ADJCALLSTACKDOWN,
123 Hexagon::ADJCALLSTACKUP),
124 RegInfo(ST.getHwMode()), Subtarget(ST) {}
125
126namespace llvm {
127namespace HexagonFUnits {
128 bool isSlot0Only(unsigned units);
129}
130}
131
133 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
134 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
135}
136
138 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_lo)) &&
139 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_hi));
140}
141
142/// Calculate number of instructions excluding the debug instructions.
145 unsigned Count = 0;
146 for (; MIB != MIE; ++MIB) {
147 if (!MIB->isDebugInstr())
148 ++Count;
149 }
150 return Count;
151}
152
153// Check if the A2_tfrsi instruction is cheap or not. If the operand has
154// to be constant-extendend it is not cheap since it occupies two slots
155// in a packet.
157 // Enable the following steps only at Os/Oz
158 if (!(MI.getMF()->getFunction().hasOptSize()))
159 return MI.isAsCheapAsAMove();
160
161 if (MI.getOpcode() == Hexagon::A2_tfrsi) {
162 auto Op = MI.getOperand(1);
163 // If the instruction has a global address as operand, it is not cheap
164 // since the operand will be constant extended.
165 if (Op.isGlobal())
166 return false;
167 // If the instruction has an operand of size > 16bits, its will be
168 // const-extended and hence, it is not cheap.
169 if (Op.isImm()) {
170 int64_t Imm = Op.getImm();
171 if (!isInt<16>(Imm))
172 return false;
173 }
174 }
175 return MI.isAsCheapAsAMove();
176}
177
178// Do not sink floating point instructions that updates USR register.
179// Example:
180// feclearexcept
181// F2_conv_w2sf
182// fetestexcept
183// MachineSink sinks F2_conv_w2sf and we are not able to catch exceptions.
184// TODO: On some of these floating point instructions, USR is marked as Use.
185// In reality, these instructions also Def the USR. If USR is marked as Def,
186// some of the assumptions in assembler packetization are broken.
188 // Assumption: A floating point instruction that reads the USR will write
189 // the USR as well.
190 if (isFloat(MI) && MI.hasRegisterImplicitUseOperand(Hexagon::USR))
191 return false;
192 return true;
193}
194
195/// Find the hardware loop instruction used to set-up the specified loop.
196/// On Hexagon, we have two instructions used to set-up the hardware loop
197/// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
198/// to indicate the end of a loop.
200 unsigned EndLoopOp, MachineBasicBlock *TargetBB,
202 unsigned LOOPi;
203 unsigned LOOPr;
204 if (EndLoopOp == Hexagon::ENDLOOP0) {
205 LOOPi = Hexagon::J2_loop0i;
206 LOOPr = Hexagon::J2_loop0r;
207 } else { // EndLoopOp == Hexagon::EndLOOP1
208 LOOPi = Hexagon::J2_loop1i;
209 LOOPr = Hexagon::J2_loop1r;
210 }
211
212 // The loop set-up instruction will be in a predecessor block
213 for (MachineBasicBlock *PB : BB->predecessors()) {
214 // If this has been visited, already skip it.
215 if (!Visited.insert(PB).second)
216 continue;
217 if (PB == BB)
218 continue;
219 for (MachineInstr &I : llvm::reverse(PB->instrs())) {
220 unsigned Opc = I.getOpcode();
221 if (Opc == LOOPi || Opc == LOOPr)
222 return &I;
223 // We've reached a different loop, which means the loop01 has been
224 // removed.
225 if (Opc == EndLoopOp && I.getOperand(0).getMBB() != TargetBB)
226 return nullptr;
227 }
228 // Check the predecessors for the LOOP instruction.
229 if (MachineInstr *Loop = findLoopInstr(PB, EndLoopOp, TargetBB, Visited))
230 return Loop;
231 }
232 return nullptr;
233}
234
235/// Gather register def/uses from MI.
236/// This treats possible (predicated) defs as actually happening ones
237/// (conservatively).
238static inline void parseOperands(const MachineInstr &MI,
240 Defs.clear();
241 Uses.clear();
242
243 for (const MachineOperand &MO : MI.operands()) {
244 if (!MO.isReg())
245 continue;
246
247 Register Reg = MO.getReg();
248 if (!Reg)
249 continue;
250
251 if (MO.isUse())
252 Uses.push_back(MO.getReg());
253
254 if (MO.isDef())
255 Defs.push_back(MO.getReg());
256 }
257}
258
259// Position dependent, so check twice for swap.
260static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
261 switch (Ga) {
263 default:
264 return false;
266 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
268 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
269 Gb == HexagonII::HSIG_A);
271 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
274 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
275 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
276 Gb == HexagonII::HSIG_A);
278 return (Gb == HexagonII::HSIG_A);
280 return (Gb == HexagonII::HSIG_Compound);
281 }
282 return false;
283}
284
285/// isLoadFromStackSlot - If the specified machine instruction is a direct
286/// load from a stack slot, return the virtual or physical register number of
287/// the destination along with the FrameIndex of the loaded stack slot. If
288/// not, return 0. This predicate must return 0 if the instruction has
289/// any side effects other than loading from the stack slot.
291 int &FrameIndex) const {
292 switch (MI.getOpcode()) {
293 default:
294 break;
295 case Hexagon::L2_loadri_io:
296 case Hexagon::L2_loadrd_io:
297 case Hexagon::V6_vL32b_ai:
298 case Hexagon::V6_vL32b_nt_ai:
299 case Hexagon::V6_vL32Ub_ai:
300 case Hexagon::LDriw_pred:
301 case Hexagon::LDriw_ctr:
302 case Hexagon::PS_vloadrq_ai:
303 case Hexagon::PS_vloadrw_ai:
304 case Hexagon::PS_vloadrw_nt_ai:
305 case Hexagon::PS_loadrl:
306 case Hexagon::PS_loadrll: {
307 const MachineOperand OpFI = MI.getOperand(1);
308 if (!OpFI.isFI())
309 return 0;
310 const MachineOperand OpOff = MI.getOperand(2);
311 if (!OpOff.isImm() || OpOff.getImm() != 0)
312 return 0;
313 FrameIndex = OpFI.getIndex();
314 return MI.getOperand(0).getReg();
315 }
316
317 case Hexagon::L2_ploadrit_io:
318 case Hexagon::L2_ploadrif_io:
319 case Hexagon::L2_ploadrdt_io:
320 case Hexagon::L2_ploadrdf_io: {
321 const MachineOperand OpFI = MI.getOperand(2);
322 if (!OpFI.isFI())
323 return 0;
324 const MachineOperand OpOff = MI.getOperand(3);
325 if (!OpOff.isImm() || OpOff.getImm() != 0)
326 return 0;
327 FrameIndex = OpFI.getIndex();
328 return MI.getOperand(0).getReg();
329 }
330 }
331
332 return 0;
333}
334
335/// isStoreToStackSlot - If the specified machine instruction is a direct
336/// store to a stack slot, return the virtual or physical register number of
337/// the source reg along with the FrameIndex of the loaded stack slot. If
338/// not, return 0. This predicate must return 0 if the instruction has
339/// any side effects other than storing to the stack slot.
341 int &FrameIndex) const {
342 switch (MI.getOpcode()) {
343 default:
344 break;
345 case Hexagon::S2_storerb_io:
346 case Hexagon::S2_storerh_io:
347 case Hexagon::S2_storeri_io:
348 case Hexagon::S2_storerd_io:
349 case Hexagon::V6_vS32b_ai:
350 case Hexagon::V6_vS32Ub_ai:
351 case Hexagon::STriw_pred:
352 case Hexagon::STriw_ctr:
353 case Hexagon::PS_vstorerq_ai:
354 case Hexagon::PS_vstorerw_ai:
355 case Hexagon::PS_storerl:
356 case Hexagon::PS_storerll: {
357 const MachineOperand &OpFI = MI.getOperand(0);
358 if (!OpFI.isFI())
359 return 0;
360 const MachineOperand &OpOff = MI.getOperand(1);
361 if (!OpOff.isImm() || OpOff.getImm() != 0)
362 return 0;
363 FrameIndex = OpFI.getIndex();
364 return MI.getOperand(2).getReg();
365 }
366
367 case Hexagon::S2_pstorerbt_io:
368 case Hexagon::S2_pstorerbf_io:
369 case Hexagon::S2_pstorerht_io:
370 case Hexagon::S2_pstorerhf_io:
371 case Hexagon::S2_pstorerit_io:
372 case Hexagon::S2_pstorerif_io:
373 case Hexagon::S2_pstorerdt_io:
374 case Hexagon::S2_pstorerdf_io: {
375 const MachineOperand &OpFI = MI.getOperand(1);
376 if (!OpFI.isFI())
377 return 0;
378 const MachineOperand &OpOff = MI.getOperand(2);
379 if (!OpOff.isImm() || OpOff.getImm() != 0)
380 return 0;
381 FrameIndex = OpFI.getIndex();
382 return MI.getOperand(3).getReg();
383 }
384 }
385
386 return 0;
387}
388
389/// This function checks if the instruction or bundle of instructions
390/// has load from stack slot and returns frameindex and machine memory
391/// operand of that instruction if true.
393 const MachineInstr &MI,
395 if (MI.isBundle()) {
396 const MachineBasicBlock *MBB = MI.getParent();
398 for (++MII; MII != MBB->instr_end() && MII->isInsideBundle(); ++MII)
400 return true;
401 return false;
402 }
403
405}
406
407/// This function checks if the instruction or bundle of instructions
408/// has store to stack slot and returns frameindex and machine memory
409/// operand of that instruction if true.
411 const MachineInstr &MI,
413 if (MI.isBundle()) {
414 const MachineBasicBlock *MBB = MI.getParent();
416 for (++MII; MII != MBB->instr_end() && MII->isInsideBundle(); ++MII)
418 return true;
419 return false;
420 }
421
423}
424
425/// This function can analyze one/two way branching only and should (mostly) be
426/// called by target independent side.
427/// First entry is always the opcode of the branching instruction, except when
428/// the Cond vector is supposed to be empty, e.g., when analyzeBranch fails, a
429/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
430/// e.g. Jump_c p will have
431/// Cond[0] = Jump_c
432/// Cond[1] = p
433/// HW-loop ENDLOOP:
434/// Cond[0] = ENDLOOP
435/// Cond[1] = MBB
436/// New value jump:
437/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
438/// Cond[1] = R
439/// Cond[2] = Imm
442 MachineBasicBlock *&FBB,
444 bool AllowModify) const {
445 TBB = nullptr;
446 FBB = nullptr;
447 Cond.clear();
448
449 // If the block has no terminators, it just falls into the block after it.
451 if (I == MBB.instr_begin())
452 return false;
453
454 // A basic block may looks like this:
455 //
456 // [ insn
457 // EH_LABEL
458 // insn
459 // insn
460 // insn
461 // EH_LABEL
462 // insn ]
463 //
464 // It has two succs but does not have a terminator
465 // Don't know how to handle it.
466 do {
467 --I;
468 if (I->isEHLabel())
469 // Don't analyze EH branches.
470 return true;
471 } while (I != MBB.instr_begin());
472
473 I = MBB.instr_end();
474 --I;
475
476 while (I->isDebugInstr()) {
477 if (I == MBB.instr_begin())
478 return false;
479 --I;
480 }
481
482 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
483 I->getOperand(0).isMBB();
484 // Delete the J2_jump if it's equivalent to a fall-through.
485 if (AllowModify && JumpToBlock &&
486 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
487 LLVM_DEBUG(dbgs() << "\nErasing the jump to successor block\n";);
488 I->eraseFromParent();
489 I = MBB.instr_end();
490 if (I == MBB.instr_begin())
491 return false;
492 --I;
493 }
494 if (!isUnpredicatedTerminator(*I))
495 return false;
496
497 // Get the last instruction in the block.
498 MachineInstr *LastInst = &*I;
499 MachineInstr *SecondLastInst = nullptr;
500 // Find one more terminator if present.
501 while (true) {
502 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
503 if (!SecondLastInst)
504 SecondLastInst = &*I;
505 else
506 // This is a third branch.
507 return true;
508 }
509 if (I == MBB.instr_begin())
510 break;
511 --I;
512 }
513
514 int LastOpcode = LastInst->getOpcode();
515 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
516 // If the branch target is not a basic block, it could be a tail call.
517 // (It is, if the target is a function.)
518 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
519 return true;
520 if (SecLastOpcode == Hexagon::J2_jump &&
521 !SecondLastInst->getOperand(0).isMBB())
522 return true;
523
524 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
525 bool LastOpcodeHasNVJump = isNewValueJump(*LastInst);
526
527 if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
528 return true;
529
530 // If there is only one terminator instruction, process it.
531 if (LastInst && !SecondLastInst) {
532 if (LastOpcode == Hexagon::J2_jump) {
533 TBB = LastInst->getOperand(0).getMBB();
534 return false;
535 }
536 if (isEndLoopN(LastOpcode)) {
537 TBB = LastInst->getOperand(0).getMBB();
538 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
539 Cond.push_back(LastInst->getOperand(0));
540 return false;
541 }
542 if (LastOpcodeHasJMP_c) {
543 TBB = LastInst->getOperand(1).getMBB();
544 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
545 Cond.push_back(LastInst->getOperand(0));
546 return false;
547 }
548 // Only supporting rr/ri versions of new-value jumps.
549 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
550 TBB = LastInst->getOperand(2).getMBB();
551 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
552 Cond.push_back(LastInst->getOperand(0));
553 Cond.push_back(LastInst->getOperand(1));
554 return false;
555 }
556 LLVM_DEBUG(dbgs() << "\nCant analyze " << printMBBReference(MBB)
557 << " with one jump\n";);
558 // Otherwise, don't know what this is.
559 return true;
560 }
561
562 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
563 bool SecLastOpcodeHasNVJump = isNewValueJump(*SecondLastInst);
564 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
565 if (!SecondLastInst->getOperand(1).isMBB())
566 return true;
567 TBB = SecondLastInst->getOperand(1).getMBB();
568 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
569 Cond.push_back(SecondLastInst->getOperand(0));
570 FBB = LastInst->getOperand(0).getMBB();
571 return false;
572 }
573
574 // Only supporting rr/ri versions of new-value jumps.
575 if (SecLastOpcodeHasNVJump &&
576 (SecondLastInst->getNumExplicitOperands() == 3) &&
577 (LastOpcode == Hexagon::J2_jump)) {
578 TBB = SecondLastInst->getOperand(2).getMBB();
579 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
580 Cond.push_back(SecondLastInst->getOperand(0));
581 Cond.push_back(SecondLastInst->getOperand(1));
582 FBB = LastInst->getOperand(0).getMBB();
583 return false;
584 }
585
586 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
587 // executed, so remove it.
588 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
589 TBB = SecondLastInst->getOperand(0).getMBB();
590 I = LastInst->getIterator();
591 if (AllowModify)
592 I->eraseFromParent();
593 return false;
594 }
595
596 // If the block ends with an ENDLOOP, and J2_jump, handle it.
597 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
598 TBB = SecondLastInst->getOperand(0).getMBB();
599 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
600 Cond.push_back(SecondLastInst->getOperand(0));
601 FBB = LastInst->getOperand(0).getMBB();
602 return false;
603 }
604 LLVM_DEBUG(dbgs() << "\nCant analyze " << printMBBReference(MBB)
605 << " with two jumps";);
606 // Otherwise, can't handle this.
607 return true;
608}
609
611 int *BytesRemoved) const {
612 assert(!BytesRemoved && "code size not handled");
613
614 LLVM_DEBUG(dbgs() << "\nRemoving branches out of " << printMBBReference(MBB));
616 unsigned Count = 0;
617 while (I != MBB.begin()) {
618 --I;
619 if (I->isDebugInstr())
620 continue;
621 // Only removing branches from end of MBB.
622 if (!I->isBranch())
623 return Count;
624 if (Count && (I->getOpcode() == Hexagon::J2_jump))
625 llvm_unreachable("Malformed basic block: unconditional branch not last");
626 MBB.erase(&MBB.back());
627 I = MBB.end();
628 ++Count;
629 }
630 return Count;
631}
632
637 const DebugLoc &DL,
638 int *BytesAdded) const {
639 unsigned BOpc = Hexagon::J2_jump;
640 unsigned BccOpc = Hexagon::J2_jumpt;
641 assert(validateBranchCond(Cond) && "Invalid branching condition");
642 assert(TBB && "insertBranch must not be told to insert a fallthrough");
643 assert(!BytesAdded && "code size not handled");
644
645 // Check if reverseBranchCondition has asked to reverse this branch
646 // If we want to reverse the branch an odd number of times, we want
647 // J2_jumpf.
648 if (!Cond.empty() && Cond[0].isImm())
649 BccOpc = Cond[0].getImm();
650
651 if (!FBB) {
652 if (Cond.empty()) {
653 // Due to a bug in TailMerging/CFG Optimization, we need to add a
654 // special case handling of a predicated jump followed by an
655 // unconditional jump. If not, Tail Merging and CFG Optimization go
656 // into an infinite loop.
657 MachineBasicBlock *NewTBB, *NewFBB;
659 auto Term = MBB.getFirstTerminator();
660 if (Term != MBB.end() && isPredicated(*Term) &&
661 !analyzeBranch(MBB, NewTBB, NewFBB, Cond, false) &&
662 MachineFunction::iterator(NewTBB) == ++MBB.getIterator()) {
665 return insertBranch(MBB, TBB, nullptr, Cond, DL);
666 }
667 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
668 } else if (isEndLoopN(Cond[0].getImm())) {
669 int EndLoopOp = Cond[0].getImm();
670 assert(Cond[1].isMBB());
671 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
672 // Check for it, and change the BB target if needed.
674 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
675 VisitedBBs);
676 assert(Loop != nullptr && "Inserting an ENDLOOP without a LOOP");
677 Loop->getOperand(0).setMBB(TBB);
678 // Add the ENDLOOP after the finding the LOOP0.
679 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
680 } else if (isNewValueJump(Cond[0].getImm())) {
681 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
682 // New value jump
683 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
684 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
685 RegState Flags1 = getUndefRegState(Cond[1].isUndef());
686 LLVM_DEBUG(dbgs() << "\nInserting NVJump for "
688 if (Cond[2].isReg()) {
689 RegState Flags2 = getUndefRegState(Cond[2].isUndef());
690 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
691 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
692 } else if(Cond[2].isImm()) {
693 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
694 addImm(Cond[2].getImm()).addMBB(TBB);
695 } else
696 llvm_unreachable("Invalid condition for branching");
697 } else {
698 assert((Cond.size() == 2) && "Malformed cond vector");
699 const MachineOperand &RO = Cond[1];
700 RegState Flags = getUndefRegState(RO.isUndef());
701 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
702 }
703 return 1;
704 }
705 assert((!Cond.empty()) &&
706 "Cond. cannot be empty when multiple branchings are required");
707 assert((!isNewValueJump(Cond[0].getImm())) &&
708 "NV-jump cannot be inserted with another branch");
709 // Special case for hardware loops. The condition is a basic block.
710 if (isEndLoopN(Cond[0].getImm())) {
711 int EndLoopOp = Cond[0].getImm();
712 assert(Cond[1].isMBB());
713 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
714 // Check for it, and change the BB target if needed.
716 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
717 VisitedBBs);
718 assert(Loop != nullptr && "Inserting an ENDLOOP without a LOOP");
719 Loop->getOperand(0).setMBB(TBB);
720 // Add the ENDLOOP after the finding the LOOP0.
721 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
722 } else {
723 const MachineOperand &RO = Cond[1];
724 RegState Flags = getUndefRegState(RO.isUndef());
725 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
726 }
727 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
728
729 return 2;
730}
731
732namespace {
733class HexagonPipelinerLoopInfo : public TargetInstrInfo::PipelinerLoopInfo {
734 MachineInstr *Loop, *EndLoop;
735 MachineFunction *MF;
736 const HexagonInstrInfo *TII;
737 int64_t TripCount;
738 Register LoopCount;
739 DebugLoc DL;
740
741public:
742 HexagonPipelinerLoopInfo(MachineInstr *Loop, MachineInstr *EndLoop)
743 : Loop(Loop), EndLoop(EndLoop), MF(Loop->getParent()->getParent()),
744 TII(MF->getSubtarget<HexagonSubtarget>().getInstrInfo()),
745 DL(Loop->getDebugLoc()) {
746 // Inspect the Loop instruction up-front, as it may be deleted when we call
747 // createTripCountGreaterCondition.
748 TripCount = Loop->getOpcode() == Hexagon::J2_loop0r
749 ? -1
750 : Loop->getOperand(1).getImm();
751 if (TripCount == -1)
752 LoopCount = Loop->getOperand(1).getReg();
753 }
754
755 bool shouldIgnoreForPipelining(const MachineInstr *MI) const override {
756 // Only ignore the terminator.
757 return MI == EndLoop || TII->isHMXActivateInstr(*MI) ||
758 TII->isHMXWeightInstr(*MI);
759 }
760
761 std::optional<bool> createTripCountGreaterCondition(
762 int TC, MachineBasicBlock &MBB,
763 SmallVectorImpl<MachineOperand> &Cond) override {
764 if (TripCount == -1) {
765 // Check if we're done with the loop.
766 Register Done = TII->createVR(MF, MVT::i1);
767 MachineInstr *NewCmp = BuildMI(&MBB, DL,
768 TII->get(Hexagon::C2_cmpgtui), Done)
769 .addReg(LoopCount)
770 .addImm(TC);
771 Cond.push_back(MachineOperand::CreateImm(Hexagon::J2_jumpf));
772 Cond.push_back(NewCmp->getOperand(0));
773 return {};
774 }
775
776 return TripCount > TC;
777 }
778
779 void setPreheader(MachineBasicBlock *NewPreheader) override {
780 NewPreheader->splice(NewPreheader->getFirstTerminator(), Loop->getParent(),
781 Loop);
782 }
783
784 void adjustTripCount(int TripCountAdjust) override {
785 // If the loop trip count is a compile-time value, then just change the
786 // value.
787 if (Loop->getOpcode() == Hexagon::J2_loop0i ||
788 Loop->getOpcode() == Hexagon::J2_loop1i) {
789 int64_t TripCount = Loop->getOperand(1).getImm() + TripCountAdjust;
790 assert(TripCount > 0 && "Can't create an empty or negative loop!");
791 Loop->getOperand(1).setImm(TripCount);
792 return;
793 }
794
795 // The loop trip count is a run-time value. We generate code to subtract
796 // one from the trip count, and update the loop instruction.
797 Register LoopCount = Loop->getOperand(1).getReg();
798 Register NewLoopCount = TII->createVR(MF, MVT::i32);
799 BuildMI(*Loop->getParent(), Loop, Loop->getDebugLoc(),
800 TII->get(Hexagon::A2_addi), NewLoopCount)
801 .addReg(LoopCount)
802 .addImm(TripCountAdjust);
803 Loop->getOperand(1).setReg(NewLoopCount);
804 }
805
806 void disposed(LiveIntervals *LIS) override {
807 if (LIS)
809 Loop->eraseFromParent();
810 }
811};
812} // namespace
813
814std::unique_ptr<TargetInstrInfo::PipelinerLoopInfo>
816 // We really "analyze" only hardware loops right now.
818
819 if (I != LoopBB->end() && isEndLoopN(I->getOpcode())) {
821 MachineInstr *LoopInst = findLoopInstr(
822 LoopBB, I->getOpcode(), I->getOperand(0).getMBB(), VisitedBBs);
823 if (LoopInst)
824 return std::make_unique<HexagonPipelinerLoopInfo>(LoopInst, &*I);
825 }
826 return nullptr;
827}
828
830 unsigned NumCycles, unsigned ExtraPredCycles,
831 BranchProbability Probability) const {
832 return nonDbgBBSize(&MBB) <= 3;
833}
834
836 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
837 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
838 const {
839 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
840}
841
843 unsigned NumInstrs, BranchProbability Probability) const {
844 return NumInstrs <= 4;
845}
846
847static void getLiveInRegsAt(LivePhysRegs &Regs, const MachineInstr &MI) {
849 const MachineBasicBlock &B = *MI.getParent();
850 Regs.addLiveIns(B);
851 auto E = MachineBasicBlock::const_iterator(MI.getIterator());
852 for (auto I = B.begin(); I != E; ++I) {
853 Clobbers.clear();
854 Regs.stepForward(*I, Clobbers);
855 }
856}
857
858static void getLiveOutRegsAt(LivePhysRegs &Regs, const MachineInstr &MI) {
859 const MachineBasicBlock &B = *MI.getParent();
860 Regs.addLiveOuts(B);
861 auto E = ++MachineBasicBlock::const_iterator(MI.getIterator()).getReverse();
862 for (auto I = B.rbegin(); I != E; ++I)
863 Regs.stepBackward(*I);
864}
865
868 const DebugLoc &DL, Register DestReg,
869 Register SrcReg, bool KillSrc,
870 bool RenamableDest,
871 bool RenamableSrc) const {
872 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
873 RegState KillFlag = getKillRegState(KillSrc);
874
875 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
876 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg)
877 .addReg(SrcReg, KillFlag);
878 return;
879 }
880 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
881 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg)
882 .addReg(SrcReg, KillFlag);
883 return;
884 }
885 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
886 // Map Pd = Ps to Pd = or(Ps, Ps).
887 BuildMI(MBB, I, DL, get(Hexagon::C2_or), DestReg)
888 .addReg(SrcReg).addReg(SrcReg, KillFlag);
889 return;
890 }
891 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
892 Hexagon::IntRegsRegClass.contains(SrcReg)) {
893 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
894 .addReg(SrcReg, KillFlag);
895 return;
896 }
897 if (Hexagon::IntRegsRegClass.contains(DestReg) &&
898 Hexagon::CtrRegsRegClass.contains(SrcReg)) {
899 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrcrr), DestReg)
900 .addReg(SrcReg, KillFlag);
901 return;
902 }
903 if (Hexagon::ModRegsRegClass.contains(DestReg) &&
904 Hexagon::IntRegsRegClass.contains(SrcReg)) {
905 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
906 .addReg(SrcReg, KillFlag);
907 return;
908 }
909 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
910 Hexagon::IntRegsRegClass.contains(DestReg)) {
911 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
912 .addReg(SrcReg, KillFlag);
913 return;
914 }
915 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
916 Hexagon::PredRegsRegClass.contains(DestReg)) {
917 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg)
918 .addReg(SrcReg, KillFlag);
919 return;
920 }
921 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
922 Hexagon::IntRegsRegClass.contains(DestReg)) {
923 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
924 .addReg(SrcReg, KillFlag);
925 return;
926 }
927 if (Hexagon::HvxVRRegClass.contains(SrcReg, DestReg)) {
928 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
929 addReg(SrcReg, KillFlag);
930 return;
931 }
932 if (Hexagon::HvxWRRegClass.contains(SrcReg, DestReg)) {
933 LivePhysRegs LiveAtMI(HRI);
934 getLiveInRegsAt(LiveAtMI, *I);
935 Register SrcLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
936 Register SrcHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
937 RegState UndefLo = getUndefRegState(!LiveAtMI.contains(SrcLo));
938 RegState UndefHi = getUndefRegState(!LiveAtMI.contains(SrcHi));
939 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
940 .addReg(SrcHi, KillFlag | UndefHi)
941 .addReg(SrcLo, KillFlag | UndefLo);
942 return;
943 }
944 if (Hexagon::HvxQRRegClass.contains(SrcReg, DestReg)) {
945 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
946 .addReg(SrcReg)
947 .addReg(SrcReg, KillFlag);
948 return;
949 }
950 if (Hexagon::HvxQRRegClass.contains(SrcReg) &&
951 Hexagon::HvxVRRegClass.contains(DestReg)) {
952 llvm_unreachable("Unimplemented pred to vec");
953 return;
954 }
955 if (Hexagon::HvxQRRegClass.contains(DestReg) &&
956 Hexagon::HvxVRRegClass.contains(SrcReg)) {
957 llvm_unreachable("Unimplemented vec to pred");
958 return;
959 }
960 if (Hexagon::HlxLRRegClass.contains(SrcReg, DestReg)) {
961 BuildMI(MBB, I, DL, get(Hexagon::LX_assign), DestReg)
962 .addReg(SrcReg, KillFlag);
963 return;
964 }
965 if (Hexagon::HlxLLRRegClass.contains(SrcReg, DestReg)) {
966 BuildMI(MBB, I, DL, get(Hexagon::LX_assign_w), DestReg)
967 .addReg(SrcReg, KillFlag);
968 return;
969 }
970
971#ifndef NDEBUG
972 // Show the invalid registers to ease debugging.
973 dbgs() << "Invalid registers for copy in " << printMBBReference(MBB) << ": "
974 << printReg(DestReg, &HRI) << " = " << printReg(SrcReg, &HRI) << '\n';
975#endif
976 llvm_unreachable("Unimplemented");
977}
978
981 Register SrcReg, bool isKill, int FI,
982 const TargetRegisterClass *RC,
983 Register VReg,
984 MachineInstr::MIFlag Flags) const {
985 DebugLoc DL = MBB.findDebugLoc(I);
986 MachineFunction &MF = *MBB.getParent();
987 MachineFrameInfo &MFI = MF.getFrameInfo();
988 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
989 RegState KillFlag = getKillRegState(isKill);
990
991 int MFIIndex = FI;
992 MachineFrameInfo *MMFI = &MFI;
993 if (HMFI.isVTCMStackIndex(FI)) {
994 MFIIndex = HMFI.getMappedVTCMObjectIndex(FI);
995 MMFI = &HMFI.getVTCMFrameInfo();
996 }
999 MMFI->getObjectSize(MFIIndex), MMFI->getObjectAlign(MFIIndex));
1000
1001 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
1002 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
1003 .addFrameIndex(FI).addImm(0)
1004 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
1005 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
1006 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
1007 .addFrameIndex(FI).addImm(0)
1008 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
1009 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
1010 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
1011 .addFrameIndex(FI).addImm(0)
1012 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
1013 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
1014 BuildMI(MBB, I, DL, get(Hexagon::STriw_ctr))
1015 .addFrameIndex(FI).addImm(0)
1016 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
1017 } else if (Hexagon::CircRegsRegClass.hasSubClassEq(RC)) {
1018 BuildMI(MBB, I, DL, get(Hexagon::STriw_circ))
1019 .addFrameIndex(FI)
1020 .addReg(SrcReg, KillFlag)
1021 .addMemOperand(MMO);
1022 } else if (Hexagon::HvxQRRegClass.hasSubClassEq(RC)) {
1023 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai))
1024 .addFrameIndex(FI).addImm(0)
1025 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
1026 } else if (Hexagon::HvxVRRegClass.hasSubClassEq(RC)) {
1027 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerv_ai))
1028 .addFrameIndex(FI).addImm(0)
1029 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
1030 } else if (Hexagon::HvxWRRegClass.hasSubClassEq(RC)) {
1031 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerw_ai))
1032 .addFrameIndex(FI).addImm(0)
1033 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
1034 } else if (Hexagon::HlxLRRegClass.hasSubClassEq(RC)) {
1035 BuildMI(MBB, I, DL, get(Hexagon::PS_storerl))
1036 .addFrameIndex(FI)
1037 .addImm(0)
1038 .addReg(SrcReg, KillFlag)
1039 .addMemOperand(MMO);
1040 } else if (Hexagon::HlxLLRRegClass.hasSubClassEq(RC)) {
1041 BuildMI(MBB, I, DL, get(Hexagon::PS_storerll))
1042 .addFrameIndex(FI)
1043 .addImm(0)
1044 .addReg(SrcReg, KillFlag)
1045 .addMemOperand(MMO);
1046 } else {
1047 llvm_unreachable("Unimplemented");
1048 }
1049}
1050
1053 Register DestReg, int FI,
1054 const TargetRegisterClass *RC,
1055 Register VReg, unsigned SubReg,
1056 MachineInstr::MIFlag Flags) const {
1057 DebugLoc DL = MBB.findDebugLoc(I);
1058 MachineFunction &MF = *MBB.getParent();
1059 MachineFrameInfo &MFI = MF.getFrameInfo();
1060 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
1061
1062 int MFIIndex = FI;
1063 MachineFrameInfo *MMFI = &MFI;
1064 if (HMFI.isVTCMStackIndex(FI)) {
1065 MFIIndex = HMFI.getMappedVTCMObjectIndex(FI);
1066 MMFI = &HMFI.getVTCMFrameInfo();
1067 }
1070 MMFI->getObjectSize(MFIIndex), MMFI->getObjectAlign(MFIIndex));
1071
1072 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
1073 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
1074 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
1075 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
1076 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
1077 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
1078 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
1079 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
1080 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
1081 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
1082 BuildMI(MBB, I, DL, get(Hexagon::LDriw_ctr), DestReg)
1083 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
1084 } else if (Hexagon::CircRegsRegClass.hasSubClassEq(RC)) {
1085 BuildMI(MBB, I, DL, get(Hexagon::LDriw_circ), DestReg)
1086 .addFrameIndex(FI)
1087 .addMemOperand(MMO);
1088 } else if (Hexagon::HvxQRRegClass.hasSubClassEq(RC)) {
1089 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai), DestReg)
1090 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
1091 } else if (Hexagon::HvxVRRegClass.hasSubClassEq(RC)) {
1092 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrv_ai), DestReg)
1093 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
1094 } else if (Hexagon::HvxWRRegClass.hasSubClassEq(RC)) {
1095 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrw_ai), DestReg)
1096 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
1097 } else if (Hexagon::HlxLRRegClass.hasSubClassEq(RC)) {
1098 BuildMI(MBB, I, DL, get(Hexagon::PS_loadrl), DestReg)
1099 .addFrameIndex(FI)
1100 .addImm(0)
1101 .addMemOperand(MMO);
1102 } else if (Hexagon::HlxLLRRegClass.hasSubClassEq(RC)) {
1103 BuildMI(MBB, I, DL, get(Hexagon::PS_loadrll), DestReg)
1104 .addFrameIndex(FI)
1105 .addImm(0)
1106 .addMemOperand(MMO);
1107 } else {
1108 llvm_unreachable("Can't store this register to stack slot");
1109 }
1110}
1111
1112/// expandPostRAPseudo - This function is called for all pseudo instructions
1113/// that remain after register allocation. Many pseudo instructions are
1114/// created to help register allocation. This is the place to convert them
1115/// into real instructions. The target can edit MI in place, or it can insert
1116/// new instructions and erase MI. The function should return true if
1117/// anything was changed.
1119 MachineBasicBlock &MBB = *MI.getParent();
1120 MachineFunction &MF = *MBB.getParent();
1122 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
1123 LivePhysRegs LiveIn(HRI), LiveOut(HRI);
1124 DebugLoc DL = MI.getDebugLoc();
1125 unsigned Opc = MI.getOpcode();
1126
1127 auto RealCirc = [&](unsigned Opc, bool HasImm, unsigned MxOp) {
1128 Register M_CSx = MI.getOperand(MxOp).getReg();
1129 Register Mx = HRI.getSubReg(M_CSx, Hexagon::isub_hi);
1130 unsigned CSOp = HasImm ? 5 : 4;
1131 auto MIB = BuildMI(MBB, MI, DL, get(Opc));
1132 for (unsigned i = 0; i != CSOp; ++i)
1133 if (i == MxOp)
1134 MIB.addReg(Mx);
1135 else
1136 MIB.add(MI.getOperand(i));
1137 Register CSx = (M_CSx == Hexagon::CS_M0 ? Hexagon::CS0 : Hexagon::CS1);
1138 MIB.addReg(CSx, RegState::Implicit);
1139 MBB.erase(MI);
1140 return true;
1141 };
1142
1143 auto UseAligned = [&](const MachineInstr &MI, Align NeedAlign) {
1144 if (MI.memoperands().empty())
1145 return false;
1146 return all_of(MI.memoperands(), [NeedAlign](const MachineMemOperand *MMO) {
1147 return MMO->getAlign() >= NeedAlign;
1148 });
1149 };
1150
1151 switch (Opc) {
1152 case Hexagon::PS_call_instrprof_custom: {
1153 auto Op0 = MI.getOperand(0);
1154 assert(Op0.isGlobal() &&
1155 "First operand must be a global containing handler name.");
1156 const GlobalValue *NameVar = Op0.getGlobal();
1157 const GlobalVariable *GV = dyn_cast<GlobalVariable>(NameVar);
1158 auto *Arr = cast<ConstantDataArray>(GV->getInitializer());
1159 StringRef NameStr = Arr->isCString() ? Arr->getAsCString() : Arr->getAsString();
1160
1161 MachineOperand &Op1 = MI.getOperand(1);
1162 // Set R0 with the imm value to be passed to the custom profiling handler.
1163 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrsi), Hexagon::R0)
1164 .addImm(Op1.getImm());
1165 // The call to the custom handler is being treated as a special one as the
1166 // callee is responsible for saving and restoring all the registers
1167 // (including caller saved registers) it needs to modify. This is
1168 // done to reduce the impact of instrumentation on the code being
1169 // instrumented/profiled.
1170 // NOTE: R14, R15 and R28 are reserved for PLT handling. These registers
1171 // are in the Def list of the Hexagon::PS_call_instrprof_custom and
1172 // therefore will be handled appropriately duing register allocation.
1173
1174 // TODO: It may be a good idea to add a separate pseudo instruction for
1175 // static relocation which doesn't need to reserve r14, r15 and r28.
1176
1177 auto MIB = BuildMI(MBB, MI, DL, get(Hexagon::J2_call))
1178 .addUse(Hexagon::R0, RegState::Implicit)
1179 .addDef(Hexagon::R29, RegState::ImplicitDefine)
1180 .addDef(Hexagon::R30, RegState::ImplicitDefine)
1181 .addDef(Hexagon::R14, RegState::ImplicitDefine)
1182 .addDef(Hexagon::R15, RegState::ImplicitDefine)
1183 .addDef(Hexagon::R28, RegState::ImplicitDefine);
1184 const char *cstr = MF.createExternalSymbolName(NameStr);
1185 MIB.addExternalSymbol(cstr);
1186 MBB.erase(MI);
1187 return true;
1188 }
1189 case TargetOpcode::COPY: {
1190 MachineOperand &MD = MI.getOperand(0);
1191 MachineOperand &MS = MI.getOperand(1);
1192 MachineBasicBlock::iterator MBBI = MI.getIterator();
1193 if (MD.getReg() != MS.getReg() && !MS.isUndef()) {
1194 copyPhysReg(MBB, MI, DL, MD.getReg(), MS.getReg(), MS.isKill());
1195 std::prev(MBBI)->copyImplicitOps(*MBB.getParent(), MI);
1196 }
1197 MBB.erase(MBBI);
1198 return true;
1199 }
1200 case Hexagon::PS_aligna:
1201 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI.getOperand(0).getReg())
1202 .addReg(HRI.getFrameRegister())
1203 .addImm(-MI.getOperand(1).getImm());
1204 MBB.erase(MI);
1205 return true;
1206 case Hexagon::V6_vassignp: {
1207 Register SrcReg = MI.getOperand(1).getReg();
1208 Register DstReg = MI.getOperand(0).getReg();
1209 Register SrcLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
1210 Register SrcHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
1211 getLiveInRegsAt(LiveIn, MI);
1212 RegState UndefLo = getUndefRegState(!LiveIn.contains(SrcLo));
1213 RegState UndefHi = getUndefRegState(!LiveIn.contains(SrcHi));
1214 RegState Kill = getKillRegState(MI.getOperand(1).isKill());
1215 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcombine), DstReg)
1216 .addReg(SrcHi, UndefHi)
1217 .addReg(SrcLo, Kill | UndefLo);
1218 MBB.erase(MI);
1219 return true;
1220 }
1221 case Hexagon::V6_lo:
1222 case Hexagon::LX_lo: {
1223 Register SrcReg = MI.getOperand(1).getReg();
1224 Register DstReg = MI.getOperand(0).getReg();
1225 Register SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
1226 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
1227 MBB.erase(MI);
1228 return true;
1229 }
1230 case Hexagon::V6_hi:
1231 case Hexagon::LX_hi: {
1232 Register SrcReg = MI.getOperand(1).getReg();
1233 Register DstReg = MI.getOperand(0).getReg();
1234 Register SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
1235 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
1236 MBB.erase(MI);
1237 return true;
1238 }
1239 case Hexagon::PS_vloadrv_ai: {
1240 Register DstReg = MI.getOperand(0).getReg();
1241 const MachineOperand &BaseOp = MI.getOperand(1);
1242 assert(BaseOp.getSubReg() == 0);
1243 int Offset = MI.getOperand(2).getImm();
1244 Align NeedAlign = HRI.getSpillAlign(Hexagon::HvxVRRegClass);
1245 unsigned NewOpc = UseAligned(MI, NeedAlign) ? Hexagon::V6_vL32b_ai
1246 : Hexagon::V6_vL32Ub_ai;
1247 BuildMI(MBB, MI, DL, get(NewOpc), DstReg)
1248 .addReg(BaseOp.getReg(), getRegState(BaseOp))
1249 .addImm(Offset)
1250 .cloneMemRefs(MI);
1251 MBB.erase(MI);
1252 return true;
1253 }
1254 case Hexagon::PS_loadrl: {
1255 Register DstReg = MI.getOperand(0).getReg();
1256 const MachineOperand &BaseOp = MI.getOperand(1);
1257 assert(BaseOp.getSubReg() == 0);
1258 int Offset = MI.getOperand(2).getImm();
1259 Register BaseReg = BaseOp.getReg();
1260 BuildMI(MBB, MI, DL, get(Hexagon::A2_addi), BaseReg)
1261 .addReg(BaseReg, getRegState(BaseOp) | RegState::Kill)
1262 .addImm(Offset);
1263 BuildMI(MBB, MI, DL, get(Hexagon::LX_L16b_pseudo), DstReg)
1264 .addReg(BaseReg, getRegState(BaseOp))
1265 .addReg(Hexagon::VSP, RegState::Implicit)
1266 .cloneMemRefs(MI);
1267 MBB.erase(MI);
1268 return true;
1269 }
1270 case Hexagon::PS_vloadrw_ai: {
1271 Register DstReg = MI.getOperand(0).getReg();
1272 const MachineOperand &BaseOp = MI.getOperand(1);
1273 assert(BaseOp.getSubReg() == 0);
1274 int Offset = MI.getOperand(2).getImm();
1275 unsigned VecOffset = HRI.getSpillSize(Hexagon::HvxVRRegClass);
1276 Align NeedAlign = HRI.getSpillAlign(Hexagon::HvxVRRegClass);
1277 unsigned NewOpc = UseAligned(MI, NeedAlign) ? Hexagon::V6_vL32b_ai
1278 : Hexagon::V6_vL32Ub_ai;
1279 BuildMI(MBB, MI, DL, get(NewOpc),
1280 HRI.getSubReg(DstReg, Hexagon::vsub_lo))
1281 .addReg(BaseOp.getReg(), getRegState(BaseOp) & ~RegState::Kill)
1282 .addImm(Offset)
1283 .cloneMemRefs(MI);
1284 BuildMI(MBB, MI, DL, get(NewOpc),
1285 HRI.getSubReg(DstReg, Hexagon::vsub_hi))
1286 .addReg(BaseOp.getReg(), getRegState(BaseOp))
1287 .addImm(Offset + VecOffset)
1288 .cloneMemRefs(MI);
1289 MBB.erase(MI);
1290 return true;
1291 }
1292 case Hexagon::PS_vstorerv_ai: {
1293 const MachineOperand &SrcOp = MI.getOperand(2);
1294 assert(SrcOp.getSubReg() == 0);
1295 const MachineOperand &BaseOp = MI.getOperand(0);
1296 assert(BaseOp.getSubReg() == 0);
1297 int Offset = MI.getOperand(1).getImm();
1298 Align NeedAlign = HRI.getSpillAlign(Hexagon::HvxVRRegClass);
1299 unsigned NewOpc = UseAligned(MI, NeedAlign) ? Hexagon::V6_vS32b_ai
1300 : Hexagon::V6_vS32Ub_ai;
1301 BuildMI(MBB, MI, DL, get(NewOpc))
1302 .addReg(BaseOp.getReg(), getRegState(BaseOp))
1303 .addImm(Offset)
1305 .cloneMemRefs(MI);
1306 MBB.erase(MI);
1307 return true;
1308 }
1309 case Hexagon::PS_storerl: {
1310 const MachineOperand &SrcOp = MI.getOperand(2);
1311 assert(SrcOp.getSubReg() == 0);
1312 const MachineOperand &BaseOp = MI.getOperand(0);
1313 assert(BaseOp.getSubReg() == 0);
1314 Register BaseReg = BaseOp.getReg();
1315 int Offset = MI.getOperand(1).getImm();
1316 BuildMI(MBB, MI, DL, get(Hexagon::A2_addi), BaseReg)
1317 .addReg(BaseReg, getRegState(BaseOp) | RegState::Kill)
1318 .addImm(Offset);
1319 BuildMI(MBB, MI, DL, get(Hexagon::LX_S16b_pseudo))
1320 .addReg(BaseReg, getRegState(BaseOp))
1322 .addReg(Hexagon::VSP, RegState::Implicit)
1323 .cloneMemRefs(MI);
1324 MBB.erase(MI);
1325 return true;
1326 }
1327 case Hexagon::PS_vstorerw_ai: {
1328 Register SrcReg = MI.getOperand(2).getReg();
1329 const MachineOperand &BaseOp = MI.getOperand(0);
1330 assert(BaseOp.getSubReg() == 0);
1331 int Offset = MI.getOperand(1).getImm();
1332 unsigned VecOffset = HRI.getSpillSize(Hexagon::HvxVRRegClass);
1333 Align NeedAlign = HRI.getSpillAlign(Hexagon::HvxVRRegClass);
1334 unsigned NewOpc = UseAligned(MI, NeedAlign) ? Hexagon::V6_vS32b_ai
1335 : Hexagon::V6_vS32Ub_ai;
1336 BuildMI(MBB, MI, DL, get(NewOpc))
1337 .addReg(BaseOp.getReg(), getRegState(BaseOp) & ~RegState::Kill)
1338 .addImm(Offset)
1339 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_lo))
1340 .cloneMemRefs(MI);
1341 BuildMI(MBB, MI, DL, get(NewOpc))
1342 .addReg(BaseOp.getReg(), getRegState(BaseOp))
1343 .addImm(Offset + VecOffset)
1344 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_hi))
1345 .cloneMemRefs(MI);
1346 MBB.erase(MI);
1347 return true;
1348 }
1349 case Hexagon::PS_true: {
1350 Register Reg = MI.getOperand(0).getReg();
1351 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1352 .addReg(Reg, RegState::Undef)
1353 .addReg(Reg, RegState::Undef);
1354 MBB.erase(MI);
1355 return true;
1356 }
1357 case Hexagon::PS_false: {
1358 Register Reg = MI.getOperand(0).getReg();
1359 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1360 .addReg(Reg, RegState::Undef)
1361 .addReg(Reg, RegState::Undef);
1362 MBB.erase(MI);
1363 return true;
1364 }
1365 case Hexagon::PS_qtrue: {
1366 BuildMI(MBB, MI, DL, get(Hexagon::V6_veqw), MI.getOperand(0).getReg())
1367 .addReg(Hexagon::V0, RegState::Undef)
1368 .addReg(Hexagon::V0, RegState::Undef);
1369 MBB.erase(MI);
1370 return true;
1371 }
1372 case Hexagon::PS_qfalse: {
1373 BuildMI(MBB, MI, DL, get(Hexagon::V6_vgtw), MI.getOperand(0).getReg())
1374 .addReg(Hexagon::V0, RegState::Undef)
1375 .addReg(Hexagon::V0, RegState::Undef);
1376 MBB.erase(MI);
1377 return true;
1378 }
1379 case Hexagon::PS_vdd0: {
1380 Register Vd = MI.getOperand(0).getReg();
1381 BuildMI(MBB, MI, DL, get(Hexagon::V6_vsubw_dv), Vd)
1383 .addReg(Vd, RegState::Undef);
1384 MBB.erase(MI);
1385 return true;
1386 }
1387 case Hexagon::PS_vmulw: {
1388 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
1389 Register DstReg = MI.getOperand(0).getReg();
1390 Register Src1Reg = MI.getOperand(1).getReg();
1391 Register Src2Reg = MI.getOperand(2).getReg();
1392 Register Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1393 Register Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1394 Register Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1395 Register Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
1396 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
1397 HRI.getSubReg(DstReg, Hexagon::isub_hi))
1398 .addReg(Src1SubHi)
1399 .addReg(Src2SubHi);
1400 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
1401 HRI.getSubReg(DstReg, Hexagon::isub_lo))
1402 .addReg(Src1SubLo)
1403 .addReg(Src2SubLo);
1404 MBB.erase(MI);
1405 return true;
1406 }
1407 case Hexagon::PS_vmulw_acc: {
1408 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
1409 Register DstReg = MI.getOperand(0).getReg();
1410 Register Src1Reg = MI.getOperand(1).getReg();
1411 Register Src2Reg = MI.getOperand(2).getReg();
1412 Register Src3Reg = MI.getOperand(3).getReg();
1413 Register Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1414 Register Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1415 Register Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1416 Register Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
1417 Register Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::isub_hi);
1418 Register Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::isub_lo);
1419 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
1420 HRI.getSubReg(DstReg, Hexagon::isub_hi))
1421 .addReg(Src1SubHi)
1422 .addReg(Src2SubHi)
1423 .addReg(Src3SubHi);
1424 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
1425 HRI.getSubReg(DstReg, Hexagon::isub_lo))
1426 .addReg(Src1SubLo)
1427 .addReg(Src2SubLo)
1428 .addReg(Src3SubLo);
1429 MBB.erase(MI);
1430 return true;
1431 }
1432 case Hexagon::PS_pselect: {
1433 const MachineOperand &Op0 = MI.getOperand(0);
1434 const MachineOperand &Op1 = MI.getOperand(1);
1435 const MachineOperand &Op2 = MI.getOperand(2);
1436 const MachineOperand &Op3 = MI.getOperand(3);
1437 Register Rd = Op0.getReg();
1438 Register Pu = Op1.getReg();
1439 Register Rs = Op2.getReg();
1440 Register Rt = Op3.getReg();
1441 DebugLoc DL = MI.getDebugLoc();
1442 RegState K1 = getKillRegState(Op1.isKill());
1443 RegState K2 = getKillRegState(Op2.isKill());
1444 RegState K3 = getKillRegState(Op3.isKill());
1445 if (Rd != Rs)
1446 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1447 .addReg(Pu, (Rd == Rt) ? K1 : RegState::NoFlags)
1448 .addReg(Rs, K2);
1449 if (Rd != Rt)
1450 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1451 .addReg(Pu, K1)
1452 .addReg(Rt, K3);
1453 MBB.erase(MI);
1454 return true;
1455 }
1456 case Hexagon::PS_vselect: {
1457 const MachineOperand &Op0 = MI.getOperand(0);
1458 const MachineOperand &Op1 = MI.getOperand(1);
1459 const MachineOperand &Op2 = MI.getOperand(2);
1460 const MachineOperand &Op3 = MI.getOperand(3);
1461 getLiveOutRegsAt(LiveOut, MI);
1462 bool IsDestLive = !LiveOut.available(MRI, Op0.getReg());
1463 Register PReg = Op1.getReg();
1464 assert(Op1.getSubReg() == 0);
1465 RegState PState = getRegState(Op1);
1466
1467 if (Op0.getReg() != Op2.getReg()) {
1468 RegState S =
1469 Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill : PState;
1470 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
1471 .add(Op0)
1472 .addReg(PReg, S)
1473 .add(Op2);
1474 if (IsDestLive)
1475 T.addReg(Op0.getReg(), RegState::Implicit);
1476 IsDestLive = true;
1477 }
1478 if (Op0.getReg() != Op3.getReg()) {
1479 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
1480 .add(Op0)
1481 .addReg(PReg, PState)
1482 .add(Op3);
1483 if (IsDestLive)
1484 T.addReg(Op0.getReg(), RegState::Implicit);
1485 }
1486 MBB.erase(MI);
1487 return true;
1488 }
1489 case Hexagon::PS_wselect: {
1490 MachineOperand &Op0 = MI.getOperand(0);
1491 MachineOperand &Op1 = MI.getOperand(1);
1492 MachineOperand &Op2 = MI.getOperand(2);
1493 MachineOperand &Op3 = MI.getOperand(3);
1494 getLiveOutRegsAt(LiveOut, MI);
1495 bool IsDestLive = !LiveOut.available(MRI, Op0.getReg());
1496 Register PReg = Op1.getReg();
1497 assert(Op1.getSubReg() == 0);
1498 RegState PState = getRegState(Op1);
1499
1500 if (Op0.getReg() != Op2.getReg()) {
1501 RegState S =
1502 Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill : PState;
1503 Register SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_lo);
1504 Register SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_hi);
1505 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
1506 .add(Op0)
1507 .addReg(PReg, S)
1508 .addReg(SrcHi)
1509 .addReg(SrcLo);
1510 if (IsDestLive)
1511 T.addReg(Op0.getReg(), RegState::Implicit);
1512 IsDestLive = true;
1513 }
1514 if (Op0.getReg() != Op3.getReg()) {
1515 Register SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_lo);
1516 Register SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_hi);
1517 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
1518 .add(Op0)
1519 .addReg(PReg, PState)
1520 .addReg(SrcHi)
1521 .addReg(SrcLo);
1522 if (IsDestLive)
1523 T.addReg(Op0.getReg(), RegState::Implicit);
1524 }
1525 MBB.erase(MI);
1526 return true;
1527 }
1528
1529 case Hexagon::PS_crash: {
1530 // Generate a misaligned load that is guaranteed to cause a crash.
1531 class CrashPseudoSourceValue : public PseudoSourceValue {
1532 public:
1533 CrashPseudoSourceValue(const TargetMachine &TM)
1534 : PseudoSourceValue(TargetCustom, TM) {}
1535
1536 bool isConstant(const MachineFrameInfo *) const override {
1537 return false;
1538 }
1539 bool isAliased(const MachineFrameInfo *) const override {
1540 return false;
1541 }
1542 bool mayAlias(const MachineFrameInfo *) const override {
1543 return false;
1544 }
1545 void printCustom(raw_ostream &OS) const override {
1546 OS << "MisalignedCrash";
1547 }
1548 };
1549
1550 static const CrashPseudoSourceValue CrashPSV(MF.getTarget());
1552 MachinePointerInfo(&CrashPSV),
1554 Align(1));
1555 BuildMI(MBB, MI, DL, get(Hexagon::PS_loadrdabs), Hexagon::D13)
1556 .addImm(0xBADC0FEE) // Misaligned load.
1557 .addMemOperand(MMO);
1558 MBB.erase(MI);
1559 return true;
1560 }
1561
1562 case Hexagon::PS_tailcall_i:
1563 MI.setDesc(get(Hexagon::J2_jump));
1564 return true;
1565 case Hexagon::PS_tailcall_r:
1566 case Hexagon::PS_jmpret:
1567 MI.setDesc(get(Hexagon::J2_jumpr));
1568 return true;
1569 case Hexagon::PS_jmprett:
1570 MI.setDesc(get(Hexagon::J2_jumprt));
1571 return true;
1572 case Hexagon::PS_jmpretf:
1573 MI.setDesc(get(Hexagon::J2_jumprf));
1574 return true;
1575 case Hexagon::PS_jmprettnewpt:
1576 MI.setDesc(get(Hexagon::J2_jumprtnewpt));
1577 return true;
1578 case Hexagon::PS_jmpretfnewpt:
1579 MI.setDesc(get(Hexagon::J2_jumprfnewpt));
1580 return true;
1581 case Hexagon::PS_jmprettnew:
1582 MI.setDesc(get(Hexagon::J2_jumprtnew));
1583 return true;
1584 case Hexagon::PS_jmpretfnew:
1585 MI.setDesc(get(Hexagon::J2_jumprfnew));
1586 return true;
1587
1588 case Hexagon::PS_loadrub_pci:
1589 return RealCirc(Hexagon::L2_loadrub_pci, /*HasImm*/true, /*MxOp*/4);
1590 case Hexagon::PS_loadrb_pci:
1591 return RealCirc(Hexagon::L2_loadrb_pci, /*HasImm*/true, /*MxOp*/4);
1592 case Hexagon::PS_loadruh_pci:
1593 return RealCirc(Hexagon::L2_loadruh_pci, /*HasImm*/true, /*MxOp*/4);
1594 case Hexagon::PS_loadrh_pci:
1595 return RealCirc(Hexagon::L2_loadrh_pci, /*HasImm*/true, /*MxOp*/4);
1596 case Hexagon::PS_loadri_pci:
1597 return RealCirc(Hexagon::L2_loadri_pci, /*HasImm*/true, /*MxOp*/4);
1598 case Hexagon::PS_loadrd_pci:
1599 return RealCirc(Hexagon::L2_loadrd_pci, /*HasImm*/true, /*MxOp*/4);
1600 case Hexagon::PS_loadrub_pcr:
1601 return RealCirc(Hexagon::L2_loadrub_pcr, /*HasImm*/false, /*MxOp*/3);
1602 case Hexagon::PS_loadrb_pcr:
1603 return RealCirc(Hexagon::L2_loadrb_pcr, /*HasImm*/false, /*MxOp*/3);
1604 case Hexagon::PS_loadruh_pcr:
1605 return RealCirc(Hexagon::L2_loadruh_pcr, /*HasImm*/false, /*MxOp*/3);
1606 case Hexagon::PS_loadrh_pcr:
1607 return RealCirc(Hexagon::L2_loadrh_pcr, /*HasImm*/false, /*MxOp*/3);
1608 case Hexagon::PS_loadri_pcr:
1609 return RealCirc(Hexagon::L2_loadri_pcr, /*HasImm*/false, /*MxOp*/3);
1610 case Hexagon::PS_loadrd_pcr:
1611 return RealCirc(Hexagon::L2_loadrd_pcr, /*HasImm*/false, /*MxOp*/3);
1612 case Hexagon::PS_storerb_pci:
1613 return RealCirc(Hexagon::S2_storerb_pci, /*HasImm*/true, /*MxOp*/3);
1614 case Hexagon::PS_storerh_pci:
1615 return RealCirc(Hexagon::S2_storerh_pci, /*HasImm*/true, /*MxOp*/3);
1616 case Hexagon::PS_storerf_pci:
1617 return RealCirc(Hexagon::S2_storerf_pci, /*HasImm*/true, /*MxOp*/3);
1618 case Hexagon::PS_storeri_pci:
1619 return RealCirc(Hexagon::S2_storeri_pci, /*HasImm*/true, /*MxOp*/3);
1620 case Hexagon::PS_storerd_pci:
1621 return RealCirc(Hexagon::S2_storerd_pci, /*HasImm*/true, /*MxOp*/3);
1622 case Hexagon::PS_storerb_pcr:
1623 return RealCirc(Hexagon::S2_storerb_pcr, /*HasImm*/false, /*MxOp*/2);
1624 case Hexagon::PS_storerh_pcr:
1625 return RealCirc(Hexagon::S2_storerh_pcr, /*HasImm*/false, /*MxOp*/2);
1626 case Hexagon::PS_storerf_pcr:
1627 return RealCirc(Hexagon::S2_storerf_pcr, /*HasImm*/false, /*MxOp*/2);
1628 case Hexagon::PS_storeri_pcr:
1629 return RealCirc(Hexagon::S2_storeri_pcr, /*HasImm*/false, /*MxOp*/2);
1630 case Hexagon::PS_storerd_pcr:
1631 return RealCirc(Hexagon::S2_storerd_pcr, /*HasImm*/false, /*MxOp*/2);
1632 }
1633
1634 return false;
1635}
1636
1639 MachineBasicBlock &MBB = *MI.getParent();
1640 const DebugLoc &DL = MI.getDebugLoc();
1641 unsigned Opc = MI.getOpcode();
1643
1644 switch (Opc) {
1645 case Hexagon::V6_vgather_vscatter_mh_pseudo:
1646 // This is mainly a place holder. It will be extended.
1647 First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermh))
1648 .add(MI.getOperand(2))
1649 .add(MI.getOperand(3))
1650 .add(MI.getOperand(4));
1651 BuildMI(MBB, MI, DL, get(Hexagon::V6_vscattermh))
1652 .add(MI.getOperand(2))
1653 .add(MI.getOperand(3))
1654 .add(MI.getOperand(4))
1655 .addReg(Hexagon::VTMP);
1656 MBB.erase(MI);
1657 return First.getInstrIterator();
1658 case Hexagon::V6_vgathermh_pseudo:
1659 First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermh))
1660 .add(MI.getOperand(2))
1661 .add(MI.getOperand(3))
1662 .add(MI.getOperand(4));
1663 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1664 .add(MI.getOperand(0))
1665 .addImm(MI.getOperand(1).getImm())
1666 .addReg(Hexagon::VTMP);
1667 MBB.erase(MI);
1668 return First.getInstrIterator();
1669
1670 case Hexagon::V6_vgathermw_pseudo:
1671 First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermw))
1672 .add(MI.getOperand(2))
1673 .add(MI.getOperand(3))
1674 .add(MI.getOperand(4));
1675 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1676 .add(MI.getOperand(0))
1677 .addImm(MI.getOperand(1).getImm())
1678 .addReg(Hexagon::VTMP);
1679 MBB.erase(MI);
1680 return First.getInstrIterator();
1681
1682 case Hexagon::V6_vgathermhw_pseudo:
1683 First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhw))
1684 .add(MI.getOperand(2))
1685 .add(MI.getOperand(3))
1686 .add(MI.getOperand(4));
1687 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1688 .add(MI.getOperand(0))
1689 .addImm(MI.getOperand(1).getImm())
1690 .addReg(Hexagon::VTMP);
1691 MBB.erase(MI);
1692 return First.getInstrIterator();
1693
1694 case Hexagon::V6_vgathermhq_pseudo:
1695 First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhq))
1696 .add(MI.getOperand(2))
1697 .add(MI.getOperand(3))
1698 .add(MI.getOperand(4))
1699 .add(MI.getOperand(5));
1700 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1701 .add(MI.getOperand(0))
1702 .addImm(MI.getOperand(1).getImm())
1703 .addReg(Hexagon::VTMP);
1704 MBB.erase(MI);
1705 return First.getInstrIterator();
1706
1707 case Hexagon::V6_vgathermwq_pseudo:
1708 First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermwq))
1709 .add(MI.getOperand(2))
1710 .add(MI.getOperand(3))
1711 .add(MI.getOperand(4))
1712 .add(MI.getOperand(5));
1713 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1714 .add(MI.getOperand(0))
1715 .addImm(MI.getOperand(1).getImm())
1716 .addReg(Hexagon::VTMP);
1717 MBB.erase(MI);
1718 return First.getInstrIterator();
1719
1720 case Hexagon::V6_vgathermhwq_pseudo:
1721 First = BuildMI(MBB, MI, DL, get(Hexagon::V6_vgathermhwq))
1722 .add(MI.getOperand(2))
1723 .add(MI.getOperand(3))
1724 .add(MI.getOperand(4))
1725 .add(MI.getOperand(5));
1726 BuildMI(MBB, MI, DL, get(Hexagon::V6_vS32b_new_ai))
1727 .add(MI.getOperand(0))
1728 .addImm(MI.getOperand(1).getImm())
1729 .addReg(Hexagon::VTMP);
1730 MBB.erase(MI);
1731 return First.getInstrIterator();
1732 }
1733
1734 return MI.getIterator();
1735}
1736
1737// We indicate that we want to reverse the branch by
1738// inserting the reversed branching opcode.
1741 if (Cond.empty())
1742 return true;
1743 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1744 unsigned opcode = Cond[0].getImm();
1745 //unsigned temp;
1746 assert(get(opcode).isBranch() && "Should be a branching condition.");
1747 if (isEndLoopN(opcode))
1748 return true;
1749 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1750 Cond[0].setImm(NewOpcode);
1751 return false;
1752}
1753
1759
1763
1765 unsigned BasePos, OffsetPos;
1766 if (!getBaseAndOffsetPosition(MI, BasePos, OffsetPos))
1767 return false;
1768 return isPostIncrement(MI) && MI.getOperand(OffsetPos).isImm();
1769}
1770
1771// Returns true if an instruction is predicated irrespective of the predicate
1772// sense. For example, all of the following will return true.
1773// if (p0) R1 = add(R2, R3)
1774// if (!p0) R1 = add(R2, R3)
1775// if (p0.new) R1 = add(R2, R3)
1776// if (!p0.new) R1 = add(R2, R3)
1777// Note: New-value stores are not included here as in the current
1778// implementation, we don't need to check their predicate sense.
1780 const uint64_t F = MI.getDesc().TSFlags;
1782}
1783
1786 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1787 isEndLoopN(Cond[0].getImm())) {
1788 LLVM_DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
1789 return false;
1790 }
1791 int Opc = MI.getOpcode();
1792 assert (isPredicable(MI) && "Expected predicable instruction");
1793 bool invertJump = predOpcodeHasNot(Cond);
1794
1795 // We have to predicate MI "in place", i.e. after this function returns,
1796 // MI will need to be transformed into a predicated form. To avoid com-
1797 // plicated manipulations with the operands (handling tied operands,
1798 // etc.), build a new temporary instruction, then overwrite MI with it.
1799
1800 MachineBasicBlock &B = *MI.getParent();
1801 DebugLoc DL = MI.getDebugLoc();
1802 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1803 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
1804 unsigned NOp = 0, NumOps = MI.getNumOperands();
1805 while (NOp < NumOps) {
1806 MachineOperand &Op = MI.getOperand(NOp);
1807 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1808 break;
1809 T.add(Op);
1810 NOp++;
1811 }
1812
1813 Register PredReg;
1814 unsigned PredRegPos;
1815 RegState PredRegFlags = {};
1816 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1817 (void)GotPredReg;
1818 assert(GotPredReg);
1819 T.addReg(PredReg, PredRegFlags);
1820 while (NOp < NumOps)
1821 T.add(MI.getOperand(NOp++));
1822
1823 MI.setDesc(get(PredOpc));
1824 while (unsigned n = MI.getNumOperands())
1825 MI.removeOperand(n-1);
1826 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
1827 MI.addOperand(T->getOperand(i));
1828
1829 MachineBasicBlock::instr_iterator TI = T->getIterator();
1830 B.erase(TI);
1831
1832 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1833 MRI.clearKillFlags(PredReg);
1834 return true;
1835}
1836
1838 ArrayRef<MachineOperand> Pred2) const {
1839 // TODO: Fix this
1840 return false;
1841}
1842
1844 std::vector<MachineOperand> &Pred,
1845 bool SkipDead) const {
1846 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
1847
1848 for (const MachineOperand &MO : MI.operands()) {
1849 if (MO.isReg()) {
1850 if (!MO.isDef())
1851 continue;
1852 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1853 if (RC == &Hexagon::PredRegsRegClass) {
1854 Pred.push_back(MO);
1855 return true;
1856 }
1857 continue;
1858 } else if (MO.isRegMask()) {
1859 for (Register PR : Hexagon::PredRegsRegClass) {
1860 if (!MI.modifiesRegister(PR, &HRI))
1861 continue;
1862 Pred.push_back(MO);
1863 return true;
1864 }
1865 }
1866 }
1867 return false;
1868}
1869
1871 if (!MI.getDesc().isPredicable())
1872 return false;
1873
1874 if (MI.isCall() || isTailCall(MI)) {
1875 if (!Subtarget.usePredicatedCalls())
1876 return false;
1877 }
1878
1879 // HVX loads are not predicable on v60, but are on v62.
1880 if (!Subtarget.hasV62Ops()) {
1881 switch (MI.getOpcode()) {
1882 case Hexagon::V6_vL32b_ai:
1883 case Hexagon::V6_vL32b_pi:
1884 case Hexagon::V6_vL32b_ppu:
1885 case Hexagon::V6_vL32b_cur_ai:
1886 case Hexagon::V6_vL32b_cur_pi:
1887 case Hexagon::V6_vL32b_cur_ppu:
1888 case Hexagon::V6_vL32b_nt_ai:
1889 case Hexagon::V6_vL32b_nt_pi:
1890 case Hexagon::V6_vL32b_nt_ppu:
1891 case Hexagon::V6_vL32b_tmp_ai:
1892 case Hexagon::V6_vL32b_tmp_pi:
1893 case Hexagon::V6_vL32b_tmp_ppu:
1894 case Hexagon::V6_vL32b_nt_cur_ai:
1895 case Hexagon::V6_vL32b_nt_cur_pi:
1896 case Hexagon::V6_vL32b_nt_cur_ppu:
1897 case Hexagon::V6_vL32b_nt_tmp_ai:
1898 case Hexagon::V6_vL32b_nt_tmp_pi:
1899 case Hexagon::V6_vL32b_nt_tmp_ppu:
1900 return false;
1901 }
1902 }
1903 return true;
1904}
1905
1907 bool Invert) const {
1908 if (Invert)
1909 return false;
1910
1911 switch (Inst.getOpcode()) {
1912 // TODO: Add more instructions to be handled by MachineCombiner.
1913 case Hexagon::F2_sfadd:
1915 default:
1916 return false;
1917 }
1918}
1919
1921 const MachineBasicBlock *MBB,
1922 const MachineFunction &MF) const {
1923 // Debug info is never a scheduling boundary. It's necessary to be explicit
1924 // due to the special treatment of IT instructions below, otherwise a
1925 // dbg_value followed by an IT will result in the IT instruction being
1926 // considered a scheduling hazard, which is wrong. It should be the actual
1927 // instruction preceding the dbg_value instruction(s), just like it is
1928 // when debug info is not present.
1929 if (MI.isDebugInstr())
1930 return false;
1931
1932 // Throwing call is a boundary.
1933 if (MI.isCall()) {
1934 // Don't mess around with no return calls.
1935 if (doesNotReturn(MI))
1936 return true;
1937 // If any of the block's successors is a landing pad, this could be a
1938 // throwing call.
1939 for (auto *I : MBB->successors())
1940 if (I->isEHPad())
1941 return true;
1942 }
1943
1944 // Terminators and labels can't be scheduled around.
1945 if (MI.getDesc().isTerminator() || MI.isPosition())
1946 return true;
1947
1948 // INLINEASM_BR can jump to another block
1949 if (MI.getOpcode() == TargetOpcode::INLINEASM_BR)
1950 return true;
1951
1952 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1953 return true;
1954
1955 return false;
1956}
1957
1958/// Measure the specified inline asm to determine an approximation of its
1959/// length.
1960/// Comments (which run till the next SeparatorString or newline) do not
1961/// count as an instruction.
1962/// Any other non-whitespace text is considered an instruction, with
1963/// multiple instructions separated by SeparatorString or newlines.
1964/// Variable-length instructions are not handled here; this function
1965/// may be overloaded in the target code to do that.
1966/// Hexagon counts the number of ##'s and adjust for that many
1967/// constant exenders.
1969 const MCAsmInfo &MAI,
1970 const TargetSubtargetInfo *STI) const {
1971 StringRef AStr(Str);
1972 // Count the number of instructions in the asm.
1973 bool atInsnStart = true;
1974 unsigned Length = 0;
1975 const unsigned MaxInstLength = MAI.getMaxInstLength(STI);
1976 for (; *Str; ++Str) {
1977 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1978 strlen(MAI.getSeparatorString())) == 0)
1979 atInsnStart = true;
1980 if (atInsnStart && !isSpace(static_cast<unsigned char>(*Str))) {
1981 Length += MaxInstLength;
1982 atInsnStart = false;
1983 }
1984 if (atInsnStart && strncmp(Str, MAI.getCommentString().data(),
1985 MAI.getCommentString().size()) == 0)
1986 atInsnStart = false;
1987 }
1988
1989 // Add to size number of constant extenders seen * 4.
1990 StringRef Occ("##");
1991 Length += AStr.count(Occ)*4;
1992 return Length;
1993}
1994
2002
2003/// For a comparison instruction, return the source registers in
2004/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
2005/// compares against in CmpValue. Return true if the comparison instruction
2006/// can be analyzed.
2008 Register &SrcReg2, int64_t &Mask,
2009 int64_t &Value) const {
2010 unsigned Opc = MI.getOpcode();
2011
2012 // Set mask and the first source register.
2013 switch (Opc) {
2014 case Hexagon::C2_cmpeq:
2015 case Hexagon::C2_cmpeqp:
2016 case Hexagon::C2_cmpgt:
2017 case Hexagon::C2_cmpgtp:
2018 case Hexagon::C2_cmpgtu:
2019 case Hexagon::C2_cmpgtup:
2020 case Hexagon::C4_cmpneq:
2021 case Hexagon::C4_cmplte:
2022 case Hexagon::C4_cmplteu:
2023 case Hexagon::C2_cmpeqi:
2024 case Hexagon::C2_cmpgti:
2025 case Hexagon::C2_cmpgtui:
2026 case Hexagon::C4_cmpneqi:
2027 case Hexagon::C4_cmplteui:
2028 case Hexagon::C4_cmpltei:
2029 SrcReg = MI.getOperand(1).getReg();
2030 Mask = ~0;
2031 break;
2032 case Hexagon::A4_cmpbeq:
2033 case Hexagon::A4_cmpbgt:
2034 case Hexagon::A4_cmpbgtu:
2035 case Hexagon::A4_cmpbeqi:
2036 case Hexagon::A4_cmpbgti:
2037 case Hexagon::A4_cmpbgtui:
2038 SrcReg = MI.getOperand(1).getReg();
2039 Mask = 0xFF;
2040 break;
2041 case Hexagon::A4_cmpheq:
2042 case Hexagon::A4_cmphgt:
2043 case Hexagon::A4_cmphgtu:
2044 case Hexagon::A4_cmpheqi:
2045 case Hexagon::A4_cmphgti:
2046 case Hexagon::A4_cmphgtui:
2047 SrcReg = MI.getOperand(1).getReg();
2048 Mask = 0xFFFF;
2049 break;
2050 }
2051
2052 // Set the value/second source register.
2053 switch (Opc) {
2054 case Hexagon::C2_cmpeq:
2055 case Hexagon::C2_cmpeqp:
2056 case Hexagon::C2_cmpgt:
2057 case Hexagon::C2_cmpgtp:
2058 case Hexagon::C2_cmpgtu:
2059 case Hexagon::C2_cmpgtup:
2060 case Hexagon::A4_cmpbeq:
2061 case Hexagon::A4_cmpbgt:
2062 case Hexagon::A4_cmpbgtu:
2063 case Hexagon::A4_cmpheq:
2064 case Hexagon::A4_cmphgt:
2065 case Hexagon::A4_cmphgtu:
2066 case Hexagon::C4_cmpneq:
2067 case Hexagon::C4_cmplte:
2068 case Hexagon::C4_cmplteu:
2069 SrcReg2 = MI.getOperand(2).getReg();
2070 Value = 0;
2071 return true;
2072
2073 case Hexagon::C2_cmpeqi:
2074 case Hexagon::C2_cmpgtui:
2075 case Hexagon::C2_cmpgti:
2076 case Hexagon::C4_cmpneqi:
2077 case Hexagon::C4_cmplteui:
2078 case Hexagon::C4_cmpltei:
2079 case Hexagon::A4_cmpbeqi:
2080 case Hexagon::A4_cmpbgti:
2081 case Hexagon::A4_cmpbgtui:
2082 case Hexagon::A4_cmpheqi:
2083 case Hexagon::A4_cmphgti:
2084 case Hexagon::A4_cmphgtui: {
2085 SrcReg2 = 0;
2086 const MachineOperand &Op2 = MI.getOperand(2);
2087 if (!Op2.isImm())
2088 return false;
2089 Value = MI.getOperand(2).getImm();
2090 return true;
2091 }
2092 }
2093
2094 return false;
2095}
2096
2098 const MachineInstr &MI,
2099 unsigned *PredCost) const {
2100 return getInstrTimingClassLatency(ItinData, MI);
2101}
2102
2104 const TargetSubtargetInfo &STI) const {
2106 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
2107}
2108
2109// Inspired by this pair:
2110// %r13 = L2_loadri_io %r29, 136; mem:LD4[FixedStack0]
2111// S2_storeri_io %r29, 132, killed %r1; flags: mem:ST4[FixedStack1]
2112// Currently AA considers the addresses in these instructions to be aliasing.
2114 const MachineInstr &MIa, const MachineInstr &MIb) const {
2117 return false;
2118
2119 // Instructions that are pure loads, not loads and stores like memops are not
2120 // dependent.
2121 if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
2122 return true;
2123
2124 // Get the base register in MIa.
2125 unsigned BasePosA, OffsetPosA;
2126 if (!getBaseAndOffsetPosition(MIa, BasePosA, OffsetPosA))
2127 return false;
2128 const MachineOperand &BaseA = MIa.getOperand(BasePosA);
2129 Register BaseRegA = BaseA.getReg();
2130 unsigned BaseSubA = BaseA.getSubReg();
2131
2132 // Get the base register in MIb.
2133 unsigned BasePosB, OffsetPosB;
2134 if (!getBaseAndOffsetPosition(MIb, BasePosB, OffsetPosB))
2135 return false;
2136 const MachineOperand &BaseB = MIb.getOperand(BasePosB);
2137 Register BaseRegB = BaseB.getReg();
2138 unsigned BaseSubB = BaseB.getSubReg();
2139
2140 if (BaseRegA != BaseRegB || BaseSubA != BaseSubB)
2141 return false;
2142
2143 // Get the access sizes.
2144 unsigned SizeA = getMemAccessSize(MIa);
2145 unsigned SizeB = getMemAccessSize(MIb);
2146
2147 // Get the offsets. Handle immediates only for now.
2148 const MachineOperand &OffA = MIa.getOperand(OffsetPosA);
2149 const MachineOperand &OffB = MIb.getOperand(OffsetPosB);
2150 if (!MIa.getOperand(OffsetPosA).isImm() ||
2151 !MIb.getOperand(OffsetPosB).isImm())
2152 return false;
2153 int OffsetA = isPostIncrement(MIa) ? 0 : OffA.getImm();
2154 int OffsetB = isPostIncrement(MIb) ? 0 : OffB.getImm();
2155
2156 // This is a mem access with the same base register and known offsets from it.
2157 // Reason about it.
2158 if (OffsetA > OffsetB) {
2159 uint64_t OffDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
2160 return SizeB <= OffDiff;
2161 }
2162 if (OffsetA < OffsetB) {
2163 uint64_t OffDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
2164 return SizeA <= OffDiff;
2165 }
2166
2167 return false;
2168}
2169
2170// This MI might have either incomplete info, or known to be unsafe
2171// to deal with (i.e. volatile object).
2173 if (!MI || MI->memoperands_empty())
2174 return true;
2175
2176 if ((*MI->memoperands_begin())->isVolatile() || MI->hasUnmodeledSideEffects())
2177 return true;
2178
2179 if (!(*MI->memoperands_begin())->getValue())
2180 return true;
2181
2182 return false;
2183}
2184
2185/// This returns true if the two MIs could be memory dependent.
2187 const MachineInstr *MIa,
2188 const MachineInstr *MIb) const {
2189 // Cover a trivial case - no edge is need to itself.
2190 if (MIa == MIb)
2191 return false;
2192
2193 if (areMemAccessesTriviallyDisjoint(*MIa, *MIb))
2194 return false;
2195
2197 return true;
2198
2199 // If we are dealing with two "normal" loads, we do not need an edge
2200 // between them - they could be reordered.
2201 if (!MIa->mayStore() && !MIb->mayStore())
2202 return false;
2203
2204 // To this point analysis is generic. From here on we do need AA.
2205 if (!AA)
2206 return true;
2207
2208 MachineMemOperand *MMOa = *MIa->memoperands_begin();
2209 MachineMemOperand *MMOb = *MIb->memoperands_begin();
2210
2211 // TODO: Need to handle multiple memory operands.
2212 // if either instruction has more than one memory operand, punt.
2213 if (!(MIa->hasOneMemOperand() && MIb->hasOneMemOperand()))
2214 return true;
2215
2216 if (!MMOa->getSize().hasValue() || !MMOb->getSize().hasValue())
2217 return true;
2218
2219 assert((MMOa->getOffset() >= 0) && "Negative MachineMemOperand offset");
2220 assert((MMOb->getOffset() >= 0) && "Negative MachineMemOperand offset");
2221 assert((MMOa->getSize().hasValue() && MMOb->getSize().hasValue()) &&
2222 "Size 0 memory access");
2223
2224 // If the base address of the two memoperands is the same. For instance,
2225 // x and x+4, then we can easily reason about them using the offset and size
2226 // of access.
2227 if (MMOa->getValue() == MMOb->getValue()) {
2228 if (MMOa->getOffset() > MMOb->getOffset()) {
2229 uint64_t offDiff = MMOa->getOffset() - MMOb->getOffset();
2230 return !(MMOb->getSize().getValue() <= offDiff);
2231 } else if (MMOa->getOffset() < MMOb->getOffset()) {
2232 uint64_t offDiff = MMOb->getOffset() - MMOa->getOffset();
2233 return !(MMOa->getSize().getValue() <= offDiff);
2234 }
2235 // MMOa->getOffset() == MMOb->getOffset()
2236 return true;
2237 }
2238
2239 int64_t MinOffset = std::min(MMOa->getOffset(), MMOb->getOffset());
2240 int64_t Overlapa = MMOa->getSize().getValue() + MMOa->getOffset() - MinOffset;
2241 int64_t Overlapb = MMOb->getSize().getValue() + MMOb->getOffset() - MinOffset;
2242
2243 AliasResult AAResult =
2244 AA->alias(MemoryLocation(MMOa->getValue(), Overlapa, MMOa->getAAInfo()),
2245 MemoryLocation(MMOb->getValue(), Overlapb, MMOb->getAAInfo()));
2246
2247 return (AAResult != AliasResult::NoAlias);
2248}
2249
2250/// If the instruction is an increment of a constant value, return the amount.
2252 int &Value) const {
2253 if (isPostIncrement(MI)) {
2254 unsigned BasePos = 0, OffsetPos = 0;
2255 if (!getBaseAndOffsetPosition(MI, BasePos, OffsetPos))
2256 return false;
2257 const MachineOperand &OffsetOp = MI.getOperand(OffsetPos);
2258 if (OffsetOp.isImm()) {
2259 Value = OffsetOp.getImm();
2260 return true;
2261 }
2262 } else if (MI.getOpcode() == Hexagon::A2_addi) {
2263 const MachineOperand &AddOp = MI.getOperand(2);
2264 if (AddOp.isImm()) {
2265 Value = AddOp.getImm();
2266 return true;
2267 }
2268 }
2269
2270 return false;
2271}
2272
2273std::pair<unsigned, unsigned>
2275 return std::make_pair(TF & ~HexagonII::MO_Bitmasks,
2277}
2278
2281 using namespace HexagonII;
2282
2283 static const std::pair<unsigned, const char*> Flags[] = {
2284 {MO_PCREL, "hexagon-pcrel"},
2285 {MO_GOT, "hexagon-got"},
2286 {MO_LO16, "hexagon-lo16"},
2287 {MO_HI16, "hexagon-hi16"},
2288 {MO_GPREL, "hexagon-gprel"},
2289 {MO_GDGOT, "hexagon-gdgot"},
2290 {MO_GDPLT, "hexagon-gdplt"},
2291 {MO_IE, "hexagon-ie"},
2292 {MO_IEGOT, "hexagon-iegot"},
2293 {MO_TPREL, "hexagon-tprel"}
2294 };
2295 return ArrayRef(Flags);
2296}
2297
2300 using namespace HexagonII;
2301
2302 static const std::pair<unsigned, const char*> Flags[] = {
2303 {HMOTF_ConstExtended, "hexagon-ext"}
2304 };
2305 return ArrayRef(Flags);
2306}
2307
2310 const TargetRegisterClass *TRC;
2311 if (VT == MVT::i1) {
2312 TRC = &Hexagon::PredRegsRegClass;
2313 } else if (VT == MVT::i32 || VT == MVT::f32) {
2314 TRC = &Hexagon::IntRegsRegClass;
2315 } else if (VT == MVT::i64 || VT == MVT::f64) {
2316 TRC = &Hexagon::DoubleRegsRegClass;
2317 } else {
2318 llvm_unreachable("Cannot handle this register class");
2319 }
2320
2321 Register NewReg = MRI.createVirtualRegister(TRC);
2322 return NewReg;
2323}
2324
2328
2330 const uint64_t F = MI.getDesc().TSFlags;
2332}
2333
2337
2339 return !isTC1(MI) && !isTC2Early(MI) && !MI.getDesc().mayLoad() &&
2340 !MI.getDesc().mayStore() &&
2341 MI.getDesc().getOpcode() != Hexagon::S2_allocframe &&
2342 MI.getDesc().getOpcode() != Hexagon::L2_deallocframe &&
2343 !isMemOp(MI) && !MI.isBranch() && !MI.isReturn() && !MI.isCall();
2344}
2345
2346// Return true if the instruction is a compound branch instruction.
2348 return getType(MI) == HexagonII::TypeCJ && MI.isBranch();
2349}
2350
2351// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
2352// isFPImm and later getFPImm as well.
2354 const uint64_t F = MI.getDesc().TSFlags;
2356 if (isExtended) // Instruction must be extended.
2357 return true;
2358
2359 unsigned isExtendable =
2361 if (!isExtendable)
2362 return false;
2363
2364 if (MI.isCall())
2365 return false;
2366
2367 short ExtOpNum = getCExtOpNum(MI);
2368 const MachineOperand &MO = MI.getOperand(ExtOpNum);
2369 // Use MO operand flags to determine if MO
2370 // has the HMOTF_ConstExtended flag set.
2372 return true;
2373 // If this is a Machine BB address we are talking about, and it is
2374 // not marked as extended, say so.
2375 if (MO.isMBB())
2376 return false;
2377
2378 // We could be using an instruction with an extendable immediate and shoehorn
2379 // a global address into it. If it is a global address it will be constant
2380 // extended. We do this for COMBINE.
2381 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
2382 MO.isJTI() || MO.isCPI() || MO.isFPImm())
2383 return true;
2384
2385 // If the extendable operand is not 'Immediate' type, the instruction should
2386 // have 'isExtended' flag set.
2387 assert(MO.isImm() && "Extendable operand must be Immediate type");
2388
2389 int64_t Value = MO.getImm();
2391 int32_t SValue = Value;
2392 int32_t MinValue = getMinValue(MI);
2393 int32_t MaxValue = getMaxValue(MI);
2394 return SValue < MinValue || SValue > MaxValue;
2395 }
2396 uint32_t UValue = Value;
2397 uint32_t MinValue = getMinValue(MI);
2398 uint32_t MaxValue = getMaxValue(MI);
2399 return UValue < MinValue || UValue > MaxValue;
2400}
2401
2403 switch (MI.getOpcode()) {
2404 case Hexagon::L4_return:
2405 case Hexagon::L4_return_t:
2406 case Hexagon::L4_return_f:
2407 case Hexagon::L4_return_tnew_pnt:
2408 case Hexagon::L4_return_fnew_pnt:
2409 case Hexagon::L4_return_tnew_pt:
2410 case Hexagon::L4_return_fnew_pt:
2411 return true;
2412 }
2413 return false;
2414}
2415
2416// Return true when ConsMI uses a register defined by ProdMI.
2418 const MachineInstr &ConsMI) const {
2419 if (!ProdMI.getDesc().getNumDefs())
2420 return false;
2421 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
2422
2427
2428 parseOperands(ProdMI, DefsA, UsesA);
2429 parseOperands(ConsMI, DefsB, UsesB);
2430
2431 for (auto &RegA : DefsA)
2432 for (auto &RegB : UsesB) {
2433 // True data dependency.
2434 if (RegA == RegB)
2435 return true;
2436
2437 if (RegA.isPhysical() && llvm::is_contained(HRI.subregs(RegA), RegB))
2438 return true;
2439
2440 if (RegB.isPhysical() && llvm::is_contained(HRI.subregs(RegB), RegA))
2441 return true;
2442 }
2443
2444 return false;
2445}
2446
2447// Returns true if the instruction is already a .cur.
2449 // TODO: Use an attribute instead of switch case of opcodes.
2450 // This would be more maintainable and allow TableGen to generate the list.
2451 switch (MI.getOpcode()) {
2452 // HVX vector load instructions with .cur suffix.
2453 // Variants include:
2454 // - Base addressing modes: pi (post-increment), ai (absolute immediate),
2455 // ppu (post-increment by register)
2456 // - Predicated versions: pred (if true), npred (if false)
2457 // - Non-temporal hint: nt (non-temporal)
2458 // Note: Does not include pseudo instructions.
2459 case Hexagon::V6_vL32b_cur_pi:
2460 case Hexagon::V6_vL32b_nt_cur_pi:
2461 case Hexagon::V6_vL32b_cur_pred_pi:
2462 case Hexagon::V6_vL32b_nt_cur_pred_pi:
2463 case Hexagon::V6_vL32b_cur_npred_pi:
2464 case Hexagon::V6_vL32b_nt_cur_npred_pi:
2465 case Hexagon::V6_vL32b_cur_ai:
2466 case Hexagon::V6_vL32b_nt_cur_ai:
2467 case Hexagon::V6_vL32b_cur_pred_ai:
2468 case Hexagon::V6_vL32b_nt_cur_pred_ai:
2469 case Hexagon::V6_vL32b_cur_npred_ai:
2470 case Hexagon::V6_vL32b_nt_cur_npred_ai:
2471 case Hexagon::V6_vL32b_cur_ppu:
2472 case Hexagon::V6_vL32b_nt_cur_ppu:
2473 case Hexagon::V6_vL32b_cur_pred_ppu:
2474 case Hexagon::V6_vL32b_nt_cur_pred_ppu:
2475 case Hexagon::V6_vL32b_cur_npred_ppu:
2476 case Hexagon::V6_vL32b_nt_cur_npred_ppu:
2477 return true;
2478 }
2479 return false;
2480}
2481
2482/// Should be able to tell if the operand in question is .new.
2483/// Instead of exhaustive case statement (which would be prohibitively large)
2484/// we use a set of rules.
2486 unsigned OpIndex) const {
2487 // If it is not MI that has "any" .new, return false.
2488 if (!isDotNewInst(MI))
2489 return false;
2490
2491 const MachineOperand &MO = MI.getOperand(OpIndex);
2492 // If it is a def, or not a register, it could not be .new.
2493 if (MO.isDef() || !MO.isReg())
2494 return false;
2495
2496 // If it is a predicated instruction, it reduces options on
2497 // what could be a .new operand.
2498 if (isPredicated(MI)) {
2499 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
2500 if (HRI.isFakeReg(MO.getReg()))
2501 return false;
2502 else {
2503 const TargetRegisterClass *RC = HRI.getMinimalPhysRegClass(MO.getReg());
2504 return (RC == &Hexagon::PredRegsRegClass);
2505 }
2506 }
2507
2508 // For a NVJ by definition, operand with .new must be an internal read.
2509 if (isNewValueJump(MI)) {
2510 if (MO.isReg() && MO.isInternalRead())
2511 return true;
2512 else
2513 return false;
2514 }
2515
2516 // Each new value store has different "position"
2517 // of the .new operand. But it always should be
2518 // an internal read.
2519 // S2_storerinew_io %R29, 28, %R1<internal>
2520 // Use: [0] is.new(0) reg(%R29)
2521 // Use: [2] is.new(1) reg(%R1)
2522 if (isNewValueStore(MI)) {
2523 if (MO.isInternalRead())
2524 return true;
2525 else
2526 return false;
2527 }
2528
2529 llvm_unreachable("Unhandled operand.");
2530 return false;
2531}
2532
2533// Returns true, if any of the operands of the instruction,
2534// is a dot tmp register
2538
2539/// Returns true if the specified operand is a .tmp (temporary) operand.
2540/// .tmp operands represent temporary values that are used in the same packet.
2541/// This function checks:
2542/// 1. The instruction is a .tmp instruction (isDotTmpInst)
2543/// 2. The operand is a def register
2544/// 3. The operand is not tied (excludes post-increment base registers)
2546 unsigned OpIndex) const {
2547 // If it is not MI that has "any" .tmp, return false.
2548 if (!isDotTmpInst(MI))
2549 return false;
2550
2551 const MachineOperand &MO = MI.getOperand(OpIndex);
2552
2553 // .tmp only applies to def registers.
2554 // Also, make sure that MO is not a tied (post-inc) operand
2555 if (MO.isDef() && MO.isReg() && !MO.isTied())
2556 return true;
2557 return false;
2558}
2559
2560/// Returns true if the specified operand is a .cur (current) operand.
2561/// .cur operands can be used immediately in the same packet.
2562/// This function checks:
2563/// 1. The instruction is a .cur instruction (isDotCurInst)
2564/// 2. The operand is a def register
2565/// 3. The operand is not tied (excludes post-increment base registers)
2567 unsigned OpIndex) const {
2568 // If it is not MI that has "any" .cur, return false.
2569 if (!isDotCurInst(MI))
2570 return false;
2571
2572 const MachineOperand &MO = MI.getOperand(OpIndex);
2573
2574 // .cur only applies to def registers.
2575 // Also, make sure that MO is not a tied (post-inc) operand
2576 if (MO.isDef() && MO.isReg() && !MO.isTied())
2577 return true;
2578 return false;
2579}
2580
2582 unsigned DefOpIdx,
2583 const MachineInstr &UseMI,
2584 unsigned UseOpIdx, bool IsPullDown,
2585 bool DefViaRegMask) const {
2586 // Push-down needs in-packet forwarding (.cur/.new) so the use sees the new
2587 // value; that analysis is not implemented, so reject it. Only pull-up today.
2588 if (IsPullDown)
2589 return false;
2590 if (!DefViaRegMask) {
2591 // A forwarding .cur/.tmp def exposes its value in-packet, making the use
2592 // read the new value, a disguised RAW, not a safe WAR. (A regmask clobber
2593 // never forwards, so these checks are skipped for it.)
2594 if (!DefMI.getOperand(DefOpIdx).isReg())
2595 return false;
2596 if (isDotCurOperand(DefMI, DefOpIdx))
2597 return false;
2598 if (isDotTmpOperand(DefMI, DefOpIdx))
2599 return false;
2600 }
2601 if (isDotNewOperand(UseMI, UseOpIdx))
2602 return false;
2603 if (UseMI.isTerminator())
2604 return false;
2605 return true;
2606}
2607
2608// Returns true, if any one of the operands is a dot new
2609// insn, whether it is predicated dot new or register dot new.
2612 return true;
2613
2614 return false;
2615}
2616
2617/// Symmetrical. See if these two instructions are fit for duplex pair.
2619 const MachineInstr &MIb) const {
2622 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
2623}
2624
2628
2632
2634 const MachineInstr &MIb) const {
2635 return (isHMXActivateInstr(MIa) && isHMXWeightInstr(MIb)) ||
2636 (isHMXWeightInstr(MIa) && isHMXActivateInstr(MIb));
2637}
2638
2639bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
2640 return (Opcode == Hexagon::ENDLOOP0 ||
2641 Opcode == Hexagon::ENDLOOP1);
2642}
2643
2644bool HexagonInstrInfo::isExpr(unsigned OpType) const {
2645 switch(OpType) {
2652 return true;
2653 default:
2654 return false;
2655 }
2656}
2657
2659 const MCInstrDesc &MID = MI.getDesc();
2660 const uint64_t F = MID.TSFlags;
2662 return true;
2663
2664 // TODO: This is largely obsolete now. Will need to be removed
2665 // in consecutive patches.
2666 switch (MI.getOpcode()) {
2667 // PS_fi and PS_fia remain special cases.
2668 case Hexagon::PS_fi:
2669 case Hexagon::PS_fia:
2670 return true;
2671 default:
2672 return false;
2673 }
2674 return false;
2675}
2676
2677// This returns true in two cases:
2678// - The OP code itself indicates that this is an extended instruction.
2679// - One of MOs has been marked with HMOTF_ConstExtended flag.
2681 // First check if this is permanently extended op code.
2682 const uint64_t F = MI.getDesc().TSFlags;
2684 return true;
2685 // Use MO operand flags to determine if one of MI's operands
2686 // has HMOTF_ConstExtended flag set.
2687 for (const MachineOperand &MO : MI.operands())
2688 if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
2689 return true;
2690 return false;
2691}
2692
2694 unsigned Opcode = MI.getOpcode();
2695 const uint64_t F = get(Opcode).TSFlags;
2696 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2697}
2698
2699// No V60 HVX VMEM with A_INDIRECT.
2701 const MachineInstr &J) const {
2702 if (!isHVXVec(I))
2703 return false;
2704 if (!I.mayLoad() && !I.mayStore())
2705 return false;
2706 return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
2707}
2708
2710 switch (MI.getOpcode()) {
2711 case Hexagon::J2_callr:
2712 case Hexagon::J2_callrf:
2713 case Hexagon::J2_callrt:
2714 case Hexagon::PS_call_nr:
2715 return true;
2716 }
2717 return false;
2718}
2719
2721 switch (MI.getOpcode()) {
2722 case Hexagon::L4_return:
2723 case Hexagon::L4_return_t:
2724 case Hexagon::L4_return_f:
2725 case Hexagon::L4_return_fnew_pnt:
2726 case Hexagon::L4_return_fnew_pt:
2727 case Hexagon::L4_return_tnew_pnt:
2728 case Hexagon::L4_return_tnew_pt:
2729 return true;
2730 }
2731 return false;
2732}
2733
2735 switch (MI.getOpcode()) {
2736 case Hexagon::J2_jumpr:
2737 case Hexagon::J2_jumprt:
2738 case Hexagon::J2_jumprf:
2739 case Hexagon::J2_jumprtnewpt:
2740 case Hexagon::J2_jumprfnewpt:
2741 case Hexagon::J2_jumprtnew:
2742 case Hexagon::J2_jumprfnew:
2743 return true;
2744 }
2745 return false;
2746}
2747
2748// Return true if a given MI can accommodate given offset.
2749// Use abs estimate as oppose to the exact number.
2750// TODO: This will need to be changed to use MC level
2751// definition of instruction extendable field size.
2753 unsigned offset) const {
2754 // This selection of jump instructions matches to that what
2755 // analyzeBranch can parse, plus NVJ.
2756 if (isNewValueJump(MI)) // r9:2
2757 return isInt<11>(offset);
2758
2759 switch (MI.getOpcode()) {
2760 // Still missing Jump to address condition on register value.
2761 default:
2762 return false;
2763 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2764 case Hexagon::J2_call:
2765 case Hexagon::PS_call_nr:
2766 return isInt<24>(offset);
2767 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2768 case Hexagon::J2_jumpf:
2769 case Hexagon::J2_jumptnew:
2770 case Hexagon::J2_jumptnewpt:
2771 case Hexagon::J2_jumpfnew:
2772 case Hexagon::J2_jumpfnewpt:
2773 case Hexagon::J2_callt:
2774 case Hexagon::J2_callf:
2775 return isInt<17>(offset);
2776 case Hexagon::J2_loop0i:
2777 case Hexagon::J2_loop0iext:
2778 case Hexagon::J2_loop0r:
2779 case Hexagon::J2_loop0rext:
2780 case Hexagon::J2_loop1i:
2781 case Hexagon::J2_loop1iext:
2782 case Hexagon::J2_loop1r:
2783 case Hexagon::J2_loop1rext:
2784 return isInt<9>(offset);
2785 // TODO: Add all the compound branches here. Can we do this in Relation model?
2786 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2787 case Hexagon::J4_cmpeqi_tp1_jump_nt:
2788 case Hexagon::J4_cmpeqn1_tp0_jump_nt:
2789 case Hexagon::J4_cmpeqn1_tp1_jump_nt:
2790 return isInt<11>(offset);
2791 }
2792}
2793
2795 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2796 // resource, but all operands can be received late like an ALU instruction.
2798}
2799
2801 unsigned Opcode = MI.getOpcode();
2802 return Opcode == Hexagon::J2_loop0i ||
2803 Opcode == Hexagon::J2_loop0r ||
2804 Opcode == Hexagon::J2_loop0iext ||
2805 Opcode == Hexagon::J2_loop0rext ||
2806 Opcode == Hexagon::J2_loop1i ||
2807 Opcode == Hexagon::J2_loop1r ||
2808 Opcode == Hexagon::J2_loop1iext ||
2809 Opcode == Hexagon::J2_loop1rext;
2810}
2811
2813 switch (MI.getOpcode()) {
2814 default: return false;
2815 case Hexagon::L4_iadd_memopw_io:
2816 case Hexagon::L4_isub_memopw_io:
2817 case Hexagon::L4_add_memopw_io:
2818 case Hexagon::L4_sub_memopw_io:
2819 case Hexagon::L4_and_memopw_io:
2820 case Hexagon::L4_or_memopw_io:
2821 case Hexagon::L4_iadd_memoph_io:
2822 case Hexagon::L4_isub_memoph_io:
2823 case Hexagon::L4_add_memoph_io:
2824 case Hexagon::L4_sub_memoph_io:
2825 case Hexagon::L4_and_memoph_io:
2826 case Hexagon::L4_or_memoph_io:
2827 case Hexagon::L4_iadd_memopb_io:
2828 case Hexagon::L4_isub_memopb_io:
2829 case Hexagon::L4_add_memopb_io:
2830 case Hexagon::L4_sub_memopb_io:
2831 case Hexagon::L4_and_memopb_io:
2832 case Hexagon::L4_or_memopb_io:
2833 case Hexagon::L4_ior_memopb_io:
2834 case Hexagon::L4_ior_memoph_io:
2835 case Hexagon::L4_ior_memopw_io:
2836 case Hexagon::L4_iand_memopb_io:
2837 case Hexagon::L4_iand_memoph_io:
2838 case Hexagon::L4_iand_memopw_io:
2839 return true;
2840 }
2841 return false;
2842}
2843
2845 const uint64_t F = MI.getDesc().TSFlags;
2847}
2848
2849bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2850 const uint64_t F = get(Opcode).TSFlags;
2852}
2853
2857
2859 return isNewValue(MI) && MI.isBranch();
2860}
2861
2862bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2863 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2864}
2865
2867 const uint64_t F = MI.getDesc().TSFlags;
2869}
2870
2871bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2872 const uint64_t F = get(Opcode).TSFlags;
2874}
2875
2876// Returns true if a particular operand is extendable for an instruction.
2878 unsigned OperandNum) const {
2879 const uint64_t F = MI.getDesc().TSFlags;
2881 == OperandNum;
2882}
2883
2885 const uint64_t F = MI.getDesc().TSFlags;
2888}
2889
2890bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2891 const uint64_t F = get(Opcode).TSFlags;
2892 assert(isPredicated(Opcode));
2894}
2895
2897 const uint64_t F = MI.getDesc().TSFlags;
2898 return !((F >> HexagonII::PredicatedFalsePos) &
2900}
2901
2902bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2903 const uint64_t F = get(Opcode).TSFlags;
2904 // Make sure that the instruction is predicated.
2906 return !((F >> HexagonII::PredicatedFalsePos) &
2908}
2909
2910bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2911 const uint64_t F = get(Opcode).TSFlags;
2913}
2914
2915bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2916 const uint64_t F = get(Opcode).TSFlags;
2918}
2919
2920bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2921 const uint64_t F = get(Opcode).TSFlags;
2922 assert(get(Opcode).isBranch() &&
2923 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2925}
2926
2928 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2929 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2930 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2931 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
2932}
2933
2935 switch (MI.getOpcode()) {
2936 // Byte
2937 case Hexagon::L2_loadrb_io:
2938 case Hexagon::L4_loadrb_ur:
2939 case Hexagon::L4_loadrb_ap:
2940 case Hexagon::L2_loadrb_pr:
2941 case Hexagon::L2_loadrb_pbr:
2942 case Hexagon::L2_loadrb_pi:
2943 case Hexagon::L2_loadrb_pci:
2944 case Hexagon::L2_loadrb_pcr:
2945 case Hexagon::L2_loadbsw2_io:
2946 case Hexagon::L4_loadbsw2_ur:
2947 case Hexagon::L4_loadbsw2_ap:
2948 case Hexagon::L2_loadbsw2_pr:
2949 case Hexagon::L2_loadbsw2_pbr:
2950 case Hexagon::L2_loadbsw2_pi:
2951 case Hexagon::L2_loadbsw2_pci:
2952 case Hexagon::L2_loadbsw2_pcr:
2953 case Hexagon::L2_loadbsw4_io:
2954 case Hexagon::L4_loadbsw4_ur:
2955 case Hexagon::L4_loadbsw4_ap:
2956 case Hexagon::L2_loadbsw4_pr:
2957 case Hexagon::L2_loadbsw4_pbr:
2958 case Hexagon::L2_loadbsw4_pi:
2959 case Hexagon::L2_loadbsw4_pci:
2960 case Hexagon::L2_loadbsw4_pcr:
2961 case Hexagon::L4_loadrb_rr:
2962 case Hexagon::L2_ploadrbt_io:
2963 case Hexagon::L2_ploadrbt_pi:
2964 case Hexagon::L2_ploadrbf_io:
2965 case Hexagon::L2_ploadrbf_pi:
2966 case Hexagon::L2_ploadrbtnew_io:
2967 case Hexagon::L2_ploadrbfnew_io:
2968 case Hexagon::L4_ploadrbt_rr:
2969 case Hexagon::L4_ploadrbf_rr:
2970 case Hexagon::L4_ploadrbtnew_rr:
2971 case Hexagon::L4_ploadrbfnew_rr:
2972 case Hexagon::L2_ploadrbtnew_pi:
2973 case Hexagon::L2_ploadrbfnew_pi:
2974 case Hexagon::L4_ploadrbt_abs:
2975 case Hexagon::L4_ploadrbf_abs:
2976 case Hexagon::L4_ploadrbtnew_abs:
2977 case Hexagon::L4_ploadrbfnew_abs:
2978 case Hexagon::L2_loadrbgp:
2979 // Half
2980 case Hexagon::L2_loadrh_io:
2981 case Hexagon::L4_loadrh_ur:
2982 case Hexagon::L4_loadrh_ap:
2983 case Hexagon::L2_loadrh_pr:
2984 case Hexagon::L2_loadrh_pbr:
2985 case Hexagon::L2_loadrh_pi:
2986 case Hexagon::L2_loadrh_pci:
2987 case Hexagon::L2_loadrh_pcr:
2988 case Hexagon::L4_loadrh_rr:
2989 case Hexagon::L2_ploadrht_io:
2990 case Hexagon::L2_ploadrht_pi:
2991 case Hexagon::L2_ploadrhf_io:
2992 case Hexagon::L2_ploadrhf_pi:
2993 case Hexagon::L2_ploadrhtnew_io:
2994 case Hexagon::L2_ploadrhfnew_io:
2995 case Hexagon::L4_ploadrht_rr:
2996 case Hexagon::L4_ploadrhf_rr:
2997 case Hexagon::L4_ploadrhtnew_rr:
2998 case Hexagon::L4_ploadrhfnew_rr:
2999 case Hexagon::L2_ploadrhtnew_pi:
3000 case Hexagon::L2_ploadrhfnew_pi:
3001 case Hexagon::L4_ploadrht_abs:
3002 case Hexagon::L4_ploadrhf_abs:
3003 case Hexagon::L4_ploadrhtnew_abs:
3004 case Hexagon::L4_ploadrhfnew_abs:
3005 case Hexagon::L2_loadrhgp:
3006 return true;
3007 default:
3008 return false;
3009 }
3010}
3011
3013 const uint64_t F = MI.getDesc().TSFlags;
3015}
3016
3018 switch (MI.getOpcode()) {
3019 case Hexagon::STriw_pred:
3020 case Hexagon::LDriw_pred:
3021 return true;
3022 default:
3023 return false;
3024 }
3025}
3026
3028 if (!MI.isBranch())
3029 return false;
3030
3031 for (auto &Op : MI.operands())
3032 if (Op.isGlobal() || Op.isSymbol())
3033 return true;
3034 return false;
3035}
3036
3037// Returns true when SU has a timing class TC1.
3039 unsigned SchedClass = MI.getDesc().getSchedClass();
3040 return is_TC1(SchedClass);
3041}
3042
3044 unsigned SchedClass = MI.getDesc().getSchedClass();
3045 return is_TC2(SchedClass);
3046}
3047
3049 unsigned SchedClass = MI.getDesc().getSchedClass();
3050 return is_TC2early(SchedClass);
3051}
3052
3054 unsigned SchedClass = MI.getDesc().getSchedClass();
3055 return is_TC4x(SchedClass);
3056}
3057
3058// Schedule this ASAP.
3060 const MachineInstr &MI2) const {
3061 if (mayBeCurLoad(MI1)) {
3062 // if (result of SU is used in Next) return true;
3063 Register DstReg = MI1.getOperand(0).getReg();
3064 int N = MI2.getNumOperands();
3065 for (int I = 0; I < N; I++)
3066 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
3067 return true;
3068 }
3069 if (mayBeNewStore(MI2))
3070 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
3071 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
3072 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
3073 return true;
3074 return false;
3075}
3076
3078 const uint64_t V = getType(MI);
3080}
3081
3083 return (MI.getDesc().TSFlags >> HexagonII::isHLXPos) & HexagonII::isHLXMask;
3084}
3085
3086// Check if the Offset is a valid auto-inc imm by Load/Store Type.
3088 int Size = VT.getSizeInBits() / 8;
3089 if (Offset % Size != 0)
3090 return false;
3091 int Count = Offset / Size;
3092
3093 switch (VT.getSimpleVT().SimpleTy) {
3094 // For scalars the auto-inc is s4
3095 case MVT::i8:
3096 case MVT::i16:
3097 case MVT::i32:
3098 case MVT::i64:
3099 case MVT::f32:
3100 case MVT::f64:
3101 case MVT::v2i16:
3102 case MVT::v2i32:
3103 case MVT::v4i8:
3104 case MVT::v4i16:
3105 case MVT::v8i8:
3106 return isInt<4>(Count);
3107 // For HVX vectors the auto-inc is s3
3108 case MVT::v64i8:
3109 case MVT::v32i16:
3110 case MVT::v16i32:
3111 case MVT::v8i64:
3112 case MVT::v128i8:
3113 case MVT::v64i16:
3114 case MVT::v32i32:
3115 case MVT::v16i64:
3116 return isInt<3>(Count);
3117 default:
3118 break;
3119 }
3120
3121 llvm_unreachable("Not an valid type!");
3122}
3123
3124bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
3125 bool Extend) const {
3126 // This function is to check whether the "Offset" is in the correct range of
3127 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
3128 // inserted to calculate the final address. Due to this reason, the function
3129 // assumes that the "Offset" has correct alignment.
3130 // We used to assert if the offset was not properly aligned, however,
3131 // there are cases where a misaligned pointer recast can cause this
3132 // problem, and we need to allow for it. The front end warns of such
3133 // misaligns with respect to load size.
3134 switch (Opcode) {
3135 case Hexagon::PS_vstorerq_ai:
3136 case Hexagon::PS_vstorerv_ai:
3137 case Hexagon::PS_vstorerw_ai:
3138 case Hexagon::PS_vstorerw_nt_ai:
3139 case Hexagon::PS_vloadrq_ai:
3140 case Hexagon::PS_vloadrv_ai:
3141 case Hexagon::PS_vloadrw_ai:
3142 case Hexagon::PS_vloadrw_nt_ai:
3143 case Hexagon::V6_vL32b_ai:
3144 case Hexagon::V6_vS32b_ai:
3145 case Hexagon::V6_vS32b_pred_ai:
3146 case Hexagon::V6_vS32b_npred_ai:
3147 case Hexagon::V6_vS32b_qpred_ai:
3148 case Hexagon::V6_vS32b_nqpred_ai:
3149 case Hexagon::V6_vS32b_new_ai:
3150 case Hexagon::V6_vS32b_new_pred_ai:
3151 case Hexagon::V6_vS32b_new_npred_ai:
3152 case Hexagon::V6_vS32b_nt_pred_ai:
3153 case Hexagon::V6_vS32b_nt_npred_ai:
3154 case Hexagon::V6_vS32b_nt_new_ai:
3155 case Hexagon::V6_vS32b_nt_new_pred_ai:
3156 case Hexagon::V6_vS32b_nt_new_npred_ai:
3157 case Hexagon::V6_vS32b_nt_qpred_ai:
3158 case Hexagon::V6_vS32b_nt_nqpred_ai:
3159 case Hexagon::V6_vL32b_nt_ai:
3160 case Hexagon::V6_vS32b_nt_ai:
3161 case Hexagon::V6_vL32Ub_ai:
3162 case Hexagon::V6_vS32Ub_ai:
3163 case Hexagon::V6_vL32b_cur_ai:
3164 case Hexagon::V6_vL32b_tmp_ai:
3165 case Hexagon::V6_vL32b_pred_ai:
3166 case Hexagon::V6_vL32b_npred_ai:
3167 case Hexagon::V6_vL32b_cur_pred_ai:
3168 case Hexagon::V6_vL32b_cur_npred_ai:
3169 case Hexagon::V6_vL32b_tmp_pred_ai:
3170 case Hexagon::V6_vL32b_tmp_npred_ai:
3171 case Hexagon::V6_vL32b_nt_cur_ai:
3172 case Hexagon::V6_vL32b_nt_tmp_ai:
3173 case Hexagon::V6_vL32b_nt_pred_ai:
3174 case Hexagon::V6_vL32b_nt_npred_ai:
3175 case Hexagon::V6_vL32b_nt_cur_pred_ai:
3176 case Hexagon::V6_vL32b_nt_cur_npred_ai:
3177 case Hexagon::V6_vL32b_nt_tmp_pred_ai:
3178 case Hexagon::V6_vL32b_nt_tmp_npred_ai:
3179 case Hexagon::V6_vS32Ub_pred_ai:
3180 case Hexagon::V6_vS32Ub_npred_ai:
3181 case Hexagon::V6_vgathermh_pseudo:
3182 case Hexagon::V6_vgather_vscatter_mh_pseudo:
3183 case Hexagon::V6_vgathermw_pseudo:
3184 case Hexagon::V6_vgathermhw_pseudo:
3185 case Hexagon::V6_vgathermhq_pseudo:
3186 case Hexagon::V6_vgathermwq_pseudo:
3187 case Hexagon::V6_vgathermhwq_pseudo: {
3188 unsigned VectorSize = RegInfo.getSpillSize(Hexagon::HvxVRRegClass);
3189 assert(isPowerOf2_32(VectorSize));
3190 if (Offset & (VectorSize-1))
3191 return false;
3192 return isInt<4>(Offset >> Log2_32(VectorSize));
3193 }
3194
3195 case Hexagon::J2_loop0i:
3196 case Hexagon::J2_loop1i:
3197 return isUInt<10>(Offset);
3198
3199 case Hexagon::S4_storeirb_io:
3200 case Hexagon::S4_storeirbt_io:
3201 case Hexagon::S4_storeirbf_io:
3202 return isUInt<6>(Offset);
3203
3204 case Hexagon::S4_storeirh_io:
3205 case Hexagon::S4_storeirht_io:
3206 case Hexagon::S4_storeirhf_io:
3207 return isShiftedUInt<6,1>(Offset);
3208
3209 case Hexagon::S4_storeiri_io:
3210 case Hexagon::S4_storeirit_io:
3211 case Hexagon::S4_storeirif_io:
3212 return isShiftedUInt<6,2>(Offset);
3213 // Handle these two compare instructions that are not extendable.
3214 case Hexagon::A4_cmpbeqi:
3215 return isUInt<8>(Offset);
3216 case Hexagon::A4_cmpbgti:
3217 return isInt<8>(Offset);
3218 }
3219
3220 if (Extend)
3221 return true;
3222
3223 switch (Opcode) {
3224 case Hexagon::L2_loadri_io:
3225 case Hexagon::S2_storeri_io:
3226 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
3228
3229 case Hexagon::L2_loadrd_io:
3230 case Hexagon::S2_storerd_io:
3231 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
3233
3234 case Hexagon::L2_loadrh_io:
3235 case Hexagon::L2_loadruh_io:
3236 case Hexagon::S2_storerh_io:
3237 case Hexagon::S2_storerf_io:
3238 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
3240
3241 case Hexagon::L2_loadrb_io:
3242 case Hexagon::L2_loadrub_io:
3243 case Hexagon::S2_storerb_io:
3244 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
3246
3247 case Hexagon::A2_addi:
3248 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
3250
3251 case Hexagon::L4_iadd_memopw_io:
3252 case Hexagon::L4_isub_memopw_io:
3253 case Hexagon::L4_add_memopw_io:
3254 case Hexagon::L4_sub_memopw_io:
3255 case Hexagon::L4_iand_memopw_io:
3256 case Hexagon::L4_ior_memopw_io:
3257 case Hexagon::L4_and_memopw_io:
3258 case Hexagon::L4_or_memopw_io:
3259 return (0 <= Offset && Offset <= 255);
3260
3261 case Hexagon::L4_iadd_memoph_io:
3262 case Hexagon::L4_isub_memoph_io:
3263 case Hexagon::L4_add_memoph_io:
3264 case Hexagon::L4_sub_memoph_io:
3265 case Hexagon::L4_iand_memoph_io:
3266 case Hexagon::L4_ior_memoph_io:
3267 case Hexagon::L4_and_memoph_io:
3268 case Hexagon::L4_or_memoph_io:
3269 return (0 <= Offset && Offset <= 127);
3270
3271 case Hexagon::L4_iadd_memopb_io:
3272 case Hexagon::L4_isub_memopb_io:
3273 case Hexagon::L4_add_memopb_io:
3274 case Hexagon::L4_sub_memopb_io:
3275 case Hexagon::L4_iand_memopb_io:
3276 case Hexagon::L4_ior_memopb_io:
3277 case Hexagon::L4_and_memopb_io:
3278 case Hexagon::L4_or_memopb_io:
3279 return (0 <= Offset && Offset <= 63);
3280
3281 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
3282 // any size. Later pass knows how to handle it.
3283 case Hexagon::STriw_pred:
3284 case Hexagon::LDriw_pred:
3285 case Hexagon::STriw_ctr:
3286 case Hexagon::LDriw_ctr:
3287 return true;
3288
3289 case Hexagon::PS_fi:
3290 case Hexagon::PS_fia:
3291 case Hexagon::INLINEASM:
3292 return true;
3293
3294 case Hexagon::L2_ploadrbt_io:
3295 case Hexagon::L2_ploadrbf_io:
3296 case Hexagon::L2_ploadrubt_io:
3297 case Hexagon::L2_ploadrubf_io:
3298 case Hexagon::S2_pstorerbt_io:
3299 case Hexagon::S2_pstorerbf_io:
3300 return isUInt<6>(Offset);
3301
3302 case Hexagon::L2_ploadrht_io:
3303 case Hexagon::L2_ploadrhf_io:
3304 case Hexagon::L2_ploadruht_io:
3305 case Hexagon::L2_ploadruhf_io:
3306 case Hexagon::S2_pstorerht_io:
3307 case Hexagon::S2_pstorerhf_io:
3308 case Hexagon::S2_pstorerft_io:
3309 case Hexagon::S2_pstorerff_io:
3310 return isShiftedUInt<6,1>(Offset);
3311
3312 case Hexagon::L2_ploadrit_io:
3313 case Hexagon::L2_ploadrif_io:
3314 case Hexagon::S2_pstorerit_io:
3315 case Hexagon::S2_pstorerif_io:
3316 return isShiftedUInt<6,2>(Offset);
3317
3318 case Hexagon::L2_ploadrdt_io:
3319 case Hexagon::L2_ploadrdf_io:
3320 case Hexagon::S2_pstorerdt_io:
3321 case Hexagon::S2_pstorerdf_io:
3322 return isShiftedUInt<6,3>(Offset);
3323
3324 case Hexagon::L2_loadbsw2_io:
3325 case Hexagon::L2_loadbzw2_io:
3326 return isShiftedInt<11,1>(Offset);
3327
3328 case Hexagon::L2_loadbsw4_io:
3329 case Hexagon::L2_loadbzw4_io:
3330 return isShiftedInt<11,2>(Offset);
3331 } // switch
3332
3333 dbgs() << "Failed Opcode is : " << Opcode << " (" << getName(Opcode)
3334 << ")\n";
3335 llvm_unreachable("No offset range is defined for this opcode. "
3336 "Please define it in the above switch statement!");
3337}
3338
3340 return isHVXVec(MI) && isAccumulator(MI);
3341}
3342
3344 const uint64_t F = get(MI.getOpcode()).TSFlags;
3345 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
3346 return
3347 V == HexagonII::TypeCVI_VA ||
3349}
3350
3352 const MachineInstr &ConsMI) const {
3353 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
3354 return true;
3355
3356 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
3357 return true;
3358
3359 if (mayBeNewStore(ConsMI))
3360 return true;
3361
3362 return false;
3363}
3364
3366 switch (MI.getOpcode()) {
3367 // Byte
3368 case Hexagon::L2_loadrub_io:
3369 case Hexagon::L4_loadrub_ur:
3370 case Hexagon::L4_loadrub_ap:
3371 case Hexagon::L2_loadrub_pr:
3372 case Hexagon::L2_loadrub_pbr:
3373 case Hexagon::L2_loadrub_pi:
3374 case Hexagon::L2_loadrub_pci:
3375 case Hexagon::L2_loadrub_pcr:
3376 case Hexagon::L2_loadbzw2_io:
3377 case Hexagon::L4_loadbzw2_ur:
3378 case Hexagon::L4_loadbzw2_ap:
3379 case Hexagon::L2_loadbzw2_pr:
3380 case Hexagon::L2_loadbzw2_pbr:
3381 case Hexagon::L2_loadbzw2_pi:
3382 case Hexagon::L2_loadbzw2_pci:
3383 case Hexagon::L2_loadbzw2_pcr:
3384 case Hexagon::L2_loadbzw4_io:
3385 case Hexagon::L4_loadbzw4_ur:
3386 case Hexagon::L4_loadbzw4_ap:
3387 case Hexagon::L2_loadbzw4_pr:
3388 case Hexagon::L2_loadbzw4_pbr:
3389 case Hexagon::L2_loadbzw4_pi:
3390 case Hexagon::L2_loadbzw4_pci:
3391 case Hexagon::L2_loadbzw4_pcr:
3392 case Hexagon::L4_loadrub_rr:
3393 case Hexagon::L2_ploadrubt_io:
3394 case Hexagon::L2_ploadrubt_pi:
3395 case Hexagon::L2_ploadrubf_io:
3396 case Hexagon::L2_ploadrubf_pi:
3397 case Hexagon::L2_ploadrubtnew_io:
3398 case Hexagon::L2_ploadrubfnew_io:
3399 case Hexagon::L4_ploadrubt_rr:
3400 case Hexagon::L4_ploadrubf_rr:
3401 case Hexagon::L4_ploadrubtnew_rr:
3402 case Hexagon::L4_ploadrubfnew_rr:
3403 case Hexagon::L2_ploadrubtnew_pi:
3404 case Hexagon::L2_ploadrubfnew_pi:
3405 case Hexagon::L4_ploadrubt_abs:
3406 case Hexagon::L4_ploadrubf_abs:
3407 case Hexagon::L4_ploadrubtnew_abs:
3408 case Hexagon::L4_ploadrubfnew_abs:
3409 case Hexagon::L2_loadrubgp:
3410 // Half
3411 case Hexagon::L2_loadruh_io:
3412 case Hexagon::L4_loadruh_ur:
3413 case Hexagon::L4_loadruh_ap:
3414 case Hexagon::L2_loadruh_pr:
3415 case Hexagon::L2_loadruh_pbr:
3416 case Hexagon::L2_loadruh_pi:
3417 case Hexagon::L2_loadruh_pci:
3418 case Hexagon::L2_loadruh_pcr:
3419 case Hexagon::L4_loadruh_rr:
3420 case Hexagon::L2_ploadruht_io:
3421 case Hexagon::L2_ploadruht_pi:
3422 case Hexagon::L2_ploadruhf_io:
3423 case Hexagon::L2_ploadruhf_pi:
3424 case Hexagon::L2_ploadruhtnew_io:
3425 case Hexagon::L2_ploadruhfnew_io:
3426 case Hexagon::L4_ploadruht_rr:
3427 case Hexagon::L4_ploadruhf_rr:
3428 case Hexagon::L4_ploadruhtnew_rr:
3429 case Hexagon::L4_ploadruhfnew_rr:
3430 case Hexagon::L2_ploadruhtnew_pi:
3431 case Hexagon::L2_ploadruhfnew_pi:
3432 case Hexagon::L4_ploadruht_abs:
3433 case Hexagon::L4_ploadruhf_abs:
3434 case Hexagon::L4_ploadruhtnew_abs:
3435 case Hexagon::L4_ploadruhfnew_abs:
3436 case Hexagon::L2_loadruhgp:
3437 return true;
3438 default:
3439 return false;
3440 }
3441}
3442
3443// Add latency to instruction.
3445 const MachineInstr &MI2) const {
3446 if (isHVXVec(MI1) && isHVXVec(MI2))
3447 if (!isVecUsableNextPacket(MI1, MI2))
3448 return true;
3449 return false;
3450}
3451
3452/// Get the base register and byte offset of a load/store instr.
3455 int64_t &Offset, bool &OffsetIsScalable, LocationSize &Width) const {
3456 OffsetIsScalable = false;
3457 const MachineOperand *BaseOp = getBaseAndOffset(LdSt, Offset, Width);
3458 if (!BaseOp || !BaseOp->isReg())
3459 return false;
3460 BaseOps.push_back(BaseOp);
3461 return true;
3462}
3463
3464/// Can these instructions execute at the same time in a bundle.
3466 const MachineInstr &Second) const {
3467 if (Second.mayStore() && First.getOpcode() == Hexagon::S2_allocframe) {
3468 const MachineOperand &Op = Second.getOperand(0);
3469 if (Op.isReg() && Op.isUse() && Op.getReg() == Hexagon::R29)
3470 return true;
3471 }
3473 return false;
3474 if (mayBeNewStore(Second)) {
3475 // Make sure the definition of the first instruction is the value being
3476 // stored.
3477 const MachineOperand &Stored =
3478 Second.getOperand(Second.getNumOperands() - 1);
3479 if (!Stored.isReg())
3480 return false;
3481 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
3482 const MachineOperand &Op = First.getOperand(i);
3483 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
3484 return true;
3485 }
3486 }
3487 return false;
3488}
3489
3491 unsigned Opc = CallMI.getOpcode();
3492 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
3493}
3494
3496 for (auto &I : *B)
3497 if (I.isEHLabel())
3498 return true;
3499 return false;
3500}
3501
3502// Returns true if an instruction can be converted into a non-extended
3503// equivalent instruction.
3505 short NonExtOpcode;
3506 // Check if the instruction has a register form that uses register in place
3507 // of the extended operand, if so return that as the non-extended form.
3508 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
3509 return true;
3510
3511 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
3512 // Check addressing mode and retrieve non-ext equivalent instruction.
3513
3514 switch (getAddrMode(MI)) {
3516 // Load/store with absolute addressing mode can be converted into
3517 // base+offset mode.
3518 NonExtOpcode = Hexagon::changeAddrMode_abs_io(MI.getOpcode());
3519 break;
3521 // Load/store with base+offset addressing mode can be converted into
3522 // base+register offset addressing mode. However left shift operand should
3523 // be set to 0.
3524 NonExtOpcode = Hexagon::changeAddrMode_io_rr(MI.getOpcode());
3525 break;
3527 NonExtOpcode = Hexagon::changeAddrMode_ur_rr(MI.getOpcode());
3528 break;
3529 default:
3530 return false;
3531 }
3532 if (NonExtOpcode < 0)
3533 return false;
3534 return true;
3535 }
3536 return false;
3537}
3538
3540 const {
3541 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
3542 while (I != E) {
3543 if (I->isBarrier())
3544 return true;
3545 ++I;
3546 }
3547 return false;
3548}
3549
3550// Returns true, if a LD insn can be promoted to a cur load.
3552 const uint64_t F = MI.getDesc().TSFlags;
3554 Subtarget.hasV60Ops();
3555}
3556
3557// Returns true, if a ST insn can be promoted to a new-value store.
3559 if (MI.mayStore() && !Subtarget.useNewValueStores())
3560 return false;
3561
3562 const uint64_t F = MI.getDesc().TSFlags;
3564}
3565
3567 return MI.getOpcode() == Hexagon::LX_L16b_pseudo;
3568}
3569
3571 return MI.getOpcode() == Hexagon::LX_S16b_pseudo;
3572}
3573
3575 const MachineInstr &ConsMI) const {
3576 // There is no stall when ProdMI is not a V60 vector.
3577 if (!isHVXVec(ProdMI))
3578 return false;
3579
3580 // There is no stall when ProdMI and ConsMI are not dependent.
3581 if (!isDependent(ProdMI, ConsMI))
3582 return false;
3583
3584 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
3585 // are scheduled in consecutive packets.
3586 if (isVecUsableNextPacket(ProdMI, ConsMI))
3587 return false;
3588
3589 return true;
3590}
3591
3594 // There is no stall when I is not a V60 vector.
3595 if (!isHVXVec(MI))
3596 return false;
3597
3599 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
3600
3601 if (!MII->isBundle())
3602 return producesStall(*MII, MI);
3603
3604 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
3605 const MachineInstr &J = *MII;
3606 if (producesStall(J, MI))
3607 return true;
3608 }
3609 return false;
3610}
3611
3613 Register PredReg) const {
3614 for (const MachineOperand &MO : MI.operands()) {
3615 // Predicate register must be explicitly defined.
3616 if (MO.isRegMask() && MO.clobbersPhysReg(PredReg))
3617 return false;
3618 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
3619 return false;
3620 }
3621
3622 // Instruction that produce late predicate cannot be used as sources of
3623 // dot-new.
3624 switch (MI.getOpcode()) {
3625 case Hexagon::A4_addp_c:
3626 case Hexagon::A4_subp_c:
3627 case Hexagon::A4_tlbmatch:
3628 case Hexagon::A5_ACS:
3629 case Hexagon::F2_sfinvsqrta:
3630 case Hexagon::F2_sfrecipa:
3631 case Hexagon::J2_endloop0:
3632 case Hexagon::J2_endloop01:
3633 case Hexagon::J2_ploop1si:
3634 case Hexagon::J2_ploop1sr:
3635 case Hexagon::J2_ploop2si:
3636 case Hexagon::J2_ploop2sr:
3637 case Hexagon::J2_ploop3si:
3638 case Hexagon::J2_ploop3sr:
3639 case Hexagon::S2_cabacdecbin:
3640 case Hexagon::S2_storew_locked:
3641 case Hexagon::S4_stored_locked:
3642 return false;
3643 }
3644 return true;
3645}
3646
3647bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
3648 return Opcode == Hexagon::J2_jumpt ||
3649 Opcode == Hexagon::J2_jumptpt ||
3650 Opcode == Hexagon::J2_jumpf ||
3651 Opcode == Hexagon::J2_jumpfpt ||
3652 Opcode == Hexagon::J2_jumptnew ||
3653 Opcode == Hexagon::J2_jumpfnew ||
3654 Opcode == Hexagon::J2_jumptnewpt ||
3655 Opcode == Hexagon::J2_jumpfnewpt;
3656}
3657
3659 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
3660 return false;
3661 return !isPredicatedTrue(Cond[0].getImm());
3662}
3663
3665 const uint64_t F = MI.getDesc().TSFlags;
3667}
3668
3669// Returns the base register in a memory access (load/store). The offset is
3670// returned in Offset and the access size is returned in AccessSize.
3671// If the base operand has a subregister or the offset field does not contain
3672// an immediate value, return nullptr.
3675 LocationSize &AccessSize) const {
3676 // Return if it is not a base+offset type instruction or a MemOp.
3680 return nullptr;
3681
3683
3684 unsigned BasePos = 0, OffsetPos = 0;
3685 if (!getBaseAndOffsetPosition(MI, BasePos, OffsetPos))
3686 return nullptr;
3687
3688 // Post increment updates its EA after the mem access,
3689 // so we need to treat its offset as zero.
3690 if (isPostIncrement(MI)) {
3691 Offset = 0;
3692 } else {
3693 const MachineOperand &OffsetOp = MI.getOperand(OffsetPos);
3694 if (!OffsetOp.isImm())
3695 return nullptr;
3696 Offset = OffsetOp.getImm();
3697 }
3698
3699 const MachineOperand &BaseOp = MI.getOperand(BasePos);
3700 if (BaseOp.getSubReg() != 0)
3701 return nullptr;
3702 return &const_cast<MachineOperand&>(BaseOp);
3703}
3704
3705/// Return the position of the base and offset operands for this instruction.
3707 unsigned &BasePos, unsigned &OffsetPos) const {
3709 return false;
3710
3711 // Deal with memops first.
3712 if (isMemOp(MI)) {
3713 BasePos = 0;
3714 OffsetPos = 1;
3715 } else if (MI.mayStore()) {
3716 BasePos = 0;
3717 OffsetPos = 1;
3718 } else if (MI.mayLoad()) {
3719 BasePos = 1;
3720 OffsetPos = 2;
3721 } else
3722 return false;
3723
3724 if (isPredicated(MI)) {
3725 BasePos++;
3726 OffsetPos++;
3727 }
3728 if (isPostIncrement(MI)) {
3729 BasePos++;
3730 OffsetPos++;
3731 }
3732
3733 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
3734 return false;
3735
3736 return true;
3737}
3738
3739// Inserts branching instructions in reverse order of their occurrence.
3740// e.g. jump_t t1 (i1)
3741// jump t2 (i2)
3742// Jumpers = {i2, i1}
3744 MachineBasicBlock& MBB) const {
3746 // If the block has no terminators, it just falls into the block after it.
3748 if (I == MBB.instr_begin())
3749 return Jumpers;
3750
3751 // A basic block may looks like this:
3752 //
3753 // [ insn
3754 // EH_LABEL
3755 // insn
3756 // insn
3757 // insn
3758 // EH_LABEL
3759 // insn ]
3760 //
3761 // It has two succs but does not have a terminator
3762 // Don't know how to handle it.
3763 do {
3764 --I;
3765 if (I->isEHLabel())
3766 return Jumpers;
3767 } while (I != MBB.instr_begin());
3768
3769 I = MBB.instr_end();
3770 --I;
3771
3772 while (I->isDebugInstr()) {
3773 if (I == MBB.instr_begin())
3774 return Jumpers;
3775 --I;
3776 }
3777 if (!isUnpredicatedTerminator(*I))
3778 return Jumpers;
3779
3780 // Get the last instruction in the block.
3781 MachineInstr *LastInst = &*I;
3782 Jumpers.push_back(LastInst);
3783 MachineInstr *SecondLastInst = nullptr;
3784 // Find one more terminator if present.
3785 do {
3786 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
3787 if (!SecondLastInst) {
3788 SecondLastInst = &*I;
3789 Jumpers.push_back(SecondLastInst);
3790 } else // This is a third branch.
3791 return Jumpers;
3792 }
3793 if (I == MBB.instr_begin())
3794 break;
3795 --I;
3796 } while (true);
3797 return Jumpers;
3798}
3799
3800// Returns Operand Index for the constant extended instruction.
3802 const uint64_t F = MI.getDesc().TSFlags;
3804}
3805
3806// See if instruction could potentially be a duplex candidate.
3807// If so, return its group. Zero otherwise.
3809 const MachineInstr &MI) const {
3810 Register DstReg, SrcReg, Src1Reg, Src2Reg;
3811
3812 switch (MI.getOpcode()) {
3813 default:
3814 return HexagonII::HCG_None;
3815 //
3816 // Compound pairs.
3817 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3818 // "Rd16=#U6 ; jump #r9:2"
3819 // "Rd16=Rs16 ; jump #r9:2"
3820 //
3821 case Hexagon::C2_cmpeq:
3822 case Hexagon::C2_cmpgt:
3823 case Hexagon::C2_cmpgtu:
3824 DstReg = MI.getOperand(0).getReg();
3825 Src1Reg = MI.getOperand(1).getReg();
3826 Src2Reg = MI.getOperand(2).getReg();
3827 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3828 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3829 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3830 return HexagonII::HCG_A;
3831 break;
3832 case Hexagon::C2_cmpeqi:
3833 case Hexagon::C2_cmpgti:
3834 case Hexagon::C2_cmpgtui:
3835 // P0 = cmp.eq(Rs,#u2)
3836 DstReg = MI.getOperand(0).getReg();
3837 SrcReg = MI.getOperand(1).getReg();
3838 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3839 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3840 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3841 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3842 (MI.getOperand(2).getImm() == -1)))
3843 return HexagonII::HCG_A;
3844 break;
3845 case Hexagon::A2_tfr:
3846 // Rd = Rs
3847 DstReg = MI.getOperand(0).getReg();
3848 SrcReg = MI.getOperand(1).getReg();
3849 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3850 return HexagonII::HCG_A;
3851 break;
3852 case Hexagon::A2_tfrsi:
3853 // Rd = #u6
3854 // Do not test for #u6 size since the const is getting extended
3855 // regardless and compound could be formed.
3856 DstReg = MI.getOperand(0).getReg();
3857 if (isIntRegForSubInst(DstReg))
3858 return HexagonII::HCG_A;
3859 break;
3860 case Hexagon::S2_tstbit_i:
3861 DstReg = MI.getOperand(0).getReg();
3862 Src1Reg = MI.getOperand(1).getReg();
3863 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3864 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3865 MI.getOperand(2).isImm() &&
3866 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
3867 return HexagonII::HCG_A;
3868 break;
3869 // The fact that .new form is used pretty much guarantees
3870 // that predicate register will match. Nevertheless,
3871 // there could be some false positives without additional
3872 // checking.
3873 case Hexagon::J2_jumptnew:
3874 case Hexagon::J2_jumpfnew:
3875 case Hexagon::J2_jumptnewpt:
3876 case Hexagon::J2_jumpfnewpt:
3877 Src1Reg = MI.getOperand(0).getReg();
3878 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3879 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3880 return HexagonII::HCG_B;
3881 break;
3882 // Transfer and jump:
3883 // Rd=#U6 ; jump #r9:2
3884 // Rd=Rs ; jump #r9:2
3885 // Do not test for jump range here.
3886 case Hexagon::J2_jump:
3887 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3888 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
3889 return HexagonII::HCG_C;
3890 }
3891
3892 return HexagonII::HCG_None;
3893}
3894
3895// Returns -1 when there is no opcode found.
3897 const MachineInstr &GB) const {
3900 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3901 (GB.getOpcode() != Hexagon::J2_jumptnew))
3902 return -1u;
3903 Register DestReg = GA.getOperand(0).getReg();
3904 if (!GB.readsRegister(DestReg, /*TRI=*/nullptr))
3905 return -1u;
3906 if (DestReg != Hexagon::P0 && DestReg != Hexagon::P1)
3907 return -1u;
3908 // The value compared against must be either u5 or -1.
3909 const MachineOperand &CmpOp = GA.getOperand(2);
3910 if (!CmpOp.isImm())
3911 return -1u;
3912 int V = CmpOp.getImm();
3913 if (V == -1)
3914 return DestReg == Hexagon::P0 ? Hexagon::J4_cmpeqn1_tp0_jump_nt
3915 : Hexagon::J4_cmpeqn1_tp1_jump_nt;
3916 if (!isUInt<5>(V))
3917 return -1u;
3918 return DestReg == Hexagon::P0 ? Hexagon::J4_cmpeqi_tp0_jump_nt
3919 : Hexagon::J4_cmpeqi_tp1_jump_nt;
3920}
3921
3922// Returns -1 if there is no opcode found.
3924 bool ForBigCore) const {
3925 // Static table to switch the opcodes across Tiny Core and Big Core.
3926 // dup_ opcodes are Big core opcodes.
3927 // NOTE: There are special instructions that need to handled later.
3928 // L4_return* instructions, they will only occupy SLOT0 (on big core too).
3929 // PS_jmpret - This pseudo translates to J2_jumpr which occupies only SLOT2.
3930 // The compiler need to base the root instruction to L6_return_map_to_raw
3931 // which can go any slot.
3932 static const std::map<unsigned, unsigned> DupMap = {
3933 {Hexagon::A2_add, Hexagon::dup_A2_add},
3934 {Hexagon::A2_addi, Hexagon::dup_A2_addi},
3935 {Hexagon::A2_andir, Hexagon::dup_A2_andir},
3936 {Hexagon::A2_combineii, Hexagon::dup_A2_combineii},
3937 {Hexagon::A2_sxtb, Hexagon::dup_A2_sxtb},
3938 {Hexagon::A2_sxth, Hexagon::dup_A2_sxth},
3939 {Hexagon::A2_tfr, Hexagon::dup_A2_tfr},
3940 {Hexagon::A2_tfrsi, Hexagon::dup_A2_tfrsi},
3941 {Hexagon::A2_zxtb, Hexagon::dup_A2_zxtb},
3942 {Hexagon::A2_zxth, Hexagon::dup_A2_zxth},
3943 {Hexagon::A4_combineii, Hexagon::dup_A4_combineii},
3944 {Hexagon::A4_combineir, Hexagon::dup_A4_combineir},
3945 {Hexagon::A4_combineri, Hexagon::dup_A4_combineri},
3946 {Hexagon::C2_cmoveif, Hexagon::dup_C2_cmoveif},
3947 {Hexagon::C2_cmoveit, Hexagon::dup_C2_cmoveit},
3948 {Hexagon::C2_cmovenewif, Hexagon::dup_C2_cmovenewif},
3949 {Hexagon::C2_cmovenewit, Hexagon::dup_C2_cmovenewit},
3950 {Hexagon::C2_cmpeqi, Hexagon::dup_C2_cmpeqi},
3951 {Hexagon::L2_deallocframe, Hexagon::dup_L2_deallocframe},
3952 {Hexagon::L2_loadrb_io, Hexagon::dup_L2_loadrb_io},
3953 {Hexagon::L2_loadrd_io, Hexagon::dup_L2_loadrd_io},
3954 {Hexagon::L2_loadrh_io, Hexagon::dup_L2_loadrh_io},
3955 {Hexagon::L2_loadri_io, Hexagon::dup_L2_loadri_io},
3956 {Hexagon::L2_loadrub_io, Hexagon::dup_L2_loadrub_io},
3957 {Hexagon::L2_loadruh_io, Hexagon::dup_L2_loadruh_io},
3958 {Hexagon::S2_allocframe, Hexagon::dup_S2_allocframe},
3959 {Hexagon::S2_storerb_io, Hexagon::dup_S2_storerb_io},
3960 {Hexagon::S2_storerd_io, Hexagon::dup_S2_storerd_io},
3961 {Hexagon::S2_storerh_io, Hexagon::dup_S2_storerh_io},
3962 {Hexagon::S2_storeri_io, Hexagon::dup_S2_storeri_io},
3963 {Hexagon::S4_storeirb_io, Hexagon::dup_S4_storeirb_io},
3964 {Hexagon::S4_storeiri_io, Hexagon::dup_S4_storeiri_io},
3965 };
3966 unsigned OpNum = MI.getOpcode();
3967 // Conversion to Big core.
3968 if (ForBigCore) {
3969 auto Iter = DupMap.find(OpNum);
3970 if (Iter != DupMap.end())
3971 return Iter->second;
3972 } else { // Conversion to Tiny core.
3973 for (const auto &Iter : DupMap)
3974 if (Iter.second == OpNum)
3975 return Iter.first;
3976 }
3977 return -1;
3978}
3979
3980int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3981 enum Hexagon::PredSense inPredSense;
3982 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3983 Hexagon::PredSense_true;
3984 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3985 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3986 return CondOpcode;
3987
3988 llvm_unreachable("Unexpected predicable instruction");
3989}
3990
3991// Return the cur value instruction for a given store.
3993 switch (MI.getOpcode()) {
3994 default: llvm_unreachable("Unknown .cur type");
3995 case Hexagon::V6_vL32b_pi:
3996 return Hexagon::V6_vL32b_cur_pi;
3997 case Hexagon::V6_vL32b_ai:
3998 return Hexagon::V6_vL32b_cur_ai;
3999 case Hexagon::V6_vL32b_nt_pi:
4000 return Hexagon::V6_vL32b_nt_cur_pi;
4001 case Hexagon::V6_vL32b_nt_ai:
4002 return Hexagon::V6_vL32b_nt_cur_ai;
4003 case Hexagon::V6_vL32b_ppu:
4004 return Hexagon::V6_vL32b_cur_ppu;
4005 case Hexagon::V6_vL32b_nt_ppu:
4006 return Hexagon::V6_vL32b_nt_cur_ppu;
4007 }
4008 return 0;
4009}
4010
4011// Return the regular version of the .cur instruction.
4013 switch (MI.getOpcode()) {
4014 default: llvm_unreachable("Unknown .cur type");
4015 case Hexagon::V6_vL32b_cur_pi:
4016 return Hexagon::V6_vL32b_pi;
4017 case Hexagon::V6_vL32b_cur_ai:
4018 return Hexagon::V6_vL32b_ai;
4019 case Hexagon::V6_vL32b_nt_cur_pi:
4020 return Hexagon::V6_vL32b_nt_pi;
4021 case Hexagon::V6_vL32b_nt_cur_ai:
4022 return Hexagon::V6_vL32b_nt_ai;
4023 case Hexagon::V6_vL32b_cur_ppu:
4024 return Hexagon::V6_vL32b_ppu;
4025 case Hexagon::V6_vL32b_nt_cur_ppu:
4026 return Hexagon::V6_vL32b_nt_ppu;
4027 }
4028 return 0;
4029}
4030
4031// The diagram below shows the steps involved in the conversion of a predicated
4032// store instruction to its .new predicated new-value form.
4033//
4034// Note: It doesn't include conditional new-value stores as they can't be
4035// converted to .new predicate.
4036//
4037// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
4038// ^ ^
4039// / \ (not OK. it will cause new-value store to be
4040// / X conditional on p0.new while R2 producer is
4041// / \ on p0)
4042// / \.
4043// p.new store p.old NV store
4044// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
4045// ^ ^
4046// \ /
4047// \ /
4048// \ /
4049// p.old store
4050// [if (p0)memw(R0+#0)=R2]
4051//
4052// The following set of instructions further explains the scenario where
4053// conditional new-value store becomes invalid when promoted to .new predicate
4054// form.
4055//
4056// { 1) if (p0) r0 = add(r1, r2)
4057// 2) p0 = cmp.eq(r3, #0) }
4058//
4059// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
4060// the first two instructions because in instr 1, r0 is conditional on old value
4061// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
4062// is not valid for new-value stores.
4063// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
4064// from the "Conditional Store" list. Because a predicated new value store
4065// would NOT be promoted to a double dot new store. See diagram below:
4066// This function returns yes for those stores that are predicated but not
4067// yet promoted to predicate dot new instructions.
4068//
4069// +---------------------+
4070// /-----| if (p0) memw(..)=r0 |---------\~
4071// || +---------------------+ ||
4072// promote || /\ /\ || promote
4073// || /||\ /||\ ||
4074// \||/ demote || \||/
4075// \/ || || \/
4076// +-------------------------+ || +-------------------------+
4077// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
4078// +-------------------------+ || +-------------------------+
4079// || || ||
4080// || demote \||/
4081// promote || \/ NOT possible
4082// || || /\~
4083// \||/ || /||\~
4084// \/ || ||
4085// +-----------------------------+
4086// | if (p0.new) memw(..)=r0.new |
4087// +-----------------------------+
4088// Double Dot New Store
4089//
4090// Returns the most basic instruction for the .new predicated instructions and
4091// new-value stores.
4092// For example, all of the following instructions will be converted back to the
4093// same instruction:
4094// 1) if (p0.new) memw(R0+#0) = R1.new --->
4095// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
4096// 3) if (p0.new) memw(R0+#0) = R1 --->
4097//
4098// To understand the translation of instruction 1 to its original form, consider
4099// a packet with 3 instructions.
4100// { p0 = cmp.eq(R0,R1)
4101// if (p0.new) R2 = add(R3, R4)
4102// R5 = add (R3, R1)
4103// }
4104// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
4105//
4106// This instruction can be part of the previous packet only if both p0 and R2
4107// are promoted to .new values. This promotion happens in steps, first
4108// predicate register is promoted to .new and in the next iteration R2 is
4109// promoted. Therefore, in case of dependence check failure (due to R5) during
4110// next iteration, it should be converted back to its most basic form.
4111
4112// Return the new value instruction for a given store.
4114 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
4115 if (NVOpcode >= 0) // Valid new-value store instruction.
4116 return NVOpcode;
4117
4118 switch (MI.getOpcode()) {
4119 default:
4120 report_fatal_error(Twine("Unknown .new type: ") +
4121 std::to_string(MI.getOpcode()));
4122 case Hexagon::S4_storerb_ur:
4123 return Hexagon::S4_storerbnew_ur;
4124
4125 case Hexagon::S2_storerb_pci:
4126 return Hexagon::S2_storerb_pci;
4127
4128 case Hexagon::S2_storeri_pci:
4129 return Hexagon::S2_storeri_pci;
4130
4131 case Hexagon::S2_storerh_pci:
4132 return Hexagon::S2_storerh_pci;
4133
4134 case Hexagon::S2_storerd_pci:
4135 return Hexagon::S2_storerd_pci;
4136
4137 case Hexagon::S2_storerf_pci:
4138 return Hexagon::S2_storerf_pci;
4139
4140 case Hexagon::V6_vS32b_ai:
4141 return Hexagon::V6_vS32b_new_ai;
4142
4143 case Hexagon::V6_vS32b_pi:
4144 return Hexagon::V6_vS32b_new_pi;
4145 }
4146 return 0;
4147}
4148
4149// Returns the opcode to use when converting MI, which is a conditional jump,
4150// into a conditional instruction which uses the .new value of the predicate.
4151// We also use branch probabilities to add a hint to the jump.
4152// If MBPI is null, all edges will be treated as equally likely for the
4153// purposes of establishing a predication hint.
4155 const MachineBranchProbabilityInfo *MBPI) const {
4156 // We assume that block can have at most two successors.
4157 const MachineBasicBlock *Src = MI.getParent();
4158 const MachineOperand &BrTarget = MI.getOperand(1);
4159 bool Taken = false;
4160 const BranchProbability OneHalf(1, 2);
4161
4162 auto getEdgeProbability = [MBPI] (const MachineBasicBlock *Src,
4163 const MachineBasicBlock *Dst) {
4164 if (MBPI)
4165 return MBPI->getEdgeProbability(Src, Dst);
4166 return BranchProbability(1, Src->succ_size());
4167 };
4168
4169 if (BrTarget.isMBB()) {
4170 const MachineBasicBlock *Dst = BrTarget.getMBB();
4171 Taken = getEdgeProbability(Src, Dst) >= OneHalf;
4172 } else {
4173 // The branch target is not a basic block (most likely a function).
4174 // Since BPI only gives probabilities for targets that are basic blocks,
4175 // try to identify another target of this branch (potentially a fall-
4176 // -through) and check the probability of that target.
4177 //
4178 // The only handled branch combinations are:
4179 // - one conditional branch,
4180 // - one conditional branch followed by one unconditional branch.
4181 // Otherwise, assume not-taken.
4182 assert(MI.isConditionalBranch());
4183 const MachineBasicBlock &B = *MI.getParent();
4184 bool SawCond = false, Bad = false;
4185 for (const MachineInstr &I : B) {
4186 if (!I.isBranch())
4187 continue;
4188 if (I.isConditionalBranch()) {
4189 SawCond = true;
4190 if (&I != &MI) {
4191 Bad = true;
4192 break;
4193 }
4194 }
4195 if (I.isUnconditionalBranch() && !SawCond) {
4196 Bad = true;
4197 break;
4198 }
4199 }
4200 if (!Bad) {
4202 MachineBasicBlock::const_instr_iterator NextIt = std::next(It);
4203 if (NextIt == B.instr_end()) {
4204 // If this branch is the last, look for the fall-through block.
4205 for (const MachineBasicBlock *SB : B.successors()) {
4206 if (!B.isLayoutSuccessor(SB))
4207 continue;
4208 Taken = getEdgeProbability(Src, SB) < OneHalf;
4209 break;
4210 }
4211 } else {
4212 assert(NextIt->isUnconditionalBranch());
4213 // Find the first MBB operand and assume it's the target.
4214 const MachineBasicBlock *BT = nullptr;
4215 for (const MachineOperand &Op : NextIt->operands()) {
4216 if (!Op.isMBB())
4217 continue;
4218 BT = Op.getMBB();
4219 break;
4220 }
4221 Taken = BT && getEdgeProbability(Src, BT) < OneHalf;
4222 }
4223 } // if (!Bad)
4224 }
4225
4226 // The Taken flag should be set to something reasonable by this point.
4227
4228 switch (MI.getOpcode()) {
4229 case Hexagon::J2_jumpt:
4230 return Taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
4231 case Hexagon::J2_jumpf:
4232 return Taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
4233
4234 default:
4235 llvm_unreachable("Unexpected jump instruction.");
4236 }
4237}
4238
4239// Return .new predicate version for an instruction.
4241 const MachineBranchProbabilityInfo *MBPI) const {
4242 switch (MI.getOpcode()) {
4243 // Conditional Jumps
4244 case Hexagon::J2_jumpt:
4245 case Hexagon::J2_jumpf:
4246 return getDotNewPredJumpOp(MI, MBPI);
4247 }
4248
4249 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
4250 if (NewOpcode >= 0)
4251 return NewOpcode;
4252 return 0;
4253}
4254
4256 int NewOp = MI.getOpcode();
4257 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
4258 NewOp = Hexagon::getPredOldOpcode(NewOp);
4259 // All Hexagon architectures have prediction bits on dot-new branches,
4260 // but only Hexagon V60+ has prediction bits on dot-old ones. Make sure
4261 // to pick the right opcode when converting back to dot-old.
4262 if (!Subtarget.hasFeature(Hexagon::ArchV60)) {
4263 switch (NewOp) {
4264 case Hexagon::J2_jumptpt:
4265 NewOp = Hexagon::J2_jumpt;
4266 break;
4267 case Hexagon::J2_jumpfpt:
4268 NewOp = Hexagon::J2_jumpf;
4269 break;
4270 case Hexagon::J2_jumprtpt:
4271 NewOp = Hexagon::J2_jumprt;
4272 break;
4273 case Hexagon::J2_jumprfpt:
4274 NewOp = Hexagon::J2_jumprf;
4275 break;
4276 }
4277 }
4278 assert(NewOp >= 0 &&
4279 "Couldn't change predicate new instruction to its old form.");
4280 }
4281
4282 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
4283 NewOp = Hexagon::getNonNVStore(NewOp);
4284 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
4285 }
4286
4287 if (Subtarget.hasV60Ops())
4288 return NewOp;
4289
4290 // Subtargets prior to V60 didn't support 'taken' forms of predicated jumps.
4291 switch (NewOp) {
4292 case Hexagon::J2_jumpfpt:
4293 return Hexagon::J2_jumpf;
4294 case Hexagon::J2_jumptpt:
4295 return Hexagon::J2_jumpt;
4296 case Hexagon::J2_jumprfpt:
4297 return Hexagon::J2_jumprf;
4298 case Hexagon::J2_jumprtpt:
4299 return Hexagon::J2_jumprt;
4300 }
4301 return NewOp;
4302}
4303
4304// See if instruction could potentially be a duplex candidate.
4305// If so, return its group. Zero otherwise.
4307 const MachineInstr &MI) const {
4308 Register DstReg, SrcReg, Src1Reg, Src2Reg;
4309 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
4310
4311 switch (MI.getOpcode()) {
4312 default:
4313 return HexagonII::HSIG_None;
4314 //
4315 // Group L1:
4316 //
4317 // Rd = memw(Rs+#u4:2)
4318 // Rd = memub(Rs+#u4:0)
4319 case Hexagon::L2_loadri_io:
4320 case Hexagon::dup_L2_loadri_io:
4321 DstReg = MI.getOperand(0).getReg();
4322 SrcReg = MI.getOperand(1).getReg();
4323 // Special case this one from Group L2.
4324 // Rd = memw(r29+#u5:2)
4325 if (isIntRegForSubInst(DstReg)) {
4326 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
4327 HRI.getStackRegister() == SrcReg &&
4328 MI.getOperand(2).isImm() &&
4329 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
4330 return HexagonII::HSIG_L2;
4331 // Rd = memw(Rs+#u4:2)
4332 if (isIntRegForSubInst(SrcReg) &&
4333 (MI.getOperand(2).isImm() &&
4334 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
4335 return HexagonII::HSIG_L1;
4336 }
4337 break;
4338 case Hexagon::L2_loadrub_io:
4339 case Hexagon::dup_L2_loadrub_io:
4340 // Rd = memub(Rs+#u4:0)
4341 DstReg = MI.getOperand(0).getReg();
4342 SrcReg = MI.getOperand(1).getReg();
4343 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
4344 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
4345 return HexagonII::HSIG_L1;
4346 break;
4347 //
4348 // Group L2:
4349 //
4350 // Rd = memh/memuh(Rs+#u3:1)
4351 // Rd = memb(Rs+#u3:0)
4352 // Rd = memw(r29+#u5:2) - Handled above.
4353 // Rdd = memd(r29+#u5:3)
4354 // deallocframe
4355 // [if ([!]p0[.new])] dealloc_return
4356 // [if ([!]p0[.new])] jumpr r31
4357 case Hexagon::L2_loadrh_io:
4358 case Hexagon::L2_loadruh_io:
4359 case Hexagon::dup_L2_loadrh_io:
4360 case Hexagon::dup_L2_loadruh_io:
4361 // Rd = memh/memuh(Rs+#u3:1)
4362 DstReg = MI.getOperand(0).getReg();
4363 SrcReg = MI.getOperand(1).getReg();
4364 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
4365 MI.getOperand(2).isImm() &&
4366 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
4367 return HexagonII::HSIG_L2;
4368 break;
4369 case Hexagon::L2_loadrb_io:
4370 case Hexagon::dup_L2_loadrb_io:
4371 // Rd = memb(Rs+#u3:0)
4372 DstReg = MI.getOperand(0).getReg();
4373 SrcReg = MI.getOperand(1).getReg();
4374 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
4375 MI.getOperand(2).isImm() &&
4376 isUInt<3>(MI.getOperand(2).getImm()))
4377 return HexagonII::HSIG_L2;
4378 break;
4379 case Hexagon::L2_loadrd_io:
4380 case Hexagon::dup_L2_loadrd_io:
4381 // Rdd = memd(r29+#u5:3)
4382 DstReg = MI.getOperand(0).getReg();
4383 SrcReg = MI.getOperand(1).getReg();
4384 if (isDblRegForSubInst(DstReg, HRI) &&
4385 Hexagon::IntRegsRegClass.contains(SrcReg) &&
4386 HRI.getStackRegister() == SrcReg &&
4387 MI.getOperand(2).isImm() &&
4388 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
4389 return HexagonII::HSIG_L2;
4390 break;
4391 // dealloc_return is not documented in Hexagon Manual, but marked
4392 // with A_SUBINSN attribute in iset_v4classic.py.
4393 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
4394 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
4395 case Hexagon::L4_return:
4396 case Hexagon::L2_deallocframe:
4397 case Hexagon::dup_L2_deallocframe:
4398 return HexagonII::HSIG_L2;
4399 case Hexagon::EH_RETURN_JMPR:
4400 case Hexagon::PS_jmpret:
4401 case Hexagon::SL2_jumpr31:
4402 // jumpr r31
4403 // Actual form JMPR implicit-def %pc, implicit %r31, implicit internal %r0
4404 DstReg = MI.getOperand(0).getReg();
4405 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
4406 return HexagonII::HSIG_L2;
4407 break;
4408 case Hexagon::PS_jmprett:
4409 case Hexagon::PS_jmpretf:
4410 case Hexagon::PS_jmprettnewpt:
4411 case Hexagon::PS_jmpretfnewpt:
4412 case Hexagon::PS_jmprettnew:
4413 case Hexagon::PS_jmpretfnew:
4414 case Hexagon::SL2_jumpr31_t:
4415 case Hexagon::SL2_jumpr31_f:
4416 case Hexagon::SL2_jumpr31_tnew:
4417 case Hexagon::SL2_jumpr31_fnew:
4418 DstReg = MI.getOperand(1).getReg();
4419 SrcReg = MI.getOperand(0).getReg();
4420 // [if ([!]p0[.new])] jumpr r31
4421 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
4422 (Hexagon::P0 == SrcReg)) &&
4423 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
4424 return HexagonII::HSIG_L2;
4425 break;
4426 case Hexagon::L4_return_t:
4427 case Hexagon::L4_return_f:
4428 case Hexagon::L4_return_tnew_pnt:
4429 case Hexagon::L4_return_fnew_pnt:
4430 case Hexagon::L4_return_tnew_pt:
4431 case Hexagon::L4_return_fnew_pt:
4432 // [if ([!]p0[.new])] dealloc_return
4433 SrcReg = MI.getOperand(0).getReg();
4434 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
4435 return HexagonII::HSIG_L2;
4436 break;
4437 //
4438 // Group S1:
4439 //
4440 // memw(Rs+#u4:2) = Rt
4441 // memb(Rs+#u4:0) = Rt
4442 case Hexagon::S2_storeri_io:
4443 case Hexagon::dup_S2_storeri_io:
4444 // Special case this one from Group S2.
4445 // memw(r29+#u5:2) = Rt
4446 Src1Reg = MI.getOperand(0).getReg();
4447 Src2Reg = MI.getOperand(2).getReg();
4448 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
4449 isIntRegForSubInst(Src2Reg) &&
4450 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
4451 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
4452 return HexagonII::HSIG_S2;
4453 // memw(Rs+#u4:2) = Rt
4454 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
4455 MI.getOperand(1).isImm() &&
4456 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
4457 return HexagonII::HSIG_S1;
4458 break;
4459 case Hexagon::S2_storerb_io:
4460 case Hexagon::dup_S2_storerb_io:
4461 // memb(Rs+#u4:0) = Rt
4462 Src1Reg = MI.getOperand(0).getReg();
4463 Src2Reg = MI.getOperand(2).getReg();
4464 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
4465 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
4466 return HexagonII::HSIG_S1;
4467 break;
4468 //
4469 // Group S2:
4470 //
4471 // memh(Rs+#u3:1) = Rt
4472 // memw(r29+#u5:2) = Rt
4473 // memd(r29+#s6:3) = Rtt
4474 // memw(Rs+#u4:2) = #U1
4475 // memb(Rs+#u4) = #U1
4476 // allocframe(#u5:3)
4477 case Hexagon::S2_storerh_io:
4478 case Hexagon::dup_S2_storerh_io:
4479 // memh(Rs+#u3:1) = Rt
4480 Src1Reg = MI.getOperand(0).getReg();
4481 Src2Reg = MI.getOperand(2).getReg();
4482 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
4483 MI.getOperand(1).isImm() &&
4484 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
4485 return HexagonII::HSIG_S1;
4486 break;
4487 case Hexagon::S2_storerd_io:
4488 case Hexagon::dup_S2_storerd_io:
4489 // memd(r29+#s6:3) = Rtt
4490 Src1Reg = MI.getOperand(0).getReg();
4491 Src2Reg = MI.getOperand(2).getReg();
4492 if (isDblRegForSubInst(Src2Reg, HRI) &&
4493 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
4494 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
4495 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
4496 return HexagonII::HSIG_S2;
4497 break;
4498 case Hexagon::S4_storeiri_io:
4499 case Hexagon::dup_S4_storeiri_io:
4500 // memw(Rs+#u4:2) = #U1
4501 Src1Reg = MI.getOperand(0).getReg();
4502 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
4503 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
4504 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
4505 return HexagonII::HSIG_S2;
4506 break;
4507 case Hexagon::S4_storeirb_io:
4508 case Hexagon::dup_S4_storeirb_io:
4509 // memb(Rs+#u4) = #U1
4510 Src1Reg = MI.getOperand(0).getReg();
4511 if (isIntRegForSubInst(Src1Reg) &&
4512 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
4513 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
4514 return HexagonII::HSIG_S2;
4515 break;
4516 case Hexagon::S2_allocframe:
4517 case Hexagon::dup_S2_allocframe:
4518 if (MI.getOperand(2).isImm() &&
4519 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
4520 return HexagonII::HSIG_S1;
4521 break;
4522 //
4523 // Group A:
4524 //
4525 // Rx = add(Rx,#s7)
4526 // Rd = Rs
4527 // Rd = #u6
4528 // Rd = #-1
4529 // if ([!]P0[.new]) Rd = #0
4530 // Rd = add(r29,#u6:2)
4531 // Rx = add(Rx,Rs)
4532 // P0 = cmp.eq(Rs,#u2)
4533 // Rdd = combine(#0,Rs)
4534 // Rdd = combine(Rs,#0)
4535 // Rdd = combine(#u2,#U2)
4536 // Rd = add(Rs,#1)
4537 // Rd = add(Rs,#-1)
4538 // Rd = sxth/sxtb/zxtb/zxth(Rs)
4539 // Rd = and(Rs,#1)
4540 case Hexagon::A2_addi:
4541 case Hexagon::dup_A2_addi:
4542 DstReg = MI.getOperand(0).getReg();
4543 SrcReg = MI.getOperand(1).getReg();
4544 if (isIntRegForSubInst(DstReg)) {
4545 // Rd = add(r29,#u6:2)
4546 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
4547 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
4548 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
4549 return HexagonII::HSIG_A;
4550 // Rx = add(Rx,#s7)
4551 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
4552 isInt<7>(MI.getOperand(2).getImm()))
4553 return HexagonII::HSIG_A;
4554 // Rd = add(Rs,#1)
4555 // Rd = add(Rs,#-1)
4556 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
4557 ((MI.getOperand(2).getImm() == 1) ||
4558 (MI.getOperand(2).getImm() == -1)))
4559 return HexagonII::HSIG_A;
4560 }
4561 break;
4562 case Hexagon::A2_add:
4563 case Hexagon::dup_A2_add:
4564 // Rx = add(Rx,Rs)
4565 DstReg = MI.getOperand(0).getReg();
4566 Src1Reg = MI.getOperand(1).getReg();
4567 Src2Reg = MI.getOperand(2).getReg();
4568 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
4569 isIntRegForSubInst(Src2Reg))
4570 return HexagonII::HSIG_A;
4571 break;
4572 case Hexagon::A2_andir:
4573 case Hexagon::dup_A2_andir:
4574 // Same as zxtb.
4575 // Rd16=and(Rs16,#255)
4576 // Rd16=and(Rs16,#1)
4577 DstReg = MI.getOperand(0).getReg();
4578 SrcReg = MI.getOperand(1).getReg();
4579 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
4580 MI.getOperand(2).isImm() &&
4581 ((MI.getOperand(2).getImm() == 1) ||
4582 (MI.getOperand(2).getImm() == 255)))
4583 return HexagonII::HSIG_A;
4584 break;
4585 case Hexagon::A2_tfr:
4586 case Hexagon::dup_A2_tfr:
4587 // Rd = Rs
4588 DstReg = MI.getOperand(0).getReg();
4589 SrcReg = MI.getOperand(1).getReg();
4590 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
4591 return HexagonII::HSIG_A;
4592 break;
4593 case Hexagon::A2_tfrsi:
4594 case Hexagon::dup_A2_tfrsi:
4595 // Rd = #u6
4596 // Do not test for #u6 size since the const is getting extended
4597 // regardless and compound could be formed.
4598 // Rd = #-1
4599 DstReg = MI.getOperand(0).getReg();
4600 if (isIntRegForSubInst(DstReg))
4601 return HexagonII::HSIG_A;
4602 break;
4603 case Hexagon::C2_cmoveit:
4604 case Hexagon::C2_cmovenewit:
4605 case Hexagon::C2_cmoveif:
4606 case Hexagon::C2_cmovenewif:
4607 case Hexagon::dup_C2_cmoveit:
4608 case Hexagon::dup_C2_cmovenewit:
4609 case Hexagon::dup_C2_cmoveif:
4610 case Hexagon::dup_C2_cmovenewif:
4611 // if ([!]P0[.new]) Rd = #0
4612 // Actual form:
4613 // %r16 = C2_cmovenewit internal %p0, 0, implicit undef %r16;
4614 DstReg = MI.getOperand(0).getReg();
4615 SrcReg = MI.getOperand(1).getReg();
4616 if (isIntRegForSubInst(DstReg) &&
4617 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
4618 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
4619 return HexagonII::HSIG_A;
4620 break;
4621 case Hexagon::C2_cmpeqi:
4622 case Hexagon::dup_C2_cmpeqi:
4623 // P0 = cmp.eq(Rs,#u2)
4624 DstReg = MI.getOperand(0).getReg();
4625 SrcReg = MI.getOperand(1).getReg();
4626 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
4627 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
4628 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
4629 return HexagonII::HSIG_A;
4630 break;
4631 case Hexagon::A2_combineii:
4632 case Hexagon::A4_combineii:
4633 case Hexagon::dup_A2_combineii:
4634 case Hexagon::dup_A4_combineii:
4635 // Rdd = combine(#u2,#U2)
4636 DstReg = MI.getOperand(0).getReg();
4637 if (isDblRegForSubInst(DstReg, HRI) &&
4638 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
4639 (MI.getOperand(1).isGlobal() &&
4640 isUInt<2>(MI.getOperand(1).getOffset()))) &&
4641 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
4642 (MI.getOperand(2).isGlobal() &&
4643 isUInt<2>(MI.getOperand(2).getOffset()))))
4644 return HexagonII::HSIG_A;
4645 break;
4646 case Hexagon::A4_combineri:
4647 case Hexagon::dup_A4_combineri:
4648 // Rdd = combine(Rs,#0)
4649 // Rdd = combine(Rs,#0)
4650 DstReg = MI.getOperand(0).getReg();
4651 SrcReg = MI.getOperand(1).getReg();
4652 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
4653 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
4654 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
4655 return HexagonII::HSIG_A;
4656 break;
4657 case Hexagon::A4_combineir:
4658 case Hexagon::dup_A4_combineir:
4659 // Rdd = combine(#0,Rs)
4660 DstReg = MI.getOperand(0).getReg();
4661 SrcReg = MI.getOperand(2).getReg();
4662 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
4663 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
4664 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
4665 return HexagonII::HSIG_A;
4666 break;
4667 case Hexagon::A2_sxtb:
4668 case Hexagon::A2_sxth:
4669 case Hexagon::A2_zxtb:
4670 case Hexagon::A2_zxth:
4671 case Hexagon::dup_A2_sxtb:
4672 case Hexagon::dup_A2_sxth:
4673 case Hexagon::dup_A2_zxtb:
4674 case Hexagon::dup_A2_zxth:
4675 // Rd = sxth/sxtb/zxtb/zxth(Rs)
4676 DstReg = MI.getOperand(0).getReg();
4677 SrcReg = MI.getOperand(1).getReg();
4678 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
4679 return HexagonII::HSIG_A;
4680 break;
4681 }
4682
4683 return HexagonII::HSIG_None;
4684}
4685
4687 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
4688 // Default to one cycle for no itinerary. However, an "empty" itinerary may
4689 // still have a MinLatency property, which getStageLatency checks.
4690 if (!ItinData)
4691 return getInstrLatency(ItinData, MI);
4692
4693 if (MI.isTransient())
4694 return 0;
4695 return ItinData->getStageLatency(MI.getDesc().getSchedClass());
4696}
4697
4698/// getOperandLatency - Compute and return the use operand latency of a given
4699/// pair of def and use.
4700/// In most cases, the static scheduling itinerary was enough to determine the
4701/// operand latency. But it may not be possible for instructions with variable
4702/// number of defs / uses.
4703///
4704/// This is a raw interface to the itinerary that may be directly overridden by
4705/// a target. Use computeOperandLatency to get the best estimate of latency.
4707 const InstrItineraryData *ItinData, const MachineInstr &DefMI,
4708 unsigned DefIdx, const MachineInstr &UseMI, unsigned UseIdx) const {
4709 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
4710
4711 // Get DefIdx and UseIdx for super registers.
4712 const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
4713
4714 if (DefMO.isReg() && DefMO.getReg().isPhysical()) {
4715 if (DefMO.isImplicit()) {
4716 for (MCPhysReg SR : HRI.superregs(DefMO.getReg())) {
4717 int Idx = DefMI.findRegisterDefOperandIdx(SR, &HRI, false, false);
4718 if (Idx != -1) {
4719 DefIdx = Idx;
4720 break;
4721 }
4722 }
4723 }
4724
4725 const MachineOperand &UseMO = UseMI.getOperand(UseIdx);
4726 if (UseMO.isImplicit()) {
4727 for (MCPhysReg SR : HRI.superregs(UseMO.getReg())) {
4728 int Idx = UseMI.findRegisterUseOperandIdx(SR, &HRI, false);
4729 if (Idx != -1) {
4730 UseIdx = Idx;
4731 break;
4732 }
4733 }
4734 }
4735 }
4736
4737 std::optional<unsigned> Latency = TargetInstrInfo::getOperandLatency(
4738 ItinData, DefMI, DefIdx, UseMI, UseIdx);
4739 if (Latency == 0)
4740 // We should never have 0 cycle latency between two instructions unless
4741 // they can be packetized together. However, this decision can't be made
4742 // here.
4743 Latency = 1;
4744 return Latency;
4745}
4746
4747// inverts the predication logic.
4748// p -> NotP
4749// NotP -> P
4752 if (Cond.empty())
4753 return false;
4754 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
4755 Cond[0].setImm(Opc);
4756 return true;
4757}
4758
4760 int InvPredOpcode;
4761 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
4762 : Hexagon::getTruePredOpcode(Opc);
4763 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
4764 return InvPredOpcode;
4765
4766 llvm_unreachable("Unexpected predicated instruction");
4767}
4768
4769// Returns the max value that doesn't need to be extended.
4771 const uint64_t F = MI.getDesc().TSFlags;
4772 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4774 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4776
4777 if (isSigned) // if value is signed
4778 return ~(-1U << (bits - 1));
4779 else
4780 return ~(-1U << bits);
4781}
4782
4783
4785 switch (MI.getOpcode()) {
4786 case Hexagon::L2_loadrbgp:
4787 case Hexagon::L2_loadrdgp:
4788 case Hexagon::L2_loadrhgp:
4789 case Hexagon::L2_loadrigp:
4790 case Hexagon::L2_loadrubgp:
4791 case Hexagon::L2_loadruhgp:
4792 case Hexagon::S2_storerbgp:
4793 case Hexagon::S2_storerbnewgp:
4794 case Hexagon::S2_storerhgp:
4795 case Hexagon::S2_storerhnewgp:
4796 case Hexagon::S2_storerigp:
4797 case Hexagon::S2_storerinewgp:
4798 case Hexagon::S2_storerdgp:
4799 case Hexagon::S2_storerfgp:
4800 return true;
4801 }
4802 const uint64_t F = MI.getDesc().TSFlags;
4803 unsigned addrMode =
4805 // Disallow any base+offset instruction. The assembler does not yet reorder
4806 // based up any zero offset instruction.
4807 return (addrMode == HexagonII::BaseRegOffset ||
4808 addrMode == HexagonII::BaseImmOffset ||
4809 addrMode == HexagonII::BaseLongOffset);
4810}
4811
4813 // Workaround for the Global Scheduler. Sometimes, it creates
4814 // A4_ext as a Pseudo instruction and calls this function to see if
4815 // it can be added to an existing bundle. Since the instruction doesn't
4816 // belong to any BB yet, we can't use getUnits API.
4817 if (MI.getOpcode() == Hexagon::A4_ext)
4818 return false;
4819
4820 unsigned FuncUnits = getUnits(MI);
4821 return HexagonFUnits::isSlot0Only(FuncUnits);
4822}
4823
4825 const uint64_t F = MI.getDesc().TSFlags;
4828}
4829
4831 bool ToBigInstrs) const {
4832 int Opcode = -1;
4833 if (ToBigInstrs) { // To BigCore Instr.
4834 // Check if the instruction can form a Duplex.
4835 if (getDuplexCandidateGroup(*MII))
4836 // Get the opcode marked "dup_*" tag.
4837 Opcode = getDuplexOpcode(*MII, ToBigInstrs);
4838 } else // To TinyCore Instr.
4839 Opcode = getDuplexOpcode(*MII, ToBigInstrs);
4840
4841 // Change the opcode of the instruction.
4842 if (Opcode >= 0)
4843 MII->setDesc(get(Opcode));
4844}
4845
4846// This function is used to translate instructions to facilitate generating
4847// Duplexes on TinyCore.
4849 bool ToBigInstrs) const {
4850 for (auto &MB : MF)
4851 for (MachineBasicBlock::instr_iterator Instr = MB.instr_begin(),
4852 End = MB.instr_end();
4853 Instr != End; ++Instr)
4854 changeDuplexOpcode(Instr, ToBigInstrs);
4855}
4856
4857// This is a specialized form of above function.
4859 MachineBasicBlock::instr_iterator MII, bool ToBigInstrs) const {
4860 MachineBasicBlock *MBB = MII->getParent();
4861 while ((MII != MBB->instr_end()) && MII->isInsideBundle()) {
4862 changeDuplexOpcode(MII, ToBigInstrs);
4863 ++MII;
4864 }
4865}
4866
4868 using namespace HexagonII;
4869
4870 const uint64_t F = MI.getDesc().TSFlags;
4871 unsigned S = (F >> MemAccessSizePos) & MemAccesSizeMask;
4872 unsigned Size = getMemAccessSizeInBytes(MemAccessSize(S));
4873 if (Size != 0)
4874 return Size;
4875 // Y2_dcfetchbo is special
4876 if (MI.getOpcode() == Hexagon::Y2_dcfetchbo)
4878
4879 // Handle vector access sizes.
4880 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
4881 switch (S) {
4883 return HRI.getSpillSize(Hexagon::HvxVRRegClass);
4885 return HRI.getSpillSize(Hexagon::HlxLRRegClass);
4886 default:
4887 llvm_unreachable("Unexpected instruction");
4888 }
4889}
4890
4891// Returns the min value that doesn't need to be extended.
4893 const uint64_t F = MI.getDesc().TSFlags;
4894 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4896 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4898
4899 if (isSigned) // if value is signed
4900 return -1U << (bits - 1);
4901 else
4902 return 0;
4903}
4904
4905// Returns opcode of the non-extended equivalent instruction.
4907 // Check if the instruction has a register form that uses register in place
4908 // of the extended operand, if so return that as the non-extended form.
4909 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
4910 if (NonExtOpcode >= 0)
4911 return NonExtOpcode;
4912
4913 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
4914 // Check addressing mode and retrieve non-ext equivalent instruction.
4915 switch (getAddrMode(MI)) {
4917 return Hexagon::changeAddrMode_abs_io(MI.getOpcode());
4919 return Hexagon::changeAddrMode_io_rr(MI.getOpcode());
4921 return Hexagon::changeAddrMode_ur_rr(MI.getOpcode());
4922
4923 default:
4924 return -1;
4925 }
4926 }
4927 return -1;
4928}
4929
4931 Register &PredReg, unsigned &PredRegPos,
4932 RegState &PredRegFlags) const {
4933 if (Cond.empty())
4934 return false;
4935 assert(Cond.size() == 2);
4936 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
4937 LLVM_DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
4938 return false;
4939 }
4940 PredReg = Cond[1].getReg();
4941 PredRegPos = 1;
4942 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4943 PredRegFlags = {};
4944 if (Cond[1].isImplicit())
4945 PredRegFlags = RegState::Implicit;
4946 if (Cond[1].isUndef())
4947 PredRegFlags |= RegState::Undef;
4948 return true;
4949}
4950
4952 return Hexagon::getRegForm(MI.getOpcode());
4953}
4954
4955// Return the number of bytes required to encode the instruction.
4956// Hexagon instructions are fixed length, 4 bytes, unless they
4957// use a constant extender, which requires another 4 bytes.
4958// For debug instructions and prolog labels, return 0.
4960 if (MI.isDebugInstr() || MI.isPosition())
4961 return 0;
4962
4963 unsigned Size = MI.getDesc().getSize();
4964 if (!Size)
4965 // Assume the default insn size in case it cannot be determined
4966 // for whatever reason.
4968
4971
4972 // Try and compute number of instructions in asm.
4973 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4974 const MachineBasicBlock &MBB = *MI.getParent();
4975 const MachineFunction *MF = MBB.getParent();
4976 const MCAsmInfo &MAI = MF->getTarget().getMCAsmInfo();
4977
4978 // Count the number of register definitions to find the asm string.
4979 unsigned NumDefs = 0;
4980 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
4981 ++NumDefs)
4982 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
4983
4984 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
4985 // Disassemble the AsmStr and approximate number of instructions.
4986 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
4987 Size = getInlineAsmLength(AsmStr, MAI);
4988 }
4989
4990 return Size;
4991}
4992
4994 const uint64_t F = MI.getDesc().TSFlags;
4996}
4997
4999 const InstrItineraryData &II = *Subtarget.getInstrItineraryData();
5000 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
5001
5002 return IS.getUnits();
5003}
5004
5005// Calculate size of the basic block without debug instructions.
5007 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
5008}
5009
5011 MachineBasicBlock::const_iterator BundleHead) const {
5012 assert(BundleHead->isBundle() && "Not a bundle header");
5013 auto MII = BundleHead.getInstrIterator();
5014 // Skip the bundle header.
5015 return nonDbgMICount(++MII, getBundleEnd(BundleHead.getInstrIterator()));
5016}
5017
5018/// immediateExtend - Changes the instruction in place to one using an immediate
5019/// extender.
5022 "Instruction must be extendable");
5023 // Find which operand is extendable.
5024 short ExtOpNum = getCExtOpNum(MI);
5025 MachineOperand &MO = MI.getOperand(ExtOpNum);
5026 // This needs to be something we understand.
5027 assert((MO.isMBB() || MO.isImm()) &&
5028 "Branch with unknown extendable field type");
5029 // Mark given operand as extended.
5031}
5032
5034 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
5035 LLVM_DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to "
5036 << printMBBReference(*NewTarget);
5037 MI.dump(););
5038 assert(MI.isBranch());
5039 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
5040 int TargetPos = MI.getNumOperands() - 1;
5041 // In general branch target is the last operand,
5042 // but some implicit defs added at the end might change it.
5043 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
5044 --TargetPos;
5045 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
5046 MI.getOperand(TargetPos).setMBB(NewTarget);
5048 NewOpcode = reversePrediction(NewOpcode);
5049 }
5050 MI.setDesc(get(NewOpcode));
5051 return true;
5052}
5053
5055 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
5057 MachineBasicBlock &B = *A;
5059 DebugLoc DL = I->getDebugLoc();
5060 MachineInstr *NewMI;
5061
5062 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
5063 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
5064 NewMI = BuildMI(B, I, DL, get(insn));
5065 LLVM_DEBUG(dbgs() << "\n"
5066 << getName(NewMI->getOpcode())
5067 << " Class: " << NewMI->getDesc().getSchedClass());
5068 NewMI->eraseFromParent();
5069 }
5070 /* --- The code above is used to generate complete set of Hexagon Insn --- */
5071}
5072
5073// inverts the predication logic.
5074// p -> NotP
5075// NotP -> P
5077 LLVM_DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
5078 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
5079 return true;
5080}
5081
5082// Reverse the branch prediction.
5083unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
5084 int PredRevOpcode = -1;
5085 if (isPredictedTaken(Opcode))
5086 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
5087 else
5088 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
5089 assert(PredRevOpcode > 0);
5090 return PredRevOpcode;
5091}
5092
5093// TODO: Add more rigorous validation.
5095 const {
5096 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
5097}
5098
5101 assert(MIB->isBundle());
5102 MachineOperand &Operand = MIB->getOperand(0);
5103 if (Operand.isImm())
5104 Operand.setImm(Operand.getImm() | memShufDisabledMask);
5105 else
5106 MIB->addOperand(MachineOperand::CreateImm(memShufDisabledMask));
5107}
5108
5110 assert(MIB.isBundle());
5111 const MachineOperand &Operand = MIB.getOperand(0);
5112 return (Operand.isImm() && (Operand.getImm() & memShufDisabledMask) != 0);
5113}
5114
5116 return (MI->getOpcode() == Hexagon::V6_vmpy_qf16_hf ||
5117 MI->getOpcode() == Hexagon::V6_vmpy_qf16_mix_hf ||
5118 MI->getOpcode() == Hexagon::V6_vmpy_qf32_hf ||
5119 MI->getOpcode() == Hexagon::V6_vmpy_qf32_mix_hf ||
5120 MI->getOpcode() == Hexagon::V6_vmpy_qf32_sf ||
5121 MI->getOpcode() == Hexagon::V6_vmpy_qf16_mix_hf ||
5122 MI->getOpcode() == Hexagon::V6_vmpy_qf16 ||
5123 MI->getOpcode() == Hexagon::V6_vmpy_qf32_mix_hf ||
5124 MI->getOpcode() == Hexagon::V6_vmpy_qf32_qf16 ||
5125 MI->getOpcode() == Hexagon::V6_vmpy_qf32);
5126}
5127
5128namespace llvm::HexagonII {
5129
5132 RegType In3 = RegType::Unknown) {
5133 RegTypeInfo I;
5134 I.Output = Out;
5135 I.Input1 = In1;
5136 I.Input2 = In2;
5137 I.Input3 = In3;
5138 return I;
5139}
5140
5141RegTypeInfo getRegTypeInfo(unsigned Opcode) {
5142 switch (Opcode) {
5143 default:
5144 return {};
5145
5146 case Hexagon::V6_vabs_qf16_hf:
5147 return make(RegType::QF16);
5148 case Hexagon::V6_vabs_qf16_qf16:
5150 case Hexagon::V6_vabs_qf32_qf32:
5152 case Hexagon::V6_vabs_qf32_sf:
5153 return make(RegType::QF32);
5154 case Hexagon::V6_vadd_hf:
5155 return make(RegType::QF16);
5156 case Hexagon::V6_vadd_qf16:
5158 case Hexagon::V6_vadd_qf16_mix:
5160 case Hexagon::V6_vadd_qf32:
5162 case Hexagon::V6_vadd_qf32_mix:
5164 case Hexagon::V6_vadd_sf:
5165 return make(RegType::QF32);
5166 case Hexagon::V6_vconv_bf_qf32:
5168 case Hexagon::V6_vconv_f8_qf16:
5170 case Hexagon::V6_vconv_hf_qf16:
5172 case Hexagon::V6_vconv_hf_qf32:
5174 case Hexagon::V6_vconv_qf16_f8:
5175 return make(RegType::QF16);
5176 case Hexagon::V6_vconv_qf16_hf:
5177 return make(RegType::QF16);
5178 case Hexagon::V6_vconv_qf16_qf16:
5180 case Hexagon::V6_vconv_qf32_qf32:
5182 case Hexagon::V6_vconv_qf32_sf:
5183 return make(RegType::QF32);
5184 case Hexagon::V6_vconv_sf_qf32:
5186 case Hexagon::V6_vilog2_qf16:
5188 case Hexagon::V6_vilog2_qf32:
5190 case Hexagon::V6_vmpy_qf16:
5192 case Hexagon::V6_vmpy_qf16_hf:
5193 return make(RegType::QF16);
5194 case Hexagon::V6_vmpy_qf16_mix_hf:
5196 case Hexagon::V6_vmpy_qf32:
5198 case Hexagon::V6_vmpy_qf32_hf:
5199 return make(RegType::QF32);
5200 case Hexagon::V6_vmpy_qf32_mix_hf:
5202 case Hexagon::V6_vmpy_qf32_qf16:
5204 case Hexagon::V6_vmpy_qf32_sf:
5205 return make(RegType::QF32);
5206 case Hexagon::V6_vmpy_rt_hf:
5207 return make(RegType::QF16);
5208 case Hexagon::V6_vmpy_rt_qf16:
5210 case Hexagon::V6_vmpy_rt_sf:
5211 return make(RegType::QF32);
5212 case Hexagon::V6_vneg_qf16_hf:
5213 return make(RegType::QF16);
5214 case Hexagon::V6_vneg_qf16_qf16:
5216 case Hexagon::V6_vneg_qf32_qf32:
5218 case Hexagon::V6_vneg_qf32_sf:
5219 return make(RegType::QF32);
5220 case Hexagon::V6_vsub_hf:
5221 return make(RegType::QF16);
5222 case Hexagon::V6_vsub_qf16:
5224 case Hexagon::V6_vsub_qf16_mix:
5226 case Hexagon::V6_vsub_qf32:
5228 case Hexagon::V6_vsub_qf32_mix:
5230 case Hexagon::V6_vsub_sf:
5231 return make(RegType::QF32);
5232 case Hexagon::V6_vsub_sf_mix:
5234 case Hexagon::V6_vsub_hf_mix:
5236 }
5237}
5238
5239} // namespace llvm::HexagonII
5240
5242 auto Info = HexagonII::getRegTypeInfo(MI->getOpcode());
5243 switch (Index) {
5244 case 1:
5245 return Info.Input1 == HexagonII::RegType::QF32;
5246 case 2:
5247 return Info.Input2 == HexagonII::RegType::QF32;
5248 case 3:
5249 return Info.Input3 == HexagonII::RegType::QF32;
5250 case 0:
5251 return Info.Input1 == HexagonII::RegType::QF32 ||
5252 Info.Input2 == HexagonII::RegType::QF32 ||
5253 Info.Input3 == HexagonII::RegType::QF32;
5254 default: // No instruction with more than 3 operands uses QF32.
5255 return false;
5256 }
5257 return false;
5258}
5259
5261 auto Info = HexagonII::getRegTypeInfo(MI->getOpcode());
5262 switch (Index) {
5263 case 1:
5264 return Info.Input1 == HexagonII::RegType::QF16;
5265 case 2:
5266 return Info.Input2 == HexagonII::RegType::QF16;
5267 case 3:
5268 return Info.Input3 == HexagonII::RegType::QF16;
5269 case 0:
5270 return Info.Input1 == HexagonII::RegType::QF16 ||
5271 Info.Input2 == HexagonII::RegType::QF16 ||
5272 Info.Input3 == HexagonII::RegType::QF16;
5273 default: // No instruction with more than 3 operands uses QF16.
5274 return false;
5275 }
5276 return false;
5277}
5278
5280 return usesQF32Operand(MI, Index) || usesQF16Operand(MI, Index);
5281}
5282
5286
5290
5294
5295// Return true if the function contains any qf-generating instructions.
5297 for (const MachineBasicBlock &MBB : MF)
5298 for (const MachineInstr &MI : MBB)
5299 if (isQFPInstr(const_cast<MachineInstr *>(&MI)))
5300 return true;
5301 return false;
5302}
5303
5304// Returns true if A appears before B within the same basic block.
5306 const MachineInstr *B) const {
5307 if (!A || !B || A->getParent() != B->getParent())
5308 return false;
5309
5310 for (const MachineInstr &MI : *A->getParent()) {
5311 if (&MI == A)
5312 return true;
5313 if (&MI == B)
5314 return false;
5315 }
5316 return false;
5317}
5318
5319// Returns false if a call, or an instruction between MI and Second, changes a
5320// register MI reads, so MI cannot be copied to just after Second.
5322 const TargetRegisterInfo *TRI) {
5323 for (MachineInstr &Between :
5324 make_range(std::next(MI.getIterator()), Second.getIterator()))
5325 for (const MachineOperand &MO : MI.debug_operands())
5326 if (MO.isReg() && MO.getReg().isValid() &&
5327 (Between.isCall() || Between.modifiesRegister(MO.getReg(), TRI)))
5328 return false;
5329 return true;
5330}
5331
5333 MachineInstr &Second) const {
5334 MachineBasicBlock &MBB = *Second.getParent();
5335 MachineFunction &MF = *MBB.getParent();
5337 Register DefReg = First.getOperand(0).getReg();
5338 auto Gap = make_range(std::next(First.getIterator()), Second.getIterator());
5339
5340 // The variables with a value that reads DefReg. All values of these
5341 // variables are copied, so that their order holds.
5343 for (MachineInstr &MI : Gap)
5344 if (MI.isDebugValue() && !MI.isDebugEntryValue() &&
5345 MI.readsRegister(DefReg, TRI) &&
5346 !is_contained(Vars, MI.getDebugVariable()))
5347 Vars.push_back(MI.getDebugVariable());
5348
5350 for (MachineInstr &MI : Gap)
5351 if (MI.isDebugValue() && is_contained(Vars, MI.getDebugVariable()))
5352 Group.push_back(&MI);
5353
5354 bool CanMove = !Second.isTerminator();
5355 for (MachineInstr *MI : Group)
5356 CanMove = CanMove && canMoveDebugValue(*MI, Second, TRI);
5357
5358 // Computed once, so that the copies stay in order.
5359 MachineBasicBlock::iterator FinalPos = std::next(Second.getIterator());
5360 for (MachineInstr *MI : Group)
5361 if (CanMove)
5362 MBB.insert(FinalPos, MF.CloneMachineInstr(MI));
5363 for (MachineInstr *MI : Group)
5364 if (!MI->isDebugEntryValue() && MI->readsRegister(DefReg, TRI))
5365 MI->setDebugValueUndef();
5366}
5367
5368// Addressing mode relations.
5370 return Opc >= 0 ? Hexagon::changeAddrMode_abs_io(Opc) : Opc;
5371}
5372
5374 return Opc >= 0 ? Hexagon::changeAddrMode_io_abs(Opc) : Opc;
5375}
5376
5378 return Opc >= 0 ? Hexagon::changeAddrMode_io_pi(Opc) : Opc;
5379}
5380
5382 return Opc >= 0 ? Hexagon::changeAddrMode_io_rr(Opc) : Opc;
5383}
5384
5386 return Opc >= 0 ? Hexagon::changeAddrMode_pi_io(Opc) : Opc;
5387}
5388
5390 return Opc >= 0 ? Hexagon::changeAddrMode_rr_io(Opc) : Opc;
5391}
5392
5394 return Opc >= 0 ? Hexagon::changeAddrMode_rr_ur(Opc) : Opc;
5395}
5396
5398 return Opc >= 0 ? Hexagon::changeAddrMode_ur_rr(Opc) : Opc;
5399}
5400
5402 static const MCInst Nop = MCInstBuilder(Hexagon::A2_nop);
5403
5404 return MCInstBuilder(Hexagon::BUNDLE)
5405 .addImm(0)
5406 .addInst(&Nop);
5407}
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
static bool mayAlias(MachineInstr &MIa, SmallVectorImpl< MachineInstr * > &MemInsns, AliasAnalysis *AA)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AArch64InstrInfo & TII
uint64_t Imm
static bool isConstant(const MachineInstr &MI)
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static const Function * getParent(const Value *V)
BitTracker BT
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
DXIL Forward Handle Accesses
static bool isSigned(unsigned Opcode)
static void parseOperands(MachineInstr *MI, SmallVector< unsigned, 4 > &Defs, SmallVector< unsigned, 8 > &Uses)
Gather register def/uses from MI.
static cl::opt< bool > DisableNVSchedule("disable-hexagon-nv-schedule", cl::Hidden, cl::desc("Disable schedule adjustment for new value stores."))
const int Hexagon_MEMH_OFFSET_MAX
const int Hexagon_MEMB_OFFSET_MAX
const int Hexagon_MEMH_OFFSET_MIN
const int Hexagon_MEMD_OFFSET_MAX
static cl::opt< bool > EnableTimingClassLatency("enable-timing-class-latency", cl::Hidden, cl::init(false), cl::desc("Enable timing class latency"))
static bool isUnsafeMemoryObject(const MachineInstr *MI)
const int Hexagon_MEMD_OFFSET_MIN
const int Hexagon_ADDI_OFFSET_MAX
static cl::opt< bool > EnableACCForwarding("enable-acc-forwarding", cl::Hidden, cl::init(true), cl::desc("Enable vec acc forwarding"))
static void getLiveInRegsAt(LivePhysRegs &Regs, const MachineInstr &MI)
const int Hexagon_MEMW_OFFSET_MAX
Constants for Hexagon instructions.
const int Hexagon_MEMW_OFFSET_MIN
cl::opt< bool > ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden, cl::init(false), cl::desc("Do not consider inline-asm a scheduling/" "packetization boundary."))
const int Hexagon_ADDI_OFFSET_MIN
static cl::opt< bool > BranchRelaxAsmLarge("branch-relax-asm-large", cl::init(true), cl::Hidden, cl::desc("branch relax asm"))
static cl::opt< bool > EnableALUForwarding("enable-alu-forwarding", cl::Hidden, cl::init(true), cl::desc("Enable vec alu forwarding"))
const int Hexagon_MEMB_OFFSET_MIN
static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB, MachineBasicBlock::const_instr_iterator MIE)
Calculate number of instructions excluding the debug instructions.
static cl::opt< bool > EnableBranchPrediction("hexagon-enable-branch-prediction", cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"))
static bool isDblRegForSubInst(Register Reg, const HexagonRegisterInfo &HRI)
static void getLiveOutRegsAt(LivePhysRegs &Regs, const MachineInstr &MI)
static bool canMoveDebugValue(MachineInstr &MI, MachineInstr &Second, const TargetRegisterInfo *TRI)
static cl::opt< bool > UseDFAHazardRec("dfa-hazard-rec", cl::init(true), cl::Hidden, cl::desc("Use the DFA based hazard recognizer."))
static bool isIntRegForSubInst(Register Reg)
static bool isDuplexPairMatch(unsigned Ga, unsigned Gb)
#define HEXAGON_INSTR_SIZE
IRTranslator LLVM IR MI
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
static constexpr Value * getValue(Ty &ValueOrUse)
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
static DebugLoc getDebugLoc(MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
Return the first DebugLoc that has line number information, given a range of instructions.
static bool isUndef(const MachineInstr &MI)
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define T
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static bool isReg(const MCInst &MI, unsigned OpNo)
uint64_t IntrinsicInst * II
PassBuilder PB(Mod->getContext(), Machine, PassOpts->PTO, std::nullopt, &PIC)
if(PassOpts->AAPipeline)
static StringRef getName(Value *V)
static bool isBranch(unsigned Opcode)
SmallVector< MachineOperand, 4 > Cond
MachineRegisterInfo & MRI
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
Remove Loads Into Fake Uses
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:479
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:119
The possible results of an alias query.
@ NoAlias
The two locations do not alias at all.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
A debug info location.
Definition DebugLoc.h:127
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isTmpLoadPseudo(const MachineInstr &MI) const
int getDuplexOpcode(const MachineInstr &MI, bool ForBigCore=true) const
bool isHLXVec(const MachineInstr &MI) const
unsigned removeBranch(MachineBasicBlock &MBB, int *BytesRemoved=nullptr) const override
Remove the branching code at the end of the specific MBB.
bool isPredicated(const MachineInstr &MI) const override
Returns true if the instruction is already predicated.
bool isHVXMemWithAIndirect(const MachineInstr &I, const MachineInstr &J) const
short changeAddrMode_abs_io(short Opc) const
bool isRestrictNoSlot1Store(const MachineInstr &MI) const
short getRegForm(const MachineInstr &MI) const
bool isVecALU(const MachineInstr &MI) const
bool isCompoundBranchInstr(const MachineInstr &MI) const
bool isDuplexPair(const MachineInstr &MIa, const MachineInstr &MIb) const
Symmetrical. See if these two instructions are fit for duplex pair.
bool isJumpR(const MachineInstr &MI) const
ScheduleHazardRecognizer * CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, const ScheduleDAG *DAG) const override
Allocate and return a hazard recognizer to use for this target when scheduling the machine instructio...
std::pair< unsigned, unsigned > decomposeMachineOperandsTargetFlags(unsigned TF) const override
Decompose the machine operand's target flags into two values - the direct target flag value and any o...
bool getMemOperandsWithOffsetWidth(const MachineInstr &LdSt, SmallVectorImpl< const MachineOperand * > &BaseOps, int64_t &Offset, bool &OffsetIsScalable, LocationSize &Width) const override
Get the base register and byte offset of a load/store instr.
bool producesStall(const MachineInstr &ProdMI, const MachineInstr &ConsMI) const
bool invertAndChangeJumpTarget(MachineInstr &MI, MachineBasicBlock *NewTarget) const
void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg, bool isKill, int FrameIndex, const TargetRegisterClass *RC, Register VReg, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
Store the specified register of the given register class to the specified stack frame index.
bool isDotTmpInst(const MachineInstr &MI) const
bool isPredictedTaken(unsigned Opcode) const
bool isSaveCalleeSavedRegsCall(const MachineInstr &MI) const
bool hasQFPInstrs(const MachineFunction &MF) const
Register isLoadFromStackSlot(const MachineInstr &MI, int &FrameIndex) const override
TargetInstrInfo overrides.
unsigned nonDbgBundleSize(MachineBasicBlock::const_iterator BundleHead) const
int getDotNewPredOp(const MachineInstr &MI, const MachineBranchProbabilityInfo *MBPI) const
bool isQFP32Instr(MachineInstr *MI) const
bool ClobbersPredicate(MachineInstr &MI, std::vector< MachineOperand > &Pred, bool SkipDead) const override
If the specified instruction defines any predicate or condition code register(s) used for predication...
unsigned getInvertedPredicatedOpcode(const int Opc) const
bool isPureSlot0(const MachineInstr &MI) const
bool doesNotReturn(const MachineInstr &CallMI) const
HexagonII::SubInstructionGroup getDuplexCandidateGroup(const MachineInstr &MI) const
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const override
Analyze the branching code at the end of MBB, returning true if it cannot be understood (e....
bool usesQF16Operand(MachineInstr *MI, unsigned Index=0) const
bool isPredicatedNew(const MachineInstr &MI) const
bool isSignExtendingLoad(const MachineInstr &MI) const
bool isVecAcc(const MachineInstr &MI) const
bool reversePredSense(MachineInstr &MI) const
bool isPostIncWithImmOffset(const MachineInstr &MI) const
bool isQFPMul(const MachineInstr *MF) const
unsigned getAddrMode(const MachineInstr &MI) const
MCInst getNop() const override
bool isJumpWithinBranchRange(const MachineInstr &MI, unsigned offset) const
bool mayBeNewStore(const MachineInstr &MI) const
bool isOperandExtended(const MachineInstr &MI, unsigned OperandNum) const
bool canExecuteInBundle(const MachineInstr &First, const MachineInstr &Second) const
Can these instructions execute at the same time in a bundle.
bool isQFP16Instr(MachineInstr *MI) const
std::optional< unsigned > getOperandLatency(const InstrItineraryData *ItinData, const MachineInstr &DefMI, unsigned DefIdx, const MachineInstr &UseMI, unsigned UseIdx) const override
getOperandLatency - Compute and return the use operand latency of a given pair of def and use.
bool isAddrModeWithOffset(const MachineInstr &MI) const
bool isBaseImmOffset(const MachineInstr &MI) const
bool isAbsoluteSet(const MachineInstr &MI) const
short changeAddrMode_io_pi(short Opc) const
void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, const DebugLoc &DL, Register DestReg, Register SrcReg, bool KillSrc, bool RenamableDest=false, bool RenamableSrc=false) const override
Emit instructions to copy a pair of physical registers.
short changeAddrMode_pi_io(short Opc) const
bool analyzeCompare(const MachineInstr &MI, Register &SrcReg, Register &SrcReg2, int64_t &Mask, int64_t &Value) const override
For a comparison instruction, return the source registers in SrcReg and SrcReg2 if having two registe...
bool reverseBranchCondition(SmallVectorImpl< MachineOperand > &Cond) const override
Reverses the branch condition of the specified condition list, returning false on success and true if...
std::unique_ptr< PipelinerLoopInfo > analyzeLoopForPipelining(MachineBasicBlock *LoopBB) const override
Analyze loop L, which must be a single-basic-block loop, and if the conditions can be understood enou...
bool isLoopN(const MachineInstr &MI) const
bool isSpillPredRegOp(const MachineInstr &MI) const
bool hasStoreToStackSlot(const MachineInstr &MI, SmallVectorImpl< const MachineMemOperand * > &Accesses) const override
Check if the instruction or the bundle of instructions has store to stack slots.
ArrayRef< std::pair< unsigned, const char * > > getSerializableDirectMachineOperandTargetFlags() const override
Return an array that contains the direct target flag values and their names.
bool isIndirectCall(const MachineInstr &MI) const
short changeAddrMode_ur_rr(short Opc) const
bool isValidAutoIncImm(const EVT VT, const int Offset) const
bool hasNonExtEquivalent(const MachineInstr &MI) const
bool isConstExtended(const MachineInstr &MI) const
bool getIncrementValue(const MachineInstr &MI, int &Value) const override
If the instruction is an increment of a constant value, return the amount.
int getCondOpcode(int Opc, bool sense) const
void moveDependentDebugValues(MachineInstr &First, MachineInstr &Second) const
Copies the debug values of the variables that read the register First and Second define to just after...
MachineInstr * findLoopInstr(MachineBasicBlock *BB, unsigned EndLoopOp, MachineBasicBlock *TargetBB, SmallPtrSet< MachineBasicBlock *, 8 > &Visited) const
Find the hardware loop instruction used to set-up the specified loop.
unsigned getInstrTimingClassLatency(const InstrItineraryData *ItinData, const MachineInstr &MI) const
bool usesQF32Operand(MachineInstr *MI, unsigned Index=0) const
bool isAccumulator(const MachineInstr &MI) const
unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, ArrayRef< MachineOperand > Cond, const DebugLoc &DL, int *BytesAdded=nullptr) const override
Insert branch code into the end of the specified MachineBasicBlock.
unsigned getInstrLatency(const InstrItineraryData *ItinData, const MachineInstr &MI, unsigned *PredCost=nullptr) const override
Compute the instruction latency of a given instruction.
bool PredOpcodeHasJMP_c(unsigned Opcode) const
bool isNewValue(const MachineInstr &MI) const
Register createVR(MachineFunction *MF, MVT VT) const
HexagonInstrInfo specifics.
bool isDotCurInst(const MachineInstr &MI) const
bool validateBranchCond(const ArrayRef< MachineOperand > &Cond) const
bool areMemAccessesDependent(AAResults *AA, const MachineInstr *MIa, const MachineInstr *MIb) const
Return true if the memory accesses of MIa and MIb may depend on each other.
bool isExtended(const MachineInstr &MI) const
bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumCycles, unsigned ExtraPredCycles, BranchProbability Probability) const override
Return true if it's profitable to predicate instructions with accumulated instruction latency of "Num...
bool isAsCheapAsAMove(const MachineInstr &MI) const override
int getMaxValue(const MachineInstr &MI) const
bool isPredicateLate(unsigned Opcode) const
short changeAddrMode_rr_ur(short Opc) const
bool isNewValueInst(const MachineInstr &MI) const
bool isHMXWeightInstr(const MachineInstr &MI) const
unsigned getInlineAsmLength(const char *Str, const MCAsmInfo &MAI, const TargetSubtargetInfo *STI=nullptr) const override
Measure the specified inline asm to determine an approximation of its length.
bool areMemAccessesTriviallyDisjoint(const MachineInstr &MIa, const MachineInstr &MIb) const override
int getNonDotCurOp(const MachineInstr &MI) const
bool isIndirectL4Return(const MachineInstr &MI) const
unsigned reversePrediction(unsigned Opcode) const
ArrayRef< std::pair< unsigned, const char * > > getSerializableBitmaskMachineOperandTargetFlags() const override
Return an array that contains the bitmask target flag values and their names.
bool isAssociativeAndCommutative(const MachineInstr &Inst, bool Invert) const override
InstrStage::FuncUnits getUnits(const MachineInstr &MI) const
unsigned getMemAccessSize(const MachineInstr &MI) const
bool predOpcodeHasNot(ArrayRef< MachineOperand > Cond) const
bool isComplex(const MachineInstr &MI) const
bool isPostIncrement(const MachineInstr &MI) const override
Return true for post-incremented instructions.
void setBundleNoShuf(MachineBasicBlock::instr_iterator MIB) const
MachineBasicBlock::instr_iterator expandVGatherPseudo(MachineInstr &MI) const
bool isDotCurOperand(const MachineInstr &MI, unsigned OpIndex) const
Returns true if the specified operand is a .cur (current) operand.
void loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register DestReg, int FrameIndex, const TargetRegisterClass *RC, Register VReg, unsigned SubReg=0, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
Load the specified register of the given register class from the specified stack frame index.
int getDotNewOp(const MachineInstr &MI) const
void changeDuplexOpcode(MachineBasicBlock::instr_iterator MII, bool ToBigInstrs) const
bool isMemOp(const MachineInstr &MI) const
int getDotOldOp(const MachineInstr &MI) const
bool hasUncondBranch(const MachineBasicBlock *B) const
short getNonExtOpcode(const MachineInstr &MI) const
bool isNewStorePseudo(const MachineInstr &MI) const
bool isTailCall(const MachineInstr &MI) const override
void insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const override
Insert a noop into the instruction stream at the specified point.
bool isDeallocRet(const MachineInstr &MI) const
HexagonInstrInfo(const HexagonSubtarget &ST)
unsigned getCExtOpNum(const MachineInstr &MI) const
bool isSolo(const MachineInstr &MI) const
DFAPacketizer * CreateTargetScheduleState(const TargetSubtargetInfo &STI) const override
Create machine specific model for scheduling.
bool isLateSourceInstr(const MachineInstr &MI) const
bool isDotNewInst(const MachineInstr &MI) const
void translateInstrsForDup(MachineFunction &MF, bool ToBigInstrs=true) const
bool isTC1(const MachineInstr &MI) const
bool isSchedulingBoundary(const MachineInstr &MI, const MachineBasicBlock *MBB, const MachineFunction &MF) const override
Test if the given instruction should be considered a scheduling boundary.
bool predCanBeUsedAsDotNew(const MachineInstr &MI, Register PredReg) const
unsigned getSize(const MachineInstr &MI) const
bool isDotNewOperand(const MachineInstr &MI, unsigned OpIndex) const
Should be able to tell if the operand in question is .new.
bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles, BranchProbability Probability) const override
Return true if it's profitable for if-converter to duplicate instructions of specified accumulated in...
short changeAddrMode_io_abs(short Opc) const
int getDotCurOp(const MachineInstr &MI) const
bool expandPostRAPseudo(MachineInstr &MI) const override
This function is called for all pseudo instructions that remain after register allocation.
bool isMIBefore(const MachineInstr *A, const MachineInstr *B) const
bool isExpr(unsigned OpType) const
bool isSafeInBundleWAR(const MachineInstr &DefMI, unsigned DefOpIdx, const MachineInstr &UseMI, unsigned UseOpIdx, bool IsPullDown=false, bool DefViaRegMask=false) const
A use (UseMI) can share a bundle with an instruction that redefines the same register (DefMI),...
void genAllInsnTimingClasses(MachineFunction &MF) const
bool isTC2Early(const MachineInstr &MI) const
bool hasEHLabel(const MachineBasicBlock *B) const
bool shouldSink(const MachineInstr &MI) const override
bool isZeroExtendingLoad(const MachineInstr &MI) const
short changeAddrMode_rr_io(short Opc) const
bool isHVXVec(const MachineInstr &MI) const
bool isDotTmpOperand(const MachineInstr &MI, unsigned OpIndex) const
Returns true if the specified operand is a .tmp (temporary) operand.
bool isDependent(const MachineInstr &ProdMI, const MachineInstr &ConsMI) const
short changeAddrMode_io_rr(short Opc) const
bool SubsumesPredicate(ArrayRef< MachineOperand > Pred1, ArrayRef< MachineOperand > Pred2) const override
Returns true if the first specified predicate subsumes the second, e.g.
bool mayBeCurLoad(const MachineInstr &MI) const
bool getBundleNoShuf(const MachineInstr &MIB) const
bool isNewValueJump(const MachineInstr &MI) const
bool isTC4x(const MachineInstr &MI) const
bool PredicateInstruction(MachineInstr &MI, ArrayRef< MachineOperand > Cond) const override
Convert the instruction into a predicated instruction.
bool getPredReg(ArrayRef< MachineOperand > Cond, Register &PredReg, unsigned &PredRegPos, RegState &PredRegFlags) const
bool isFloat(const MachineInstr &MI) const
bool isQFPInstr(MachineInstr *MI) const
bool isToBeScheduledASAP(const MachineInstr &MI1, const MachineInstr &MI2) const
MachineOperand * getBaseAndOffset(const MachineInstr &MI, int64_t &Offset, LocationSize &AccessSize) const
bool getInvertedPredSense(SmallVectorImpl< MachineOperand > &Cond) const
unsigned nonDbgBBSize(const MachineBasicBlock *BB) const
getInstrTimingClassLatency - Compute the instruction latency of a given instruction using Timing Clas...
uint64_t getType(const MachineInstr &MI) const
bool isEndLoopN(unsigned Opcode) const
bool getBaseAndOffsetPosition(const MachineInstr &MI, unsigned &BasePos, unsigned &OffsetPos) const override
For instructions with a base and offset, return the position of the base register and offset operands...
bool isPredicable(const MachineInstr &MI) const override
Return true if the specified instruction can be predicated.
bool isHMXActivateInstr(const MachineInstr &MI) const
bool isExtendable(const MachineInstr &MI) const
void immediateExtend(MachineInstr &MI) const
immediateExtend - Changes the instruction in place to one using an immediate extender.
bool isValidOffset(unsigned Opcode, int Offset, bool Extend=true) const
bool isActivateWeightPair(const MachineInstr &MIa, const MachineInstr &MIb) const
HexagonII::CompoundGroup getCompoundCandidateGroup(const MachineInstr &MI) const
bool hasLoadFromStackSlot(const MachineInstr &MI, SmallVectorImpl< const MachineMemOperand * > &Accesses) const override
Check if the instruction or the bundle of instructions has load from stack slots.
SmallVector< MachineInstr *, 2 > getBranchingInstrs(MachineBasicBlock &MBB) const
bool isPredicatedTrue(const MachineInstr &MI) const
bool isNewValueStore(const MachineInstr &MI) const
int getMinValue(const MachineInstr &MI) const
bool isVecUsableNextPacket(const MachineInstr &ProdMI, const MachineInstr &ConsMI) const
unsigned getCompoundOpcode(const MachineInstr &GA, const MachineInstr &GB) const
bool addLatencyToSchedule(const MachineInstr &MI1, const MachineInstr &MI2) const
Register isStoreToStackSlot(const MachineInstr &MI, int &FrameIndex) const override
If the specified machine instruction is a direct store to a stack slot, return the virtual or physica...
int getDotNewPredJumpOp(const MachineInstr &MI, const MachineBranchProbabilityInfo *MBPI) const
bool usesQFOperand(MachineInstr *MI, unsigned Index=0) const
bool isTC2(const MachineInstr &MI) const
Hexagon target-specific information for each MachineFunction.
Register getFrameRegister(const MachineFunction &MF) const override
bool isFakeReg(MCPhysReg Reg) const
Returns true if the given reserved physical register Reg is live across function calls/returns.
Itinerary data supplied by a subtarget to be used by a target.
unsigned getStageLatency(unsigned ItinClassIndx) const
Return the total stage latency of the given class.
void RemoveMachineInstrFromMaps(MachineInstr &MI)
A set of physical registers with utility functions to track liveness when walking backward/forward th...
LLVM_ABI void stepForward(const MachineInstr &MI, SmallVectorImpl< std::pair< MCPhysReg, const MachineOperand * > > &Clobbers)
Simulates liveness when stepping forward over an instruction(bundle).
LLVM_ABI void stepBackward(const MachineInstr &MI)
Simulates liveness when stepping backwards over an instruction(bundle).
LLVM_ABI void addLiveIns(const MachineBasicBlock &MBB)
Adds all live-in registers of basic block MBB.
LLVM_ABI bool available(const MachineRegisterInfo &MRI, MCRegister Reg) const
Returns true if register Reg and no aliasing register is in the set.
LLVM_ABI void addLiveOuts(const MachineBasicBlock &MBB)
Adds all live-out registers of basic block MBB.
bool contains(MCRegister Reg) const
Returns true if register Reg is contained in the set.
bool hasValue() const
static LocationSize precise(uint64_t Value)
TypeSize getValue() const
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:67
virtual unsigned getMaxInstLength(const MCSubtargetInfo *STI=nullptr) const
Returns the maximum possible encoded instruction size in bytes.
Definition MCAsmInfo.h:554
StringRef getCommentString() const
Definition MCAsmInfo.h:565
const char * getSeparatorString() const
Definition MCAsmInfo.h:560
MCInstBuilder & addInst(const MCInst *Val)
Add a new MCInst operand.
MCInstBuilder & addImm(int64_t Val)
Add a new integer immediate operand.
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
Describe properties that are true of each instruction in the target description file.
unsigned getSchedClass() const
Return the scheduling class for this instruction.
unsigned getNumDefs() const
Return the number of MachineOperands that are register definitions.
Machine Value Type.
SimpleValueType SimpleTy
MachineInstrBundleIterator< const MachineInstr > const_iterator
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
Instructions::iterator instr_iterator
Instructions::const_iterator const_instr_iterator
iterator_range< pred_iterator > predecessors()
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI BranchProbability getEdgeProbability(const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
const char * createExternalSymbolName(StringRef Name)
Allocate a string and populate it with the given external symbol name.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
BasicBlockListType::iterator iterator
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags F, LLT MemTy, Align BaseAlignment, const MMOMetadata &Metadata=MMOMetadata(), SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
const MachineInstrBuilder & cloneMemRefs(const MachineInstr &OtherMI) const
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
reverse_iterator getReverse() const
Get a reverse iterator to the same node.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
bool isTerminator(QueryType Type=AnyInBundle) const
Returns true if this instruction part of the terminator for a basic block.
const MachineBasicBlock * getParent() const
bool readsRegister(Register Reg, const TargetRegisterInfo *TRI) const
Return true if the MachineInstr reads the specified register.
bool getFlag(MIFlag Flag) const
Return whether an MI flag is set.
bool isBundle() const
unsigned getNumOperands() const
Retuns the total number of operands.
LLVM_ABI unsigned getNumExplicitOperands() const
Returns the number of non-implicit operands.
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
LLVM_ABI bool hasUnmodeledSideEffects() const
Return true if this instruction has side effects that are not modeled by mayLoad / mayStore,...
bool hasOneMemOperand() const
Return true if this instruction has exactly one MachineMemOperand.
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
LLVM_ABI bool hasOrderedMemoryRef() const
Return true if this instruction may have an ordered or volatile memory reference, or if the informati...
bool mayStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly modify memory.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
bool isIndirectBranch(QueryType Type=AnyInBundle) const
Return true if this is an indirect branch, such as a branch through a register.
A description of a memory reference used in the backend.
LocationSize getSize() const
Return the size in bytes of the memory reference.
@ MOVolatile
The memory access is volatile.
@ MOLoad
The memory access reads data.
@ MOStore
The memory access writes data.
AAMDNodes getAAInfo() const
Return the AA tags for the memory reference.
const Value * getValue() const
Return the base address of the memory access.
int64_t getOffset() const
For normal values, this is a byte offset added to the base address.
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
void setImm(int64_t immVal)
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
bool isCPI() const
isCPI - Tests if this is a MO_ConstantPoolIndex operand.
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
unsigned getTargetFlags() const
static MachineOperand CreateImm(int64_t Val)
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
bool isBlockAddress() const
isBlockAddress - Tests if this is a MO_BlockAddress operand.
Register getReg() const
getReg - Returns the register number.
void addTargetFlag(unsigned F)
bool isInternalRead() const
bool isFI() const
isFI - Tests if this is a MO_FrameIndex operand.
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ MO_GlobalAddress
Address of a global value.
@ MO_BlockAddress
Address of a basic block.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_ExternalSymbol
Name of external global symbol.
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
bool isFPImm() const
isFPImm - Tests if this is a MO_FPImmediate operand.
bool isMBB() const
isMBB - Tests if this is a MO_MachineBasicBlock operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Representation for a specific memory location.
Special value supplied for machine level alias analysis.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
HazardRecognizer - This determines whether or not an instruction can be issued this cycle,...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Register getReg() const
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:138
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition StringRef.h:463
Object returned by analyzeLoopForPipelining.
virtual ScheduleHazardRecognizer * CreateTargetPostRAHazardRecognizer(const InstrItineraryData *, const ScheduleDAG *DAG) const
Allocate and return a hazard recognizer to use for this target when scheduling the machine instructio...
virtual bool hasStoreToStackSlot(const MachineInstr &MI, SmallVectorImpl< const MachineMemOperand * > &Accesses) const
If the specified machine instruction has a store to a stack slot, return true along with the FrameInd...
virtual std::optional< unsigned > getOperandLatency(const InstrItineraryData *ItinData, SDNode *DefNode, unsigned DefIdx, SDNode *UseNode, unsigned UseIdx) const
virtual bool hasLoadFromStackSlot(const MachineInstr &MI, SmallVectorImpl< const MachineMemOperand * > &Accesses) const
If the specified machine instruction has a load from a stack slot, return true along with the FrameIn...
Primary interface to the complete machine description for the target machine.
const MCAsmInfo & getMCAsmInfo() const
Return target specific asm information.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
virtual const InstrItineraryData * getInstrItineraryData() const
getInstrItineraryData - Returns instruction itinerary data for the target or specific subtarget.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM Value Representation.
Definition Value.h:75
self_iterator getIterator()
Definition ilist_node.h:123
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract Attribute helper functions.
Definition Attributor.h:166
bool isSlot0Only(unsigned units)
HexagonII - This namespace holds all of the target specific flags that instruction info tracks.
unsigned const TypeCVI_LAST
static constexpr RegTypeInfo make(RegType Out, RegType In1=RegType::Unknown, RegType In2=RegType::Unknown, RegType In3=RegType::Unknown)
unsigned const TypeCVI_FIRST
RegType getOpRegType(unsigned Opcode)
RegTypeInfo getRegTypeInfo(unsigned Opcode)
bool getTSFlag(const MCInstrDesc &MID, HexagonTSFlagsVal Pos)
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
@ Length
Definition DWP.cpp:577
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1755
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
RegState
Flags to represent properties of register accesses.
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Kill
The last use of a register.
@ Undef
Value of the register doesn't matter.
@ NoFlags
No Specific Flags.
constexpr RegState getKillRegState(bool B)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ Done
Definition Threading.h:60
bool is_TC1(unsigned SchedClass)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:326
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
MachineBasicBlock::instr_iterator getBundleEnd(MachineBasicBlock::instr_iterator I)
Returns an iterator pointing beyond the bundle containing I.
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
RegState getRegState(const MachineOperand &RegOp)
Get all register state flags from machine operand RegOp.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
bool is_TC2(unsigned SchedClass)
bool is_TC2early(unsigned SchedClass)
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
constexpr bool isShiftedInt(int64_t x)
Checks if a signed integer is an N bit number shifted left by S.
Definition MathExtras.h:183
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
bool isSpace(char C)
Checks whether character C is whitespace in the "C" locale.
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
constexpr RegState getUndefRegState(bool B)
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
constexpr bool isShiftedUInt(uint64_t x)
Checks if a unsigned integer is an N bit number shifted left by S.
Definition MathExtras.h:199
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
bool is_TC4x(unsigned SchedClass)
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Extended Value Type.
Definition ValueTypes.h:35
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:411
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:354
These values represent a non-pipelined step in the execution of an instruction.
uint64_t FuncUnits
Bitmask representing a set of functional units.
This class contains a discriminated union of information about pointers in memory operands,...
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.