1 //===- HexagonInstrInfo.cpp - Hexagon Instruction Information -------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains the Hexagon implementation of the TargetInstrInfo class.
11 //===----------------------------------------------------------------------===//
13 #include "HexagonInstrInfo.h"
15 #include "HexagonFrameLowering.h"
16 #include "HexagonHazardRecognizer.h"
17 #include "HexagonRegisterInfo.h"
18 #include "HexagonSubtarget.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/CodeGen/DFAPacketizer.h"
25 #include "llvm/CodeGen/LivePhysRegs.h"
26 #include "llvm/CodeGen/MachineBasicBlock.h"
27 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
28 #include "llvm/CodeGen/MachineFrameInfo.h"
29 #include "llvm/CodeGen/MachineFunction.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/CodeGen/MachineInstrBuilder.h"
32 #include "llvm/CodeGen/MachineInstrBundle.h"
33 #include "llvm/CodeGen/MachineLoopInfo.h"
34 #include "llvm/CodeGen/MachineMemOperand.h"
35 #include "llvm/CodeGen/MachineOperand.h"
36 #include "llvm/CodeGen/MachineRegisterInfo.h"
37 #include "llvm/CodeGen/ScheduleDAG.h"
38 #include "llvm/CodeGen/TargetInstrInfo.h"
39 #include "llvm/CodeGen/TargetOpcodes.h"
40 #include "llvm/CodeGen/TargetRegisterInfo.h"
41 #include "llvm/CodeGen/TargetSubtargetInfo.h"
42 #include "llvm/CodeGenTypes/MachineValueType.h"
43 #include "llvm/IR/DebugLoc.h"
44 #include "llvm/IR/GlobalVariable.h"
45 #include "llvm/MC/MCAsmInfo.h"
46 #include "llvm/MC/MCInstBuilder.h"
47 #include "llvm/MC/MCInstrDesc.h"
48 #include "llvm/MC/MCInstrItineraries.h"
49 #include "llvm/MC/MCRegisterInfo.h"
50 #include "llvm/Support/BranchProbability.h"
51 #include "llvm/Support/CommandLine.h"
52 #include "llvm/Support/Debug.h"
53 #include "llvm/Support/ErrorHandling.h"
54 #include "llvm/Support/MathExtras.h"
55 #include "llvm/Support/raw_ostream.h"
56 #include "llvm/Target/TargetMachine.h"
68 #define DEBUG_TYPE "hexagon-instrinfo"
70 #define GET_INSTRINFO_CTOR_DTOR
71 #define GET_INSTRMAP_INFO
72 #include "HexagonDepTimingClasses.h"
73 #include "HexagonGenDFAPacketizer.inc"
74 #include "HexagonGenInstrInfo.inc"
76 cl::opt
<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden
,
77 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
78 "packetization boundary."));
80 static cl::opt
<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
81 cl::Hidden
, cl::init(true), cl::desc("Enable branch prediction"));
83 static cl::opt
<bool> DisableNVSchedule(
84 "disable-hexagon-nv-schedule", cl::Hidden
,
85 cl::desc("Disable schedule adjustment for new value stores."));
87 static cl::opt
<bool> EnableTimingClassLatency(
88 "enable-timing-class-latency", cl::Hidden
, cl::init(false),
89 cl::desc("Enable timing class latency"));
91 static cl::opt
<bool> EnableALUForwarding(
92 "enable-alu-forwarding", cl::Hidden
, cl::init(true),
93 cl::desc("Enable vec alu forwarding"));
95 static cl::opt
<bool> EnableACCForwarding(
96 "enable-acc-forwarding", cl::Hidden
, cl::init(true),
97 cl::desc("Enable vec acc forwarding"));
99 static cl::opt
<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
100 cl::init(true), cl::Hidden
,
101 cl::desc("branch relax asm"));
104 UseDFAHazardRec("dfa-hazard-rec", cl::init(true), cl::Hidden
,
105 cl::desc("Use the DFA based hazard recognizer."));
107 /// Constants for Hexagon instructions.
108 const int Hexagon_MEMW_OFFSET_MAX
= 4095;
109 const int Hexagon_MEMW_OFFSET_MIN
= -4096;
110 const int Hexagon_MEMD_OFFSET_MAX
= 8191;
111 const int Hexagon_MEMD_OFFSET_MIN
= -8192;
112 const int Hexagon_MEMH_OFFSET_MAX
= 2047;
113 const int Hexagon_MEMH_OFFSET_MIN
= -2048;
114 const int Hexagon_MEMB_OFFSET_MAX
= 1023;
115 const int Hexagon_MEMB_OFFSET_MIN
= -1024;
116 const int Hexagon_ADDI_OFFSET_MAX
= 32767;
117 const int Hexagon_ADDI_OFFSET_MIN
= -32768;
119 // Pin the vtable to this file.
120 void HexagonInstrInfo::anchor() {}
122 HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget
&ST
)
123 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN
, Hexagon::ADJCALLSTACKUP
),
127 namespace HexagonFUnits
{
128 bool isSlot0Only(unsigned units
);
132 static bool isIntRegForSubInst(Register Reg
) {
133 return (Reg
>= Hexagon::R0
&& Reg
<= Hexagon::R7
) ||
134 (Reg
>= Hexagon::R16
&& Reg
<= Hexagon::R23
);
137 static bool isDblRegForSubInst(Register Reg
, const HexagonRegisterInfo
&HRI
) {
138 return isIntRegForSubInst(HRI
.getSubReg(Reg
, Hexagon::isub_lo
)) &&
139 isIntRegForSubInst(HRI
.getSubReg(Reg
, Hexagon::isub_hi
));
142 /// Calculate number of instructions excluding the debug instructions.
143 static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB
,
144 MachineBasicBlock::const_instr_iterator MIE
) {
146 for (; MIB
!= MIE
; ++MIB
) {
147 if (!MIB
->isDebugInstr())
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
156 bool HexagonInstrInfo::isAsCheapAsAMove(const MachineInstr
&MI
) const {
157 // Enable the following steps only at Os/Oz
158 if (!(MI
.getMF()->getFunction().hasOptSize()))
159 return MI
.isAsCheapAsAMove();
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.
167 // If the instruction has an operand of size > 16bits, its will be
168 // const-extended and hence, it is not cheap.
170 int64_t Imm
= Op
.getImm();
175 return MI
.isAsCheapAsAMove();
178 // Do not sink floating point instructions that updates USR register.
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.
187 bool HexagonInstrInfo::shouldSink(const MachineInstr
&MI
) const {
188 // Assumption: A floating point instruction that reads the USR will write
190 if (isFloat(MI
) && MI
.hasRegisterImplicitUseOperand(Hexagon::USR
))
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.
199 MachineInstr
*HexagonInstrInfo::findLoopInstr(MachineBasicBlock
*BB
,
200 unsigned EndLoopOp
, MachineBasicBlock
*TargetBB
,
201 SmallPtrSet
<MachineBasicBlock
*, 8> &Visited
) const {
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
;
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
)
219 for (MachineInstr
&I
: llvm::reverse(PB
->instrs())) {
220 unsigned Opc
= I
.getOpcode();
221 if (Opc
== LOOPi
|| Opc
== LOOPr
)
223 // We've reached a different loop, which means the loop01 has been
225 if (Opc
== EndLoopOp
&& I
.getOperand(0).getMBB() != TargetBB
)
228 // Check the predecessors for the LOOP instruction.
229 if (MachineInstr
*Loop
= findLoopInstr(PB
, EndLoopOp
, TargetBB
, Visited
))
235 /// Gather register def/uses from MI.
236 /// This treats possible (predicated) defs as actually happening ones
237 /// (conservatively).
238 static inline void parseOperands(const MachineInstr
&MI
,
239 SmallVectorImpl
<Register
> &Defs
, SmallVectorImpl
<Register
> &Uses
) {
243 for (const MachineOperand
&MO
: MI
.operands()) {
247 Register Reg
= MO
.getReg();
252 Uses
.push_back(MO
.getReg());
255 Defs
.push_back(MO
.getReg());
259 // Position dependent, so check twice for swap.
260 static bool isDuplexPairMatch(unsigned Ga
, unsigned Gb
) {
262 case HexagonII::HSIG_None
:
265 case HexagonII::HSIG_L1
:
266 return (Gb
== HexagonII::HSIG_L1
|| Gb
== HexagonII::HSIG_A
);
267 case HexagonII::HSIG_L2
:
268 return (Gb
== HexagonII::HSIG_L1
|| Gb
== HexagonII::HSIG_L2
||
269 Gb
== HexagonII::HSIG_A
);
270 case HexagonII::HSIG_S1
:
271 return (Gb
== HexagonII::HSIG_L1
|| Gb
== HexagonII::HSIG_L2
||
272 Gb
== HexagonII::HSIG_S1
|| Gb
== HexagonII::HSIG_A
);
273 case HexagonII::HSIG_S2
:
274 return (Gb
== HexagonII::HSIG_L1
|| Gb
== HexagonII::HSIG_L2
||
275 Gb
== HexagonII::HSIG_S1
|| Gb
== HexagonII::HSIG_S2
||
276 Gb
== HexagonII::HSIG_A
);
277 case HexagonII::HSIG_A
:
278 return (Gb
== HexagonII::HSIG_A
);
279 case HexagonII::HSIG_Compound
:
280 return (Gb
== HexagonII::HSIG_Compound
);
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.
290 Register
HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr
&MI
,
291 int &FrameIndex
) const {
292 switch (MI
.getOpcode()) {
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 const MachineOperand OpFI
= MI
.getOperand(1);
308 const MachineOperand OpOff
= MI
.getOperand(2);
309 if (!OpOff
.isImm() || OpOff
.getImm() != 0)
311 FrameIndex
= OpFI
.getIndex();
312 return MI
.getOperand(0).getReg();
315 case Hexagon::L2_ploadrit_io
:
316 case Hexagon::L2_ploadrif_io
:
317 case Hexagon::L2_ploadrdt_io
:
318 case Hexagon::L2_ploadrdf_io
: {
319 const MachineOperand OpFI
= MI
.getOperand(2);
322 const MachineOperand OpOff
= MI
.getOperand(3);
323 if (!OpOff
.isImm() || OpOff
.getImm() != 0)
325 FrameIndex
= OpFI
.getIndex();
326 return MI
.getOperand(0).getReg();
333 /// isStoreToStackSlot - If the specified machine instruction is a direct
334 /// store to a stack slot, return the virtual or physical register number of
335 /// the source reg along with the FrameIndex of the loaded stack slot. If
336 /// not, return 0. This predicate must return 0 if the instruction has
337 /// any side effects other than storing to the stack slot.
338 Register
HexagonInstrInfo::isStoreToStackSlot(const MachineInstr
&MI
,
339 int &FrameIndex
) const {
340 switch (MI
.getOpcode()) {
343 case Hexagon::S2_storerb_io
:
344 case Hexagon::S2_storerh_io
:
345 case Hexagon::S2_storeri_io
:
346 case Hexagon::S2_storerd_io
:
347 case Hexagon::V6_vS32b_ai
:
348 case Hexagon::V6_vS32Ub_ai
:
349 case Hexagon::STriw_pred
:
350 case Hexagon::STriw_ctr
:
351 case Hexagon::PS_vstorerq_ai
:
352 case Hexagon::PS_vstorerw_ai
: {
353 const MachineOperand
&OpFI
= MI
.getOperand(0);
356 const MachineOperand
&OpOff
= MI
.getOperand(1);
357 if (!OpOff
.isImm() || OpOff
.getImm() != 0)
359 FrameIndex
= OpFI
.getIndex();
360 return MI
.getOperand(2).getReg();
363 case Hexagon::S2_pstorerbt_io
:
364 case Hexagon::S2_pstorerbf_io
:
365 case Hexagon::S2_pstorerht_io
:
366 case Hexagon::S2_pstorerhf_io
:
367 case Hexagon::S2_pstorerit_io
:
368 case Hexagon::S2_pstorerif_io
:
369 case Hexagon::S2_pstorerdt_io
:
370 case Hexagon::S2_pstorerdf_io
: {
371 const MachineOperand
&OpFI
= MI
.getOperand(1);
374 const MachineOperand
&OpOff
= MI
.getOperand(2);
375 if (!OpOff
.isImm() || OpOff
.getImm() != 0)
377 FrameIndex
= OpFI
.getIndex();
378 return MI
.getOperand(3).getReg();
385 /// This function checks if the instruction or bundle of instructions
386 /// has load from stack slot and returns frameindex and machine memory
387 /// operand of that instruction if true.
388 bool HexagonInstrInfo::hasLoadFromStackSlot(
389 const MachineInstr
&MI
,
390 SmallVectorImpl
<const MachineMemOperand
*> &Accesses
) const {
392 const MachineBasicBlock
*MBB
= MI
.getParent();
393 MachineBasicBlock::const_instr_iterator MII
= MI
.getIterator();
394 for (++MII
; MII
!= MBB
->instr_end() && MII
->isInsideBundle(); ++MII
)
395 if (TargetInstrInfo::hasLoadFromStackSlot(*MII
, Accesses
))
400 return TargetInstrInfo::hasLoadFromStackSlot(MI
, Accesses
);
403 /// This function checks if the instruction or bundle of instructions
404 /// has store to stack slot and returns frameindex and machine memory
405 /// operand of that instruction if true.
406 bool HexagonInstrInfo::hasStoreToStackSlot(
407 const MachineInstr
&MI
,
408 SmallVectorImpl
<const MachineMemOperand
*> &Accesses
) const {
410 const MachineBasicBlock
*MBB
= MI
.getParent();
411 MachineBasicBlock::const_instr_iterator MII
= MI
.getIterator();
412 for (++MII
; MII
!= MBB
->instr_end() && MII
->isInsideBundle(); ++MII
)
413 if (TargetInstrInfo::hasStoreToStackSlot(*MII
, Accesses
))
418 return TargetInstrInfo::hasStoreToStackSlot(MI
, Accesses
);
421 /// This function can analyze one/two way branching only and should (mostly) be
422 /// called by target independent side.
423 /// First entry is always the opcode of the branching instruction, except when
424 /// the Cond vector is supposed to be empty, e.g., when analyzeBranch fails, a
425 /// BB with only unconditional jump. Subsequent entries depend upon the opcode,
426 /// e.g. Jump_c p will have
430 /// Cond[0] = ENDLOOP
433 /// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
436 bool HexagonInstrInfo::analyzeBranch(MachineBasicBlock
&MBB
,
437 MachineBasicBlock
*&TBB
,
438 MachineBasicBlock
*&FBB
,
439 SmallVectorImpl
<MachineOperand
> &Cond
,
440 bool AllowModify
) const {
445 // If the block has no terminators, it just falls into the block after it.
446 MachineBasicBlock::instr_iterator I
= MBB
.instr_end();
447 if (I
== MBB
.instr_begin())
450 // A basic block may looks like this:
460 // It has two succs but does not have a terminator
461 // Don't know how to handle it.
465 // Don't analyze EH branches.
467 } while (I
!= MBB
.instr_begin());
472 while (I
->isDebugInstr()) {
473 if (I
== MBB
.instr_begin())
478 bool JumpToBlock
= I
->getOpcode() == Hexagon::J2_jump
&&
479 I
->getOperand(0).isMBB();
480 // Delete the J2_jump if it's equivalent to a fall-through.
481 if (AllowModify
&& JumpToBlock
&&
482 MBB
.isLayoutSuccessor(I
->getOperand(0).getMBB())) {
483 LLVM_DEBUG(dbgs() << "\nErasing the jump to successor block\n";);
484 I
->eraseFromParent();
486 if (I
== MBB
.instr_begin())
490 if (!isUnpredicatedTerminator(*I
))
493 // Get the last instruction in the block.
494 MachineInstr
*LastInst
= &*I
;
495 MachineInstr
*SecondLastInst
= nullptr;
496 // Find one more terminator if present.
498 if (&*I
!= LastInst
&& !I
->isBundle() && isUnpredicatedTerminator(*I
)) {
500 SecondLastInst
= &*I
;
502 // This is a third branch.
505 if (I
== MBB
.instr_begin())
510 int LastOpcode
= LastInst
->getOpcode();
511 int SecLastOpcode
= SecondLastInst
? SecondLastInst
->getOpcode() : 0;
512 // If the branch target is not a basic block, it could be a tail call.
513 // (It is, if the target is a function.)
514 if (LastOpcode
== Hexagon::J2_jump
&& !LastInst
->getOperand(0).isMBB())
516 if (SecLastOpcode
== Hexagon::J2_jump
&&
517 !SecondLastInst
->getOperand(0).isMBB())
520 bool LastOpcodeHasJMP_c
= PredOpcodeHasJMP_c(LastOpcode
);
521 bool LastOpcodeHasNVJump
= isNewValueJump(*LastInst
);
523 if (LastOpcodeHasJMP_c
&& !LastInst
->getOperand(1).isMBB())
526 // If there is only one terminator instruction, process it.
527 if (LastInst
&& !SecondLastInst
) {
528 if (LastOpcode
== Hexagon::J2_jump
) {
529 TBB
= LastInst
->getOperand(0).getMBB();
532 if (isEndLoopN(LastOpcode
)) {
533 TBB
= LastInst
->getOperand(0).getMBB();
534 Cond
.push_back(MachineOperand::CreateImm(LastInst
->getOpcode()));
535 Cond
.push_back(LastInst
->getOperand(0));
538 if (LastOpcodeHasJMP_c
) {
539 TBB
= LastInst
->getOperand(1).getMBB();
540 Cond
.push_back(MachineOperand::CreateImm(LastInst
->getOpcode()));
541 Cond
.push_back(LastInst
->getOperand(0));
544 // Only supporting rr/ri versions of new-value jumps.
545 if (LastOpcodeHasNVJump
&& (LastInst
->getNumExplicitOperands() == 3)) {
546 TBB
= LastInst
->getOperand(2).getMBB();
547 Cond
.push_back(MachineOperand::CreateImm(LastInst
->getOpcode()));
548 Cond
.push_back(LastInst
->getOperand(0));
549 Cond
.push_back(LastInst
->getOperand(1));
552 LLVM_DEBUG(dbgs() << "\nCant analyze " << printMBBReference(MBB
)
553 << " with one jump\n";);
554 // Otherwise, don't know what this is.
558 bool SecLastOpcodeHasJMP_c
= PredOpcodeHasJMP_c(SecLastOpcode
);
559 bool SecLastOpcodeHasNVJump
= isNewValueJump(*SecondLastInst
);
560 if (SecLastOpcodeHasJMP_c
&& (LastOpcode
== Hexagon::J2_jump
)) {
561 if (!SecondLastInst
->getOperand(1).isMBB())
563 TBB
= SecondLastInst
->getOperand(1).getMBB();
564 Cond
.push_back(MachineOperand::CreateImm(SecondLastInst
->getOpcode()));
565 Cond
.push_back(SecondLastInst
->getOperand(0));
566 FBB
= LastInst
->getOperand(0).getMBB();
570 // Only supporting rr/ri versions of new-value jumps.
571 if (SecLastOpcodeHasNVJump
&&
572 (SecondLastInst
->getNumExplicitOperands() == 3) &&
573 (LastOpcode
== Hexagon::J2_jump
)) {
574 TBB
= SecondLastInst
->getOperand(2).getMBB();
575 Cond
.push_back(MachineOperand::CreateImm(SecondLastInst
->getOpcode()));
576 Cond
.push_back(SecondLastInst
->getOperand(0));
577 Cond
.push_back(SecondLastInst
->getOperand(1));
578 FBB
= LastInst
->getOperand(0).getMBB();
582 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
583 // executed, so remove it.
584 if (SecLastOpcode
== Hexagon::J2_jump
&& LastOpcode
== Hexagon::J2_jump
) {
585 TBB
= SecondLastInst
->getOperand(0).getMBB();
586 I
= LastInst
->getIterator();
588 I
->eraseFromParent();
592 // If the block ends with an ENDLOOP, and J2_jump, handle it.
593 if (isEndLoopN(SecLastOpcode
) && LastOpcode
== Hexagon::J2_jump
) {
594 TBB
= SecondLastInst
->getOperand(0).getMBB();
595 Cond
.push_back(MachineOperand::CreateImm(SecondLastInst
->getOpcode()));
596 Cond
.push_back(SecondLastInst
->getOperand(0));
597 FBB
= LastInst
->getOperand(0).getMBB();
600 LLVM_DEBUG(dbgs() << "\nCant analyze " << printMBBReference(MBB
)
601 << " with two jumps";);
602 // Otherwise, can't handle this.
606 unsigned HexagonInstrInfo::removeBranch(MachineBasicBlock
&MBB
,
607 int *BytesRemoved
) const {
608 assert(!BytesRemoved
&& "code size not handled");
610 LLVM_DEBUG(dbgs() << "\nRemoving branches out of " << printMBBReference(MBB
));
611 MachineBasicBlock::iterator I
= MBB
.end();
613 while (I
!= MBB
.begin()) {
615 if (I
->isDebugInstr())
617 // Only removing branches from end of MBB.
620 if (Count
&& (I
->getOpcode() == Hexagon::J2_jump
))
621 llvm_unreachable("Malformed basic block: unconditional branch not last");
622 MBB
.erase(&MBB
.back());
629 unsigned HexagonInstrInfo::insertBranch(MachineBasicBlock
&MBB
,
630 MachineBasicBlock
*TBB
,
631 MachineBasicBlock
*FBB
,
632 ArrayRef
<MachineOperand
> Cond
,
634 int *BytesAdded
) const {
635 unsigned BOpc
= Hexagon::J2_jump
;
636 unsigned BccOpc
= Hexagon::J2_jumpt
;
637 assert(validateBranchCond(Cond
) && "Invalid branching condition");
638 assert(TBB
&& "insertBranch must not be told to insert a fallthrough");
639 assert(!BytesAdded
&& "code size not handled");
641 // Check if reverseBranchCondition has asked to reverse this branch
642 // If we want to reverse the branch an odd number of times, we want
644 if (!Cond
.empty() && Cond
[0].isImm())
645 BccOpc
= Cond
[0].getImm();
649 // Due to a bug in TailMerging/CFG Optimization, we need to add a
650 // special case handling of a predicated jump followed by an
651 // unconditional jump. If not, Tail Merging and CFG Optimization go
652 // into an infinite loop.
653 MachineBasicBlock
*NewTBB
, *NewFBB
;
654 SmallVector
<MachineOperand
, 4> Cond
;
655 auto Term
= MBB
.getFirstTerminator();
656 if (Term
!= MBB
.end() && isPredicated(*Term
) &&
657 !analyzeBranch(MBB
, NewTBB
, NewFBB
, Cond
, false) &&
658 MachineFunction::iterator(NewTBB
) == ++MBB
.getIterator()) {
659 reverseBranchCondition(Cond
);
661 return insertBranch(MBB
, TBB
, nullptr, Cond
, DL
);
663 BuildMI(&MBB
, DL
, get(BOpc
)).addMBB(TBB
);
664 } else if (isEndLoopN(Cond
[0].getImm())) {
665 int EndLoopOp
= Cond
[0].getImm();
666 assert(Cond
[1].isMBB());
667 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
668 // Check for it, and change the BB target if needed.
669 SmallPtrSet
<MachineBasicBlock
*, 8> VisitedBBs
;
670 MachineInstr
*Loop
= findLoopInstr(TBB
, EndLoopOp
, Cond
[1].getMBB(),
672 assert(Loop
!= nullptr && "Inserting an ENDLOOP without a LOOP");
673 Loop
->getOperand(0).setMBB(TBB
);
674 // Add the ENDLOOP after the finding the LOOP0.
675 BuildMI(&MBB
, DL
, get(EndLoopOp
)).addMBB(TBB
);
676 } else if (isNewValueJump(Cond
[0].getImm())) {
677 assert((Cond
.size() == 3) && "Only supporting rr/ri version of nvjump");
679 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
680 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
681 unsigned Flags1
= getUndefRegState(Cond
[1].isUndef());
682 LLVM_DEBUG(dbgs() << "\nInserting NVJump for "
683 << printMBBReference(MBB
););
684 if (Cond
[2].isReg()) {
685 unsigned Flags2
= getUndefRegState(Cond
[2].isUndef());
686 BuildMI(&MBB
, DL
, get(BccOpc
)).addReg(Cond
[1].getReg(), Flags1
).
687 addReg(Cond
[2].getReg(), Flags2
).addMBB(TBB
);
688 } else if(Cond
[2].isImm()) {
689 BuildMI(&MBB
, DL
, get(BccOpc
)).addReg(Cond
[1].getReg(), Flags1
).
690 addImm(Cond
[2].getImm()).addMBB(TBB
);
692 llvm_unreachable("Invalid condition for branching");
694 assert((Cond
.size() == 2) && "Malformed cond vector");
695 const MachineOperand
&RO
= Cond
[1];
696 unsigned Flags
= getUndefRegState(RO
.isUndef());
697 BuildMI(&MBB
, DL
, get(BccOpc
)).addReg(RO
.getReg(), Flags
).addMBB(TBB
);
701 assert((!Cond
.empty()) &&
702 "Cond. cannot be empty when multiple branchings are required");
703 assert((!isNewValueJump(Cond
[0].getImm())) &&
704 "NV-jump cannot be inserted with another branch");
705 // Special case for hardware loops. The condition is a basic block.
706 if (isEndLoopN(Cond
[0].getImm())) {
707 int EndLoopOp
= Cond
[0].getImm();
708 assert(Cond
[1].isMBB());
709 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
710 // Check for it, and change the BB target if needed.
711 SmallPtrSet
<MachineBasicBlock
*, 8> VisitedBBs
;
712 MachineInstr
*Loop
= findLoopInstr(TBB
, EndLoopOp
, Cond
[1].getMBB(),
714 assert(Loop
!= nullptr && "Inserting an ENDLOOP without a LOOP");
715 Loop
->getOperand(0).setMBB(TBB
);
716 // Add the ENDLOOP after the finding the LOOP0.
717 BuildMI(&MBB
, DL
, get(EndLoopOp
)).addMBB(TBB
);
719 const MachineOperand
&RO
= Cond
[1];
720 unsigned Flags
= getUndefRegState(RO
.isUndef());
721 BuildMI(&MBB
, DL
, get(BccOpc
)).addReg(RO
.getReg(), Flags
).addMBB(TBB
);
723 BuildMI(&MBB
, DL
, get(BOpc
)).addMBB(FBB
);
729 class HexagonPipelinerLoopInfo
: public TargetInstrInfo::PipelinerLoopInfo
{
730 MachineInstr
*Loop
, *EndLoop
;
732 const HexagonInstrInfo
*TII
;
738 HexagonPipelinerLoopInfo(MachineInstr
*Loop
, MachineInstr
*EndLoop
)
739 : Loop(Loop
), EndLoop(EndLoop
), MF(Loop
->getParent()->getParent()),
740 TII(MF
->getSubtarget
<HexagonSubtarget
>().getInstrInfo()),
741 DL(Loop
->getDebugLoc()) {
742 // Inspect the Loop instruction up-front, as it may be deleted when we call
743 // createTripCountGreaterCondition.
744 TripCount
= Loop
->getOpcode() == Hexagon::J2_loop0r
746 : Loop
->getOperand(1).getImm();
748 LoopCount
= Loop
->getOperand(1).getReg();
751 bool shouldIgnoreForPipelining(const MachineInstr
*MI
) const override
{
752 // Only ignore the terminator.
753 return MI
== EndLoop
;
756 std::optional
<bool> createTripCountGreaterCondition(
757 int TC
, MachineBasicBlock
&MBB
,
758 SmallVectorImpl
<MachineOperand
> &Cond
) override
{
759 if (TripCount
== -1) {
760 // Check if we're done with the loop.
761 Register Done
= TII
->createVR(MF
, MVT::i1
);
762 MachineInstr
*NewCmp
= BuildMI(&MBB
, DL
,
763 TII
->get(Hexagon::C2_cmpgtui
), Done
)
766 Cond
.push_back(MachineOperand::CreateImm(Hexagon::J2_jumpf
));
767 Cond
.push_back(NewCmp
->getOperand(0));
771 return TripCount
> TC
;
774 void setPreheader(MachineBasicBlock
*NewPreheader
) override
{
775 NewPreheader
->splice(NewPreheader
->getFirstTerminator(), Loop
->getParent(),
779 void adjustTripCount(int TripCountAdjust
) override
{
780 // If the loop trip count is a compile-time value, then just change the
782 if (Loop
->getOpcode() == Hexagon::J2_loop0i
||
783 Loop
->getOpcode() == Hexagon::J2_loop1i
) {
784 int64_t TripCount
= Loop
->getOperand(1).getImm() + TripCountAdjust
;
785 assert(TripCount
> 0 && "Can't create an empty or negative loop!");
786 Loop
->getOperand(1).setImm(TripCount
);
790 // The loop trip count is a run-time value. We generate code to subtract
791 // one from the trip count, and update the loop instruction.
792 Register LoopCount
= Loop
->getOperand(1).getReg();
793 Register NewLoopCount
= TII
->createVR(MF
, MVT::i32
);
794 BuildMI(*Loop
->getParent(), Loop
, Loop
->getDebugLoc(),
795 TII
->get(Hexagon::A2_addi
), NewLoopCount
)
797 .addImm(TripCountAdjust
);
798 Loop
->getOperand(1).setReg(NewLoopCount
);
801 void disposed() override
{ Loop
->eraseFromParent(); }
805 std::unique_ptr
<TargetInstrInfo::PipelinerLoopInfo
>
806 HexagonInstrInfo::analyzeLoopForPipelining(MachineBasicBlock
*LoopBB
) const {
807 // We really "analyze" only hardware loops right now.
808 MachineBasicBlock::iterator I
= LoopBB
->getFirstTerminator();
810 if (I
!= LoopBB
->end() && isEndLoopN(I
->getOpcode())) {
811 SmallPtrSet
<MachineBasicBlock
*, 8> VisitedBBs
;
812 MachineInstr
*LoopInst
= findLoopInstr(
813 LoopBB
, I
->getOpcode(), I
->getOperand(0).getMBB(), VisitedBBs
);
815 return std::make_unique
<HexagonPipelinerLoopInfo
>(LoopInst
, &*I
);
820 bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock
&MBB
,
821 unsigned NumCycles
, unsigned ExtraPredCycles
,
822 BranchProbability Probability
) const {
823 return nonDbgBBSize(&MBB
) <= 3;
826 bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock
&TMBB
,
827 unsigned NumTCycles
, unsigned ExtraTCycles
, MachineBasicBlock
&FMBB
,
828 unsigned NumFCycles
, unsigned ExtraFCycles
, BranchProbability Probability
)
830 return nonDbgBBSize(&TMBB
) <= 3 && nonDbgBBSize(&FMBB
) <= 3;
833 bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock
&MBB
,
834 unsigned NumInstrs
, BranchProbability Probability
) const {
835 return NumInstrs
<= 4;
838 static void getLiveInRegsAt(LivePhysRegs
&Regs
, const MachineInstr
&MI
) {
839 SmallVector
<std::pair
<MCPhysReg
, const MachineOperand
*>,2> Clobbers
;
840 const MachineBasicBlock
&B
= *MI
.getParent();
842 auto E
= MachineBasicBlock::const_iterator(MI
.getIterator());
843 for (auto I
= B
.begin(); I
!= E
; ++I
) {
845 Regs
.stepForward(*I
, Clobbers
);
849 static void getLiveOutRegsAt(LivePhysRegs
&Regs
, const MachineInstr
&MI
) {
850 const MachineBasicBlock
&B
= *MI
.getParent();
852 auto E
= ++MachineBasicBlock::const_iterator(MI
.getIterator()).getReverse();
853 for (auto I
= B
.rbegin(); I
!= E
; ++I
)
854 Regs
.stepBackward(*I
);
857 void HexagonInstrInfo::copyPhysReg(MachineBasicBlock
&MBB
,
858 MachineBasicBlock::iterator I
,
859 const DebugLoc
&DL
, MCRegister DestReg
,
860 MCRegister SrcReg
, bool KillSrc
) const {
861 const HexagonRegisterInfo
&HRI
= *Subtarget
.getRegisterInfo();
862 unsigned KillFlag
= getKillRegState(KillSrc
);
864 if (Hexagon::IntRegsRegClass
.contains(SrcReg
, DestReg
)) {
865 BuildMI(MBB
, I
, DL
, get(Hexagon::A2_tfr
), DestReg
)
866 .addReg(SrcReg
, KillFlag
);
869 if (Hexagon::DoubleRegsRegClass
.contains(SrcReg
, DestReg
)) {
870 BuildMI(MBB
, I
, DL
, get(Hexagon::A2_tfrp
), DestReg
)
871 .addReg(SrcReg
, KillFlag
);
874 if (Hexagon::PredRegsRegClass
.contains(SrcReg
, DestReg
)) {
875 // Map Pd = Ps to Pd = or(Ps, Ps).
876 BuildMI(MBB
, I
, DL
, get(Hexagon::C2_or
), DestReg
)
877 .addReg(SrcReg
).addReg(SrcReg
, KillFlag
);
880 if (Hexagon::CtrRegsRegClass
.contains(DestReg
) &&
881 Hexagon::IntRegsRegClass
.contains(SrcReg
)) {
882 BuildMI(MBB
, I
, DL
, get(Hexagon::A2_tfrrcr
), DestReg
)
883 .addReg(SrcReg
, KillFlag
);
886 if (Hexagon::IntRegsRegClass
.contains(DestReg
) &&
887 Hexagon::CtrRegsRegClass
.contains(SrcReg
)) {
888 BuildMI(MBB
, I
, DL
, get(Hexagon::A2_tfrcrr
), DestReg
)
889 .addReg(SrcReg
, KillFlag
);
892 if (Hexagon::ModRegsRegClass
.contains(DestReg
) &&
893 Hexagon::IntRegsRegClass
.contains(SrcReg
)) {
894 BuildMI(MBB
, I
, DL
, get(Hexagon::A2_tfrrcr
), DestReg
)
895 .addReg(SrcReg
, KillFlag
);
898 if (Hexagon::PredRegsRegClass
.contains(SrcReg
) &&
899 Hexagon::IntRegsRegClass
.contains(DestReg
)) {
900 BuildMI(MBB
, I
, DL
, get(Hexagon::C2_tfrpr
), DestReg
)
901 .addReg(SrcReg
, KillFlag
);
904 if (Hexagon::IntRegsRegClass
.contains(SrcReg
) &&
905 Hexagon::PredRegsRegClass
.contains(DestReg
)) {
906 BuildMI(MBB
, I
, DL
, get(Hexagon::C2_tfrrp
), DestReg
)
907 .addReg(SrcReg
, KillFlag
);
910 if (Hexagon::PredRegsRegClass
.contains(SrcReg
) &&
911 Hexagon::IntRegsRegClass
.contains(DestReg
)) {
912 BuildMI(MBB
, I
, DL
, get(Hexagon::C2_tfrpr
), DestReg
)
913 .addReg(SrcReg
, KillFlag
);
916 if (Hexagon::HvxVRRegClass
.contains(SrcReg
, DestReg
)) {
917 BuildMI(MBB
, I
, DL
, get(Hexagon::V6_vassign
), DestReg
).
918 addReg(SrcReg
, KillFlag
);
921 if (Hexagon::HvxWRRegClass
.contains(SrcReg
, DestReg
)) {
922 LivePhysRegs
LiveAtMI(HRI
);
923 getLiveInRegsAt(LiveAtMI
, *I
);
924 Register SrcLo
= HRI
.getSubReg(SrcReg
, Hexagon::vsub_lo
);
925 Register SrcHi
= HRI
.getSubReg(SrcReg
, Hexagon::vsub_hi
);
926 unsigned UndefLo
= getUndefRegState(!LiveAtMI
.contains(SrcLo
));
927 unsigned UndefHi
= getUndefRegState(!LiveAtMI
.contains(SrcHi
));
928 BuildMI(MBB
, I
, DL
, get(Hexagon::V6_vcombine
), DestReg
)
929 .addReg(SrcHi
, KillFlag
| UndefHi
)
930 .addReg(SrcLo
, KillFlag
| UndefLo
);
933 if (Hexagon::HvxQRRegClass
.contains(SrcReg
, DestReg
)) {
934 BuildMI(MBB
, I
, DL
, get(Hexagon::V6_pred_and
), DestReg
)
936 .addReg(SrcReg
, KillFlag
);
939 if (Hexagon::HvxQRRegClass
.contains(SrcReg
) &&
940 Hexagon::HvxVRRegClass
.contains(DestReg
)) {
941 llvm_unreachable("Unimplemented pred to vec");
944 if (Hexagon::HvxQRRegClass
.contains(DestReg
) &&
945 Hexagon::HvxVRRegClass
.contains(SrcReg
)) {
946 llvm_unreachable("Unimplemented vec to pred");
951 // Show the invalid registers to ease debugging.
952 dbgs() << "Invalid registers for copy in " << printMBBReference(MBB
) << ": "
953 << printReg(DestReg
, &HRI
) << " = " << printReg(SrcReg
, &HRI
) << '\n';
955 llvm_unreachable("Unimplemented");
958 void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock
&MBB
,
959 MachineBasicBlock::iterator I
,
960 Register SrcReg
, bool isKill
, int FI
,
961 const TargetRegisterClass
*RC
,
962 const TargetRegisterInfo
*TRI
,
963 Register VReg
) const {
964 DebugLoc DL
= MBB
.findDebugLoc(I
);
965 MachineFunction
&MF
= *MBB
.getParent();
966 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
967 unsigned KillFlag
= getKillRegState(isKill
);
969 MachineMemOperand
*MMO
= MF
.getMachineMemOperand(
970 MachinePointerInfo::getFixedStack(MF
, FI
), MachineMemOperand::MOStore
,
971 MFI
.getObjectSize(FI
), MFI
.getObjectAlign(FI
));
973 if (Hexagon::IntRegsRegClass
.hasSubClassEq(RC
)) {
974 BuildMI(MBB
, I
, DL
, get(Hexagon::S2_storeri_io
))
975 .addFrameIndex(FI
).addImm(0)
976 .addReg(SrcReg
, KillFlag
).addMemOperand(MMO
);
977 } else if (Hexagon::DoubleRegsRegClass
.hasSubClassEq(RC
)) {
978 BuildMI(MBB
, I
, DL
, get(Hexagon::S2_storerd_io
))
979 .addFrameIndex(FI
).addImm(0)
980 .addReg(SrcReg
, KillFlag
).addMemOperand(MMO
);
981 } else if (Hexagon::PredRegsRegClass
.hasSubClassEq(RC
)) {
982 BuildMI(MBB
, I
, DL
, get(Hexagon::STriw_pred
))
983 .addFrameIndex(FI
).addImm(0)
984 .addReg(SrcReg
, KillFlag
).addMemOperand(MMO
);
985 } else if (Hexagon::ModRegsRegClass
.hasSubClassEq(RC
)) {
986 BuildMI(MBB
, I
, DL
, get(Hexagon::STriw_ctr
))
987 .addFrameIndex(FI
).addImm(0)
988 .addReg(SrcReg
, KillFlag
).addMemOperand(MMO
);
989 } else if (Hexagon::HvxQRRegClass
.hasSubClassEq(RC
)) {
990 BuildMI(MBB
, I
, DL
, get(Hexagon::PS_vstorerq_ai
))
991 .addFrameIndex(FI
).addImm(0)
992 .addReg(SrcReg
, KillFlag
).addMemOperand(MMO
);
993 } else if (Hexagon::HvxVRRegClass
.hasSubClassEq(RC
)) {
994 BuildMI(MBB
, I
, DL
, get(Hexagon::PS_vstorerv_ai
))
995 .addFrameIndex(FI
).addImm(0)
996 .addReg(SrcReg
, KillFlag
).addMemOperand(MMO
);
997 } else if (Hexagon::HvxWRRegClass
.hasSubClassEq(RC
)) {
998 BuildMI(MBB
, I
, DL
, get(Hexagon::PS_vstorerw_ai
))
999 .addFrameIndex(FI
).addImm(0)
1000 .addReg(SrcReg
, KillFlag
).addMemOperand(MMO
);
1002 llvm_unreachable("Unimplemented");
1006 void HexagonInstrInfo::loadRegFromStackSlot(MachineBasicBlock
&MBB
,
1007 MachineBasicBlock::iterator I
,
1008 Register DestReg
, int FI
,
1009 const TargetRegisterClass
*RC
,
1010 const TargetRegisterInfo
*TRI
,
1011 Register VReg
) const {
1012 DebugLoc DL
= MBB
.findDebugLoc(I
);
1013 MachineFunction
&MF
= *MBB
.getParent();
1014 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
1016 MachineMemOperand
*MMO
= MF
.getMachineMemOperand(
1017 MachinePointerInfo::getFixedStack(MF
, FI
), MachineMemOperand::MOLoad
,
1018 MFI
.getObjectSize(FI
), MFI
.getObjectAlign(FI
));
1020 if (Hexagon::IntRegsRegClass
.hasSubClassEq(RC
)) {
1021 BuildMI(MBB
, I
, DL
, get(Hexagon::L2_loadri_io
), DestReg
)
1022 .addFrameIndex(FI
).addImm(0).addMemOperand(MMO
);
1023 } else if (Hexagon::DoubleRegsRegClass
.hasSubClassEq(RC
)) {
1024 BuildMI(MBB
, I
, DL
, get(Hexagon::L2_loadrd_io
), DestReg
)
1025 .addFrameIndex(FI
).addImm(0).addMemOperand(MMO
);
1026 } else if (Hexagon::PredRegsRegClass
.hasSubClassEq(RC
)) {
1027 BuildMI(MBB
, I
, DL
, get(Hexagon::LDriw_pred
), DestReg
)
1028 .addFrameIndex(FI
).addImm(0).addMemOperand(MMO
);
1029 } else if (Hexagon::ModRegsRegClass
.hasSubClassEq(RC
)) {
1030 BuildMI(MBB
, I
, DL
, get(Hexagon::LDriw_ctr
), DestReg
)
1031 .addFrameIndex(FI
).addImm(0).addMemOperand(MMO
);
1032 } else if (Hexagon::HvxQRRegClass
.hasSubClassEq(RC
)) {
1033 BuildMI(MBB
, I
, DL
, get(Hexagon::PS_vloadrq_ai
), DestReg
)
1034 .addFrameIndex(FI
).addImm(0).addMemOperand(MMO
);
1035 } else if (Hexagon::HvxVRRegClass
.hasSubClassEq(RC
)) {
1036 BuildMI(MBB
, I
, DL
, get(Hexagon::PS_vloadrv_ai
), DestReg
)
1037 .addFrameIndex(FI
).addImm(0).addMemOperand(MMO
);
1038 } else if (Hexagon::HvxWRRegClass
.hasSubClassEq(RC
)) {
1039 BuildMI(MBB
, I
, DL
, get(Hexagon::PS_vloadrw_ai
), DestReg
)
1040 .addFrameIndex(FI
).addImm(0).addMemOperand(MMO
);
1042 llvm_unreachable("Can't store this register to stack slot");
1046 /// expandPostRAPseudo - This function is called for all pseudo instructions
1047 /// that remain after register allocation. Many pseudo instructions are
1048 /// created to help register allocation. This is the place to convert them
1049 /// into real instructions. The target can edit MI in place, or it can insert
1050 /// new instructions and erase MI. The function should return true if
1051 /// anything was changed.
1052 bool HexagonInstrInfo::expandPostRAPseudo(MachineInstr
&MI
) const {
1053 MachineBasicBlock
&MBB
= *MI
.getParent();
1054 MachineFunction
&MF
= *MBB
.getParent();
1055 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
1056 const HexagonRegisterInfo
&HRI
= *Subtarget
.getRegisterInfo();
1057 LivePhysRegs
LiveIn(HRI
), LiveOut(HRI
);
1058 DebugLoc DL
= MI
.getDebugLoc();
1059 unsigned Opc
= MI
.getOpcode();
1061 auto RealCirc
= [&](unsigned Opc
, bool HasImm
, unsigned MxOp
) {
1062 Register Mx
= MI
.getOperand(MxOp
).getReg();
1063 Register CSx
= (Mx
== Hexagon::M0
? Hexagon::CS0
: Hexagon::CS1
);
1064 BuildMI(MBB
, MI
, DL
, get(Hexagon::A2_tfrrcr
), CSx
)
1065 .add(MI
.getOperand((HasImm
? 5 : 4)));
1066 auto MIB
= BuildMI(MBB
, MI
, DL
, get(Opc
)).add(MI
.getOperand(0))
1067 .add(MI
.getOperand(1)).add(MI
.getOperand(2)).add(MI
.getOperand(3));
1069 MIB
.add(MI
.getOperand(4));
1070 MIB
.addReg(CSx
, RegState::Implicit
);
1075 auto UseAligned
= [&](const MachineInstr
&MI
, Align NeedAlign
) {
1076 if (MI
.memoperands().empty())
1078 return all_of(MI
.memoperands(), [NeedAlign
](const MachineMemOperand
*MMO
) {
1079 return MMO
->getAlign() >= NeedAlign
;
1084 case Hexagon::PS_call_instrprof_custom
: {
1085 auto Op0
= MI
.getOperand(0);
1086 assert(Op0
.isGlobal() &&
1087 "First operand must be a global containing handler name.");
1088 const GlobalValue
*NameVar
= Op0
.getGlobal();
1089 const GlobalVariable
*GV
= dyn_cast
<GlobalVariable
>(NameVar
);
1090 auto *Arr
= cast
<ConstantDataArray
>(GV
->getInitializer());
1091 StringRef NameStr
= Arr
->isCString() ? Arr
->getAsCString() : Arr
->getAsString();
1093 MachineOperand
&Op1
= MI
.getOperand(1);
1094 // Set R0 with the imm value to be passed to the custom profiling handler.
1095 BuildMI(MBB
, MI
, DL
, get(Hexagon::A2_tfrsi
), Hexagon::R0
)
1096 .addImm(Op1
.getImm());
1097 // The call to the custom handler is being treated as a special one as the
1098 // callee is responsible for saving and restoring all the registers
1099 // (including caller saved registers) it needs to modify. This is
1100 // done to reduce the impact of instrumentation on the code being
1101 // instrumented/profiled.
1102 // NOTE: R14, R15 and R28 are reserved for PLT handling. These registers
1103 // are in the Def list of the Hexagon::PS_call_instrprof_custom and
1104 // therefore will be handled appropriately duing register allocation.
1106 // TODO: It may be a good idea to add a separate pseudo instruction for
1107 // static relocation which doesn't need to reserve r14, r15 and r28.
1109 auto MIB
= BuildMI(MBB
, MI
, DL
, get(Hexagon::J2_call
))
1110 .addUse(Hexagon::R0
, RegState::Implicit
|RegState::InternalRead
)
1111 .addDef(Hexagon::R29
, RegState::ImplicitDefine
)
1112 .addDef(Hexagon::R30
, RegState::ImplicitDefine
)
1113 .addDef(Hexagon::R14
, RegState::ImplicitDefine
)
1114 .addDef(Hexagon::R15
, RegState::ImplicitDefine
)
1115 .addDef(Hexagon::R28
, RegState::ImplicitDefine
);
1116 const char *cstr
= MF
.createExternalSymbolName(NameStr
);
1117 MIB
.addExternalSymbol(cstr
);
1121 case TargetOpcode::COPY
: {
1122 MachineOperand
&MD
= MI
.getOperand(0);
1123 MachineOperand
&MS
= MI
.getOperand(1);
1124 MachineBasicBlock::iterator MBBI
= MI
.getIterator();
1125 if (MD
.getReg() != MS
.getReg() && !MS
.isUndef()) {
1126 copyPhysReg(MBB
, MI
, DL
, MD
.getReg(), MS
.getReg(), MS
.isKill());
1127 std::prev(MBBI
)->copyImplicitOps(*MBB
.getParent(), MI
);
1132 case Hexagon::PS_aligna
:
1133 BuildMI(MBB
, MI
, DL
, get(Hexagon::A2_andir
), MI
.getOperand(0).getReg())
1134 .addReg(HRI
.getFrameRegister())
1135 .addImm(-MI
.getOperand(1).getImm());
1138 case Hexagon::V6_vassignp
: {
1139 Register SrcReg
= MI
.getOperand(1).getReg();
1140 Register DstReg
= MI
.getOperand(0).getReg();
1141 Register SrcLo
= HRI
.getSubReg(SrcReg
, Hexagon::vsub_lo
);
1142 Register SrcHi
= HRI
.getSubReg(SrcReg
, Hexagon::vsub_hi
);
1143 getLiveInRegsAt(LiveIn
, MI
);
1144 unsigned UndefLo
= getUndefRegState(!LiveIn
.contains(SrcLo
));
1145 unsigned UndefHi
= getUndefRegState(!LiveIn
.contains(SrcHi
));
1146 unsigned Kill
= getKillRegState(MI
.getOperand(1).isKill());
1147 BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_vcombine
), DstReg
)
1148 .addReg(SrcHi
, UndefHi
)
1149 .addReg(SrcLo
, Kill
| UndefLo
);
1153 case Hexagon::V6_lo
: {
1154 Register SrcReg
= MI
.getOperand(1).getReg();
1155 Register DstReg
= MI
.getOperand(0).getReg();
1156 Register SrcSubLo
= HRI
.getSubReg(SrcReg
, Hexagon::vsub_lo
);
1157 copyPhysReg(MBB
, MI
, DL
, DstReg
, SrcSubLo
, MI
.getOperand(1).isKill());
1159 MRI
.clearKillFlags(SrcSubLo
);
1162 case Hexagon::V6_hi
: {
1163 Register SrcReg
= MI
.getOperand(1).getReg();
1164 Register DstReg
= MI
.getOperand(0).getReg();
1165 Register SrcSubHi
= HRI
.getSubReg(SrcReg
, Hexagon::vsub_hi
);
1166 copyPhysReg(MBB
, MI
, DL
, DstReg
, SrcSubHi
, MI
.getOperand(1).isKill());
1168 MRI
.clearKillFlags(SrcSubHi
);
1171 case Hexagon::PS_vloadrv_ai
: {
1172 Register DstReg
= MI
.getOperand(0).getReg();
1173 const MachineOperand
&BaseOp
= MI
.getOperand(1);
1174 assert(BaseOp
.getSubReg() == 0);
1175 int Offset
= MI
.getOperand(2).getImm();
1176 Align NeedAlign
= HRI
.getSpillAlign(Hexagon::HvxVRRegClass
);
1177 unsigned NewOpc
= UseAligned(MI
, NeedAlign
) ? Hexagon::V6_vL32b_ai
1178 : Hexagon::V6_vL32Ub_ai
;
1179 BuildMI(MBB
, MI
, DL
, get(NewOpc
), DstReg
)
1180 .addReg(BaseOp
.getReg(), getRegState(BaseOp
))
1186 case Hexagon::PS_vloadrw_ai
: {
1187 Register DstReg
= MI
.getOperand(0).getReg();
1188 const MachineOperand
&BaseOp
= MI
.getOperand(1);
1189 assert(BaseOp
.getSubReg() == 0);
1190 int Offset
= MI
.getOperand(2).getImm();
1191 unsigned VecOffset
= HRI
.getSpillSize(Hexagon::HvxVRRegClass
);
1192 Align NeedAlign
= HRI
.getSpillAlign(Hexagon::HvxVRRegClass
);
1193 unsigned NewOpc
= UseAligned(MI
, NeedAlign
) ? Hexagon::V6_vL32b_ai
1194 : Hexagon::V6_vL32Ub_ai
;
1195 BuildMI(MBB
, MI
, DL
, get(NewOpc
),
1196 HRI
.getSubReg(DstReg
, Hexagon::vsub_lo
))
1197 .addReg(BaseOp
.getReg(), getRegState(BaseOp
) & ~RegState::Kill
)
1200 BuildMI(MBB
, MI
, DL
, get(NewOpc
),
1201 HRI
.getSubReg(DstReg
, Hexagon::vsub_hi
))
1202 .addReg(BaseOp
.getReg(), getRegState(BaseOp
))
1203 .addImm(Offset
+ VecOffset
)
1208 case Hexagon::PS_vstorerv_ai
: {
1209 const MachineOperand
&SrcOp
= MI
.getOperand(2);
1210 assert(SrcOp
.getSubReg() == 0);
1211 const MachineOperand
&BaseOp
= MI
.getOperand(0);
1212 assert(BaseOp
.getSubReg() == 0);
1213 int Offset
= MI
.getOperand(1).getImm();
1214 Align NeedAlign
= HRI
.getSpillAlign(Hexagon::HvxVRRegClass
);
1215 unsigned NewOpc
= UseAligned(MI
, NeedAlign
) ? Hexagon::V6_vS32b_ai
1216 : Hexagon::V6_vS32Ub_ai
;
1217 BuildMI(MBB
, MI
, DL
, get(NewOpc
))
1218 .addReg(BaseOp
.getReg(), getRegState(BaseOp
))
1220 .addReg(SrcOp
.getReg(), getRegState(SrcOp
))
1225 case Hexagon::PS_vstorerw_ai
: {
1226 Register SrcReg
= MI
.getOperand(2).getReg();
1227 const MachineOperand
&BaseOp
= MI
.getOperand(0);
1228 assert(BaseOp
.getSubReg() == 0);
1229 int Offset
= MI
.getOperand(1).getImm();
1230 unsigned VecOffset
= HRI
.getSpillSize(Hexagon::HvxVRRegClass
);
1231 Align NeedAlign
= HRI
.getSpillAlign(Hexagon::HvxVRRegClass
);
1232 unsigned NewOpc
= UseAligned(MI
, NeedAlign
) ? Hexagon::V6_vS32b_ai
1233 : Hexagon::V6_vS32Ub_ai
;
1234 BuildMI(MBB
, MI
, DL
, get(NewOpc
))
1235 .addReg(BaseOp
.getReg(), getRegState(BaseOp
) & ~RegState::Kill
)
1237 .addReg(HRI
.getSubReg(SrcReg
, Hexagon::vsub_lo
))
1239 BuildMI(MBB
, MI
, DL
, get(NewOpc
))
1240 .addReg(BaseOp
.getReg(), getRegState(BaseOp
))
1241 .addImm(Offset
+ VecOffset
)
1242 .addReg(HRI
.getSubReg(SrcReg
, Hexagon::vsub_hi
))
1247 case Hexagon::PS_true
: {
1248 Register Reg
= MI
.getOperand(0).getReg();
1249 BuildMI(MBB
, MI
, DL
, get(Hexagon::C2_orn
), Reg
)
1250 .addReg(Reg
, RegState::Undef
)
1251 .addReg(Reg
, RegState::Undef
);
1255 case Hexagon::PS_false
: {
1256 Register Reg
= MI
.getOperand(0).getReg();
1257 BuildMI(MBB
, MI
, DL
, get(Hexagon::C2_andn
), Reg
)
1258 .addReg(Reg
, RegState::Undef
)
1259 .addReg(Reg
, RegState::Undef
);
1263 case Hexagon::PS_qtrue
: {
1264 BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_veqw
), MI
.getOperand(0).getReg())
1265 .addReg(Hexagon::V0
, RegState::Undef
)
1266 .addReg(Hexagon::V0
, RegState::Undef
);
1270 case Hexagon::PS_qfalse
: {
1271 BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_vgtw
), MI
.getOperand(0).getReg())
1272 .addReg(Hexagon::V0
, RegState::Undef
)
1273 .addReg(Hexagon::V0
, RegState::Undef
);
1277 case Hexagon::PS_vdd0
: {
1278 Register Vd
= MI
.getOperand(0).getReg();
1279 BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_vsubw_dv
), Vd
)
1280 .addReg(Vd
, RegState::Undef
)
1281 .addReg(Vd
, RegState::Undef
);
1285 case Hexagon::PS_vmulw
: {
1286 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
1287 Register DstReg
= MI
.getOperand(0).getReg();
1288 Register Src1Reg
= MI
.getOperand(1).getReg();
1289 Register Src2Reg
= MI
.getOperand(2).getReg();
1290 Register Src1SubHi
= HRI
.getSubReg(Src1Reg
, Hexagon::isub_hi
);
1291 Register Src1SubLo
= HRI
.getSubReg(Src1Reg
, Hexagon::isub_lo
);
1292 Register Src2SubHi
= HRI
.getSubReg(Src2Reg
, Hexagon::isub_hi
);
1293 Register Src2SubLo
= HRI
.getSubReg(Src2Reg
, Hexagon::isub_lo
);
1294 BuildMI(MBB
, MI
, MI
.getDebugLoc(), get(Hexagon::M2_mpyi
),
1295 HRI
.getSubReg(DstReg
, Hexagon::isub_hi
))
1298 BuildMI(MBB
, MI
, MI
.getDebugLoc(), get(Hexagon::M2_mpyi
),
1299 HRI
.getSubReg(DstReg
, Hexagon::isub_lo
))
1303 MRI
.clearKillFlags(Src1SubHi
);
1304 MRI
.clearKillFlags(Src1SubLo
);
1305 MRI
.clearKillFlags(Src2SubHi
);
1306 MRI
.clearKillFlags(Src2SubLo
);
1309 case Hexagon::PS_vmulw_acc
: {
1310 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
1311 Register DstReg
= MI
.getOperand(0).getReg();
1312 Register Src1Reg
= MI
.getOperand(1).getReg();
1313 Register Src2Reg
= MI
.getOperand(2).getReg();
1314 Register Src3Reg
= MI
.getOperand(3).getReg();
1315 Register Src1SubHi
= HRI
.getSubReg(Src1Reg
, Hexagon::isub_hi
);
1316 Register Src1SubLo
= HRI
.getSubReg(Src1Reg
, Hexagon::isub_lo
);
1317 Register Src2SubHi
= HRI
.getSubReg(Src2Reg
, Hexagon::isub_hi
);
1318 Register Src2SubLo
= HRI
.getSubReg(Src2Reg
, Hexagon::isub_lo
);
1319 Register Src3SubHi
= HRI
.getSubReg(Src3Reg
, Hexagon::isub_hi
);
1320 Register Src3SubLo
= HRI
.getSubReg(Src3Reg
, Hexagon::isub_lo
);
1321 BuildMI(MBB
, MI
, MI
.getDebugLoc(), get(Hexagon::M2_maci
),
1322 HRI
.getSubReg(DstReg
, Hexagon::isub_hi
))
1326 BuildMI(MBB
, MI
, MI
.getDebugLoc(), get(Hexagon::M2_maci
),
1327 HRI
.getSubReg(DstReg
, Hexagon::isub_lo
))
1332 MRI
.clearKillFlags(Src1SubHi
);
1333 MRI
.clearKillFlags(Src1SubLo
);
1334 MRI
.clearKillFlags(Src2SubHi
);
1335 MRI
.clearKillFlags(Src2SubLo
);
1336 MRI
.clearKillFlags(Src3SubHi
);
1337 MRI
.clearKillFlags(Src3SubLo
);
1340 case Hexagon::PS_pselect
: {
1341 const MachineOperand
&Op0
= MI
.getOperand(0);
1342 const MachineOperand
&Op1
= MI
.getOperand(1);
1343 const MachineOperand
&Op2
= MI
.getOperand(2);
1344 const MachineOperand
&Op3
= MI
.getOperand(3);
1345 Register Rd
= Op0
.getReg();
1346 Register Pu
= Op1
.getReg();
1347 Register Rs
= Op2
.getReg();
1348 Register Rt
= Op3
.getReg();
1349 DebugLoc DL
= MI
.getDebugLoc();
1350 unsigned K1
= getKillRegState(Op1
.isKill());
1351 unsigned K2
= getKillRegState(Op2
.isKill());
1352 unsigned K3
= getKillRegState(Op3
.isKill());
1354 BuildMI(MBB
, MI
, DL
, get(Hexagon::A2_tfrpt
), Rd
)
1355 .addReg(Pu
, (Rd
== Rt
) ? K1
: 0)
1358 BuildMI(MBB
, MI
, DL
, get(Hexagon::A2_tfrpf
), Rd
)
1364 case Hexagon::PS_vselect
: {
1365 const MachineOperand
&Op0
= MI
.getOperand(0);
1366 const MachineOperand
&Op1
= MI
.getOperand(1);
1367 const MachineOperand
&Op2
= MI
.getOperand(2);
1368 const MachineOperand
&Op3
= MI
.getOperand(3);
1369 getLiveOutRegsAt(LiveOut
, MI
);
1370 bool IsDestLive
= !LiveOut
.available(MRI
, Op0
.getReg());
1371 Register PReg
= Op1
.getReg();
1372 assert(Op1
.getSubReg() == 0);
1373 unsigned PState
= getRegState(Op1
);
1375 if (Op0
.getReg() != Op2
.getReg()) {
1376 unsigned S
= Op0
.getReg() != Op3
.getReg() ? PState
& ~RegState::Kill
1378 auto T
= BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_vcmov
))
1383 T
.addReg(Op0
.getReg(), RegState::Implicit
);
1386 if (Op0
.getReg() != Op3
.getReg()) {
1387 auto T
= BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_vncmov
))
1389 .addReg(PReg
, PState
)
1392 T
.addReg(Op0
.getReg(), RegState::Implicit
);
1397 case Hexagon::PS_wselect
: {
1398 MachineOperand
&Op0
= MI
.getOperand(0);
1399 MachineOperand
&Op1
= MI
.getOperand(1);
1400 MachineOperand
&Op2
= MI
.getOperand(2);
1401 MachineOperand
&Op3
= MI
.getOperand(3);
1402 getLiveOutRegsAt(LiveOut
, MI
);
1403 bool IsDestLive
= !LiveOut
.available(MRI
, Op0
.getReg());
1404 Register PReg
= Op1
.getReg();
1405 assert(Op1
.getSubReg() == 0);
1406 unsigned PState
= getRegState(Op1
);
1408 if (Op0
.getReg() != Op2
.getReg()) {
1409 unsigned S
= Op0
.getReg() != Op3
.getReg() ? PState
& ~RegState::Kill
1411 Register SrcLo
= HRI
.getSubReg(Op2
.getReg(), Hexagon::vsub_lo
);
1412 Register SrcHi
= HRI
.getSubReg(Op2
.getReg(), Hexagon::vsub_hi
);
1413 auto T
= BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_vccombine
))
1419 T
.addReg(Op0
.getReg(), RegState::Implicit
);
1422 if (Op0
.getReg() != Op3
.getReg()) {
1423 Register SrcLo
= HRI
.getSubReg(Op3
.getReg(), Hexagon::vsub_lo
);
1424 Register SrcHi
= HRI
.getSubReg(Op3
.getReg(), Hexagon::vsub_hi
);
1425 auto T
= BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_vnccombine
))
1427 .addReg(PReg
, PState
)
1431 T
.addReg(Op0
.getReg(), RegState::Implicit
);
1437 case Hexagon::PS_crash
: {
1438 // Generate a misaligned load that is guaranteed to cause a crash.
1439 class CrashPseudoSourceValue
: public PseudoSourceValue
{
1441 CrashPseudoSourceValue(const TargetMachine
&TM
)
1442 : PseudoSourceValue(TargetCustom
, TM
) {}
1444 bool isConstant(const MachineFrameInfo
*) const override
{
1447 bool isAliased(const MachineFrameInfo
*) const override
{
1450 bool mayAlias(const MachineFrameInfo
*) const override
{
1453 void printCustom(raw_ostream
&OS
) const override
{
1454 OS
<< "MisalignedCrash";
1458 static const CrashPseudoSourceValue
CrashPSV(MF
.getTarget());
1459 MachineMemOperand
*MMO
= MF
.getMachineMemOperand(
1460 MachinePointerInfo(&CrashPSV
),
1461 MachineMemOperand::MOLoad
| MachineMemOperand::MOVolatile
, 8,
1463 BuildMI(MBB
, MI
, DL
, get(Hexagon::PS_loadrdabs
), Hexagon::D13
)
1464 .addImm(0xBADC0FEE) // Misaligned load.
1465 .addMemOperand(MMO
);
1470 case Hexagon::PS_tailcall_i
:
1471 MI
.setDesc(get(Hexagon::J2_jump
));
1473 case Hexagon::PS_tailcall_r
:
1474 case Hexagon::PS_jmpret
:
1475 MI
.setDesc(get(Hexagon::J2_jumpr
));
1477 case Hexagon::PS_jmprett
:
1478 MI
.setDesc(get(Hexagon::J2_jumprt
));
1480 case Hexagon::PS_jmpretf
:
1481 MI
.setDesc(get(Hexagon::J2_jumprf
));
1483 case Hexagon::PS_jmprettnewpt
:
1484 MI
.setDesc(get(Hexagon::J2_jumprtnewpt
));
1486 case Hexagon::PS_jmpretfnewpt
:
1487 MI
.setDesc(get(Hexagon::J2_jumprfnewpt
));
1489 case Hexagon::PS_jmprettnew
:
1490 MI
.setDesc(get(Hexagon::J2_jumprtnew
));
1492 case Hexagon::PS_jmpretfnew
:
1493 MI
.setDesc(get(Hexagon::J2_jumprfnew
));
1496 case Hexagon::PS_loadrub_pci
:
1497 return RealCirc(Hexagon::L2_loadrub_pci
, /*HasImm*/true, /*MxOp*/4);
1498 case Hexagon::PS_loadrb_pci
:
1499 return RealCirc(Hexagon::L2_loadrb_pci
, /*HasImm*/true, /*MxOp*/4);
1500 case Hexagon::PS_loadruh_pci
:
1501 return RealCirc(Hexagon::L2_loadruh_pci
, /*HasImm*/true, /*MxOp*/4);
1502 case Hexagon::PS_loadrh_pci
:
1503 return RealCirc(Hexagon::L2_loadrh_pci
, /*HasImm*/true, /*MxOp*/4);
1504 case Hexagon::PS_loadri_pci
:
1505 return RealCirc(Hexagon::L2_loadri_pci
, /*HasImm*/true, /*MxOp*/4);
1506 case Hexagon::PS_loadrd_pci
:
1507 return RealCirc(Hexagon::L2_loadrd_pci
, /*HasImm*/true, /*MxOp*/4);
1508 case Hexagon::PS_loadrub_pcr
:
1509 return RealCirc(Hexagon::L2_loadrub_pcr
, /*HasImm*/false, /*MxOp*/3);
1510 case Hexagon::PS_loadrb_pcr
:
1511 return RealCirc(Hexagon::L2_loadrb_pcr
, /*HasImm*/false, /*MxOp*/3);
1512 case Hexagon::PS_loadruh_pcr
:
1513 return RealCirc(Hexagon::L2_loadruh_pcr
, /*HasImm*/false, /*MxOp*/3);
1514 case Hexagon::PS_loadrh_pcr
:
1515 return RealCirc(Hexagon::L2_loadrh_pcr
, /*HasImm*/false, /*MxOp*/3);
1516 case Hexagon::PS_loadri_pcr
:
1517 return RealCirc(Hexagon::L2_loadri_pcr
, /*HasImm*/false, /*MxOp*/3);
1518 case Hexagon::PS_loadrd_pcr
:
1519 return RealCirc(Hexagon::L2_loadrd_pcr
, /*HasImm*/false, /*MxOp*/3);
1520 case Hexagon::PS_storerb_pci
:
1521 return RealCirc(Hexagon::S2_storerb_pci
, /*HasImm*/true, /*MxOp*/3);
1522 case Hexagon::PS_storerh_pci
:
1523 return RealCirc(Hexagon::S2_storerh_pci
, /*HasImm*/true, /*MxOp*/3);
1524 case Hexagon::PS_storerf_pci
:
1525 return RealCirc(Hexagon::S2_storerf_pci
, /*HasImm*/true, /*MxOp*/3);
1526 case Hexagon::PS_storeri_pci
:
1527 return RealCirc(Hexagon::S2_storeri_pci
, /*HasImm*/true, /*MxOp*/3);
1528 case Hexagon::PS_storerd_pci
:
1529 return RealCirc(Hexagon::S2_storerd_pci
, /*HasImm*/true, /*MxOp*/3);
1530 case Hexagon::PS_storerb_pcr
:
1531 return RealCirc(Hexagon::S2_storerb_pcr
, /*HasImm*/false, /*MxOp*/2);
1532 case Hexagon::PS_storerh_pcr
:
1533 return RealCirc(Hexagon::S2_storerh_pcr
, /*HasImm*/false, /*MxOp*/2);
1534 case Hexagon::PS_storerf_pcr
:
1535 return RealCirc(Hexagon::S2_storerf_pcr
, /*HasImm*/false, /*MxOp*/2);
1536 case Hexagon::PS_storeri_pcr
:
1537 return RealCirc(Hexagon::S2_storeri_pcr
, /*HasImm*/false, /*MxOp*/2);
1538 case Hexagon::PS_storerd_pcr
:
1539 return RealCirc(Hexagon::S2_storerd_pcr
, /*HasImm*/false, /*MxOp*/2);
1545 MachineBasicBlock::instr_iterator
1546 HexagonInstrInfo::expandVGatherPseudo(MachineInstr
&MI
) const {
1547 MachineBasicBlock
&MBB
= *MI
.getParent();
1548 const DebugLoc
&DL
= MI
.getDebugLoc();
1549 unsigned Opc
= MI
.getOpcode();
1550 MachineBasicBlock::iterator First
;
1553 case Hexagon::V6_vgathermh_pseudo
:
1554 First
= BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_vgathermh
))
1555 .add(MI
.getOperand(2))
1556 .add(MI
.getOperand(3))
1557 .add(MI
.getOperand(4));
1558 BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_vS32b_new_ai
))
1559 .add(MI
.getOperand(0))
1560 .addImm(MI
.getOperand(1).getImm())
1561 .addReg(Hexagon::VTMP
);
1563 return First
.getInstrIterator();
1565 case Hexagon::V6_vgathermw_pseudo
:
1566 First
= BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_vgathermw
))
1567 .add(MI
.getOperand(2))
1568 .add(MI
.getOperand(3))
1569 .add(MI
.getOperand(4));
1570 BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_vS32b_new_ai
))
1571 .add(MI
.getOperand(0))
1572 .addImm(MI
.getOperand(1).getImm())
1573 .addReg(Hexagon::VTMP
);
1575 return First
.getInstrIterator();
1577 case Hexagon::V6_vgathermhw_pseudo
:
1578 First
= BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_vgathermhw
))
1579 .add(MI
.getOperand(2))
1580 .add(MI
.getOperand(3))
1581 .add(MI
.getOperand(4));
1582 BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_vS32b_new_ai
))
1583 .add(MI
.getOperand(0))
1584 .addImm(MI
.getOperand(1).getImm())
1585 .addReg(Hexagon::VTMP
);
1587 return First
.getInstrIterator();
1589 case Hexagon::V6_vgathermhq_pseudo
:
1590 First
= BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_vgathermhq
))
1591 .add(MI
.getOperand(2))
1592 .add(MI
.getOperand(3))
1593 .add(MI
.getOperand(4))
1594 .add(MI
.getOperand(5));
1595 BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_vS32b_new_ai
))
1596 .add(MI
.getOperand(0))
1597 .addImm(MI
.getOperand(1).getImm())
1598 .addReg(Hexagon::VTMP
);
1600 return First
.getInstrIterator();
1602 case Hexagon::V6_vgathermwq_pseudo
:
1603 First
= BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_vgathermwq
))
1604 .add(MI
.getOperand(2))
1605 .add(MI
.getOperand(3))
1606 .add(MI
.getOperand(4))
1607 .add(MI
.getOperand(5));
1608 BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_vS32b_new_ai
))
1609 .add(MI
.getOperand(0))
1610 .addImm(MI
.getOperand(1).getImm())
1611 .addReg(Hexagon::VTMP
);
1613 return First
.getInstrIterator();
1615 case Hexagon::V6_vgathermhwq_pseudo
:
1616 First
= BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_vgathermhwq
))
1617 .add(MI
.getOperand(2))
1618 .add(MI
.getOperand(3))
1619 .add(MI
.getOperand(4))
1620 .add(MI
.getOperand(5));
1621 BuildMI(MBB
, MI
, DL
, get(Hexagon::V6_vS32b_new_ai
))
1622 .add(MI
.getOperand(0))
1623 .addImm(MI
.getOperand(1).getImm())
1624 .addReg(Hexagon::VTMP
);
1626 return First
.getInstrIterator();
1629 return MI
.getIterator();
1632 // We indicate that we want to reverse the branch by
1633 // inserting the reversed branching opcode.
1634 bool HexagonInstrInfo::reverseBranchCondition(
1635 SmallVectorImpl
<MachineOperand
> &Cond
) const {
1638 assert(Cond
[0].isImm() && "First entry in the cond vector not imm-val");
1639 unsigned opcode
= Cond
[0].getImm();
1641 assert(get(opcode
).isBranch() && "Should be a branching condition.");
1642 if (isEndLoopN(opcode
))
1644 unsigned NewOpcode
= getInvertedPredicatedOpcode(opcode
);
1645 Cond
[0].setImm(NewOpcode
);
1649 void HexagonInstrInfo::insertNoop(MachineBasicBlock
&MBB
,
1650 MachineBasicBlock::iterator MI
) const {
1652 BuildMI(MBB
, MI
, DL
, get(Hexagon::A2_nop
));
1655 bool HexagonInstrInfo::isPostIncrement(const MachineInstr
&MI
) const {
1656 return getAddrMode(MI
) == HexagonII::PostInc
;
1659 // Returns true if an instruction is predicated irrespective of the predicate
1660 // sense. For example, all of the following will return true.
1661 // if (p0) R1 = add(R2, R3)
1662 // if (!p0) R1 = add(R2, R3)
1663 // if (p0.new) R1 = add(R2, R3)
1664 // if (!p0.new) R1 = add(R2, R3)
1665 // Note: New-value stores are not included here as in the current
1666 // implementation, we don't need to check their predicate sense.
1667 bool HexagonInstrInfo::isPredicated(const MachineInstr
&MI
) const {
1668 const uint64_t F
= MI
.getDesc().TSFlags
;
1669 return (F
>> HexagonII::PredicatedPos
) & HexagonII::PredicatedMask
;
1672 bool HexagonInstrInfo::PredicateInstruction(
1673 MachineInstr
&MI
, ArrayRef
<MachineOperand
> Cond
) const {
1674 if (Cond
.empty() || isNewValueJump(Cond
[0].getImm()) ||
1675 isEndLoopN(Cond
[0].getImm())) {
1676 LLVM_DEBUG(dbgs() << "\nCannot predicate:"; MI
.dump(););
1679 int Opc
= MI
.getOpcode();
1680 assert (isPredicable(MI
) && "Expected predicable instruction");
1681 bool invertJump
= predOpcodeHasNot(Cond
);
1683 // We have to predicate MI "in place", i.e. after this function returns,
1684 // MI will need to be transformed into a predicated form. To avoid com-
1685 // plicated manipulations with the operands (handling tied operands,
1686 // etc.), build a new temporary instruction, then overwrite MI with it.
1688 MachineBasicBlock
&B
= *MI
.getParent();
1689 DebugLoc DL
= MI
.getDebugLoc();
1690 unsigned PredOpc
= getCondOpcode(Opc
, invertJump
);
1691 MachineInstrBuilder T
= BuildMI(B
, MI
, DL
, get(PredOpc
));
1692 unsigned NOp
= 0, NumOps
= MI
.getNumOperands();
1693 while (NOp
< NumOps
) {
1694 MachineOperand
&Op
= MI
.getOperand(NOp
);
1695 if (!Op
.isReg() || !Op
.isDef() || Op
.isImplicit())
1702 unsigned PredRegPos
, PredRegFlags
;
1703 bool GotPredReg
= getPredReg(Cond
, PredReg
, PredRegPos
, PredRegFlags
);
1706 T
.addReg(PredReg
, PredRegFlags
);
1707 while (NOp
< NumOps
)
1708 T
.add(MI
.getOperand(NOp
++));
1710 MI
.setDesc(get(PredOpc
));
1711 while (unsigned n
= MI
.getNumOperands())
1712 MI
.removeOperand(n
-1);
1713 for (unsigned i
= 0, n
= T
->getNumOperands(); i
< n
; ++i
)
1714 MI
.addOperand(T
->getOperand(i
));
1716 MachineBasicBlock::instr_iterator TI
= T
->getIterator();
1719 MachineRegisterInfo
&MRI
= B
.getParent()->getRegInfo();
1720 MRI
.clearKillFlags(PredReg
);
1724 bool HexagonInstrInfo::SubsumesPredicate(ArrayRef
<MachineOperand
> Pred1
,
1725 ArrayRef
<MachineOperand
> Pred2
) const {
1730 bool HexagonInstrInfo::ClobbersPredicate(MachineInstr
&MI
,
1731 std::vector
<MachineOperand
> &Pred
,
1732 bool SkipDead
) const {
1733 const HexagonRegisterInfo
&HRI
= *Subtarget
.getRegisterInfo();
1735 for (const MachineOperand
&MO
: MI
.operands()) {
1739 const TargetRegisterClass
* RC
= HRI
.getMinimalPhysRegClass(MO
.getReg());
1740 if (RC
== &Hexagon::PredRegsRegClass
) {
1745 } else if (MO
.isRegMask()) {
1746 for (Register PR
: Hexagon::PredRegsRegClass
) {
1747 if (!MI
.modifiesRegister(PR
, &HRI
))
1757 bool HexagonInstrInfo::isPredicable(const MachineInstr
&MI
) const {
1758 if (!MI
.getDesc().isPredicable())
1761 if (MI
.isCall() || isTailCall(MI
)) {
1762 if (!Subtarget
.usePredicatedCalls())
1766 // HVX loads are not predicable on v60, but are on v62.
1767 if (!Subtarget
.hasV62Ops()) {
1768 switch (MI
.getOpcode()) {
1769 case Hexagon::V6_vL32b_ai
:
1770 case Hexagon::V6_vL32b_pi
:
1771 case Hexagon::V6_vL32b_ppu
:
1772 case Hexagon::V6_vL32b_cur_ai
:
1773 case Hexagon::V6_vL32b_cur_pi
:
1774 case Hexagon::V6_vL32b_cur_ppu
:
1775 case Hexagon::V6_vL32b_nt_ai
:
1776 case Hexagon::V6_vL32b_nt_pi
:
1777 case Hexagon::V6_vL32b_nt_ppu
:
1778 case Hexagon::V6_vL32b_tmp_ai
:
1779 case Hexagon::V6_vL32b_tmp_pi
:
1780 case Hexagon::V6_vL32b_tmp_ppu
:
1781 case Hexagon::V6_vL32b_nt_cur_ai
:
1782 case Hexagon::V6_vL32b_nt_cur_pi
:
1783 case Hexagon::V6_vL32b_nt_cur_ppu
:
1784 case Hexagon::V6_vL32b_nt_tmp_ai
:
1785 case Hexagon::V6_vL32b_nt_tmp_pi
:
1786 case Hexagon::V6_vL32b_nt_tmp_ppu
:
1793 bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr
&MI
,
1794 const MachineBasicBlock
*MBB
,
1795 const MachineFunction
&MF
) const {
1796 // Debug info is never a scheduling boundary. It's necessary to be explicit
1797 // due to the special treatment of IT instructions below, otherwise a
1798 // dbg_value followed by an IT will result in the IT instruction being
1799 // considered a scheduling hazard, which is wrong. It should be the actual
1800 // instruction preceding the dbg_value instruction(s), just like it is
1801 // when debug info is not present.
1802 if (MI
.isDebugInstr())
1805 // Throwing call is a boundary.
1807 // Don't mess around with no return calls.
1808 if (doesNotReturn(MI
))
1810 // If any of the block's successors is a landing pad, this could be a
1812 for (auto *I
: MBB
->successors())
1817 // Terminators and labels can't be scheduled around.
1818 if (MI
.getDesc().isTerminator() || MI
.isPosition())
1821 // INLINEASM_BR can jump to another block
1822 if (MI
.getOpcode() == TargetOpcode::INLINEASM_BR
)
1825 if (MI
.isInlineAsm() && !ScheduleInlineAsm
)
1831 /// Measure the specified inline asm to determine an approximation of its
1833 /// Comments (which run till the next SeparatorString or newline) do not
1834 /// count as an instruction.
1835 /// Any other non-whitespace text is considered an instruction, with
1836 /// multiple instructions separated by SeparatorString or newlines.
1837 /// Variable-length instructions are not handled here; this function
1838 /// may be overloaded in the target code to do that.
1839 /// Hexagon counts the number of ##'s and adjust for that many
1840 /// constant exenders.
1841 unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str
,
1842 const MCAsmInfo
&MAI
,
1843 const TargetSubtargetInfo
*STI
) const {
1844 StringRef
AStr(Str
);
1845 // Count the number of instructions in the asm.
1846 bool atInsnStart
= true;
1847 unsigned Length
= 0;
1848 const unsigned MaxInstLength
= MAI
.getMaxInstLength(STI
);
1849 for (; *Str
; ++Str
) {
1850 if (*Str
== '\n' || strncmp(Str
, MAI
.getSeparatorString(),
1851 strlen(MAI
.getSeparatorString())) == 0)
1853 if (atInsnStart
&& !isSpace(static_cast<unsigned char>(*Str
))) {
1854 Length
+= MaxInstLength
;
1855 atInsnStart
= false;
1857 if (atInsnStart
&& strncmp(Str
, MAI
.getCommentString().data(),
1858 MAI
.getCommentString().size()) == 0)
1859 atInsnStart
= false;
1862 // Add to size number of constant extenders seen * 4.
1863 StringRef
Occ("##");
1864 Length
+= AStr
.count(Occ
)*4;
1868 ScheduleHazardRecognizer
*
1869 HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1870 const InstrItineraryData
*II
, const ScheduleDAG
*DAG
) const {
1871 if (UseDFAHazardRec
)
1872 return new HexagonHazardRecognizer(II
, this, Subtarget
);
1873 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II
, DAG
);
1876 /// For a comparison instruction, return the source registers in
1877 /// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1878 /// compares against in CmpValue. Return true if the comparison instruction
1879 /// can be analyzed.
1880 bool HexagonInstrInfo::analyzeCompare(const MachineInstr
&MI
, Register
&SrcReg
,
1881 Register
&SrcReg2
, int64_t &Mask
,
1882 int64_t &Value
) const {
1883 unsigned Opc
= MI
.getOpcode();
1885 // Set mask and the first source register.
1887 case Hexagon::C2_cmpeq
:
1888 case Hexagon::C2_cmpeqp
:
1889 case Hexagon::C2_cmpgt
:
1890 case Hexagon::C2_cmpgtp
:
1891 case Hexagon::C2_cmpgtu
:
1892 case Hexagon::C2_cmpgtup
:
1893 case Hexagon::C4_cmpneq
:
1894 case Hexagon::C4_cmplte
:
1895 case Hexagon::C4_cmplteu
:
1896 case Hexagon::C2_cmpeqi
:
1897 case Hexagon::C2_cmpgti
:
1898 case Hexagon::C2_cmpgtui
:
1899 case Hexagon::C4_cmpneqi
:
1900 case Hexagon::C4_cmplteui
:
1901 case Hexagon::C4_cmpltei
:
1902 SrcReg
= MI
.getOperand(1).getReg();
1905 case Hexagon::A4_cmpbeq
:
1906 case Hexagon::A4_cmpbgt
:
1907 case Hexagon::A4_cmpbgtu
:
1908 case Hexagon::A4_cmpbeqi
:
1909 case Hexagon::A4_cmpbgti
:
1910 case Hexagon::A4_cmpbgtui
:
1911 SrcReg
= MI
.getOperand(1).getReg();
1914 case Hexagon::A4_cmpheq
:
1915 case Hexagon::A4_cmphgt
:
1916 case Hexagon::A4_cmphgtu
:
1917 case Hexagon::A4_cmpheqi
:
1918 case Hexagon::A4_cmphgti
:
1919 case Hexagon::A4_cmphgtui
:
1920 SrcReg
= MI
.getOperand(1).getReg();
1925 // Set the value/second source register.
1927 case Hexagon::C2_cmpeq
:
1928 case Hexagon::C2_cmpeqp
:
1929 case Hexagon::C2_cmpgt
:
1930 case Hexagon::C2_cmpgtp
:
1931 case Hexagon::C2_cmpgtu
:
1932 case Hexagon::C2_cmpgtup
:
1933 case Hexagon::A4_cmpbeq
:
1934 case Hexagon::A4_cmpbgt
:
1935 case Hexagon::A4_cmpbgtu
:
1936 case Hexagon::A4_cmpheq
:
1937 case Hexagon::A4_cmphgt
:
1938 case Hexagon::A4_cmphgtu
:
1939 case Hexagon::C4_cmpneq
:
1940 case Hexagon::C4_cmplte
:
1941 case Hexagon::C4_cmplteu
:
1942 SrcReg2
= MI
.getOperand(2).getReg();
1946 case Hexagon::C2_cmpeqi
:
1947 case Hexagon::C2_cmpgtui
:
1948 case Hexagon::C2_cmpgti
:
1949 case Hexagon::C4_cmpneqi
:
1950 case Hexagon::C4_cmplteui
:
1951 case Hexagon::C4_cmpltei
:
1952 case Hexagon::A4_cmpbeqi
:
1953 case Hexagon::A4_cmpbgti
:
1954 case Hexagon::A4_cmpbgtui
:
1955 case Hexagon::A4_cmpheqi
:
1956 case Hexagon::A4_cmphgti
:
1957 case Hexagon::A4_cmphgtui
: {
1959 const MachineOperand
&Op2
= MI
.getOperand(2);
1962 Value
= MI
.getOperand(2).getImm();
1970 unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData
*ItinData
,
1971 const MachineInstr
&MI
,
1972 unsigned *PredCost
) const {
1973 return getInstrTimingClassLatency(ItinData
, MI
);
1976 DFAPacketizer
*HexagonInstrInfo::CreateTargetScheduleState(
1977 const TargetSubtargetInfo
&STI
) const {
1978 const InstrItineraryData
*II
= STI
.getInstrItineraryData();
1979 return static_cast<const HexagonSubtarget
&>(STI
).createDFAPacketizer(II
);
1982 // Inspired by this pair:
1983 // %r13 = L2_loadri_io %r29, 136; mem:LD4[FixedStack0]
1984 // S2_storeri_io %r29, 132, killed %r1; flags: mem:ST4[FixedStack1]
1985 // Currently AA considers the addresses in these instructions to be aliasing.
1986 bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1987 const MachineInstr
&MIa
, const MachineInstr
&MIb
) const {
1988 if (MIa
.hasUnmodeledSideEffects() || MIb
.hasUnmodeledSideEffects() ||
1989 MIa
.hasOrderedMemoryRef() || MIb
.hasOrderedMemoryRef())
1992 // Instructions that are pure loads, not loads and stores like memops are not
1994 if (MIa
.mayLoad() && !isMemOp(MIa
) && MIb
.mayLoad() && !isMemOp(MIb
))
1997 // Get the base register in MIa.
1998 unsigned BasePosA
, OffsetPosA
;
1999 if (!getBaseAndOffsetPosition(MIa
, BasePosA
, OffsetPosA
))
2001 const MachineOperand
&BaseA
= MIa
.getOperand(BasePosA
);
2002 Register BaseRegA
= BaseA
.getReg();
2003 unsigned BaseSubA
= BaseA
.getSubReg();
2005 // Get the base register in MIb.
2006 unsigned BasePosB
, OffsetPosB
;
2007 if (!getBaseAndOffsetPosition(MIb
, BasePosB
, OffsetPosB
))
2009 const MachineOperand
&BaseB
= MIb
.getOperand(BasePosB
);
2010 Register BaseRegB
= BaseB
.getReg();
2011 unsigned BaseSubB
= BaseB
.getSubReg();
2013 if (BaseRegA
!= BaseRegB
|| BaseSubA
!= BaseSubB
)
2016 // Get the access sizes.
2017 unsigned SizeA
= getMemAccessSize(MIa
);
2018 unsigned SizeB
= getMemAccessSize(MIb
);
2020 // Get the offsets. Handle immediates only for now.
2021 const MachineOperand
&OffA
= MIa
.getOperand(OffsetPosA
);
2022 const MachineOperand
&OffB
= MIb
.getOperand(OffsetPosB
);
2023 if (!MIa
.getOperand(OffsetPosA
).isImm() ||
2024 !MIb
.getOperand(OffsetPosB
).isImm())
2026 int OffsetA
= isPostIncrement(MIa
) ? 0 : OffA
.getImm();
2027 int OffsetB
= isPostIncrement(MIb
) ? 0 : OffB
.getImm();
2029 // This is a mem access with the same base register and known offsets from it.
2031 if (OffsetA
> OffsetB
) {
2032 uint64_t OffDiff
= (uint64_t)((int64_t)OffsetA
- (int64_t)OffsetB
);
2033 return SizeB
<= OffDiff
;
2035 if (OffsetA
< OffsetB
) {
2036 uint64_t OffDiff
= (uint64_t)((int64_t)OffsetB
- (int64_t)OffsetA
);
2037 return SizeA
<= OffDiff
;
2043 /// If the instruction is an increment of a constant value, return the amount.
2044 bool HexagonInstrInfo::getIncrementValue(const MachineInstr
&MI
,
2046 if (isPostIncrement(MI
)) {
2047 unsigned BasePos
= 0, OffsetPos
= 0;
2048 if (!getBaseAndOffsetPosition(MI
, BasePos
, OffsetPos
))
2050 const MachineOperand
&OffsetOp
= MI
.getOperand(OffsetPos
);
2051 if (OffsetOp
.isImm()) {
2052 Value
= OffsetOp
.getImm();
2055 } else if (MI
.getOpcode() == Hexagon::A2_addi
) {
2056 const MachineOperand
&AddOp
= MI
.getOperand(2);
2057 if (AddOp
.isImm()) {
2058 Value
= AddOp
.getImm();
2066 std::pair
<unsigned, unsigned>
2067 HexagonInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF
) const {
2068 return std::make_pair(TF
& ~HexagonII::MO_Bitmasks
,
2069 TF
& HexagonII::MO_Bitmasks
);
2072 ArrayRef
<std::pair
<unsigned, const char*>>
2073 HexagonInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
2074 using namespace HexagonII
;
2076 static const std::pair
<unsigned, const char*> Flags
[] = {
2077 {MO_PCREL
, "hexagon-pcrel"},
2078 {MO_GOT
, "hexagon-got"},
2079 {MO_LO16
, "hexagon-lo16"},
2080 {MO_HI16
, "hexagon-hi16"},
2081 {MO_GPREL
, "hexagon-gprel"},
2082 {MO_GDGOT
, "hexagon-gdgot"},
2083 {MO_GDPLT
, "hexagon-gdplt"},
2084 {MO_IE
, "hexagon-ie"},
2085 {MO_IEGOT
, "hexagon-iegot"},
2086 {MO_TPREL
, "hexagon-tprel"}
2088 return ArrayRef(Flags
);
2091 ArrayRef
<std::pair
<unsigned, const char*>>
2092 HexagonInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
2093 using namespace HexagonII
;
2095 static const std::pair
<unsigned, const char*> Flags
[] = {
2096 {HMOTF_ConstExtended
, "hexagon-ext"}
2098 return ArrayRef(Flags
);
2101 Register
HexagonInstrInfo::createVR(MachineFunction
*MF
, MVT VT
) const {
2102 MachineRegisterInfo
&MRI
= MF
->getRegInfo();
2103 const TargetRegisterClass
*TRC
;
2104 if (VT
== MVT::i1
) {
2105 TRC
= &Hexagon::PredRegsRegClass
;
2106 } else if (VT
== MVT::i32
|| VT
== MVT::f32
) {
2107 TRC
= &Hexagon::IntRegsRegClass
;
2108 } else if (VT
== MVT::i64
|| VT
== MVT::f64
) {
2109 TRC
= &Hexagon::DoubleRegsRegClass
;
2111 llvm_unreachable("Cannot handle this register class");
2114 Register NewReg
= MRI
.createVirtualRegister(TRC
);
2118 bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr
&MI
) const {
2119 return (getAddrMode(MI
) == HexagonII::AbsoluteSet
);
2122 bool HexagonInstrInfo::isAccumulator(const MachineInstr
&MI
) const {
2123 const uint64_t F
= MI
.getDesc().TSFlags
;
2124 return((F
>> HexagonII::AccumulatorPos
) & HexagonII::AccumulatorMask
);
2127 bool HexagonInstrInfo::isBaseImmOffset(const MachineInstr
&MI
) const {
2128 return getAddrMode(MI
) == HexagonII::BaseImmOffset
;
2131 bool HexagonInstrInfo::isComplex(const MachineInstr
&MI
) const {
2132 return !isTC1(MI
) && !isTC2Early(MI
) && !MI
.getDesc().mayLoad() &&
2133 !MI
.getDesc().mayStore() &&
2134 MI
.getDesc().getOpcode() != Hexagon::S2_allocframe
&&
2135 MI
.getDesc().getOpcode() != Hexagon::L2_deallocframe
&&
2136 !isMemOp(MI
) && !MI
.isBranch() && !MI
.isReturn() && !MI
.isCall();
2139 // Return true if the instruction is a compound branch instruction.
2140 bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr
&MI
) const {
2141 return getType(MI
) == HexagonII::TypeCJ
&& MI
.isBranch();
2144 // TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
2145 // isFPImm and later getFPImm as well.
2146 bool HexagonInstrInfo::isConstExtended(const MachineInstr
&MI
) const {
2147 const uint64_t F
= MI
.getDesc().TSFlags
;
2148 unsigned isExtended
= (F
>> HexagonII::ExtendedPos
) & HexagonII::ExtendedMask
;
2149 if (isExtended
) // Instruction must be extended.
2152 unsigned isExtendable
=
2153 (F
>> HexagonII::ExtendablePos
) & HexagonII::ExtendableMask
;
2160 short ExtOpNum
= getCExtOpNum(MI
);
2161 const MachineOperand
&MO
= MI
.getOperand(ExtOpNum
);
2162 // Use MO operand flags to determine if MO
2163 // has the HMOTF_ConstExtended flag set.
2164 if (MO
.getTargetFlags() & HexagonII::HMOTF_ConstExtended
)
2166 // If this is a Machine BB address we are talking about, and it is
2167 // not marked as extended, say so.
2171 // We could be using an instruction with an extendable immediate and shoehorn
2172 // a global address into it. If it is a global address it will be constant
2173 // extended. We do this for COMBINE.
2174 if (MO
.isGlobal() || MO
.isSymbol() || MO
.isBlockAddress() ||
2175 MO
.isJTI() || MO
.isCPI() || MO
.isFPImm())
2178 // If the extendable operand is not 'Immediate' type, the instruction should
2179 // have 'isExtended' flag set.
2180 assert(MO
.isImm() && "Extendable operand must be Immediate type");
2182 int64_t Value
= MO
.getImm();
2183 if ((F
>> HexagonII::ExtentSignedPos
) & HexagonII::ExtentSignedMask
) {
2184 int32_t SValue
= Value
;
2185 int32_t MinValue
= getMinValue(MI
);
2186 int32_t MaxValue
= getMaxValue(MI
);
2187 return SValue
< MinValue
|| SValue
> MaxValue
;
2189 uint32_t UValue
= Value
;
2190 uint32_t MinValue
= getMinValue(MI
);
2191 uint32_t MaxValue
= getMaxValue(MI
);
2192 return UValue
< MinValue
|| UValue
> MaxValue
;
2195 bool HexagonInstrInfo::isDeallocRet(const MachineInstr
&MI
) const {
2196 switch (MI
.getOpcode()) {
2197 case Hexagon::L4_return
:
2198 case Hexagon::L4_return_t
:
2199 case Hexagon::L4_return_f
:
2200 case Hexagon::L4_return_tnew_pnt
:
2201 case Hexagon::L4_return_fnew_pnt
:
2202 case Hexagon::L4_return_tnew_pt
:
2203 case Hexagon::L4_return_fnew_pt
:
2209 // Return true when ConsMI uses a register defined by ProdMI.
2210 bool HexagonInstrInfo::isDependent(const MachineInstr
&ProdMI
,
2211 const MachineInstr
&ConsMI
) const {
2212 if (!ProdMI
.getDesc().getNumDefs())
2214 const HexagonRegisterInfo
&HRI
= *Subtarget
.getRegisterInfo();
2216 SmallVector
<Register
, 4> DefsA
;
2217 SmallVector
<Register
, 4> DefsB
;
2218 SmallVector
<Register
, 8> UsesA
;
2219 SmallVector
<Register
, 8> UsesB
;
2221 parseOperands(ProdMI
, DefsA
, UsesA
);
2222 parseOperands(ConsMI
, DefsB
, UsesB
);
2224 for (auto &RegA
: DefsA
)
2225 for (auto &RegB
: UsesB
) {
2226 // True data dependency.
2230 if (RegA
.isPhysical() && llvm::is_contained(HRI
.subregs(RegA
), RegB
))
2233 if (RegB
.isPhysical() && llvm::is_contained(HRI
.subregs(RegB
), RegA
))
2240 // Returns true if the instruction is alread a .cur.
2241 bool HexagonInstrInfo::isDotCurInst(const MachineInstr
&MI
) const {
2242 switch (MI
.getOpcode()) {
2243 case Hexagon::V6_vL32b_cur_pi
:
2244 case Hexagon::V6_vL32b_cur_ai
:
2250 // Returns true, if any one of the operands is a dot new
2251 // insn, whether it is predicated dot new or register dot new.
2252 bool HexagonInstrInfo::isDotNewInst(const MachineInstr
&MI
) const {
2253 if (isNewValueInst(MI
) || (isPredicated(MI
) && isPredicatedNew(MI
)))
2259 /// Symmetrical. See if these two instructions are fit for duplex pair.
2260 bool HexagonInstrInfo::isDuplexPair(const MachineInstr
&MIa
,
2261 const MachineInstr
&MIb
) const {
2262 HexagonII::SubInstructionGroup MIaG
= getDuplexCandidateGroup(MIa
);
2263 HexagonII::SubInstructionGroup MIbG
= getDuplexCandidateGroup(MIb
);
2264 return (isDuplexPairMatch(MIaG
, MIbG
) || isDuplexPairMatch(MIbG
, MIaG
));
2267 bool HexagonInstrInfo::isEndLoopN(unsigned Opcode
) const {
2268 return (Opcode
== Hexagon::ENDLOOP0
||
2269 Opcode
== Hexagon::ENDLOOP1
);
2272 bool HexagonInstrInfo::isExpr(unsigned OpType
) const {
2274 case MachineOperand::MO_MachineBasicBlock
:
2275 case MachineOperand::MO_GlobalAddress
:
2276 case MachineOperand::MO_ExternalSymbol
:
2277 case MachineOperand::MO_JumpTableIndex
:
2278 case MachineOperand::MO_ConstantPoolIndex
:
2279 case MachineOperand::MO_BlockAddress
:
2286 bool HexagonInstrInfo::isExtendable(const MachineInstr
&MI
) const {
2287 const MCInstrDesc
&MID
= MI
.getDesc();
2288 const uint64_t F
= MID
.TSFlags
;
2289 if ((F
>> HexagonII::ExtendablePos
) & HexagonII::ExtendableMask
)
2292 // TODO: This is largely obsolete now. Will need to be removed
2293 // in consecutive patches.
2294 switch (MI
.getOpcode()) {
2295 // PS_fi and PS_fia remain special cases.
2296 case Hexagon::PS_fi
:
2297 case Hexagon::PS_fia
:
2305 // This returns true in two cases:
2306 // - The OP code itself indicates that this is an extended instruction.
2307 // - One of MOs has been marked with HMOTF_ConstExtended flag.
2308 bool HexagonInstrInfo::isExtended(const MachineInstr
&MI
) const {
2309 // First check if this is permanently extended op code.
2310 const uint64_t F
= MI
.getDesc().TSFlags
;
2311 if ((F
>> HexagonII::ExtendedPos
) & HexagonII::ExtendedMask
)
2313 // Use MO operand flags to determine if one of MI's operands
2314 // has HMOTF_ConstExtended flag set.
2315 for (const MachineOperand
&MO
: MI
.operands())
2316 if (MO
.getTargetFlags() & HexagonII::HMOTF_ConstExtended
)
2321 bool HexagonInstrInfo::isFloat(const MachineInstr
&MI
) const {
2322 unsigned Opcode
= MI
.getOpcode();
2323 const uint64_t F
= get(Opcode
).TSFlags
;
2324 return (F
>> HexagonII::FPPos
) & HexagonII::FPMask
;
2327 // No V60 HVX VMEM with A_INDIRECT.
2328 bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr
&I
,
2329 const MachineInstr
&J
) const {
2332 if (!I
.mayLoad() && !I
.mayStore())
2334 return J
.isIndirectBranch() || isIndirectCall(J
) || isIndirectL4Return(J
);
2337 bool HexagonInstrInfo::isIndirectCall(const MachineInstr
&MI
) const {
2338 switch (MI
.getOpcode()) {
2339 case Hexagon::J2_callr
:
2340 case Hexagon::J2_callrf
:
2341 case Hexagon::J2_callrt
:
2342 case Hexagon::PS_call_nr
:
2348 bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr
&MI
) const {
2349 switch (MI
.getOpcode()) {
2350 case Hexagon::L4_return
:
2351 case Hexagon::L4_return_t
:
2352 case Hexagon::L4_return_f
:
2353 case Hexagon::L4_return_fnew_pnt
:
2354 case Hexagon::L4_return_fnew_pt
:
2355 case Hexagon::L4_return_tnew_pnt
:
2356 case Hexagon::L4_return_tnew_pt
:
2362 bool HexagonInstrInfo::isJumpR(const MachineInstr
&MI
) const {
2363 switch (MI
.getOpcode()) {
2364 case Hexagon::J2_jumpr
:
2365 case Hexagon::J2_jumprt
:
2366 case Hexagon::J2_jumprf
:
2367 case Hexagon::J2_jumprtnewpt
:
2368 case Hexagon::J2_jumprfnewpt
:
2369 case Hexagon::J2_jumprtnew
:
2370 case Hexagon::J2_jumprfnew
:
2376 // Return true if a given MI can accommodate given offset.
2377 // Use abs estimate as oppose to the exact number.
2378 // TODO: This will need to be changed to use MC level
2379 // definition of instruction extendable field size.
2380 bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr
&MI
,
2381 unsigned offset
) const {
2382 // This selection of jump instructions matches to that what
2383 // analyzeBranch can parse, plus NVJ.
2384 if (isNewValueJump(MI
)) // r9:2
2385 return isInt
<11>(offset
);
2387 switch (MI
.getOpcode()) {
2388 // Still missing Jump to address condition on register value.
2391 case Hexagon::J2_jump
: // bits<24> dst; // r22:2
2392 case Hexagon::J2_call
:
2393 case Hexagon::PS_call_nr
:
2394 return isInt
<24>(offset
);
2395 case Hexagon::J2_jumpt
: //bits<17> dst; // r15:2
2396 case Hexagon::J2_jumpf
:
2397 case Hexagon::J2_jumptnew
:
2398 case Hexagon::J2_jumptnewpt
:
2399 case Hexagon::J2_jumpfnew
:
2400 case Hexagon::J2_jumpfnewpt
:
2401 case Hexagon::J2_callt
:
2402 case Hexagon::J2_callf
:
2403 return isInt
<17>(offset
);
2404 case Hexagon::J2_loop0i
:
2405 case Hexagon::J2_loop0iext
:
2406 case Hexagon::J2_loop0r
:
2407 case Hexagon::J2_loop0rext
:
2408 case Hexagon::J2_loop1i
:
2409 case Hexagon::J2_loop1iext
:
2410 case Hexagon::J2_loop1r
:
2411 case Hexagon::J2_loop1rext
:
2412 return isInt
<9>(offset
);
2413 // TODO: Add all the compound branches here. Can we do this in Relation model?
2414 case Hexagon::J4_cmpeqi_tp0_jump_nt
:
2415 case Hexagon::J4_cmpeqi_tp1_jump_nt
:
2416 case Hexagon::J4_cmpeqn1_tp0_jump_nt
:
2417 case Hexagon::J4_cmpeqn1_tp1_jump_nt
:
2418 return isInt
<11>(offset
);
2422 bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr
&MI
) const {
2423 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2424 // resource, but all operands can be received late like an ALU instruction.
2425 return getType(MI
) == HexagonII::TypeCVI_VX_LATE
;
2428 bool HexagonInstrInfo::isLoopN(const MachineInstr
&MI
) const {
2429 unsigned Opcode
= MI
.getOpcode();
2430 return Opcode
== Hexagon::J2_loop0i
||
2431 Opcode
== Hexagon::J2_loop0r
||
2432 Opcode
== Hexagon::J2_loop0iext
||
2433 Opcode
== Hexagon::J2_loop0rext
||
2434 Opcode
== Hexagon::J2_loop1i
||
2435 Opcode
== Hexagon::J2_loop1r
||
2436 Opcode
== Hexagon::J2_loop1iext
||
2437 Opcode
== Hexagon::J2_loop1rext
;
2440 bool HexagonInstrInfo::isMemOp(const MachineInstr
&MI
) const {
2441 switch (MI
.getOpcode()) {
2442 default: return false;
2443 case Hexagon::L4_iadd_memopw_io
:
2444 case Hexagon::L4_isub_memopw_io
:
2445 case Hexagon::L4_add_memopw_io
:
2446 case Hexagon::L4_sub_memopw_io
:
2447 case Hexagon::L4_and_memopw_io
:
2448 case Hexagon::L4_or_memopw_io
:
2449 case Hexagon::L4_iadd_memoph_io
:
2450 case Hexagon::L4_isub_memoph_io
:
2451 case Hexagon::L4_add_memoph_io
:
2452 case Hexagon::L4_sub_memoph_io
:
2453 case Hexagon::L4_and_memoph_io
:
2454 case Hexagon::L4_or_memoph_io
:
2455 case Hexagon::L4_iadd_memopb_io
:
2456 case Hexagon::L4_isub_memopb_io
:
2457 case Hexagon::L4_add_memopb_io
:
2458 case Hexagon::L4_sub_memopb_io
:
2459 case Hexagon::L4_and_memopb_io
:
2460 case Hexagon::L4_or_memopb_io
:
2461 case Hexagon::L4_ior_memopb_io
:
2462 case Hexagon::L4_ior_memoph_io
:
2463 case Hexagon::L4_ior_memopw_io
:
2464 case Hexagon::L4_iand_memopb_io
:
2465 case Hexagon::L4_iand_memoph_io
:
2466 case Hexagon::L4_iand_memopw_io
:
2472 bool HexagonInstrInfo::isNewValue(const MachineInstr
&MI
) const {
2473 const uint64_t F
= MI
.getDesc().TSFlags
;
2474 return (F
>> HexagonII::NewValuePos
) & HexagonII::NewValueMask
;
2477 bool HexagonInstrInfo::isNewValue(unsigned Opcode
) const {
2478 const uint64_t F
= get(Opcode
).TSFlags
;
2479 return (F
>> HexagonII::NewValuePos
) & HexagonII::NewValueMask
;
2482 bool HexagonInstrInfo::isNewValueInst(const MachineInstr
&MI
) const {
2483 return isNewValueJump(MI
) || isNewValueStore(MI
);
2486 bool HexagonInstrInfo::isNewValueJump(const MachineInstr
&MI
) const {
2487 return isNewValue(MI
) && MI
.isBranch();
2490 bool HexagonInstrInfo::isNewValueJump(unsigned Opcode
) const {
2491 return isNewValue(Opcode
) && get(Opcode
).isBranch() && isPredicated(Opcode
);
2494 bool HexagonInstrInfo::isNewValueStore(const MachineInstr
&MI
) const {
2495 const uint64_t F
= MI
.getDesc().TSFlags
;
2496 return (F
>> HexagonII::NVStorePos
) & HexagonII::NVStoreMask
;
2499 bool HexagonInstrInfo::isNewValueStore(unsigned Opcode
) const {
2500 const uint64_t F
= get(Opcode
).TSFlags
;
2501 return (F
>> HexagonII::NVStorePos
) & HexagonII::NVStoreMask
;
2504 // Returns true if a particular operand is extendable for an instruction.
2505 bool HexagonInstrInfo::isOperandExtended(const MachineInstr
&MI
,
2506 unsigned OperandNum
) const {
2507 const uint64_t F
= MI
.getDesc().TSFlags
;
2508 return ((F
>> HexagonII::ExtendableOpPos
) & HexagonII::ExtendableOpMask
)
2512 bool HexagonInstrInfo::isPredicatedNew(const MachineInstr
&MI
) const {
2513 const uint64_t F
= MI
.getDesc().TSFlags
;
2514 assert(isPredicated(MI
));
2515 return (F
>> HexagonII::PredicatedNewPos
) & HexagonII::PredicatedNewMask
;
2518 bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode
) const {
2519 const uint64_t F
= get(Opcode
).TSFlags
;
2520 assert(isPredicated(Opcode
));
2521 return (F
>> HexagonII::PredicatedNewPos
) & HexagonII::PredicatedNewMask
;
2524 bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr
&MI
) const {
2525 const uint64_t F
= MI
.getDesc().TSFlags
;
2526 return !((F
>> HexagonII::PredicatedFalsePos
) &
2527 HexagonII::PredicatedFalseMask
);
2530 bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode
) const {
2531 const uint64_t F
= get(Opcode
).TSFlags
;
2532 // Make sure that the instruction is predicated.
2533 assert((F
>> HexagonII::PredicatedPos
) & HexagonII::PredicatedMask
);
2534 return !((F
>> HexagonII::PredicatedFalsePos
) &
2535 HexagonII::PredicatedFalseMask
);
2538 bool HexagonInstrInfo::isPredicated(unsigned Opcode
) const {
2539 const uint64_t F
= get(Opcode
).TSFlags
;
2540 return (F
>> HexagonII::PredicatedPos
) & HexagonII::PredicatedMask
;
2543 bool HexagonInstrInfo::isPredicateLate(unsigned Opcode
) const {
2544 const uint64_t F
= get(Opcode
).TSFlags
;
2545 return (F
>> HexagonII::PredicateLatePos
) & HexagonII::PredicateLateMask
;
2548 bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode
) const {
2549 const uint64_t F
= get(Opcode
).TSFlags
;
2550 assert(get(Opcode
).isBranch() &&
2551 (isPredicatedNew(Opcode
) || isNewValue(Opcode
)));
2552 return (F
>> HexagonII::TakenPos
) & HexagonII::TakenMask
;
2555 bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr
&MI
) const {
2556 return MI
.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4
||
2557 MI
.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT
||
2558 MI
.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC
||
2559 MI
.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC
;
2562 bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr
&MI
) const {
2563 switch (MI
.getOpcode()) {
2565 case Hexagon::L2_loadrb_io
:
2566 case Hexagon::L4_loadrb_ur
:
2567 case Hexagon::L4_loadrb_ap
:
2568 case Hexagon::L2_loadrb_pr
:
2569 case Hexagon::L2_loadrb_pbr
:
2570 case Hexagon::L2_loadrb_pi
:
2571 case Hexagon::L2_loadrb_pci
:
2572 case Hexagon::L2_loadrb_pcr
:
2573 case Hexagon::L2_loadbsw2_io
:
2574 case Hexagon::L4_loadbsw2_ur
:
2575 case Hexagon::L4_loadbsw2_ap
:
2576 case Hexagon::L2_loadbsw2_pr
:
2577 case Hexagon::L2_loadbsw2_pbr
:
2578 case Hexagon::L2_loadbsw2_pi
:
2579 case Hexagon::L2_loadbsw2_pci
:
2580 case Hexagon::L2_loadbsw2_pcr
:
2581 case Hexagon::L2_loadbsw4_io
:
2582 case Hexagon::L4_loadbsw4_ur
:
2583 case Hexagon::L4_loadbsw4_ap
:
2584 case Hexagon::L2_loadbsw4_pr
:
2585 case Hexagon::L2_loadbsw4_pbr
:
2586 case Hexagon::L2_loadbsw4_pi
:
2587 case Hexagon::L2_loadbsw4_pci
:
2588 case Hexagon::L2_loadbsw4_pcr
:
2589 case Hexagon::L4_loadrb_rr
:
2590 case Hexagon::L2_ploadrbt_io
:
2591 case Hexagon::L2_ploadrbt_pi
:
2592 case Hexagon::L2_ploadrbf_io
:
2593 case Hexagon::L2_ploadrbf_pi
:
2594 case Hexagon::L2_ploadrbtnew_io
:
2595 case Hexagon::L2_ploadrbfnew_io
:
2596 case Hexagon::L4_ploadrbt_rr
:
2597 case Hexagon::L4_ploadrbf_rr
:
2598 case Hexagon::L4_ploadrbtnew_rr
:
2599 case Hexagon::L4_ploadrbfnew_rr
:
2600 case Hexagon::L2_ploadrbtnew_pi
:
2601 case Hexagon::L2_ploadrbfnew_pi
:
2602 case Hexagon::L4_ploadrbt_abs
:
2603 case Hexagon::L4_ploadrbf_abs
:
2604 case Hexagon::L4_ploadrbtnew_abs
:
2605 case Hexagon::L4_ploadrbfnew_abs
:
2606 case Hexagon::L2_loadrbgp
:
2608 case Hexagon::L2_loadrh_io
:
2609 case Hexagon::L4_loadrh_ur
:
2610 case Hexagon::L4_loadrh_ap
:
2611 case Hexagon::L2_loadrh_pr
:
2612 case Hexagon::L2_loadrh_pbr
:
2613 case Hexagon::L2_loadrh_pi
:
2614 case Hexagon::L2_loadrh_pci
:
2615 case Hexagon::L2_loadrh_pcr
:
2616 case Hexagon::L4_loadrh_rr
:
2617 case Hexagon::L2_ploadrht_io
:
2618 case Hexagon::L2_ploadrht_pi
:
2619 case Hexagon::L2_ploadrhf_io
:
2620 case Hexagon::L2_ploadrhf_pi
:
2621 case Hexagon::L2_ploadrhtnew_io
:
2622 case Hexagon::L2_ploadrhfnew_io
:
2623 case Hexagon::L4_ploadrht_rr
:
2624 case Hexagon::L4_ploadrhf_rr
:
2625 case Hexagon::L4_ploadrhtnew_rr
:
2626 case Hexagon::L4_ploadrhfnew_rr
:
2627 case Hexagon::L2_ploadrhtnew_pi
:
2628 case Hexagon::L2_ploadrhfnew_pi
:
2629 case Hexagon::L4_ploadrht_abs
:
2630 case Hexagon::L4_ploadrhf_abs
:
2631 case Hexagon::L4_ploadrhtnew_abs
:
2632 case Hexagon::L4_ploadrhfnew_abs
:
2633 case Hexagon::L2_loadrhgp
:
2640 bool HexagonInstrInfo::isSolo(const MachineInstr
&MI
) const {
2641 const uint64_t F
= MI
.getDesc().TSFlags
;
2642 return (F
>> HexagonII::SoloPos
) & HexagonII::SoloMask
;
2645 bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr
&MI
) const {
2646 switch (MI
.getOpcode()) {
2647 case Hexagon::STriw_pred
:
2648 case Hexagon::LDriw_pred
:
2655 bool HexagonInstrInfo::isTailCall(const MachineInstr
&MI
) const {
2659 for (auto &Op
: MI
.operands())
2660 if (Op
.isGlobal() || Op
.isSymbol())
2665 // Returns true when SU has a timing class TC1.
2666 bool HexagonInstrInfo::isTC1(const MachineInstr
&MI
) const {
2667 unsigned SchedClass
= MI
.getDesc().getSchedClass();
2668 return is_TC1(SchedClass
);
2671 bool HexagonInstrInfo::isTC2(const MachineInstr
&MI
) const {
2672 unsigned SchedClass
= MI
.getDesc().getSchedClass();
2673 return is_TC2(SchedClass
);
2676 bool HexagonInstrInfo::isTC2Early(const MachineInstr
&MI
) const {
2677 unsigned SchedClass
= MI
.getDesc().getSchedClass();
2678 return is_TC2early(SchedClass
);
2681 bool HexagonInstrInfo::isTC4x(const MachineInstr
&MI
) const {
2682 unsigned SchedClass
= MI
.getDesc().getSchedClass();
2683 return is_TC4x(SchedClass
);
2686 // Schedule this ASAP.
2687 bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr
&MI1
,
2688 const MachineInstr
&MI2
) const {
2689 if (mayBeCurLoad(MI1
)) {
2690 // if (result of SU is used in Next) return true;
2691 Register DstReg
= MI1
.getOperand(0).getReg();
2692 int N
= MI2
.getNumOperands();
2693 for (int I
= 0; I
< N
; I
++)
2694 if (MI2
.getOperand(I
).isReg() && DstReg
== MI2
.getOperand(I
).getReg())
2697 if (mayBeNewStore(MI2
))
2698 if (MI2
.getOpcode() == Hexagon::V6_vS32b_pi
)
2699 if (MI1
.getOperand(0).isReg() && MI2
.getOperand(3).isReg() &&
2700 MI1
.getOperand(0).getReg() == MI2
.getOperand(3).getReg())
2705 bool HexagonInstrInfo::isHVXVec(const MachineInstr
&MI
) const {
2706 const uint64_t V
= getType(MI
);
2707 return HexagonII::TypeCVI_FIRST
<= V
&& V
<= HexagonII::TypeCVI_LAST
;
2710 // Check if the Offset is a valid auto-inc imm by Load/Store Type.
2711 bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT
, int Offset
) const {
2712 int Size
= VT
.getSizeInBits() / 8;
2713 if (Offset
% Size
!= 0)
2715 int Count
= Offset
/ Size
;
2717 switch (VT
.getSimpleVT().SimpleTy
) {
2718 // For scalars the auto-inc is s4
2730 return isInt
<4>(Count
);
2731 // For HVX vectors the auto-inc is s3
2740 return isInt
<3>(Count
);
2745 llvm_unreachable("Not an valid type!");
2748 bool HexagonInstrInfo::isValidOffset(unsigned Opcode
, int Offset
,
2749 const TargetRegisterInfo
*TRI
, bool Extend
) const {
2750 // This function is to check whether the "Offset" is in the correct range of
2751 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
2752 // inserted to calculate the final address. Due to this reason, the function
2753 // assumes that the "Offset" has correct alignment.
2754 // We used to assert if the offset was not properly aligned, however,
2755 // there are cases where a misaligned pointer recast can cause this
2756 // problem, and we need to allow for it. The front end warns of such
2757 // misaligns with respect to load size.
2759 case Hexagon::PS_vstorerq_ai
:
2760 case Hexagon::PS_vstorerv_ai
:
2761 case Hexagon::PS_vstorerw_ai
:
2762 case Hexagon::PS_vstorerw_nt_ai
:
2763 case Hexagon::PS_vloadrq_ai
:
2764 case Hexagon::PS_vloadrv_ai
:
2765 case Hexagon::PS_vloadrw_ai
:
2766 case Hexagon::PS_vloadrw_nt_ai
:
2767 case Hexagon::V6_vL32b_ai
:
2768 case Hexagon::V6_vS32b_ai
:
2769 case Hexagon::V6_vS32b_pred_ai
:
2770 case Hexagon::V6_vS32b_npred_ai
:
2771 case Hexagon::V6_vS32b_qpred_ai
:
2772 case Hexagon::V6_vS32b_nqpred_ai
:
2773 case Hexagon::V6_vS32b_new_ai
:
2774 case Hexagon::V6_vS32b_new_pred_ai
:
2775 case Hexagon::V6_vS32b_new_npred_ai
:
2776 case Hexagon::V6_vS32b_nt_pred_ai
:
2777 case Hexagon::V6_vS32b_nt_npred_ai
:
2778 case Hexagon::V6_vS32b_nt_new_ai
:
2779 case Hexagon::V6_vS32b_nt_new_pred_ai
:
2780 case Hexagon::V6_vS32b_nt_new_npred_ai
:
2781 case Hexagon::V6_vS32b_nt_qpred_ai
:
2782 case Hexagon::V6_vS32b_nt_nqpred_ai
:
2783 case Hexagon::V6_vL32b_nt_ai
:
2784 case Hexagon::V6_vS32b_nt_ai
:
2785 case Hexagon::V6_vL32Ub_ai
:
2786 case Hexagon::V6_vS32Ub_ai
:
2787 case Hexagon::V6_vL32b_cur_ai
:
2788 case Hexagon::V6_vL32b_tmp_ai
:
2789 case Hexagon::V6_vL32b_pred_ai
:
2790 case Hexagon::V6_vL32b_npred_ai
:
2791 case Hexagon::V6_vL32b_cur_pred_ai
:
2792 case Hexagon::V6_vL32b_cur_npred_ai
:
2793 case Hexagon::V6_vL32b_tmp_pred_ai
:
2794 case Hexagon::V6_vL32b_tmp_npred_ai
:
2795 case Hexagon::V6_vL32b_nt_cur_ai
:
2796 case Hexagon::V6_vL32b_nt_tmp_ai
:
2797 case Hexagon::V6_vL32b_nt_pred_ai
:
2798 case Hexagon::V6_vL32b_nt_npred_ai
:
2799 case Hexagon::V6_vL32b_nt_cur_pred_ai
:
2800 case Hexagon::V6_vL32b_nt_cur_npred_ai
:
2801 case Hexagon::V6_vL32b_nt_tmp_pred_ai
:
2802 case Hexagon::V6_vL32b_nt_tmp_npred_ai
:
2803 case Hexagon::V6_vgathermh_pseudo
:
2804 case Hexagon::V6_vgathermw_pseudo
:
2805 case Hexagon::V6_vgathermhw_pseudo
:
2806 case Hexagon::V6_vgathermhq_pseudo
:
2807 case Hexagon::V6_vgathermwq_pseudo
:
2808 case Hexagon::V6_vgathermhwq_pseudo
: {
2809 unsigned VectorSize
= TRI
->getSpillSize(Hexagon::HvxVRRegClass
);
2810 assert(isPowerOf2_32(VectorSize
));
2811 if (Offset
& (VectorSize
-1))
2813 return isInt
<4>(Offset
>> Log2_32(VectorSize
));
2816 case Hexagon::J2_loop0i
:
2817 case Hexagon::J2_loop1i
:
2818 return isUInt
<10>(Offset
);
2820 case Hexagon::S4_storeirb_io
:
2821 case Hexagon::S4_storeirbt_io
:
2822 case Hexagon::S4_storeirbf_io
:
2823 return isUInt
<6>(Offset
);
2825 case Hexagon::S4_storeirh_io
:
2826 case Hexagon::S4_storeirht_io
:
2827 case Hexagon::S4_storeirhf_io
:
2828 return isShiftedUInt
<6,1>(Offset
);
2830 case Hexagon::S4_storeiri_io
:
2831 case Hexagon::S4_storeirit_io
:
2832 case Hexagon::S4_storeirif_io
:
2833 return isShiftedUInt
<6,2>(Offset
);
2834 // Handle these two compare instructions that are not extendable.
2835 case Hexagon::A4_cmpbeqi
:
2836 return isUInt
<8>(Offset
);
2837 case Hexagon::A4_cmpbgti
:
2838 return isInt
<8>(Offset
);
2845 case Hexagon::L2_loadri_io
:
2846 case Hexagon::S2_storeri_io
:
2847 return (Offset
>= Hexagon_MEMW_OFFSET_MIN
) &&
2848 (Offset
<= Hexagon_MEMW_OFFSET_MAX
);
2850 case Hexagon::L2_loadrd_io
:
2851 case Hexagon::S2_storerd_io
:
2852 return (Offset
>= Hexagon_MEMD_OFFSET_MIN
) &&
2853 (Offset
<= Hexagon_MEMD_OFFSET_MAX
);
2855 case Hexagon::L2_loadrh_io
:
2856 case Hexagon::L2_loadruh_io
:
2857 case Hexagon::S2_storerh_io
:
2858 case Hexagon::S2_storerf_io
:
2859 return (Offset
>= Hexagon_MEMH_OFFSET_MIN
) &&
2860 (Offset
<= Hexagon_MEMH_OFFSET_MAX
);
2862 case Hexagon::L2_loadrb_io
:
2863 case Hexagon::L2_loadrub_io
:
2864 case Hexagon::S2_storerb_io
:
2865 return (Offset
>= Hexagon_MEMB_OFFSET_MIN
) &&
2866 (Offset
<= Hexagon_MEMB_OFFSET_MAX
);
2868 case Hexagon::A2_addi
:
2869 return (Offset
>= Hexagon_ADDI_OFFSET_MIN
) &&
2870 (Offset
<= Hexagon_ADDI_OFFSET_MAX
);
2872 case Hexagon::L4_iadd_memopw_io
:
2873 case Hexagon::L4_isub_memopw_io
:
2874 case Hexagon::L4_add_memopw_io
:
2875 case Hexagon::L4_sub_memopw_io
:
2876 case Hexagon::L4_iand_memopw_io
:
2877 case Hexagon::L4_ior_memopw_io
:
2878 case Hexagon::L4_and_memopw_io
:
2879 case Hexagon::L4_or_memopw_io
:
2880 return (0 <= Offset
&& Offset
<= 255);
2882 case Hexagon::L4_iadd_memoph_io
:
2883 case Hexagon::L4_isub_memoph_io
:
2884 case Hexagon::L4_add_memoph_io
:
2885 case Hexagon::L4_sub_memoph_io
:
2886 case Hexagon::L4_iand_memoph_io
:
2887 case Hexagon::L4_ior_memoph_io
:
2888 case Hexagon::L4_and_memoph_io
:
2889 case Hexagon::L4_or_memoph_io
:
2890 return (0 <= Offset
&& Offset
<= 127);
2892 case Hexagon::L4_iadd_memopb_io
:
2893 case Hexagon::L4_isub_memopb_io
:
2894 case Hexagon::L4_add_memopb_io
:
2895 case Hexagon::L4_sub_memopb_io
:
2896 case Hexagon::L4_iand_memopb_io
:
2897 case Hexagon::L4_ior_memopb_io
:
2898 case Hexagon::L4_and_memopb_io
:
2899 case Hexagon::L4_or_memopb_io
:
2900 return (0 <= Offset
&& Offset
<= 63);
2902 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
2903 // any size. Later pass knows how to handle it.
2904 case Hexagon::STriw_pred
:
2905 case Hexagon::LDriw_pred
:
2906 case Hexagon::STriw_ctr
:
2907 case Hexagon::LDriw_ctr
:
2910 case Hexagon::PS_fi
:
2911 case Hexagon::PS_fia
:
2912 case Hexagon::INLINEASM
:
2915 case Hexagon::L2_ploadrbt_io
:
2916 case Hexagon::L2_ploadrbf_io
:
2917 case Hexagon::L2_ploadrubt_io
:
2918 case Hexagon::L2_ploadrubf_io
:
2919 case Hexagon::S2_pstorerbt_io
:
2920 case Hexagon::S2_pstorerbf_io
:
2921 return isUInt
<6>(Offset
);
2923 case Hexagon::L2_ploadrht_io
:
2924 case Hexagon::L2_ploadrhf_io
:
2925 case Hexagon::L2_ploadruht_io
:
2926 case Hexagon::L2_ploadruhf_io
:
2927 case Hexagon::S2_pstorerht_io
:
2928 case Hexagon::S2_pstorerhf_io
:
2929 return isShiftedUInt
<6,1>(Offset
);
2931 case Hexagon::L2_ploadrit_io
:
2932 case Hexagon::L2_ploadrif_io
:
2933 case Hexagon::S2_pstorerit_io
:
2934 case Hexagon::S2_pstorerif_io
:
2935 return isShiftedUInt
<6,2>(Offset
);
2937 case Hexagon::L2_ploadrdt_io
:
2938 case Hexagon::L2_ploadrdf_io
:
2939 case Hexagon::S2_pstorerdt_io
:
2940 case Hexagon::S2_pstorerdf_io
:
2941 return isShiftedUInt
<6,3>(Offset
);
2943 case Hexagon::L2_loadbsw2_io
:
2944 case Hexagon::L2_loadbzw2_io
:
2945 return isShiftedInt
<11,1>(Offset
);
2947 case Hexagon::L2_loadbsw4_io
:
2948 case Hexagon::L2_loadbzw4_io
:
2949 return isShiftedInt
<11,2>(Offset
);
2952 dbgs() << "Failed Opcode is : " << Opcode
<< " (" << getName(Opcode
)
2954 llvm_unreachable("No offset range is defined for this opcode. "
2955 "Please define it in the above switch statement!");
2958 bool HexagonInstrInfo::isVecAcc(const MachineInstr
&MI
) const {
2959 return isHVXVec(MI
) && isAccumulator(MI
);
2962 bool HexagonInstrInfo::isVecALU(const MachineInstr
&MI
) const {
2963 const uint64_t F
= get(MI
.getOpcode()).TSFlags
;
2964 const uint64_t V
= ((F
>> HexagonII::TypePos
) & HexagonII::TypeMask
);
2966 V
== HexagonII::TypeCVI_VA
||
2967 V
== HexagonII::TypeCVI_VA_DV
;
2970 bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr
&ProdMI
,
2971 const MachineInstr
&ConsMI
) const {
2972 if (EnableACCForwarding
&& isVecAcc(ProdMI
) && isVecAcc(ConsMI
))
2975 if (EnableALUForwarding
&& (isVecALU(ConsMI
) || isLateSourceInstr(ConsMI
)))
2978 if (mayBeNewStore(ConsMI
))
2984 bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr
&MI
) const {
2985 switch (MI
.getOpcode()) {
2987 case Hexagon::L2_loadrub_io
:
2988 case Hexagon::L4_loadrub_ur
:
2989 case Hexagon::L4_loadrub_ap
:
2990 case Hexagon::L2_loadrub_pr
:
2991 case Hexagon::L2_loadrub_pbr
:
2992 case Hexagon::L2_loadrub_pi
:
2993 case Hexagon::L2_loadrub_pci
:
2994 case Hexagon::L2_loadrub_pcr
:
2995 case Hexagon::L2_loadbzw2_io
:
2996 case Hexagon::L4_loadbzw2_ur
:
2997 case Hexagon::L4_loadbzw2_ap
:
2998 case Hexagon::L2_loadbzw2_pr
:
2999 case Hexagon::L2_loadbzw2_pbr
:
3000 case Hexagon::L2_loadbzw2_pi
:
3001 case Hexagon::L2_loadbzw2_pci
:
3002 case Hexagon::L2_loadbzw2_pcr
:
3003 case Hexagon::L2_loadbzw4_io
:
3004 case Hexagon::L4_loadbzw4_ur
:
3005 case Hexagon::L4_loadbzw4_ap
:
3006 case Hexagon::L2_loadbzw4_pr
:
3007 case Hexagon::L2_loadbzw4_pbr
:
3008 case Hexagon::L2_loadbzw4_pi
:
3009 case Hexagon::L2_loadbzw4_pci
:
3010 case Hexagon::L2_loadbzw4_pcr
:
3011 case Hexagon::L4_loadrub_rr
:
3012 case Hexagon::L2_ploadrubt_io
:
3013 case Hexagon::L2_ploadrubt_pi
:
3014 case Hexagon::L2_ploadrubf_io
:
3015 case Hexagon::L2_ploadrubf_pi
:
3016 case Hexagon::L2_ploadrubtnew_io
:
3017 case Hexagon::L2_ploadrubfnew_io
:
3018 case Hexagon::L4_ploadrubt_rr
:
3019 case Hexagon::L4_ploadrubf_rr
:
3020 case Hexagon::L4_ploadrubtnew_rr
:
3021 case Hexagon::L4_ploadrubfnew_rr
:
3022 case Hexagon::L2_ploadrubtnew_pi
:
3023 case Hexagon::L2_ploadrubfnew_pi
:
3024 case Hexagon::L4_ploadrubt_abs
:
3025 case Hexagon::L4_ploadrubf_abs
:
3026 case Hexagon::L4_ploadrubtnew_abs
:
3027 case Hexagon::L4_ploadrubfnew_abs
:
3028 case Hexagon::L2_loadrubgp
:
3030 case Hexagon::L2_loadruh_io
:
3031 case Hexagon::L4_loadruh_ur
:
3032 case Hexagon::L4_loadruh_ap
:
3033 case Hexagon::L2_loadruh_pr
:
3034 case Hexagon::L2_loadruh_pbr
:
3035 case Hexagon::L2_loadruh_pi
:
3036 case Hexagon::L2_loadruh_pci
:
3037 case Hexagon::L2_loadruh_pcr
:
3038 case Hexagon::L4_loadruh_rr
:
3039 case Hexagon::L2_ploadruht_io
:
3040 case Hexagon::L2_ploadruht_pi
:
3041 case Hexagon::L2_ploadruhf_io
:
3042 case Hexagon::L2_ploadruhf_pi
:
3043 case Hexagon::L2_ploadruhtnew_io
:
3044 case Hexagon::L2_ploadruhfnew_io
:
3045 case Hexagon::L4_ploadruht_rr
:
3046 case Hexagon::L4_ploadruhf_rr
:
3047 case Hexagon::L4_ploadruhtnew_rr
:
3048 case Hexagon::L4_ploadruhfnew_rr
:
3049 case Hexagon::L2_ploadruhtnew_pi
:
3050 case Hexagon::L2_ploadruhfnew_pi
:
3051 case Hexagon::L4_ploadruht_abs
:
3052 case Hexagon::L4_ploadruhf_abs
:
3053 case Hexagon::L4_ploadruhtnew_abs
:
3054 case Hexagon::L4_ploadruhfnew_abs
:
3055 case Hexagon::L2_loadruhgp
:
3062 // Add latency to instruction.
3063 bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr
&MI1
,
3064 const MachineInstr
&MI2
) const {
3065 if (isHVXVec(MI1
) && isHVXVec(MI2
))
3066 if (!isVecUsableNextPacket(MI1
, MI2
))
3071 /// Get the base register and byte offset of a load/store instr.
3072 bool HexagonInstrInfo::getMemOperandsWithOffsetWidth(
3073 const MachineInstr
&LdSt
, SmallVectorImpl
<const MachineOperand
*> &BaseOps
,
3074 int64_t &Offset
, bool &OffsetIsScalable
, LocationSize
&Width
,
3075 const TargetRegisterInfo
*TRI
) const {
3076 OffsetIsScalable
= false;
3077 const MachineOperand
*BaseOp
= getBaseAndOffset(LdSt
, Offset
, Width
);
3078 if (!BaseOp
|| !BaseOp
->isReg())
3080 BaseOps
.push_back(BaseOp
);
3084 /// Can these instructions execute at the same time in a bundle.
3085 bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr
&First
,
3086 const MachineInstr
&Second
) const {
3087 if (Second
.mayStore() && First
.getOpcode() == Hexagon::S2_allocframe
) {
3088 const MachineOperand
&Op
= Second
.getOperand(0);
3089 if (Op
.isReg() && Op
.isUse() && Op
.getReg() == Hexagon::R29
)
3092 if (DisableNVSchedule
)
3094 if (mayBeNewStore(Second
)) {
3095 // Make sure the definition of the first instruction is the value being
3097 const MachineOperand
&Stored
=
3098 Second
.getOperand(Second
.getNumOperands() - 1);
3099 if (!Stored
.isReg())
3101 for (unsigned i
= 0, e
= First
.getNumOperands(); i
< e
; ++i
) {
3102 const MachineOperand
&Op
= First
.getOperand(i
);
3103 if (Op
.isReg() && Op
.isDef() && Op
.getReg() == Stored
.getReg())
3110 bool HexagonInstrInfo::doesNotReturn(const MachineInstr
&CallMI
) const {
3111 unsigned Opc
= CallMI
.getOpcode();
3112 return Opc
== Hexagon::PS_call_nr
|| Opc
== Hexagon::PS_callr_nr
;
3115 bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock
*B
) const {
3122 // Returns true if an instruction can be converted into a non-extended
3123 // equivalent instruction.
3124 bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr
&MI
) const {
3126 // Check if the instruction has a register form that uses register in place
3127 // of the extended operand, if so return that as the non-extended form.
3128 if (Hexagon::getRegForm(MI
.getOpcode()) >= 0)
3131 if (MI
.getDesc().mayLoad() || MI
.getDesc().mayStore()) {
3132 // Check addressing mode and retrieve non-ext equivalent instruction.
3134 switch (getAddrMode(MI
)) {
3135 case HexagonII::Absolute
:
3136 // Load/store with absolute addressing mode can be converted into
3137 // base+offset mode.
3138 NonExtOpcode
= Hexagon::changeAddrMode_abs_io(MI
.getOpcode());
3140 case HexagonII::BaseImmOffset
:
3141 // Load/store with base+offset addressing mode can be converted into
3142 // base+register offset addressing mode. However left shift operand should
3144 NonExtOpcode
= Hexagon::changeAddrMode_io_rr(MI
.getOpcode());
3146 case HexagonII::BaseLongOffset
:
3147 NonExtOpcode
= Hexagon::changeAddrMode_ur_rr(MI
.getOpcode());
3152 if (NonExtOpcode
< 0)
3159 bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr
&MI
) const {
3160 return Hexagon::getRealHWInstr(MI
.getOpcode(),
3161 Hexagon::InstrType_Pseudo
) >= 0;
3164 bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock
*B
)
3166 MachineBasicBlock::const_iterator I
= B
->getFirstTerminator(), E
= B
->end();
3175 // Returns true, if a LD insn can be promoted to a cur load.
3176 bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr
&MI
) const {
3177 const uint64_t F
= MI
.getDesc().TSFlags
;
3178 return ((F
>> HexagonII::mayCVLoadPos
) & HexagonII::mayCVLoadMask
) &&
3179 Subtarget
.hasV60Ops();
3182 // Returns true, if a ST insn can be promoted to a new-value store.
3183 bool HexagonInstrInfo::mayBeNewStore(const MachineInstr
&MI
) const {
3184 if (MI
.mayStore() && !Subtarget
.useNewValueStores())
3187 const uint64_t F
= MI
.getDesc().TSFlags
;
3188 return (F
>> HexagonII::mayNVStorePos
) & HexagonII::mayNVStoreMask
;
3191 bool HexagonInstrInfo::producesStall(const MachineInstr
&ProdMI
,
3192 const MachineInstr
&ConsMI
) const {
3193 // There is no stall when ProdMI is not a V60 vector.
3194 if (!isHVXVec(ProdMI
))
3197 // There is no stall when ProdMI and ConsMI are not dependent.
3198 if (!isDependent(ProdMI
, ConsMI
))
3201 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
3202 // are scheduled in consecutive packets.
3203 if (isVecUsableNextPacket(ProdMI
, ConsMI
))
3209 bool HexagonInstrInfo::producesStall(const MachineInstr
&MI
,
3210 MachineBasicBlock::const_instr_iterator BII
) const {
3211 // There is no stall when I is not a V60 vector.
3215 MachineBasicBlock::const_instr_iterator MII
= BII
;
3216 MachineBasicBlock::const_instr_iterator MIE
= MII
->getParent()->instr_end();
3218 if (!MII
->isBundle())
3219 return producesStall(*MII
, MI
);
3221 for (++MII
; MII
!= MIE
&& MII
->isInsideBundle(); ++MII
) {
3222 const MachineInstr
&J
= *MII
;
3223 if (producesStall(J
, MI
))
3229 bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr
&MI
,
3230 Register PredReg
) const {
3231 for (const MachineOperand
&MO
: MI
.operands()) {
3232 // Predicate register must be explicitly defined.
3233 if (MO
.isRegMask() && MO
.clobbersPhysReg(PredReg
))
3235 if (MO
.isReg() && MO
.isDef() && MO
.isImplicit() && (MO
.getReg() == PredReg
))
3239 // Instruction that produce late predicate cannot be used as sources of
3241 switch (MI
.getOpcode()) {
3242 case Hexagon::A4_addp_c
:
3243 case Hexagon::A4_subp_c
:
3244 case Hexagon::A4_tlbmatch
:
3245 case Hexagon::A5_ACS
:
3246 case Hexagon::F2_sfinvsqrta
:
3247 case Hexagon::F2_sfrecipa
:
3248 case Hexagon::J2_endloop0
:
3249 case Hexagon::J2_endloop01
:
3250 case Hexagon::J2_ploop1si
:
3251 case Hexagon::J2_ploop1sr
:
3252 case Hexagon::J2_ploop2si
:
3253 case Hexagon::J2_ploop2sr
:
3254 case Hexagon::J2_ploop3si
:
3255 case Hexagon::J2_ploop3sr
:
3256 case Hexagon::S2_cabacdecbin
:
3257 case Hexagon::S2_storew_locked
:
3258 case Hexagon::S4_stored_locked
:
3264 bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode
) const {
3265 return Opcode
== Hexagon::J2_jumpt
||
3266 Opcode
== Hexagon::J2_jumptpt
||
3267 Opcode
== Hexagon::J2_jumpf
||
3268 Opcode
== Hexagon::J2_jumpfpt
||
3269 Opcode
== Hexagon::J2_jumptnew
||
3270 Opcode
== Hexagon::J2_jumpfnew
||
3271 Opcode
== Hexagon::J2_jumptnewpt
||
3272 Opcode
== Hexagon::J2_jumpfnewpt
;
3275 bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef
<MachineOperand
> Cond
) const {
3276 if (Cond
.empty() || !isPredicated(Cond
[0].getImm()))
3278 return !isPredicatedTrue(Cond
[0].getImm());
3281 unsigned HexagonInstrInfo::getAddrMode(const MachineInstr
&MI
) const {
3282 const uint64_t F
= MI
.getDesc().TSFlags
;
3283 return (F
>> HexagonII::AddrModePos
) & HexagonII::AddrModeMask
;
3286 // Returns the base register in a memory access (load/store). The offset is
3287 // returned in Offset and the access size is returned in AccessSize.
3288 // If the base operand has a subregister or the offset field does not contain
3289 // an immediate value, return nullptr.
3291 HexagonInstrInfo::getBaseAndOffset(const MachineInstr
&MI
, int64_t &Offset
,
3292 LocationSize
&AccessSize
) const {
3293 // Return if it is not a base+offset type instruction or a MemOp.
3294 if (getAddrMode(MI
) != HexagonII::BaseImmOffset
&&
3295 getAddrMode(MI
) != HexagonII::BaseLongOffset
&&
3296 !isMemOp(MI
) && !isPostIncrement(MI
))
3299 AccessSize
= getMemAccessSize(MI
);
3301 unsigned BasePos
= 0, OffsetPos
= 0;
3302 if (!getBaseAndOffsetPosition(MI
, BasePos
, OffsetPos
))
3305 // Post increment updates its EA after the mem access,
3306 // so we need to treat its offset as zero.
3307 if (isPostIncrement(MI
)) {
3310 const MachineOperand
&OffsetOp
= MI
.getOperand(OffsetPos
);
3311 if (!OffsetOp
.isImm())
3313 Offset
= OffsetOp
.getImm();
3316 const MachineOperand
&BaseOp
= MI
.getOperand(BasePos
);
3317 if (BaseOp
.getSubReg() != 0)
3319 return &const_cast<MachineOperand
&>(BaseOp
);
3322 /// Return the position of the base and offset operands for this instruction.
3323 bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr
&MI
,
3324 unsigned &BasePos
, unsigned &OffsetPos
) const {
3325 if (!isAddrModeWithOffset(MI
) && !isPostIncrement(MI
))
3328 // Deal with memops first.
3332 } else if (MI
.mayStore()) {
3335 } else if (MI
.mayLoad()) {
3341 if (isPredicated(MI
)) {
3345 if (isPostIncrement(MI
)) {
3350 if (!MI
.getOperand(BasePos
).isReg() || !MI
.getOperand(OffsetPos
).isImm())
3356 // Inserts branching instructions in reverse order of their occurrence.
3357 // e.g. jump_t t1 (i1)
3359 // Jumpers = {i2, i1}
3360 SmallVector
<MachineInstr
*, 2> HexagonInstrInfo::getBranchingInstrs(
3361 MachineBasicBlock
& MBB
) const {
3362 SmallVector
<MachineInstr
*, 2> Jumpers
;
3363 // If the block has no terminators, it just falls into the block after it.
3364 MachineBasicBlock::instr_iterator I
= MBB
.instr_end();
3365 if (I
== MBB
.instr_begin())
3368 // A basic block may looks like this:
3378 // It has two succs but does not have a terminator
3379 // Don't know how to handle it.
3384 } while (I
!= MBB
.instr_begin());
3386 I
= MBB
.instr_end();
3389 while (I
->isDebugInstr()) {
3390 if (I
== MBB
.instr_begin())
3394 if (!isUnpredicatedTerminator(*I
))
3397 // Get the last instruction in the block.
3398 MachineInstr
*LastInst
= &*I
;
3399 Jumpers
.push_back(LastInst
);
3400 MachineInstr
*SecondLastInst
= nullptr;
3401 // Find one more terminator if present.
3403 if (&*I
!= LastInst
&& !I
->isBundle() && isUnpredicatedTerminator(*I
)) {
3404 if (!SecondLastInst
) {
3405 SecondLastInst
= &*I
;
3406 Jumpers
.push_back(SecondLastInst
);
3407 } else // This is a third branch.
3410 if (I
== MBB
.instr_begin())
3417 // Returns Operand Index for the constant extended instruction.
3418 unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr
&MI
) const {
3419 const uint64_t F
= MI
.getDesc().TSFlags
;
3420 return (F
>> HexagonII::ExtendableOpPos
) & HexagonII::ExtendableOpMask
;
3423 // See if instruction could potentially be a duplex candidate.
3424 // If so, return its group. Zero otherwise.
3425 HexagonII::CompoundGroup
HexagonInstrInfo::getCompoundCandidateGroup(
3426 const MachineInstr
&MI
) const {
3427 Register DstReg
, SrcReg
, Src1Reg
, Src2Reg
;
3429 switch (MI
.getOpcode()) {
3431 return HexagonII::HCG_None
;
3434 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3435 // "Rd16=#U6 ; jump #r9:2"
3436 // "Rd16=Rs16 ; jump #r9:2"
3438 case Hexagon::C2_cmpeq
:
3439 case Hexagon::C2_cmpgt
:
3440 case Hexagon::C2_cmpgtu
:
3441 DstReg
= MI
.getOperand(0).getReg();
3442 Src1Reg
= MI
.getOperand(1).getReg();
3443 Src2Reg
= MI
.getOperand(2).getReg();
3444 if (Hexagon::PredRegsRegClass
.contains(DstReg
) &&
3445 (Hexagon::P0
== DstReg
|| Hexagon::P1
== DstReg
) &&
3446 isIntRegForSubInst(Src1Reg
) && isIntRegForSubInst(Src2Reg
))
3447 return HexagonII::HCG_A
;
3449 case Hexagon::C2_cmpeqi
:
3450 case Hexagon::C2_cmpgti
:
3451 case Hexagon::C2_cmpgtui
:
3452 // P0 = cmp.eq(Rs,#u2)
3453 DstReg
= MI
.getOperand(0).getReg();
3454 SrcReg
= MI
.getOperand(1).getReg();
3455 if (Hexagon::PredRegsRegClass
.contains(DstReg
) &&
3456 (Hexagon::P0
== DstReg
|| Hexagon::P1
== DstReg
) &&
3457 isIntRegForSubInst(SrcReg
) && MI
.getOperand(2).isImm() &&
3458 ((isUInt
<5>(MI
.getOperand(2).getImm())) ||
3459 (MI
.getOperand(2).getImm() == -1)))
3460 return HexagonII::HCG_A
;
3462 case Hexagon::A2_tfr
:
3464 DstReg
= MI
.getOperand(0).getReg();
3465 SrcReg
= MI
.getOperand(1).getReg();
3466 if (isIntRegForSubInst(DstReg
) && isIntRegForSubInst(SrcReg
))
3467 return HexagonII::HCG_A
;
3469 case Hexagon::A2_tfrsi
:
3471 // Do not test for #u6 size since the const is getting extended
3472 // regardless and compound could be formed.
3473 DstReg
= MI
.getOperand(0).getReg();
3474 if (isIntRegForSubInst(DstReg
))
3475 return HexagonII::HCG_A
;
3477 case Hexagon::S2_tstbit_i
:
3478 DstReg
= MI
.getOperand(0).getReg();
3479 Src1Reg
= MI
.getOperand(1).getReg();
3480 if (Hexagon::PredRegsRegClass
.contains(DstReg
) &&
3481 (Hexagon::P0
== DstReg
|| Hexagon::P1
== DstReg
) &&
3482 MI
.getOperand(2).isImm() &&
3483 isIntRegForSubInst(Src1Reg
) && (MI
.getOperand(2).getImm() == 0))
3484 return HexagonII::HCG_A
;
3486 // The fact that .new form is used pretty much guarantees
3487 // that predicate register will match. Nevertheless,
3488 // there could be some false positives without additional
3490 case Hexagon::J2_jumptnew
:
3491 case Hexagon::J2_jumpfnew
:
3492 case Hexagon::J2_jumptnewpt
:
3493 case Hexagon::J2_jumpfnewpt
:
3494 Src1Reg
= MI
.getOperand(0).getReg();
3495 if (Hexagon::PredRegsRegClass
.contains(Src1Reg
) &&
3496 (Hexagon::P0
== Src1Reg
|| Hexagon::P1
== Src1Reg
))
3497 return HexagonII::HCG_B
;
3499 // Transfer and jump:
3500 // Rd=#U6 ; jump #r9:2
3501 // Rd=Rs ; jump #r9:2
3502 // Do not test for jump range here.
3503 case Hexagon::J2_jump
:
3504 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4
:
3505 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC
:
3506 return HexagonII::HCG_C
;
3509 return HexagonII::HCG_None
;
3512 // Returns -1 when there is no opcode found.
3513 unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr
&GA
,
3514 const MachineInstr
&GB
) const {
3515 assert(getCompoundCandidateGroup(GA
) == HexagonII::HCG_A
);
3516 assert(getCompoundCandidateGroup(GB
) == HexagonII::HCG_B
);
3517 if ((GA
.getOpcode() != Hexagon::C2_cmpeqi
) ||
3518 (GB
.getOpcode() != Hexagon::J2_jumptnew
))
3520 Register DestReg
= GA
.getOperand(0).getReg();
3521 if (!GB
.readsRegister(DestReg
, /*TRI=*/nullptr))
3523 if (DestReg
!= Hexagon::P0
&& DestReg
!= Hexagon::P1
)
3525 // The value compared against must be either u5 or -1.
3526 const MachineOperand
&CmpOp
= GA
.getOperand(2);
3529 int V
= CmpOp
.getImm();
3531 return DestReg
== Hexagon::P0
? Hexagon::J4_cmpeqn1_tp0_jump_nt
3532 : Hexagon::J4_cmpeqn1_tp1_jump_nt
;
3535 return DestReg
== Hexagon::P0
? Hexagon::J4_cmpeqi_tp0_jump_nt
3536 : Hexagon::J4_cmpeqi_tp1_jump_nt
;
3539 // Returns -1 if there is no opcode found.
3540 int HexagonInstrInfo::getDuplexOpcode(const MachineInstr
&MI
,
3541 bool ForBigCore
) const {
3542 // Static table to switch the opcodes across Tiny Core and Big Core.
3543 // dup_ opcodes are Big core opcodes.
3544 // NOTE: There are special instructions that need to handled later.
3545 // L4_return* instructions, they will only occupy SLOT0 (on big core too).
3546 // PS_jmpret - This pseudo translates to J2_jumpr which occupies only SLOT2.
3547 // The compiler need to base the root instruction to L6_return_map_to_raw
3548 // which can go any slot.
3549 static const std::map
<unsigned, unsigned> DupMap
= {
3550 {Hexagon::A2_add
, Hexagon::dup_A2_add
},
3551 {Hexagon::A2_addi
, Hexagon::dup_A2_addi
},
3552 {Hexagon::A2_andir
, Hexagon::dup_A2_andir
},
3553 {Hexagon::A2_combineii
, Hexagon::dup_A2_combineii
},
3554 {Hexagon::A2_sxtb
, Hexagon::dup_A2_sxtb
},
3555 {Hexagon::A2_sxth
, Hexagon::dup_A2_sxth
},
3556 {Hexagon::A2_tfr
, Hexagon::dup_A2_tfr
},
3557 {Hexagon::A2_tfrsi
, Hexagon::dup_A2_tfrsi
},
3558 {Hexagon::A2_zxtb
, Hexagon::dup_A2_zxtb
},
3559 {Hexagon::A2_zxth
, Hexagon::dup_A2_zxth
},
3560 {Hexagon::A4_combineii
, Hexagon::dup_A4_combineii
},
3561 {Hexagon::A4_combineir
, Hexagon::dup_A4_combineir
},
3562 {Hexagon::A4_combineri
, Hexagon::dup_A4_combineri
},
3563 {Hexagon::C2_cmoveif
, Hexagon::dup_C2_cmoveif
},
3564 {Hexagon::C2_cmoveit
, Hexagon::dup_C2_cmoveit
},
3565 {Hexagon::C2_cmovenewif
, Hexagon::dup_C2_cmovenewif
},
3566 {Hexagon::C2_cmovenewit
, Hexagon::dup_C2_cmovenewit
},
3567 {Hexagon::C2_cmpeqi
, Hexagon::dup_C2_cmpeqi
},
3568 {Hexagon::L2_deallocframe
, Hexagon::dup_L2_deallocframe
},
3569 {Hexagon::L2_loadrb_io
, Hexagon::dup_L2_loadrb_io
},
3570 {Hexagon::L2_loadrd_io
, Hexagon::dup_L2_loadrd_io
},
3571 {Hexagon::L2_loadrh_io
, Hexagon::dup_L2_loadrh_io
},
3572 {Hexagon::L2_loadri_io
, Hexagon::dup_L2_loadri_io
},
3573 {Hexagon::L2_loadrub_io
, Hexagon::dup_L2_loadrub_io
},
3574 {Hexagon::L2_loadruh_io
, Hexagon::dup_L2_loadruh_io
},
3575 {Hexagon::S2_allocframe
, Hexagon::dup_S2_allocframe
},
3576 {Hexagon::S2_storerb_io
, Hexagon::dup_S2_storerb_io
},
3577 {Hexagon::S2_storerd_io
, Hexagon::dup_S2_storerd_io
},
3578 {Hexagon::S2_storerh_io
, Hexagon::dup_S2_storerh_io
},
3579 {Hexagon::S2_storeri_io
, Hexagon::dup_S2_storeri_io
},
3580 {Hexagon::S4_storeirb_io
, Hexagon::dup_S4_storeirb_io
},
3581 {Hexagon::S4_storeiri_io
, Hexagon::dup_S4_storeiri_io
},
3583 unsigned OpNum
= MI
.getOpcode();
3584 // Conversion to Big core.
3586 auto Iter
= DupMap
.find(OpNum
);
3587 if (Iter
!= DupMap
.end())
3588 return Iter
->second
;
3589 } else { // Conversion to Tiny core.
3590 for (const auto &Iter
: DupMap
)
3591 if (Iter
.second
== OpNum
)
3597 int HexagonInstrInfo::getCondOpcode(int Opc
, bool invertPredicate
) const {
3598 enum Hexagon::PredSense inPredSense
;
3599 inPredSense
= invertPredicate
? Hexagon::PredSense_false
:
3600 Hexagon::PredSense_true
;
3601 int CondOpcode
= Hexagon::getPredOpcode(Opc
, inPredSense
);
3602 if (CondOpcode
>= 0) // Valid Conditional opcode/instruction
3605 llvm_unreachable("Unexpected predicable instruction");
3608 // Return the cur value instruction for a given store.
3609 int HexagonInstrInfo::getDotCurOp(const MachineInstr
&MI
) const {
3610 switch (MI
.getOpcode()) {
3611 default: llvm_unreachable("Unknown .cur type");
3612 case Hexagon::V6_vL32b_pi
:
3613 return Hexagon::V6_vL32b_cur_pi
;
3614 case Hexagon::V6_vL32b_ai
:
3615 return Hexagon::V6_vL32b_cur_ai
;
3616 case Hexagon::V6_vL32b_nt_pi
:
3617 return Hexagon::V6_vL32b_nt_cur_pi
;
3618 case Hexagon::V6_vL32b_nt_ai
:
3619 return Hexagon::V6_vL32b_nt_cur_ai
;
3620 case Hexagon::V6_vL32b_ppu
:
3621 return Hexagon::V6_vL32b_cur_ppu
;
3622 case Hexagon::V6_vL32b_nt_ppu
:
3623 return Hexagon::V6_vL32b_nt_cur_ppu
;
3628 // Return the regular version of the .cur instruction.
3629 int HexagonInstrInfo::getNonDotCurOp(const MachineInstr
&MI
) const {
3630 switch (MI
.getOpcode()) {
3631 default: llvm_unreachable("Unknown .cur type");
3632 case Hexagon::V6_vL32b_cur_pi
:
3633 return Hexagon::V6_vL32b_pi
;
3634 case Hexagon::V6_vL32b_cur_ai
:
3635 return Hexagon::V6_vL32b_ai
;
3636 case Hexagon::V6_vL32b_nt_cur_pi
:
3637 return Hexagon::V6_vL32b_nt_pi
;
3638 case Hexagon::V6_vL32b_nt_cur_ai
:
3639 return Hexagon::V6_vL32b_nt_ai
;
3640 case Hexagon::V6_vL32b_cur_ppu
:
3641 return Hexagon::V6_vL32b_ppu
;
3642 case Hexagon::V6_vL32b_nt_cur_ppu
:
3643 return Hexagon::V6_vL32b_nt_ppu
;
3648 // The diagram below shows the steps involved in the conversion of a predicated
3649 // store instruction to its .new predicated new-value form.
3651 // Note: It doesn't include conditional new-value stores as they can't be
3652 // converted to .new predicate.
3654 // p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3656 // / \ (not OK. it will cause new-value store to be
3657 // / X conditional on p0.new while R2 producer is
3660 // p.new store p.old NV store
3661 // [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3667 // [if (p0)memw(R0+#0)=R2]
3669 // The following set of instructions further explains the scenario where
3670 // conditional new-value store becomes invalid when promoted to .new predicate
3673 // { 1) if (p0) r0 = add(r1, r2)
3674 // 2) p0 = cmp.eq(r3, #0) }
3676 // 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3677 // the first two instructions because in instr 1, r0 is conditional on old value
3678 // of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3679 // is not valid for new-value stores.
3680 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3681 // from the "Conditional Store" list. Because a predicated new value store
3682 // would NOT be promoted to a double dot new store. See diagram below:
3683 // This function returns yes for those stores that are predicated but not
3684 // yet promoted to predicate dot new instructions.
3686 // +---------------------+
3687 // /-----| if (p0) memw(..)=r0 |---------\~
3688 // || +---------------------+ ||
3689 // promote || /\ /\ || promote
3691 // \||/ demote || \||/
3693 // +-------------------------+ || +-------------------------+
3694 // | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3695 // +-------------------------+ || +-------------------------+
3698 // promote || \/ NOT possible
3702 // +-----------------------------+
3703 // | if (p0.new) memw(..)=r0.new |
3704 // +-----------------------------+
3705 // Double Dot New Store
3707 // Returns the most basic instruction for the .new predicated instructions and
3708 // new-value stores.
3709 // For example, all of the following instructions will be converted back to the
3710 // same instruction:
3711 // 1) if (p0.new) memw(R0+#0) = R1.new --->
3712 // 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3713 // 3) if (p0.new) memw(R0+#0) = R1 --->
3715 // To understand the translation of instruction 1 to its original form, consider
3716 // a packet with 3 instructions.
3717 // { p0 = cmp.eq(R0,R1)
3718 // if (p0.new) R2 = add(R3, R4)
3719 // R5 = add (R3, R1)
3721 // if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3723 // This instruction can be part of the previous packet only if both p0 and R2
3724 // are promoted to .new values. This promotion happens in steps, first
3725 // predicate register is promoted to .new and in the next iteration R2 is
3726 // promoted. Therefore, in case of dependence check failure (due to R5) during
3727 // next iteration, it should be converted back to its most basic form.
3729 // Return the new value instruction for a given store.
3730 int HexagonInstrInfo::getDotNewOp(const MachineInstr
&MI
) const {
3731 int NVOpcode
= Hexagon::getNewValueOpcode(MI
.getOpcode());
3732 if (NVOpcode
>= 0) // Valid new-value store instruction.
3735 switch (MI
.getOpcode()) {
3737 report_fatal_error(Twine("Unknown .new type: ") +
3738 std::to_string(MI
.getOpcode()));
3739 case Hexagon::S4_storerb_ur
:
3740 return Hexagon::S4_storerbnew_ur
;
3742 case Hexagon::S2_storerb_pci
:
3743 return Hexagon::S2_storerb_pci
;
3745 case Hexagon::S2_storeri_pci
:
3746 return Hexagon::S2_storeri_pci
;
3748 case Hexagon::S2_storerh_pci
:
3749 return Hexagon::S2_storerh_pci
;
3751 case Hexagon::S2_storerd_pci
:
3752 return Hexagon::S2_storerd_pci
;
3754 case Hexagon::S2_storerf_pci
:
3755 return Hexagon::S2_storerf_pci
;
3757 case Hexagon::V6_vS32b_ai
:
3758 return Hexagon::V6_vS32b_new_ai
;
3760 case Hexagon::V6_vS32b_pi
:
3761 return Hexagon::V6_vS32b_new_pi
;
3766 // Returns the opcode to use when converting MI, which is a conditional jump,
3767 // into a conditional instruction which uses the .new value of the predicate.
3768 // We also use branch probabilities to add a hint to the jump.
3769 // If MBPI is null, all edges will be treated as equally likely for the
3770 // purposes of establishing a predication hint.
3771 int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr
&MI
,
3772 const MachineBranchProbabilityInfo
*MBPI
) const {
3773 // We assume that block can have at most two successors.
3774 const MachineBasicBlock
*Src
= MI
.getParent();
3775 const MachineOperand
&BrTarget
= MI
.getOperand(1);
3777 const BranchProbability
OneHalf(1, 2);
3779 auto getEdgeProbability
= [MBPI
] (const MachineBasicBlock
*Src
,
3780 const MachineBasicBlock
*Dst
) {
3782 return MBPI
->getEdgeProbability(Src
, Dst
);
3783 return BranchProbability(1, Src
->succ_size());
3786 if (BrTarget
.isMBB()) {
3787 const MachineBasicBlock
*Dst
= BrTarget
.getMBB();
3788 Taken
= getEdgeProbability(Src
, Dst
) >= OneHalf
;
3790 // The branch target is not a basic block (most likely a function).
3791 // Since BPI only gives probabilities for targets that are basic blocks,
3792 // try to identify another target of this branch (potentially a fall-
3793 // -through) and check the probability of that target.
3795 // The only handled branch combinations are:
3796 // - one conditional branch,
3797 // - one conditional branch followed by one unconditional branch.
3798 // Otherwise, assume not-taken.
3799 assert(MI
.isConditionalBranch());
3800 const MachineBasicBlock
&B
= *MI
.getParent();
3801 bool SawCond
= false, Bad
= false;
3802 for (const MachineInstr
&I
: B
) {
3805 if (I
.isConditionalBranch()) {
3812 if (I
.isUnconditionalBranch() && !SawCond
) {
3818 MachineBasicBlock::const_instr_iterator
It(MI
);
3819 MachineBasicBlock::const_instr_iterator NextIt
= std::next(It
);
3820 if (NextIt
== B
.instr_end()) {
3821 // If this branch is the last, look for the fall-through block.
3822 for (const MachineBasicBlock
*SB
: B
.successors()) {
3823 if (!B
.isLayoutSuccessor(SB
))
3825 Taken
= getEdgeProbability(Src
, SB
) < OneHalf
;
3829 assert(NextIt
->isUnconditionalBranch());
3830 // Find the first MBB operand and assume it's the target.
3831 const MachineBasicBlock
*BT
= nullptr;
3832 for (const MachineOperand
&Op
: NextIt
->operands()) {
3838 Taken
= BT
&& getEdgeProbability(Src
, BT
) < OneHalf
;
3843 // The Taken flag should be set to something reasonable by this point.
3845 switch (MI
.getOpcode()) {
3846 case Hexagon::J2_jumpt
:
3847 return Taken
? Hexagon::J2_jumptnewpt
: Hexagon::J2_jumptnew
;
3848 case Hexagon::J2_jumpf
:
3849 return Taken
? Hexagon::J2_jumpfnewpt
: Hexagon::J2_jumpfnew
;
3852 llvm_unreachable("Unexpected jump instruction.");
3856 // Return .new predicate version for an instruction.
3857 int HexagonInstrInfo::getDotNewPredOp(const MachineInstr
&MI
,
3858 const MachineBranchProbabilityInfo
*MBPI
) const {
3859 switch (MI
.getOpcode()) {
3861 case Hexagon::J2_jumpt
:
3862 case Hexagon::J2_jumpf
:
3863 return getDotNewPredJumpOp(MI
, MBPI
);
3866 int NewOpcode
= Hexagon::getPredNewOpcode(MI
.getOpcode());
3872 int HexagonInstrInfo::getDotOldOp(const MachineInstr
&MI
) const {
3873 int NewOp
= MI
.getOpcode();
3874 if (isPredicated(NewOp
) && isPredicatedNew(NewOp
)) { // Get predicate old form
3875 NewOp
= Hexagon::getPredOldOpcode(NewOp
);
3876 // All Hexagon architectures have prediction bits on dot-new branches,
3877 // but only Hexagon V60+ has prediction bits on dot-old ones. Make sure
3878 // to pick the right opcode when converting back to dot-old.
3879 if (!Subtarget
.hasFeature(Hexagon::ArchV60
)) {
3881 case Hexagon::J2_jumptpt
:
3882 NewOp
= Hexagon::J2_jumpt
;
3884 case Hexagon::J2_jumpfpt
:
3885 NewOp
= Hexagon::J2_jumpf
;
3887 case Hexagon::J2_jumprtpt
:
3888 NewOp
= Hexagon::J2_jumprt
;
3890 case Hexagon::J2_jumprfpt
:
3891 NewOp
= Hexagon::J2_jumprf
;
3895 assert(NewOp
>= 0 &&
3896 "Couldn't change predicate new instruction to its old form.");
3899 if (isNewValueStore(NewOp
)) { // Convert into non-new-value format
3900 NewOp
= Hexagon::getNonNVStore(NewOp
);
3901 assert(NewOp
>= 0 && "Couldn't change new-value store to its old form.");
3904 if (Subtarget
.hasV60Ops())
3907 // Subtargets prior to V60 didn't support 'taken' forms of predicated jumps.
3909 case Hexagon::J2_jumpfpt
:
3910 return Hexagon::J2_jumpf
;
3911 case Hexagon::J2_jumptpt
:
3912 return Hexagon::J2_jumpt
;
3913 case Hexagon::J2_jumprfpt
:
3914 return Hexagon::J2_jumprf
;
3915 case Hexagon::J2_jumprtpt
:
3916 return Hexagon::J2_jumprt
;
3921 // See if instruction could potentially be a duplex candidate.
3922 // If so, return its group. Zero otherwise.
3923 HexagonII::SubInstructionGroup
HexagonInstrInfo::getDuplexCandidateGroup(
3924 const MachineInstr
&MI
) const {
3925 Register DstReg
, SrcReg
, Src1Reg
, Src2Reg
;
3926 const HexagonRegisterInfo
&HRI
= *Subtarget
.getRegisterInfo();
3928 switch (MI
.getOpcode()) {
3930 return HexagonII::HSIG_None
;
3934 // Rd = memw(Rs+#u4:2)
3935 // Rd = memub(Rs+#u4:0)
3936 case Hexagon::L2_loadri_io
:
3937 case Hexagon::dup_L2_loadri_io
:
3938 DstReg
= MI
.getOperand(0).getReg();
3939 SrcReg
= MI
.getOperand(1).getReg();
3940 // Special case this one from Group L2.
3941 // Rd = memw(r29+#u5:2)
3942 if (isIntRegForSubInst(DstReg
)) {
3943 if (Hexagon::IntRegsRegClass
.contains(SrcReg
) &&
3944 HRI
.getStackRegister() == SrcReg
&&
3945 MI
.getOperand(2).isImm() &&
3946 isShiftedUInt
<5,2>(MI
.getOperand(2).getImm()))
3947 return HexagonII::HSIG_L2
;
3948 // Rd = memw(Rs+#u4:2)
3949 if (isIntRegForSubInst(SrcReg
) &&
3950 (MI
.getOperand(2).isImm() &&
3951 isShiftedUInt
<4,2>(MI
.getOperand(2).getImm())))
3952 return HexagonII::HSIG_L1
;
3955 case Hexagon::L2_loadrub_io
:
3956 case Hexagon::dup_L2_loadrub_io
:
3957 // Rd = memub(Rs+#u4:0)
3958 DstReg
= MI
.getOperand(0).getReg();
3959 SrcReg
= MI
.getOperand(1).getReg();
3960 if (isIntRegForSubInst(DstReg
) && isIntRegForSubInst(SrcReg
) &&
3961 MI
.getOperand(2).isImm() && isUInt
<4>(MI
.getOperand(2).getImm()))
3962 return HexagonII::HSIG_L1
;
3967 // Rd = memh/memuh(Rs+#u3:1)
3968 // Rd = memb(Rs+#u3:0)
3969 // Rd = memw(r29+#u5:2) - Handled above.
3970 // Rdd = memd(r29+#u5:3)
3972 // [if ([!]p0[.new])] dealloc_return
3973 // [if ([!]p0[.new])] jumpr r31
3974 case Hexagon::L2_loadrh_io
:
3975 case Hexagon::L2_loadruh_io
:
3976 case Hexagon::dup_L2_loadrh_io
:
3977 case Hexagon::dup_L2_loadruh_io
:
3978 // Rd = memh/memuh(Rs+#u3:1)
3979 DstReg
= MI
.getOperand(0).getReg();
3980 SrcReg
= MI
.getOperand(1).getReg();
3981 if (isIntRegForSubInst(DstReg
) && isIntRegForSubInst(SrcReg
) &&
3982 MI
.getOperand(2).isImm() &&
3983 isShiftedUInt
<3,1>(MI
.getOperand(2).getImm()))
3984 return HexagonII::HSIG_L2
;
3986 case Hexagon::L2_loadrb_io
:
3987 case Hexagon::dup_L2_loadrb_io
:
3988 // Rd = memb(Rs+#u3:0)
3989 DstReg
= MI
.getOperand(0).getReg();
3990 SrcReg
= MI
.getOperand(1).getReg();
3991 if (isIntRegForSubInst(DstReg
) && isIntRegForSubInst(SrcReg
) &&
3992 MI
.getOperand(2).isImm() &&
3993 isUInt
<3>(MI
.getOperand(2).getImm()))
3994 return HexagonII::HSIG_L2
;
3996 case Hexagon::L2_loadrd_io
:
3997 case Hexagon::dup_L2_loadrd_io
:
3998 // Rdd = memd(r29+#u5:3)
3999 DstReg
= MI
.getOperand(0).getReg();
4000 SrcReg
= MI
.getOperand(1).getReg();
4001 if (isDblRegForSubInst(DstReg
, HRI
) &&
4002 Hexagon::IntRegsRegClass
.contains(SrcReg
) &&
4003 HRI
.getStackRegister() == SrcReg
&&
4004 MI
.getOperand(2).isImm() &&
4005 isShiftedUInt
<5,3>(MI
.getOperand(2).getImm()))
4006 return HexagonII::HSIG_L2
;
4008 // dealloc_return is not documented in Hexagon Manual, but marked
4009 // with A_SUBINSN attribute in iset_v4classic.py.
4010 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4
:
4011 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC
:
4012 case Hexagon::L4_return
:
4013 case Hexagon::L2_deallocframe
:
4014 case Hexagon::dup_L2_deallocframe
:
4015 return HexagonII::HSIG_L2
;
4016 case Hexagon::EH_RETURN_JMPR
:
4017 case Hexagon::PS_jmpret
:
4018 case Hexagon::SL2_jumpr31
:
4020 // Actual form JMPR implicit-def %pc, implicit %r31, implicit internal %r0
4021 DstReg
= MI
.getOperand(0).getReg();
4022 if (Hexagon::IntRegsRegClass
.contains(DstReg
) && (Hexagon::R31
== DstReg
))
4023 return HexagonII::HSIG_L2
;
4025 case Hexagon::PS_jmprett
:
4026 case Hexagon::PS_jmpretf
:
4027 case Hexagon::PS_jmprettnewpt
:
4028 case Hexagon::PS_jmpretfnewpt
:
4029 case Hexagon::PS_jmprettnew
:
4030 case Hexagon::PS_jmpretfnew
:
4031 case Hexagon::SL2_jumpr31_t
:
4032 case Hexagon::SL2_jumpr31_f
:
4033 case Hexagon::SL2_jumpr31_tnew
:
4034 case Hexagon::SL2_jumpr31_fnew
:
4035 DstReg
= MI
.getOperand(1).getReg();
4036 SrcReg
= MI
.getOperand(0).getReg();
4037 // [if ([!]p0[.new])] jumpr r31
4038 if ((Hexagon::PredRegsRegClass
.contains(SrcReg
) &&
4039 (Hexagon::P0
== SrcReg
)) &&
4040 (Hexagon::IntRegsRegClass
.contains(DstReg
) && (Hexagon::R31
== DstReg
)))
4041 return HexagonII::HSIG_L2
;
4043 case Hexagon::L4_return_t
:
4044 case Hexagon::L4_return_f
:
4045 case Hexagon::L4_return_tnew_pnt
:
4046 case Hexagon::L4_return_fnew_pnt
:
4047 case Hexagon::L4_return_tnew_pt
:
4048 case Hexagon::L4_return_fnew_pt
:
4049 // [if ([!]p0[.new])] dealloc_return
4050 SrcReg
= MI
.getOperand(0).getReg();
4051 if (Hexagon::PredRegsRegClass
.contains(SrcReg
) && (Hexagon::P0
== SrcReg
))
4052 return HexagonII::HSIG_L2
;
4057 // memw(Rs+#u4:2) = Rt
4058 // memb(Rs+#u4:0) = Rt
4059 case Hexagon::S2_storeri_io
:
4060 case Hexagon::dup_S2_storeri_io
:
4061 // Special case this one from Group S2.
4062 // memw(r29+#u5:2) = Rt
4063 Src1Reg
= MI
.getOperand(0).getReg();
4064 Src2Reg
= MI
.getOperand(2).getReg();
4065 if (Hexagon::IntRegsRegClass
.contains(Src1Reg
) &&
4066 isIntRegForSubInst(Src2Reg
) &&
4067 HRI
.getStackRegister() == Src1Reg
&& MI
.getOperand(1).isImm() &&
4068 isShiftedUInt
<5,2>(MI
.getOperand(1).getImm()))
4069 return HexagonII::HSIG_S2
;
4070 // memw(Rs+#u4:2) = Rt
4071 if (isIntRegForSubInst(Src1Reg
) && isIntRegForSubInst(Src2Reg
) &&
4072 MI
.getOperand(1).isImm() &&
4073 isShiftedUInt
<4,2>(MI
.getOperand(1).getImm()))
4074 return HexagonII::HSIG_S1
;
4076 case Hexagon::S2_storerb_io
:
4077 case Hexagon::dup_S2_storerb_io
:
4078 // memb(Rs+#u4:0) = Rt
4079 Src1Reg
= MI
.getOperand(0).getReg();
4080 Src2Reg
= MI
.getOperand(2).getReg();
4081 if (isIntRegForSubInst(Src1Reg
) && isIntRegForSubInst(Src2Reg
) &&
4082 MI
.getOperand(1).isImm() && isUInt
<4>(MI
.getOperand(1).getImm()))
4083 return HexagonII::HSIG_S1
;
4088 // memh(Rs+#u3:1) = Rt
4089 // memw(r29+#u5:2) = Rt
4090 // memd(r29+#s6:3) = Rtt
4091 // memw(Rs+#u4:2) = #U1
4092 // memb(Rs+#u4) = #U1
4093 // allocframe(#u5:3)
4094 case Hexagon::S2_storerh_io
:
4095 case Hexagon::dup_S2_storerh_io
:
4096 // memh(Rs+#u3:1) = Rt
4097 Src1Reg
= MI
.getOperand(0).getReg();
4098 Src2Reg
= MI
.getOperand(2).getReg();
4099 if (isIntRegForSubInst(Src1Reg
) && isIntRegForSubInst(Src2Reg
) &&
4100 MI
.getOperand(1).isImm() &&
4101 isShiftedUInt
<3,1>(MI
.getOperand(1).getImm()))
4102 return HexagonII::HSIG_S1
;
4104 case Hexagon::S2_storerd_io
:
4105 case Hexagon::dup_S2_storerd_io
:
4106 // memd(r29+#s6:3) = Rtt
4107 Src1Reg
= MI
.getOperand(0).getReg();
4108 Src2Reg
= MI
.getOperand(2).getReg();
4109 if (isDblRegForSubInst(Src2Reg
, HRI
) &&
4110 Hexagon::IntRegsRegClass
.contains(Src1Reg
) &&
4111 HRI
.getStackRegister() == Src1Reg
&& MI
.getOperand(1).isImm() &&
4112 isShiftedInt
<6,3>(MI
.getOperand(1).getImm()))
4113 return HexagonII::HSIG_S2
;
4115 case Hexagon::S4_storeiri_io
:
4116 case Hexagon::dup_S4_storeiri_io
:
4117 // memw(Rs+#u4:2) = #U1
4118 Src1Reg
= MI
.getOperand(0).getReg();
4119 if (isIntRegForSubInst(Src1Reg
) && MI
.getOperand(1).isImm() &&
4120 isShiftedUInt
<4,2>(MI
.getOperand(1).getImm()) &&
4121 MI
.getOperand(2).isImm() && isUInt
<1>(MI
.getOperand(2).getImm()))
4122 return HexagonII::HSIG_S2
;
4124 case Hexagon::S4_storeirb_io
:
4125 case Hexagon::dup_S4_storeirb_io
:
4126 // memb(Rs+#u4) = #U1
4127 Src1Reg
= MI
.getOperand(0).getReg();
4128 if (isIntRegForSubInst(Src1Reg
) &&
4129 MI
.getOperand(1).isImm() && isUInt
<4>(MI
.getOperand(1).getImm()) &&
4130 MI
.getOperand(2).isImm() && isUInt
<1>(MI
.getOperand(2).getImm()))
4131 return HexagonII::HSIG_S2
;
4133 case Hexagon::S2_allocframe
:
4134 case Hexagon::dup_S2_allocframe
:
4135 if (MI
.getOperand(2).isImm() &&
4136 isShiftedUInt
<5,3>(MI
.getOperand(2).getImm()))
4137 return HexagonII::HSIG_S1
;
4146 // if ([!]P0[.new]) Rd = #0
4147 // Rd = add(r29,#u6:2)
4149 // P0 = cmp.eq(Rs,#u2)
4150 // Rdd = combine(#0,Rs)
4151 // Rdd = combine(Rs,#0)
4152 // Rdd = combine(#u2,#U2)
4155 // Rd = sxth/sxtb/zxtb/zxth(Rs)
4157 case Hexagon::A2_addi
:
4158 case Hexagon::dup_A2_addi
:
4159 DstReg
= MI
.getOperand(0).getReg();
4160 SrcReg
= MI
.getOperand(1).getReg();
4161 if (isIntRegForSubInst(DstReg
)) {
4162 // Rd = add(r29,#u6:2)
4163 if (Hexagon::IntRegsRegClass
.contains(SrcReg
) &&
4164 HRI
.getStackRegister() == SrcReg
&& MI
.getOperand(2).isImm() &&
4165 isShiftedUInt
<6,2>(MI
.getOperand(2).getImm()))
4166 return HexagonII::HSIG_A
;
4168 if ((DstReg
== SrcReg
) && MI
.getOperand(2).isImm() &&
4169 isInt
<7>(MI
.getOperand(2).getImm()))
4170 return HexagonII::HSIG_A
;
4173 if (isIntRegForSubInst(SrcReg
) && MI
.getOperand(2).isImm() &&
4174 ((MI
.getOperand(2).getImm() == 1) ||
4175 (MI
.getOperand(2).getImm() == -1)))
4176 return HexagonII::HSIG_A
;
4179 case Hexagon::A2_add
:
4180 case Hexagon::dup_A2_add
:
4182 DstReg
= MI
.getOperand(0).getReg();
4183 Src1Reg
= MI
.getOperand(1).getReg();
4184 Src2Reg
= MI
.getOperand(2).getReg();
4185 if (isIntRegForSubInst(DstReg
) && (DstReg
== Src1Reg
) &&
4186 isIntRegForSubInst(Src2Reg
))
4187 return HexagonII::HSIG_A
;
4189 case Hexagon::A2_andir
:
4190 case Hexagon::dup_A2_andir
:
4192 // Rd16=and(Rs16,#255)
4193 // Rd16=and(Rs16,#1)
4194 DstReg
= MI
.getOperand(0).getReg();
4195 SrcReg
= MI
.getOperand(1).getReg();
4196 if (isIntRegForSubInst(DstReg
) && isIntRegForSubInst(SrcReg
) &&
4197 MI
.getOperand(2).isImm() &&
4198 ((MI
.getOperand(2).getImm() == 1) ||
4199 (MI
.getOperand(2).getImm() == 255)))
4200 return HexagonII::HSIG_A
;
4202 case Hexagon::A2_tfr
:
4203 case Hexagon::dup_A2_tfr
:
4205 DstReg
= MI
.getOperand(0).getReg();
4206 SrcReg
= MI
.getOperand(1).getReg();
4207 if (isIntRegForSubInst(DstReg
) && isIntRegForSubInst(SrcReg
))
4208 return HexagonII::HSIG_A
;
4210 case Hexagon::A2_tfrsi
:
4211 case Hexagon::dup_A2_tfrsi
:
4213 // Do not test for #u6 size since the const is getting extended
4214 // regardless and compound could be formed.
4216 DstReg
= MI
.getOperand(0).getReg();
4217 if (isIntRegForSubInst(DstReg
))
4218 return HexagonII::HSIG_A
;
4220 case Hexagon::C2_cmoveit
:
4221 case Hexagon::C2_cmovenewit
:
4222 case Hexagon::C2_cmoveif
:
4223 case Hexagon::C2_cmovenewif
:
4224 case Hexagon::dup_C2_cmoveit
:
4225 case Hexagon::dup_C2_cmovenewit
:
4226 case Hexagon::dup_C2_cmoveif
:
4227 case Hexagon::dup_C2_cmovenewif
:
4228 // if ([!]P0[.new]) Rd = #0
4230 // %r16 = C2_cmovenewit internal %p0, 0, implicit undef %r16;
4231 DstReg
= MI
.getOperand(0).getReg();
4232 SrcReg
= MI
.getOperand(1).getReg();
4233 if (isIntRegForSubInst(DstReg
) &&
4234 Hexagon::PredRegsRegClass
.contains(SrcReg
) && Hexagon::P0
== SrcReg
&&
4235 MI
.getOperand(2).isImm() && MI
.getOperand(2).getImm() == 0)
4236 return HexagonII::HSIG_A
;
4238 case Hexagon::C2_cmpeqi
:
4239 case Hexagon::dup_C2_cmpeqi
:
4240 // P0 = cmp.eq(Rs,#u2)
4241 DstReg
= MI
.getOperand(0).getReg();
4242 SrcReg
= MI
.getOperand(1).getReg();
4243 if (Hexagon::PredRegsRegClass
.contains(DstReg
) &&
4244 Hexagon::P0
== DstReg
&& isIntRegForSubInst(SrcReg
) &&
4245 MI
.getOperand(2).isImm() && isUInt
<2>(MI
.getOperand(2).getImm()))
4246 return HexagonII::HSIG_A
;
4248 case Hexagon::A2_combineii
:
4249 case Hexagon::A4_combineii
:
4250 case Hexagon::dup_A2_combineii
:
4251 case Hexagon::dup_A4_combineii
:
4252 // Rdd = combine(#u2,#U2)
4253 DstReg
= MI
.getOperand(0).getReg();
4254 if (isDblRegForSubInst(DstReg
, HRI
) &&
4255 ((MI
.getOperand(1).isImm() && isUInt
<2>(MI
.getOperand(1).getImm())) ||
4256 (MI
.getOperand(1).isGlobal() &&
4257 isUInt
<2>(MI
.getOperand(1).getOffset()))) &&
4258 ((MI
.getOperand(2).isImm() && isUInt
<2>(MI
.getOperand(2).getImm())) ||
4259 (MI
.getOperand(2).isGlobal() &&
4260 isUInt
<2>(MI
.getOperand(2).getOffset()))))
4261 return HexagonII::HSIG_A
;
4263 case Hexagon::A4_combineri
:
4264 case Hexagon::dup_A4_combineri
:
4265 // Rdd = combine(Rs,#0)
4266 // Rdd = combine(Rs,#0)
4267 DstReg
= MI
.getOperand(0).getReg();
4268 SrcReg
= MI
.getOperand(1).getReg();
4269 if (isDblRegForSubInst(DstReg
, HRI
) && isIntRegForSubInst(SrcReg
) &&
4270 ((MI
.getOperand(2).isImm() && MI
.getOperand(2).getImm() == 0) ||
4271 (MI
.getOperand(2).isGlobal() && MI
.getOperand(2).getOffset() == 0)))
4272 return HexagonII::HSIG_A
;
4274 case Hexagon::A4_combineir
:
4275 case Hexagon::dup_A4_combineir
:
4276 // Rdd = combine(#0,Rs)
4277 DstReg
= MI
.getOperand(0).getReg();
4278 SrcReg
= MI
.getOperand(2).getReg();
4279 if (isDblRegForSubInst(DstReg
, HRI
) && isIntRegForSubInst(SrcReg
) &&
4280 ((MI
.getOperand(1).isImm() && MI
.getOperand(1).getImm() == 0) ||
4281 (MI
.getOperand(1).isGlobal() && MI
.getOperand(1).getOffset() == 0)))
4282 return HexagonII::HSIG_A
;
4284 case Hexagon::A2_sxtb
:
4285 case Hexagon::A2_sxth
:
4286 case Hexagon::A2_zxtb
:
4287 case Hexagon::A2_zxth
:
4288 case Hexagon::dup_A2_sxtb
:
4289 case Hexagon::dup_A2_sxth
:
4290 case Hexagon::dup_A2_zxtb
:
4291 case Hexagon::dup_A2_zxth
:
4292 // Rd = sxth/sxtb/zxtb/zxth(Rs)
4293 DstReg
= MI
.getOperand(0).getReg();
4294 SrcReg
= MI
.getOperand(1).getReg();
4295 if (isIntRegForSubInst(DstReg
) && isIntRegForSubInst(SrcReg
))
4296 return HexagonII::HSIG_A
;
4300 return HexagonII::HSIG_None
;
4303 short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr
&MI
) const {
4304 return Hexagon::getRealHWInstr(MI
.getOpcode(), Hexagon::InstrType_Real
);
4307 unsigned HexagonInstrInfo::getInstrTimingClassLatency(
4308 const InstrItineraryData
*ItinData
, const MachineInstr
&MI
) const {
4309 // Default to one cycle for no itinerary. However, an "empty" itinerary may
4310 // still have a MinLatency property, which getStageLatency checks.
4312 return getInstrLatency(ItinData
, MI
);
4314 if (MI
.isTransient())
4316 return ItinData
->getStageLatency(MI
.getDesc().getSchedClass());
4319 /// getOperandLatency - Compute and return the use operand latency of a given
4320 /// pair of def and use.
4321 /// In most cases, the static scheduling itinerary was enough to determine the
4322 /// operand latency. But it may not be possible for instructions with variable
4323 /// number of defs / uses.
4325 /// This is a raw interface to the itinerary that may be directly overriden by
4326 /// a target. Use computeOperandLatency to get the best estimate of latency.
4327 std::optional
<unsigned> HexagonInstrInfo::getOperandLatency(
4328 const InstrItineraryData
*ItinData
, const MachineInstr
&DefMI
,
4329 unsigned DefIdx
, const MachineInstr
&UseMI
, unsigned UseIdx
) const {
4330 const HexagonRegisterInfo
&HRI
= *Subtarget
.getRegisterInfo();
4332 // Get DefIdx and UseIdx for super registers.
4333 const MachineOperand
&DefMO
= DefMI
.getOperand(DefIdx
);
4335 if (DefMO
.isReg() && DefMO
.getReg().isPhysical()) {
4336 if (DefMO
.isImplicit()) {
4337 for (MCPhysReg SR
: HRI
.superregs(DefMO
.getReg())) {
4338 int Idx
= DefMI
.findRegisterDefOperandIdx(SR
, &HRI
, false, false);
4346 const MachineOperand
&UseMO
= UseMI
.getOperand(UseIdx
);
4347 if (UseMO
.isImplicit()) {
4348 for (MCPhysReg SR
: HRI
.superregs(UseMO
.getReg())) {
4349 int Idx
= UseMI
.findRegisterUseOperandIdx(SR
, &HRI
, false);
4358 std::optional
<unsigned> Latency
= TargetInstrInfo::getOperandLatency(
4359 ItinData
, DefMI
, DefIdx
, UseMI
, UseIdx
);
4361 // We should never have 0 cycle latency between two instructions unless
4362 // they can be packetized together. However, this decision can't be made
4368 // inverts the predication logic.
4371 bool HexagonInstrInfo::getInvertedPredSense(
4372 SmallVectorImpl
<MachineOperand
> &Cond
) const {
4375 unsigned Opc
= getInvertedPredicatedOpcode(Cond
[0].getImm());
4376 Cond
[0].setImm(Opc
);
4380 unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc
) const {
4382 InvPredOpcode
= isPredicatedTrue(Opc
) ? Hexagon::getFalsePredOpcode(Opc
)
4383 : Hexagon::getTruePredOpcode(Opc
);
4384 if (InvPredOpcode
>= 0) // Valid instruction with the inverted predicate.
4385 return InvPredOpcode
;
4387 llvm_unreachable("Unexpected predicated instruction");
4390 // Returns the max value that doesn't need to be extended.
4391 int HexagonInstrInfo::getMaxValue(const MachineInstr
&MI
) const {
4392 const uint64_t F
= MI
.getDesc().TSFlags
;
4393 unsigned isSigned
= (F
>> HexagonII::ExtentSignedPos
)
4394 & HexagonII::ExtentSignedMask
;
4395 unsigned bits
= (F
>> HexagonII::ExtentBitsPos
)
4396 & HexagonII::ExtentBitsMask
;
4398 if (isSigned
) // if value is signed
4399 return ~(-1U << (bits
- 1));
4401 return ~(-1U << bits
);
4405 bool HexagonInstrInfo::isAddrModeWithOffset(const MachineInstr
&MI
) const {
4406 switch (MI
.getOpcode()) {
4407 case Hexagon::L2_loadrbgp
:
4408 case Hexagon::L2_loadrdgp
:
4409 case Hexagon::L2_loadrhgp
:
4410 case Hexagon::L2_loadrigp
:
4411 case Hexagon::L2_loadrubgp
:
4412 case Hexagon::L2_loadruhgp
:
4413 case Hexagon::S2_storerbgp
:
4414 case Hexagon::S2_storerbnewgp
:
4415 case Hexagon::S2_storerhgp
:
4416 case Hexagon::S2_storerhnewgp
:
4417 case Hexagon::S2_storerigp
:
4418 case Hexagon::S2_storerinewgp
:
4419 case Hexagon::S2_storerdgp
:
4420 case Hexagon::S2_storerfgp
:
4423 const uint64_t F
= MI
.getDesc().TSFlags
;
4425 ((F
>> HexagonII::AddrModePos
) & HexagonII::AddrModeMask
);
4426 // Disallow any base+offset instruction. The assembler does not yet reorder
4427 // based up any zero offset instruction.
4428 return (addrMode
== HexagonII::BaseRegOffset
||
4429 addrMode
== HexagonII::BaseImmOffset
||
4430 addrMode
== HexagonII::BaseLongOffset
);
4433 bool HexagonInstrInfo::isPureSlot0(const MachineInstr
&MI
) const {
4434 // Workaround for the Global Scheduler. Sometimes, it creates
4435 // A4_ext as a Pseudo instruction and calls this function to see if
4436 // it can be added to an existing bundle. Since the instruction doesn't
4437 // belong to any BB yet, we can't use getUnits API.
4438 if (MI
.getOpcode() == Hexagon::A4_ext
)
4441 unsigned FuncUnits
= getUnits(MI
);
4442 return HexagonFUnits::isSlot0Only(FuncUnits
);
4445 bool HexagonInstrInfo::isRestrictNoSlot1Store(const MachineInstr
&MI
) const {
4446 const uint64_t F
= MI
.getDesc().TSFlags
;
4447 return ((F
>> HexagonII::RestrictNoSlot1StorePos
) &
4448 HexagonII::RestrictNoSlot1StoreMask
);
4451 void HexagonInstrInfo::changeDuplexOpcode(MachineBasicBlock::instr_iterator MII
,
4452 bool ToBigInstrs
) const {
4454 if (ToBigInstrs
) { // To BigCore Instr.
4455 // Check if the instruction can form a Duplex.
4456 if (getDuplexCandidateGroup(*MII
))
4457 // Get the opcode marked "dup_*" tag.
4458 Opcode
= getDuplexOpcode(*MII
, ToBigInstrs
);
4459 } else // To TinyCore Instr.
4460 Opcode
= getDuplexOpcode(*MII
, ToBigInstrs
);
4462 // Change the opcode of the instruction.
4464 MII
->setDesc(get(Opcode
));
4467 // This function is used to translate instructions to facilitate generating
4468 // Duplexes on TinyCore.
4469 void HexagonInstrInfo::translateInstrsForDup(MachineFunction
&MF
,
4470 bool ToBigInstrs
) const {
4472 for (MachineBasicBlock::instr_iterator Instr
= MB
.instr_begin(),
4473 End
= MB
.instr_end();
4474 Instr
!= End
; ++Instr
)
4475 changeDuplexOpcode(Instr
, ToBigInstrs
);
4478 // This is a specialized form of above function.
4479 void HexagonInstrInfo::translateInstrsForDup(
4480 MachineBasicBlock::instr_iterator MII
, bool ToBigInstrs
) const {
4481 MachineBasicBlock
*MBB
= MII
->getParent();
4482 while ((MII
!= MBB
->instr_end()) && MII
->isInsideBundle()) {
4483 changeDuplexOpcode(MII
, ToBigInstrs
);
4488 unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr
&MI
) const {
4489 using namespace HexagonII
;
4491 const uint64_t F
= MI
.getDesc().TSFlags
;
4492 unsigned S
= (F
>> MemAccessSizePos
) & MemAccesSizeMask
;
4493 unsigned Size
= getMemAccessSizeInBytes(MemAccessSize(S
));
4496 // Y2_dcfetchbo is special
4497 if (MI
.getOpcode() == Hexagon::Y2_dcfetchbo
)
4498 return HexagonII::DoubleWordAccess
;
4500 // Handle vector access sizes.
4501 const HexagonRegisterInfo
&HRI
= *Subtarget
.getRegisterInfo();
4503 case HexagonII::HVXVectorAccess
:
4504 return HRI
.getSpillSize(Hexagon::HvxVRRegClass
);
4506 llvm_unreachable("Unexpected instruction");
4510 // Returns the min value that doesn't need to be extended.
4511 int HexagonInstrInfo::getMinValue(const MachineInstr
&MI
) const {
4512 const uint64_t F
= MI
.getDesc().TSFlags
;
4513 unsigned isSigned
= (F
>> HexagonII::ExtentSignedPos
)
4514 & HexagonII::ExtentSignedMask
;
4515 unsigned bits
= (F
>> HexagonII::ExtentBitsPos
)
4516 & HexagonII::ExtentBitsMask
;
4518 if (isSigned
) // if value is signed
4519 return -1U << (bits
- 1);
4524 // Returns opcode of the non-extended equivalent instruction.
4525 short HexagonInstrInfo::getNonExtOpcode(const MachineInstr
&MI
) const {
4526 // Check if the instruction has a register form that uses register in place
4527 // of the extended operand, if so return that as the non-extended form.
4528 short NonExtOpcode
= Hexagon::getRegForm(MI
.getOpcode());
4529 if (NonExtOpcode
>= 0)
4530 return NonExtOpcode
;
4532 if (MI
.getDesc().mayLoad() || MI
.getDesc().mayStore()) {
4533 // Check addressing mode and retrieve non-ext equivalent instruction.
4534 switch (getAddrMode(MI
)) {
4535 case HexagonII::Absolute
:
4536 return Hexagon::changeAddrMode_abs_io(MI
.getOpcode());
4537 case HexagonII::BaseImmOffset
:
4538 return Hexagon::changeAddrMode_io_rr(MI
.getOpcode());
4539 case HexagonII::BaseLongOffset
:
4540 return Hexagon::changeAddrMode_ur_rr(MI
.getOpcode());
4549 bool HexagonInstrInfo::getPredReg(ArrayRef
<MachineOperand
> Cond
,
4550 Register
&PredReg
, unsigned &PredRegPos
, unsigned &PredRegFlags
) const {
4553 assert(Cond
.size() == 2);
4554 if (isNewValueJump(Cond
[0].getImm()) || Cond
[1].isMBB()) {
4555 LLVM_DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
4558 PredReg
= Cond
[1].getReg();
4560 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4562 if (Cond
[1].isImplicit())
4563 PredRegFlags
= RegState::Implicit
;
4564 if (Cond
[1].isUndef())
4565 PredRegFlags
|= RegState::Undef
;
4569 short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr
&MI
) const {
4570 return Hexagon::getRealHWInstr(MI
.getOpcode(), Hexagon::InstrType_Pseudo
);
4573 short HexagonInstrInfo::getRegForm(const MachineInstr
&MI
) const {
4574 return Hexagon::getRegForm(MI
.getOpcode());
4577 // Return the number of bytes required to encode the instruction.
4578 // Hexagon instructions are fixed length, 4 bytes, unless they
4579 // use a constant extender, which requires another 4 bytes.
4580 // For debug instructions and prolog labels, return 0.
4581 unsigned HexagonInstrInfo::getSize(const MachineInstr
&MI
) const {
4582 if (MI
.isDebugInstr() || MI
.isPosition())
4585 unsigned Size
= MI
.getDesc().getSize();
4587 // Assume the default insn size in case it cannot be determined
4588 // for whatever reason.
4589 Size
= HEXAGON_INSTR_SIZE
;
4591 if (isConstExtended(MI
) || isExtended(MI
))
4592 Size
+= HEXAGON_INSTR_SIZE
;
4594 // Try and compute number of instructions in asm.
4595 if (BranchRelaxAsmLarge
&& MI
.getOpcode() == Hexagon::INLINEASM
) {
4596 const MachineBasicBlock
&MBB
= *MI
.getParent();
4597 const MachineFunction
*MF
= MBB
.getParent();
4598 const MCAsmInfo
*MAI
= MF
->getTarget().getMCAsmInfo();
4600 // Count the number of register definitions to find the asm string.
4601 unsigned NumDefs
= 0;
4602 for (; MI
.getOperand(NumDefs
).isReg() && MI
.getOperand(NumDefs
).isDef();
4604 assert(NumDefs
!= MI
.getNumOperands()-2 && "No asm string?");
4606 assert(MI
.getOperand(NumDefs
).isSymbol() && "No asm string?");
4607 // Disassemble the AsmStr and approximate number of instructions.
4608 const char *AsmStr
= MI
.getOperand(NumDefs
).getSymbolName();
4609 Size
= getInlineAsmLength(AsmStr
, *MAI
);
4615 uint64_t HexagonInstrInfo::getType(const MachineInstr
&MI
) const {
4616 const uint64_t F
= MI
.getDesc().TSFlags
;
4617 return (F
>> HexagonII::TypePos
) & HexagonII::TypeMask
;
4620 InstrStage::FuncUnits
HexagonInstrInfo::getUnits(const MachineInstr
&MI
) const {
4621 const InstrItineraryData
&II
= *Subtarget
.getInstrItineraryData();
4622 const InstrStage
&IS
= *II
.beginStage(MI
.getDesc().getSchedClass());
4624 return IS
.getUnits();
4627 // Calculate size of the basic block without debug instructions.
4628 unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock
*BB
) const {
4629 return nonDbgMICount(BB
->instr_begin(), BB
->instr_end());
4632 unsigned HexagonInstrInfo::nonDbgBundleSize(
4633 MachineBasicBlock::const_iterator BundleHead
) const {
4634 assert(BundleHead
->isBundle() && "Not a bundle header");
4635 auto MII
= BundleHead
.getInstrIterator();
4636 // Skip the bundle header.
4637 return nonDbgMICount(++MII
, getBundleEnd(BundleHead
.getInstrIterator()));
4640 /// immediateExtend - Changes the instruction in place to one using an immediate
4642 void HexagonInstrInfo::immediateExtend(MachineInstr
&MI
) const {
4643 assert((isExtendable(MI
)||isConstExtended(MI
)) &&
4644 "Instruction must be extendable");
4645 // Find which operand is extendable.
4646 short ExtOpNum
= getCExtOpNum(MI
);
4647 MachineOperand
&MO
= MI
.getOperand(ExtOpNum
);
4648 // This needs to be something we understand.
4649 assert((MO
.isMBB() || MO
.isImm()) &&
4650 "Branch with unknown extendable field type");
4651 // Mark given operand as extended.
4652 MO
.addTargetFlag(HexagonII::HMOTF_ConstExtended
);
4655 bool HexagonInstrInfo::invertAndChangeJumpTarget(
4656 MachineInstr
&MI
, MachineBasicBlock
*NewTarget
) const {
4657 LLVM_DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to "
4658 << printMBBReference(*NewTarget
);
4660 assert(MI
.isBranch());
4661 unsigned NewOpcode
= getInvertedPredicatedOpcode(MI
.getOpcode());
4662 int TargetPos
= MI
.getNumOperands() - 1;
4663 // In general branch target is the last operand,
4664 // but some implicit defs added at the end might change it.
4665 while ((TargetPos
> -1) && !MI
.getOperand(TargetPos
).isMBB())
4667 assert((TargetPos
>= 0) && MI
.getOperand(TargetPos
).isMBB());
4668 MI
.getOperand(TargetPos
).setMBB(NewTarget
);
4669 if (EnableBranchPrediction
&& isPredicatedNew(MI
)) {
4670 NewOpcode
= reversePrediction(NewOpcode
);
4672 MI
.setDesc(get(NewOpcode
));
4676 void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction
&MF
) const {
4677 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4678 MachineFunction::iterator A
= MF
.begin();
4679 MachineBasicBlock
&B
= *A
;
4680 MachineBasicBlock::iterator I
= B
.begin();
4681 DebugLoc DL
= I
->getDebugLoc();
4682 MachineInstr
*NewMI
;
4684 for (unsigned insn
= TargetOpcode::GENERIC_OP_END
+1;
4685 insn
< Hexagon::INSTRUCTION_LIST_END
; ++insn
) {
4686 NewMI
= BuildMI(B
, I
, DL
, get(insn
));
4687 LLVM_DEBUG(dbgs() << "\n"
4688 << getName(NewMI
->getOpcode())
4689 << " Class: " << NewMI
->getDesc().getSchedClass());
4690 NewMI
->eraseFromParent();
4692 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4695 // inverts the predication logic.
4698 bool HexagonInstrInfo::reversePredSense(MachineInstr
&MI
) const {
4699 LLVM_DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI
.dump());
4700 MI
.setDesc(get(getInvertedPredicatedOpcode(MI
.getOpcode())));
4704 // Reverse the branch prediction.
4705 unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode
) const {
4706 int PredRevOpcode
= -1;
4707 if (isPredictedTaken(Opcode
))
4708 PredRevOpcode
= Hexagon::notTakenBranchPrediction(Opcode
);
4710 PredRevOpcode
= Hexagon::takenBranchPrediction(Opcode
);
4711 assert(PredRevOpcode
> 0);
4712 return PredRevOpcode
;
4715 // TODO: Add more rigorous validation.
4716 bool HexagonInstrInfo::validateBranchCond(const ArrayRef
<MachineOperand
> &Cond
)
4718 return Cond
.empty() || (Cond
[0].isImm() && (Cond
.size() != 1));
4721 void HexagonInstrInfo::
4722 setBundleNoShuf(MachineBasicBlock::instr_iterator MIB
) const {
4723 assert(MIB
->isBundle());
4724 MachineOperand
&Operand
= MIB
->getOperand(0);
4725 if (Operand
.isImm())
4726 Operand
.setImm(Operand
.getImm() | memShufDisabledMask
);
4728 MIB
->addOperand(MachineOperand::CreateImm(memShufDisabledMask
));
4731 bool HexagonInstrInfo::getBundleNoShuf(const MachineInstr
&MIB
) const {
4732 assert(MIB
.isBundle());
4733 const MachineOperand
&Operand
= MIB
.getOperand(0);
4734 return (Operand
.isImm() && (Operand
.getImm() & memShufDisabledMask
) != 0);
4737 // Addressing mode relations.
4738 short HexagonInstrInfo::changeAddrMode_abs_io(short Opc
) const {
4739 return Opc
>= 0 ? Hexagon::changeAddrMode_abs_io(Opc
) : Opc
;
4742 short HexagonInstrInfo::changeAddrMode_io_abs(short Opc
) const {
4743 return Opc
>= 0 ? Hexagon::changeAddrMode_io_abs(Opc
) : Opc
;
4746 short HexagonInstrInfo::changeAddrMode_io_pi(short Opc
) const {
4747 return Opc
>= 0 ? Hexagon::changeAddrMode_io_pi(Opc
) : Opc
;
4750 short HexagonInstrInfo::changeAddrMode_io_rr(short Opc
) const {
4751 return Opc
>= 0 ? Hexagon::changeAddrMode_io_rr(Opc
) : Opc
;
4754 short HexagonInstrInfo::changeAddrMode_pi_io(short Opc
) const {
4755 return Opc
>= 0 ? Hexagon::changeAddrMode_pi_io(Opc
) : Opc
;
4758 short HexagonInstrInfo::changeAddrMode_rr_io(short Opc
) const {
4759 return Opc
>= 0 ? Hexagon::changeAddrMode_rr_io(Opc
) : Opc
;
4762 short HexagonInstrInfo::changeAddrMode_rr_ur(short Opc
) const {
4763 return Opc
>= 0 ? Hexagon::changeAddrMode_rr_ur(Opc
) : Opc
;
4766 short HexagonInstrInfo::changeAddrMode_ur_rr(short Opc
) const {
4767 return Opc
>= 0 ? Hexagon::changeAddrMode_ur_rr(Opc
) : Opc
;
4770 MCInst
HexagonInstrInfo::getNop() const {
4771 static const MCInst Nop
= MCInstBuilder(Hexagon::A2_nop
);
4773 return MCInstBuilder(Hexagon::BUNDLE
)