blob: 34ce3e6529958bece814150cb109d93f0d5103a7 [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
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000014#include "HexagonHazardRecognizer.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000015#include "HexagonInstrInfo.h"
Craig Topperb25fda92012-03-17 18:46:09 +000016#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000017#include "HexagonSubtarget.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000018#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SmallVector.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000020#include "llvm/CodeGen/DFAPacketizer.h"
Ron Lieberman88159e52016-09-02 22:56:24 +000021#include "llvm/CodeGen/LivePhysRegs.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000024#include "llvm/CodeGen/MachineMemOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000026#include "llvm/CodeGen/PseudoSourceValue.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000027#include "llvm/CodeGen/ScheduleDAG.h"
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000028#include "llvm/MC/MCAsmInfo.h"
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +000029#include "llvm/Support/CommandLine.h"
Jyotsna Verma5ed51812013-05-01 21:37:34 +000030#include "llvm/Support/Debug.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000031#include "llvm/Support/MathExtras.h"
Reid Kleckner1c76f1552013-05-03 00:54:56 +000032#include "llvm/Support/raw_ostream.h"
Krzysztof Parzyszekaa935752015-11-24 15:11:13 +000033#include <cctype>
Tony Linthicum1213a7a2011-12-12 21:14:40 +000034
Tony Linthicum1213a7a2011-12-12 21:14:40 +000035using namespace llvm;
36
Chandler Carruthe96dd892014-04-21 22:55:11 +000037#define DEBUG_TYPE "hexagon-instrinfo"
38
Chandler Carruthd174b722014-04-22 02:03:14 +000039#define GET_INSTRINFO_CTOR_DTOR
40#define GET_INSTRMAP_INFO
41#include "HexagonGenInstrInfo.inc"
42#include "HexagonGenDFAPacketizer.inc"
43
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000044cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000045 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
46 "packetization boundary."));
47
48static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
49 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
50
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000051static cl::opt<bool> DisableNVSchedule("disable-hexagon-nv-schedule",
52 cl::Hidden, cl::ZeroOrMore, cl::init(false),
53 cl::desc("Disable schedule adjustment for new value stores."));
54
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000055static cl::opt<bool> EnableTimingClassLatency(
56 "enable-timing-class-latency", cl::Hidden, cl::init(false),
57 cl::desc("Enable timing class latency"));
58
59static cl::opt<bool> EnableALUForwarding(
60 "enable-alu-forwarding", cl::Hidden, cl::init(true),
61 cl::desc("Enable vec alu forwarding"));
62
63static cl::opt<bool> EnableACCForwarding(
64 "enable-acc-forwarding", cl::Hidden, cl::init(true),
65 cl::desc("Enable vec acc forwarding"));
66
67static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
68 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
69
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000070static cl::opt<bool> UseDFAHazardRec("dfa-hazard-rec",
71 cl::init(true), cl::Hidden, cl::ZeroOrMore,
72 cl::desc("Use the DFA based hazard recognizer."));
73
Tony Linthicum1213a7a2011-12-12 21:14:40 +000074///
75/// Constants for Hexagon instructions.
76///
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +000077const int Hexagon_MEMV_OFFSET_MAX_128B = 896; // #s4: -8*128...7*128
78const int Hexagon_MEMV_OFFSET_MIN_128B = -1024; // #s4
79const int Hexagon_MEMV_OFFSET_MAX = 448; // #s4: -8*64...7*64
80const int Hexagon_MEMV_OFFSET_MIN = -512; // #s4
Tony Linthicum1213a7a2011-12-12 21:14:40 +000081const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000082const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000083const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000084const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000085const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000086const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000087const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000088const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000089const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000090const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000091const int Hexagon_MEMD_AUTOINC_MAX = 56;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000092const int Hexagon_MEMD_AUTOINC_MIN = -64;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000093const int Hexagon_MEMW_AUTOINC_MAX = 28;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000094const int Hexagon_MEMW_AUTOINC_MIN = -32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000095const int Hexagon_MEMH_AUTOINC_MAX = 14;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000096const int Hexagon_MEMH_AUTOINC_MIN = -16;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000097const int Hexagon_MEMB_AUTOINC_MAX = 7;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000098const int Hexagon_MEMB_AUTOINC_MIN = -8;
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +000099const int Hexagon_MEMV_AUTOINC_MAX = 192; // #s3
100const int Hexagon_MEMV_AUTOINC_MIN = -256; // #s3
101const int Hexagon_MEMV_AUTOINC_MAX_128B = 384; // #s3
102const int Hexagon_MEMV_AUTOINC_MIN_128B = -512; // #s3
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000103
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000104// Pin the vtable to this file.
105void HexagonInstrInfo::anchor() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000106
107HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
Eric Christopherc4d31402015-03-10 23:45:55 +0000108 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000109 RI() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000110
111
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000112static bool isIntRegForSubInst(unsigned Reg) {
113 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
114 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000115}
116
117
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000118static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000119 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_lo)) &&
120 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::isub_hi));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000121}
122
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000123
124/// 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
135
136/// Find the hardware loop instruction used to set-up the specified loop.
137/// On Hexagon, we have two instructions used to set-up the hardware loop
138/// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
139/// to indicate the end of a loop.
140static MachineInstr *findLoopInstr(MachineBasicBlock *BB, int EndLoopOp,
141 SmallPtrSet<MachineBasicBlock *, 8> &Visited) {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000142 int LOOPi;
143 int LOOPr;
144 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
153 for (MachineBasicBlock::pred_iterator PB = BB->pred_begin(),
154 PE = BB->pred_end(); PB != PE; ++PB) {
155 // If this has been visited, already skip it.
156 if (!Visited.insert(*PB).second)
157 continue;
158 if (*PB == BB)
159 continue;
160 for (MachineBasicBlock::reverse_instr_iterator I = (*PB)->instr_rbegin(),
161 E = (*PB)->instr_rend(); I != E; ++I) {
162 int Opc = I->getOpcode();
163 if (Opc == LOOPi || Opc == LOOPr)
164 return &*I;
165 // We've reached a different loop, which means the loop0 has been removed.
166 if (Opc == EndLoopOp)
167 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000168 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000169 // Check the predecessors for the LOOP instruction.
170 MachineInstr *loop = findLoopInstr(*PB, EndLoopOp, Visited);
171 if (loop)
172 return loop;
173 }
174 return 0;
175}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000176
Brendon Cahoondf43e682015-05-08 16:16:29 +0000177
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000178/// Gather register def/uses from MI.
179/// This treats possible (predicated) defs as actually happening ones
180/// (conservatively).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000181static inline void parseOperands(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000182 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
183 Defs.clear();
184 Uses.clear();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000185
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000186 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
187 const MachineOperand &MO = MI.getOperand(i);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000188
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000189 if (!MO.isReg())
190 continue;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000191
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000192 unsigned Reg = MO.getReg();
193 if (!Reg)
194 continue;
195
196 if (MO.isUse())
197 Uses.push_back(MO.getReg());
198
199 if (MO.isDef())
200 Defs.push_back(MO.getReg());
201 }
202}
203
204
205// Position dependent, so check twice for swap.
206static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
207 switch (Ga) {
208 case HexagonII::HSIG_None:
209 default:
210 return false;
211 case HexagonII::HSIG_L1:
212 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
213 case HexagonII::HSIG_L2:
214 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
215 Gb == HexagonII::HSIG_A);
216 case HexagonII::HSIG_S1:
217 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
218 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
219 case HexagonII::HSIG_S2:
220 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
221 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
222 Gb == HexagonII::HSIG_A);
223 case HexagonII::HSIG_A:
224 return (Gb == HexagonII::HSIG_A);
225 case HexagonII::HSIG_Compound:
226 return (Gb == HexagonII::HSIG_Compound);
227 }
228 return false;
229}
230
231
232
233/// isLoadFromStackSlot - If the specified machine instruction is a direct
234/// load from a stack slot, return the virtual or physical register number of
235/// the destination along with the FrameIndex of the loaded stack slot. If
236/// not, return 0. This predicate must return 0 if the instruction has
237/// any side effects other than loading from the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000238unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000239 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000240 switch (MI.getOpcode()) {
241 default:
242 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000243 case Hexagon::L2_loadri_io:
244 case Hexagon::L2_loadrd_io:
245 case Hexagon::V6_vL32b_ai:
246 case Hexagon::V6_vL32b_ai_128B:
247 case Hexagon::V6_vL32Ub_ai:
248 case Hexagon::V6_vL32Ub_ai_128B:
249 case Hexagon::LDriw_pred:
250 case Hexagon::LDriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000251 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000252 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000253 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000254 case Hexagon::PS_vloadrw_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000255 const MachineOperand OpFI = MI.getOperand(1);
256 if (!OpFI.isFI())
257 return 0;
258 const MachineOperand OpOff = MI.getOperand(2);
259 if (!OpOff.isImm() || OpOff.getImm() != 0)
260 return 0;
261 FrameIndex = OpFI.getIndex();
262 return MI.getOperand(0).getReg();
263 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000264
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000265 case Hexagon::L2_ploadrit_io:
266 case Hexagon::L2_ploadrif_io:
267 case Hexagon::L2_ploadrdt_io:
268 case Hexagon::L2_ploadrdf_io: {
269 const MachineOperand OpFI = MI.getOperand(2);
270 if (!OpFI.isFI())
271 return 0;
272 const MachineOperand OpOff = MI.getOperand(3);
273 if (!OpOff.isImm() || OpOff.getImm() != 0)
274 return 0;
275 FrameIndex = OpFI.getIndex();
276 return MI.getOperand(0).getReg();
277 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000278 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000279
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000280 return 0;
281}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000282
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000283
284/// isStoreToStackSlot - If the specified machine instruction is a direct
285/// store to a stack slot, return the virtual or physical register number of
286/// the source reg along with the FrameIndex of the loaded stack slot. If
287/// not, return 0. This predicate must return 0 if the instruction has
288/// any side effects other than storing to the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000289unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000290 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000291 switch (MI.getOpcode()) {
292 default:
293 break;
294 case Hexagon::S2_storerb_io:
295 case Hexagon::S2_storerh_io:
296 case Hexagon::S2_storeri_io:
297 case Hexagon::S2_storerd_io:
298 case Hexagon::V6_vS32b_ai:
299 case Hexagon::V6_vS32b_ai_128B:
300 case Hexagon::V6_vS32Ub_ai:
301 case Hexagon::V6_vS32Ub_ai_128B:
302 case Hexagon::STriw_pred:
303 case Hexagon::STriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000304 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000305 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000306 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000307 case Hexagon::PS_vstorerw_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000308 const MachineOperand &OpFI = MI.getOperand(0);
309 if (!OpFI.isFI())
310 return 0;
311 const MachineOperand &OpOff = MI.getOperand(1);
312 if (!OpOff.isImm() || OpOff.getImm() != 0)
313 return 0;
314 FrameIndex = OpFI.getIndex();
315 return MI.getOperand(2).getReg();
316 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000317
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000318 case Hexagon::S2_pstorerbt_io:
319 case Hexagon::S2_pstorerbf_io:
320 case Hexagon::S2_pstorerht_io:
321 case Hexagon::S2_pstorerhf_io:
322 case Hexagon::S2_pstorerit_io:
323 case Hexagon::S2_pstorerif_io:
324 case Hexagon::S2_pstorerdt_io:
325 case Hexagon::S2_pstorerdf_io: {
326 const MachineOperand &OpFI = MI.getOperand(1);
327 if (!OpFI.isFI())
328 return 0;
329 const MachineOperand &OpOff = MI.getOperand(2);
330 if (!OpOff.isImm() || OpOff.getImm() != 0)
331 return 0;
332 FrameIndex = OpFI.getIndex();
333 return MI.getOperand(3).getReg();
334 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000335 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000336
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000337 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000338}
339
340
Brendon Cahoondf43e682015-05-08 16:16:29 +0000341/// This function can analyze one/two way branching only and should (mostly) be
342/// called by target independent side.
343/// First entry is always the opcode of the branching instruction, except when
344/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
345/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
346/// e.g. Jump_c p will have
347/// Cond[0] = Jump_c
348/// Cond[1] = p
349/// HW-loop ENDLOOP:
350/// Cond[0] = ENDLOOP
351/// Cond[1] = MBB
352/// New value jump:
353/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
354/// Cond[1] = R
355/// Cond[2] = Imm
Brendon Cahoondf43e682015-05-08 16:16:29 +0000356///
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000357bool HexagonInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000358 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000359 MachineBasicBlock *&FBB,
360 SmallVectorImpl<MachineOperand> &Cond,
361 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000362 TBB = nullptr;
363 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000364 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000365
366 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000367 MachineBasicBlock::instr_iterator I = MBB.instr_end();
368 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000369 return false;
370
371 // A basic block may looks like this:
372 //
373 // [ insn
374 // EH_LABEL
375 // insn
376 // insn
377 // insn
378 // EH_LABEL
379 // insn ]
380 //
381 // It has two succs but does not have a terminator
382 // Don't know how to handle it.
383 do {
384 --I;
385 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000386 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000387 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000388 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000389
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000390 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000391 --I;
392
393 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000394 if (I == MBB.instr_begin())
395 return false;
396 --I;
397 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000398
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000399 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
400 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000401 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000402 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000403 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
404 DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
405 I->eraseFromParent();
406 I = MBB.instr_end();
407 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000408 return false;
409 --I;
410 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000411 if (!isUnpredicatedTerminator(*I))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000412 return false;
413
414 // Get the last instruction in the block.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000415 MachineInstr *LastInst = &*I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000416 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000417 // Find one more terminator if present.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000418 for (;;) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000419 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000420 if (!SecondLastInst)
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000421 SecondLastInst = &*I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000422 else
423 // This is a third branch.
424 return true;
425 }
426 if (I == MBB.instr_begin())
427 break;
428 --I;
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000429 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000430
431 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000432 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
433 // If the branch target is not a basic block, it could be a tail call.
434 // (It is, if the target is a function.)
435 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
436 return true;
437 if (SecLastOpcode == Hexagon::J2_jump &&
438 !SecondLastInst->getOperand(0).isMBB())
439 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000440
441 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000442 bool LastOpcodeHasNVJump = isNewValueJump(*LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000443
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000444 if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
445 return true;
446
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000447 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000448 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000449 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000450 TBB = LastInst->getOperand(0).getMBB();
451 return false;
452 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000453 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000454 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000455 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000456 Cond.push_back(LastInst->getOperand(0));
457 return false;
458 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000459 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000460 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000461 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000462 Cond.push_back(LastInst->getOperand(0));
463 return false;
464 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000465 // Only supporting rr/ri versions of new-value jumps.
466 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
467 TBB = LastInst->getOperand(2).getMBB();
468 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
469 Cond.push_back(LastInst->getOperand(0));
470 Cond.push_back(LastInst->getOperand(1));
471 return false;
472 }
473 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
474 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000475 // Otherwise, don't know what this is.
476 return true;
477 }
478
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000479 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000480 bool SecLastOpcodeHasNVJump = isNewValueJump(*SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000481 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000482 if (!SecondLastInst->getOperand(1).isMBB())
483 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000484 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000485 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000486 Cond.push_back(SecondLastInst->getOperand(0));
487 FBB = LastInst->getOperand(0).getMBB();
488 return false;
489 }
490
Brendon Cahoondf43e682015-05-08 16:16:29 +0000491 // Only supporting rr/ri versions of new-value jumps.
492 if (SecLastOpcodeHasNVJump &&
493 (SecondLastInst->getNumExplicitOperands() == 3) &&
494 (LastOpcode == Hexagon::J2_jump)) {
495 TBB = SecondLastInst->getOperand(2).getMBB();
496 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
497 Cond.push_back(SecondLastInst->getOperand(0));
498 Cond.push_back(SecondLastInst->getOperand(1));
499 FBB = LastInst->getOperand(0).getMBB();
500 return false;
501 }
502
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000503 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
504 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000505 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000506 TBB = SecondLastInst->getOperand(0).getMBB();
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000507 I = LastInst->getIterator();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000508 if (AllowModify)
509 I->eraseFromParent();
510 return false;
511 }
512
Brendon Cahoondf43e682015-05-08 16:16:29 +0000513 // If the block ends with an ENDLOOP, and J2_jump, handle it.
514 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000515 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000516 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000517 Cond.push_back(SecondLastInst->getOperand(0));
518 FBB = LastInst->getOperand(0).getMBB();
519 return false;
520 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000521 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
522 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000523 // Otherwise, can't handle this.
524 return true;
525}
526
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000527
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000528unsigned HexagonInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000529 int *BytesRemoved) const {
530 assert(!BytesRemoved && "code size not handled");
531
Brendon Cahoondf43e682015-05-08 16:16:29 +0000532 DEBUG(dbgs() << "\nRemoving branches out of BB#" << MBB.getNumber());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000533 MachineBasicBlock::iterator I = MBB.end();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000534 unsigned Count = 0;
535 while (I != MBB.begin()) {
536 --I;
537 if (I->isDebugValue())
538 continue;
539 // Only removing branches from end of MBB.
540 if (!I->isBranch())
541 return Count;
542 if (Count && (I->getOpcode() == Hexagon::J2_jump))
543 llvm_unreachable("Malformed basic block: unconditional branch not last");
544 MBB.erase(&MBB.back());
545 I = MBB.end();
546 ++Count;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000547 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000548 return Count;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000549}
550
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000551unsigned HexagonInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000552 MachineBasicBlock *TBB,
553 MachineBasicBlock *FBB,
554 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000555 const DebugLoc &DL,
556 int *BytesAdded) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000557 unsigned BOpc = Hexagon::J2_jump;
558 unsigned BccOpc = Hexagon::J2_jumpt;
559 assert(validateBranchCond(Cond) && "Invalid branching condition");
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000560 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000561 assert(!BytesAdded && "code size not handled");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000562
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000563 // Check if reverseBranchCondition has asked to reverse this branch
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000564 // If we want to reverse the branch an odd number of times, we want
565 // J2_jumpf.
566 if (!Cond.empty() && Cond[0].isImm())
567 BccOpc = Cond[0].getImm();
568
569 if (!FBB) {
570 if (Cond.empty()) {
571 // Due to a bug in TailMerging/CFG Optimization, we need to add a
572 // special case handling of a predicated jump followed by an
573 // unconditional jump. If not, Tail Merging and CFG Optimization go
574 // into an infinite loop.
575 MachineBasicBlock *NewTBB, *NewFBB;
576 SmallVector<MachineOperand, 4> Cond;
Duncan P. N. Exon Smith25b132e2016-07-08 18:26:20 +0000577 auto Term = MBB.getFirstTerminator();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000578 if (Term != MBB.end() && isPredicated(*Term) &&
Duncan P. N. Exon Smithe04fe1a2016-08-17 00:34:00 +0000579 !analyzeBranch(MBB, NewTBB, NewFBB, Cond, false) &&
580 MachineFunction::iterator(NewTBB) == ++MBB.getIterator()) {
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000581 reverseBranchCondition(Cond);
582 removeBranch(MBB);
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000583 return insertBranch(MBB, TBB, nullptr, Cond, DL);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000584 }
585 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
586 } else if (isEndLoopN(Cond[0].getImm())) {
587 int EndLoopOp = Cond[0].getImm();
588 assert(Cond[1].isMBB());
589 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
590 // Check for it, and change the BB target if needed.
591 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
592 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
593 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
594 Loop->getOperand(0).setMBB(TBB);
595 // Add the ENDLOOP after the finding the LOOP0.
596 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
597 } else if (isNewValueJump(Cond[0].getImm())) {
598 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
599 // New value jump
600 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
601 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
602 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
603 DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
604 if (Cond[2].isReg()) {
605 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
606 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
607 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
608 } else if(Cond[2].isImm()) {
609 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
610 addImm(Cond[2].getImm()).addMBB(TBB);
611 } else
612 llvm_unreachable("Invalid condition for branching");
613 } else {
614 assert((Cond.size() == 2) && "Malformed cond vector");
615 const MachineOperand &RO = Cond[1];
616 unsigned Flags = getUndefRegState(RO.isUndef());
617 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
618 }
619 return 1;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000620 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000621 assert((!Cond.empty()) &&
622 "Cond. cannot be empty when multiple branchings are required");
623 assert((!isNewValueJump(Cond[0].getImm())) &&
624 "NV-jump cannot be inserted with another branch");
625 // Special case for hardware loops. The condition is a basic block.
626 if (isEndLoopN(Cond[0].getImm())) {
627 int EndLoopOp = Cond[0].getImm();
628 assert(Cond[1].isMBB());
629 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
630 // Check for it, and change the BB target if needed.
631 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
632 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
633 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
634 Loop->getOperand(0).setMBB(TBB);
635 // Add the ENDLOOP after the finding the LOOP0.
636 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
637 } else {
638 const MachineOperand &RO = Cond[1];
639 unsigned Flags = getUndefRegState(RO.isUndef());
640 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000641 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000642 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000643
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000644 return 2;
645}
646
Brendon Cahoon254f8892016-07-29 16:44:44 +0000647/// Analyze the loop code to find the loop induction variable and compare used
648/// to compute the number of iterations. Currently, we analyze loop that are
649/// controlled using hardware loops. In this case, the induction variable
650/// instruction is null. For all other cases, this function returns true, which
651/// means we're unable to analyze it.
652bool HexagonInstrInfo::analyzeLoop(MachineLoop &L,
653 MachineInstr *&IndVarInst,
654 MachineInstr *&CmpInst) const {
655
656 MachineBasicBlock *LoopEnd = L.getBottomBlock();
657 MachineBasicBlock::iterator I = LoopEnd->getFirstTerminator();
658 // We really "analyze" only hardware loops right now.
659 if (I != LoopEnd->end() && isEndLoopN(I->getOpcode())) {
660 IndVarInst = nullptr;
661 CmpInst = &*I;
662 return false;
663 }
664 return true;
665}
666
667/// Generate code to reduce the loop iteration by one and check if the loop is
668/// finished. Return the value/register of the new loop count. this function
669/// assumes the nth iteration is peeled first.
670unsigned HexagonInstrInfo::reduceLoopCount(MachineBasicBlock &MBB,
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000671 MachineInstr *IndVar, MachineInstr &Cmp,
Brendon Cahoon254f8892016-07-29 16:44:44 +0000672 SmallVectorImpl<MachineOperand> &Cond,
673 SmallVectorImpl<MachineInstr *> &PrevInsts,
674 unsigned Iter, unsigned MaxIter) const {
675 // We expect a hardware loop currently. This means that IndVar is set
676 // to null, and the compare is the ENDLOOP instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000677 assert((!IndVar) && isEndLoopN(Cmp.getOpcode())
Brendon Cahoon254f8892016-07-29 16:44:44 +0000678 && "Expecting a hardware loop");
679 MachineFunction *MF = MBB.getParent();
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000680 DebugLoc DL = Cmp.getDebugLoc();
Brendon Cahoon254f8892016-07-29 16:44:44 +0000681 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000682 MachineInstr *Loop = findLoopInstr(&MBB, Cmp.getOpcode(), VisitedBBs);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000683 if (!Loop)
684 return 0;
685 // If the loop trip count is a compile-time value, then just change the
686 // value.
687 if (Loop->getOpcode() == Hexagon::J2_loop0i ||
688 Loop->getOpcode() == Hexagon::J2_loop1i) {
689 int64_t Offset = Loop->getOperand(1).getImm();
690 if (Offset <= 1)
691 Loop->eraseFromParent();
692 else
693 Loop->getOperand(1).setImm(Offset - 1);
694 return Offset - 1;
695 }
696 // The loop trip count is a run-time value. We generate code to subtract
697 // one from the trip count, and update the loop instruction.
698 assert(Loop->getOpcode() == Hexagon::J2_loop0r && "Unexpected instruction");
699 unsigned LoopCount = Loop->getOperand(1).getReg();
700 // Check if we're done with the loop.
701 unsigned LoopEnd = createVR(MF, MVT::i1);
702 MachineInstr *NewCmp = BuildMI(&MBB, DL, get(Hexagon::C2_cmpgtui), LoopEnd).
703 addReg(LoopCount).addImm(1);
704 unsigned NewLoopCount = createVR(MF, MVT::i32);
705 MachineInstr *NewAdd = BuildMI(&MBB, DL, get(Hexagon::A2_addi), NewLoopCount).
706 addReg(LoopCount).addImm(-1);
707 // Update the previously generated instructions with the new loop counter.
708 for (SmallVectorImpl<MachineInstr *>::iterator I = PrevInsts.begin(),
709 E = PrevInsts.end(); I != E; ++I)
710 (*I)->substituteRegister(LoopCount, NewLoopCount, 0, getRegisterInfo());
711 PrevInsts.clear();
712 PrevInsts.push_back(NewCmp);
713 PrevInsts.push_back(NewAdd);
714 // Insert the new loop instruction if this is the last time the loop is
715 // decremented.
716 if (Iter == MaxIter)
717 BuildMI(&MBB, DL, get(Hexagon::J2_loop0r)).
718 addMBB(Loop->getOperand(0).getMBB()).addReg(NewLoopCount);
719 // Delete the old loop instruction.
720 if (Iter == 0)
721 Loop->eraseFromParent();
722 Cond.push_back(MachineOperand::CreateImm(Hexagon::J2_jumpf));
723 Cond.push_back(NewCmp->getOperand(0));
724 return NewLoopCount;
725}
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000726
727bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
728 unsigned NumCycles, unsigned ExtraPredCycles,
729 BranchProbability Probability) const {
730 return nonDbgBBSize(&MBB) <= 3;
731}
732
733
734bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
735 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
736 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
737 const {
738 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
739}
740
741
742bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
743 unsigned NumInstrs, BranchProbability Probability) const {
744 return NumInstrs <= 4;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000745}
746
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000747void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000748 MachineBasicBlock::iterator I,
749 const DebugLoc &DL, unsigned DestReg,
750 unsigned SrcReg, bool KillSrc) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000751 auto &HRI = getRegisterInfo();
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000752 unsigned KillFlag = getKillRegState(KillSrc);
753
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000754 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000755 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000756 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000757 return;
758 }
759 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000760 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg)
761 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000762 return;
763 }
764 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
765 // Map Pd = Ps to Pd = or(Ps, Ps).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000766 BuildMI(MBB, I, DL, get(Hexagon::C2_or), DestReg)
767 .addReg(SrcReg).addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000768 return;
769 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000770 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000771 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000772 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
773 .addReg(SrcReg, KillFlag);
774 return;
775 }
776 if (Hexagon::IntRegsRegClass.contains(DestReg) &&
777 Hexagon::CtrRegsRegClass.contains(SrcReg)) {
778 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrcrr), DestReg)
779 .addReg(SrcReg, KillFlag);
780 return;
781 }
782 if (Hexagon::ModRegsRegClass.contains(DestReg) &&
783 Hexagon::IntRegsRegClass.contains(SrcReg)) {
784 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
785 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000786 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000787 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +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);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000792 return;
793 }
794 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
795 Hexagon::PredRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000796 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg)
797 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000798 return;
799 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000800 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
801 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000802 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
803 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000804 return;
805 }
806 if (Hexagon::VectorRegsRegClass.contains(SrcReg, DestReg)) {
807 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000808 addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000809 return;
810 }
811 if (Hexagon::VecDblRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000812 unsigned LoSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
813 unsigned HiSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000814 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000815 .addReg(HiSrc, KillFlag)
816 .addReg(LoSrc, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000817 return;
818 }
819 if (Hexagon::VecPredRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000820 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
821 .addReg(SrcReg)
822 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000823 return;
824 }
825 if (Hexagon::VecPredRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000826 Hexagon::VectorRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000827 llvm_unreachable("Unimplemented pred to vec");
828 return;
829 }
830 if (Hexagon::VecPredRegsRegClass.contains(DestReg) &&
831 Hexagon::VectorRegsRegClass.contains(SrcReg)) {
832 llvm_unreachable("Unimplemented vec to pred");
833 return;
834 }
835 if (Hexagon::VecPredRegs128BRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +0000836 unsigned HiDst = HRI.getSubReg(DestReg, Hexagon::vsub_hi);
837 unsigned LoDst = HRI.getSubReg(DestReg, Hexagon::vsub_lo);
838 unsigned HiSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
839 unsigned LoSrc = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
840 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), HiDst)
841 .addReg(HiSrc, KillFlag);
842 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), LoDst)
843 .addReg(LoSrc, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000844 return;
845 }
Sirish Pande30804c22012-02-15 18:52:27 +0000846
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000847#ifndef NDEBUG
848 // Show the invalid registers to ease debugging.
849 dbgs() << "Invalid registers for copy in BB#" << MBB.getNumber()
850 << ": " << PrintReg(DestReg, &HRI)
851 << " = " << PrintReg(SrcReg, &HRI) << '\n';
852#endif
Sirish Pande30804c22012-02-15 18:52:27 +0000853 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000854}
855
856
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000857void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
858 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
859 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000860 DebugLoc DL = MBB.findDebugLoc(I);
861 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000862 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000863 unsigned Align = MFI.getObjectAlignment(FI);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000864 unsigned KillFlag = getKillRegState(isKill);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000865
Alex Lorenze40c8a22015-08-11 23:09:45 +0000866 MachineMemOperand *MMO = MF.getMachineMemOperand(
867 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
868 MFI.getObjectSize(FI), Align);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000869
Craig Topperc7242e02012-04-20 07:30:17 +0000870 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000871 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000872 .addFrameIndex(FI).addImm(0)
873 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000874 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000875 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000876 .addFrameIndex(FI).addImm(0)
877 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000878 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000879 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000880 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000881 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000882 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
883 BuildMI(MBB, I, DL, get(Hexagon::STriw_mod))
884 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000885 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
886 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000887 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai_128B))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000888 .addFrameIndex(FI).addImm(0)
889 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
890 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000891 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000892 .addFrameIndex(FI).addImm(0)
893 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
894 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000895 unsigned Opc = Align < 128 ? Hexagon::V6_vS32Ub_ai_128B
896 : Hexagon::V6_vS32b_ai_128B;
897 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000898 .addFrameIndex(FI).addImm(0)
899 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
900 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000901 unsigned Opc = Align < 64 ? Hexagon::V6_vS32Ub_ai
902 : Hexagon::V6_vS32b_ai;
903 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000904 .addFrameIndex(FI).addImm(0)
905 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
906 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000907 unsigned Opc = Align < 64 ? Hexagon::PS_vstorerwu_ai
908 : Hexagon::PS_vstorerw_ai;
909 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000910 .addFrameIndex(FI).addImm(0)
911 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
912 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000913 unsigned Opc = Align < 128 ? Hexagon::PS_vstorerwu_ai_128B
914 : Hexagon::PS_vstorerw_ai_128B;
915 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000916 .addFrameIndex(FI).addImm(0)
917 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000918 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000919 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000920 }
921}
922
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000923void HexagonInstrInfo::loadRegFromStackSlot(
924 MachineBasicBlock &MBB, MachineBasicBlock::iterator I, unsigned DestReg,
925 int FI, const TargetRegisterClass *RC,
926 const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000927 DebugLoc DL = MBB.findDebugLoc(I);
928 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000929 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000930 unsigned Align = MFI.getObjectAlignment(FI);
931
Alex Lorenze40c8a22015-08-11 23:09:45 +0000932 MachineMemOperand *MMO = MF.getMachineMemOperand(
933 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
934 MFI.getObjectSize(FI), Align);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000935
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000936 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000937 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000938 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000939 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000940 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000941 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000942 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000943 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000944 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
945 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
946 BuildMI(MBB, I, DL, get(Hexagon::LDriw_mod), DestReg)
947 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000948 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000949 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai_128B), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000950 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
951 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000952 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000953 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
954 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000955 unsigned Opc = Align < 128 ? Hexagon::PS_vloadrwu_ai_128B
956 : Hexagon::PS_vloadrw_ai_128B;
957 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000958 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
959 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000960 unsigned Opc = Align < 128 ? Hexagon::V6_vL32Ub_ai_128B
961 : Hexagon::V6_vL32b_ai_128B;
962 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000963 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
964 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000965 unsigned Opc = Align < 64 ? Hexagon::V6_vL32Ub_ai
966 : Hexagon::V6_vL32b_ai;
967 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000968 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
969 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000970 unsigned Opc = Align < 64 ? Hexagon::PS_vloadrwu_ai
971 : Hexagon::PS_vloadrw_ai;
972 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000973 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000974 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000975 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000976 }
977}
978
979
Ron Lieberman88159e52016-09-02 22:56:24 +0000980static void getLiveRegsAt(LivePhysRegs &Regs, const MachineInstr &MI) {
981 const MachineBasicBlock &B = *MI.getParent();
982 Regs.addLiveOuts(B);
Duncan P. N. Exon Smith18720962016-09-11 18:51:28 +0000983 auto E = ++MachineBasicBlock::const_iterator(MI.getIterator()).getReverse();
Ron Lieberman88159e52016-09-02 22:56:24 +0000984 for (auto I = B.rbegin(); I != E; ++I)
985 Regs.stepBackward(*I);
986}
987
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000988/// expandPostRAPseudo - This function is called for all pseudo instructions
989/// that remain after register allocation. Many pseudo instructions are
990/// created to help register allocation. This is the place to convert them
991/// into real instructions. The target can edit MI in place, or it can insert
992/// new instructions and erase MI. The function should return true if
993/// anything was changed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000994bool HexagonInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000995 const HexagonRegisterInfo &HRI = getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000996 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
997 MachineBasicBlock &MBB = *MI.getParent();
998 DebugLoc DL = MI.getDebugLoc();
999 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001000 const unsigned VecOffset = 1;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001001
1002 switch (Opc) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001003 case TargetOpcode::COPY: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001004 MachineOperand &MD = MI.getOperand(0);
1005 MachineOperand &MS = MI.getOperand(1);
1006 MachineBasicBlock::iterator MBBI = MI.getIterator();
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001007 if (MD.getReg() != MS.getReg() && !MS.isUndef()) {
1008 copyPhysReg(MBB, MI, DL, MD.getReg(), MS.getReg(), MS.isKill());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001009 std::prev(MBBI)->copyImplicitOps(*MBB.getParent(), MI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001010 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001011 MBB.erase(MBBI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001012 return true;
1013 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001014 case Hexagon::PS_aligna:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001015 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI.getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001016 .addReg(HRI.getFrameRegister())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001017 .addImm(-MI.getOperand(1).getImm());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001018 MBB.erase(MI);
1019 return true;
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001020 case Hexagon::V6_vassignp_128B:
1021 case Hexagon::V6_vassignp: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001022 unsigned SrcReg = MI.getOperand(1).getReg();
1023 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001024 unsigned Kill = getKillRegState(MI.getOperand(1).isKill());
1025 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcombine), DstReg)
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001026 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_hi), Kill)
1027 .addReg(HRI.getSubReg(SrcReg, Hexagon::vsub_lo), Kill);
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001028 MBB.erase(MI);
1029 return true;
1030 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001031 case Hexagon::V6_lo_128B:
1032 case Hexagon::V6_lo: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001033 unsigned SrcReg = MI.getOperand(1).getReg();
1034 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001035 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001036 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001037 MBB.erase(MI);
1038 MRI.clearKillFlags(SrcSubLo);
1039 return true;
1040 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001041 case Hexagon::V6_hi_128B:
1042 case Hexagon::V6_hi: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001043 unsigned SrcReg = MI.getOperand(1).getReg();
1044 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001045 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001046 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001047 MBB.erase(MI);
1048 MRI.clearKillFlags(SrcSubHi);
1049 return true;
1050 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001051 case Hexagon::PS_vstorerw_ai:
1052 case Hexagon::PS_vstorerwu_ai:
1053 case Hexagon::PS_vstorerw_ai_128B:
1054 case Hexagon::PS_vstorerwu_ai_128B: {
1055 bool Is128B = (Opc == Hexagon::PS_vstorerw_ai_128B ||
1056 Opc == Hexagon::PS_vstorerwu_ai_128B);
1057 bool Aligned = (Opc == Hexagon::PS_vstorerw_ai ||
1058 Opc == Hexagon::PS_vstorerw_ai_128B);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001059 unsigned SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001060 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::vsub_hi);
1061 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::vsub_lo);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001062 unsigned NewOpc;
1063 if (Aligned)
1064 NewOpc = Is128B ? Hexagon::V6_vS32b_ai_128B
1065 : Hexagon::V6_vS32b_ai;
1066 else
1067 NewOpc = Is128B ? Hexagon::V6_vS32Ub_ai_128B
1068 : Hexagon::V6_vS32Ub_ai;
1069
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001070 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001071 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001072 BuildMI(MBB, MI, DL, get(NewOpc))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001073 .addOperand(MI.getOperand(0))
1074 .addImm(MI.getOperand(1).getImm())
1075 .addReg(SrcSubLo)
1076 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001077 MI1New->getOperand(0).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001078 BuildMI(MBB, MI, DL, get(NewOpc))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001079 .addOperand(MI.getOperand(0))
1080 // The Vectors are indexed in multiples of vector size.
1081 .addImm(MI.getOperand(1).getImm() + Offset)
1082 .addReg(SrcSubHi)
1083 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001084 MBB.erase(MI);
1085 return true;
1086 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001087 case Hexagon::PS_vloadrw_ai:
1088 case Hexagon::PS_vloadrwu_ai:
1089 case Hexagon::PS_vloadrw_ai_128B:
1090 case Hexagon::PS_vloadrwu_ai_128B: {
1091 bool Is128B = (Opc == Hexagon::PS_vloadrw_ai_128B ||
1092 Opc == Hexagon::PS_vloadrwu_ai_128B);
1093 bool Aligned = (Opc == Hexagon::PS_vloadrw_ai ||
1094 Opc == Hexagon::PS_vloadrw_ai_128B);
1095 unsigned NewOpc;
1096 if (Aligned)
1097 NewOpc = Is128B ? Hexagon::V6_vL32b_ai_128B
1098 : Hexagon::V6_vL32b_ai;
1099 else
1100 NewOpc = Is128B ? Hexagon::V6_vL32Ub_ai_128B
1101 : Hexagon::V6_vL32Ub_ai;
1102
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001103 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001104 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
1105 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001106 BuildMI(MBB, MI, DL, get(NewOpc),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001107 HRI.getSubReg(DstReg, Hexagon::vsub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001108 .addOperand(MI.getOperand(1))
1109 .addImm(MI.getOperand(2).getImm());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001110 MI1New->getOperand(1).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001111 BuildMI(MBB, MI, DL, get(NewOpc),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001112 HRI.getSubReg(DstReg, Hexagon::vsub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001113 .addOperand(MI.getOperand(1))
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001114 // The Vectors are indexed in multiples of vector size.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001115 .addImm(MI.getOperand(2).getImm() + Offset)
1116 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001117 MBB.erase(MI);
1118 return true;
1119 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001120 case Hexagon::PS_true: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001121 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001122 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1123 .addReg(Reg, RegState::Undef)
1124 .addReg(Reg, RegState::Undef);
1125 MBB.erase(MI);
1126 return true;
1127 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001128 case Hexagon::PS_false: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001129 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001130 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1131 .addReg(Reg, RegState::Undef)
1132 .addReg(Reg, RegState::Undef);
1133 MBB.erase(MI);
1134 return true;
1135 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001136 case Hexagon::PS_vmulw: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001137 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001138 unsigned DstReg = MI.getOperand(0).getReg();
1139 unsigned Src1Reg = MI.getOperand(1).getReg();
1140 unsigned Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001141 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1142 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1143 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1144 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001145 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001146 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001147 .addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001148 .addReg(Src2SubHi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001149 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001150 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001151 .addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001152 .addReg(Src2SubLo);
1153 MBB.erase(MI);
1154 MRI.clearKillFlags(Src1SubHi);
1155 MRI.clearKillFlags(Src1SubLo);
1156 MRI.clearKillFlags(Src2SubHi);
1157 MRI.clearKillFlags(Src2SubLo);
1158 return true;
1159 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001160 case Hexagon::PS_vmulw_acc: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001161 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001162 unsigned DstReg = MI.getOperand(0).getReg();
1163 unsigned Src1Reg = MI.getOperand(1).getReg();
1164 unsigned Src2Reg = MI.getOperand(2).getReg();
1165 unsigned Src3Reg = MI.getOperand(3).getReg();
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001166 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::isub_hi);
1167 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::isub_lo);
1168 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::isub_hi);
1169 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::isub_lo);
1170 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::isub_hi);
1171 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::isub_lo);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001172 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001173 HRI.getSubReg(DstReg, Hexagon::isub_hi))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001174 .addReg(Src1SubHi)
1175 .addReg(Src2SubHi)
1176 .addReg(Src3SubHi);
1177 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001178 HRI.getSubReg(DstReg, Hexagon::isub_lo))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001179 .addReg(Src1SubLo)
1180 .addReg(Src2SubLo)
1181 .addReg(Src3SubLo);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001182 MBB.erase(MI);
1183 MRI.clearKillFlags(Src1SubHi);
1184 MRI.clearKillFlags(Src1SubLo);
1185 MRI.clearKillFlags(Src2SubHi);
1186 MRI.clearKillFlags(Src2SubLo);
1187 MRI.clearKillFlags(Src3SubHi);
1188 MRI.clearKillFlags(Src3SubLo);
1189 return true;
1190 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001191 case Hexagon::PS_pselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001192 const MachineOperand &Op0 = MI.getOperand(0);
1193 const MachineOperand &Op1 = MI.getOperand(1);
1194 const MachineOperand &Op2 = MI.getOperand(2);
1195 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001196 unsigned Rd = Op0.getReg();
1197 unsigned Pu = Op1.getReg();
1198 unsigned Rs = Op2.getReg();
1199 unsigned Rt = Op3.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001200 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001201 unsigned K1 = getKillRegState(Op1.isKill());
1202 unsigned K2 = getKillRegState(Op2.isKill());
1203 unsigned K3 = getKillRegState(Op3.isKill());
1204 if (Rd != Rs)
1205 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1206 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1207 .addReg(Rs, K2);
1208 if (Rd != Rt)
1209 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1210 .addReg(Pu, K1)
1211 .addReg(Rt, K3);
1212 MBB.erase(MI);
1213 return true;
1214 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001215 case Hexagon::PS_vselect:
1216 case Hexagon::PS_vselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001217 const MachineOperand &Op0 = MI.getOperand(0);
1218 const MachineOperand &Op1 = MI.getOperand(1);
1219 const MachineOperand &Op2 = MI.getOperand(2);
1220 const MachineOperand &Op3 = MI.getOperand(3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001221 LivePhysRegs LiveAtMI(&HRI);
1222 getLiveRegsAt(LiveAtMI, MI);
1223 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
1224 if (Op0.getReg() != Op2.getReg()) {
1225 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
1226 .addOperand(Op0)
1227 .addOperand(Op1)
1228 .addOperand(Op2);
1229 if (IsDestLive)
1230 T.addReg(Op0.getReg(), RegState::Implicit);
1231 IsDestLive = true;
1232 }
1233 if (Op0.getReg() != Op3.getReg()) {
1234 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
1235 .addOperand(Op0)
1236 .addOperand(Op1)
1237 .addOperand(Op3);
1238 if (IsDestLive)
1239 T.addReg(Op0.getReg(), RegState::Implicit);
1240 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001241 MBB.erase(MI);
1242 return true;
1243 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001244 case Hexagon::PS_wselect:
1245 case Hexagon::PS_wselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001246 MachineOperand &Op0 = MI.getOperand(0);
1247 MachineOperand &Op1 = MI.getOperand(1);
1248 MachineOperand &Op2 = MI.getOperand(2);
1249 MachineOperand &Op3 = MI.getOperand(3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001250 LivePhysRegs LiveAtMI(&HRI);
1251 getLiveRegsAt(LiveAtMI, MI);
1252 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
1253
1254 if (Op0.getReg() != Op2.getReg()) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001255 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_lo);
1256 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001257 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
1258 .addOperand(Op0)
1259 .addOperand(Op1)
1260 .addReg(SrcHi)
1261 .addReg(SrcLo);
1262 if (IsDestLive)
1263 T.addReg(Op0.getReg(), RegState::Implicit);
1264 IsDestLive = true;
1265 }
1266 if (Op0.getReg() != Op3.getReg()) {
Krzysztof Parzyszeka5409972016-11-09 16:19:08 +00001267 unsigned SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_lo);
1268 unsigned SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::vsub_hi);
Ron Lieberman88159e52016-09-02 22:56:24 +00001269 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
1270 .addOperand(Op0)
1271 .addOperand(Op1)
1272 .addReg(SrcHi)
1273 .addReg(SrcLo);
1274 if (IsDestLive)
1275 T.addReg(Op0.getReg(), RegState::Implicit);
1276 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001277 MBB.erase(MI);
1278 return true;
1279 }
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001280 case Hexagon::PS_tailcall_i:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001281 MI.setDesc(get(Hexagon::J2_jump));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001282 return true;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001283 case Hexagon::PS_tailcall_r:
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001284 case Hexagon::PS_jmpret:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001285 MI.setDesc(get(Hexagon::J2_jumpr));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001286 return true;
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001287 case Hexagon::PS_jmprett:
1288 MI.setDesc(get(Hexagon::J2_jumprt));
1289 return true;
1290 case Hexagon::PS_jmpretf:
1291 MI.setDesc(get(Hexagon::J2_jumprf));
1292 return true;
1293 case Hexagon::PS_jmprettnewpt:
1294 MI.setDesc(get(Hexagon::J2_jumprtnewpt));
1295 return true;
1296 case Hexagon::PS_jmpretfnewpt:
1297 MI.setDesc(get(Hexagon::J2_jumprfnewpt));
1298 return true;
1299 case Hexagon::PS_jmprettnew:
1300 MI.setDesc(get(Hexagon::J2_jumprtnew));
1301 return true;
1302 case Hexagon::PS_jmpretfnew:
1303 MI.setDesc(get(Hexagon::J2_jumprfnew));
1304 return true;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001305 }
1306
1307 return false;
1308}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001309
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001310
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001311// We indicate that we want to reverse the branch by
1312// inserting the reversed branching opcode.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001313bool HexagonInstrInfo::reverseBranchCondition(
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001314 SmallVectorImpl<MachineOperand> &Cond) const {
1315 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001316 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001317 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1318 unsigned opcode = Cond[0].getImm();
1319 //unsigned temp;
1320 assert(get(opcode).isBranch() && "Should be a branching condition.");
1321 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001322 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001323 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1324 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001325 return false;
1326}
1327
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001328
1329void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1330 MachineBasicBlock::iterator MI) const {
1331 DebugLoc DL;
1332 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1333}
1334
1335
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001336bool HexagonInstrInfo::isPostIncrement(const MachineInstr &MI) const {
1337 return getAddrMode(MI) == HexagonII::PostInc;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001338}
1339
1340
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001341// Returns true if an instruction is predicated irrespective of the predicate
1342// sense. For example, all of the following will return true.
1343// if (p0) R1 = add(R2, R3)
1344// if (!p0) R1 = add(R2, R3)
1345// if (p0.new) R1 = add(R2, R3)
1346// if (!p0.new) R1 = add(R2, R3)
1347// Note: New-value stores are not included here as in the current
1348// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001349bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1350 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001351 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001352}
1353
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001354
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001355bool HexagonInstrInfo::PredicateInstruction(
1356 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001357 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1358 isEndLoopN(Cond[0].getImm())) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001359 DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001360 return false;
1361 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001362 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001363 assert (isPredicable(MI) && "Expected predicable instruction");
1364 bool invertJump = predOpcodeHasNot(Cond);
1365
1366 // We have to predicate MI "in place", i.e. after this function returns,
1367 // MI will need to be transformed into a predicated form. To avoid com-
1368 // plicated manipulations with the operands (handling tied operands,
1369 // etc.), build a new temporary instruction, then overwrite MI with it.
1370
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001371 MachineBasicBlock &B = *MI.getParent();
1372 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001373 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1374 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001375 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001376 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001377 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001378 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1379 break;
1380 T.addOperand(Op);
1381 NOp++;
1382 }
1383
1384 unsigned PredReg, PredRegPos, PredRegFlags;
1385 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1386 (void)GotPredReg;
1387 assert(GotPredReg);
1388 T.addReg(PredReg, PredRegFlags);
1389 while (NOp < NumOps)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001390 T.addOperand(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001391
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001392 MI.setDesc(get(PredOpc));
1393 while (unsigned n = MI.getNumOperands())
1394 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001395 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001396 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001397
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001398 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001399 B.erase(TI);
1400
1401 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1402 MRI.clearKillFlags(PredReg);
1403 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001404}
1405
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001406
1407bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1408 ArrayRef<MachineOperand> Pred2) const {
1409 // TODO: Fix this
1410 return false;
1411}
1412
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001413
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001414bool HexagonInstrInfo::DefinesPredicate(
1415 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001416 auto &HRI = getRegisterInfo();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001417 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1418 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001419 if (MO.isReg() && MO.isDef()) {
1420 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1421 if (RC == &Hexagon::PredRegsRegClass) {
1422 Pred.push_back(MO);
1423 return true;
1424 }
1425 }
1426 }
1427 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001428}
Andrew Trickd06df962012-02-01 22:13:57 +00001429
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001430
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001431bool HexagonInstrInfo::isPredicable(MachineInstr &MI) const {
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001432 return MI.getDesc().isPredicable();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001433}
1434
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001435bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1436 const MachineBasicBlock *MBB,
1437 const MachineFunction &MF) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001438 // Debug info is never a scheduling boundary. It's necessary to be explicit
1439 // due to the special treatment of IT instructions below, otherwise a
1440 // dbg_value followed by an IT will result in the IT instruction being
1441 // considered a scheduling hazard, which is wrong. It should be the actual
1442 // instruction preceding the dbg_value instruction(s), just like it is
1443 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001444 if (MI.isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001445 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001446
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001447 // Throwing call is a boundary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001448 if (MI.isCall()) {
Krzysztof Parzyszekab9127c2016-08-12 11:01:10 +00001449 // Don't mess around with no return calls.
1450 if (doesNotReturn(MI))
1451 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001452 // If any of the block's successors is a landing pad, this could be a
1453 // throwing call.
1454 for (auto I : MBB->successors())
1455 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001456 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001457 }
1458
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001459 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001460 if (MI.getDesc().isTerminator() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001461 return true;
1462
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001463 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1464 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001465
1466 return false;
1467}
1468
1469
1470/// Measure the specified inline asm to determine an approximation of its
1471/// length.
1472/// Comments (which run till the next SeparatorString or newline) do not
1473/// count as an instruction.
1474/// Any other non-whitespace text is considered an instruction, with
1475/// multiple instructions separated by SeparatorString or newlines.
1476/// Variable-length instructions are not handled here; this function
1477/// may be overloaded in the target code to do that.
1478/// Hexagon counts the number of ##'s and adjust for that many
1479/// constant exenders.
1480unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1481 const MCAsmInfo &MAI) const {
1482 StringRef AStr(Str);
1483 // Count the number of instructions in the asm.
1484 bool atInsnStart = true;
1485 unsigned Length = 0;
1486 for (; *Str; ++Str) {
1487 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1488 strlen(MAI.getSeparatorString())) == 0)
1489 atInsnStart = true;
1490 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1491 Length += MAI.getMaxInstLength();
1492 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001493 }
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001494 if (atInsnStart && strncmp(Str, MAI.getCommentString().data(),
1495 MAI.getCommentString().size()) == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001496 atInsnStart = false;
1497 }
1498
1499 // Add to size number of constant extenders seen * 4.
1500 StringRef Occ("##");
1501 Length += AStr.count(Occ)*4;
1502 return Length;
1503}
1504
1505
1506ScheduleHazardRecognizer*
1507HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1508 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +00001509 if (UseDFAHazardRec) {
1510 auto &HST = DAG->MF.getSubtarget<HexagonSubtarget>();
1511 return new HexagonHazardRecognizer(II, this, HST);
1512 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001513 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1514}
1515
1516
1517/// \brief For a comparison instruction, return the source registers in
1518/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1519/// compares against in CmpValue. Return true if the comparison instruction
1520/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001521bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1522 unsigned &SrcReg2, int &Mask,
1523 int &Value) const {
1524 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001525
1526 // Set mask and the first source register.
1527 switch (Opc) {
1528 case Hexagon::C2_cmpeq:
1529 case Hexagon::C2_cmpeqp:
1530 case Hexagon::C2_cmpgt:
1531 case Hexagon::C2_cmpgtp:
1532 case Hexagon::C2_cmpgtu:
1533 case Hexagon::C2_cmpgtup:
1534 case Hexagon::C4_cmpneq:
1535 case Hexagon::C4_cmplte:
1536 case Hexagon::C4_cmplteu:
1537 case Hexagon::C2_cmpeqi:
1538 case Hexagon::C2_cmpgti:
1539 case Hexagon::C2_cmpgtui:
1540 case Hexagon::C4_cmpneqi:
1541 case Hexagon::C4_cmplteui:
1542 case Hexagon::C4_cmpltei:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001543 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001544 Mask = ~0;
1545 break;
1546 case Hexagon::A4_cmpbeq:
1547 case Hexagon::A4_cmpbgt:
1548 case Hexagon::A4_cmpbgtu:
1549 case Hexagon::A4_cmpbeqi:
1550 case Hexagon::A4_cmpbgti:
1551 case Hexagon::A4_cmpbgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001552 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001553 Mask = 0xFF;
1554 break;
1555 case Hexagon::A4_cmpheq:
1556 case Hexagon::A4_cmphgt:
1557 case Hexagon::A4_cmphgtu:
1558 case Hexagon::A4_cmpheqi:
1559 case Hexagon::A4_cmphgti:
1560 case Hexagon::A4_cmphgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001561 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001562 Mask = 0xFFFF;
1563 break;
1564 }
1565
1566 // Set the value/second source register.
1567 switch (Opc) {
1568 case Hexagon::C2_cmpeq:
1569 case Hexagon::C2_cmpeqp:
1570 case Hexagon::C2_cmpgt:
1571 case Hexagon::C2_cmpgtp:
1572 case Hexagon::C2_cmpgtu:
1573 case Hexagon::C2_cmpgtup:
1574 case Hexagon::A4_cmpbeq:
1575 case Hexagon::A4_cmpbgt:
1576 case Hexagon::A4_cmpbgtu:
1577 case Hexagon::A4_cmpheq:
1578 case Hexagon::A4_cmphgt:
1579 case Hexagon::A4_cmphgtu:
1580 case Hexagon::C4_cmpneq:
1581 case Hexagon::C4_cmplte:
1582 case Hexagon::C4_cmplteu:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001583 SrcReg2 = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001584 return true;
1585
1586 case Hexagon::C2_cmpeqi:
1587 case Hexagon::C2_cmpgtui:
1588 case Hexagon::C2_cmpgti:
1589 case Hexagon::C4_cmpneqi:
1590 case Hexagon::C4_cmplteui:
1591 case Hexagon::C4_cmpltei:
1592 case Hexagon::A4_cmpbeqi:
1593 case Hexagon::A4_cmpbgti:
1594 case Hexagon::A4_cmpbgtui:
1595 case Hexagon::A4_cmpheqi:
1596 case Hexagon::A4_cmphgti:
1597 case Hexagon::A4_cmphgtui:
1598 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001599 Value = MI.getOperand(2).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001600 return true;
1601 }
1602
1603 return false;
1604}
1605
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001606unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001607 const MachineInstr &MI,
1608 unsigned *PredCost) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001609 return getInstrTimingClassLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001610}
1611
1612
1613DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1614 const TargetSubtargetInfo &STI) const {
1615 const InstrItineraryData *II = STI.getInstrItineraryData();
1616 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1617}
1618
1619
1620// Inspired by this pair:
1621// %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1622// S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1623// Currently AA considers the addresses in these instructions to be aliasing.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001624bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1625 MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001626 int OffsetA = 0, OffsetB = 0;
1627 unsigned SizeA = 0, SizeB = 0;
1628
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001629 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1630 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001631 return false;
1632
1633 // Instructions that are pure loads, not loads and stores like memops are not
1634 // dependent.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001635 if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001636 return true;
1637
1638 // Get base, offset, and access size in MIa.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001639 unsigned BaseRegA = getBaseAndOffset(MIa, OffsetA, SizeA);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001640 if (!BaseRegA || !SizeA)
1641 return false;
1642
1643 // Get base, offset, and access size in MIb.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001644 unsigned BaseRegB = getBaseAndOffset(MIb, OffsetB, SizeB);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001645 if (!BaseRegB || !SizeB)
1646 return false;
1647
1648 if (BaseRegA != BaseRegB)
1649 return false;
1650
1651 // This is a mem access with the same base register and known offsets from it.
1652 // Reason about it.
1653 if (OffsetA > OffsetB) {
1654 uint64_t offDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1655 return (SizeB <= offDiff);
1656 } else if (OffsetA < OffsetB) {
1657 uint64_t offDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1658 return (SizeA <= offDiff);
1659 }
1660
1661 return false;
1662}
1663
1664
Brendon Cahoon254f8892016-07-29 16:44:44 +00001665/// If the instruction is an increment of a constant value, return the amount.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001666bool HexagonInstrInfo::getIncrementValue(const MachineInstr &MI,
Brendon Cahoon254f8892016-07-29 16:44:44 +00001667 int &Value) const {
1668 if (isPostIncrement(MI)) {
1669 unsigned AccessSize;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001670 return getBaseAndOffset(MI, Value, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00001671 }
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001672 if (MI.getOpcode() == Hexagon::A2_addi) {
1673 Value = MI.getOperand(2).getImm();
Brendon Cahoon254f8892016-07-29 16:44:44 +00001674 return true;
1675 }
1676
1677 return false;
1678}
1679
1680
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001681unsigned HexagonInstrInfo::createVR(MachineFunction *MF, MVT VT) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001682 MachineRegisterInfo &MRI = MF->getRegInfo();
1683 const TargetRegisterClass *TRC;
1684 if (VT == MVT::i1) {
1685 TRC = &Hexagon::PredRegsRegClass;
1686 } else if (VT == MVT::i32 || VT == MVT::f32) {
1687 TRC = &Hexagon::IntRegsRegClass;
1688 } else if (VT == MVT::i64 || VT == MVT::f64) {
1689 TRC = &Hexagon::DoubleRegsRegClass;
1690 } else {
1691 llvm_unreachable("Cannot handle this register class");
1692 }
1693
1694 unsigned NewReg = MRI.createVirtualRegister(TRC);
1695 return NewReg;
1696}
1697
1698
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001699bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001700 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1701}
1702
1703
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001704bool HexagonInstrInfo::isAccumulator(const MachineInstr &MI) const {
1705 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001706 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1707}
1708
1709
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001710bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
1711 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001712 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1713 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1714
1715 if (!(isTC1(MI))
1716 && !(QII->isTC2Early(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001717 && !(MI.getDesc().mayLoad())
1718 && !(MI.getDesc().mayStore())
1719 && (MI.getDesc().getOpcode() != Hexagon::S2_allocframe)
1720 && (MI.getDesc().getOpcode() != Hexagon::L2_deallocframe)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001721 && !(QII->isMemOp(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001722 && !(MI.isBranch())
1723 && !(MI.isReturn())
1724 && !MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001725 return true;
1726
1727 return false;
1728}
1729
1730
Sanjay Patele4b9f502015-12-07 19:21:39 +00001731// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001732bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
1733 return (getType(MI) == HexagonII::TypeCOMPOUND && MI.isBranch());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001734}
1735
1736
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001737bool HexagonInstrInfo::isCondInst(const MachineInstr &MI) const {
1738 return (MI.isBranch() && isPredicated(MI)) ||
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001739 isConditionalTransfer(MI) ||
1740 isConditionalALU32(MI) ||
1741 isConditionalLoad(MI) ||
1742 // Predicated stores which don't have a .new on any operands.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001743 (MI.mayStore() && isPredicated(MI) && !isNewValueStore(MI) &&
1744 !isPredicatedNew(MI));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001745}
1746
1747
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001748bool HexagonInstrInfo::isConditionalALU32(const MachineInstr &MI) const {
1749 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001750 case Hexagon::A2_paddf:
1751 case Hexagon::A2_paddfnew:
1752 case Hexagon::A2_paddif:
1753 case Hexagon::A2_paddifnew:
1754 case Hexagon::A2_paddit:
1755 case Hexagon::A2_padditnew:
1756 case Hexagon::A2_paddt:
1757 case Hexagon::A2_paddtnew:
1758 case Hexagon::A2_pandf:
1759 case Hexagon::A2_pandfnew:
1760 case Hexagon::A2_pandt:
1761 case Hexagon::A2_pandtnew:
1762 case Hexagon::A2_porf:
1763 case Hexagon::A2_porfnew:
1764 case Hexagon::A2_port:
1765 case Hexagon::A2_portnew:
1766 case Hexagon::A2_psubf:
1767 case Hexagon::A2_psubfnew:
1768 case Hexagon::A2_psubt:
1769 case Hexagon::A2_psubtnew:
1770 case Hexagon::A2_pxorf:
1771 case Hexagon::A2_pxorfnew:
1772 case Hexagon::A2_pxort:
1773 case Hexagon::A2_pxortnew:
1774 case Hexagon::A4_paslhf:
1775 case Hexagon::A4_paslhfnew:
1776 case Hexagon::A4_paslht:
1777 case Hexagon::A4_paslhtnew:
1778 case Hexagon::A4_pasrhf:
1779 case Hexagon::A4_pasrhfnew:
1780 case Hexagon::A4_pasrht:
1781 case Hexagon::A4_pasrhtnew:
1782 case Hexagon::A4_psxtbf:
1783 case Hexagon::A4_psxtbfnew:
1784 case Hexagon::A4_psxtbt:
1785 case Hexagon::A4_psxtbtnew:
1786 case Hexagon::A4_psxthf:
1787 case Hexagon::A4_psxthfnew:
1788 case Hexagon::A4_psxtht:
1789 case Hexagon::A4_psxthtnew:
1790 case Hexagon::A4_pzxtbf:
1791 case Hexagon::A4_pzxtbfnew:
1792 case Hexagon::A4_pzxtbt:
1793 case Hexagon::A4_pzxtbtnew:
1794 case Hexagon::A4_pzxthf:
1795 case Hexagon::A4_pzxthfnew:
1796 case Hexagon::A4_pzxtht:
1797 case Hexagon::A4_pzxthtnew:
1798 case Hexagon::C2_ccombinewf:
1799 case Hexagon::C2_ccombinewt:
1800 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001801 }
1802 return false;
1803}
1804
1805
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001806// FIXME - Function name and it's functionality don't match.
1807// It should be renamed to hasPredNewOpcode()
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001808bool HexagonInstrInfo::isConditionalLoad(const MachineInstr &MI) const {
1809 if (!MI.getDesc().mayLoad() || !isPredicated(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001810 return false;
1811
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001812 int PNewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001813 // Instruction with valid predicated-new opcode can be promoted to .new.
1814 return PNewOpcode >= 0;
1815}
1816
1817
1818// Returns true if an instruction is a conditional store.
1819//
1820// Note: It doesn't include conditional new-value stores as they can't be
1821// converted to .new predicate.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001822bool HexagonInstrInfo::isConditionalStore(const MachineInstr &MI) const {
1823 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001824 default: return false;
1825 case Hexagon::S4_storeirbt_io:
1826 case Hexagon::S4_storeirbf_io:
1827 case Hexagon::S4_pstorerbt_rr:
1828 case Hexagon::S4_pstorerbf_rr:
1829 case Hexagon::S2_pstorerbt_io:
1830 case Hexagon::S2_pstorerbf_io:
1831 case Hexagon::S2_pstorerbt_pi:
1832 case Hexagon::S2_pstorerbf_pi:
1833 case Hexagon::S2_pstorerdt_io:
1834 case Hexagon::S2_pstorerdf_io:
1835 case Hexagon::S4_pstorerdt_rr:
1836 case Hexagon::S4_pstorerdf_rr:
1837 case Hexagon::S2_pstorerdt_pi:
1838 case Hexagon::S2_pstorerdf_pi:
1839 case Hexagon::S2_pstorerht_io:
1840 case Hexagon::S2_pstorerhf_io:
1841 case Hexagon::S4_storeirht_io:
1842 case Hexagon::S4_storeirhf_io:
1843 case Hexagon::S4_pstorerht_rr:
1844 case Hexagon::S4_pstorerhf_rr:
1845 case Hexagon::S2_pstorerht_pi:
1846 case Hexagon::S2_pstorerhf_pi:
1847 case Hexagon::S2_pstorerit_io:
1848 case Hexagon::S2_pstorerif_io:
1849 case Hexagon::S4_storeirit_io:
1850 case Hexagon::S4_storeirif_io:
1851 case Hexagon::S4_pstorerit_rr:
1852 case Hexagon::S4_pstorerif_rr:
1853 case Hexagon::S2_pstorerit_pi:
1854 case Hexagon::S2_pstorerif_pi:
1855
1856 // V4 global address store before promoting to dot new.
1857 case Hexagon::S4_pstorerdt_abs:
1858 case Hexagon::S4_pstorerdf_abs:
1859 case Hexagon::S4_pstorerbt_abs:
1860 case Hexagon::S4_pstorerbf_abs:
1861 case Hexagon::S4_pstorerht_abs:
1862 case Hexagon::S4_pstorerhf_abs:
1863 case Hexagon::S4_pstorerit_abs:
1864 case Hexagon::S4_pstorerif_abs:
1865 return true;
1866
1867 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1868 // from the "Conditional Store" list. Because a predicated new value store
1869 // would NOT be promoted to a double dot new store.
1870 // This function returns yes for those stores that are predicated but not
1871 // yet promoted to predicate dot new instructions.
1872 }
1873}
1874
1875
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001876bool HexagonInstrInfo::isConditionalTransfer(const MachineInstr &MI) const {
1877 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001878 case Hexagon::A2_tfrt:
1879 case Hexagon::A2_tfrf:
1880 case Hexagon::C2_cmoveit:
1881 case Hexagon::C2_cmoveif:
1882 case Hexagon::A2_tfrtnew:
1883 case Hexagon::A2_tfrfnew:
1884 case Hexagon::C2_cmovenewit:
1885 case Hexagon::C2_cmovenewif:
1886 case Hexagon::A2_tfrpt:
1887 case Hexagon::A2_tfrpf:
1888 return true;
1889
1890 default:
1891 return false;
1892 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001893 return false;
1894}
1895
1896
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001897// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1898// isFPImm and later getFPImm as well.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001899bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
1900 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001901 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1902 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001903 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001904
1905 unsigned isExtendable =
1906 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1907 if (!isExtendable)
1908 return false;
1909
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001910 if (MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001911 return false;
1912
1913 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001914 const MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001915 // Use MO operand flags to determine if MO
1916 // has the HMOTF_ConstExtended flag set.
1917 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001918 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001919 // If this is a Machine BB address we are talking about, and it is
1920 // not marked as extended, say so.
1921 if (MO.isMBB())
1922 return false;
1923
1924 // We could be using an instruction with an extendable immediate and shoehorn
1925 // a global address into it. If it is a global address it will be constant
1926 // extended. We do this for COMBINE.
1927 // We currently only handle isGlobal() because it is the only kind of
1928 // object we are going to end up with here for now.
1929 // In the future we probably should add isSymbol(), etc.
1930 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001931 MO.isJTI() || MO.isCPI() || MO.isFPImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001932 return true;
1933
1934 // If the extendable operand is not 'Immediate' type, the instruction should
1935 // have 'isExtended' flag set.
1936 assert(MO.isImm() && "Extendable operand must be Immediate type");
1937
1938 int MinValue = getMinValue(MI);
1939 int MaxValue = getMaxValue(MI);
1940 int ImmValue = MO.getImm();
1941
1942 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001943}
1944
1945
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001946bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
1947 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001948 case Hexagon::L4_return :
1949 case Hexagon::L4_return_t :
1950 case Hexagon::L4_return_f :
1951 case Hexagon::L4_return_tnew_pnt :
1952 case Hexagon::L4_return_fnew_pnt :
1953 case Hexagon::L4_return_tnew_pt :
1954 case Hexagon::L4_return_fnew_pt :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001955 return true;
1956 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001957 return false;
1958}
1959
1960
1961// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001962bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
1963 const MachineInstr &ConsMI) const {
1964 if (!ProdMI.getDesc().getNumDefs())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001965 return false;
1966
1967 auto &HRI = getRegisterInfo();
1968
1969 SmallVector<unsigned, 4> DefsA;
1970 SmallVector<unsigned, 4> DefsB;
1971 SmallVector<unsigned, 8> UsesA;
1972 SmallVector<unsigned, 8> UsesB;
1973
1974 parseOperands(ProdMI, DefsA, UsesA);
1975 parseOperands(ConsMI, DefsB, UsesB);
1976
1977 for (auto &RegA : DefsA)
1978 for (auto &RegB : UsesB) {
1979 // True data dependency.
1980 if (RegA == RegB)
1981 return true;
1982
1983 if (Hexagon::DoubleRegsRegClass.contains(RegA))
1984 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1985 if (RegB == *SubRegs)
1986 return true;
1987
1988 if (Hexagon::DoubleRegsRegClass.contains(RegB))
1989 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1990 if (RegA == *SubRegs)
1991 return true;
1992 }
1993
1994 return false;
1995}
1996
1997
1998// Returns true if the instruction is alread a .cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001999bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
2000 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002001 case Hexagon::V6_vL32b_cur_pi:
2002 case Hexagon::V6_vL32b_cur_ai:
2003 case Hexagon::V6_vL32b_cur_pi_128B:
2004 case Hexagon::V6_vL32b_cur_ai_128B:
2005 return true;
2006 }
2007 return false;
2008}
2009
2010
2011// Returns true, if any one of the operands is a dot new
2012// insn, whether it is predicated dot new or register dot new.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002013bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
2014 if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002015 return true;
2016
2017 return false;
2018}
2019
2020
2021/// Symmetrical. See if these two instructions are fit for duplex pair.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002022bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
2023 const MachineInstr &MIb) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002024 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
2025 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
2026 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
2027}
2028
2029
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002030bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
2031 if (MI.mayLoad() || MI.mayStore() || MI.isCompare())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002032 return true;
2033
2034 // Multiply
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002035 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002036 if (SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23)
2037 return true;
2038 return false;
2039}
2040
2041
2042bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
2043 return (Opcode == Hexagon::ENDLOOP0 ||
2044 Opcode == Hexagon::ENDLOOP1);
2045}
2046
2047
2048bool HexagonInstrInfo::isExpr(unsigned OpType) const {
2049 switch(OpType) {
2050 case MachineOperand::MO_MachineBasicBlock:
2051 case MachineOperand::MO_GlobalAddress:
2052 case MachineOperand::MO_ExternalSymbol:
2053 case MachineOperand::MO_JumpTableIndex:
2054 case MachineOperand::MO_ConstantPoolIndex:
2055 case MachineOperand::MO_BlockAddress:
2056 return true;
2057 default:
2058 return false;
2059 }
2060}
2061
2062
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002063bool HexagonInstrInfo::isExtendable(const MachineInstr &MI) const {
2064 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002065 const uint64_t F = MID.TSFlags;
2066 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
2067 return true;
2068
2069 // TODO: This is largely obsolete now. Will need to be removed
2070 // in consecutive patches.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002071 switch (MI.getOpcode()) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002072 // PS_fi and PS_fia remain special cases.
2073 case Hexagon::PS_fi:
2074 case Hexagon::PS_fia:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002075 return true;
2076 default:
2077 return false;
2078 }
2079 return false;
2080}
2081
2082
2083// This returns true in two cases:
2084// - The OP code itself indicates that this is an extended instruction.
2085// - One of MOs has been marked with HMOTF_ConstExtended flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002086bool HexagonInstrInfo::isExtended(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002087 // First check if this is permanently extended op code.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002088 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002089 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2090 return true;
2091 // Use MO operand flags to determine if one of MI's operands
2092 // has HMOTF_ConstExtended flag set.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002093 for (MachineInstr::const_mop_iterator I = MI.operands_begin(),
2094 E = MI.operands_end(); I != E; ++I) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002095 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
2096 return true;
2097 }
2098 return false;
2099}
2100
2101
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002102bool HexagonInstrInfo::isFloat(const MachineInstr &MI) const {
2103 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002104 const uint64_t F = get(Opcode).TSFlags;
2105 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2106}
2107
2108
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002109// No V60 HVX VMEM with A_INDIRECT.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002110bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr &I,
2111 const MachineInstr &J) const {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002112 if (!isV60VectorInstruction(I))
2113 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002114 if (!I.mayLoad() && !I.mayStore())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002115 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002116 return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002117}
2118
2119
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002120bool HexagonInstrInfo::isIndirectCall(const MachineInstr &MI) const {
2121 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002122 case Hexagon::J2_callr :
2123 case Hexagon::J2_callrf :
2124 case Hexagon::J2_callrt :
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00002125 case Hexagon::PS_call_nr :
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002126 return true;
2127 }
2128 return false;
2129}
2130
2131
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002132bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr &MI) const {
2133 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002134 case Hexagon::L4_return :
2135 case Hexagon::L4_return_t :
2136 case Hexagon::L4_return_f :
2137 case Hexagon::L4_return_fnew_pnt :
2138 case Hexagon::L4_return_fnew_pt :
2139 case Hexagon::L4_return_tnew_pnt :
2140 case Hexagon::L4_return_tnew_pt :
2141 return true;
2142 }
2143 return false;
2144}
2145
2146
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002147bool HexagonInstrInfo::isJumpR(const MachineInstr &MI) const {
2148 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002149 case Hexagon::J2_jumpr :
2150 case Hexagon::J2_jumprt :
2151 case Hexagon::J2_jumprf :
2152 case Hexagon::J2_jumprtnewpt :
2153 case Hexagon::J2_jumprfnewpt :
2154 case Hexagon::J2_jumprtnew :
2155 case Hexagon::J2_jumprfnew :
2156 return true;
2157 }
2158 return false;
2159}
2160
2161
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00002162// Return true if a given MI can accommodate given offset.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002163// Use abs estimate as oppose to the exact number.
2164// TODO: This will need to be changed to use MC level
2165// definition of instruction extendable field size.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002166bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002167 unsigned offset) const {
2168 // This selection of jump instructions matches to that what
2169 // AnalyzeBranch can parse, plus NVJ.
2170 if (isNewValueJump(MI)) // r9:2
2171 return isInt<11>(offset);
2172
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002173 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002174 // Still missing Jump to address condition on register value.
2175 default:
2176 return false;
2177 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2178 case Hexagon::J2_call:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002179 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002180 return isInt<24>(offset);
2181 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2182 case Hexagon::J2_jumpf:
2183 case Hexagon::J2_jumptnew:
2184 case Hexagon::J2_jumptnewpt:
2185 case Hexagon::J2_jumpfnew:
2186 case Hexagon::J2_jumpfnewpt:
2187 case Hexagon::J2_callt:
2188 case Hexagon::J2_callf:
2189 return isInt<17>(offset);
2190 case Hexagon::J2_loop0i:
2191 case Hexagon::J2_loop0iext:
2192 case Hexagon::J2_loop0r:
2193 case Hexagon::J2_loop0rext:
2194 case Hexagon::J2_loop1i:
2195 case Hexagon::J2_loop1iext:
2196 case Hexagon::J2_loop1r:
2197 case Hexagon::J2_loop1rext:
2198 return isInt<9>(offset);
2199 // TODO: Add all the compound branches here. Can we do this in Relation model?
2200 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2201 case Hexagon::J4_cmpeqi_tp1_jump_nt:
2202 return isInt<11>(offset);
2203 }
2204}
2205
2206
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002207bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr &LRMI,
2208 const MachineInstr &ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002209 bool isLate = isLateResultInstr(LRMI);
2210 bool isEarly = isEarlySourceInstr(ESMI);
2211
2212 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002213 DEBUG(LRMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002214 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002215 DEBUG(ESMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002216
2217 if (isLate && isEarly) {
2218 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2219 return true;
2220 }
2221
2222 return false;
2223}
2224
2225
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002226bool HexagonInstrInfo::isLateResultInstr(const MachineInstr &MI) const {
2227 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002228 case TargetOpcode::EXTRACT_SUBREG:
2229 case TargetOpcode::INSERT_SUBREG:
2230 case TargetOpcode::SUBREG_TO_REG:
2231 case TargetOpcode::REG_SEQUENCE:
2232 case TargetOpcode::IMPLICIT_DEF:
2233 case TargetOpcode::COPY:
2234 case TargetOpcode::INLINEASM:
2235 case TargetOpcode::PHI:
2236 return false;
2237 default:
2238 break;
2239 }
2240
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002241 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002242
2243 switch (SchedClass) {
2244 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2245 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2246 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2247 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2248 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2249 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2250 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2251 case Hexagon::Sched::V2LDST_tc_ld_SLOT01:
2252 case Hexagon::Sched::V2LDST_tc_st_SLOT0:
2253 case Hexagon::Sched::V2LDST_tc_st_SLOT01:
2254 case Hexagon::Sched::V4LDST_tc_ld_SLOT01:
2255 case Hexagon::Sched::V4LDST_tc_st_SLOT0:
2256 case Hexagon::Sched::V4LDST_tc_st_SLOT01:
2257 return false;
2258 }
2259 return true;
2260}
2261
2262
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002263bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002264 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2265 // resource, but all operands can be received late like an ALU instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002266 return MI.getDesc().getSchedClass() == Hexagon::Sched::CVI_VX_LATE;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002267}
2268
2269
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002270bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2271 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002272 return Opcode == Hexagon::J2_loop0i ||
2273 Opcode == Hexagon::J2_loop0r ||
2274 Opcode == Hexagon::J2_loop0iext ||
2275 Opcode == Hexagon::J2_loop0rext ||
2276 Opcode == Hexagon::J2_loop1i ||
2277 Opcode == Hexagon::J2_loop1r ||
2278 Opcode == Hexagon::J2_loop1iext ||
2279 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002280}
2281
2282
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002283bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2284 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002285 default: return false;
2286 case Hexagon::L4_iadd_memopw_io :
2287 case Hexagon::L4_isub_memopw_io :
2288 case Hexagon::L4_add_memopw_io :
2289 case Hexagon::L4_sub_memopw_io :
2290 case Hexagon::L4_and_memopw_io :
2291 case Hexagon::L4_or_memopw_io :
2292 case Hexagon::L4_iadd_memoph_io :
2293 case Hexagon::L4_isub_memoph_io :
2294 case Hexagon::L4_add_memoph_io :
2295 case Hexagon::L4_sub_memoph_io :
2296 case Hexagon::L4_and_memoph_io :
2297 case Hexagon::L4_or_memoph_io :
2298 case Hexagon::L4_iadd_memopb_io :
2299 case Hexagon::L4_isub_memopb_io :
2300 case Hexagon::L4_add_memopb_io :
2301 case Hexagon::L4_sub_memopb_io :
2302 case Hexagon::L4_and_memopb_io :
2303 case Hexagon::L4_or_memopb_io :
2304 case Hexagon::L4_ior_memopb_io:
2305 case Hexagon::L4_ior_memoph_io:
2306 case Hexagon::L4_ior_memopw_io:
2307 case Hexagon::L4_iand_memopb_io:
2308 case Hexagon::L4_iand_memoph_io:
2309 case Hexagon::L4_iand_memopw_io:
2310 return true;
2311 }
2312 return false;
2313}
2314
2315
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002316bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2317 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002318 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2319}
2320
2321
2322bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2323 const uint64_t F = get(Opcode).TSFlags;
2324 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2325}
2326
2327
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002328bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002329 return isNewValueJump(MI) || isNewValueStore(MI);
2330}
2331
2332
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002333bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2334 return isNewValue(MI) && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002335}
2336
2337
2338bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2339 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2340}
2341
2342
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002343bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2344 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002345 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2346}
2347
2348
2349bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2350 const uint64_t F = get(Opcode).TSFlags;
2351 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2352}
2353
2354
2355// Returns true if a particular operand is extendable for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002356bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002357 unsigned OperandNum) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002358 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002359 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2360 == OperandNum;
2361}
2362
2363
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002364bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2365 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002366 assert(isPredicated(MI));
2367 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2368}
2369
2370
2371bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2372 const uint64_t F = get(Opcode).TSFlags;
2373 assert(isPredicated(Opcode));
2374 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2375}
2376
2377
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002378bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2379 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002380 return !((F >> HexagonII::PredicatedFalsePos) &
2381 HexagonII::PredicatedFalseMask);
2382}
2383
2384
2385bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2386 const uint64_t F = get(Opcode).TSFlags;
2387 // Make sure that the instruction is predicated.
2388 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2389 return !((F >> HexagonII::PredicatedFalsePos) &
2390 HexagonII::PredicatedFalseMask);
2391}
2392
2393
2394bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2395 const uint64_t F = get(Opcode).TSFlags;
2396 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2397}
2398
2399
2400bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2401 const uint64_t F = get(Opcode).TSFlags;
2402 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2403}
2404
2405
2406bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2407 const uint64_t F = get(Opcode).TSFlags;
2408 assert(get(Opcode).isBranch() &&
2409 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2410 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2411}
2412
2413
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002414bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2415 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2416 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2417 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2418 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002419}
2420
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002421bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2422 switch (MI.getOpcode()) {
2423 // Byte
2424 case Hexagon::L2_loadrb_io:
2425 case Hexagon::L4_loadrb_ur:
2426 case Hexagon::L4_loadrb_ap:
2427 case Hexagon::L2_loadrb_pr:
2428 case Hexagon::L2_loadrb_pbr:
2429 case Hexagon::L2_loadrb_pi:
2430 case Hexagon::L2_loadrb_pci:
2431 case Hexagon::L2_loadrb_pcr:
2432 case Hexagon::L2_loadbsw2_io:
2433 case Hexagon::L4_loadbsw2_ur:
2434 case Hexagon::L4_loadbsw2_ap:
2435 case Hexagon::L2_loadbsw2_pr:
2436 case Hexagon::L2_loadbsw2_pbr:
2437 case Hexagon::L2_loadbsw2_pi:
2438 case Hexagon::L2_loadbsw2_pci:
2439 case Hexagon::L2_loadbsw2_pcr:
2440 case Hexagon::L2_loadbsw4_io:
2441 case Hexagon::L4_loadbsw4_ur:
2442 case Hexagon::L4_loadbsw4_ap:
2443 case Hexagon::L2_loadbsw4_pr:
2444 case Hexagon::L2_loadbsw4_pbr:
2445 case Hexagon::L2_loadbsw4_pi:
2446 case Hexagon::L2_loadbsw4_pci:
2447 case Hexagon::L2_loadbsw4_pcr:
2448 case Hexagon::L4_loadrb_rr:
2449 case Hexagon::L2_ploadrbt_io:
2450 case Hexagon::L2_ploadrbt_pi:
2451 case Hexagon::L2_ploadrbf_io:
2452 case Hexagon::L2_ploadrbf_pi:
2453 case Hexagon::L2_ploadrbtnew_io:
2454 case Hexagon::L2_ploadrbfnew_io:
2455 case Hexagon::L4_ploadrbt_rr:
2456 case Hexagon::L4_ploadrbf_rr:
2457 case Hexagon::L4_ploadrbtnew_rr:
2458 case Hexagon::L4_ploadrbfnew_rr:
2459 case Hexagon::L2_ploadrbtnew_pi:
2460 case Hexagon::L2_ploadrbfnew_pi:
2461 case Hexagon::L4_ploadrbt_abs:
2462 case Hexagon::L4_ploadrbf_abs:
2463 case Hexagon::L4_ploadrbtnew_abs:
2464 case Hexagon::L4_ploadrbfnew_abs:
2465 case Hexagon::L2_loadrbgp:
2466 // Half
2467 case Hexagon::L2_loadrh_io:
2468 case Hexagon::L4_loadrh_ur:
2469 case Hexagon::L4_loadrh_ap:
2470 case Hexagon::L2_loadrh_pr:
2471 case Hexagon::L2_loadrh_pbr:
2472 case Hexagon::L2_loadrh_pi:
2473 case Hexagon::L2_loadrh_pci:
2474 case Hexagon::L2_loadrh_pcr:
2475 case Hexagon::L4_loadrh_rr:
2476 case Hexagon::L2_ploadrht_io:
2477 case Hexagon::L2_ploadrht_pi:
2478 case Hexagon::L2_ploadrhf_io:
2479 case Hexagon::L2_ploadrhf_pi:
2480 case Hexagon::L2_ploadrhtnew_io:
2481 case Hexagon::L2_ploadrhfnew_io:
2482 case Hexagon::L4_ploadrht_rr:
2483 case Hexagon::L4_ploadrhf_rr:
2484 case Hexagon::L4_ploadrhtnew_rr:
2485 case Hexagon::L4_ploadrhfnew_rr:
2486 case Hexagon::L2_ploadrhtnew_pi:
2487 case Hexagon::L2_ploadrhfnew_pi:
2488 case Hexagon::L4_ploadrht_abs:
2489 case Hexagon::L4_ploadrhf_abs:
2490 case Hexagon::L4_ploadrhtnew_abs:
2491 case Hexagon::L4_ploadrhfnew_abs:
2492 case Hexagon::L2_loadrhgp:
2493 return true;
2494 default:
2495 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002496 }
2497}
2498
2499
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002500bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2501 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002502 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2503}
2504
2505
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002506bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2507 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002508 case Hexagon::STriw_pred :
2509 case Hexagon::LDriw_pred :
2510 return true;
2511 default:
2512 return false;
2513 }
2514}
2515
2516
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002517bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2518 if (!MI.isBranch())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002519 return false;
2520
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002521 for (auto &Op : MI.operands())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002522 if (Op.isGlobal() || Op.isSymbol())
2523 return true;
2524 return false;
2525}
2526
2527
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002528// Returns true when SU has a timing class TC1.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002529bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2530 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002531 switch (SchedClass) {
2532 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2533 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2534 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2535 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2536 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2537 //case Hexagon::Sched::M_tc_1_SLOT23:
2538 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2539 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2540 return true;
2541
2542 default:
2543 return false;
2544 }
2545}
2546
2547
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002548bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2549 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002550 switch (SchedClass) {
2551 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
2552 case Hexagon::Sched::ALU64_tc_2_SLOT23:
2553 case Hexagon::Sched::CR_tc_2_SLOT3:
2554 case Hexagon::Sched::M_tc_2_SLOT23:
2555 case Hexagon::Sched::S_2op_tc_2_SLOT23:
2556 case Hexagon::Sched::S_3op_tc_2_SLOT23:
2557 return true;
2558
2559 default:
2560 return false;
2561 }
2562}
2563
2564
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002565bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2566 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002567 switch (SchedClass) {
2568 case Hexagon::Sched::ALU32_2op_tc_2early_SLOT0123:
2569 case Hexagon::Sched::ALU32_3op_tc_2early_SLOT0123:
2570 case Hexagon::Sched::ALU64_tc_2early_SLOT23:
2571 case Hexagon::Sched::CR_tc_2early_SLOT23:
2572 case Hexagon::Sched::CR_tc_2early_SLOT3:
2573 case Hexagon::Sched::J_tc_2early_SLOT0123:
2574 case Hexagon::Sched::J_tc_2early_SLOT2:
2575 case Hexagon::Sched::J_tc_2early_SLOT23:
2576 case Hexagon::Sched::S_2op_tc_2early_SLOT23:
2577 case Hexagon::Sched::S_3op_tc_2early_SLOT23:
2578 return true;
2579
2580 default:
2581 return false;
2582 }
2583}
2584
2585
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002586bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2587 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002588 return SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23;
2589}
2590
2591
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002592// Schedule this ASAP.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002593bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2594 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002595 if (mayBeCurLoad(MI1)) {
2596 // if (result of SU is used in Next) return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002597 unsigned DstReg = MI1.getOperand(0).getReg();
2598 int N = MI2.getNumOperands();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002599 for (int I = 0; I < N; I++)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002600 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002601 return true;
2602 }
2603 if (mayBeNewStore(MI2))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002604 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2605 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2606 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002607 return true;
2608 return false;
2609}
2610
2611
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002612bool HexagonInstrInfo::isV60VectorInstruction(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002613 const uint64_t V = getType(MI);
2614 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2615}
2616
2617
2618// Check if the Offset is a valid auto-inc imm by Load/Store Type.
2619//
2620bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2621 if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2622 VT == MVT::v32i16 || VT == MVT::v64i8) {
2623 return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2624 Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2625 (Offset & 0x3f) == 0);
2626 }
2627 // 128B
2628 if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2629 VT == MVT::v64i16 || VT == MVT::v128i8) {
2630 return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2631 Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2632 (Offset & 0x7f) == 0);
2633 }
2634 if (VT == MVT::i64) {
2635 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2636 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2637 (Offset & 0x7) == 0);
2638 }
2639 if (VT == MVT::i32) {
2640 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2641 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2642 (Offset & 0x3) == 0);
2643 }
2644 if (VT == MVT::i16) {
2645 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2646 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2647 (Offset & 0x1) == 0);
2648 }
2649 if (VT == MVT::i8) {
2650 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2651 Offset <= Hexagon_MEMB_AUTOINC_MAX);
2652 }
2653 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002654}
2655
2656
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002657bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2658 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002659 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002660 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002661 // inserted to calculate the final address. Due to this reason, the function
2662 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002663 // We used to assert if the offset was not properly aligned, however,
2664 // there are cases where a misaligned pointer recast can cause this
2665 // problem, and we need to allow for it. The front end warns of such
2666 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002667
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002668 switch (Opcode) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002669 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002670 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002671 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002672 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002673 case Hexagon::V6_vL32b_ai:
2674 case Hexagon::V6_vS32b_ai:
2675 case Hexagon::V6_vL32Ub_ai:
2676 case Hexagon::V6_vS32Ub_ai:
2677 return (Offset >= Hexagon_MEMV_OFFSET_MIN) &&
2678 (Offset <= Hexagon_MEMV_OFFSET_MAX);
2679
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002680 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002681 case Hexagon::PS_vstorerw_ai_128B:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002682 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002683 case Hexagon::PS_vloadrw_ai_128B:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002684 case Hexagon::V6_vL32b_ai_128B:
2685 case Hexagon::V6_vS32b_ai_128B:
2686 case Hexagon::V6_vL32Ub_ai_128B:
2687 case Hexagon::V6_vS32Ub_ai_128B:
2688 return (Offset >= Hexagon_MEMV_OFFSET_MIN_128B) &&
2689 (Offset <= Hexagon_MEMV_OFFSET_MAX_128B);
2690
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002691 case Hexagon::J2_loop0i:
2692 case Hexagon::J2_loop1i:
2693 return isUInt<10>(Offset);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00002694
2695 case Hexagon::S4_storeirb_io:
2696 case Hexagon::S4_storeirbt_io:
2697 case Hexagon::S4_storeirbf_io:
2698 return isUInt<6>(Offset);
2699
2700 case Hexagon::S4_storeirh_io:
2701 case Hexagon::S4_storeirht_io:
2702 case Hexagon::S4_storeirhf_io:
2703 return isShiftedUInt<6,1>(Offset);
2704
2705 case Hexagon::S4_storeiri_io:
2706 case Hexagon::S4_storeirit_io:
2707 case Hexagon::S4_storeirif_io:
2708 return isShiftedUInt<6,2>(Offset);
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002709 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002710
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002711 if (Extend)
2712 return true;
2713
2714 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002715 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002716 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002717 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2718 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2719
Colin LeMahieu947cd702014-12-23 20:44:59 +00002720 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002721 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002722 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2723 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2724
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002725 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002726 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002727 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002728 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2729 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2730
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002731 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002732 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002733 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002734 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2735 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2736
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002737 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002738 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2739 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2740
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002741 case Hexagon::L4_iadd_memopw_io :
2742 case Hexagon::L4_isub_memopw_io :
2743 case Hexagon::L4_add_memopw_io :
2744 case Hexagon::L4_sub_memopw_io :
2745 case Hexagon::L4_and_memopw_io :
2746 case Hexagon::L4_or_memopw_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002747 return (0 <= Offset && Offset <= 255);
2748
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002749 case Hexagon::L4_iadd_memoph_io :
2750 case Hexagon::L4_isub_memoph_io :
2751 case Hexagon::L4_add_memoph_io :
2752 case Hexagon::L4_sub_memoph_io :
2753 case Hexagon::L4_and_memoph_io :
2754 case Hexagon::L4_or_memoph_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002755 return (0 <= Offset && Offset <= 127);
2756
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002757 case Hexagon::L4_iadd_memopb_io :
2758 case Hexagon::L4_isub_memopb_io :
2759 case Hexagon::L4_add_memopb_io :
2760 case Hexagon::L4_sub_memopb_io :
2761 case Hexagon::L4_and_memopb_io :
2762 case Hexagon::L4_or_memopb_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002763 return (0 <= Offset && Offset <= 63);
2764
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002765 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002766 // any size. Later pass knows how to handle it.
2767 case Hexagon::STriw_pred:
2768 case Hexagon::LDriw_pred:
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00002769 case Hexagon::STriw_mod:
2770 case Hexagon::LDriw_mod:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002771 return true;
2772
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002773 case Hexagon::PS_fi:
2774 case Hexagon::PS_fia:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002775 case Hexagon::INLINEASM:
2776 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002777
2778 case Hexagon::L2_ploadrbt_io:
2779 case Hexagon::L2_ploadrbf_io:
2780 case Hexagon::L2_ploadrubt_io:
2781 case Hexagon::L2_ploadrubf_io:
2782 case Hexagon::S2_pstorerbt_io:
2783 case Hexagon::S2_pstorerbf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002784 return isUInt<6>(Offset);
2785
2786 case Hexagon::L2_ploadrht_io:
2787 case Hexagon::L2_ploadrhf_io:
2788 case Hexagon::L2_ploadruht_io:
2789 case Hexagon::L2_ploadruhf_io:
2790 case Hexagon::S2_pstorerht_io:
2791 case Hexagon::S2_pstorerhf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002792 return isShiftedUInt<6,1>(Offset);
2793
2794 case Hexagon::L2_ploadrit_io:
2795 case Hexagon::L2_ploadrif_io:
2796 case Hexagon::S2_pstorerit_io:
2797 case Hexagon::S2_pstorerif_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002798 return isShiftedUInt<6,2>(Offset);
2799
2800 case Hexagon::L2_ploadrdt_io:
2801 case Hexagon::L2_ploadrdf_io:
2802 case Hexagon::S2_pstorerdt_io:
2803 case Hexagon::S2_pstorerdf_io:
2804 return isShiftedUInt<6,3>(Offset);
2805 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002806
Benjamin Kramerb6684012011-12-27 11:41:05 +00002807 llvm_unreachable("No offset range is defined for this opcode. "
2808 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002809}
2810
2811
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002812bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
2813 return isV60VectorInstruction(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002814}
2815
2816
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002817bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2818 const uint64_t F = get(MI.getOpcode()).TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002819 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2820 return
2821 V == HexagonII::TypeCVI_VA ||
2822 V == HexagonII::TypeCVI_VA_DV;
2823}
Andrew Trickd06df962012-02-01 22:13:57 +00002824
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002825
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002826bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2827 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002828 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2829 return true;
2830
2831 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2832 return true;
2833
2834 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002835 return true;
2836
2837 return false;
2838}
Jyotsna Verma84256432013-03-01 17:37:13 +00002839
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002840bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2841 switch (MI.getOpcode()) {
2842 // Byte
2843 case Hexagon::L2_loadrub_io:
2844 case Hexagon::L4_loadrub_ur:
2845 case Hexagon::L4_loadrub_ap:
2846 case Hexagon::L2_loadrub_pr:
2847 case Hexagon::L2_loadrub_pbr:
2848 case Hexagon::L2_loadrub_pi:
2849 case Hexagon::L2_loadrub_pci:
2850 case Hexagon::L2_loadrub_pcr:
2851 case Hexagon::L2_loadbzw2_io:
2852 case Hexagon::L4_loadbzw2_ur:
2853 case Hexagon::L4_loadbzw2_ap:
2854 case Hexagon::L2_loadbzw2_pr:
2855 case Hexagon::L2_loadbzw2_pbr:
2856 case Hexagon::L2_loadbzw2_pi:
2857 case Hexagon::L2_loadbzw2_pci:
2858 case Hexagon::L2_loadbzw2_pcr:
2859 case Hexagon::L2_loadbzw4_io:
2860 case Hexagon::L4_loadbzw4_ur:
2861 case Hexagon::L4_loadbzw4_ap:
2862 case Hexagon::L2_loadbzw4_pr:
2863 case Hexagon::L2_loadbzw4_pbr:
2864 case Hexagon::L2_loadbzw4_pi:
2865 case Hexagon::L2_loadbzw4_pci:
2866 case Hexagon::L2_loadbzw4_pcr:
2867 case Hexagon::L4_loadrub_rr:
2868 case Hexagon::L2_ploadrubt_io:
2869 case Hexagon::L2_ploadrubt_pi:
2870 case Hexagon::L2_ploadrubf_io:
2871 case Hexagon::L2_ploadrubf_pi:
2872 case Hexagon::L2_ploadrubtnew_io:
2873 case Hexagon::L2_ploadrubfnew_io:
2874 case Hexagon::L4_ploadrubt_rr:
2875 case Hexagon::L4_ploadrubf_rr:
2876 case Hexagon::L4_ploadrubtnew_rr:
2877 case Hexagon::L4_ploadrubfnew_rr:
2878 case Hexagon::L2_ploadrubtnew_pi:
2879 case Hexagon::L2_ploadrubfnew_pi:
2880 case Hexagon::L4_ploadrubt_abs:
2881 case Hexagon::L4_ploadrubf_abs:
2882 case Hexagon::L4_ploadrubtnew_abs:
2883 case Hexagon::L4_ploadrubfnew_abs:
2884 case Hexagon::L2_loadrubgp:
2885 // Half
2886 case Hexagon::L2_loadruh_io:
2887 case Hexagon::L4_loadruh_ur:
2888 case Hexagon::L4_loadruh_ap:
2889 case Hexagon::L2_loadruh_pr:
2890 case Hexagon::L2_loadruh_pbr:
2891 case Hexagon::L2_loadruh_pi:
2892 case Hexagon::L2_loadruh_pci:
2893 case Hexagon::L2_loadruh_pcr:
2894 case Hexagon::L4_loadruh_rr:
2895 case Hexagon::L2_ploadruht_io:
2896 case Hexagon::L2_ploadruht_pi:
2897 case Hexagon::L2_ploadruhf_io:
2898 case Hexagon::L2_ploadruhf_pi:
2899 case Hexagon::L2_ploadruhtnew_io:
2900 case Hexagon::L2_ploadruhfnew_io:
2901 case Hexagon::L4_ploadruht_rr:
2902 case Hexagon::L4_ploadruhf_rr:
2903 case Hexagon::L4_ploadruhtnew_rr:
2904 case Hexagon::L4_ploadruhfnew_rr:
2905 case Hexagon::L2_ploadruhtnew_pi:
2906 case Hexagon::L2_ploadruhfnew_pi:
2907 case Hexagon::L4_ploadruht_abs:
2908 case Hexagon::L4_ploadruhf_abs:
2909 case Hexagon::L4_ploadruhtnew_abs:
2910 case Hexagon::L4_ploadruhfnew_abs:
2911 case Hexagon::L2_loadruhgp:
2912 return true;
2913 default:
2914 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002915 }
2916}
2917
2918
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002919// Add latency to instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002920bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2921 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002922 if (isV60VectorInstruction(MI1) && isV60VectorInstruction(MI2))
2923 if (!isVecUsableNextPacket(MI1, MI2))
2924 return true;
2925 return false;
2926}
2927
2928
Brendon Cahoon254f8892016-07-29 16:44:44 +00002929/// \brief Get the base register and byte offset of a load/store instr.
2930bool HexagonInstrInfo::getMemOpBaseRegImmOfs(MachineInstr &LdSt,
2931 unsigned &BaseReg, int64_t &Offset, const TargetRegisterInfo *TRI)
2932 const {
2933 unsigned AccessSize = 0;
2934 int OffsetVal = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002935 BaseReg = getBaseAndOffset(LdSt, OffsetVal, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002936 Offset = OffsetVal;
2937 return BaseReg != 0;
2938}
2939
2940
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002941/// \brief Can these instructions execute at the same time in a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002942bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2943 const MachineInstr &Second) const {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002944 if (DisableNVSchedule)
2945 return false;
2946 if (mayBeNewStore(Second)) {
2947 // Make sure the definition of the first instruction is the value being
2948 // stored.
2949 const MachineOperand &Stored =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002950 Second.getOperand(Second.getNumOperands() - 1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002951 if (!Stored.isReg())
2952 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002953 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
2954 const MachineOperand &Op = First.getOperand(i);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002955 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2956 return true;
2957 }
2958 }
2959 return false;
2960}
2961
2962
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002963bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
2964 unsigned Opc = CallMI.getOpcode();
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002965 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002966}
2967
2968
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002969bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2970 for (auto &I : *B)
2971 if (I.isEHLabel())
2972 return true;
2973 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002974}
2975
Jyotsna Verma84256432013-03-01 17:37:13 +00002976
2977// Returns true if an instruction can be converted into a non-extended
2978// equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002979bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002980 short NonExtOpcode;
2981 // Check if the instruction has a register form that uses register in place
2982 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002983 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
Jyotsna Verma84256432013-03-01 17:37:13 +00002984 return true;
2985
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002986 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002987 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002988
2989 switch (getAddrMode(MI)) {
2990 case HexagonII::Absolute :
2991 // Load/store with absolute addressing mode can be converted into
2992 // base+offset mode.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002993 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002994 break;
2995 case HexagonII::BaseImmOffset :
2996 // Load/store with base+offset addressing mode can be converted into
2997 // base+register offset addressing mode. However left shift operand should
2998 // be set to 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002999 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00003000 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003001 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003002 NonExtOpcode = Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003003 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00003004 default:
3005 return false;
3006 }
3007 if (NonExtOpcode < 0)
3008 return false;
3009 return true;
3010 }
3011 return false;
3012}
3013
Jyotsna Verma84256432013-03-01 17:37:13 +00003014
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003015bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
3016 return Hexagon::getRealHWInstr(MI.getOpcode(),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003017 Hexagon::InstrType_Pseudo) >= 0;
3018}
3019
3020
3021bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
3022 const {
3023 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
3024 while (I != E) {
3025 if (I->isBarrier())
3026 return true;
3027 ++I;
3028 }
3029 return false;
3030}
3031
3032
3033// Returns true, if a LD insn can be promoted to a cur load.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003034bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
3035 auto &HST = MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>();
3036 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003037 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
3038 HST.hasV60TOps();
3039}
3040
3041
3042// Returns true, if a ST insn can be promoted to a new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003043bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
3044 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003045 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
3046}
3047
3048
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003049bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
3050 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003051 // There is no stall when ProdMI is not a V60 vector.
3052 if (!isV60VectorInstruction(ProdMI))
3053 return false;
3054
3055 // There is no stall when ProdMI and ConsMI are not dependent.
3056 if (!isDependent(ProdMI, ConsMI))
3057 return false;
3058
3059 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
3060 // are scheduled in consecutive packets.
3061 if (isVecUsableNextPacket(ProdMI, ConsMI))
3062 return false;
3063
3064 return true;
3065}
3066
3067
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003068bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003069 MachineBasicBlock::const_instr_iterator BII) const {
3070 // There is no stall when I is not a V60 vector.
3071 if (!isV60VectorInstruction(MI))
3072 return false;
3073
3074 MachineBasicBlock::const_instr_iterator MII = BII;
3075 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
3076
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003077 if (!MII->isBundle()) {
3078 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003079 if (!isV60VectorInstruction(J))
3080 return false;
3081 else if (isVecUsableNextPacket(J, MI))
3082 return false;
3083 return true;
3084 }
3085
3086 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003087 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003088 if (producesStall(J, MI))
3089 return true;
3090 }
3091 return false;
3092}
3093
3094
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003095bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003096 unsigned PredReg) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003097 for (unsigned opNum = 0; opNum < MI.getNumOperands(); opNum++) {
3098 const MachineOperand &MO = MI.getOperand(opNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003099 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
3100 return false; // Predicate register must be explicitly defined.
3101 }
3102
3103 // Hexagon Programmer's Reference says that decbin, memw_locked, and
3104 // memd_locked cannot be used as .new as well,
3105 // but we don't seem to have these instructions defined.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003106 return MI.getOpcode() != Hexagon::A4_tlbmatch;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003107}
3108
3109
3110bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
3111 return (Opcode == Hexagon::J2_jumpt) ||
3112 (Opcode == Hexagon::J2_jumpf) ||
3113 (Opcode == Hexagon::J2_jumptnew) ||
3114 (Opcode == Hexagon::J2_jumpfnew) ||
3115 (Opcode == Hexagon::J2_jumptnewpt) ||
3116 (Opcode == Hexagon::J2_jumpfnewpt);
3117}
3118
3119
3120bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
3121 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
3122 return false;
3123 return !isPredicatedTrue(Cond[0].getImm());
3124}
3125
3126
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003127short HexagonInstrInfo::getAbsoluteForm(const MachineInstr &MI) const {
3128 return Hexagon::getAbsoluteForm(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003129}
3130
3131
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003132unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
3133 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003134 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3135}
3136
3137
3138// Returns the base register in a memory access (load/store). The offset is
3139// returned in Offset and the access size is returned in AccessSize.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003140unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003141 int &Offset, unsigned &AccessSize) const {
3142 // Return if it is not a base+offset type instruction or a MemOp.
3143 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3144 getAddrMode(MI) != HexagonII::BaseLongOffset &&
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003145 !isMemOp(MI) && !isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003146 return 0;
3147
3148 // Since it is a memory access instruction, getMemAccessSize() should never
3149 // return 0.
3150 assert (getMemAccessSize(MI) &&
3151 "BaseImmOffset or BaseLongOffset or MemOp without accessSize");
3152
3153 // Return Values of getMemAccessSize() are
3154 // 0 - Checked in the assert above.
3155 // 1, 2, 3, 4 & 7, 8 - The statement below is correct for all these.
3156 // MemAccessSize is represented as 1+log2(N) where N is size in bits.
3157 AccessSize = (1U << (getMemAccessSize(MI) - 1));
3158
3159 unsigned basePos = 0, offsetPos = 0;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003160 if (!getBaseAndOffsetPosition(MI, basePos, offsetPos))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003161 return 0;
3162
3163 // Post increment updates its EA after the mem access,
3164 // so we need to treat its offset as zero.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003165 if (isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003166 Offset = 0;
3167 else {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003168 Offset = MI.getOperand(offsetPos).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003169 }
3170
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003171 return MI.getOperand(basePos).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003172}
3173
3174
3175/// Return the position of the base and offset operands for this instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003176bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003177 unsigned &BasePos, unsigned &OffsetPos) const {
3178 // Deal with memops first.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003179 if (isMemOp(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003180 BasePos = 0;
3181 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003182 } else if (MI.mayStore()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003183 BasePos = 0;
3184 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003185 } else if (MI.mayLoad()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003186 BasePos = 1;
3187 OffsetPos = 2;
3188 } else
3189 return false;
3190
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003191 if (isPredicated(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003192 BasePos++;
3193 OffsetPos++;
3194 }
3195 if (isPostIncrement(MI)) {
3196 BasePos++;
3197 OffsetPos++;
3198 }
3199
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003200 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003201 return false;
3202
3203 return true;
3204}
3205
3206
Simon Pilgrim6ba672e2016-11-17 19:21:20 +00003207// Inserts branching instructions in reverse order of their occurrence.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003208// e.g. jump_t t1 (i1)
3209// jump t2 (i2)
3210// Jumpers = {i2, i1}
3211SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3212 MachineBasicBlock& MBB) const {
3213 SmallVector<MachineInstr*, 2> Jumpers;
3214 // If the block has no terminators, it just falls into the block after it.
3215 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3216 if (I == MBB.instr_begin())
3217 return Jumpers;
3218
3219 // A basic block may looks like this:
3220 //
3221 // [ insn
3222 // EH_LABEL
3223 // insn
3224 // insn
3225 // insn
3226 // EH_LABEL
3227 // insn ]
3228 //
3229 // It has two succs but does not have a terminator
3230 // Don't know how to handle it.
3231 do {
3232 --I;
3233 if (I->isEHLabel())
3234 return Jumpers;
3235 } while (I != MBB.instr_begin());
3236
3237 I = MBB.instr_end();
3238 --I;
3239
3240 while (I->isDebugValue()) {
3241 if (I == MBB.instr_begin())
3242 return Jumpers;
3243 --I;
3244 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003245 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003246 return Jumpers;
3247
3248 // Get the last instruction in the block.
3249 MachineInstr *LastInst = &*I;
3250 Jumpers.push_back(LastInst);
3251 MachineInstr *SecondLastInst = nullptr;
3252 // Find one more terminator if present.
3253 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003254 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003255 if (!SecondLastInst) {
3256 SecondLastInst = &*I;
3257 Jumpers.push_back(SecondLastInst);
3258 } else // This is a third branch.
3259 return Jumpers;
3260 }
3261 if (I == MBB.instr_begin())
3262 break;
3263 --I;
3264 } while (true);
3265 return Jumpers;
3266}
3267
3268
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003269short HexagonInstrInfo::getBaseWithLongOffset(short Opcode) const {
3270 if (Opcode < 0)
3271 return -1;
3272 return Hexagon::getBaseWithLongOffset(Opcode);
3273}
3274
3275
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003276short HexagonInstrInfo::getBaseWithLongOffset(const MachineInstr &MI) const {
3277 return Hexagon::getBaseWithLongOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003278}
3279
3280
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003281short HexagonInstrInfo::getBaseWithRegOffset(const MachineInstr &MI) const {
3282 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003283}
3284
3285
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003286// Returns Operand Index for the constant extended instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003287unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
3288 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003289 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3290}
3291
3292// See if instruction could potentially be a duplex candidate.
3293// If so, return its group. Zero otherwise.
3294HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003295 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003296 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3297
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003298 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003299 default:
3300 return HexagonII::HCG_None;
3301 //
3302 // Compound pairs.
3303 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3304 // "Rd16=#U6 ; jump #r9:2"
3305 // "Rd16=Rs16 ; jump #r9:2"
3306 //
3307 case Hexagon::C2_cmpeq:
3308 case Hexagon::C2_cmpgt:
3309 case Hexagon::C2_cmpgtu:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003310 DstReg = MI.getOperand(0).getReg();
3311 Src1Reg = MI.getOperand(1).getReg();
3312 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003313 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3314 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3315 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3316 return HexagonII::HCG_A;
3317 break;
3318 case Hexagon::C2_cmpeqi:
3319 case Hexagon::C2_cmpgti:
3320 case Hexagon::C2_cmpgtui:
3321 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003322 DstReg = MI.getOperand(0).getReg();
3323 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003324 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3325 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003326 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3327 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3328 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003329 return HexagonII::HCG_A;
3330 break;
3331 case Hexagon::A2_tfr:
3332 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003333 DstReg = MI.getOperand(0).getReg();
3334 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003335 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3336 return HexagonII::HCG_A;
3337 break;
3338 case Hexagon::A2_tfrsi:
3339 // Rd = #u6
3340 // Do not test for #u6 size since the const is getting extended
3341 // regardless and compound could be formed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003342 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003343 if (isIntRegForSubInst(DstReg))
3344 return HexagonII::HCG_A;
3345 break;
3346 case Hexagon::S2_tstbit_i:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003347 DstReg = MI.getOperand(0).getReg();
3348 Src1Reg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003349 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3350 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003351 MI.getOperand(2).isImm() &&
3352 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003353 return HexagonII::HCG_A;
3354 break;
3355 // The fact that .new form is used pretty much guarantees
3356 // that predicate register will match. Nevertheless,
3357 // there could be some false positives without additional
3358 // checking.
3359 case Hexagon::J2_jumptnew:
3360 case Hexagon::J2_jumpfnew:
3361 case Hexagon::J2_jumptnewpt:
3362 case Hexagon::J2_jumpfnewpt:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003363 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003364 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3365 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3366 return HexagonII::HCG_B;
3367 break;
3368 // Transfer and jump:
3369 // Rd=#U6 ; jump #r9:2
3370 // Rd=Rs ; jump #r9:2
3371 // Do not test for jump range here.
3372 case Hexagon::J2_jump:
3373 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003374 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003375 return HexagonII::HCG_C;
3376 break;
3377 }
3378
3379 return HexagonII::HCG_None;
3380}
3381
3382
3383// Returns -1 when there is no opcode found.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003384unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3385 const MachineInstr &GB) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003386 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3387 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003388 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3389 (GB.getOpcode() != Hexagon::J2_jumptnew))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003390 return -1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003391 unsigned DestReg = GA.getOperand(0).getReg();
3392 if (!GB.readsRegister(DestReg))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003393 return -1;
3394 if (DestReg == Hexagon::P0)
3395 return Hexagon::J4_cmpeqi_tp0_jump_nt;
3396 if (DestReg == Hexagon::P1)
3397 return Hexagon::J4_cmpeqi_tp1_jump_nt;
3398 return -1;
3399}
3400
3401
3402int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3403 enum Hexagon::PredSense inPredSense;
3404 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3405 Hexagon::PredSense_true;
3406 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3407 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3408 return CondOpcode;
3409
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003410 llvm_unreachable("Unexpected predicable instruction");
3411}
3412
3413
3414// Return the cur value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003415int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3416 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003417 default: llvm_unreachable("Unknown .cur type");
3418 case Hexagon::V6_vL32b_pi:
3419 return Hexagon::V6_vL32b_cur_pi;
3420 case Hexagon::V6_vL32b_ai:
3421 return Hexagon::V6_vL32b_cur_ai;
3422 //128B
3423 case Hexagon::V6_vL32b_pi_128B:
3424 return Hexagon::V6_vL32b_cur_pi_128B;
3425 case Hexagon::V6_vL32b_ai_128B:
3426 return Hexagon::V6_vL32b_cur_ai_128B;
3427 }
3428 return 0;
3429}
3430
3431
3432
3433// The diagram below shows the steps involved in the conversion of a predicated
3434// store instruction to its .new predicated new-value form.
3435//
3436// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3437// ^ ^
3438// / \ (not OK. it will cause new-value store to be
3439// / X conditional on p0.new while R2 producer is
3440// / \ on p0)
3441// / \.
3442// p.new store p.old NV store
3443// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3444// ^ ^
3445// \ /
3446// \ /
3447// \ /
3448// p.old store
3449// [if (p0)memw(R0+#0)=R2]
3450//
3451//
3452// The following set of instructions further explains the scenario where
3453// conditional new-value store becomes invalid when promoted to .new predicate
3454// form.
3455//
3456// { 1) if (p0) r0 = add(r1, r2)
3457// 2) p0 = cmp.eq(r3, #0) }
3458//
3459// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3460// the first two instructions because in instr 1, r0 is conditional on old value
3461// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3462// is not valid for new-value stores.
3463// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3464// from the "Conditional Store" list. Because a predicated new value store
3465// would NOT be promoted to a double dot new store. See diagram below:
3466// This function returns yes for those stores that are predicated but not
3467// yet promoted to predicate dot new instructions.
3468//
3469// +---------------------+
3470// /-----| if (p0) memw(..)=r0 |---------\~
3471// || +---------------------+ ||
3472// promote || /\ /\ || promote
3473// || /||\ /||\ ||
3474// \||/ demote || \||/
3475// \/ || || \/
3476// +-------------------------+ || +-------------------------+
3477// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3478// +-------------------------+ || +-------------------------+
3479// || || ||
3480// || demote \||/
3481// promote || \/ NOT possible
3482// || || /\~
3483// \||/ || /||\~
3484// \/ || ||
3485// +-----------------------------+
3486// | if (p0.new) memw(..)=r0.new |
3487// +-----------------------------+
3488// Double Dot New Store
3489//
3490// Returns the most basic instruction for the .new predicated instructions and
3491// new-value stores.
3492// For example, all of the following instructions will be converted back to the
3493// same instruction:
3494// 1) if (p0.new) memw(R0+#0) = R1.new --->
3495// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3496// 3) if (p0.new) memw(R0+#0) = R1 --->
3497//
3498// To understand the translation of instruction 1 to its original form, consider
3499// a packet with 3 instructions.
3500// { p0 = cmp.eq(R0,R1)
3501// if (p0.new) R2 = add(R3, R4)
3502// R5 = add (R3, R1)
3503// }
3504// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3505//
3506// This instruction can be part of the previous packet only if both p0 and R2
3507// are promoted to .new values. This promotion happens in steps, first
3508// predicate register is promoted to .new and in the next iteration R2 is
3509// promoted. Therefore, in case of dependence check failure (due to R5) during
3510// next iteration, it should be converted back to its most basic form.
3511
3512
3513// Return the new value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003514int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3515 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003516 if (NVOpcode >= 0) // Valid new-value store instruction.
3517 return NVOpcode;
3518
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003519 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003520 default: llvm_unreachable("Unknown .new type");
3521 case Hexagon::S4_storerb_ur:
3522 return Hexagon::S4_storerbnew_ur;
3523
3524 case Hexagon::S2_storerb_pci:
3525 return Hexagon::S2_storerb_pci;
3526
3527 case Hexagon::S2_storeri_pci:
3528 return Hexagon::S2_storeri_pci;
3529
3530 case Hexagon::S2_storerh_pci:
3531 return Hexagon::S2_storerh_pci;
3532
3533 case Hexagon::S2_storerd_pci:
3534 return Hexagon::S2_storerd_pci;
3535
3536 case Hexagon::S2_storerf_pci:
3537 return Hexagon::S2_storerf_pci;
3538
3539 case Hexagon::V6_vS32b_ai:
3540 return Hexagon::V6_vS32b_new_ai;
3541
3542 case Hexagon::V6_vS32b_pi:
3543 return Hexagon::V6_vS32b_new_pi;
3544
3545 // 128B
3546 case Hexagon::V6_vS32b_ai_128B:
3547 return Hexagon::V6_vS32b_new_ai_128B;
3548
3549 case Hexagon::V6_vS32b_pi_128B:
3550 return Hexagon::V6_vS32b_new_pi_128B;
3551 }
3552 return 0;
3553}
3554
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00003555
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003556// Returns the opcode to use when converting MI, which is a conditional jump,
3557// into a conditional instruction which uses the .new value of the predicate.
3558// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003559int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003560 const MachineBranchProbabilityInfo *MBPI) const {
3561 // We assume that block can have at most two successors.
3562 bool taken = false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003563 const MachineBasicBlock *Src = MI.getParent();
3564 const MachineOperand &BrTarget = MI.getOperand(1);
3565 const MachineBasicBlock *Dst = BrTarget.getMBB();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003566
3567 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
3568 if (Prediction >= BranchProbability(1,2))
3569 taken = true;
3570
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003571 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003572 case Hexagon::J2_jumpt:
3573 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3574 case Hexagon::J2_jumpf:
3575 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3576
3577 default:
3578 llvm_unreachable("Unexpected jump instruction.");
3579 }
3580}
3581
3582
3583// Return .new predicate version for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003584int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003585 const MachineBranchProbabilityInfo *MBPI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003586 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003587 if (NewOpcode >= 0) // Valid predicate new instruction
3588 return NewOpcode;
3589
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003590 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003591 // Condtional Jumps
3592 case Hexagon::J2_jumpt:
3593 case Hexagon::J2_jumpf:
3594 return getDotNewPredJumpOp(MI, MBPI);
3595
3596 default:
3597 assert(0 && "Unknown .new type");
3598 }
3599 return 0;
3600}
3601
3602
3603int HexagonInstrInfo::getDotOldOp(const int opc) const {
3604 int NewOp = opc;
3605 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3606 NewOp = Hexagon::getPredOldOpcode(NewOp);
3607 assert(NewOp >= 0 &&
3608 "Couldn't change predicate new instruction to its old form.");
3609 }
3610
3611 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3612 NewOp = Hexagon::getNonNVStore(NewOp);
3613 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3614 }
3615 return NewOp;
3616}
3617
3618
3619// See if instruction could potentially be a duplex candidate.
3620// If so, return its group. Zero otherwise.
3621HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003622 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003623 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3624 auto &HRI = getRegisterInfo();
3625
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003626 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003627 default:
3628 return HexagonII::HSIG_None;
3629 //
3630 // Group L1:
3631 //
3632 // Rd = memw(Rs+#u4:2)
3633 // Rd = memub(Rs+#u4:0)
3634 case Hexagon::L2_loadri_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003635 DstReg = MI.getOperand(0).getReg();
3636 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003637 // Special case this one from Group L2.
3638 // Rd = memw(r29+#u5:2)
3639 if (isIntRegForSubInst(DstReg)) {
3640 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3641 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003642 MI.getOperand(2).isImm() &&
3643 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003644 return HexagonII::HSIG_L2;
3645 // Rd = memw(Rs+#u4:2)
3646 if (isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003647 (MI.getOperand(2).isImm() &&
3648 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003649 return HexagonII::HSIG_L1;
3650 }
3651 break;
3652 case Hexagon::L2_loadrub_io:
3653 // Rd = memub(Rs+#u4:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003654 DstReg = MI.getOperand(0).getReg();
3655 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003656 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003657 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003658 return HexagonII::HSIG_L1;
3659 break;
3660 //
3661 // Group L2:
3662 //
3663 // Rd = memh/memuh(Rs+#u3:1)
3664 // Rd = memb(Rs+#u3:0)
3665 // Rd = memw(r29+#u5:2) - Handled above.
3666 // Rdd = memd(r29+#u5:3)
3667 // deallocframe
3668 // [if ([!]p0[.new])] dealloc_return
3669 // [if ([!]p0[.new])] jumpr r31
3670 case Hexagon::L2_loadrh_io:
3671 case Hexagon::L2_loadruh_io:
3672 // Rd = memh/memuh(Rs+#u3:1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003673 DstReg = MI.getOperand(0).getReg();
3674 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003675 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003676 MI.getOperand(2).isImm() &&
3677 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003678 return HexagonII::HSIG_L2;
3679 break;
3680 case Hexagon::L2_loadrb_io:
3681 // Rd = memb(Rs+#u3:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003682 DstReg = MI.getOperand(0).getReg();
3683 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003684 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003685 MI.getOperand(2).isImm() &&
3686 isUInt<3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003687 return HexagonII::HSIG_L2;
3688 break;
3689 case Hexagon::L2_loadrd_io:
3690 // Rdd = memd(r29+#u5:3)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003691 DstReg = MI.getOperand(0).getReg();
3692 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003693 if (isDblRegForSubInst(DstReg, HRI) &&
3694 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3695 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003696 MI.getOperand(2).isImm() &&
3697 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003698 return HexagonII::HSIG_L2;
3699 break;
3700 // dealloc_return is not documented in Hexagon Manual, but marked
3701 // with A_SUBINSN attribute in iset_v4classic.py.
3702 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003703 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003704 case Hexagon::L4_return:
3705 case Hexagon::L2_deallocframe:
3706 return HexagonII::HSIG_L2;
3707 case Hexagon::EH_RETURN_JMPR:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003708 case Hexagon::PS_jmpret:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003709 // jumpr r31
3710 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003711 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003712 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3713 return HexagonII::HSIG_L2;
3714 break;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003715 case Hexagon::PS_jmprett:
3716 case Hexagon::PS_jmpretf:
3717 case Hexagon::PS_jmprettnewpt:
3718 case Hexagon::PS_jmpretfnewpt:
3719 case Hexagon::PS_jmprettnew:
3720 case Hexagon::PS_jmpretfnew:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003721 DstReg = MI.getOperand(1).getReg();
3722 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003723 // [if ([!]p0[.new])] jumpr r31
3724 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3725 (Hexagon::P0 == SrcReg)) &&
3726 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3727 return HexagonII::HSIG_L2;
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003728 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003729 case Hexagon::L4_return_t :
3730 case Hexagon::L4_return_f :
3731 case Hexagon::L4_return_tnew_pnt :
3732 case Hexagon::L4_return_fnew_pnt :
3733 case Hexagon::L4_return_tnew_pt :
3734 case Hexagon::L4_return_fnew_pt :
3735 // [if ([!]p0[.new])] dealloc_return
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003736 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003737 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3738 return HexagonII::HSIG_L2;
3739 break;
3740 //
3741 // Group S1:
3742 //
3743 // memw(Rs+#u4:2) = Rt
3744 // memb(Rs+#u4:0) = Rt
3745 case Hexagon::S2_storeri_io:
3746 // Special case this one from Group S2.
3747 // memw(r29+#u5:2) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003748 Src1Reg = MI.getOperand(0).getReg();
3749 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003750 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3751 isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003752 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3753 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003754 return HexagonII::HSIG_S2;
3755 // memw(Rs+#u4:2) = Rt
3756 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003757 MI.getOperand(1).isImm() &&
3758 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003759 return HexagonII::HSIG_S1;
3760 break;
3761 case Hexagon::S2_storerb_io:
3762 // memb(Rs+#u4:0) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003763 Src1Reg = MI.getOperand(0).getReg();
3764 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003765 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003766 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003767 return HexagonII::HSIG_S1;
3768 break;
3769 //
3770 // Group S2:
3771 //
3772 // memh(Rs+#u3:1) = Rt
3773 // memw(r29+#u5:2) = Rt
3774 // memd(r29+#s6:3) = Rtt
3775 // memw(Rs+#u4:2) = #U1
3776 // memb(Rs+#u4) = #U1
3777 // allocframe(#u5:3)
3778 case Hexagon::S2_storerh_io:
3779 // memh(Rs+#u3:1) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003780 Src1Reg = MI.getOperand(0).getReg();
3781 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003782 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003783 MI.getOperand(1).isImm() &&
3784 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003785 return HexagonII::HSIG_S1;
3786 break;
3787 case Hexagon::S2_storerd_io:
3788 // memd(r29+#s6:3) = Rtt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003789 Src1Reg = MI.getOperand(0).getReg();
3790 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003791 if (isDblRegForSubInst(Src2Reg, HRI) &&
3792 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003793 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3794 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003795 return HexagonII::HSIG_S2;
3796 break;
3797 case Hexagon::S4_storeiri_io:
3798 // memw(Rs+#u4:2) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003799 Src1Reg = MI.getOperand(0).getReg();
3800 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
3801 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
3802 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003803 return HexagonII::HSIG_S2;
3804 break;
3805 case Hexagon::S4_storeirb_io:
3806 // memb(Rs+#u4) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003807 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003808 if (isIntRegForSubInst(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003809 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
3810 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003811 return HexagonII::HSIG_S2;
3812 break;
3813 case Hexagon::S2_allocframe:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003814 if (MI.getOperand(0).isImm() &&
3815 isShiftedUInt<5,3>(MI.getOperand(0).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003816 return HexagonII::HSIG_S1;
3817 break;
3818 //
3819 // Group A:
3820 //
3821 // Rx = add(Rx,#s7)
3822 // Rd = Rs
3823 // Rd = #u6
3824 // Rd = #-1
3825 // if ([!]P0[.new]) Rd = #0
3826 // Rd = add(r29,#u6:2)
3827 // Rx = add(Rx,Rs)
3828 // P0 = cmp.eq(Rs,#u2)
3829 // Rdd = combine(#0,Rs)
3830 // Rdd = combine(Rs,#0)
3831 // Rdd = combine(#u2,#U2)
3832 // Rd = add(Rs,#1)
3833 // Rd = add(Rs,#-1)
3834 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3835 // Rd = and(Rs,#1)
3836 case Hexagon::A2_addi:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003837 DstReg = MI.getOperand(0).getReg();
3838 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003839 if (isIntRegForSubInst(DstReg)) {
3840 // Rd = add(r29,#u6:2)
3841 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003842 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
3843 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003844 return HexagonII::HSIG_A;
3845 // Rx = add(Rx,#s7)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003846 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
3847 isInt<7>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003848 return HexagonII::HSIG_A;
3849 // Rd = add(Rs,#1)
3850 // Rd = add(Rs,#-1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003851 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3852 ((MI.getOperand(2).getImm() == 1) ||
3853 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003854 return HexagonII::HSIG_A;
3855 }
3856 break;
3857 case Hexagon::A2_add:
3858 // Rx = add(Rx,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003859 DstReg = MI.getOperand(0).getReg();
3860 Src1Reg = MI.getOperand(1).getReg();
3861 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003862 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3863 isIntRegForSubInst(Src2Reg))
3864 return HexagonII::HSIG_A;
3865 break;
3866 case Hexagon::A2_andir:
3867 // Same as zxtb.
3868 // Rd16=and(Rs16,#255)
3869 // Rd16=and(Rs16,#1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003870 DstReg = MI.getOperand(0).getReg();
3871 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003872 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003873 MI.getOperand(2).isImm() &&
3874 ((MI.getOperand(2).getImm() == 1) ||
3875 (MI.getOperand(2).getImm() == 255)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003876 return HexagonII::HSIG_A;
3877 break;
3878 case Hexagon::A2_tfr:
3879 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003880 DstReg = MI.getOperand(0).getReg();
3881 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003882 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3883 return HexagonII::HSIG_A;
3884 break;
3885 case Hexagon::A2_tfrsi:
3886 // Rd = #u6
3887 // Do not test for #u6 size since the const is getting extended
3888 // regardless and compound could be formed.
3889 // Rd = #-1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003890 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003891 if (isIntRegForSubInst(DstReg))
3892 return HexagonII::HSIG_A;
3893 break;
3894 case Hexagon::C2_cmoveit:
3895 case Hexagon::C2_cmovenewit:
3896 case Hexagon::C2_cmoveif:
3897 case Hexagon::C2_cmovenewif:
3898 // if ([!]P0[.new]) Rd = #0
3899 // Actual form:
3900 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003901 DstReg = MI.getOperand(0).getReg();
3902 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003903 if (isIntRegForSubInst(DstReg) &&
3904 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003905 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003906 return HexagonII::HSIG_A;
3907 break;
3908 case Hexagon::C2_cmpeqi:
3909 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003910 DstReg = MI.getOperand(0).getReg();
3911 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003912 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3913 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003914 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003915 return HexagonII::HSIG_A;
3916 break;
3917 case Hexagon::A2_combineii:
3918 case Hexagon::A4_combineii:
3919 // Rdd = combine(#u2,#U2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003920 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003921 if (isDblRegForSubInst(DstReg, HRI) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003922 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
3923 (MI.getOperand(1).isGlobal() &&
3924 isUInt<2>(MI.getOperand(1).getOffset()))) &&
3925 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
3926 (MI.getOperand(2).isGlobal() &&
3927 isUInt<2>(MI.getOperand(2).getOffset()))))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003928 return HexagonII::HSIG_A;
3929 break;
3930 case Hexagon::A4_combineri:
3931 // Rdd = combine(Rs,#0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003932 DstReg = MI.getOperand(0).getReg();
3933 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003934 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003935 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
3936 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003937 return HexagonII::HSIG_A;
3938 break;
3939 case Hexagon::A4_combineir:
3940 // Rdd = combine(#0,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003941 DstReg = MI.getOperand(0).getReg();
3942 SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003943 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003944 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
3945 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003946 return HexagonII::HSIG_A;
3947 break;
3948 case Hexagon::A2_sxtb:
3949 case Hexagon::A2_sxth:
3950 case Hexagon::A2_zxtb:
3951 case Hexagon::A2_zxth:
3952 // Rd = sxth/sxtb/zxtb/zxth(Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003953 DstReg = MI.getOperand(0).getReg();
3954 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003955 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3956 return HexagonII::HSIG_A;
3957 break;
3958 }
3959
3960 return HexagonII::HSIG_None;
3961}
3962
3963
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003964short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
3965 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003966}
3967
3968
3969// Return first non-debug instruction in the basic block.
3970MachineInstr *HexagonInstrInfo::getFirstNonDbgInst(MachineBasicBlock *BB)
3971 const {
3972 for (auto MII = BB->instr_begin(), End = BB->instr_end(); MII != End; MII++) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003973 MachineInstr &MI = *MII;
3974 if (MI.isDebugValue())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003975 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003976 return &MI;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003977 }
3978 return nullptr;
3979}
3980
3981
3982unsigned HexagonInstrInfo::getInstrTimingClassLatency(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003983 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003984 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3985 // still have a MinLatency property, which getStageLatency checks.
3986 if (!ItinData)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003987 return getInstrLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003988
3989 // Get the latency embedded in the itinerary. If we're not using timing class
3990 // latencies or if we using BSB scheduling, then restrict the maximum latency
3991 // to 1 (that is, either 0 or 1).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003992 if (MI.isTransient())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003993 return 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003994 unsigned Latency = ItinData->getStageLatency(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003995 if (!EnableTimingClassLatency ||
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003996 MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>().
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003997 useBSBScheduling())
3998 if (Latency > 1)
3999 Latency = 1;
4000 return Latency;
4001}
4002
4003
4004// inverts the predication logic.
4005// p -> NotP
4006// NotP -> P
4007bool HexagonInstrInfo::getInvertedPredSense(
4008 SmallVectorImpl<MachineOperand> &Cond) const {
4009 if (Cond.empty())
4010 return false;
4011 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
4012 Cond[0].setImm(Opc);
4013 return true;
4014}
4015
4016
4017unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
4018 int InvPredOpcode;
4019 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
4020 : Hexagon::getTruePredOpcode(Opc);
4021 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
4022 return InvPredOpcode;
4023
4024 llvm_unreachable("Unexpected predicated instruction");
4025}
4026
4027
4028// Returns the max value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004029int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
4030 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004031 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4032 & HexagonII::ExtentSignedMask;
4033 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4034 & HexagonII::ExtentBitsMask;
4035
4036 if (isSigned) // if value is signed
4037 return ~(-1U << (bits - 1));
4038 else
4039 return ~(-1U << bits);
4040}
4041
4042
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004043unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
4044 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004045 return (F >> HexagonII::MemAccessSizePos) & HexagonII::MemAccesSizeMask;
4046}
4047
4048
4049// Returns the min value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004050int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
4051 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004052 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4053 & HexagonII::ExtentSignedMask;
4054 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4055 & HexagonII::ExtentBitsMask;
4056
4057 if (isSigned) // if value is signed
4058 return -1U << (bits - 1);
4059 else
4060 return 0;
4061}
4062
4063
4064// Returns opcode of the non-extended equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004065short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00004066 // Check if the instruction has a register form that uses register in place
4067 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004068 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004069 if (NonExtOpcode >= 0)
4070 return NonExtOpcode;
4071
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004072 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00004073 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00004074 switch (getAddrMode(MI)) {
4075 case HexagonII::Absolute :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004076 return Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004077 case HexagonII::BaseImmOffset :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004078 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004079 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004080 return Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004081
Jyotsna Verma84256432013-03-01 17:37:13 +00004082 default:
4083 return -1;
4084 }
4085 }
4086 return -1;
4087}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00004088
Brendon Cahoondf43e682015-05-08 16:16:29 +00004089
Ahmed Bougachac88bf542015-06-11 19:30:37 +00004090bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004091 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00004092 if (Cond.empty())
4093 return false;
4094 assert(Cond.size() == 2);
4095 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00004096 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
4097 return false;
Brendon Cahoondf43e682015-05-08 16:16:29 +00004098 }
4099 PredReg = Cond[1].getReg();
4100 PredRegPos = 1;
4101 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4102 PredRegFlags = 0;
4103 if (Cond[1].isImplicit())
4104 PredRegFlags = RegState::Implicit;
4105 if (Cond[1].isUndef())
4106 PredRegFlags |= RegState::Undef;
4107 return true;
4108}
4109
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004110
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004111short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
4112 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004113}
4114
4115
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004116short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
4117 return Hexagon::getRegForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004118}
4119
4120
4121// Return the number of bytes required to encode the instruction.
4122// Hexagon instructions are fixed length, 4 bytes, unless they
4123// use a constant extender, which requires another 4 bytes.
4124// For debug instructions and prolog labels, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004125unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
4126 if (MI.isDebugValue() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004127 return 0;
4128
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004129 unsigned Size = MI.getDesc().getSize();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004130 if (!Size)
4131 // Assume the default insn size in case it cannot be determined
4132 // for whatever reason.
4133 Size = HEXAGON_INSTR_SIZE;
4134
4135 if (isConstExtended(MI) || isExtended(MI))
4136 Size += HEXAGON_INSTR_SIZE;
4137
4138 // Try and compute number of instructions in asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004139 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4140 const MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004141 const MachineFunction *MF = MBB.getParent();
4142 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4143
4144 // Count the number of register definitions to find the asm string.
4145 unsigned NumDefs = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004146 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004147 ++NumDefs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004148 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004149
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004150 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004151 // Disassemble the AsmStr and approximate number of instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004152 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004153 Size = getInlineAsmLength(AsmStr, *MAI);
4154 }
4155
4156 return Size;
4157}
4158
4159
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004160uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
4161 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004162 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4163}
4164
4165
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004166unsigned HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
4167 const TargetSubtargetInfo &ST = MI.getParent()->getParent()->getSubtarget();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004168 const InstrItineraryData &II = *ST.getInstrItineraryData();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004169 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004170
4171 return IS.getUnits();
4172}
4173
4174
4175unsigned HexagonInstrInfo::getValidSubTargets(const unsigned Opcode) const {
4176 const uint64_t F = get(Opcode).TSFlags;
4177 return (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask;
4178}
4179
4180
4181// Calculate size of the basic block without debug instructions.
4182unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4183 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4184}
4185
4186
4187unsigned HexagonInstrInfo::nonDbgBundleSize(
4188 MachineBasicBlock::const_iterator BundleHead) const {
4189 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004190 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004191 // Skip the bundle header.
Matthias Braunc8440dd2016-10-25 02:55:17 +00004192 return nonDbgMICount(++MII, getBundleEnd(BundleHead.getInstrIterator()));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004193}
4194
4195
4196/// immediateExtend - Changes the instruction in place to one using an immediate
4197/// extender.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004198void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004199 assert((isExtendable(MI)||isConstExtended(MI)) &&
4200 "Instruction must be extendable");
4201 // Find which operand is extendable.
4202 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004203 MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004204 // This needs to be something we understand.
4205 assert((MO.isMBB() || MO.isImm()) &&
4206 "Branch with unknown extendable field type");
4207 // Mark given operand as extended.
4208 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4209}
4210
4211
4212bool HexagonInstrInfo::invertAndChangeJumpTarget(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004213 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004214 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004215 << NewTarget->getNumber(); MI.dump(););
4216 assert(MI.isBranch());
4217 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4218 int TargetPos = MI.getNumOperands() - 1;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004219 // In general branch target is the last operand,
4220 // but some implicit defs added at the end might change it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004221 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004222 --TargetPos;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004223 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4224 MI.getOperand(TargetPos).setMBB(NewTarget);
4225 if (EnableBranchPrediction && isPredicatedNew(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004226 NewOpcode = reversePrediction(NewOpcode);
4227 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004228 MI.setDesc(get(NewOpcode));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004229 return true;
4230}
4231
4232
4233void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4234 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4235 MachineFunction::iterator A = MF.begin();
4236 MachineBasicBlock &B = *A;
4237 MachineBasicBlock::iterator I = B.begin();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004238 DebugLoc DL = I->getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004239 MachineInstr *NewMI;
4240
4241 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4242 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004243 NewMI = BuildMI(B, I, DL, get(insn));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004244 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
4245 " Class: " << NewMI->getDesc().getSchedClass());
4246 NewMI->eraseFromParent();
4247 }
4248 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4249}
4250
4251
4252// inverts the predication logic.
4253// p -> NotP
4254// NotP -> P
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004255bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
4256 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
4257 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004258 return true;
4259}
4260
4261
4262// Reverse the branch prediction.
4263unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4264 int PredRevOpcode = -1;
4265 if (isPredictedTaken(Opcode))
4266 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4267 else
4268 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4269 assert(PredRevOpcode > 0);
4270 return PredRevOpcode;
4271}
4272
4273
4274// TODO: Add more rigorous validation.
4275bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4276 const {
4277 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4278}
4279
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004280
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004281short HexagonInstrInfo::xformRegToImmOffset(const MachineInstr &MI) const {
4282 return Hexagon::xformRegToImmOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004283}