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