blob: 2f6da901d8975c7bc75cf79ed168f41c3188cb58 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonInstrInfo.cpp - Hexagon Instruction Information ------------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Hexagon implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "HexagonInstrInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000015#include "Hexagon.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000016#include "HexagonHazardRecognizer.h"
Craig Topperb25fda92012-03-17 18:46:09 +000017#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000018#include "HexagonSubtarget.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000019#include "llvm/ADT/SmallPtrSet.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000020#include "llvm/ADT/SmallVector.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000021#include "llvm/ADT/StringRef.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000022#include "llvm/CodeGen/DFAPacketizer.h"
Ron Lieberman88159e52016-09-02 22:56:24 +000023#include "llvm/CodeGen/LivePhysRegs.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000024#include "llvm/CodeGen/MachineBasicBlock.h"
25#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000027#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000030#include "llvm/CodeGen/MachineInstrBundle.h"
31#include "llvm/CodeGen/MachineLoopInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000032#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000033#include "llvm/CodeGen/MachineOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000035#include "llvm/CodeGen/ScheduleDAG.h"
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000036#include "llvm/MC/MCAsmInfo.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000037#include "llvm/MC/MCInstrDesc.h"
38#include "llvm/MC/MCInstrItineraries.h"
39#include "llvm/MC/MCRegisterInfo.h"
40#include "llvm/Support/BranchProbability.h"
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +000041#include "llvm/Support/CommandLine.h"
Jyotsna Verma5ed51812013-05-01 21:37:34 +000042#include "llvm/Support/Debug.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000043#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000044#include "llvm/Support/MathExtras.h"
Reid Kleckner1c76f1552013-05-03 00:54:56 +000045#include "llvm/Support/raw_ostream.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000046#include "llvm/Target/TargetInstrInfo.h"
47#include "llvm/Target/TargetSubtargetInfo.h"
48#include <cassert>
Krzysztof Parzyszekaa935752015-11-24 15:11:13 +000049#include <cctype>
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000050#include <cstdint>
51#include <cstring>
52#include <iterator>
Tony Linthicum1213a7a2011-12-12 21:14:40 +000053
Tony Linthicum1213a7a2011-12-12 21:14:40 +000054using namespace llvm;
55
Chandler Carruthe96dd892014-04-21 22:55:11 +000056#define DEBUG_TYPE "hexagon-instrinfo"
57
Chandler Carruthd174b722014-04-22 02:03:14 +000058#define GET_INSTRINFO_CTOR_DTOR
59#define GET_INSTRMAP_INFO
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +000060#include "HexagonDepTimingClasses.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000061#include "HexagonGenDFAPacketizer.inc"
62#include "HexagonGenInstrInfo.inc"
Chandler Carruthd174b722014-04-22 02:03:14 +000063
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000064cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000065 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
66 "packetization boundary."));
67
68static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
69 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
70
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000071static cl::opt<bool> DisableNVSchedule("disable-hexagon-nv-schedule",
72 cl::Hidden, cl::ZeroOrMore, cl::init(false),
73 cl::desc("Disable schedule adjustment for new value stores."));
74
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000075static cl::opt<bool> EnableTimingClassLatency(
76 "enable-timing-class-latency", cl::Hidden, cl::init(false),
77 cl::desc("Enable timing class latency"));
78
79static cl::opt<bool> EnableALUForwarding(
80 "enable-alu-forwarding", cl::Hidden, cl::init(true),
81 cl::desc("Enable vec alu forwarding"));
82
83static cl::opt<bool> EnableACCForwarding(
84 "enable-acc-forwarding", cl::Hidden, cl::init(true),
85 cl::desc("Enable vec acc forwarding"));
86
87static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
88 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
89
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000090static cl::opt<bool> UseDFAHazardRec("dfa-hazard-rec",
91 cl::init(true), cl::Hidden, cl::ZeroOrMore,
92 cl::desc("Use the DFA based hazard recognizer."));
93
Tony Linthicum1213a7a2011-12-12 21:14:40 +000094///
95/// Constants for Hexagon instructions.
96///
97const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000098const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000099const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000100const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000101const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000102const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000103const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000104const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000105const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000106const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000107
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000108// Pin the vtable to this file.
109void HexagonInstrInfo::anchor() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000110
111HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000112 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP) {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000113
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000114static bool isIntRegForSubInst(unsigned Reg) {
115 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
116 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000117}
118
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000119static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000120 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_lo)) &&
121 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_hi));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000122}
123
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000124/// Calculate number of instructions excluding the debug instructions.
125static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
126 MachineBasicBlock::const_instr_iterator MIE) {
127 unsigned Count = 0;
128 for (; MIB != MIE; ++MIB) {
129 if (!MIB->isDebugValue())
130 ++Count;
131 }
132 return Count;
133}
134
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000135/// Find the hardware loop instruction used to set-up the specified loop.
136/// On Hexagon, we have two instructions used to set-up the hardware loop
137/// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
138/// to indicate the end of a loop.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000139static MachineInstr *findLoopInstr(MachineBasicBlock *BB, unsigned EndLoopOp,
140 MachineBasicBlock *TargetBB,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000141 SmallPtrSet<MachineBasicBlock *, 8> &Visited) {
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000142 unsigned LOOPi;
143 unsigned LOOPr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000144 if (EndLoopOp == Hexagon::ENDLOOP0) {
145 LOOPi = Hexagon::J2_loop0i;
146 LOOPr = Hexagon::J2_loop0r;
147 } else { // EndLoopOp == Hexagon::EndLOOP1
148 LOOPi = Hexagon::J2_loop1i;
149 LOOPr = Hexagon::J2_loop1r;
150 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000151
Brendon Cahoondf43e682015-05-08 16:16:29 +0000152 // The loop set-up instruction will be in a predecessor block
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000153 for (MachineBasicBlock *PB : BB->predecessors()) {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000154 // If this has been visited, already skip it.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000155 if (!Visited.insert(PB).second)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000156 continue;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000157 if (PB == BB)
Brendon Cahoondf43e682015-05-08 16:16:29 +0000158 continue;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000159 for (auto I = PB->instr_rbegin(), E = PB->instr_rend(); I != E; ++I) {
160 unsigned Opc = I->getOpcode();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000161 if (Opc == LOOPi || Opc == LOOPr)
162 return &*I;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000163 // We've reached a different loop, which means the loop01 has been
164 // removed.
165 if (Opc == EndLoopOp && I->getOperand(0).getMBB() != TargetBB)
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000166 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000167 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000168 // Check the predecessors for the LOOP instruction.
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000169 if (MachineInstr *Loop = findLoopInstr(PB, EndLoopOp, TargetBB, Visited))
170 return Loop;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000171 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000172 return nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000173}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000174
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000175/// Gather register def/uses from MI.
176/// This treats possible (predicated) defs as actually happening ones
177/// (conservatively).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000178static inline void parseOperands(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000179 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
180 Defs.clear();
181 Uses.clear();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000182
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000183 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
184 const MachineOperand &MO = MI.getOperand(i);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000185
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000186 if (!MO.isReg())
187 continue;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000188
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000189 unsigned Reg = MO.getReg();
190 if (!Reg)
191 continue;
192
193 if (MO.isUse())
194 Uses.push_back(MO.getReg());
195
196 if (MO.isDef())
197 Defs.push_back(MO.getReg());
198 }
199}
200
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000201// Position dependent, so check twice for swap.
202static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
203 switch (Ga) {
204 case HexagonII::HSIG_None:
205 default:
206 return false;
207 case HexagonII::HSIG_L1:
208 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
209 case HexagonII::HSIG_L2:
210 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
211 Gb == HexagonII::HSIG_A);
212 case HexagonII::HSIG_S1:
213 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
214 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
215 case HexagonII::HSIG_S2:
216 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
217 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
218 Gb == HexagonII::HSIG_A);
219 case HexagonII::HSIG_A:
220 return (Gb == HexagonII::HSIG_A);
221 case HexagonII::HSIG_Compound:
222 return (Gb == HexagonII::HSIG_Compound);
223 }
224 return false;
225}
226
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000227/// isLoadFromStackSlot - If the specified machine instruction is a direct
228/// load from a stack slot, return the virtual or physical register number of
229/// the destination along with the FrameIndex of the loaded stack slot. If
230/// not, return 0. This predicate must return 0 if the instruction has
231/// any side effects other than loading from the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000232unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000233 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000234 switch (MI.getOpcode()) {
235 default:
236 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000237 case Hexagon::L2_loadri_io:
238 case Hexagon::L2_loadrd_io:
239 case Hexagon::V6_vL32b_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +0000240 case Hexagon::V6_vL32b_nt_ai:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000241 case Hexagon::V6_vL32Ub_ai:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000242 case Hexagon::LDriw_pred:
243 case Hexagon::LDriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000244 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000245 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000246 case Hexagon::PS_vloadrw_nt_ai: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000247 const MachineOperand OpFI = MI.getOperand(1);
248 if (!OpFI.isFI())
249 return 0;
250 const MachineOperand OpOff = MI.getOperand(2);
251 if (!OpOff.isImm() || OpOff.getImm() != 0)
252 return 0;
253 FrameIndex = OpFI.getIndex();
254 return MI.getOperand(0).getReg();
255 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000256
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000257 case Hexagon::L2_ploadrit_io:
258 case Hexagon::L2_ploadrif_io:
259 case Hexagon::L2_ploadrdt_io:
260 case Hexagon::L2_ploadrdf_io: {
261 const MachineOperand OpFI = MI.getOperand(2);
262 if (!OpFI.isFI())
263 return 0;
264 const MachineOperand OpOff = MI.getOperand(3);
265 if (!OpOff.isImm() || OpOff.getImm() != 0)
266 return 0;
267 FrameIndex = OpFI.getIndex();
268 return MI.getOperand(0).getReg();
269 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000270 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000271
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000272 return 0;
273}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000274
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000275/// isStoreToStackSlot - If the specified machine instruction is a direct
276/// store to a stack slot, return the virtual or physical register number of
277/// the source reg along with the FrameIndex of the loaded stack slot. If
278/// not, return 0. This predicate must return 0 if the instruction has
279/// any side effects other than storing to the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000280unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000281 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000282 switch (MI.getOpcode()) {
283 default:
284 break;
285 case Hexagon::S2_storerb_io:
286 case Hexagon::S2_storerh_io:
287 case Hexagon::S2_storeri_io:
288 case Hexagon::S2_storerd_io:
289 case Hexagon::V6_vS32b_ai:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000290 case Hexagon::V6_vS32Ub_ai:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000291 case Hexagon::STriw_pred:
292 case Hexagon::STriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000293 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000294 case Hexagon::PS_vstorerw_ai: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000295 const MachineOperand &OpFI = MI.getOperand(0);
296 if (!OpFI.isFI())
297 return 0;
298 const MachineOperand &OpOff = MI.getOperand(1);
299 if (!OpOff.isImm() || OpOff.getImm() != 0)
300 return 0;
301 FrameIndex = OpFI.getIndex();
302 return MI.getOperand(2).getReg();
303 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000304
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000305 case Hexagon::S2_pstorerbt_io:
306 case Hexagon::S2_pstorerbf_io:
307 case Hexagon::S2_pstorerht_io:
308 case Hexagon::S2_pstorerhf_io:
309 case Hexagon::S2_pstorerit_io:
310 case Hexagon::S2_pstorerif_io:
311 case Hexagon::S2_pstorerdt_io:
312 case Hexagon::S2_pstorerdf_io: {
313 const MachineOperand &OpFI = MI.getOperand(1);
314 if (!OpFI.isFI())
315 return 0;
316 const MachineOperand &OpOff = MI.getOperand(2);
317 if (!OpOff.isImm() || OpOff.getImm() != 0)
318 return 0;
319 FrameIndex = OpFI.getIndex();
320 return MI.getOperand(3).getReg();
321 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000322 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000323
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000324 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000325}
326
Brendon Cahoondf43e682015-05-08 16:16:29 +0000327/// This function can analyze one/two way branching only and should (mostly) be
328/// called by target independent side.
329/// First entry is always the opcode of the branching instruction, except when
330/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
331/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
332/// e.g. Jump_c p will have
333/// Cond[0] = Jump_c
334/// Cond[1] = p
335/// HW-loop ENDLOOP:
336/// Cond[0] = ENDLOOP
337/// Cond[1] = MBB
338/// New value jump:
339/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
340/// Cond[1] = R
341/// Cond[2] = Imm
Brendon Cahoondf43e682015-05-08 16:16:29 +0000342///
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000343bool HexagonInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000344 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000345 MachineBasicBlock *&FBB,
346 SmallVectorImpl<MachineOperand> &Cond,
347 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000348 TBB = nullptr;
349 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000350 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000351
352 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000353 MachineBasicBlock::instr_iterator I = MBB.instr_end();
354 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000355 return false;
356
357 // A basic block may looks like this:
358 //
359 // [ insn
360 // EH_LABEL
361 // insn
362 // insn
363 // insn
364 // EH_LABEL
365 // insn ]
366 //
367 // It has two succs but does not have a terminator
368 // Don't know how to handle it.
369 do {
370 --I;
371 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000372 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000373 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000374 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000375
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000376 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000377 --I;
378
379 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000380 if (I == MBB.instr_begin())
381 return false;
382 --I;
383 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000384
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000385 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
386 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000387 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000388 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000389 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000390 DEBUG(dbgs() << "\nErasing the jump to successor block\n";);
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000391 I->eraseFromParent();
392 I = MBB.instr_end();
393 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000394 return false;
395 --I;
396 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000397 if (!isUnpredicatedTerminator(*I))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000398 return false;
399
400 // Get the last instruction in the block.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000401 MachineInstr *LastInst = &*I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000402 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000403 // Find one more terminator if present.
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000404 while (true) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000405 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000406 if (!SecondLastInst)
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000407 SecondLastInst = &*I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000408 else
409 // This is a third branch.
410 return true;
411 }
412 if (I == MBB.instr_begin())
413 break;
414 --I;
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000415 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000416
417 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000418 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
419 // If the branch target is not a basic block, it could be a tail call.
420 // (It is, if the target is a function.)
421 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
422 return true;
423 if (SecLastOpcode == Hexagon::J2_jump &&
424 !SecondLastInst->getOperand(0).isMBB())
425 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000426
427 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000428 bool LastOpcodeHasNVJump = isNewValueJump(*LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000429
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000430 if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
431 return true;
432
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000433 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000434 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000435 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000436 TBB = LastInst->getOperand(0).getMBB();
437 return false;
438 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000439 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000440 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000441 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000442 Cond.push_back(LastInst->getOperand(0));
443 return false;
444 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000445 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000446 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000447 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000448 Cond.push_back(LastInst->getOperand(0));
449 return false;
450 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000451 // Only supporting rr/ri versions of new-value jumps.
452 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
453 TBB = LastInst->getOperand(2).getMBB();
454 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
455 Cond.push_back(LastInst->getOperand(0));
456 Cond.push_back(LastInst->getOperand(1));
457 return false;
458 }
459 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
460 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000461 // Otherwise, don't know what this is.
462 return true;
463 }
464
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000465 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000466 bool SecLastOpcodeHasNVJump = isNewValueJump(*SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000467 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000468 if (!SecondLastInst->getOperand(1).isMBB())
469 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000470 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000471 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000472 Cond.push_back(SecondLastInst->getOperand(0));
473 FBB = LastInst->getOperand(0).getMBB();
474 return false;
475 }
476
Brendon Cahoondf43e682015-05-08 16:16:29 +0000477 // Only supporting rr/ri versions of new-value jumps.
478 if (SecLastOpcodeHasNVJump &&
479 (SecondLastInst->getNumExplicitOperands() == 3) &&
480 (LastOpcode == Hexagon::J2_jump)) {
481 TBB = SecondLastInst->getOperand(2).getMBB();
482 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
483 Cond.push_back(SecondLastInst->getOperand(0));
484 Cond.push_back(SecondLastInst->getOperand(1));
485 FBB = LastInst->getOperand(0).getMBB();
486 return false;
487 }
488
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000489 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
490 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000491 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000492 TBB = SecondLastInst->getOperand(0).getMBB();
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000493 I = LastInst->getIterator();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000494 if (AllowModify)
495 I->eraseFromParent();
496 return false;
497 }
498
Brendon Cahoondf43e682015-05-08 16:16:29 +0000499 // If the block ends with an ENDLOOP, and J2_jump, handle it.
500 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000501 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000502 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000503 Cond.push_back(SecondLastInst->getOperand(0));
504 FBB = LastInst->getOperand(0).getMBB();
505 return false;
506 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000507 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
508 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000509 // Otherwise, can't handle this.
510 return true;
511}
512
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000513unsigned HexagonInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000514 int *BytesRemoved) const {
515 assert(!BytesRemoved && "code size not handled");
516
Brendon Cahoondf43e682015-05-08 16:16:29 +0000517 DEBUG(dbgs() << "\nRemoving branches out of BB#" << MBB.getNumber());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000518 MachineBasicBlock::iterator I = MBB.end();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000519 unsigned Count = 0;
520 while (I != MBB.begin()) {
521 --I;
522 if (I->isDebugValue())
523 continue;
524 // Only removing branches from end of MBB.
525 if (!I->isBranch())
526 return Count;
527 if (Count && (I->getOpcode() == Hexagon::J2_jump))
528 llvm_unreachable("Malformed basic block: unconditional branch not last");
529 MBB.erase(&MBB.back());
530 I = MBB.end();
531 ++Count;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000532 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000533 return Count;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000534}
535
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000536unsigned HexagonInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000537 MachineBasicBlock *TBB,
538 MachineBasicBlock *FBB,
539 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000540 const DebugLoc &DL,
541 int *BytesAdded) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000542 unsigned BOpc = Hexagon::J2_jump;
543 unsigned BccOpc = Hexagon::J2_jumpt;
544 assert(validateBranchCond(Cond) && "Invalid branching condition");
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000545 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000546 assert(!BytesAdded && "code size not handled");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000547
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000548 // Check if reverseBranchCondition has asked to reverse this branch
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000549 // If we want to reverse the branch an odd number of times, we want
550 // J2_jumpf.
551 if (!Cond.empty() && Cond[0].isImm())
552 BccOpc = Cond[0].getImm();
553
554 if (!FBB) {
555 if (Cond.empty()) {
556 // Due to a bug in TailMerging/CFG Optimization, we need to add a
557 // special case handling of a predicated jump followed by an
558 // unconditional jump. If not, Tail Merging and CFG Optimization go
559 // into an infinite loop.
560 MachineBasicBlock *NewTBB, *NewFBB;
561 SmallVector<MachineOperand, 4> Cond;
Duncan P. N. Exon Smith25b132e2016-07-08 18:26:20 +0000562 auto Term = MBB.getFirstTerminator();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000563 if (Term != MBB.end() && isPredicated(*Term) &&
Duncan P. N. Exon Smithe04fe1a2016-08-17 00:34:00 +0000564 !analyzeBranch(MBB, NewTBB, NewFBB, Cond, false) &&
565 MachineFunction::iterator(NewTBB) == ++MBB.getIterator()) {
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000566 reverseBranchCondition(Cond);
567 removeBranch(MBB);
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000568 return insertBranch(MBB, TBB, nullptr, Cond, DL);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000569 }
570 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
571 } else if (isEndLoopN(Cond[0].getImm())) {
572 int EndLoopOp = Cond[0].getImm();
573 assert(Cond[1].isMBB());
574 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
575 // Check for it, and change the BB target if needed.
576 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000577 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
578 VisitedBBs);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000579 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
580 Loop->getOperand(0).setMBB(TBB);
581 // Add the ENDLOOP after the finding the LOOP0.
582 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
583 } else if (isNewValueJump(Cond[0].getImm())) {
584 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
585 // New value jump
586 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
587 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
588 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
589 DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
590 if (Cond[2].isReg()) {
591 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
592 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
593 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
594 } else if(Cond[2].isImm()) {
595 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
596 addImm(Cond[2].getImm()).addMBB(TBB);
597 } else
598 llvm_unreachable("Invalid condition for branching");
599 } else {
600 assert((Cond.size() == 2) && "Malformed cond vector");
601 const MachineOperand &RO = Cond[1];
602 unsigned Flags = getUndefRegState(RO.isUndef());
603 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
604 }
605 return 1;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000606 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000607 assert((!Cond.empty()) &&
608 "Cond. cannot be empty when multiple branchings are required");
609 assert((!isNewValueJump(Cond[0].getImm())) &&
610 "NV-jump cannot be inserted with another branch");
611 // Special case for hardware loops. The condition is a basic block.
612 if (isEndLoopN(Cond[0].getImm())) {
613 int EndLoopOp = Cond[0].getImm();
614 assert(Cond[1].isMBB());
615 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
616 // Check for it, and change the BB target if needed.
617 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000618 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, Cond[1].getMBB(),
619 VisitedBBs);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000620 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
621 Loop->getOperand(0).setMBB(TBB);
622 // Add the ENDLOOP after the finding the LOOP0.
623 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
624 } else {
625 const MachineOperand &RO = Cond[1];
626 unsigned Flags = getUndefRegState(RO.isUndef());
627 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000628 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000629 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000630
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000631 return 2;
632}
633
Brendon Cahoon254f8892016-07-29 16:44:44 +0000634/// Analyze the loop code to find the loop induction variable and compare used
635/// to compute the number of iterations. Currently, we analyze loop that are
636/// controlled using hardware loops. In this case, the induction variable
637/// instruction is null. For all other cases, this function returns true, which
638/// means we're unable to analyze it.
639bool HexagonInstrInfo::analyzeLoop(MachineLoop &L,
640 MachineInstr *&IndVarInst,
641 MachineInstr *&CmpInst) const {
642
643 MachineBasicBlock *LoopEnd = L.getBottomBlock();
644 MachineBasicBlock::iterator I = LoopEnd->getFirstTerminator();
645 // We really "analyze" only hardware loops right now.
646 if (I != LoopEnd->end() && isEndLoopN(I->getOpcode())) {
647 IndVarInst = nullptr;
648 CmpInst = &*I;
649 return false;
650 }
651 return true;
652}
653
654/// Generate code to reduce the loop iteration by one and check if the loop is
655/// finished. Return the value/register of the new loop count. this function
656/// assumes the nth iteration is peeled first.
657unsigned HexagonInstrInfo::reduceLoopCount(MachineBasicBlock &MBB,
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000658 MachineInstr *IndVar, MachineInstr &Cmp,
Brendon Cahoon254f8892016-07-29 16:44:44 +0000659 SmallVectorImpl<MachineOperand> &Cond,
660 SmallVectorImpl<MachineInstr *> &PrevInsts,
661 unsigned Iter, unsigned MaxIter) const {
662 // We expect a hardware loop currently. This means that IndVar is set
663 // to null, and the compare is the ENDLOOP instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000664 assert((!IndVar) && isEndLoopN(Cmp.getOpcode())
Brendon Cahoon254f8892016-07-29 16:44:44 +0000665 && "Expecting a hardware loop");
666 MachineFunction *MF = MBB.getParent();
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000667 DebugLoc DL = Cmp.getDebugLoc();
Brendon Cahoon254f8892016-07-29 16:44:44 +0000668 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszekd67ab622017-02-02 19:36:37 +0000669 MachineInstr *Loop = findLoopInstr(&MBB, Cmp.getOpcode(),
670 Cmp.getOperand(0).getMBB(), VisitedBBs);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000671 if (!Loop)
672 return 0;
673 // If the loop trip count is a compile-time value, then just change the
674 // value.
675 if (Loop->getOpcode() == Hexagon::J2_loop0i ||
676 Loop->getOpcode() == Hexagon::J2_loop1i) {
677 int64_t Offset = Loop->getOperand(1).getImm();
678 if (Offset <= 1)
679 Loop->eraseFromParent();
680 else
681 Loop->getOperand(1).setImm(Offset - 1);
682 return Offset - 1;
683 }
684 // The loop trip count is a run-time value. We generate code to subtract
685 // one from the trip count, and update the loop instruction.
686 assert(Loop->getOpcode() == Hexagon::J2_loop0r && "Unexpected instruction");
687 unsigned LoopCount = Loop->getOperand(1).getReg();
688 // Check if we're done with the loop.
689 unsigned LoopEnd = createVR(MF, MVT::i1);
690 MachineInstr *NewCmp = BuildMI(&MBB, DL, get(Hexagon::C2_cmpgtui), LoopEnd).
691 addReg(LoopCount).addImm(1);
692 unsigned NewLoopCount = createVR(MF, MVT::i32);
693 MachineInstr *NewAdd = BuildMI(&MBB, DL, get(Hexagon::A2_addi), NewLoopCount).
694 addReg(LoopCount).addImm(-1);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000695 const auto &HRI = *MF->getSubtarget<HexagonSubtarget>().getRegisterInfo();
Brendon Cahoon254f8892016-07-29 16:44:44 +0000696 // Update the previously generated instructions with the new loop counter.
697 for (SmallVectorImpl<MachineInstr *>::iterator I = PrevInsts.begin(),
698 E = PrevInsts.end(); I != E; ++I)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000699 (*I)->substituteRegister(LoopCount, NewLoopCount, 0, HRI);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000700 PrevInsts.clear();
701 PrevInsts.push_back(NewCmp);
702 PrevInsts.push_back(NewAdd);
703 // Insert the new loop instruction if this is the last time the loop is
704 // decremented.
705 if (Iter == MaxIter)
706 BuildMI(&MBB, DL, get(Hexagon::J2_loop0r)).
707 addMBB(Loop->getOperand(0).getMBB()).addReg(NewLoopCount);
708 // Delete the old loop instruction.
709 if (Iter == 0)
710 Loop->eraseFromParent();
711 Cond.push_back(MachineOperand::CreateImm(Hexagon::J2_jumpf));
712 Cond.push_back(NewCmp->getOperand(0));
713 return NewLoopCount;
714}
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000715
716bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
717 unsigned NumCycles, unsigned ExtraPredCycles,
718 BranchProbability Probability) const {
719 return nonDbgBBSize(&MBB) <= 3;
720}
721
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000722bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
723 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
724 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
725 const {
726 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
727}
728
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000729bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
730 unsigned NumInstrs, BranchProbability Probability) const {
731 return NumInstrs <= 4;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000732}
733
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000734void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000735 MachineBasicBlock::iterator I,
736 const DebugLoc &DL, unsigned DestReg,
737 unsigned SrcReg, bool KillSrc) const {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000738 MachineFunction &MF = *MBB.getParent();
739 auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000740 unsigned KillFlag = getKillRegState(KillSrc);
741
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000742 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000743 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000744 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000745 return;
746 }
747 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000748 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg)
749 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000750 return;
751 }
752 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
753 // Map Pd = Ps to Pd = or(Ps, Ps).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000754 BuildMI(MBB, I, DL, get(Hexagon::C2_or), DestReg)
755 .addReg(SrcReg).addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000756 return;
757 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000758 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000759 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000760 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
761 .addReg(SrcReg, KillFlag);
762 return;
763 }
764 if (Hexagon::IntRegsRegClass.contains(DestReg) &&
765 Hexagon::CtrRegsRegClass.contains(SrcReg)) {
766 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrcrr), DestReg)
767 .addReg(SrcReg, KillFlag);
768 return;
769 }
770 if (Hexagon::ModRegsRegClass.contains(DestReg) &&
771 Hexagon::IntRegsRegClass.contains(SrcReg)) {
772 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
773 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000774 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000775 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000776 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
777 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000778 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
779 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000780 return;
781 }
782 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
783 Hexagon::PredRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000784 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg)
785 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000786 return;
787 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000788 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
789 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000790 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
791 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000792 return;
793 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000794 if (Hexagon::HvxVRRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000795 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000796 addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000797 return;
798 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000799 if (Hexagon::HvxWRRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000800 unsigned LoSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
801 unsigned HiSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000802 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000803 .addReg(HiSrc, KillFlag)
804 .addReg(LoSrc, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000805 return;
806 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000807 if (Hexagon::HvxQRRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000808 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
809 .addReg(SrcReg)
810 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000811 return;
812 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000813 if (Hexagon::HvxQRRegClass.contains(SrcReg) &&
814 Hexagon::HvxVRRegClass.contains(DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000815 llvm_unreachable("Unimplemented pred to vec");
816 return;
817 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000818 if (Hexagon::HvxQRRegClass.contains(DestReg) &&
819 Hexagon::HvxVRRegClass.contains(SrcReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000820 llvm_unreachable("Unimplemented vec to pred");
821 return;
822 }
Sirish Pande30804c22012-02-15 18:52:27 +0000823
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000824#ifndef NDEBUG
825 // Show the invalid registers to ease debugging.
826 dbgs() << "Invalid registers for copy in BB#" << MBB.getNumber()
827 << ": " << PrintReg(DestReg, &HRI)
828 << " = " << PrintReg(SrcReg, &HRI) << '\n';
829#endif
Sirish Pande30804c22012-02-15 18:52:27 +0000830 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000831}
832
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000833void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
834 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
835 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000836 DebugLoc DL = MBB.findDebugLoc(I);
837 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000838 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000839 unsigned SlotAlign = MFI.getObjectAlignment(FI);
840 unsigned RegAlign = TRI->getSpillAlignment(*RC);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000841 unsigned KillFlag = getKillRegState(isKill);
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000842 bool HasAlloca = MFI.hasVarSizedObjects();
843 const auto &HST = MF.getSubtarget<HexagonSubtarget>();
844 const HexagonFrameLowering &HFI = *HST.getFrameLowering();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000845
Alex Lorenze40c8a22015-08-11 23:09:45 +0000846 MachineMemOperand *MMO = MF.getMachineMemOperand(
847 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000848 MFI.getObjectSize(FI), SlotAlign);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000849
Craig Topperc7242e02012-04-20 07:30:17 +0000850 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000851 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000852 .addFrameIndex(FI).addImm(0)
853 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000854 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000855 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000856 .addFrameIndex(FI).addImm(0)
857 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000858 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000859 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000860 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000861 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000862 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
863 BuildMI(MBB, I, DL, get(Hexagon::STriw_mod))
864 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000865 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000866 } else if (Hexagon::HvxQRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000867 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000868 .addFrameIndex(FI).addImm(0)
869 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000870 } else if (Hexagon::HvxVRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000871 // If there are variable-sized objects, spills will not be aligned.
872 if (HasAlloca)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000873 SlotAlign = HFI.getStackAlignment();
874 unsigned Opc = SlotAlign < RegAlign ? Hexagon::V6_vS32Ub_ai
875 : Hexagon::V6_vS32b_ai;
876 MachineMemOperand *MMOA = MF.getMachineMemOperand(
877 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
878 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000879 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000880 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000881 .addReg(SrcReg, KillFlag).addMemOperand(MMOA);
882 } else if (Hexagon::HvxWRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000883 // If there are variable-sized objects, spills will not be aligned.
884 if (HasAlloca)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000885 SlotAlign = HFI.getStackAlignment();
886 unsigned Opc = SlotAlign < RegAlign ? Hexagon::PS_vstorerwu_ai
887 : Hexagon::PS_vstorerw_ai;
888 MachineMemOperand *MMOA = MF.getMachineMemOperand(
889 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
890 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000891 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000892 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000893 .addReg(SrcReg, KillFlag).addMemOperand(MMOA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000894 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000895 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000896 }
897}
898
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000899void HexagonInstrInfo::loadRegFromStackSlot(
900 MachineBasicBlock &MBB, MachineBasicBlock::iterator I, unsigned DestReg,
901 int FI, const TargetRegisterClass *RC,
902 const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000903 DebugLoc DL = MBB.findDebugLoc(I);
904 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000905 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000906 unsigned SlotAlign = MFI.getObjectAlignment(FI);
907 unsigned RegAlign = TRI->getSpillAlignment(*RC);
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000908 bool HasAlloca = MFI.hasVarSizedObjects();
909 const auto &HST = MF.getSubtarget<HexagonSubtarget>();
910 const HexagonFrameLowering &HFI = *HST.getFrameLowering();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000911
Alex Lorenze40c8a22015-08-11 23:09:45 +0000912 MachineMemOperand *MMO = MF.getMachineMemOperand(
913 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000914 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000915
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000916 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000917 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000918 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000919 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000920 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000921 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000922 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000923 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000924 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
925 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
926 BuildMI(MBB, I, DL, get(Hexagon::LDriw_mod), DestReg)
927 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000928 } else if (Hexagon::HvxQRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000929 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000930 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000931 } else if (Hexagon::HvxVRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000932 // If there are variable-sized objects, spills will not be aligned.
933 if (HasAlloca)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000934 SlotAlign = HFI.getStackAlignment();
935 unsigned Opc = SlotAlign < RegAlign ? Hexagon::V6_vL32Ub_ai
936 : Hexagon::V6_vL32b_ai;
937 MachineMemOperand *MMOA = MF.getMachineMemOperand(
938 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
939 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000940 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000941 .addFrameIndex(FI).addImm(0).addMemOperand(MMOA);
942 } else if (Hexagon::HvxWRRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek781324f2017-05-03 15:23:53 +0000943 // If there are variable-sized objects, spills will not be aligned.
944 if (HasAlloca)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000945 SlotAlign = HFI.getStackAlignment();
946 unsigned Opc = SlotAlign < RegAlign ? Hexagon::PS_vloadrwu_ai
947 : Hexagon::PS_vloadrw_ai;
948 MachineMemOperand *MMOA = MF.getMachineMemOperand(
949 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
950 MFI.getObjectSize(FI), SlotAlign);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000951 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000952 .addFrameIndex(FI).addImm(0).addMemOperand(MMOA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000953 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000954 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000955 }
956}
957
Ron Lieberman88159e52016-09-02 22:56:24 +0000958static void getLiveRegsAt(LivePhysRegs &Regs, const MachineInstr &MI) {
959 const MachineBasicBlock &B = *MI.getParent();
960 Regs.addLiveOuts(B);
Duncan P. N. Exon Smith18720962016-09-11 18:51:28 +0000961 auto E = ++MachineBasicBlock::const_iterator(MI.getIterator()).getReverse();
Ron Lieberman88159e52016-09-02 22:56:24 +0000962 for (auto I = B.rbegin(); I != E; ++I)
963 Regs.stepBackward(*I);
964}
965
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000966/// expandPostRAPseudo - This function is called for all pseudo instructions
967/// that remain after register allocation. Many pseudo instructions are
968/// created to help register allocation. This is the place to convert them
969/// into real instructions. The target can edit MI in place, or it can insert
970/// new instructions and erase MI. The function should return true if
971/// anything was changed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000972bool HexagonInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000973 MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000974 MachineFunction &MF = *MBB.getParent();
975 MachineRegisterInfo &MRI = MF.getRegInfo();
976 const auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000977 DebugLoc DL = MI.getDebugLoc();
978 unsigned Opc = MI.getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000979
980 switch (Opc) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000981 case TargetOpcode::COPY: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000982 MachineOperand &MD = MI.getOperand(0);
983 MachineOperand &MS = MI.getOperand(1);
984 MachineBasicBlock::iterator MBBI = MI.getIterator();
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000985 if (MD.getReg() != MS.getReg() && !MS.isUndef()) {
986 copyPhysReg(MBB, MI, DL, MD.getReg(), MS.getReg(), MS.isKill());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000987 std::prev(MBBI)->copyImplicitOps(*MBB.getParent(), MI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000988 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000989 MBB.erase(MBBI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000990 return true;
991 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +0000992 case Hexagon::PS_aligna:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000993 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI.getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000994 .addReg(HRI.getFrameRegister())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000995 .addImm(-MI.getOperand(1).getImm());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000996 MBB.erase(MI);
997 return true;
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +0000998 case Hexagon::V6_vassignp: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000999 unsigned SrcReg = MI.getOperand(1).getReg();
1000 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001001 unsigned Kill = getKillRegState(MI.getOperand(1).isKill());
1002 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcombine), DstReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001003 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_hi), Kill)
1004 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_lo), Kill);
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001005 MBB.erase(MI);
1006 return true;
1007 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001008 case Hexagon::V6_lo: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001009 unsigned SrcReg = MI.getOperand(1).getReg();
1010 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001011 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001012 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001013 MBB.erase(MI);
1014 MRI.clearKillFlags(SrcSubLo);
1015 return true;
1016 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001017 case Hexagon::V6_hi: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001018 unsigned SrcReg = MI.getOperand(1).getReg();
1019 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001020 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001021 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001022 MBB.erase(MI);
1023 MRI.clearKillFlags(SrcSubHi);
1024 return true;
1025 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001026 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001027 case Hexagon::PS_vstorerwu_ai: {
1028 bool Aligned = Opc == Hexagon::PS_vstorerw_ai;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001029 unsigned SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001030 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
1031 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001032 unsigned NewOpc = Aligned ? Hexagon::V6_vS32b_ai : Hexagon::V6_vS32Ub_ai;
1033 unsigned Offset = HRI.getSpillSize(Hexagon::HvxVRRegClass);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001034
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001035 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001036 BuildMI(MBB, MI, DL, get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001037 .add(MI.getOperand(0))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001038 .addImm(MI.getOperand(1).getImm())
1039 .addReg(SrcSubLo)
1040 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001041 MI1New->getOperand(0).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001042 BuildMI(MBB, MI, DL, get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001043 .add(MI.getOperand(0))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001044 // The Vectors are indexed in multiples of vector size.
1045 .addImm(MI.getOperand(1).getImm() + Offset)
1046 .addReg(SrcSubHi)
1047 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001048 MBB.erase(MI);
1049 return true;
1050 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001051 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001052 case Hexagon::PS_vloadrwu_ai: {
1053 bool Aligned = Opc == Hexagon::PS_vloadrw_ai;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001054 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001055 unsigned NewOpc = Aligned ? Hexagon::V6_vL32b_ai : Hexagon::V6_vL32Ub_ai;
1056 unsigned Offset = HRI.getSpillSize(Hexagon::HvxVRRegClass);
1057
Diana Picus116bbab2017-01-13 09:58:52 +00001058 MachineInstr *MI1New = BuildMI(MBB, MI, DL, get(NewOpc),
1059 HRI.getSubReg(DstReg, Hexagon::vsub_lo))
Krzysztof Parzyszek4be9d922017-05-03 15:26:13 +00001060 .add(MI.getOperand(1))
1061 .addImm(MI.getOperand(2).getImm())
1062 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001063 MI1New->getOperand(1).setIsKill(false);
Diana Picus116bbab2017-01-13 09:58:52 +00001064 BuildMI(MBB, MI, DL, get(NewOpc), HRI.getSubReg(DstReg, Hexagon::vsub_hi))
1065 .add(MI.getOperand(1))
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001066 // The Vectors are indexed in multiples of vector size.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001067 .addImm(MI.getOperand(2).getImm() + Offset)
1068 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001069 MBB.erase(MI);
1070 return true;
1071 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001072 case Hexagon::PS_true: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001073 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001074 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1075 .addReg(Reg, RegState::Undef)
1076 .addReg(Reg, RegState::Undef);
1077 MBB.erase(MI);
1078 return true;
1079 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001080 case Hexagon::PS_false: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001081 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001082 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1083 .addReg(Reg, RegState::Undef)
1084 .addReg(Reg, RegState::Undef);
1085 MBB.erase(MI);
1086 return true;
1087 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001088 case Hexagon::PS_vmulw: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001089 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001090 unsigned DstReg = MI.getOperand(0).getReg();
1091 unsigned Src1Reg = MI.getOperand(1).getReg();
1092 unsigned Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001093 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1094 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1095 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1096 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001097 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001098 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001099 .addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001100 .addReg(Src2SubHi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001101 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001102 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001103 .addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001104 .addReg(Src2SubLo);
1105 MBB.erase(MI);
1106 MRI.clearKillFlags(Src1SubHi);
1107 MRI.clearKillFlags(Src1SubLo);
1108 MRI.clearKillFlags(Src2SubHi);
1109 MRI.clearKillFlags(Src2SubLo);
1110 return true;
1111 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001112 case Hexagon::PS_vmulw_acc: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001113 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001114 unsigned DstReg = MI.getOperand(0).getReg();
1115 unsigned Src1Reg = MI.getOperand(1).getReg();
1116 unsigned Src2Reg = MI.getOperand(2).getReg();
1117 unsigned Src3Reg = MI.getOperand(3).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001118 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1119 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1120 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1121 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
1122 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::isub_hi);
1123 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001124 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001125 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001126 .addReg(Src1SubHi)
1127 .addReg(Src2SubHi)
1128 .addReg(Src3SubHi);
1129 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001130 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001131 .addReg(Src1SubLo)
1132 .addReg(Src2SubLo)
1133 .addReg(Src3SubLo);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001134 MBB.erase(MI);
1135 MRI.clearKillFlags(Src1SubHi);
1136 MRI.clearKillFlags(Src1SubLo);
1137 MRI.clearKillFlags(Src2SubHi);
1138 MRI.clearKillFlags(Src2SubLo);
1139 MRI.clearKillFlags(Src3SubHi);
1140 MRI.clearKillFlags(Src3SubLo);
1141 return true;
1142 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001143 case Hexagon::PS_pselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001144 const MachineOperand &Op0 = MI.getOperand(0);
1145 const MachineOperand &Op1 = MI.getOperand(1);
1146 const MachineOperand &Op2 = MI.getOperand(2);
1147 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001148 unsigned Rd = Op0.getReg();
1149 unsigned Pu = Op1.getReg();
1150 unsigned Rs = Op2.getReg();
1151 unsigned Rt = Op3.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001152 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001153 unsigned K1 = getKillRegState(Op1.isKill());
1154 unsigned K2 = getKillRegState(Op2.isKill());
1155 unsigned K3 = getKillRegState(Op3.isKill());
1156 if (Rd != Rs)
1157 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1158 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1159 .addReg(Rs, K2);
1160 if (Rd != Rt)
1161 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1162 .addReg(Pu, K1)
1163 .addReg(Rt, K3);
1164 MBB.erase(MI);
1165 return true;
1166 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001167 case Hexagon::PS_vselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001168 const MachineOperand &Op0 = MI.getOperand(0);
1169 const MachineOperand &Op1 = MI.getOperand(1);
1170 const MachineOperand &Op2 = MI.getOperand(2);
1171 const MachineOperand &Op3 = MI.getOperand(3);
Matthias Braunac4307c2017-05-26 21:51:00 +00001172 LivePhysRegs LiveAtMI(HRI);
Ron Lieberman88159e52016-09-02 22:56:24 +00001173 getLiveRegsAt(LiveAtMI, MI);
1174 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001175 unsigned PReg = Op1.getReg();
1176 assert(Op1.getSubReg() == 0);
1177 unsigned PState = getRegState(Op1);
1178
Ron Lieberman88159e52016-09-02 22:56:24 +00001179 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001180 unsigned S = Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill
1181 : PState;
Ron Lieberman88159e52016-09-02 22:56:24 +00001182 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001183 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001184 .addReg(PReg, S)
Diana Picus116bbab2017-01-13 09:58:52 +00001185 .add(Op2);
Ron Lieberman88159e52016-09-02 22:56:24 +00001186 if (IsDestLive)
1187 T.addReg(Op0.getReg(), RegState::Implicit);
1188 IsDestLive = true;
1189 }
1190 if (Op0.getReg() != Op3.getReg()) {
1191 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
Diana Picus116bbab2017-01-13 09:58:52 +00001192 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001193 .addReg(PReg, PState)
Diana Picus116bbab2017-01-13 09:58:52 +00001194 .add(Op3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001195 if (IsDestLive)
1196 T.addReg(Op0.getReg(), RegState::Implicit);
1197 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001198 MBB.erase(MI);
1199 return true;
1200 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001201 case Hexagon::PS_wselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001202 MachineOperand &Op0 = MI.getOperand(0);
1203 MachineOperand &Op1 = MI.getOperand(1);
1204 MachineOperand &Op2 = MI.getOperand(2);
1205 MachineOperand &Op3 = MI.getOperand(3);
Matthias Braunac4307c2017-05-26 21:51:00 +00001206 LivePhysRegs LiveAtMI(HRI);
Ron Lieberman88159e52016-09-02 22:56:24 +00001207 getLiveRegsAt(LiveAtMI, MI);
1208 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001209 unsigned PReg = Op1.getReg();
1210 assert(Op1.getSubReg() == 0);
1211 unsigned PState = getRegState(Op1);
Ron Lieberman88159e52016-09-02 22:56:24 +00001212
1213 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001214 unsigned S = Op0.getReg() != Op3.getReg() ? PState & ~RegState::Kill
1215 : PState;
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001216 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_lo);
1217 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001218 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001219 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001220 .addReg(PReg, S)
Diana Picus116bbab2017-01-13 09:58:52 +00001221 .add(Op1)
1222 .addReg(SrcHi)
1223 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001224 if (IsDestLive)
1225 T.addReg(Op0.getReg(), RegState::Implicit);
1226 IsDestLive = true;
1227 }
1228 if (Op0.getReg() != Op3.getReg()) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001229 unsigned SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_lo);
1230 unsigned SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001231 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
Diana Picus116bbab2017-01-13 09:58:52 +00001232 .add(Op0)
Krzysztof Parzyszek25173e42017-06-27 19:59:46 +00001233 .addReg(PReg, PState)
Diana Picus116bbab2017-01-13 09:58:52 +00001234 .addReg(SrcHi)
1235 .addReg(SrcLo);
Ron Lieberman88159e52016-09-02 22:56:24 +00001236 if (IsDestLive)
1237 T.addReg(Op0.getReg(), RegState::Implicit);
1238 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001239 MBB.erase(MI);
1240 return true;
1241 }
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001242 case Hexagon::PS_tailcall_i:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001243 MI.setDesc(get(Hexagon::J2_jump));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001244 return true;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001245 case Hexagon::PS_tailcall_r:
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001246 case Hexagon::PS_jmpret:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001247 MI.setDesc(get(Hexagon::J2_jumpr));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001248 return true;
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001249 case Hexagon::PS_jmprett:
1250 MI.setDesc(get(Hexagon::J2_jumprt));
1251 return true;
1252 case Hexagon::PS_jmpretf:
1253 MI.setDesc(get(Hexagon::J2_jumprf));
1254 return true;
1255 case Hexagon::PS_jmprettnewpt:
1256 MI.setDesc(get(Hexagon::J2_jumprtnewpt));
1257 return true;
1258 case Hexagon::PS_jmpretfnewpt:
1259 MI.setDesc(get(Hexagon::J2_jumprfnewpt));
1260 return true;
1261 case Hexagon::PS_jmprettnew:
1262 MI.setDesc(get(Hexagon::J2_jumprtnew));
1263 return true;
1264 case Hexagon::PS_jmpretfnew:
1265 MI.setDesc(get(Hexagon::J2_jumprfnew));
1266 return true;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001267 }
1268
1269 return false;
1270}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001271
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001272// We indicate that we want to reverse the branch by
1273// inserting the reversed branching opcode.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001274bool HexagonInstrInfo::reverseBranchCondition(
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001275 SmallVectorImpl<MachineOperand> &Cond) const {
1276 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001277 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001278 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1279 unsigned opcode = Cond[0].getImm();
1280 //unsigned temp;
1281 assert(get(opcode).isBranch() && "Should be a branching condition.");
1282 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001283 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001284 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1285 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001286 return false;
1287}
1288
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001289void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1290 MachineBasicBlock::iterator MI) const {
1291 DebugLoc DL;
1292 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1293}
1294
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001295bool HexagonInstrInfo::isPostIncrement(const MachineInstr &MI) const {
1296 return getAddrMode(MI) == HexagonII::PostInc;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001297}
1298
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001299// Returns true if an instruction is predicated irrespective of the predicate
1300// sense. For example, all of the following will return true.
1301// if (p0) R1 = add(R2, R3)
1302// if (!p0) R1 = add(R2, R3)
1303// if (p0.new) R1 = add(R2, R3)
1304// if (!p0.new) R1 = add(R2, R3)
1305// Note: New-value stores are not included here as in the current
1306// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001307bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1308 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001309 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001310}
1311
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001312bool HexagonInstrInfo::PredicateInstruction(
1313 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001314 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1315 isEndLoopN(Cond[0].getImm())) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001316 DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001317 return false;
1318 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001319 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001320 assert (isPredicable(MI) && "Expected predicable instruction");
1321 bool invertJump = predOpcodeHasNot(Cond);
1322
1323 // We have to predicate MI "in place", i.e. after this function returns,
1324 // MI will need to be transformed into a predicated form. To avoid com-
1325 // plicated manipulations with the operands (handling tied operands,
1326 // etc.), build a new temporary instruction, then overwrite MI with it.
1327
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001328 MachineBasicBlock &B = *MI.getParent();
1329 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001330 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1331 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001332 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001333 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001334 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001335 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1336 break;
Diana Picus116bbab2017-01-13 09:58:52 +00001337 T.add(Op);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001338 NOp++;
1339 }
1340
1341 unsigned PredReg, PredRegPos, PredRegFlags;
1342 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1343 (void)GotPredReg;
1344 assert(GotPredReg);
1345 T.addReg(PredReg, PredRegFlags);
1346 while (NOp < NumOps)
Diana Picus116bbab2017-01-13 09:58:52 +00001347 T.add(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001348
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001349 MI.setDesc(get(PredOpc));
1350 while (unsigned n = MI.getNumOperands())
1351 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001352 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001353 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001354
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001355 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001356 B.erase(TI);
1357
1358 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1359 MRI.clearKillFlags(PredReg);
1360 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001361}
1362
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001363bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1364 ArrayRef<MachineOperand> Pred2) const {
1365 // TODO: Fix this
1366 return false;
1367}
1368
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001369bool HexagonInstrInfo::DefinesPredicate(MachineInstr &MI,
1370 std::vector<MachineOperand> &Pred) const {
1371 MachineFunction &MF = *MI.getParent()->getParent();
1372 const auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
1373
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001374 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1375 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001376 if (MO.isReg()) {
1377 if (!MO.isDef())
1378 continue;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001379 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1380 if (RC == &Hexagon::PredRegsRegClass) {
1381 Pred.push_back(MO);
1382 return true;
1383 }
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001384 continue;
1385 } else if (MO.isRegMask()) {
1386 for (unsigned PR : Hexagon::PredRegsRegClass) {
1387 if (!MI.modifiesRegister(PR, &HRI))
1388 continue;
1389 Pred.push_back(MO);
1390 return true;
1391 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001392 }
1393 }
1394 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001395}
Andrew Trickd06df962012-02-01 22:13:57 +00001396
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +00001397bool HexagonInstrInfo::isPredicable(const MachineInstr &MI) const {
Krzysztof Parzyszekee93e002017-05-05 22:13:57 +00001398 if (!MI.getDesc().isPredicable())
1399 return false;
1400
1401 if (MI.isCall() || isTailCall(MI)) {
1402 const MachineFunction &MF = *MI.getParent()->getParent();
1403 if (!MF.getSubtarget<HexagonSubtarget>().usePredicatedCalls())
1404 return false;
1405 }
1406 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001407}
1408
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001409bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1410 const MachineBasicBlock *MBB,
1411 const MachineFunction &MF) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001412 // Debug info is never a scheduling boundary. It's necessary to be explicit
1413 // due to the special treatment of IT instructions below, otherwise a
1414 // dbg_value followed by an IT will result in the IT instruction being
1415 // considered a scheduling hazard, which is wrong. It should be the actual
1416 // instruction preceding the dbg_value instruction(s), just like it is
1417 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001418 if (MI.isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001419 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001420
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001421 // Throwing call is a boundary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001422 if (MI.isCall()) {
Krzysztof Parzyszekab9127c2016-08-12 11:01:10 +00001423 // Don't mess around with no return calls.
1424 if (doesNotReturn(MI))
1425 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001426 // If any of the block's successors is a landing pad, this could be a
1427 // throwing call.
1428 for (auto I : MBB->successors())
1429 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001430 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001431 }
1432
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001433 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001434 if (MI.getDesc().isTerminator() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001435 return true;
1436
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001437 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1438 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001439
1440 return false;
1441}
1442
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001443/// Measure the specified inline asm to determine an approximation of its
1444/// length.
1445/// Comments (which run till the next SeparatorString or newline) do not
1446/// count as an instruction.
1447/// Any other non-whitespace text is considered an instruction, with
1448/// multiple instructions separated by SeparatorString or newlines.
1449/// Variable-length instructions are not handled here; this function
1450/// may be overloaded in the target code to do that.
1451/// Hexagon counts the number of ##'s and adjust for that many
1452/// constant exenders.
1453unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1454 const MCAsmInfo &MAI) const {
1455 StringRef AStr(Str);
1456 // Count the number of instructions in the asm.
1457 bool atInsnStart = true;
1458 unsigned Length = 0;
1459 for (; *Str; ++Str) {
1460 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1461 strlen(MAI.getSeparatorString())) == 0)
1462 atInsnStart = true;
1463 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1464 Length += MAI.getMaxInstLength();
1465 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001466 }
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001467 if (atInsnStart && strncmp(Str, MAI.getCommentString().data(),
1468 MAI.getCommentString().size()) == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001469 atInsnStart = false;
1470 }
1471
1472 // Add to size number of constant extenders seen * 4.
1473 StringRef Occ("##");
1474 Length += AStr.count(Occ)*4;
1475 return Length;
1476}
1477
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001478ScheduleHazardRecognizer*
1479HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1480 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +00001481 if (UseDFAHazardRec) {
1482 auto &HST = DAG->MF.getSubtarget<HexagonSubtarget>();
1483 return new HexagonHazardRecognizer(II, this, HST);
1484 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001485 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1486}
1487
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001488/// \brief For a comparison instruction, return the source registers in
1489/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1490/// compares against in CmpValue. Return true if the comparison instruction
1491/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001492bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1493 unsigned &SrcReg2, int &Mask,
1494 int &Value) const {
1495 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001496
1497 // Set mask and the first source register.
1498 switch (Opc) {
1499 case Hexagon::C2_cmpeq:
1500 case Hexagon::C2_cmpeqp:
1501 case Hexagon::C2_cmpgt:
1502 case Hexagon::C2_cmpgtp:
1503 case Hexagon::C2_cmpgtu:
1504 case Hexagon::C2_cmpgtup:
1505 case Hexagon::C4_cmpneq:
1506 case Hexagon::C4_cmplte:
1507 case Hexagon::C4_cmplteu:
1508 case Hexagon::C2_cmpeqi:
1509 case Hexagon::C2_cmpgti:
1510 case Hexagon::C2_cmpgtui:
1511 case Hexagon::C4_cmpneqi:
1512 case Hexagon::C4_cmplteui:
1513 case Hexagon::C4_cmpltei:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001514 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001515 Mask = ~0;
1516 break;
1517 case Hexagon::A4_cmpbeq:
1518 case Hexagon::A4_cmpbgt:
1519 case Hexagon::A4_cmpbgtu:
1520 case Hexagon::A4_cmpbeqi:
1521 case Hexagon::A4_cmpbgti:
1522 case Hexagon::A4_cmpbgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001523 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001524 Mask = 0xFF;
1525 break;
1526 case Hexagon::A4_cmpheq:
1527 case Hexagon::A4_cmphgt:
1528 case Hexagon::A4_cmphgtu:
1529 case Hexagon::A4_cmpheqi:
1530 case Hexagon::A4_cmphgti:
1531 case Hexagon::A4_cmphgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001532 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001533 Mask = 0xFFFF;
1534 break;
1535 }
1536
1537 // Set the value/second source register.
1538 switch (Opc) {
1539 case Hexagon::C2_cmpeq:
1540 case Hexagon::C2_cmpeqp:
1541 case Hexagon::C2_cmpgt:
1542 case Hexagon::C2_cmpgtp:
1543 case Hexagon::C2_cmpgtu:
1544 case Hexagon::C2_cmpgtup:
1545 case Hexagon::A4_cmpbeq:
1546 case Hexagon::A4_cmpbgt:
1547 case Hexagon::A4_cmpbgtu:
1548 case Hexagon::A4_cmpheq:
1549 case Hexagon::A4_cmphgt:
1550 case Hexagon::A4_cmphgtu:
1551 case Hexagon::C4_cmpneq:
1552 case Hexagon::C4_cmplte:
1553 case Hexagon::C4_cmplteu:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001554 SrcReg2 = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001555 return true;
1556
1557 case Hexagon::C2_cmpeqi:
1558 case Hexagon::C2_cmpgtui:
1559 case Hexagon::C2_cmpgti:
1560 case Hexagon::C4_cmpneqi:
1561 case Hexagon::C4_cmplteui:
1562 case Hexagon::C4_cmpltei:
1563 case Hexagon::A4_cmpbeqi:
1564 case Hexagon::A4_cmpbgti:
1565 case Hexagon::A4_cmpbgtui:
1566 case Hexagon::A4_cmpheqi:
1567 case Hexagon::A4_cmphgti:
1568 case Hexagon::A4_cmphgtui:
1569 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001570 Value = MI.getOperand(2).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001571 return true;
1572 }
1573
1574 return false;
1575}
1576
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001577unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001578 const MachineInstr &MI,
1579 unsigned *PredCost) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001580 return getInstrTimingClassLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001581}
1582
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001583
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001584DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1585 const TargetSubtargetInfo &STI) const {
1586 const InstrItineraryData *II = STI.getInstrItineraryData();
1587 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1588}
1589
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001590// Inspired by this pair:
1591// %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1592// S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1593// Currently AA considers the addresses in these instructions to be aliasing.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001594bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1595 MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001596 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1597 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001598 return false;
1599
1600 // Instructions that are pure loads, not loads and stores like memops are not
1601 // dependent.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001602 if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001603 return true;
1604
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001605 // Get the base register in MIa.
1606 unsigned BasePosA, OffsetPosA;
1607 if (!getBaseAndOffsetPosition(MIa, BasePosA, OffsetPosA))
1608 return false;
1609 const MachineOperand &BaseA = MIa.getOperand(BasePosA);
1610 unsigned BaseRegA = BaseA.getReg();
1611 unsigned BaseSubA = BaseA.getSubReg();
1612
1613 // Get the base register in MIb.
1614 unsigned BasePosB, OffsetPosB;
1615 if (!getBaseAndOffsetPosition(MIb, BasePosB, OffsetPosB))
1616 return false;
1617 const MachineOperand &BaseB = MIb.getOperand(BasePosB);
1618 unsigned BaseRegB = BaseB.getReg();
1619 unsigned BaseSubB = BaseB.getSubReg();
1620
1621 if (BaseRegA != BaseRegB || BaseSubA != BaseSubB)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001622 return false;
1623
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001624 // Get the access sizes.
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00001625 unsigned SizeA = getMemAccessSize(MIa);
1626 unsigned SizeB = getMemAccessSize(MIb);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001627
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001628 // Get the offsets. Handle immediates only for now.
1629 const MachineOperand &OffA = MIa.getOperand(OffsetPosA);
1630 const MachineOperand &OffB = MIb.getOperand(OffsetPosB);
1631 if (!MIa.getOperand(OffsetPosA).isImm() ||
1632 !MIb.getOperand(OffsetPosB).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001633 return false;
Krzysztof Parzyszekac019942017-07-19 19:17:32 +00001634 int OffsetA = isPostIncrement(MIa) ? 0 : OffA.getImm();
1635 int OffsetB = isPostIncrement(MIb) ? 0 : OffB.getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001636
1637 // This is a mem access with the same base register and known offsets from it.
1638 // Reason about it.
1639 if (OffsetA > OffsetB) {
Krzysztof Parzyszek3fce9d92017-07-19 18:03:46 +00001640 uint64_t OffDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1641 return SizeB <= OffDiff;
1642 }
1643 if (OffsetA < OffsetB) {
1644 uint64_t OffDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1645 return SizeA <= OffDiff;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001646 }
1647
1648 return false;
1649}
1650
Brendon Cahoon254f8892016-07-29 16:44:44 +00001651/// If the instruction is an increment of a constant value, return the amount.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001652bool HexagonInstrInfo::getIncrementValue(const MachineInstr &MI,
Brendon Cahoon254f8892016-07-29 16:44:44 +00001653 int &Value) const {
1654 if (isPostIncrement(MI)) {
1655 unsigned AccessSize;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001656 return getBaseAndOffset(MI, Value, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00001657 }
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001658 if (MI.getOpcode() == Hexagon::A2_addi) {
1659 Value = MI.getOperand(2).getImm();
Brendon Cahoon254f8892016-07-29 16:44:44 +00001660 return true;
1661 }
1662
1663 return false;
1664}
1665
Krzysztof Parzyszek0ac065f2017-07-10 18:31:02 +00001666std::pair<unsigned, unsigned>
1667HexagonInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
1668 return std::make_pair(TF & ~HexagonII::MO_Bitmasks,
1669 TF & HexagonII::MO_Bitmasks);
1670}
1671
1672ArrayRef<std::pair<unsigned, const char*>>
1673HexagonInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
1674 using namespace HexagonII;
1675 static const std::pair<unsigned, const char*> Flags[] = {
1676 {MO_PCREL, "hexagon-pcrel"},
1677 {MO_GOT, "hexagon-got"},
1678 {MO_LO16, "hexagon-lo16"},
1679 {MO_HI16, "hexagon-hi16"},
1680 {MO_GPREL, "hexagon-gprel"},
1681 {MO_GDGOT, "hexagon-gdgot"},
1682 {MO_GDPLT, "hexagon-gdplt"},
1683 {MO_IE, "hexagon-ie"},
1684 {MO_IEGOT, "hexagon-iegot"},
1685 {MO_TPREL, "hexagon-tprel"}
1686 };
1687 return makeArrayRef(Flags);
1688}
1689
1690ArrayRef<std::pair<unsigned, const char*>>
1691HexagonInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
1692 using namespace HexagonII;
1693 static const std::pair<unsigned, const char*> Flags[] = {
1694 {HMOTF_ConstExtended, "hexagon-ext"}
1695 };
1696 return makeArrayRef(Flags);
1697}
1698
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001699unsigned HexagonInstrInfo::createVR(MachineFunction *MF, MVT VT) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001700 MachineRegisterInfo &MRI = MF->getRegInfo();
1701 const TargetRegisterClass *TRC;
1702 if (VT == MVT::i1) {
1703 TRC = &Hexagon::PredRegsRegClass;
1704 } else if (VT == MVT::i32 || VT == MVT::f32) {
1705 TRC = &Hexagon::IntRegsRegClass;
1706 } else if (VT == MVT::i64 || VT == MVT::f64) {
1707 TRC = &Hexagon::DoubleRegsRegClass;
1708 } else {
1709 llvm_unreachable("Cannot handle this register class");
1710 }
1711
1712 unsigned NewReg = MRI.createVirtualRegister(TRC);
1713 return NewReg;
1714}
1715
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001716bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001717 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1718}
1719
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001720bool HexagonInstrInfo::isAccumulator(const MachineInstr &MI) const {
1721 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001722 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1723}
1724
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001725bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
1726 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001727 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1728 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1729
1730 if (!(isTC1(MI))
1731 && !(QII->isTC2Early(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001732 && !(MI.getDesc().mayLoad())
1733 && !(MI.getDesc().mayStore())
1734 && (MI.getDesc().getOpcode() != Hexagon::S2_allocframe)
1735 && (MI.getDesc().getOpcode() != Hexagon::L2_deallocframe)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001736 && !(QII->isMemOp(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001737 && !(MI.isBranch())
1738 && !(MI.isReturn())
1739 && !MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001740 return true;
1741
1742 return false;
1743}
1744
Sanjay Patele4b9f502015-12-07 19:21:39 +00001745// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001746bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekf65b8f12017-02-02 15:03:30 +00001747 return getType(MI) == HexagonII::TypeCJ && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001748}
1749
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001750// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1751// isFPImm and later getFPImm as well.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001752bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
1753 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001754 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1755 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001756 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001757
1758 unsigned isExtendable =
1759 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1760 if (!isExtendable)
1761 return false;
1762
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001763 if (MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001764 return false;
1765
1766 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001767 const MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001768 // Use MO operand flags to determine if MO
1769 // has the HMOTF_ConstExtended flag set.
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +00001770 if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001771 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001772 // If this is a Machine BB address we are talking about, and it is
1773 // not marked as extended, say so.
1774 if (MO.isMBB())
1775 return false;
1776
1777 // We could be using an instruction with an extendable immediate and shoehorn
1778 // a global address into it. If it is a global address it will be constant
1779 // extended. We do this for COMBINE.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001780 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001781 MO.isJTI() || MO.isCPI() || MO.isFPImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001782 return true;
1783
1784 // If the extendable operand is not 'Immediate' type, the instruction should
1785 // have 'isExtended' flag set.
1786 assert(MO.isImm() && "Extendable operand must be Immediate type");
1787
1788 int MinValue = getMinValue(MI);
1789 int MaxValue = getMaxValue(MI);
1790 int ImmValue = MO.getImm();
1791
1792 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001793}
1794
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001795bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
1796 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001797 case Hexagon::L4_return :
1798 case Hexagon::L4_return_t :
1799 case Hexagon::L4_return_f :
1800 case Hexagon::L4_return_tnew_pnt :
1801 case Hexagon::L4_return_fnew_pnt :
1802 case Hexagon::L4_return_tnew_pt :
1803 case Hexagon::L4_return_fnew_pt :
Krzysztof Parzyszek700a5f92017-05-03 15:34:52 +00001804 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001805 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001806 return false;
1807}
1808
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001809// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001810bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
1811 const MachineInstr &ConsMI) const {
1812 if (!ProdMI.getDesc().getNumDefs())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001813 return false;
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00001814 const MachineFunction &MF = *ProdMI.getParent()->getParent();
1815 const auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001816
1817 SmallVector<unsigned, 4> DefsA;
1818 SmallVector<unsigned, 4> DefsB;
1819 SmallVector<unsigned, 8> UsesA;
1820 SmallVector<unsigned, 8> UsesB;
1821
1822 parseOperands(ProdMI, DefsA, UsesA);
1823 parseOperands(ConsMI, DefsB, UsesB);
1824
1825 for (auto &RegA : DefsA)
1826 for (auto &RegB : UsesB) {
1827 // True data dependency.
1828 if (RegA == RegB)
1829 return true;
1830
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001831 if (TargetRegisterInfo::isPhysicalRegister(RegA))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001832 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1833 if (RegB == *SubRegs)
1834 return true;
1835
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001836 if (TargetRegisterInfo::isPhysicalRegister(RegB))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001837 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1838 if (RegA == *SubRegs)
1839 return true;
1840 }
1841
1842 return false;
1843}
1844
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001845// Returns true if the instruction is alread a .cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001846bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
1847 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001848 case Hexagon::V6_vL32b_cur_pi:
1849 case Hexagon::V6_vL32b_cur_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001850 return true;
1851 }
1852 return false;
1853}
1854
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001855// Returns true, if any one of the operands is a dot new
1856// insn, whether it is predicated dot new or register dot new.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001857bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
1858 if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001859 return true;
1860
1861 return false;
1862}
1863
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001864/// Symmetrical. See if these two instructions are fit for duplex pair.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001865bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
1866 const MachineInstr &MIb) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001867 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
1868 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
1869 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
1870}
1871
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001872bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
1873 if (MI.mayLoad() || MI.mayStore() || MI.isCompare())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001874 return true;
1875
1876 // Multiply
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001877 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001878 return is_TC4x(SchedClass) || is_TC3x(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001879}
1880
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001881bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
1882 return (Opcode == Hexagon::ENDLOOP0 ||
1883 Opcode == Hexagon::ENDLOOP1);
1884}
1885
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001886bool HexagonInstrInfo::isExpr(unsigned OpType) const {
1887 switch(OpType) {
1888 case MachineOperand::MO_MachineBasicBlock:
1889 case MachineOperand::MO_GlobalAddress:
1890 case MachineOperand::MO_ExternalSymbol:
1891 case MachineOperand::MO_JumpTableIndex:
1892 case MachineOperand::MO_ConstantPoolIndex:
1893 case MachineOperand::MO_BlockAddress:
1894 return true;
1895 default:
1896 return false;
1897 }
1898}
1899
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001900bool HexagonInstrInfo::isExtendable(const MachineInstr &MI) const {
1901 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001902 const uint64_t F = MID.TSFlags;
1903 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
1904 return true;
1905
1906 // TODO: This is largely obsolete now. Will need to be removed
1907 // in consecutive patches.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001908 switch (MI.getOpcode()) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001909 // PS_fi and PS_fia remain special cases.
1910 case Hexagon::PS_fi:
1911 case Hexagon::PS_fia:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001912 return true;
1913 default:
1914 return false;
1915 }
1916 return false;
1917}
1918
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001919// This returns true in two cases:
1920// - The OP code itself indicates that this is an extended instruction.
1921// - One of MOs has been marked with HMOTF_ConstExtended flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001922bool HexagonInstrInfo::isExtended(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001923 // First check if this is permanently extended op code.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001924 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001925 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
1926 return true;
1927 // Use MO operand flags to determine if one of MI's operands
1928 // has HMOTF_ConstExtended flag set.
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +00001929 for (const MachineOperand &MO : MI.operands())
1930 if (MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001931 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001932 return false;
1933}
1934
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001935bool HexagonInstrInfo::isFloat(const MachineInstr &MI) const {
1936 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001937 const uint64_t F = get(Opcode).TSFlags;
1938 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
1939}
1940
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001941// No V60 HVX VMEM with A_INDIRECT.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001942bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr &I,
1943 const MachineInstr &J) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001944 if (!isHVXVec(I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001945 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001946 if (!I.mayLoad() && !I.mayStore())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001947 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001948 return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001949}
1950
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001951bool HexagonInstrInfo::isIndirectCall(const MachineInstr &MI) const {
1952 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001953 case Hexagon::J2_callr :
1954 case Hexagon::J2_callrf :
1955 case Hexagon::J2_callrt :
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00001956 case Hexagon::PS_call_nr :
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001957 return true;
1958 }
1959 return false;
1960}
1961
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001962bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr &MI) const {
1963 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001964 case Hexagon::L4_return :
1965 case Hexagon::L4_return_t :
1966 case Hexagon::L4_return_f :
1967 case Hexagon::L4_return_fnew_pnt :
1968 case Hexagon::L4_return_fnew_pt :
1969 case Hexagon::L4_return_tnew_pnt :
1970 case Hexagon::L4_return_tnew_pt :
1971 return true;
1972 }
1973 return false;
1974}
1975
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001976bool HexagonInstrInfo::isJumpR(const MachineInstr &MI) const {
1977 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001978 case Hexagon::J2_jumpr :
1979 case Hexagon::J2_jumprt :
1980 case Hexagon::J2_jumprf :
1981 case Hexagon::J2_jumprtnewpt :
1982 case Hexagon::J2_jumprfnewpt :
1983 case Hexagon::J2_jumprtnew :
1984 case Hexagon::J2_jumprfnew :
1985 return true;
1986 }
1987 return false;
1988}
1989
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00001990// Return true if a given MI can accommodate given offset.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001991// Use abs estimate as oppose to the exact number.
1992// TODO: This will need to be changed to use MC level
1993// definition of instruction extendable field size.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001994bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001995 unsigned offset) const {
1996 // This selection of jump instructions matches to that what
Krzysztof Parzyszek700a5f92017-05-03 15:34:52 +00001997 // analyzeBranch can parse, plus NVJ.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001998 if (isNewValueJump(MI)) // r9:2
1999 return isInt<11>(offset);
2000
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002001 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002002 // Still missing Jump to address condition on register value.
2003 default:
2004 return false;
2005 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2006 case Hexagon::J2_call:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002007 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002008 return isInt<24>(offset);
2009 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2010 case Hexagon::J2_jumpf:
2011 case Hexagon::J2_jumptnew:
2012 case Hexagon::J2_jumptnewpt:
2013 case Hexagon::J2_jumpfnew:
2014 case Hexagon::J2_jumpfnewpt:
2015 case Hexagon::J2_callt:
2016 case Hexagon::J2_callf:
2017 return isInt<17>(offset);
2018 case Hexagon::J2_loop0i:
2019 case Hexagon::J2_loop0iext:
2020 case Hexagon::J2_loop0r:
2021 case Hexagon::J2_loop0rext:
2022 case Hexagon::J2_loop1i:
2023 case Hexagon::J2_loop1iext:
2024 case Hexagon::J2_loop1r:
2025 case Hexagon::J2_loop1rext:
2026 return isInt<9>(offset);
2027 // TODO: Add all the compound branches here. Can we do this in Relation model?
2028 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2029 case Hexagon::J4_cmpeqi_tp1_jump_nt:
2030 return isInt<11>(offset);
2031 }
2032}
2033
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002034bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr &LRMI,
2035 const MachineInstr &ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002036 bool isLate = isLateResultInstr(LRMI);
2037 bool isEarly = isEarlySourceInstr(ESMI);
2038
2039 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002040 DEBUG(LRMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002041 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002042 DEBUG(ESMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002043
2044 if (isLate && isEarly) {
2045 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2046 return true;
2047 }
2048
2049 return false;
2050}
2051
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002052bool HexagonInstrInfo::isLateResultInstr(const MachineInstr &MI) const {
2053 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002054 case TargetOpcode::EXTRACT_SUBREG:
2055 case TargetOpcode::INSERT_SUBREG:
2056 case TargetOpcode::SUBREG_TO_REG:
2057 case TargetOpcode::REG_SEQUENCE:
2058 case TargetOpcode::IMPLICIT_DEF:
2059 case TargetOpcode::COPY:
2060 case TargetOpcode::INLINEASM:
2061 case TargetOpcode::PHI:
2062 return false;
2063 default:
2064 break;
2065 }
2066
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002067 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002068 return !is_TC1(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002069}
2070
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002071bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002072 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2073 // resource, but all operands can be received late like an ALU instruction.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002074 return getType(MI) == HexagonII::TypeCVI_VX_LATE;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002075}
2076
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002077bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2078 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002079 return Opcode == Hexagon::J2_loop0i ||
2080 Opcode == Hexagon::J2_loop0r ||
2081 Opcode == Hexagon::J2_loop0iext ||
2082 Opcode == Hexagon::J2_loop0rext ||
2083 Opcode == Hexagon::J2_loop1i ||
2084 Opcode == Hexagon::J2_loop1r ||
2085 Opcode == Hexagon::J2_loop1iext ||
2086 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002087}
2088
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002089bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2090 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002091 default: return false;
2092 case Hexagon::L4_iadd_memopw_io :
2093 case Hexagon::L4_isub_memopw_io :
2094 case Hexagon::L4_add_memopw_io :
2095 case Hexagon::L4_sub_memopw_io :
2096 case Hexagon::L4_and_memopw_io :
2097 case Hexagon::L4_or_memopw_io :
2098 case Hexagon::L4_iadd_memoph_io :
2099 case Hexagon::L4_isub_memoph_io :
2100 case Hexagon::L4_add_memoph_io :
2101 case Hexagon::L4_sub_memoph_io :
2102 case Hexagon::L4_and_memoph_io :
2103 case Hexagon::L4_or_memoph_io :
2104 case Hexagon::L4_iadd_memopb_io :
2105 case Hexagon::L4_isub_memopb_io :
2106 case Hexagon::L4_add_memopb_io :
2107 case Hexagon::L4_sub_memopb_io :
2108 case Hexagon::L4_and_memopb_io :
2109 case Hexagon::L4_or_memopb_io :
2110 case Hexagon::L4_ior_memopb_io:
2111 case Hexagon::L4_ior_memoph_io:
2112 case Hexagon::L4_ior_memopw_io:
2113 case Hexagon::L4_iand_memopb_io:
2114 case Hexagon::L4_iand_memoph_io:
2115 case Hexagon::L4_iand_memopw_io:
2116 return true;
2117 }
2118 return false;
2119}
2120
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002121bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2122 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002123 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2124}
2125
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002126bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2127 const uint64_t F = get(Opcode).TSFlags;
2128 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2129}
2130
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002131bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002132 return isNewValueJump(MI) || isNewValueStore(MI);
2133}
2134
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002135bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2136 return isNewValue(MI) && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002137}
2138
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002139bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2140 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2141}
2142
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002143bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2144 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002145 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2146}
2147
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002148bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2149 const uint64_t F = get(Opcode).TSFlags;
2150 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2151}
2152
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002153// Returns true if a particular operand is extendable for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002154bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002155 unsigned OperandNum) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002156 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002157 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2158 == OperandNum;
2159}
2160
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002161bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2162 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002163 assert(isPredicated(MI));
2164 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2165}
2166
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002167bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2168 const uint64_t F = get(Opcode).TSFlags;
2169 assert(isPredicated(Opcode));
2170 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2171}
2172
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002173bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2174 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002175 return !((F >> HexagonII::PredicatedFalsePos) &
2176 HexagonII::PredicatedFalseMask);
2177}
2178
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002179bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2180 const uint64_t F = get(Opcode).TSFlags;
2181 // Make sure that the instruction is predicated.
2182 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2183 return !((F >> HexagonII::PredicatedFalsePos) &
2184 HexagonII::PredicatedFalseMask);
2185}
2186
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002187bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2188 const uint64_t F = get(Opcode).TSFlags;
2189 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2190}
2191
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002192bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2193 const uint64_t F = get(Opcode).TSFlags;
2194 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2195}
2196
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002197bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2198 const uint64_t F = get(Opcode).TSFlags;
2199 assert(get(Opcode).isBranch() &&
2200 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2201 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2202}
2203
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002204bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2205 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2206 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2207 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2208 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002209}
2210
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002211bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2212 switch (MI.getOpcode()) {
2213 // Byte
2214 case Hexagon::L2_loadrb_io:
2215 case Hexagon::L4_loadrb_ur:
2216 case Hexagon::L4_loadrb_ap:
2217 case Hexagon::L2_loadrb_pr:
2218 case Hexagon::L2_loadrb_pbr:
2219 case Hexagon::L2_loadrb_pi:
2220 case Hexagon::L2_loadrb_pci:
2221 case Hexagon::L2_loadrb_pcr:
2222 case Hexagon::L2_loadbsw2_io:
2223 case Hexagon::L4_loadbsw2_ur:
2224 case Hexagon::L4_loadbsw2_ap:
2225 case Hexagon::L2_loadbsw2_pr:
2226 case Hexagon::L2_loadbsw2_pbr:
2227 case Hexagon::L2_loadbsw2_pi:
2228 case Hexagon::L2_loadbsw2_pci:
2229 case Hexagon::L2_loadbsw2_pcr:
2230 case Hexagon::L2_loadbsw4_io:
2231 case Hexagon::L4_loadbsw4_ur:
2232 case Hexagon::L4_loadbsw4_ap:
2233 case Hexagon::L2_loadbsw4_pr:
2234 case Hexagon::L2_loadbsw4_pbr:
2235 case Hexagon::L2_loadbsw4_pi:
2236 case Hexagon::L2_loadbsw4_pci:
2237 case Hexagon::L2_loadbsw4_pcr:
2238 case Hexagon::L4_loadrb_rr:
2239 case Hexagon::L2_ploadrbt_io:
2240 case Hexagon::L2_ploadrbt_pi:
2241 case Hexagon::L2_ploadrbf_io:
2242 case Hexagon::L2_ploadrbf_pi:
2243 case Hexagon::L2_ploadrbtnew_io:
2244 case Hexagon::L2_ploadrbfnew_io:
2245 case Hexagon::L4_ploadrbt_rr:
2246 case Hexagon::L4_ploadrbf_rr:
2247 case Hexagon::L4_ploadrbtnew_rr:
2248 case Hexagon::L4_ploadrbfnew_rr:
2249 case Hexagon::L2_ploadrbtnew_pi:
2250 case Hexagon::L2_ploadrbfnew_pi:
2251 case Hexagon::L4_ploadrbt_abs:
2252 case Hexagon::L4_ploadrbf_abs:
2253 case Hexagon::L4_ploadrbtnew_abs:
2254 case Hexagon::L4_ploadrbfnew_abs:
2255 case Hexagon::L2_loadrbgp:
2256 // Half
2257 case Hexagon::L2_loadrh_io:
2258 case Hexagon::L4_loadrh_ur:
2259 case Hexagon::L4_loadrh_ap:
2260 case Hexagon::L2_loadrh_pr:
2261 case Hexagon::L2_loadrh_pbr:
2262 case Hexagon::L2_loadrh_pi:
2263 case Hexagon::L2_loadrh_pci:
2264 case Hexagon::L2_loadrh_pcr:
2265 case Hexagon::L4_loadrh_rr:
2266 case Hexagon::L2_ploadrht_io:
2267 case Hexagon::L2_ploadrht_pi:
2268 case Hexagon::L2_ploadrhf_io:
2269 case Hexagon::L2_ploadrhf_pi:
2270 case Hexagon::L2_ploadrhtnew_io:
2271 case Hexagon::L2_ploadrhfnew_io:
2272 case Hexagon::L4_ploadrht_rr:
2273 case Hexagon::L4_ploadrhf_rr:
2274 case Hexagon::L4_ploadrhtnew_rr:
2275 case Hexagon::L4_ploadrhfnew_rr:
2276 case Hexagon::L2_ploadrhtnew_pi:
2277 case Hexagon::L2_ploadrhfnew_pi:
2278 case Hexagon::L4_ploadrht_abs:
2279 case Hexagon::L4_ploadrhf_abs:
2280 case Hexagon::L4_ploadrhtnew_abs:
2281 case Hexagon::L4_ploadrhfnew_abs:
2282 case Hexagon::L2_loadrhgp:
2283 return true;
2284 default:
2285 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002286 }
2287}
2288
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002289bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2290 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002291 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2292}
2293
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002294bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2295 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002296 case Hexagon::STriw_pred :
2297 case Hexagon::LDriw_pred :
2298 return true;
2299 default:
2300 return false;
2301 }
2302}
2303
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002304bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2305 if (!MI.isBranch())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002306 return false;
2307
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002308 for (auto &Op : MI.operands())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002309 if (Op.isGlobal() || Op.isSymbol())
2310 return true;
2311 return false;
2312}
2313
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002314// Returns true when SU has a timing class TC1.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002315bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2316 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002317 return is_TC1(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002318}
2319
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002320bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2321 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002322 return is_TC2(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002323}
2324
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002325bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2326 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002327 return is_TC2early(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002328}
2329
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002330bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2331 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002332 return is_TC4x(SchedClass);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002333}
2334
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002335// Schedule this ASAP.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002336bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2337 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002338 if (mayBeCurLoad(MI1)) {
2339 // if (result of SU is used in Next) return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002340 unsigned DstReg = MI1.getOperand(0).getReg();
2341 int N = MI2.getNumOperands();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002342 for (int I = 0; I < N; I++)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002343 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002344 return true;
2345 }
2346 if (mayBeNewStore(MI2))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002347 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2348 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2349 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002350 return true;
2351 return false;
2352}
2353
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002354bool HexagonInstrInfo::isHVXVec(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002355 const uint64_t V = getType(MI);
2356 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2357}
2358
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002359// Check if the Offset is a valid auto-inc imm by Load/Store Type.
2360//
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002361bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, int Offset) const {
2362 int Size = VT.getSizeInBits() / 8;
2363 if (Offset % Size != 0)
2364 return false;
2365 int Count = Offset / Size;
2366
2367 switch (VT.getSimpleVT().SimpleTy) {
2368 // For scalars the auto-inc is s4
2369 case MVT::i8:
2370 case MVT::i16:
2371 case MVT::i32:
2372 case MVT::i64:
2373 return isInt<4>(Count);
2374 // For HVX vectors the auto-inc is s3
2375 case MVT::v64i8:
2376 case MVT::v32i16:
2377 case MVT::v16i32:
2378 case MVT::v8i64:
2379 case MVT::v128i8:
2380 case MVT::v64i16:
2381 case MVT::v32i32:
2382 case MVT::v16i64:
2383 return isInt<3>(Count);
2384 default:
2385 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002386 }
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002387
2388 llvm_unreachable("Not an valid type!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002389}
2390
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002391bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002392 const TargetRegisterInfo *TRI, bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002393 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002394 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002395 // inserted to calculate the final address. Due to this reason, the function
2396 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002397 // We used to assert if the offset was not properly aligned, however,
2398 // there are cases where a misaligned pointer recast can cause this
2399 // problem, and we need to allow for it. The front end warns of such
2400 // misaligns with respect to load size.
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002401 switch (Opcode) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002402 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002403 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002404 case Hexagon::PS_vstorerw_nt_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002405 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002406 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002407 case Hexagon::PS_vloadrw_nt_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002408 case Hexagon::V6_vL32b_ai:
2409 case Hexagon::V6_vS32b_ai:
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00002410 case Hexagon::V6_vL32b_nt_ai:
2411 case Hexagon::V6_vS32b_nt_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002412 case Hexagon::V6_vL32Ub_ai:
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00002413 case Hexagon::V6_vS32Ub_ai: {
2414 unsigned VectorSize = TRI->getSpillSize(Hexagon::HvxVRRegClass);
2415 assert(isPowerOf2_32(VectorSize));
2416 if (Offset & (VectorSize-1))
2417 return false;
2418 return isInt<4>(Offset >> Log2_32(VectorSize));
2419 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002420
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002421 case Hexagon::J2_loop0i:
2422 case Hexagon::J2_loop1i:
2423 return isUInt<10>(Offset);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00002424
2425 case Hexagon::S4_storeirb_io:
2426 case Hexagon::S4_storeirbt_io:
2427 case Hexagon::S4_storeirbf_io:
2428 return isUInt<6>(Offset);
2429
2430 case Hexagon::S4_storeirh_io:
2431 case Hexagon::S4_storeirht_io:
2432 case Hexagon::S4_storeirhf_io:
2433 return isShiftedUInt<6,1>(Offset);
2434
2435 case Hexagon::S4_storeiri_io:
2436 case Hexagon::S4_storeirit_io:
2437 case Hexagon::S4_storeirif_io:
2438 return isShiftedUInt<6,2>(Offset);
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002439 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002440
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002441 if (Extend)
2442 return true;
2443
2444 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002445 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002446 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002447 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2448 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2449
Colin LeMahieu947cd702014-12-23 20:44:59 +00002450 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002451 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002452 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2453 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2454
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002455 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002456 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002457 case Hexagon::S2_storerh_io:
Krzysztof Parzyszekd10df492017-05-03 15:36:51 +00002458 case Hexagon::S2_storerf_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002459 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2460 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2461
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002462 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002463 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002464 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002465 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2466 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2467
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002468 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002469 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2470 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2471
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002472 case Hexagon::L4_iadd_memopw_io :
2473 case Hexagon::L4_isub_memopw_io :
2474 case Hexagon::L4_add_memopw_io :
2475 case Hexagon::L4_sub_memopw_io :
2476 case Hexagon::L4_and_memopw_io :
2477 case Hexagon::L4_or_memopw_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002478 return (0 <= Offset && Offset <= 255);
2479
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002480 case Hexagon::L4_iadd_memoph_io :
2481 case Hexagon::L4_isub_memoph_io :
2482 case Hexagon::L4_add_memoph_io :
2483 case Hexagon::L4_sub_memoph_io :
2484 case Hexagon::L4_and_memoph_io :
2485 case Hexagon::L4_or_memoph_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002486 return (0 <= Offset && Offset <= 127);
2487
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002488 case Hexagon::L4_iadd_memopb_io :
2489 case Hexagon::L4_isub_memopb_io :
2490 case Hexagon::L4_add_memopb_io :
2491 case Hexagon::L4_sub_memopb_io :
2492 case Hexagon::L4_and_memopb_io :
2493 case Hexagon::L4_or_memopb_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002494 return (0 <= Offset && Offset <= 63);
2495
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002496 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002497 // any size. Later pass knows how to handle it.
2498 case Hexagon::STriw_pred:
2499 case Hexagon::LDriw_pred:
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00002500 case Hexagon::STriw_mod:
2501 case Hexagon::LDriw_mod:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002502 return true;
2503
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002504 case Hexagon::PS_fi:
2505 case Hexagon::PS_fia:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002506 case Hexagon::INLINEASM:
2507 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002508
2509 case Hexagon::L2_ploadrbt_io:
2510 case Hexagon::L2_ploadrbf_io:
2511 case Hexagon::L2_ploadrubt_io:
2512 case Hexagon::L2_ploadrubf_io:
2513 case Hexagon::S2_pstorerbt_io:
2514 case Hexagon::S2_pstorerbf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002515 return isUInt<6>(Offset);
2516
2517 case Hexagon::L2_ploadrht_io:
2518 case Hexagon::L2_ploadrhf_io:
2519 case Hexagon::L2_ploadruht_io:
2520 case Hexagon::L2_ploadruhf_io:
2521 case Hexagon::S2_pstorerht_io:
2522 case Hexagon::S2_pstorerhf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002523 return isShiftedUInt<6,1>(Offset);
2524
2525 case Hexagon::L2_ploadrit_io:
2526 case Hexagon::L2_ploadrif_io:
2527 case Hexagon::S2_pstorerit_io:
2528 case Hexagon::S2_pstorerif_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002529 return isShiftedUInt<6,2>(Offset);
2530
2531 case Hexagon::L2_ploadrdt_io:
2532 case Hexagon::L2_ploadrdf_io:
2533 case Hexagon::S2_pstorerdt_io:
2534 case Hexagon::S2_pstorerdf_io:
2535 return isShiftedUInt<6,3>(Offset);
2536 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002537
Benjamin Kramerb6684012011-12-27 11:41:05 +00002538 llvm_unreachable("No offset range is defined for this opcode. "
2539 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002540}
2541
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002542bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002543 return isHVXVec(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002544}
2545
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002546bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2547 const uint64_t F = get(MI.getOpcode()).TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002548 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2549 return
2550 V == HexagonII::TypeCVI_VA ||
2551 V == HexagonII::TypeCVI_VA_DV;
2552}
Andrew Trickd06df962012-02-01 22:13:57 +00002553
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002554bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2555 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002556 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2557 return true;
2558
2559 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2560 return true;
2561
2562 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002563 return true;
2564
2565 return false;
2566}
Jyotsna Verma84256432013-03-01 17:37:13 +00002567
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002568bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2569 switch (MI.getOpcode()) {
2570 // Byte
2571 case Hexagon::L2_loadrub_io:
2572 case Hexagon::L4_loadrub_ur:
2573 case Hexagon::L4_loadrub_ap:
2574 case Hexagon::L2_loadrub_pr:
2575 case Hexagon::L2_loadrub_pbr:
2576 case Hexagon::L2_loadrub_pi:
2577 case Hexagon::L2_loadrub_pci:
2578 case Hexagon::L2_loadrub_pcr:
2579 case Hexagon::L2_loadbzw2_io:
2580 case Hexagon::L4_loadbzw2_ur:
2581 case Hexagon::L4_loadbzw2_ap:
2582 case Hexagon::L2_loadbzw2_pr:
2583 case Hexagon::L2_loadbzw2_pbr:
2584 case Hexagon::L2_loadbzw2_pi:
2585 case Hexagon::L2_loadbzw2_pci:
2586 case Hexagon::L2_loadbzw2_pcr:
2587 case Hexagon::L2_loadbzw4_io:
2588 case Hexagon::L4_loadbzw4_ur:
2589 case Hexagon::L4_loadbzw4_ap:
2590 case Hexagon::L2_loadbzw4_pr:
2591 case Hexagon::L2_loadbzw4_pbr:
2592 case Hexagon::L2_loadbzw4_pi:
2593 case Hexagon::L2_loadbzw4_pci:
2594 case Hexagon::L2_loadbzw4_pcr:
2595 case Hexagon::L4_loadrub_rr:
2596 case Hexagon::L2_ploadrubt_io:
2597 case Hexagon::L2_ploadrubt_pi:
2598 case Hexagon::L2_ploadrubf_io:
2599 case Hexagon::L2_ploadrubf_pi:
2600 case Hexagon::L2_ploadrubtnew_io:
2601 case Hexagon::L2_ploadrubfnew_io:
2602 case Hexagon::L4_ploadrubt_rr:
2603 case Hexagon::L4_ploadrubf_rr:
2604 case Hexagon::L4_ploadrubtnew_rr:
2605 case Hexagon::L4_ploadrubfnew_rr:
2606 case Hexagon::L2_ploadrubtnew_pi:
2607 case Hexagon::L2_ploadrubfnew_pi:
2608 case Hexagon::L4_ploadrubt_abs:
2609 case Hexagon::L4_ploadrubf_abs:
2610 case Hexagon::L4_ploadrubtnew_abs:
2611 case Hexagon::L4_ploadrubfnew_abs:
2612 case Hexagon::L2_loadrubgp:
2613 // Half
2614 case Hexagon::L2_loadruh_io:
2615 case Hexagon::L4_loadruh_ur:
2616 case Hexagon::L4_loadruh_ap:
2617 case Hexagon::L2_loadruh_pr:
2618 case Hexagon::L2_loadruh_pbr:
2619 case Hexagon::L2_loadruh_pi:
2620 case Hexagon::L2_loadruh_pci:
2621 case Hexagon::L2_loadruh_pcr:
2622 case Hexagon::L4_loadruh_rr:
2623 case Hexagon::L2_ploadruht_io:
2624 case Hexagon::L2_ploadruht_pi:
2625 case Hexagon::L2_ploadruhf_io:
2626 case Hexagon::L2_ploadruhf_pi:
2627 case Hexagon::L2_ploadruhtnew_io:
2628 case Hexagon::L2_ploadruhfnew_io:
2629 case Hexagon::L4_ploadruht_rr:
2630 case Hexagon::L4_ploadruhf_rr:
2631 case Hexagon::L4_ploadruhtnew_rr:
2632 case Hexagon::L4_ploadruhfnew_rr:
2633 case Hexagon::L2_ploadruhtnew_pi:
2634 case Hexagon::L2_ploadruhfnew_pi:
2635 case Hexagon::L4_ploadruht_abs:
2636 case Hexagon::L4_ploadruhf_abs:
2637 case Hexagon::L4_ploadruhtnew_abs:
2638 case Hexagon::L4_ploadruhfnew_abs:
2639 case Hexagon::L2_loadruhgp:
2640 return true;
2641 default:
2642 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002643 }
2644}
2645
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002646// Add latency to instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002647bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2648 const MachineInstr &MI2) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002649 if (isHVXVec(MI1) && isHVXVec(MI2))
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002650 if (!isVecUsableNextPacket(MI1, MI2))
2651 return true;
2652 return false;
2653}
2654
Brendon Cahoon254f8892016-07-29 16:44:44 +00002655/// \brief Get the base register and byte offset of a load/store instr.
2656bool HexagonInstrInfo::getMemOpBaseRegImmOfs(MachineInstr &LdSt,
2657 unsigned &BaseReg, int64_t &Offset, const TargetRegisterInfo *TRI)
2658 const {
2659 unsigned AccessSize = 0;
2660 int OffsetVal = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002661 BaseReg = getBaseAndOffset(LdSt, OffsetVal, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002662 Offset = OffsetVal;
2663 return BaseReg != 0;
2664}
2665
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002666/// \brief Can these instructions execute at the same time in a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002667bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2668 const MachineInstr &Second) const {
Krzysztof Parzyszek4763c2d2017-05-03 15:33:09 +00002669 if (Second.mayStore() && First.getOpcode() == Hexagon::S2_allocframe) {
2670 const MachineOperand &Op = Second.getOperand(0);
2671 if (Op.isReg() && Op.isUse() && Op.getReg() == Hexagon::R29)
2672 return true;
2673 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002674 if (DisableNVSchedule)
2675 return false;
2676 if (mayBeNewStore(Second)) {
2677 // Make sure the definition of the first instruction is the value being
2678 // stored.
2679 const MachineOperand &Stored =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002680 Second.getOperand(Second.getNumOperands() - 1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002681 if (!Stored.isReg())
2682 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002683 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
2684 const MachineOperand &Op = First.getOperand(i);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002685 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2686 return true;
2687 }
2688 }
2689 return false;
2690}
2691
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002692bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
2693 unsigned Opc = CallMI.getOpcode();
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002694 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002695}
2696
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002697bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2698 for (auto &I : *B)
2699 if (I.isEHLabel())
2700 return true;
2701 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002702}
2703
Jyotsna Verma84256432013-03-01 17:37:13 +00002704// Returns true if an instruction can be converted into a non-extended
2705// equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002706bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002707 short NonExtOpcode;
2708 // Check if the instruction has a register form that uses register in place
2709 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002710 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
Jyotsna Verma84256432013-03-01 17:37:13 +00002711 return true;
2712
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002713 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002714 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002715
2716 switch (getAddrMode(MI)) {
2717 case HexagonII::Absolute :
2718 // Load/store with absolute addressing mode can be converted into
2719 // base+offset mode.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002720 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002721 break;
2722 case HexagonII::BaseImmOffset :
2723 // Load/store with base+offset addressing mode can be converted into
2724 // base+register offset addressing mode. However left shift operand should
2725 // be set to 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002726 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002727 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002728 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002729 NonExtOpcode = Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002730 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002731 default:
2732 return false;
2733 }
2734 if (NonExtOpcode < 0)
2735 return false;
2736 return true;
2737 }
2738 return false;
2739}
2740
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002741bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
2742 return Hexagon::getRealHWInstr(MI.getOpcode(),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002743 Hexagon::InstrType_Pseudo) >= 0;
2744}
2745
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002746bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2747 const {
2748 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2749 while (I != E) {
2750 if (I->isBarrier())
2751 return true;
2752 ++I;
2753 }
2754 return false;
2755}
2756
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002757// Returns true, if a LD insn can be promoted to a cur load.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002758bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
2759 auto &HST = MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>();
2760 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002761 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
2762 HST.hasV60TOps();
2763}
2764
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002765// Returns true, if a ST insn can be promoted to a new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002766bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
2767 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002768 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
2769}
2770
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002771bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
2772 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002773 // There is no stall when ProdMI is not a V60 vector.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002774 if (!isHVXVec(ProdMI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002775 return false;
2776
2777 // There is no stall when ProdMI and ConsMI are not dependent.
2778 if (!isDependent(ProdMI, ConsMI))
2779 return false;
2780
2781 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
2782 // are scheduled in consecutive packets.
2783 if (isVecUsableNextPacket(ProdMI, ConsMI))
2784 return false;
2785
2786 return true;
2787}
2788
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002789bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002790 MachineBasicBlock::const_instr_iterator BII) const {
2791 // There is no stall when I is not a V60 vector.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00002792 if (!isHVXVec(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002793 return false;
2794
2795 MachineBasicBlock::const_instr_iterator MII = BII;
2796 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
2797
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00002798 if (!(*MII).isBundle()) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002799 const MachineInstr &J = *MII;
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00002800 return producesStall(J, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002801 }
2802
2803 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002804 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002805 if (producesStall(J, MI))
2806 return true;
2807 }
2808 return false;
2809}
2810
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002811bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002812 unsigned PredReg) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00002813 for (const MachineOperand &MO : MI.operands()) {
2814 // Predicate register must be explicitly defined.
2815 if (MO.isRegMask() && MO.clobbersPhysReg(PredReg))
2816 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002817 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00002818 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002819 }
2820
2821 // Hexagon Programmer's Reference says that decbin, memw_locked, and
2822 // memd_locked cannot be used as .new as well,
2823 // but we don't seem to have these instructions defined.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002824 return MI.getOpcode() != Hexagon::A4_tlbmatch;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002825}
2826
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002827bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00002828 return Opcode == Hexagon::J2_jumpt ||
2829 Opcode == Hexagon::J2_jumptpt ||
2830 Opcode == Hexagon::J2_jumpf ||
2831 Opcode == Hexagon::J2_jumpfpt ||
2832 Opcode == Hexagon::J2_jumptnew ||
2833 Opcode == Hexagon::J2_jumpfnew ||
2834 Opcode == Hexagon::J2_jumptnewpt ||
2835 Opcode == Hexagon::J2_jumpfnewpt;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002836}
2837
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002838bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
2839 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
2840 return false;
2841 return !isPredicatedTrue(Cond[0].getImm());
2842}
2843
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002844short HexagonInstrInfo::getAbsoluteForm(const MachineInstr &MI) const {
2845 return Hexagon::getAbsoluteForm(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00002846}
2847
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002848unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
2849 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002850 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
2851}
2852
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002853// Returns the base register in a memory access (load/store). The offset is
2854// returned in Offset and the access size is returned in AccessSize.
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00002855// If the base register has a subregister or the offset field does not contain
2856// an immediate value, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002857unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002858 int &Offset, unsigned &AccessSize) const {
2859 // Return if it is not a base+offset type instruction or a MemOp.
2860 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
2861 getAddrMode(MI) != HexagonII::BaseLongOffset &&
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002862 !isMemOp(MI) && !isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002863 return 0;
2864
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00002865 AccessSize = getMemAccessSize(MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002866
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00002867 unsigned BasePos = 0, OffsetPos = 0;
2868 if (!getBaseAndOffsetPosition(MI, BasePos, OffsetPos))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002869 return 0;
2870
2871 // Post increment updates its EA after the mem access,
2872 // so we need to treat its offset as zero.
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00002873 if (isPostIncrement(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002874 Offset = 0;
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00002875 } else {
2876 const MachineOperand &OffsetOp = MI.getOperand(OffsetPos);
2877 if (!OffsetOp.isImm())
2878 return 0;
2879 Offset = OffsetOp.getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002880 }
2881
Krzysztof Parzyszekb449dc12017-07-19 15:39:28 +00002882 const MachineOperand &BaseOp = MI.getOperand(BasePos);
2883 if (BaseOp.getSubReg() != 0)
2884 return 0;
2885 return BaseOp.getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002886}
2887
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002888/// Return the position of the base and offset operands for this instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002889bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002890 unsigned &BasePos, unsigned &OffsetPos) const {
2891 // Deal with memops first.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002892 if (isMemOp(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002893 BasePos = 0;
2894 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002895 } else if (MI.mayStore()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002896 BasePos = 0;
2897 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002898 } else if (MI.mayLoad()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002899 BasePos = 1;
2900 OffsetPos = 2;
2901 } else
2902 return false;
2903
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002904 if (isPredicated(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002905 BasePos++;
2906 OffsetPos++;
2907 }
2908 if (isPostIncrement(MI)) {
2909 BasePos++;
2910 OffsetPos++;
2911 }
2912
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002913 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002914 return false;
2915
2916 return true;
2917}
2918
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00002919// Inserts branching instructions in reverse order of their occurrence.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002920// e.g. jump_t t1 (i1)
2921// jump t2 (i2)
2922// Jumpers = {i2, i1}
2923SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
2924 MachineBasicBlock& MBB) const {
2925 SmallVector<MachineInstr*, 2> Jumpers;
2926 // If the block has no terminators, it just falls into the block after it.
2927 MachineBasicBlock::instr_iterator I = MBB.instr_end();
2928 if (I == MBB.instr_begin())
2929 return Jumpers;
2930
2931 // A basic block may looks like this:
2932 //
2933 // [ insn
2934 // EH_LABEL
2935 // insn
2936 // insn
2937 // insn
2938 // EH_LABEL
2939 // insn ]
2940 //
2941 // It has two succs but does not have a terminator
2942 // Don't know how to handle it.
2943 do {
2944 --I;
2945 if (I->isEHLabel())
2946 return Jumpers;
2947 } while (I != MBB.instr_begin());
2948
2949 I = MBB.instr_end();
2950 --I;
2951
2952 while (I->isDebugValue()) {
2953 if (I == MBB.instr_begin())
2954 return Jumpers;
2955 --I;
2956 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002957 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002958 return Jumpers;
2959
2960 // Get the last instruction in the block.
2961 MachineInstr *LastInst = &*I;
2962 Jumpers.push_back(LastInst);
2963 MachineInstr *SecondLastInst = nullptr;
2964 // Find one more terminator if present.
2965 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002966 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002967 if (!SecondLastInst) {
2968 SecondLastInst = &*I;
2969 Jumpers.push_back(SecondLastInst);
2970 } else // This is a third branch.
2971 return Jumpers;
2972 }
2973 if (I == MBB.instr_begin())
2974 break;
2975 --I;
2976 } while (true);
2977 return Jumpers;
2978}
2979
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00002980short HexagonInstrInfo::getBaseWithLongOffset(short Opcode) const {
2981 if (Opcode < 0)
2982 return -1;
2983 return Hexagon::getBaseWithLongOffset(Opcode);
2984}
2985
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002986short HexagonInstrInfo::getBaseWithLongOffset(const MachineInstr &MI) const {
2987 return Hexagon::getBaseWithLongOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00002988}
2989
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002990short HexagonInstrInfo::getBaseWithRegOffset(const MachineInstr &MI) const {
2991 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00002992}
2993
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002994// Returns Operand Index for the constant extended instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002995unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
2996 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002997 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
2998}
2999
3000// See if instruction could potentially be a duplex candidate.
3001// If so, return its group. Zero otherwise.
3002HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003003 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003004 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3005
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003006 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003007 default:
3008 return HexagonII::HCG_None;
3009 //
3010 // Compound pairs.
3011 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3012 // "Rd16=#U6 ; jump #r9:2"
3013 // "Rd16=Rs16 ; jump #r9:2"
3014 //
3015 case Hexagon::C2_cmpeq:
3016 case Hexagon::C2_cmpgt:
3017 case Hexagon::C2_cmpgtu:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003018 DstReg = MI.getOperand(0).getReg();
3019 Src1Reg = MI.getOperand(1).getReg();
3020 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003021 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3022 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3023 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3024 return HexagonII::HCG_A;
3025 break;
3026 case Hexagon::C2_cmpeqi:
3027 case Hexagon::C2_cmpgti:
3028 case Hexagon::C2_cmpgtui:
3029 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003030 DstReg = MI.getOperand(0).getReg();
3031 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003032 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3033 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003034 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3035 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3036 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003037 return HexagonII::HCG_A;
3038 break;
3039 case Hexagon::A2_tfr:
3040 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003041 DstReg = MI.getOperand(0).getReg();
3042 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003043 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3044 return HexagonII::HCG_A;
3045 break;
3046 case Hexagon::A2_tfrsi:
3047 // Rd = #u6
3048 // Do not test for #u6 size since the const is getting extended
3049 // regardless and compound could be formed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003050 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003051 if (isIntRegForSubInst(DstReg))
3052 return HexagonII::HCG_A;
3053 break;
3054 case Hexagon::S2_tstbit_i:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003055 DstReg = MI.getOperand(0).getReg();
3056 Src1Reg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003057 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3058 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003059 MI.getOperand(2).isImm() &&
3060 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003061 return HexagonII::HCG_A;
3062 break;
3063 // The fact that .new form is used pretty much guarantees
3064 // that predicate register will match. Nevertheless,
3065 // there could be some false positives without additional
3066 // checking.
3067 case Hexagon::J2_jumptnew:
3068 case Hexagon::J2_jumpfnew:
3069 case Hexagon::J2_jumptnewpt:
3070 case Hexagon::J2_jumpfnewpt:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003071 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003072 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3073 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3074 return HexagonII::HCG_B;
3075 break;
3076 // Transfer and jump:
3077 // Rd=#U6 ; jump #r9:2
3078 // Rd=Rs ; jump #r9:2
3079 // Do not test for jump range here.
3080 case Hexagon::J2_jump:
3081 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003082 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003083 return HexagonII::HCG_C;
3084 break;
3085 }
3086
3087 return HexagonII::HCG_None;
3088}
3089
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003090// Returns -1 when there is no opcode found.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003091unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3092 const MachineInstr &GB) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003093 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3094 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003095 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3096 (GB.getOpcode() != Hexagon::J2_jumptnew))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003097 return -1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003098 unsigned DestReg = GA.getOperand(0).getReg();
3099 if (!GB.readsRegister(DestReg))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003100 return -1;
3101 if (DestReg == Hexagon::P0)
3102 return Hexagon::J4_cmpeqi_tp0_jump_nt;
3103 if (DestReg == Hexagon::P1)
3104 return Hexagon::J4_cmpeqi_tp1_jump_nt;
3105 return -1;
3106}
3107
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003108int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3109 enum Hexagon::PredSense inPredSense;
3110 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3111 Hexagon::PredSense_true;
3112 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3113 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3114 return CondOpcode;
3115
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003116 llvm_unreachable("Unexpected predicable instruction");
3117}
3118
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003119// Return the cur value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003120int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3121 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003122 default: llvm_unreachable("Unknown .cur type");
3123 case Hexagon::V6_vL32b_pi:
3124 return Hexagon::V6_vL32b_cur_pi;
3125 case Hexagon::V6_vL32b_ai:
3126 return Hexagon::V6_vL32b_cur_ai;
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00003127 case Hexagon::V6_vL32b_nt_pi:
3128 return Hexagon::V6_vL32b_nt_cur_pi;
3129 case Hexagon::V6_vL32b_nt_ai:
3130 return Hexagon::V6_vL32b_nt_cur_ai;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003131 }
3132 return 0;
3133}
3134
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003135// Return the regular version of the .cur instruction.
3136int HexagonInstrInfo::getNonDotCurOp(const MachineInstr &MI) const {
3137 switch (MI.getOpcode()) {
3138 default: llvm_unreachable("Unknown .cur type");
3139 case Hexagon::V6_vL32b_cur_pi:
3140 return Hexagon::V6_vL32b_pi;
3141 case Hexagon::V6_vL32b_cur_ai:
3142 return Hexagon::V6_vL32b_ai;
Krzysztof Parzyszekc86e2ef2017-07-11 16:39:33 +00003143 case Hexagon::V6_vL32b_nt_cur_pi:
3144 return Hexagon::V6_vL32b_nt_pi;
3145 case Hexagon::V6_vL32b_nt_cur_ai:
3146 return Hexagon::V6_vL32b_nt_ai;
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003147 }
3148 return 0;
3149}
3150
3151
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003152// The diagram below shows the steps involved in the conversion of a predicated
3153// store instruction to its .new predicated new-value form.
3154//
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +00003155// Note: It doesn't include conditional new-value stores as they can't be
3156// converted to .new predicate.
3157//
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003158// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3159// ^ ^
3160// / \ (not OK. it will cause new-value store to be
3161// / X conditional on p0.new while R2 producer is
3162// / \ on p0)
3163// / \.
3164// p.new store p.old NV store
3165// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3166// ^ ^
3167// \ /
3168// \ /
3169// \ /
3170// p.old store
3171// [if (p0)memw(R0+#0)=R2]
3172//
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003173// The following set of instructions further explains the scenario where
3174// conditional new-value store becomes invalid when promoted to .new predicate
3175// form.
3176//
3177// { 1) if (p0) r0 = add(r1, r2)
3178// 2) p0 = cmp.eq(r3, #0) }
3179//
3180// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3181// the first two instructions because in instr 1, r0 is conditional on old value
3182// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3183// is not valid for new-value stores.
3184// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3185// from the "Conditional Store" list. Because a predicated new value store
3186// would NOT be promoted to a double dot new store. See diagram below:
3187// This function returns yes for those stores that are predicated but not
3188// yet promoted to predicate dot new instructions.
3189//
3190// +---------------------+
3191// /-----| if (p0) memw(..)=r0 |---------\~
3192// || +---------------------+ ||
3193// promote || /\ /\ || promote
3194// || /||\ /||\ ||
3195// \||/ demote || \||/
3196// \/ || || \/
3197// +-------------------------+ || +-------------------------+
3198// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3199// +-------------------------+ || +-------------------------+
3200// || || ||
3201// || demote \||/
3202// promote || \/ NOT possible
3203// || || /\~
3204// \||/ || /||\~
3205// \/ || ||
3206// +-----------------------------+
3207// | if (p0.new) memw(..)=r0.new |
3208// +-----------------------------+
3209// Double Dot New Store
3210//
3211// Returns the most basic instruction for the .new predicated instructions and
3212// new-value stores.
3213// For example, all of the following instructions will be converted back to the
3214// same instruction:
3215// 1) if (p0.new) memw(R0+#0) = R1.new --->
3216// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3217// 3) if (p0.new) memw(R0+#0) = R1 --->
3218//
3219// To understand the translation of instruction 1 to its original form, consider
3220// a packet with 3 instructions.
3221// { p0 = cmp.eq(R0,R1)
3222// if (p0.new) R2 = add(R3, R4)
3223// R5 = add (R3, R1)
3224// }
3225// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3226//
3227// This instruction can be part of the previous packet only if both p0 and R2
3228// are promoted to .new values. This promotion happens in steps, first
3229// predicate register is promoted to .new and in the next iteration R2 is
3230// promoted. Therefore, in case of dependence check failure (due to R5) during
3231// next iteration, it should be converted back to its most basic form.
3232
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003233// Return the new value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003234int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3235 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003236 if (NVOpcode >= 0) // Valid new-value store instruction.
3237 return NVOpcode;
3238
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003239 switch (MI.getOpcode()) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +00003240 default:
3241 llvm::report_fatal_error(std::string("Unknown .new type: ") +
3242 std::to_string(MI.getOpcode()).c_str());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003243 case Hexagon::S4_storerb_ur:
3244 return Hexagon::S4_storerbnew_ur;
3245
3246 case Hexagon::S2_storerb_pci:
3247 return Hexagon::S2_storerb_pci;
3248
3249 case Hexagon::S2_storeri_pci:
3250 return Hexagon::S2_storeri_pci;
3251
3252 case Hexagon::S2_storerh_pci:
3253 return Hexagon::S2_storerh_pci;
3254
3255 case Hexagon::S2_storerd_pci:
3256 return Hexagon::S2_storerd_pci;
3257
3258 case Hexagon::S2_storerf_pci:
3259 return Hexagon::S2_storerf_pci;
3260
3261 case Hexagon::V6_vS32b_ai:
3262 return Hexagon::V6_vS32b_new_ai;
3263
3264 case Hexagon::V6_vS32b_pi:
3265 return Hexagon::V6_vS32b_new_pi;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003266 }
3267 return 0;
3268}
3269
3270// Returns the opcode to use when converting MI, which is a conditional jump,
3271// into a conditional instruction which uses the .new value of the predicate.
3272// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003273// If MBPI is null, all edges will be treated as equally likely for the
3274// purposes of establishing a predication hint.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003275int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003276 const MachineBranchProbabilityInfo *MBPI) const {
3277 // We assume that block can have at most two successors.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003278 const MachineBasicBlock *Src = MI.getParent();
3279 const MachineOperand &BrTarget = MI.getOperand(1);
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003280 bool Taken = false;
3281 const BranchProbability OneHalf(1, 2);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003282
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003283 auto getEdgeProbability = [MBPI] (const MachineBasicBlock *Src,
3284 const MachineBasicBlock *Dst) {
3285 if (MBPI)
3286 return MBPI->getEdgeProbability(Src, Dst);
3287 return BranchProbability(1, Src->succ_size());
3288 };
3289
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003290 if (BrTarget.isMBB()) {
3291 const MachineBasicBlock *Dst = BrTarget.getMBB();
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003292 Taken = getEdgeProbability(Src, Dst) >= OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003293 } else {
3294 // The branch target is not a basic block (most likely a function).
3295 // Since BPI only gives probabilities for targets that are basic blocks,
3296 // try to identify another target of this branch (potentially a fall-
3297 // -through) and check the probability of that target.
3298 //
3299 // The only handled branch combinations are:
3300 // - one conditional branch,
3301 // - one conditional branch followed by one unconditional branch.
3302 // Otherwise, assume not-taken.
3303 assert(MI.isConditionalBranch());
3304 const MachineBasicBlock &B = *MI.getParent();
3305 bool SawCond = false, Bad = false;
3306 for (const MachineInstr &I : B) {
3307 if (!I.isBranch())
3308 continue;
3309 if (I.isConditionalBranch()) {
3310 SawCond = true;
3311 if (&I != &MI) {
3312 Bad = true;
3313 break;
3314 }
3315 }
3316 if (I.isUnconditionalBranch() && !SawCond) {
3317 Bad = true;
3318 break;
3319 }
3320 }
3321 if (!Bad) {
3322 MachineBasicBlock::const_instr_iterator It(MI);
3323 MachineBasicBlock::const_instr_iterator NextIt = std::next(It);
3324 if (NextIt == B.instr_end()) {
3325 // If this branch is the last, look for the fall-through block.
3326 for (const MachineBasicBlock *SB : B.successors()) {
3327 if (!B.isLayoutSuccessor(SB))
3328 continue;
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003329 Taken = getEdgeProbability(Src, SB) < OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003330 break;
3331 }
3332 } else {
3333 assert(NextIt->isUnconditionalBranch());
3334 // Find the first MBB operand and assume it's the target.
3335 const MachineBasicBlock *BT = nullptr;
3336 for (const MachineOperand &Op : NextIt->operands()) {
3337 if (!Op.isMBB())
3338 continue;
3339 BT = Op.getMBB();
3340 break;
3341 }
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +00003342 Taken = BT && getEdgeProbability(Src, BT) < OneHalf;
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003343 }
3344 } // if (!Bad)
3345 }
3346
3347 // The Taken flag should be set to something reasonable by this point.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003348
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003349 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003350 case Hexagon::J2_jumpt:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003351 return Taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003352 case Hexagon::J2_jumpf:
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003353 return Taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003354
3355 default:
3356 llvm_unreachable("Unexpected jump instruction.");
3357 }
3358}
3359
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003360// Return .new predicate version for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003361int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003362 const MachineBranchProbabilityInfo *MBPI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003363 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003364 // Condtional Jumps
3365 case Hexagon::J2_jumpt:
3366 case Hexagon::J2_jumpf:
3367 return getDotNewPredJumpOp(MI, MBPI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003368 }
Krzysztof Parzyszeke720feb2017-03-02 21:49:49 +00003369
3370 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
3371 if (NewOpcode >= 0)
3372 return NewOpcode;
Krzysztof Parzyszek066e8b52017-06-02 14:07:06 +00003373 return 0;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003374}
3375
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003376int HexagonInstrInfo::getDotOldOp(const MachineInstr &MI) const {
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003377 const MachineFunction &MF = *MI.getParent()->getParent();
3378 const HexagonSubtarget &HST = MF.getSubtarget<HexagonSubtarget>();
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003379 int NewOp = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003380 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3381 NewOp = Hexagon::getPredOldOpcode(NewOp);
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +00003382 // All Hexagon architectures have prediction bits on dot-new branches,
3383 // but only Hexagon V60+ has prediction bits on dot-old ones. Make sure
3384 // to pick the right opcode when converting back to dot-old.
3385 if (!HST.getFeatureBits()[Hexagon::ArchV60]) {
3386 switch (NewOp) {
3387 case Hexagon::J2_jumptpt:
3388 NewOp = Hexagon::J2_jumpt;
3389 break;
3390 case Hexagon::J2_jumpfpt:
3391 NewOp = Hexagon::J2_jumpf;
3392 break;
3393 case Hexagon::J2_jumprtpt:
3394 NewOp = Hexagon::J2_jumprt;
3395 break;
3396 case Hexagon::J2_jumprfpt:
3397 NewOp = Hexagon::J2_jumprf;
3398 break;
3399 }
3400 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003401 assert(NewOp >= 0 &&
3402 "Couldn't change predicate new instruction to its old form.");
3403 }
3404
3405 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3406 NewOp = Hexagon::getNonNVStore(NewOp);
3407 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3408 }
Krzysztof Parzyszek19635bd2017-05-03 15:30:46 +00003409
3410 if (HST.hasV60TOps())
3411 return NewOp;
3412
3413 // Subtargets prior to V60 didn't support 'taken' forms of predicated jumps.
3414 switch (NewOp) {
3415 case Hexagon::J2_jumpfpt:
3416 return Hexagon::J2_jumpf;
3417 case Hexagon::J2_jumptpt:
3418 return Hexagon::J2_jumpt;
3419 case Hexagon::J2_jumprfpt:
3420 return Hexagon::J2_jumprf;
3421 case Hexagon::J2_jumprtpt:
3422 return Hexagon::J2_jumprt;
3423 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003424 return NewOp;
3425}
3426
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003427// See if instruction could potentially be a duplex candidate.
3428// If so, return its group. Zero otherwise.
3429HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003430 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003431 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003432 const MachineFunction &MF = *MI.getParent()->getParent();
3433 const auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003434
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003435 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003436 default:
3437 return HexagonII::HSIG_None;
3438 //
3439 // Group L1:
3440 //
3441 // Rd = memw(Rs+#u4:2)
3442 // Rd = memub(Rs+#u4:0)
3443 case Hexagon::L2_loadri_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003444 DstReg = MI.getOperand(0).getReg();
3445 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003446 // Special case this one from Group L2.
3447 // Rd = memw(r29+#u5:2)
3448 if (isIntRegForSubInst(DstReg)) {
3449 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3450 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003451 MI.getOperand(2).isImm() &&
3452 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003453 return HexagonII::HSIG_L2;
3454 // Rd = memw(Rs+#u4:2)
3455 if (isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003456 (MI.getOperand(2).isImm() &&
3457 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003458 return HexagonII::HSIG_L1;
3459 }
3460 break;
3461 case Hexagon::L2_loadrub_io:
3462 // Rd = memub(Rs+#u4:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003463 DstReg = MI.getOperand(0).getReg();
3464 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003465 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003466 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003467 return HexagonII::HSIG_L1;
3468 break;
3469 //
3470 // Group L2:
3471 //
3472 // Rd = memh/memuh(Rs+#u3:1)
3473 // Rd = memb(Rs+#u3:0)
3474 // Rd = memw(r29+#u5:2) - Handled above.
3475 // Rdd = memd(r29+#u5:3)
3476 // deallocframe
3477 // [if ([!]p0[.new])] dealloc_return
3478 // [if ([!]p0[.new])] jumpr r31
3479 case Hexagon::L2_loadrh_io:
3480 case Hexagon::L2_loadruh_io:
3481 // Rd = memh/memuh(Rs+#u3:1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003482 DstReg = MI.getOperand(0).getReg();
3483 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003484 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003485 MI.getOperand(2).isImm() &&
3486 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003487 return HexagonII::HSIG_L2;
3488 break;
3489 case Hexagon::L2_loadrb_io:
3490 // Rd = memb(Rs+#u3:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003491 DstReg = MI.getOperand(0).getReg();
3492 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003493 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003494 MI.getOperand(2).isImm() &&
3495 isUInt<3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003496 return HexagonII::HSIG_L2;
3497 break;
3498 case Hexagon::L2_loadrd_io:
3499 // Rdd = memd(r29+#u5:3)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003500 DstReg = MI.getOperand(0).getReg();
3501 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003502 if (isDblRegForSubInst(DstReg, HRI) &&
3503 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3504 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003505 MI.getOperand(2).isImm() &&
3506 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003507 return HexagonII::HSIG_L2;
3508 break;
3509 // dealloc_return is not documented in Hexagon Manual, but marked
3510 // with A_SUBINSN attribute in iset_v4classic.py.
3511 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003512 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003513 case Hexagon::L4_return:
3514 case Hexagon::L2_deallocframe:
3515 return HexagonII::HSIG_L2;
3516 case Hexagon::EH_RETURN_JMPR:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003517 case Hexagon::PS_jmpret:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003518 // jumpr r31
3519 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003520 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003521 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3522 return HexagonII::HSIG_L2;
3523 break;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003524 case Hexagon::PS_jmprett:
3525 case Hexagon::PS_jmpretf:
3526 case Hexagon::PS_jmprettnewpt:
3527 case Hexagon::PS_jmpretfnewpt:
3528 case Hexagon::PS_jmprettnew:
3529 case Hexagon::PS_jmpretfnew:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003530 DstReg = MI.getOperand(1).getReg();
3531 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003532 // [if ([!]p0[.new])] jumpr r31
3533 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3534 (Hexagon::P0 == SrcReg)) &&
3535 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3536 return HexagonII::HSIG_L2;
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003537 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003538 case Hexagon::L4_return_t :
3539 case Hexagon::L4_return_f :
3540 case Hexagon::L4_return_tnew_pnt :
3541 case Hexagon::L4_return_fnew_pnt :
3542 case Hexagon::L4_return_tnew_pt :
3543 case Hexagon::L4_return_fnew_pt :
3544 // [if ([!]p0[.new])] dealloc_return
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003545 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003546 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3547 return HexagonII::HSIG_L2;
3548 break;
3549 //
3550 // Group S1:
3551 //
3552 // memw(Rs+#u4:2) = Rt
3553 // memb(Rs+#u4:0) = Rt
3554 case Hexagon::S2_storeri_io:
3555 // Special case this one from Group S2.
3556 // memw(r29+#u5:2) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003557 Src1Reg = MI.getOperand(0).getReg();
3558 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003559 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3560 isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003561 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3562 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003563 return HexagonII::HSIG_S2;
3564 // memw(Rs+#u4:2) = Rt
3565 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003566 MI.getOperand(1).isImm() &&
3567 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003568 return HexagonII::HSIG_S1;
3569 break;
3570 case Hexagon::S2_storerb_io:
3571 // memb(Rs+#u4:0) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003572 Src1Reg = MI.getOperand(0).getReg();
3573 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003574 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003575 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003576 return HexagonII::HSIG_S1;
3577 break;
3578 //
3579 // Group S2:
3580 //
3581 // memh(Rs+#u3:1) = Rt
3582 // memw(r29+#u5:2) = Rt
3583 // memd(r29+#s6:3) = Rtt
3584 // memw(Rs+#u4:2) = #U1
3585 // memb(Rs+#u4) = #U1
3586 // allocframe(#u5:3)
3587 case Hexagon::S2_storerh_io:
3588 // memh(Rs+#u3:1) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003589 Src1Reg = MI.getOperand(0).getReg();
3590 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003591 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003592 MI.getOperand(1).isImm() &&
3593 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003594 return HexagonII::HSIG_S1;
3595 break;
3596 case Hexagon::S2_storerd_io:
3597 // memd(r29+#s6:3) = Rtt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003598 Src1Reg = MI.getOperand(0).getReg();
3599 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003600 if (isDblRegForSubInst(Src2Reg, HRI) &&
3601 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003602 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3603 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003604 return HexagonII::HSIG_S2;
3605 break;
3606 case Hexagon::S4_storeiri_io:
3607 // memw(Rs+#u4:2) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003608 Src1Reg = MI.getOperand(0).getReg();
3609 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
3610 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
3611 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003612 return HexagonII::HSIG_S2;
3613 break;
3614 case Hexagon::S4_storeirb_io:
3615 // memb(Rs+#u4) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003616 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003617 if (isIntRegForSubInst(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003618 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
3619 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003620 return HexagonII::HSIG_S2;
3621 break;
3622 case Hexagon::S2_allocframe:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003623 if (MI.getOperand(0).isImm() &&
3624 isShiftedUInt<5,3>(MI.getOperand(0).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003625 return HexagonII::HSIG_S1;
3626 break;
3627 //
3628 // Group A:
3629 //
3630 // Rx = add(Rx,#s7)
3631 // Rd = Rs
3632 // Rd = #u6
3633 // Rd = #-1
3634 // if ([!]P0[.new]) Rd = #0
3635 // Rd = add(r29,#u6:2)
3636 // Rx = add(Rx,Rs)
3637 // P0 = cmp.eq(Rs,#u2)
3638 // Rdd = combine(#0,Rs)
3639 // Rdd = combine(Rs,#0)
3640 // Rdd = combine(#u2,#U2)
3641 // Rd = add(Rs,#1)
3642 // Rd = add(Rs,#-1)
3643 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3644 // Rd = and(Rs,#1)
3645 case Hexagon::A2_addi:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003646 DstReg = MI.getOperand(0).getReg();
3647 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003648 if (isIntRegForSubInst(DstReg)) {
3649 // Rd = add(r29,#u6:2)
3650 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003651 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
3652 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003653 return HexagonII::HSIG_A;
3654 // Rx = add(Rx,#s7)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003655 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
3656 isInt<7>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003657 return HexagonII::HSIG_A;
3658 // Rd = add(Rs,#1)
3659 // Rd = add(Rs,#-1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003660 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3661 ((MI.getOperand(2).getImm() == 1) ||
3662 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003663 return HexagonII::HSIG_A;
3664 }
3665 break;
3666 case Hexagon::A2_add:
3667 // Rx = add(Rx,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003668 DstReg = MI.getOperand(0).getReg();
3669 Src1Reg = MI.getOperand(1).getReg();
3670 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003671 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3672 isIntRegForSubInst(Src2Reg))
3673 return HexagonII::HSIG_A;
3674 break;
3675 case Hexagon::A2_andir:
3676 // Same as zxtb.
3677 // Rd16=and(Rs16,#255)
3678 // Rd16=and(Rs16,#1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003679 DstReg = MI.getOperand(0).getReg();
3680 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003681 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003682 MI.getOperand(2).isImm() &&
3683 ((MI.getOperand(2).getImm() == 1) ||
3684 (MI.getOperand(2).getImm() == 255)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003685 return HexagonII::HSIG_A;
3686 break;
3687 case Hexagon::A2_tfr:
3688 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003689 DstReg = MI.getOperand(0).getReg();
3690 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003691 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3692 return HexagonII::HSIG_A;
3693 break;
3694 case Hexagon::A2_tfrsi:
3695 // Rd = #u6
3696 // Do not test for #u6 size since the const is getting extended
3697 // regardless and compound could be formed.
3698 // Rd = #-1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003699 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003700 if (isIntRegForSubInst(DstReg))
3701 return HexagonII::HSIG_A;
3702 break;
3703 case Hexagon::C2_cmoveit:
3704 case Hexagon::C2_cmovenewit:
3705 case Hexagon::C2_cmoveif:
3706 case Hexagon::C2_cmovenewif:
3707 // if ([!]P0[.new]) Rd = #0
3708 // Actual form:
3709 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003710 DstReg = MI.getOperand(0).getReg();
3711 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003712 if (isIntRegForSubInst(DstReg) &&
3713 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003714 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003715 return HexagonII::HSIG_A;
3716 break;
3717 case Hexagon::C2_cmpeqi:
3718 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003719 DstReg = MI.getOperand(0).getReg();
3720 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003721 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3722 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003723 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003724 return HexagonII::HSIG_A;
3725 break;
3726 case Hexagon::A2_combineii:
3727 case Hexagon::A4_combineii:
3728 // Rdd = combine(#u2,#U2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003729 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003730 if (isDblRegForSubInst(DstReg, HRI) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003731 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
3732 (MI.getOperand(1).isGlobal() &&
3733 isUInt<2>(MI.getOperand(1).getOffset()))) &&
3734 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
3735 (MI.getOperand(2).isGlobal() &&
3736 isUInt<2>(MI.getOperand(2).getOffset()))))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003737 return HexagonII::HSIG_A;
3738 break;
3739 case Hexagon::A4_combineri:
3740 // Rdd = combine(Rs,#0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003741 DstReg = MI.getOperand(0).getReg();
3742 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003743 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003744 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
3745 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003746 return HexagonII::HSIG_A;
3747 break;
3748 case Hexagon::A4_combineir:
3749 // Rdd = combine(#0,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003750 DstReg = MI.getOperand(0).getReg();
3751 SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003752 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003753 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
3754 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003755 return HexagonII::HSIG_A;
3756 break;
3757 case Hexagon::A2_sxtb:
3758 case Hexagon::A2_sxth:
3759 case Hexagon::A2_zxtb:
3760 case Hexagon::A2_zxth:
3761 // Rd = sxth/sxtb/zxtb/zxth(Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003762 DstReg = MI.getOperand(0).getReg();
3763 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003764 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3765 return HexagonII::HSIG_A;
3766 break;
3767 }
3768
3769 return HexagonII::HSIG_None;
3770}
3771
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003772short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
3773 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003774}
3775
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003776unsigned HexagonInstrInfo::getInstrTimingClassLatency(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003777 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003778 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3779 // still have a MinLatency property, which getStageLatency checks.
3780 if (!ItinData)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003781 return getInstrLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003782
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003783 if (MI.isTransient())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003784 return 0;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003785 return ItinData->getStageLatency(MI.getDesc().getSchedClass());
3786}
3787
3788/// getOperandLatency - Compute and return the use operand latency of a given
3789/// pair of def and use.
3790/// In most cases, the static scheduling itinerary was enough to determine the
3791/// operand latency. But it may not be possible for instructions with variable
3792/// number of defs / uses.
3793///
3794/// This is a raw interface to the itinerary that may be directly overriden by
3795/// a target. Use computeOperandLatency to get the best estimate of latency.
3796int HexagonInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3797 const MachineInstr &DefMI,
3798 unsigned DefIdx,
3799 const MachineInstr &UseMI,
3800 unsigned UseIdx) const {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003801 const MachineFunction &MF = *DefMI.getParent()->getParent();
3802 const auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
3803
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003804 // Get DefIdx and UseIdx for super registers.
3805 MachineOperand DefMO = DefMI.getOperand(DefIdx);
3806
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003807 if (HRI.isPhysicalRegister(DefMO.getReg())) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003808 if (DefMO.isImplicit()) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003809 for (MCSuperRegIterator SR(DefMO.getReg(), &HRI); SR.isValid(); ++SR) {
3810 int Idx = DefMI.findRegisterDefOperandIdx(*SR, false, false, &HRI);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003811 if (Idx != -1) {
3812 DefIdx = Idx;
3813 break;
3814 }
3815 }
3816 }
3817
3818 MachineOperand UseMO = UseMI.getOperand(UseIdx);
3819 if (UseMO.isImplicit()) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003820 for (MCSuperRegIterator SR(UseMO.getReg(), &HRI); SR.isValid(); ++SR) {
3821 int Idx = UseMI.findRegisterUseOperandIdx(*SR, false, &HRI);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00003822 if (Idx != -1) {
3823 UseIdx = Idx;
3824 break;
3825 }
3826 }
3827 }
3828 }
3829
3830 return TargetInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
3831 UseMI, UseIdx);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003832}
3833
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003834// inverts the predication logic.
3835// p -> NotP
3836// NotP -> P
3837bool HexagonInstrInfo::getInvertedPredSense(
3838 SmallVectorImpl<MachineOperand> &Cond) const {
3839 if (Cond.empty())
3840 return false;
3841 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
3842 Cond[0].setImm(Opc);
3843 return true;
3844}
3845
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003846unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
3847 int InvPredOpcode;
3848 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
3849 : Hexagon::getTruePredOpcode(Opc);
3850 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
3851 return InvPredOpcode;
3852
3853 llvm_unreachable("Unexpected predicated instruction");
3854}
3855
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003856// Returns the max value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003857int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
3858 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003859 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3860 & HexagonII::ExtentSignedMask;
3861 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3862 & HexagonII::ExtentBitsMask;
3863
3864 if (isSigned) // if value is signed
3865 return ~(-1U << (bits - 1));
3866 else
3867 return ~(-1U << bits);
3868}
3869
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003870unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00003871 using namespace HexagonII;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003872 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00003873 unsigned S = (F >> MemAccessSizePos) & MemAccesSizeMask;
3874 unsigned Size = getMemAccessSizeInBytes(MemAccessSize(S));
3875 if (Size != 0)
3876 return Size;
3877
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003878 const MachineFunction &MF = *MI.getParent()->getParent();
3879 const auto &HRI = *MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
3880
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00003881 // Handle vector access sizes.
3882 switch (S) {
Krzysztof Parzyszek55772972017-09-15 15:46:05 +00003883 case HexagonII::HVXVectorAccess:
3884 return HRI.getSpillSize(Hexagon::HvxVRRegClass);
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +00003885 default:
3886 llvm_unreachable("Unexpected instruction");
3887 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003888}
3889
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003890// Returns the min value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003891int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
3892 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003893 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3894 & HexagonII::ExtentSignedMask;
3895 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3896 & HexagonII::ExtentBitsMask;
3897
3898 if (isSigned) // if value is signed
3899 return -1U << (bits - 1);
3900 else
3901 return 0;
3902}
3903
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003904// Returns opcode of the non-extended equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003905short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00003906 // Check if the instruction has a register form that uses register in place
3907 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003908 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00003909 if (NonExtOpcode >= 0)
3910 return NonExtOpcode;
3911
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003912 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00003913 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00003914 switch (getAddrMode(MI)) {
3915 case HexagonII::Absolute :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003916 return Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00003917 case HexagonII::BaseImmOffset :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003918 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003919 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003920 return Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003921
Jyotsna Verma84256432013-03-01 17:37:13 +00003922 default:
3923 return -1;
3924 }
3925 }
3926 return -1;
3927}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00003928
Ahmed Bougachac88bf542015-06-11 19:30:37 +00003929bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003930 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00003931 if (Cond.empty())
3932 return false;
3933 assert(Cond.size() == 2);
3934 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003935 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
3936 return false;
Brendon Cahoondf43e682015-05-08 16:16:29 +00003937 }
3938 PredReg = Cond[1].getReg();
3939 PredRegPos = 1;
3940 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
3941 PredRegFlags = 0;
3942 if (Cond[1].isImplicit())
3943 PredRegFlags = RegState::Implicit;
3944 if (Cond[1].isUndef())
3945 PredRegFlags |= RegState::Undef;
3946 return true;
3947}
3948
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003949short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
3950 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003951}
3952
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003953short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
3954 return Hexagon::getRegForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003955}
3956
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003957// Return the number of bytes required to encode the instruction.
3958// Hexagon instructions are fixed length, 4 bytes, unless they
3959// use a constant extender, which requires another 4 bytes.
3960// For debug instructions and prolog labels, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003961unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
3962 if (MI.isDebugValue() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003963 return 0;
3964
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003965 unsigned Size = MI.getDesc().getSize();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003966 if (!Size)
3967 // Assume the default insn size in case it cannot be determined
3968 // for whatever reason.
3969 Size = HEXAGON_INSTR_SIZE;
3970
3971 if (isConstExtended(MI) || isExtended(MI))
3972 Size += HEXAGON_INSTR_SIZE;
3973
3974 // Try and compute number of instructions in asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003975 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
3976 const MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003977 const MachineFunction *MF = MBB.getParent();
3978 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
3979
3980 // Count the number of register definitions to find the asm string.
3981 unsigned NumDefs = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003982 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003983 ++NumDefs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003984 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003985
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003986 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003987 // Disassemble the AsmStr and approximate number of instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003988 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003989 Size = getInlineAsmLength(AsmStr, *MAI);
3990 }
3991
3992 return Size;
3993}
3994
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003995uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
3996 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003997 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
3998}
3999
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004000unsigned HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
4001 const TargetSubtargetInfo &ST = MI.getParent()->getParent()->getSubtarget();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004002 const InstrItineraryData &II = *ST.getInstrItineraryData();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004003 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004004
4005 return IS.getUnits();
4006}
4007
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004008// Calculate size of the basic block without debug instructions.
4009unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4010 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4011}
4012
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004013unsigned HexagonInstrInfo::nonDbgBundleSize(
4014 MachineBasicBlock::const_iterator BundleHead) const {
4015 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004016 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004017 // Skip the bundle header.
Matthias Braunc8440dd2016-10-25 02:55:17 +00004018 return nonDbgMICount(++MII, getBundleEnd(BundleHead.getInstrIterator()));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004019}
4020
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004021/// immediateExtend - Changes the instruction in place to one using an immediate
4022/// extender.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004023void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004024 assert((isExtendable(MI)||isConstExtended(MI)) &&
4025 "Instruction must be extendable");
4026 // Find which operand is extendable.
4027 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004028 MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004029 // This needs to be something we understand.
4030 assert((MO.isMBB() || MO.isImm()) &&
4031 "Branch with unknown extendable field type");
4032 // Mark given operand as extended.
4033 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4034}
4035
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004036bool HexagonInstrInfo::invertAndChangeJumpTarget(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004037 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004038 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004039 << NewTarget->getNumber(); MI.dump(););
4040 assert(MI.isBranch());
4041 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4042 int TargetPos = MI.getNumOperands() - 1;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004043 // In general branch target is the last operand,
4044 // but some implicit defs added at the end might change it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004045 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004046 --TargetPos;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004047 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4048 MI.getOperand(TargetPos).setMBB(NewTarget);
4049 if (EnableBranchPrediction && isPredicatedNew(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004050 NewOpcode = reversePrediction(NewOpcode);
4051 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004052 MI.setDesc(get(NewOpcode));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004053 return true;
4054}
4055
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004056void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4057 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4058 MachineFunction::iterator A = MF.begin();
4059 MachineBasicBlock &B = *A;
4060 MachineBasicBlock::iterator I = B.begin();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004061 DebugLoc DL = I->getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004062 MachineInstr *NewMI;
4063
4064 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4065 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004066 NewMI = BuildMI(B, I, DL, get(insn));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004067 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
4068 " Class: " << NewMI->getDesc().getSchedClass());
4069 NewMI->eraseFromParent();
4070 }
4071 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4072}
4073
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004074// inverts the predication logic.
4075// p -> NotP
4076// NotP -> P
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004077bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
4078 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
4079 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004080 return true;
4081}
4082
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004083// Reverse the branch prediction.
4084unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4085 int PredRevOpcode = -1;
4086 if (isPredictedTaken(Opcode))
4087 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4088 else
4089 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4090 assert(PredRevOpcode > 0);
4091 return PredRevOpcode;
4092}
4093
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004094// TODO: Add more rigorous validation.
4095bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4096 const {
4097 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4098}
4099
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004100short HexagonInstrInfo::xformRegToImmOffset(const MachineInstr &MI) const {
4101 return Hexagon::xformRegToImmOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004102}