blob: abba22273f0e55495061e890b432786542103601 [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"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000023#include "llvm/CodeGen/MachineMemOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000025#include "llvm/CodeGen/PseudoSourceValue.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000026#include "llvm/CodeGen/ScheduleDAG.h"
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000027#include "llvm/MC/MCAsmInfo.h"
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +000028#include "llvm/Support/CommandLine.h"
Jyotsna Verma5ed51812013-05-01 21:37:34 +000029#include "llvm/Support/Debug.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000030#include "llvm/Support/MathExtras.h"
Reid Kleckner1c76f1552013-05-03 00:54:56 +000031#include "llvm/Support/raw_ostream.h"
Krzysztof Parzyszekaa935752015-11-24 15:11:13 +000032#include <cctype>
Tony Linthicum1213a7a2011-12-12 21:14:40 +000033
Tony Linthicum1213a7a2011-12-12 21:14:40 +000034using namespace llvm;
35
Chandler Carruthe96dd892014-04-21 22:55:11 +000036#define DEBUG_TYPE "hexagon-instrinfo"
37
Chandler Carruthd174b722014-04-22 02:03:14 +000038#define GET_INSTRINFO_CTOR_DTOR
39#define GET_INSTRMAP_INFO
40#include "HexagonGenInstrInfo.inc"
41#include "HexagonGenDFAPacketizer.inc"
42
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000043cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000044 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
45 "packetization boundary."));
46
47static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
48 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
49
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000050static cl::opt<bool> DisableNVSchedule("disable-hexagon-nv-schedule",
51 cl::Hidden, cl::ZeroOrMore, cl::init(false),
52 cl::desc("Disable schedule adjustment for new value stores."));
53
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000054static cl::opt<bool> EnableTimingClassLatency(
55 "enable-timing-class-latency", cl::Hidden, cl::init(false),
56 cl::desc("Enable timing class latency"));
57
58static cl::opt<bool> EnableALUForwarding(
59 "enable-alu-forwarding", cl::Hidden, cl::init(true),
60 cl::desc("Enable vec alu forwarding"));
61
62static cl::opt<bool> EnableACCForwarding(
63 "enable-acc-forwarding", cl::Hidden, cl::init(true),
64 cl::desc("Enable vec acc forwarding"));
65
66static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
67 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
68
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000069static cl::opt<bool> UseDFAHazardRec("dfa-hazard-rec",
70 cl::init(true), cl::Hidden, cl::ZeroOrMore,
71 cl::desc("Use the DFA based hazard recognizer."));
72
Tony Linthicum1213a7a2011-12-12 21:14:40 +000073///
74/// Constants for Hexagon instructions.
75///
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +000076const int Hexagon_MEMV_OFFSET_MAX_128B = 896; // #s4: -8*128...7*128
77const int Hexagon_MEMV_OFFSET_MIN_128B = -1024; // #s4
78const int Hexagon_MEMV_OFFSET_MAX = 448; // #s4: -8*64...7*64
79const int Hexagon_MEMV_OFFSET_MIN = -512; // #s4
Tony Linthicum1213a7a2011-12-12 21:14:40 +000080const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000081const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000082const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000083const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000084const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000085const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000086const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000087const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000088const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000089const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000090const int Hexagon_MEMD_AUTOINC_MAX = 56;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000091const int Hexagon_MEMD_AUTOINC_MIN = -64;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000092const int Hexagon_MEMW_AUTOINC_MAX = 28;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000093const int Hexagon_MEMW_AUTOINC_MIN = -32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000094const int Hexagon_MEMH_AUTOINC_MAX = 14;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000095const int Hexagon_MEMH_AUTOINC_MIN = -16;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000096const int Hexagon_MEMB_AUTOINC_MAX = 7;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000097const int Hexagon_MEMB_AUTOINC_MIN = -8;
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +000098const int Hexagon_MEMV_AUTOINC_MAX = 192; // #s3
99const int Hexagon_MEMV_AUTOINC_MIN = -256; // #s3
100const int Hexagon_MEMV_AUTOINC_MAX_128B = 384; // #s3
101const int Hexagon_MEMV_AUTOINC_MIN_128B = -512; // #s3
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000102
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000103// Pin the vtable to this file.
104void HexagonInstrInfo::anchor() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000105
106HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
Eric Christopherc4d31402015-03-10 23:45:55 +0000107 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000108 RI() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000109
110
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000111static bool isIntRegForSubInst(unsigned Reg) {
112 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
113 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000114}
115
116
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000117static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
118 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::subreg_loreg)) &&
119 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::subreg_hireg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000120}
121
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000122
123/// Calculate number of instructions excluding the debug instructions.
124static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
125 MachineBasicBlock::const_instr_iterator MIE) {
126 unsigned Count = 0;
127 for (; MIB != MIE; ++MIB) {
128 if (!MIB->isDebugValue())
129 ++Count;
130 }
131 return Count;
132}
133
134
135/// Find the hardware loop instruction used to set-up the specified loop.
136/// On Hexagon, we have two instructions used to set-up the hardware loop
137/// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
138/// to indicate the end of a loop.
139static MachineInstr *findLoopInstr(MachineBasicBlock *BB, int EndLoopOp,
140 SmallPtrSet<MachineBasicBlock *, 8> &Visited) {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000141 int LOOPi;
142 int LOOPr;
143 if (EndLoopOp == Hexagon::ENDLOOP0) {
144 LOOPi = Hexagon::J2_loop0i;
145 LOOPr = Hexagon::J2_loop0r;
146 } else { // EndLoopOp == Hexagon::EndLOOP1
147 LOOPi = Hexagon::J2_loop1i;
148 LOOPr = Hexagon::J2_loop1r;
149 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000150
Brendon Cahoondf43e682015-05-08 16:16:29 +0000151 // The loop set-up instruction will be in a predecessor block
152 for (MachineBasicBlock::pred_iterator PB = BB->pred_begin(),
153 PE = BB->pred_end(); PB != PE; ++PB) {
154 // If this has been visited, already skip it.
155 if (!Visited.insert(*PB).second)
156 continue;
157 if (*PB == BB)
158 continue;
159 for (MachineBasicBlock::reverse_instr_iterator I = (*PB)->instr_rbegin(),
160 E = (*PB)->instr_rend(); I != E; ++I) {
161 int Opc = I->getOpcode();
162 if (Opc == LOOPi || Opc == LOOPr)
163 return &*I;
164 // We've reached a different loop, which means the loop0 has been removed.
165 if (Opc == EndLoopOp)
166 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000167 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000168 // Check the predecessors for the LOOP instruction.
169 MachineInstr *loop = findLoopInstr(*PB, EndLoopOp, Visited);
170 if (loop)
171 return loop;
172 }
173 return 0;
174}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000175
Brendon Cahoondf43e682015-05-08 16:16:29 +0000176
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000177/// Gather register def/uses from MI.
178/// This treats possible (predicated) defs as actually happening ones
179/// (conservatively).
180static inline void parseOperands(const MachineInstr *MI,
181 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
182 Defs.clear();
183 Uses.clear();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000184
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000185 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
186 const MachineOperand &MO = MI->getOperand(i);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000187
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000188 if (!MO.isReg())
189 continue;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000190
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000191 unsigned Reg = MO.getReg();
192 if (!Reg)
193 continue;
194
195 if (MO.isUse())
196 Uses.push_back(MO.getReg());
197
198 if (MO.isDef())
199 Defs.push_back(MO.getReg());
200 }
201}
202
203
204// Position dependent, so check twice for swap.
205static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
206 switch (Ga) {
207 case HexagonII::HSIG_None:
208 default:
209 return false;
210 case HexagonII::HSIG_L1:
211 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
212 case HexagonII::HSIG_L2:
213 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
214 Gb == HexagonII::HSIG_A);
215 case HexagonII::HSIG_S1:
216 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
217 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
218 case HexagonII::HSIG_S2:
219 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
220 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
221 Gb == HexagonII::HSIG_A);
222 case HexagonII::HSIG_A:
223 return (Gb == HexagonII::HSIG_A);
224 case HexagonII::HSIG_Compound:
225 return (Gb == HexagonII::HSIG_Compound);
226 }
227 return false;
228}
229
230
231
232/// isLoadFromStackSlot - If the specified machine instruction is a direct
233/// load from a stack slot, return the virtual or physical register number of
234/// the destination along with the FrameIndex of the loaded stack slot. If
235/// not, return 0. This predicate must return 0 if the instruction has
236/// any side effects other than loading from the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000237unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000238 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000239 switch (MI.getOpcode()) {
240 default:
241 break;
242 case Hexagon::L2_loadrb_io:
243 case Hexagon::L2_loadrub_io:
244 case Hexagon::L2_loadrh_io:
245 case Hexagon::L2_loadruh_io:
246 case Hexagon::L2_loadri_io:
247 case Hexagon::L2_loadrd_io:
248 case Hexagon::V6_vL32b_ai:
249 case Hexagon::V6_vL32b_ai_128B:
250 case Hexagon::V6_vL32Ub_ai:
251 case Hexagon::V6_vL32Ub_ai_128B:
252 case Hexagon::LDriw_pred:
253 case Hexagon::LDriw_mod:
254 case Hexagon::LDriq_pred_V6:
255 case Hexagon::LDriq_pred_vec_V6:
256 case Hexagon::LDriv_pseudo_V6:
257 case Hexagon::LDrivv_pseudo_V6:
258 case Hexagon::LDriq_pred_V6_128B:
259 case Hexagon::LDriq_pred_vec_V6_128B:
260 case Hexagon::LDriv_pseudo_V6_128B:
261 case Hexagon::LDrivv_pseudo_V6_128B: {
262 const MachineOperand OpFI = MI.getOperand(1);
263 if (!OpFI.isFI())
264 return 0;
265 const MachineOperand OpOff = MI.getOperand(2);
266 if (!OpOff.isImm() || OpOff.getImm() != 0)
267 return 0;
268 FrameIndex = OpFI.getIndex();
269 return MI.getOperand(0).getReg();
270 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000271
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000272 case Hexagon::L2_ploadrbt_io:
273 case Hexagon::L2_ploadrbf_io:
274 case Hexagon::L2_ploadrubt_io:
275 case Hexagon::L2_ploadrubf_io:
276 case Hexagon::L2_ploadrht_io:
277 case Hexagon::L2_ploadrhf_io:
278 case Hexagon::L2_ploadruht_io:
279 case Hexagon::L2_ploadruhf_io:
280 case Hexagon::L2_ploadrit_io:
281 case Hexagon::L2_ploadrif_io:
282 case Hexagon::L2_ploadrdt_io:
283 case Hexagon::L2_ploadrdf_io: {
284 const MachineOperand OpFI = MI.getOperand(2);
285 if (!OpFI.isFI())
286 return 0;
287 const MachineOperand OpOff = MI.getOperand(3);
288 if (!OpOff.isImm() || OpOff.getImm() != 0)
289 return 0;
290 FrameIndex = OpFI.getIndex();
291 return MI.getOperand(0).getReg();
292 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000293 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000294
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000295 return 0;
296}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000297
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000298
299/// isStoreToStackSlot - If the specified machine instruction is a direct
300/// store to a stack slot, return the virtual or physical register number of
301/// the source reg along with the FrameIndex of the loaded stack slot. If
302/// not, return 0. This predicate must return 0 if the instruction has
303/// any side effects other than storing to the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000304unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000305 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000306 switch (MI.getOpcode()) {
307 default:
308 break;
309 case Hexagon::S2_storerb_io:
310 case Hexagon::S2_storerh_io:
311 case Hexagon::S2_storeri_io:
312 case Hexagon::S2_storerd_io:
313 case Hexagon::V6_vS32b_ai:
314 case Hexagon::V6_vS32b_ai_128B:
315 case Hexagon::V6_vS32Ub_ai:
316 case Hexagon::V6_vS32Ub_ai_128B:
317 case Hexagon::STriw_pred:
318 case Hexagon::STriw_mod:
319 case Hexagon::STriq_pred_V6:
320 case Hexagon::STriq_pred_vec_V6:
321 case Hexagon::STriv_pseudo_V6:
322 case Hexagon::STrivv_pseudo_V6:
323 case Hexagon::STriq_pred_V6_128B:
324 case Hexagon::STriq_pred_vec_V6_128B:
325 case Hexagon::STriv_pseudo_V6_128B:
326 case Hexagon::STrivv_pseudo_V6_128B: {
327 const MachineOperand &OpFI = MI.getOperand(0);
328 if (!OpFI.isFI())
329 return 0;
330 const MachineOperand &OpOff = MI.getOperand(1);
331 if (!OpOff.isImm() || OpOff.getImm() != 0)
332 return 0;
333 FrameIndex = OpFI.getIndex();
334 return MI.getOperand(2).getReg();
335 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000336
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000337 case Hexagon::S2_pstorerbt_io:
338 case Hexagon::S2_pstorerbf_io:
339 case Hexagon::S2_pstorerht_io:
340 case Hexagon::S2_pstorerhf_io:
341 case Hexagon::S2_pstorerit_io:
342 case Hexagon::S2_pstorerif_io:
343 case Hexagon::S2_pstorerdt_io:
344 case Hexagon::S2_pstorerdf_io: {
345 const MachineOperand &OpFI = MI.getOperand(1);
346 if (!OpFI.isFI())
347 return 0;
348 const MachineOperand &OpOff = MI.getOperand(2);
349 if (!OpOff.isImm() || OpOff.getImm() != 0)
350 return 0;
351 FrameIndex = OpFI.getIndex();
352 return MI.getOperand(3).getReg();
353 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000354 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000355
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000356 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000357}
358
359
Brendon Cahoondf43e682015-05-08 16:16:29 +0000360/// This function can analyze one/two way branching only and should (mostly) be
361/// called by target independent side.
362/// First entry is always the opcode of the branching instruction, except when
363/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
364/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
365/// e.g. Jump_c p will have
366/// Cond[0] = Jump_c
367/// Cond[1] = p
368/// HW-loop ENDLOOP:
369/// Cond[0] = ENDLOOP
370/// Cond[1] = MBB
371/// New value jump:
372/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
373/// Cond[1] = R
374/// Cond[2] = Imm
Brendon Cahoondf43e682015-05-08 16:16:29 +0000375///
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000376bool HexagonInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000377 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000378 MachineBasicBlock *&FBB,
379 SmallVectorImpl<MachineOperand> &Cond,
380 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000381 TBB = nullptr;
382 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000383 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000384
385 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000386 MachineBasicBlock::instr_iterator I = MBB.instr_end();
387 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000388 return false;
389
390 // A basic block may looks like this:
391 //
392 // [ insn
393 // EH_LABEL
394 // insn
395 // insn
396 // insn
397 // EH_LABEL
398 // insn ]
399 //
400 // It has two succs but does not have a terminator
401 // Don't know how to handle it.
402 do {
403 --I;
404 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000405 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000406 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000407 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000408
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000409 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000410 --I;
411
412 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000413 if (I == MBB.instr_begin())
414 return false;
415 --I;
416 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000417
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000418 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
419 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000420 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000421 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000422 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
423 DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
424 I->eraseFromParent();
425 I = MBB.instr_end();
426 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000427 return false;
428 --I;
429 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000430 if (!isUnpredicatedTerminator(*I))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000431 return false;
432
433 // Get the last instruction in the block.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000434 MachineInstr *LastInst = &*I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000435 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000436 // Find one more terminator if present.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000437 for (;;) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000438 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000439 if (!SecondLastInst)
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000440 SecondLastInst = &*I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000441 else
442 // This is a third branch.
443 return true;
444 }
445 if (I == MBB.instr_begin())
446 break;
447 --I;
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000448 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000449
450 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000451 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
452 // If the branch target is not a basic block, it could be a tail call.
453 // (It is, if the target is a function.)
454 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
455 return true;
456 if (SecLastOpcode == Hexagon::J2_jump &&
457 !SecondLastInst->getOperand(0).isMBB())
458 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000459
460 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000461 bool LastOpcodeHasNVJump = isNewValueJump(LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000462
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000463 if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
464 return true;
465
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000466 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000467 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000468 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000469 TBB = LastInst->getOperand(0).getMBB();
470 return false;
471 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000472 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000473 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000474 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000475 Cond.push_back(LastInst->getOperand(0));
476 return false;
477 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000478 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000479 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000480 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000481 Cond.push_back(LastInst->getOperand(0));
482 return false;
483 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000484 // Only supporting rr/ri versions of new-value jumps.
485 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
486 TBB = LastInst->getOperand(2).getMBB();
487 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
488 Cond.push_back(LastInst->getOperand(0));
489 Cond.push_back(LastInst->getOperand(1));
490 return false;
491 }
492 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
493 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000494 // Otherwise, don't know what this is.
495 return true;
496 }
497
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000498 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000499 bool SecLastOpcodeHasNVJump = isNewValueJump(SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000500 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000501 if (!SecondLastInst->getOperand(1).isMBB())
502 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000503 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000504 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000505 Cond.push_back(SecondLastInst->getOperand(0));
506 FBB = LastInst->getOperand(0).getMBB();
507 return false;
508 }
509
Brendon Cahoondf43e682015-05-08 16:16:29 +0000510 // Only supporting rr/ri versions of new-value jumps.
511 if (SecLastOpcodeHasNVJump &&
512 (SecondLastInst->getNumExplicitOperands() == 3) &&
513 (LastOpcode == Hexagon::J2_jump)) {
514 TBB = SecondLastInst->getOperand(2).getMBB();
515 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
516 Cond.push_back(SecondLastInst->getOperand(0));
517 Cond.push_back(SecondLastInst->getOperand(1));
518 FBB = LastInst->getOperand(0).getMBB();
519 return false;
520 }
521
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000522 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
523 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000524 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000525 TBB = SecondLastInst->getOperand(0).getMBB();
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000526 I = LastInst->getIterator();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000527 if (AllowModify)
528 I->eraseFromParent();
529 return false;
530 }
531
Brendon Cahoondf43e682015-05-08 16:16:29 +0000532 // If the block ends with an ENDLOOP, and J2_jump, handle it.
533 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000534 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000535 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000536 Cond.push_back(SecondLastInst->getOperand(0));
537 FBB = LastInst->getOperand(0).getMBB();
538 return false;
539 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000540 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
541 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000542 // Otherwise, can't handle this.
543 return true;
544}
545
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000546
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000547unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000548 DEBUG(dbgs() << "\nRemoving branches out of BB#" << MBB.getNumber());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000549 MachineBasicBlock::iterator I = MBB.end();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000550 unsigned Count = 0;
551 while (I != MBB.begin()) {
552 --I;
553 if (I->isDebugValue())
554 continue;
555 // Only removing branches from end of MBB.
556 if (!I->isBranch())
557 return Count;
558 if (Count && (I->getOpcode() == Hexagon::J2_jump))
559 llvm_unreachable("Malformed basic block: unconditional branch not last");
560 MBB.erase(&MBB.back());
561 I = MBB.end();
562 ++Count;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000563 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000564 return Count;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000565}
566
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000567unsigned HexagonInstrInfo::InsertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000568 MachineBasicBlock *TBB,
569 MachineBasicBlock *FBB,
570 ArrayRef<MachineOperand> Cond,
571 const DebugLoc &DL) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000572 unsigned BOpc = Hexagon::J2_jump;
573 unsigned BccOpc = Hexagon::J2_jumpt;
574 assert(validateBranchCond(Cond) && "Invalid branching condition");
575 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
576
577 // Check if ReverseBranchCondition has asked to reverse this branch
578 // If we want to reverse the branch an odd number of times, we want
579 // J2_jumpf.
580 if (!Cond.empty() && Cond[0].isImm())
581 BccOpc = Cond[0].getImm();
582
583 if (!FBB) {
584 if (Cond.empty()) {
585 // Due to a bug in TailMerging/CFG Optimization, we need to add a
586 // special case handling of a predicated jump followed by an
587 // unconditional jump. If not, Tail Merging and CFG Optimization go
588 // into an infinite loop.
589 MachineBasicBlock *NewTBB, *NewFBB;
590 SmallVector<MachineOperand, 4> Cond;
Duncan P. N. Exon Smith25b132e2016-07-08 18:26:20 +0000591 auto Term = MBB.getFirstTerminator();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000592 if (Term != MBB.end() && isPredicated(*Term) &&
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000593 !analyzeBranch(MBB, NewTBB, NewFBB, Cond, false)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000594 MachineBasicBlock *NextBB = &*++MBB.getIterator();
595 if (NewTBB == NextBB) {
596 ReverseBranchCondition(Cond);
597 RemoveBranch(MBB);
598 return InsertBranch(MBB, TBB, nullptr, Cond, DL);
599 }
600 }
601 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
602 } else if (isEndLoopN(Cond[0].getImm())) {
603 int EndLoopOp = Cond[0].getImm();
604 assert(Cond[1].isMBB());
605 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
606 // Check for it, and change the BB target if needed.
607 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
608 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
609 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
610 Loop->getOperand(0).setMBB(TBB);
611 // Add the ENDLOOP after the finding the LOOP0.
612 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
613 } else if (isNewValueJump(Cond[0].getImm())) {
614 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
615 // New value jump
616 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
617 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
618 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
619 DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
620 if (Cond[2].isReg()) {
621 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
622 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
623 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
624 } else if(Cond[2].isImm()) {
625 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
626 addImm(Cond[2].getImm()).addMBB(TBB);
627 } else
628 llvm_unreachable("Invalid condition for branching");
629 } else {
630 assert((Cond.size() == 2) && "Malformed cond vector");
631 const MachineOperand &RO = Cond[1];
632 unsigned Flags = getUndefRegState(RO.isUndef());
633 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
634 }
635 return 1;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000636 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000637 assert((!Cond.empty()) &&
638 "Cond. cannot be empty when multiple branchings are required");
639 assert((!isNewValueJump(Cond[0].getImm())) &&
640 "NV-jump cannot be inserted with another branch");
641 // Special case for hardware loops. The condition is a basic block.
642 if (isEndLoopN(Cond[0].getImm())) {
643 int EndLoopOp = Cond[0].getImm();
644 assert(Cond[1].isMBB());
645 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
646 // Check for it, and change the BB target if needed.
647 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
648 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
649 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
650 Loop->getOperand(0).setMBB(TBB);
651 // Add the ENDLOOP after the finding the LOOP0.
652 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
653 } else {
654 const MachineOperand &RO = Cond[1];
655 unsigned Flags = getUndefRegState(RO.isUndef());
656 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000657 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000658 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000659
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000660 return 2;
661}
662
663
664bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
665 unsigned NumCycles, unsigned ExtraPredCycles,
666 BranchProbability Probability) const {
667 return nonDbgBBSize(&MBB) <= 3;
668}
669
670
671bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
672 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
673 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
674 const {
675 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
676}
677
678
679bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
680 unsigned NumInstrs, BranchProbability Probability) const {
681 return NumInstrs <= 4;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000682}
683
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000684void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000685 MachineBasicBlock::iterator I,
686 const DebugLoc &DL, unsigned DestReg,
687 unsigned SrcReg, bool KillSrc) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000688 auto &HRI = getRegisterInfo();
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000689 unsigned KillFlag = getKillRegState(KillSrc);
690
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000691 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000692 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000693 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000694 return;
695 }
696 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000697 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg)
698 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000699 return;
700 }
701 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
702 // Map Pd = Ps to Pd = or(Ps, Ps).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000703 BuildMI(MBB, I, DL, get(Hexagon::C2_or), DestReg)
704 .addReg(SrcReg).addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000705 return;
706 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000707 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000708 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000709 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
710 .addReg(SrcReg, KillFlag);
711 return;
712 }
713 if (Hexagon::IntRegsRegClass.contains(DestReg) &&
714 Hexagon::CtrRegsRegClass.contains(SrcReg)) {
715 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrcrr), DestReg)
716 .addReg(SrcReg, KillFlag);
717 return;
718 }
719 if (Hexagon::ModRegsRegClass.contains(DestReg) &&
720 Hexagon::IntRegsRegClass.contains(SrcReg)) {
721 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
722 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000723 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000724 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000725 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
726 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000727 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
728 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000729 return;
730 }
731 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
732 Hexagon::PredRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000733 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg)
734 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000735 return;
736 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000737 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
738 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000739 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
740 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000741 return;
742 }
743 if (Hexagon::VectorRegsRegClass.contains(SrcReg, DestReg)) {
744 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000745 addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000746 return;
747 }
748 if (Hexagon::VecDblRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000749 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
750 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg), KillFlag)
751 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg), KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000752 return;
753 }
754 if (Hexagon::VecPredRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000755 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
756 .addReg(SrcReg)
757 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000758 return;
759 }
760 if (Hexagon::VecPredRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000761 Hexagon::VectorRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000762 llvm_unreachable("Unimplemented pred to vec");
763 return;
764 }
765 if (Hexagon::VecPredRegsRegClass.contains(DestReg) &&
766 Hexagon::VectorRegsRegClass.contains(SrcReg)) {
767 llvm_unreachable("Unimplemented vec to pred");
768 return;
769 }
770 if (Hexagon::VecPredRegs128BRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000771 unsigned DstHi = HRI.getSubReg(DestReg, Hexagon::subreg_hireg);
772 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DstHi)
773 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg), KillFlag);
774 unsigned DstLo = HRI.getSubReg(DestReg, Hexagon::subreg_loreg);
775 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DstLo)
776 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg), KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000777 return;
778 }
Sirish Pande30804c22012-02-15 18:52:27 +0000779
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000780#ifndef NDEBUG
781 // Show the invalid registers to ease debugging.
782 dbgs() << "Invalid registers for copy in BB#" << MBB.getNumber()
783 << ": " << PrintReg(DestReg, &HRI)
784 << " = " << PrintReg(SrcReg, &HRI) << '\n';
785#endif
Sirish Pande30804c22012-02-15 18:52:27 +0000786 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000787}
788
789
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000790void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
791 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
792 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000793 DebugLoc DL = MBB.findDebugLoc(I);
794 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000795 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000796 unsigned Align = MFI.getObjectAlignment(FI);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000797 unsigned KillFlag = getKillRegState(isKill);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000798
Alex Lorenze40c8a22015-08-11 23:09:45 +0000799 MachineMemOperand *MMO = MF.getMachineMemOperand(
800 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
801 MFI.getObjectSize(FI), Align);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000802
Craig Topperc7242e02012-04-20 07:30:17 +0000803 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000804 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000805 .addFrameIndex(FI).addImm(0)
806 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000807 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000808 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000809 .addFrameIndex(FI).addImm(0)
810 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000811 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000812 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000813 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000814 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000815 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
816 BuildMI(MBB, I, DL, get(Hexagon::STriw_mod))
817 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000818 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
819 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
820 BuildMI(MBB, I, DL, get(Hexagon::STriq_pred_V6_128B))
821 .addFrameIndex(FI).addImm(0)
822 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
823 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
824 BuildMI(MBB, I, DL, get(Hexagon::STriq_pred_V6))
825 .addFrameIndex(FI).addImm(0)
826 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
827 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
828 DEBUG(dbgs() << "++Generating 128B vector spill");
829 BuildMI(MBB, I, DL, get(Hexagon::STriv_pseudo_V6_128B))
830 .addFrameIndex(FI).addImm(0)
831 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
832 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
833 DEBUG(dbgs() << "++Generating vector spill");
834 BuildMI(MBB, I, DL, get(Hexagon::STriv_pseudo_V6))
835 .addFrameIndex(FI).addImm(0)
836 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
837 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
838 DEBUG(dbgs() << "++Generating double vector spill");
839 BuildMI(MBB, I, DL, get(Hexagon::STrivv_pseudo_V6))
840 .addFrameIndex(FI).addImm(0)
841 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
842 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
843 DEBUG(dbgs() << "++Generating 128B double vector spill");
844 BuildMI(MBB, I, DL, get(Hexagon::STrivv_pseudo_V6_128B))
845 .addFrameIndex(FI).addImm(0)
846 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000847 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000848 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000849 }
850}
851
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000852void HexagonInstrInfo::loadRegFromStackSlot(
853 MachineBasicBlock &MBB, MachineBasicBlock::iterator I, unsigned DestReg,
854 int FI, const TargetRegisterClass *RC,
855 const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000856 DebugLoc DL = MBB.findDebugLoc(I);
857 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000858 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000859 unsigned Align = MFI.getObjectAlignment(FI);
860
Alex Lorenze40c8a22015-08-11 23:09:45 +0000861 MachineMemOperand *MMO = MF.getMachineMemOperand(
862 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
863 MFI.getObjectSize(FI), Align);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000864
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000865 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000866 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000867 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000868 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000869 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000870 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000871 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000872 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000873 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
874 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
875 BuildMI(MBB, I, DL, get(Hexagon::LDriw_mod), DestReg)
876 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000877 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
878 BuildMI(MBB, I, DL, get(Hexagon::LDriq_pred_V6_128B), DestReg)
879 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
880 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
881 BuildMI(MBB, I, DL, get(Hexagon::LDriq_pred_V6), DestReg)
882 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
883 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
884 DEBUG(dbgs() << "++Generating 128B double vector restore");
885 BuildMI(MBB, I, DL, get(Hexagon::LDrivv_pseudo_V6_128B), DestReg)
886 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
887 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
888 DEBUG(dbgs() << "++Generating 128B vector restore");
889 BuildMI(MBB, I, DL, get(Hexagon::LDriv_pseudo_V6_128B), DestReg)
890 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
891 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
892 DEBUG(dbgs() << "++Generating vector restore");
893 BuildMI(MBB, I, DL, get(Hexagon::LDriv_pseudo_V6), DestReg)
894 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
895 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
896 DEBUG(dbgs() << "++Generating double vector restore");
897 BuildMI(MBB, I, DL, get(Hexagon::LDrivv_pseudo_V6), DestReg)
898 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000899 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000900 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000901 }
902}
903
904
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000905/// expandPostRAPseudo - This function is called for all pseudo instructions
906/// that remain after register allocation. Many pseudo instructions are
907/// created to help register allocation. This is the place to convert them
908/// into real instructions. The target can edit MI in place, or it can insert
909/// new instructions and erase MI. The function should return true if
910/// anything was changed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000911bool HexagonInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000912 const HexagonRegisterInfo &HRI = getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000913 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
914 MachineBasicBlock &MBB = *MI.getParent();
915 DebugLoc DL = MI.getDebugLoc();
916 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +0000917 const unsigned VecOffset = 1;
918 bool Is128B = false;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000919
920 switch (Opc) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000921 case TargetOpcode::COPY: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000922 MachineOperand &MD = MI.getOperand(0);
923 MachineOperand &MS = MI.getOperand(1);
924 MachineBasicBlock::iterator MBBI = MI.getIterator();
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000925 if (MD.getReg() != MS.getReg() && !MS.isUndef()) {
926 copyPhysReg(MBB, MI, DL, MD.getReg(), MS.getReg(), MS.isKill());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000927 std::prev(MBBI)->copyImplicitOps(*MBB.getParent(), MI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000928 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000929 MBB.erase(MBBI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000930 return true;
931 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000932 case Hexagon::ALIGNA:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000933 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI.getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000934 .addReg(HRI.getFrameRegister())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000935 .addImm(-MI.getOperand(1).getImm());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000936 MBB.erase(MI);
937 return true;
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +0000938 case Hexagon::HEXAGON_V6_vassignp_128B:
939 case Hexagon::HEXAGON_V6_vassignp: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000940 unsigned SrcReg = MI.getOperand(1).getReg();
941 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +0000942 if (SrcReg != DstReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000943 copyPhysReg(MBB, MI, DL, DstReg, SrcReg, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +0000944 MBB.erase(MI);
945 return true;
946 }
947 case Hexagon::HEXAGON_V6_lo_128B:
948 case Hexagon::HEXAGON_V6_lo: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000949 unsigned SrcReg = MI.getOperand(1).getReg();
950 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +0000951 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000952 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +0000953 MBB.erase(MI);
954 MRI.clearKillFlags(SrcSubLo);
955 return true;
956 }
957 case Hexagon::HEXAGON_V6_hi_128B:
958 case Hexagon::HEXAGON_V6_hi: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000959 unsigned SrcReg = MI.getOperand(1).getReg();
960 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +0000961 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::subreg_hireg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000962 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +0000963 MBB.erase(MI);
964 MRI.clearKillFlags(SrcSubHi);
965 return true;
966 }
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +0000967 case Hexagon::STrivv_indexed_128B:
968 Is128B = true;
969 case Hexagon::STrivv_indexed: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000970 unsigned SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +0000971 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::subreg_hireg);
972 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::subreg_loreg);
973 unsigned NewOpcd = Is128B ? Hexagon::V6_vS32b_ai_128B
974 : Hexagon::V6_vS32b_ai;
975 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000976 MachineInstr *MI1New =
977 BuildMI(MBB, MI, DL, get(NewOpcd))
978 .addOperand(MI.getOperand(0))
979 .addImm(MI.getOperand(1).getImm())
980 .addReg(SrcSubLo)
981 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +0000982 MI1New->getOperand(0).setIsKill(false);
983 BuildMI(MBB, MI, DL, get(NewOpcd))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000984 .addOperand(MI.getOperand(0))
985 // The Vectors are indexed in multiples of vector size.
986 .addImm(MI.getOperand(1).getImm() + Offset)
987 .addReg(SrcSubHi)
988 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +0000989 MBB.erase(MI);
990 return true;
991 }
992 case Hexagon::LDrivv_pseudo_V6_128B:
993 case Hexagon::LDrivv_indexed_128B:
994 Is128B = true;
995 case Hexagon::LDrivv_pseudo_V6:
996 case Hexagon::LDrivv_indexed: {
997 unsigned NewOpcd = Is128B ? Hexagon::V6_vL32b_ai_128B
998 : Hexagon::V6_vL32b_ai;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000999 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001000 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
1001 MachineInstr *MI1New =
1002 BuildMI(MBB, MI, DL, get(NewOpcd),
1003 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001004 .addOperand(MI.getOperand(1))
1005 .addImm(MI.getOperand(2).getImm());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001006 MI1New->getOperand(1).setIsKill(false);
1007 BuildMI(MBB, MI, DL, get(NewOpcd),
1008 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001009 .addOperand(MI.getOperand(1))
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001010 // The Vectors are indexed in multiples of vector size.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001011 .addImm(MI.getOperand(2).getImm() + Offset)
1012 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001013 MBB.erase(MI);
1014 return true;
1015 }
1016 case Hexagon::LDriv_pseudo_V6_128B:
1017 Is128B = true;
1018 case Hexagon::LDriv_pseudo_V6: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001019 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001020 unsigned NewOpc = Is128B ? Hexagon::V6_vL32b_ai_128B
1021 : Hexagon::V6_vL32b_ai;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001022 int32_t Off = MI.getOperand(2).getImm();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001023 BuildMI(MBB, MI, DL, get(NewOpc), DstReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001024 .addOperand(MI.getOperand(1))
1025 .addImm(Off)
1026 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001027 MBB.erase(MI);
1028 return true;
1029 }
1030 case Hexagon::STriv_pseudo_V6_128B:
1031 Is128B = true;
1032 case Hexagon::STriv_pseudo_V6: {
1033 unsigned NewOpc = Is128B ? Hexagon::V6_vS32b_ai_128B
1034 : Hexagon::V6_vS32b_ai;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001035 int32_t Off = MI.getOperand(1).getImm();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001036 BuildMI(MBB, MI, DL, get(NewOpc))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001037 .addOperand(MI.getOperand(0))
1038 .addImm(Off)
1039 .addOperand(MI.getOperand(2))
1040 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001041 MBB.erase(MI);
1042 return true;
1043 }
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001044 case Hexagon::TFR_PdTrue: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001045 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001046 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1047 .addReg(Reg, RegState::Undef)
1048 .addReg(Reg, RegState::Undef);
1049 MBB.erase(MI);
1050 return true;
1051 }
1052 case Hexagon::TFR_PdFalse: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001053 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001054 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1055 .addReg(Reg, RegState::Undef)
1056 .addReg(Reg, RegState::Undef);
1057 MBB.erase(MI);
1058 return true;
1059 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001060 case Hexagon::VMULW: {
1061 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001062 unsigned DstReg = MI.getOperand(0).getReg();
1063 unsigned Src1Reg = MI.getOperand(1).getReg();
1064 unsigned Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001065 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
1066 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
1067 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
1068 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001069 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
1070 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1071 .addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001072 .addReg(Src2SubHi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001073 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
1074 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1075 .addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001076 .addReg(Src2SubLo);
1077 MBB.erase(MI);
1078 MRI.clearKillFlags(Src1SubHi);
1079 MRI.clearKillFlags(Src1SubLo);
1080 MRI.clearKillFlags(Src2SubHi);
1081 MRI.clearKillFlags(Src2SubLo);
1082 return true;
1083 }
1084 case Hexagon::VMULW_ACC: {
1085 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001086 unsigned DstReg = MI.getOperand(0).getReg();
1087 unsigned Src1Reg = MI.getOperand(1).getReg();
1088 unsigned Src2Reg = MI.getOperand(2).getReg();
1089 unsigned Src3Reg = MI.getOperand(3).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001090 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
1091 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
1092 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
1093 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
1094 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::subreg_hireg);
1095 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001096 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
1097 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1098 .addReg(Src1SubHi)
1099 .addReg(Src2SubHi)
1100 .addReg(Src3SubHi);
1101 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
1102 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1103 .addReg(Src1SubLo)
1104 .addReg(Src2SubLo)
1105 .addReg(Src3SubLo);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001106 MBB.erase(MI);
1107 MRI.clearKillFlags(Src1SubHi);
1108 MRI.clearKillFlags(Src1SubLo);
1109 MRI.clearKillFlags(Src2SubHi);
1110 MRI.clearKillFlags(Src2SubLo);
1111 MRI.clearKillFlags(Src3SubHi);
1112 MRI.clearKillFlags(Src3SubLo);
1113 return true;
1114 }
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +00001115 case Hexagon::Insert4: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001116 unsigned DstReg = MI.getOperand(0).getReg();
1117 unsigned Src1Reg = MI.getOperand(1).getReg();
1118 unsigned Src2Reg = MI.getOperand(2).getReg();
1119 unsigned Src3Reg = MI.getOperand(3).getReg();
1120 unsigned Src4Reg = MI.getOperand(4).getReg();
1121 unsigned Src1RegIsKill = getKillRegState(MI.getOperand(1).isKill());
1122 unsigned Src2RegIsKill = getKillRegState(MI.getOperand(2).isKill());
1123 unsigned Src3RegIsKill = getKillRegState(MI.getOperand(3).isKill());
1124 unsigned Src4RegIsKill = getKillRegState(MI.getOperand(4).isKill());
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +00001125 unsigned DstSubHi = HRI.getSubReg(DstReg, Hexagon::subreg_hireg);
1126 unsigned DstSubLo = HRI.getSubReg(DstReg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001127 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1128 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1129 .addReg(DstSubLo)
1130 .addReg(Src1Reg, Src1RegIsKill)
1131 .addImm(16)
1132 .addImm(0);
1133 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1134 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1135 .addReg(DstSubLo)
1136 .addReg(Src2Reg, Src2RegIsKill)
1137 .addImm(16)
1138 .addImm(16);
1139 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1140 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1141 .addReg(DstSubHi)
1142 .addReg(Src3Reg, Src3RegIsKill)
1143 .addImm(16)
1144 .addImm(0);
1145 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1146 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1147 .addReg(DstSubHi)
1148 .addReg(Src4Reg, Src4RegIsKill)
1149 .addImm(16)
1150 .addImm(16);
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +00001151 MBB.erase(MI);
1152 MRI.clearKillFlags(DstReg);
1153 MRI.clearKillFlags(DstSubHi);
1154 MRI.clearKillFlags(DstSubLo);
1155 return true;
1156 }
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001157 case Hexagon::MUX64_rr: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001158 const MachineOperand &Op0 = MI.getOperand(0);
1159 const MachineOperand &Op1 = MI.getOperand(1);
1160 const MachineOperand &Op2 = MI.getOperand(2);
1161 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001162 unsigned Rd = Op0.getReg();
1163 unsigned Pu = Op1.getReg();
1164 unsigned Rs = Op2.getReg();
1165 unsigned Rt = Op3.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001166 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001167 unsigned K1 = getKillRegState(Op1.isKill());
1168 unsigned K2 = getKillRegState(Op2.isKill());
1169 unsigned K3 = getKillRegState(Op3.isKill());
1170 if (Rd != Rs)
1171 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1172 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1173 .addReg(Rs, K2);
1174 if (Rd != Rt)
1175 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1176 .addReg(Pu, K1)
1177 .addReg(Rt, K3);
1178 MBB.erase(MI);
1179 return true;
1180 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001181 case Hexagon::VSelectPseudo_V6: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001182 const MachineOperand &Op0 = MI.getOperand(0);
1183 const MachineOperand &Op1 = MI.getOperand(1);
1184 const MachineOperand &Op2 = MI.getOperand(2);
1185 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001186 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
1187 .addOperand(Op0)
1188 .addOperand(Op1)
1189 .addOperand(Op2);
1190 BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
1191 .addOperand(Op0)
1192 .addOperand(Op1)
1193 .addOperand(Op3);
1194 MBB.erase(MI);
1195 return true;
1196 }
1197 case Hexagon::VSelectDblPseudo_V6: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001198 MachineOperand &Op0 = MI.getOperand(0);
1199 MachineOperand &Op1 = MI.getOperand(1);
1200 MachineOperand &Op2 = MI.getOperand(2);
1201 MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001202 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::subreg_loreg);
1203 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::subreg_hireg);
1204 BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
1205 .addOperand(Op0)
1206 .addOperand(Op1)
1207 .addReg(SrcHi)
1208 .addReg(SrcLo);
1209 SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::subreg_loreg);
1210 SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::subreg_hireg);
1211 BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
1212 .addOperand(Op0)
1213 .addOperand(Op1)
1214 .addReg(SrcHi)
1215 .addReg(SrcLo);
1216 MBB.erase(MI);
1217 return true;
1218 }
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001219 case Hexagon::TCRETURNi:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001220 MI.setDesc(get(Hexagon::J2_jump));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001221 return true;
1222 case Hexagon::TCRETURNr:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001223 MI.setDesc(get(Hexagon::J2_jumpr));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001224 return true;
Krzysztof Parzyszek70a134d2015-11-25 21:40:03 +00001225 case Hexagon::TFRI_f:
1226 case Hexagon::TFRI_cPt_f:
1227 case Hexagon::TFRI_cNotPt_f: {
1228 unsigned Opx = (Opc == Hexagon::TFRI_f) ? 1 : 2;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001229 APFloat FVal = MI.getOperand(Opx).getFPImm()->getValueAPF();
Krzysztof Parzyszek70a134d2015-11-25 21:40:03 +00001230 APInt IVal = FVal.bitcastToAPInt();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001231 MI.RemoveOperand(Opx);
Krzysztof Parzyszek70a134d2015-11-25 21:40:03 +00001232 unsigned NewOpc = (Opc == Hexagon::TFRI_f) ? Hexagon::A2_tfrsi :
1233 (Opc == Hexagon::TFRI_cPt_f) ? Hexagon::C2_cmoveit :
1234 Hexagon::C2_cmoveif;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001235 MI.setDesc(get(NewOpc));
1236 MI.addOperand(MachineOperand::CreateImm(IVal.getZExtValue()));
Krzysztof Parzyszek70a134d2015-11-25 21:40:03 +00001237 return true;
1238 }
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001239 }
1240
1241 return false;
1242}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001243
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001244
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001245// We indicate that we want to reverse the branch by
1246// inserting the reversed branching opcode.
1247bool HexagonInstrInfo::ReverseBranchCondition(
1248 SmallVectorImpl<MachineOperand> &Cond) const {
1249 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001250 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001251 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1252 unsigned opcode = Cond[0].getImm();
1253 //unsigned temp;
1254 assert(get(opcode).isBranch() && "Should be a branching condition.");
1255 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001256 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001257 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1258 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001259 return false;
1260}
1261
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001262
1263void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1264 MachineBasicBlock::iterator MI) const {
1265 DebugLoc DL;
1266 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1267}
1268
1269
1270// Returns true if an instruction is predicated irrespective of the predicate
1271// sense. For example, all of the following will return true.
1272// if (p0) R1 = add(R2, R3)
1273// if (!p0) R1 = add(R2, R3)
1274// if (p0.new) R1 = add(R2, R3)
1275// if (!p0.new) R1 = add(R2, R3)
1276// Note: New-value stores are not included here as in the current
1277// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001278bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1279 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001280 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001281}
1282
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001283
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001284bool HexagonInstrInfo::PredicateInstruction(
1285 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001286 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1287 isEndLoopN(Cond[0].getImm())) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001288 DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001289 return false;
1290 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001291 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001292 assert (isPredicable(MI) && "Expected predicable instruction");
1293 bool invertJump = predOpcodeHasNot(Cond);
1294
1295 // We have to predicate MI "in place", i.e. after this function returns,
1296 // MI will need to be transformed into a predicated form. To avoid com-
1297 // plicated manipulations with the operands (handling tied operands,
1298 // etc.), build a new temporary instruction, then overwrite MI with it.
1299
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001300 MachineBasicBlock &B = *MI.getParent();
1301 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001302 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1303 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001304 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001305 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001306 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001307 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1308 break;
1309 T.addOperand(Op);
1310 NOp++;
1311 }
1312
1313 unsigned PredReg, PredRegPos, PredRegFlags;
1314 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1315 (void)GotPredReg;
1316 assert(GotPredReg);
1317 T.addReg(PredReg, PredRegFlags);
1318 while (NOp < NumOps)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001319 T.addOperand(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001320
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001321 MI.setDesc(get(PredOpc));
1322 while (unsigned n = MI.getNumOperands())
1323 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001324 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001325 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001326
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001327 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001328 B.erase(TI);
1329
1330 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1331 MRI.clearKillFlags(PredReg);
1332 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001333}
1334
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001335
1336bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1337 ArrayRef<MachineOperand> Pred2) const {
1338 // TODO: Fix this
1339 return false;
1340}
1341
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001342
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001343bool HexagonInstrInfo::DefinesPredicate(
1344 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001345 auto &HRI = getRegisterInfo();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001346 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1347 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001348 if (MO.isReg() && MO.isDef()) {
1349 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1350 if (RC == &Hexagon::PredRegsRegClass) {
1351 Pred.push_back(MO);
1352 return true;
1353 }
1354 }
1355 }
1356 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001357}
Andrew Trickd06df962012-02-01 22:13:57 +00001358
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001359
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001360bool HexagonInstrInfo::isPredicable(MachineInstr &MI) const {
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001361 return MI.getDesc().isPredicable();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001362}
1363
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001364bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1365 const MachineBasicBlock *MBB,
1366 const MachineFunction &MF) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001367 // Debug info is never a scheduling boundary. It's necessary to be explicit
1368 // due to the special treatment of IT instructions below, otherwise a
1369 // dbg_value followed by an IT will result in the IT instruction being
1370 // considered a scheduling hazard, which is wrong. It should be the actual
1371 // instruction preceding the dbg_value instruction(s), just like it is
1372 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001373 if (MI.isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001374 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001375
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001376 // Throwing call is a boundary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001377 if (MI.isCall()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001378 // If any of the block's successors is a landing pad, this could be a
1379 // throwing call.
1380 for (auto I : MBB->successors())
1381 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001382 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001383 }
1384
1385 // Don't mess around with no return calls.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001386 if (MI.getOpcode() == Hexagon::CALLv3nr)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001387 return true;
1388
1389 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001390 if (MI.getDesc().isTerminator() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001391 return true;
1392
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001393 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1394 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001395
1396 return false;
1397}
1398
1399
1400/// Measure the specified inline asm to determine an approximation of its
1401/// length.
1402/// Comments (which run till the next SeparatorString or newline) do not
1403/// count as an instruction.
1404/// Any other non-whitespace text is considered an instruction, with
1405/// multiple instructions separated by SeparatorString or newlines.
1406/// Variable-length instructions are not handled here; this function
1407/// may be overloaded in the target code to do that.
1408/// Hexagon counts the number of ##'s and adjust for that many
1409/// constant exenders.
1410unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1411 const MCAsmInfo &MAI) const {
1412 StringRef AStr(Str);
1413 // Count the number of instructions in the asm.
1414 bool atInsnStart = true;
1415 unsigned Length = 0;
1416 for (; *Str; ++Str) {
1417 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1418 strlen(MAI.getSeparatorString())) == 0)
1419 atInsnStart = true;
1420 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1421 Length += MAI.getMaxInstLength();
1422 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001423 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001424 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
1425 strlen(MAI.getCommentString())) == 0)
1426 atInsnStart = false;
1427 }
1428
1429 // Add to size number of constant extenders seen * 4.
1430 StringRef Occ("##");
1431 Length += AStr.count(Occ)*4;
1432 return Length;
1433}
1434
1435
1436ScheduleHazardRecognizer*
1437HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1438 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +00001439 if (UseDFAHazardRec) {
1440 auto &HST = DAG->MF.getSubtarget<HexagonSubtarget>();
1441 return new HexagonHazardRecognizer(II, this, HST);
1442 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001443 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1444}
1445
1446
1447/// \brief For a comparison instruction, return the source registers in
1448/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1449/// compares against in CmpValue. Return true if the comparison instruction
1450/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001451bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1452 unsigned &SrcReg2, int &Mask,
1453 int &Value) const {
1454 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001455
1456 // Set mask and the first source register.
1457 switch (Opc) {
1458 case Hexagon::C2_cmpeq:
1459 case Hexagon::C2_cmpeqp:
1460 case Hexagon::C2_cmpgt:
1461 case Hexagon::C2_cmpgtp:
1462 case Hexagon::C2_cmpgtu:
1463 case Hexagon::C2_cmpgtup:
1464 case Hexagon::C4_cmpneq:
1465 case Hexagon::C4_cmplte:
1466 case Hexagon::C4_cmplteu:
1467 case Hexagon::C2_cmpeqi:
1468 case Hexagon::C2_cmpgti:
1469 case Hexagon::C2_cmpgtui:
1470 case Hexagon::C4_cmpneqi:
1471 case Hexagon::C4_cmplteui:
1472 case Hexagon::C4_cmpltei:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001473 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001474 Mask = ~0;
1475 break;
1476 case Hexagon::A4_cmpbeq:
1477 case Hexagon::A4_cmpbgt:
1478 case Hexagon::A4_cmpbgtu:
1479 case Hexagon::A4_cmpbeqi:
1480 case Hexagon::A4_cmpbgti:
1481 case Hexagon::A4_cmpbgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001482 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001483 Mask = 0xFF;
1484 break;
1485 case Hexagon::A4_cmpheq:
1486 case Hexagon::A4_cmphgt:
1487 case Hexagon::A4_cmphgtu:
1488 case Hexagon::A4_cmpheqi:
1489 case Hexagon::A4_cmphgti:
1490 case Hexagon::A4_cmphgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001491 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001492 Mask = 0xFFFF;
1493 break;
1494 }
1495
1496 // Set the value/second source register.
1497 switch (Opc) {
1498 case Hexagon::C2_cmpeq:
1499 case Hexagon::C2_cmpeqp:
1500 case Hexagon::C2_cmpgt:
1501 case Hexagon::C2_cmpgtp:
1502 case Hexagon::C2_cmpgtu:
1503 case Hexagon::C2_cmpgtup:
1504 case Hexagon::A4_cmpbeq:
1505 case Hexagon::A4_cmpbgt:
1506 case Hexagon::A4_cmpbgtu:
1507 case Hexagon::A4_cmpheq:
1508 case Hexagon::A4_cmphgt:
1509 case Hexagon::A4_cmphgtu:
1510 case Hexagon::C4_cmpneq:
1511 case Hexagon::C4_cmplte:
1512 case Hexagon::C4_cmplteu:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001513 SrcReg2 = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001514 return true;
1515
1516 case Hexagon::C2_cmpeqi:
1517 case Hexagon::C2_cmpgtui:
1518 case Hexagon::C2_cmpgti:
1519 case Hexagon::C4_cmpneqi:
1520 case Hexagon::C4_cmplteui:
1521 case Hexagon::C4_cmpltei:
1522 case Hexagon::A4_cmpbeqi:
1523 case Hexagon::A4_cmpbgti:
1524 case Hexagon::A4_cmpbgtui:
1525 case Hexagon::A4_cmpheqi:
1526 case Hexagon::A4_cmphgti:
1527 case Hexagon::A4_cmphgtui:
1528 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001529 Value = MI.getOperand(2).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001530 return true;
1531 }
1532
1533 return false;
1534}
1535
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001536unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001537 const MachineInstr &MI,
1538 unsigned *PredCost) const {
1539 return getInstrTimingClassLatency(ItinData, &MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001540}
1541
1542
1543DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1544 const TargetSubtargetInfo &STI) const {
1545 const InstrItineraryData *II = STI.getInstrItineraryData();
1546 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1547}
1548
1549
1550// Inspired by this pair:
1551// %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1552// S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1553// Currently AA considers the addresses in these instructions to be aliasing.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001554bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1555 MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001556 int OffsetA = 0, OffsetB = 0;
1557 unsigned SizeA = 0, SizeB = 0;
1558
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001559 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1560 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001561 return false;
1562
1563 // Instructions that are pure loads, not loads and stores like memops are not
1564 // dependent.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001565 if (MIa.mayLoad() && !isMemOp(&MIa) && MIb.mayLoad() && !isMemOp(&MIb))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001566 return true;
1567
1568 // Get base, offset, and access size in MIa.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001569 unsigned BaseRegA = getBaseAndOffset(&MIa, OffsetA, SizeA);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001570 if (!BaseRegA || !SizeA)
1571 return false;
1572
1573 // Get base, offset, and access size in MIb.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001574 unsigned BaseRegB = getBaseAndOffset(&MIb, OffsetB, SizeB);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001575 if (!BaseRegB || !SizeB)
1576 return false;
1577
1578 if (BaseRegA != BaseRegB)
1579 return false;
1580
1581 // This is a mem access with the same base register and known offsets from it.
1582 // Reason about it.
1583 if (OffsetA > OffsetB) {
1584 uint64_t offDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1585 return (SizeB <= offDiff);
1586 } else if (OffsetA < OffsetB) {
1587 uint64_t offDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1588 return (SizeA <= offDiff);
1589 }
1590
1591 return false;
1592}
1593
1594
1595unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
1596 MachineRegisterInfo &MRI = MF->getRegInfo();
1597 const TargetRegisterClass *TRC;
1598 if (VT == MVT::i1) {
1599 TRC = &Hexagon::PredRegsRegClass;
1600 } else if (VT == MVT::i32 || VT == MVT::f32) {
1601 TRC = &Hexagon::IntRegsRegClass;
1602 } else if (VT == MVT::i64 || VT == MVT::f64) {
1603 TRC = &Hexagon::DoubleRegsRegClass;
1604 } else {
1605 llvm_unreachable("Cannot handle this register class");
1606 }
1607
1608 unsigned NewReg = MRI.createVirtualRegister(TRC);
1609 return NewReg;
1610}
1611
1612
1613bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr* MI) const {
1614 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1615}
1616
1617
1618bool HexagonInstrInfo::isAccumulator(const MachineInstr *MI) const {
1619 const uint64_t F = MI->getDesc().TSFlags;
1620 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1621}
1622
1623
1624bool HexagonInstrInfo::isComplex(const MachineInstr *MI) const {
1625 const MachineFunction *MF = MI->getParent()->getParent();
1626 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1627 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1628
1629 if (!(isTC1(MI))
1630 && !(QII->isTC2Early(MI))
1631 && !(MI->getDesc().mayLoad())
1632 && !(MI->getDesc().mayStore())
1633 && (MI->getDesc().getOpcode() != Hexagon::S2_allocframe)
1634 && (MI->getDesc().getOpcode() != Hexagon::L2_deallocframe)
1635 && !(QII->isMemOp(MI))
1636 && !(MI->isBranch())
1637 && !(MI->isReturn())
1638 && !MI->isCall())
1639 return true;
1640
1641 return false;
1642}
1643
1644
Sanjay Patele4b9f502015-12-07 19:21:39 +00001645// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001646bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr *MI) const {
1647 return (getType(MI) == HexagonII::TypeCOMPOUND && MI->isBranch());
1648}
1649
1650
1651bool HexagonInstrInfo::isCondInst(const MachineInstr *MI) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001652 return (MI->isBranch() && isPredicated(*MI)) ||
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001653 isConditionalTransfer(MI) ||
1654 isConditionalALU32(MI) ||
1655 isConditionalLoad(MI) ||
1656 // Predicated stores which don't have a .new on any operands.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001657 (MI->mayStore() && isPredicated(*MI) && !isNewValueStore(MI) &&
1658 !isPredicatedNew(*MI));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001659}
1660
1661
1662bool HexagonInstrInfo::isConditionalALU32(const MachineInstr* MI) const {
1663 switch (MI->getOpcode()) {
1664 case Hexagon::A2_paddf:
1665 case Hexagon::A2_paddfnew:
1666 case Hexagon::A2_paddif:
1667 case Hexagon::A2_paddifnew:
1668 case Hexagon::A2_paddit:
1669 case Hexagon::A2_padditnew:
1670 case Hexagon::A2_paddt:
1671 case Hexagon::A2_paddtnew:
1672 case Hexagon::A2_pandf:
1673 case Hexagon::A2_pandfnew:
1674 case Hexagon::A2_pandt:
1675 case Hexagon::A2_pandtnew:
1676 case Hexagon::A2_porf:
1677 case Hexagon::A2_porfnew:
1678 case Hexagon::A2_port:
1679 case Hexagon::A2_portnew:
1680 case Hexagon::A2_psubf:
1681 case Hexagon::A2_psubfnew:
1682 case Hexagon::A2_psubt:
1683 case Hexagon::A2_psubtnew:
1684 case Hexagon::A2_pxorf:
1685 case Hexagon::A2_pxorfnew:
1686 case Hexagon::A2_pxort:
1687 case Hexagon::A2_pxortnew:
1688 case Hexagon::A4_paslhf:
1689 case Hexagon::A4_paslhfnew:
1690 case Hexagon::A4_paslht:
1691 case Hexagon::A4_paslhtnew:
1692 case Hexagon::A4_pasrhf:
1693 case Hexagon::A4_pasrhfnew:
1694 case Hexagon::A4_pasrht:
1695 case Hexagon::A4_pasrhtnew:
1696 case Hexagon::A4_psxtbf:
1697 case Hexagon::A4_psxtbfnew:
1698 case Hexagon::A4_psxtbt:
1699 case Hexagon::A4_psxtbtnew:
1700 case Hexagon::A4_psxthf:
1701 case Hexagon::A4_psxthfnew:
1702 case Hexagon::A4_psxtht:
1703 case Hexagon::A4_psxthtnew:
1704 case Hexagon::A4_pzxtbf:
1705 case Hexagon::A4_pzxtbfnew:
1706 case Hexagon::A4_pzxtbt:
1707 case Hexagon::A4_pzxtbtnew:
1708 case Hexagon::A4_pzxthf:
1709 case Hexagon::A4_pzxthfnew:
1710 case Hexagon::A4_pzxtht:
1711 case Hexagon::A4_pzxthtnew:
1712 case Hexagon::C2_ccombinewf:
1713 case Hexagon::C2_ccombinewt:
1714 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001715 }
1716 return false;
1717}
1718
1719
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001720// FIXME - Function name and it's functionality don't match.
1721// It should be renamed to hasPredNewOpcode()
1722bool HexagonInstrInfo::isConditionalLoad(const MachineInstr* MI) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001723 if (!MI->getDesc().mayLoad() || !isPredicated(*MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001724 return false;
1725
1726 int PNewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1727 // Instruction with valid predicated-new opcode can be promoted to .new.
1728 return PNewOpcode >= 0;
1729}
1730
1731
1732// Returns true if an instruction is a conditional store.
1733//
1734// Note: It doesn't include conditional new-value stores as they can't be
1735// converted to .new predicate.
1736bool HexagonInstrInfo::isConditionalStore(const MachineInstr* MI) const {
1737 switch (MI->getOpcode()) {
1738 default: return false;
1739 case Hexagon::S4_storeirbt_io:
1740 case Hexagon::S4_storeirbf_io:
1741 case Hexagon::S4_pstorerbt_rr:
1742 case Hexagon::S4_pstorerbf_rr:
1743 case Hexagon::S2_pstorerbt_io:
1744 case Hexagon::S2_pstorerbf_io:
1745 case Hexagon::S2_pstorerbt_pi:
1746 case Hexagon::S2_pstorerbf_pi:
1747 case Hexagon::S2_pstorerdt_io:
1748 case Hexagon::S2_pstorerdf_io:
1749 case Hexagon::S4_pstorerdt_rr:
1750 case Hexagon::S4_pstorerdf_rr:
1751 case Hexagon::S2_pstorerdt_pi:
1752 case Hexagon::S2_pstorerdf_pi:
1753 case Hexagon::S2_pstorerht_io:
1754 case Hexagon::S2_pstorerhf_io:
1755 case Hexagon::S4_storeirht_io:
1756 case Hexagon::S4_storeirhf_io:
1757 case Hexagon::S4_pstorerht_rr:
1758 case Hexagon::S4_pstorerhf_rr:
1759 case Hexagon::S2_pstorerht_pi:
1760 case Hexagon::S2_pstorerhf_pi:
1761 case Hexagon::S2_pstorerit_io:
1762 case Hexagon::S2_pstorerif_io:
1763 case Hexagon::S4_storeirit_io:
1764 case Hexagon::S4_storeirif_io:
1765 case Hexagon::S4_pstorerit_rr:
1766 case Hexagon::S4_pstorerif_rr:
1767 case Hexagon::S2_pstorerit_pi:
1768 case Hexagon::S2_pstorerif_pi:
1769
1770 // V4 global address store before promoting to dot new.
1771 case Hexagon::S4_pstorerdt_abs:
1772 case Hexagon::S4_pstorerdf_abs:
1773 case Hexagon::S4_pstorerbt_abs:
1774 case Hexagon::S4_pstorerbf_abs:
1775 case Hexagon::S4_pstorerht_abs:
1776 case Hexagon::S4_pstorerhf_abs:
1777 case Hexagon::S4_pstorerit_abs:
1778 case Hexagon::S4_pstorerif_abs:
1779 return true;
1780
1781 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1782 // from the "Conditional Store" list. Because a predicated new value store
1783 // would NOT be promoted to a double dot new store.
1784 // This function returns yes for those stores that are predicated but not
1785 // yet promoted to predicate dot new instructions.
1786 }
1787}
1788
1789
1790bool HexagonInstrInfo::isConditionalTransfer(const MachineInstr *MI) const {
1791 switch (MI->getOpcode()) {
1792 case Hexagon::A2_tfrt:
1793 case Hexagon::A2_tfrf:
1794 case Hexagon::C2_cmoveit:
1795 case Hexagon::C2_cmoveif:
1796 case Hexagon::A2_tfrtnew:
1797 case Hexagon::A2_tfrfnew:
1798 case Hexagon::C2_cmovenewit:
1799 case Hexagon::C2_cmovenewif:
1800 case Hexagon::A2_tfrpt:
1801 case Hexagon::A2_tfrpf:
1802 return true;
1803
1804 default:
1805 return false;
1806 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001807 return false;
1808}
1809
1810
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001811// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1812// isFPImm and later getFPImm as well.
1813bool HexagonInstrInfo::isConstExtended(const MachineInstr *MI) const {
1814 const uint64_t F = MI->getDesc().TSFlags;
1815 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1816 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001817 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001818
1819 unsigned isExtendable =
1820 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1821 if (!isExtendable)
1822 return false;
1823
1824 if (MI->isCall())
1825 return false;
1826
1827 short ExtOpNum = getCExtOpNum(MI);
1828 const MachineOperand &MO = MI->getOperand(ExtOpNum);
1829 // Use MO operand flags to determine if MO
1830 // has the HMOTF_ConstExtended flag set.
1831 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001832 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001833 // If this is a Machine BB address we are talking about, and it is
1834 // not marked as extended, say so.
1835 if (MO.isMBB())
1836 return false;
1837
1838 // We could be using an instruction with an extendable immediate and shoehorn
1839 // a global address into it. If it is a global address it will be constant
1840 // extended. We do this for COMBINE.
1841 // We currently only handle isGlobal() because it is the only kind of
1842 // object we are going to end up with here for now.
1843 // In the future we probably should add isSymbol(), etc.
1844 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
1845 MO.isJTI() || MO.isCPI())
1846 return true;
1847
1848 // If the extendable operand is not 'Immediate' type, the instruction should
1849 // have 'isExtended' flag set.
1850 assert(MO.isImm() && "Extendable operand must be Immediate type");
1851
1852 int MinValue = getMinValue(MI);
1853 int MaxValue = getMaxValue(MI);
1854 int ImmValue = MO.getImm();
1855
1856 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001857}
1858
1859
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001860bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1861 switch (MI->getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001862 case Hexagon::L4_return :
1863 case Hexagon::L4_return_t :
1864 case Hexagon::L4_return_f :
1865 case Hexagon::L4_return_tnew_pnt :
1866 case Hexagon::L4_return_fnew_pnt :
1867 case Hexagon::L4_return_tnew_pt :
1868 case Hexagon::L4_return_fnew_pt :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001869 return true;
1870 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001871 return false;
1872}
1873
1874
1875// Return true when ConsMI uses a register defined by ProdMI.
1876bool HexagonInstrInfo::isDependent(const MachineInstr *ProdMI,
1877 const MachineInstr *ConsMI) const {
1878 const MCInstrDesc &ProdMCID = ProdMI->getDesc();
1879 if (!ProdMCID.getNumDefs())
1880 return false;
1881
1882 auto &HRI = getRegisterInfo();
1883
1884 SmallVector<unsigned, 4> DefsA;
1885 SmallVector<unsigned, 4> DefsB;
1886 SmallVector<unsigned, 8> UsesA;
1887 SmallVector<unsigned, 8> UsesB;
1888
1889 parseOperands(ProdMI, DefsA, UsesA);
1890 parseOperands(ConsMI, DefsB, UsesB);
1891
1892 for (auto &RegA : DefsA)
1893 for (auto &RegB : UsesB) {
1894 // True data dependency.
1895 if (RegA == RegB)
1896 return true;
1897
1898 if (Hexagon::DoubleRegsRegClass.contains(RegA))
1899 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1900 if (RegB == *SubRegs)
1901 return true;
1902
1903 if (Hexagon::DoubleRegsRegClass.contains(RegB))
1904 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1905 if (RegA == *SubRegs)
1906 return true;
1907 }
1908
1909 return false;
1910}
1911
1912
1913// Returns true if the instruction is alread a .cur.
1914bool HexagonInstrInfo::isDotCurInst(const MachineInstr* MI) const {
1915 switch (MI->getOpcode()) {
1916 case Hexagon::V6_vL32b_cur_pi:
1917 case Hexagon::V6_vL32b_cur_ai:
1918 case Hexagon::V6_vL32b_cur_pi_128B:
1919 case Hexagon::V6_vL32b_cur_ai_128B:
1920 return true;
1921 }
1922 return false;
1923}
1924
1925
1926// Returns true, if any one of the operands is a dot new
1927// insn, whether it is predicated dot new or register dot new.
1928bool HexagonInstrInfo::isDotNewInst(const MachineInstr* MI) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001929 if (isNewValueInst(MI) || (isPredicated(*MI) && isPredicatedNew(*MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001930 return true;
1931
1932 return false;
1933}
1934
1935
1936/// Symmetrical. See if these two instructions are fit for duplex pair.
1937bool HexagonInstrInfo::isDuplexPair(const MachineInstr *MIa,
1938 const MachineInstr *MIb) const {
1939 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
1940 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
1941 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
1942}
1943
1944
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00001945bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001946 if (!MI)
1947 return false;
1948
1949 if (MI->mayLoad() || MI->mayStore() || MI->isCompare())
1950 return true;
1951
1952 // Multiply
1953 unsigned SchedClass = MI->getDesc().getSchedClass();
1954 if (SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23)
1955 return true;
1956 return false;
1957}
1958
1959
1960bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
1961 return (Opcode == Hexagon::ENDLOOP0 ||
1962 Opcode == Hexagon::ENDLOOP1);
1963}
1964
1965
1966bool HexagonInstrInfo::isExpr(unsigned OpType) const {
1967 switch(OpType) {
1968 case MachineOperand::MO_MachineBasicBlock:
1969 case MachineOperand::MO_GlobalAddress:
1970 case MachineOperand::MO_ExternalSymbol:
1971 case MachineOperand::MO_JumpTableIndex:
1972 case MachineOperand::MO_ConstantPoolIndex:
1973 case MachineOperand::MO_BlockAddress:
1974 return true;
1975 default:
1976 return false;
1977 }
1978}
1979
1980
1981bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
1982 const MCInstrDesc &MID = MI->getDesc();
1983 const uint64_t F = MID.TSFlags;
1984 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
1985 return true;
1986
1987 // TODO: This is largely obsolete now. Will need to be removed
1988 // in consecutive patches.
1989 switch(MI->getOpcode()) {
1990 // TFR_FI Remains a special case.
1991 case Hexagon::TFR_FI:
1992 return true;
1993 default:
1994 return false;
1995 }
1996 return false;
1997}
1998
1999
2000// This returns true in two cases:
2001// - The OP code itself indicates that this is an extended instruction.
2002// - One of MOs has been marked with HMOTF_ConstExtended flag.
2003bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
2004 // First check if this is permanently extended op code.
2005 const uint64_t F = MI->getDesc().TSFlags;
2006 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2007 return true;
2008 // Use MO operand flags to determine if one of MI's operands
2009 // has HMOTF_ConstExtended flag set.
2010 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
2011 E = MI->operands_end(); I != E; ++I) {
2012 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
2013 return true;
2014 }
2015 return false;
2016}
2017
2018
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002019bool HexagonInstrInfo::isFloat(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002020 unsigned Opcode = MI->getOpcode();
2021 const uint64_t F = get(Opcode).TSFlags;
2022 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2023}
2024
2025
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002026// No V60 HVX VMEM with A_INDIRECT.
2027bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr *I,
2028 const MachineInstr *J) const {
2029 if (!isV60VectorInstruction(I))
2030 return false;
2031 if (!I->mayLoad() && !I->mayStore())
2032 return false;
2033 return J->isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
2034}
2035
2036
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002037bool HexagonInstrInfo::isIndirectCall(const MachineInstr *MI) const {
2038 switch (MI->getOpcode()) {
2039 case Hexagon::J2_callr :
2040 case Hexagon::J2_callrf :
2041 case Hexagon::J2_callrt :
2042 return true;
2043 }
2044 return false;
2045}
2046
2047
2048bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr *MI) const {
2049 switch (MI->getOpcode()) {
2050 case Hexagon::L4_return :
2051 case Hexagon::L4_return_t :
2052 case Hexagon::L4_return_f :
2053 case Hexagon::L4_return_fnew_pnt :
2054 case Hexagon::L4_return_fnew_pt :
2055 case Hexagon::L4_return_tnew_pnt :
2056 case Hexagon::L4_return_tnew_pt :
2057 return true;
2058 }
2059 return false;
2060}
2061
2062
2063bool HexagonInstrInfo::isJumpR(const MachineInstr *MI) const {
2064 switch (MI->getOpcode()) {
2065 case Hexagon::J2_jumpr :
2066 case Hexagon::J2_jumprt :
2067 case Hexagon::J2_jumprf :
2068 case Hexagon::J2_jumprtnewpt :
2069 case Hexagon::J2_jumprfnewpt :
2070 case Hexagon::J2_jumprtnew :
2071 case Hexagon::J2_jumprfnew :
2072 return true;
2073 }
2074 return false;
2075}
2076
2077
2078// Return true if a given MI can accomodate given offset.
2079// Use abs estimate as oppose to the exact number.
2080// TODO: This will need to be changed to use MC level
2081// definition of instruction extendable field size.
2082bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr *MI,
2083 unsigned offset) const {
2084 // This selection of jump instructions matches to that what
2085 // AnalyzeBranch can parse, plus NVJ.
2086 if (isNewValueJump(MI)) // r9:2
2087 return isInt<11>(offset);
2088
2089 switch (MI->getOpcode()) {
2090 // Still missing Jump to address condition on register value.
2091 default:
2092 return false;
2093 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2094 case Hexagon::J2_call:
2095 case Hexagon::CALLv3nr:
2096 return isInt<24>(offset);
2097 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2098 case Hexagon::J2_jumpf:
2099 case Hexagon::J2_jumptnew:
2100 case Hexagon::J2_jumptnewpt:
2101 case Hexagon::J2_jumpfnew:
2102 case Hexagon::J2_jumpfnewpt:
2103 case Hexagon::J2_callt:
2104 case Hexagon::J2_callf:
2105 return isInt<17>(offset);
2106 case Hexagon::J2_loop0i:
2107 case Hexagon::J2_loop0iext:
2108 case Hexagon::J2_loop0r:
2109 case Hexagon::J2_loop0rext:
2110 case Hexagon::J2_loop1i:
2111 case Hexagon::J2_loop1iext:
2112 case Hexagon::J2_loop1r:
2113 case Hexagon::J2_loop1rext:
2114 return isInt<9>(offset);
2115 // TODO: Add all the compound branches here. Can we do this in Relation model?
2116 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2117 case Hexagon::J4_cmpeqi_tp1_jump_nt:
2118 return isInt<11>(offset);
2119 }
2120}
2121
2122
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002123bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr *LRMI,
2124 const MachineInstr *ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002125 if (!LRMI || !ESMI)
2126 return false;
2127
2128 bool isLate = isLateResultInstr(LRMI);
2129 bool isEarly = isEarlySourceInstr(ESMI);
2130
2131 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
2132 DEBUG(LRMI->dump());
2133 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
2134 DEBUG(ESMI->dump());
2135
2136 if (isLate && isEarly) {
2137 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2138 return true;
2139 }
2140
2141 return false;
2142}
2143
2144
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002145bool HexagonInstrInfo::isLateResultInstr(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002146 if (!MI)
2147 return false;
2148
2149 switch (MI->getOpcode()) {
2150 case TargetOpcode::EXTRACT_SUBREG:
2151 case TargetOpcode::INSERT_SUBREG:
2152 case TargetOpcode::SUBREG_TO_REG:
2153 case TargetOpcode::REG_SEQUENCE:
2154 case TargetOpcode::IMPLICIT_DEF:
2155 case TargetOpcode::COPY:
2156 case TargetOpcode::INLINEASM:
2157 case TargetOpcode::PHI:
2158 return false;
2159 default:
2160 break;
2161 }
2162
2163 unsigned SchedClass = MI->getDesc().getSchedClass();
2164
2165 switch (SchedClass) {
2166 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2167 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2168 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2169 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2170 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2171 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2172 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2173 case Hexagon::Sched::V2LDST_tc_ld_SLOT01:
2174 case Hexagon::Sched::V2LDST_tc_st_SLOT0:
2175 case Hexagon::Sched::V2LDST_tc_st_SLOT01:
2176 case Hexagon::Sched::V4LDST_tc_ld_SLOT01:
2177 case Hexagon::Sched::V4LDST_tc_st_SLOT0:
2178 case Hexagon::Sched::V4LDST_tc_st_SLOT01:
2179 return false;
2180 }
2181 return true;
2182}
2183
2184
2185bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr *MI) const {
2186 if (!MI)
2187 return false;
2188
2189 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2190 // resource, but all operands can be received late like an ALU instruction.
2191 return MI->getDesc().getSchedClass() == Hexagon::Sched::CVI_VX_LATE;
2192}
2193
2194
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002195bool HexagonInstrInfo::isLoopN(const MachineInstr *MI) const {
2196 unsigned Opcode = MI->getOpcode();
2197 return Opcode == Hexagon::J2_loop0i ||
2198 Opcode == Hexagon::J2_loop0r ||
2199 Opcode == Hexagon::J2_loop0iext ||
2200 Opcode == Hexagon::J2_loop0rext ||
2201 Opcode == Hexagon::J2_loop1i ||
2202 Opcode == Hexagon::J2_loop1r ||
2203 Opcode == Hexagon::J2_loop1iext ||
2204 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002205}
2206
2207
2208bool HexagonInstrInfo::isMemOp(const MachineInstr *MI) const {
2209 switch (MI->getOpcode()) {
2210 default: return false;
2211 case Hexagon::L4_iadd_memopw_io :
2212 case Hexagon::L4_isub_memopw_io :
2213 case Hexagon::L4_add_memopw_io :
2214 case Hexagon::L4_sub_memopw_io :
2215 case Hexagon::L4_and_memopw_io :
2216 case Hexagon::L4_or_memopw_io :
2217 case Hexagon::L4_iadd_memoph_io :
2218 case Hexagon::L4_isub_memoph_io :
2219 case Hexagon::L4_add_memoph_io :
2220 case Hexagon::L4_sub_memoph_io :
2221 case Hexagon::L4_and_memoph_io :
2222 case Hexagon::L4_or_memoph_io :
2223 case Hexagon::L4_iadd_memopb_io :
2224 case Hexagon::L4_isub_memopb_io :
2225 case Hexagon::L4_add_memopb_io :
2226 case Hexagon::L4_sub_memopb_io :
2227 case Hexagon::L4_and_memopb_io :
2228 case Hexagon::L4_or_memopb_io :
2229 case Hexagon::L4_ior_memopb_io:
2230 case Hexagon::L4_ior_memoph_io:
2231 case Hexagon::L4_ior_memopw_io:
2232 case Hexagon::L4_iand_memopb_io:
2233 case Hexagon::L4_iand_memoph_io:
2234 case Hexagon::L4_iand_memopw_io:
2235 return true;
2236 }
2237 return false;
2238}
2239
2240
2241bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
2242 const uint64_t F = MI->getDesc().TSFlags;
2243 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2244}
2245
2246
2247bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2248 const uint64_t F = get(Opcode).TSFlags;
2249 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2250}
2251
2252
2253bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
2254 return isNewValueJump(MI) || isNewValueStore(MI);
2255}
2256
2257
2258bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
2259 return isNewValue(MI) && MI->isBranch();
2260}
2261
2262
2263bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2264 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2265}
2266
2267
2268bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
2269 const uint64_t F = MI->getDesc().TSFlags;
2270 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2271}
2272
2273
2274bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2275 const uint64_t F = get(Opcode).TSFlags;
2276 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2277}
2278
2279
2280// Returns true if a particular operand is extendable for an instruction.
2281bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
2282 unsigned OperandNum) const {
2283 const uint64_t F = MI->getDesc().TSFlags;
2284 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2285 == OperandNum;
2286}
2287
2288
2289bool HexagonInstrInfo::isPostIncrement(const MachineInstr* MI) const {
2290 return getAddrMode(MI) == HexagonII::PostInc;
2291}
2292
2293
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002294bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2295 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002296 assert(isPredicated(MI));
2297 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2298}
2299
2300
2301bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2302 const uint64_t F = get(Opcode).TSFlags;
2303 assert(isPredicated(Opcode));
2304 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2305}
2306
2307
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002308bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2309 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002310 return !((F >> HexagonII::PredicatedFalsePos) &
2311 HexagonII::PredicatedFalseMask);
2312}
2313
2314
2315bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2316 const uint64_t F = get(Opcode).TSFlags;
2317 // Make sure that the instruction is predicated.
2318 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2319 return !((F >> HexagonII::PredicatedFalsePos) &
2320 HexagonII::PredicatedFalseMask);
2321}
2322
2323
2324bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2325 const uint64_t F = get(Opcode).TSFlags;
2326 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2327}
2328
2329
2330bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2331 const uint64_t F = get(Opcode).TSFlags;
2332 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2333}
2334
2335
2336bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2337 const uint64_t F = get(Opcode).TSFlags;
2338 assert(get(Opcode).isBranch() &&
2339 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2340 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2341}
2342
2343
2344bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
2345 return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
Krzysztof Parzyszek181fdbd2016-03-24 19:18:48 +00002346 MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2347 MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2348 MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002349}
2350
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002351bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2352 switch (MI.getOpcode()) {
2353 // Byte
2354 case Hexagon::L2_loadrb_io:
2355 case Hexagon::L4_loadrb_ur:
2356 case Hexagon::L4_loadrb_ap:
2357 case Hexagon::L2_loadrb_pr:
2358 case Hexagon::L2_loadrb_pbr:
2359 case Hexagon::L2_loadrb_pi:
2360 case Hexagon::L2_loadrb_pci:
2361 case Hexagon::L2_loadrb_pcr:
2362 case Hexagon::L2_loadbsw2_io:
2363 case Hexagon::L4_loadbsw2_ur:
2364 case Hexagon::L4_loadbsw2_ap:
2365 case Hexagon::L2_loadbsw2_pr:
2366 case Hexagon::L2_loadbsw2_pbr:
2367 case Hexagon::L2_loadbsw2_pi:
2368 case Hexagon::L2_loadbsw2_pci:
2369 case Hexagon::L2_loadbsw2_pcr:
2370 case Hexagon::L2_loadbsw4_io:
2371 case Hexagon::L4_loadbsw4_ur:
2372 case Hexagon::L4_loadbsw4_ap:
2373 case Hexagon::L2_loadbsw4_pr:
2374 case Hexagon::L2_loadbsw4_pbr:
2375 case Hexagon::L2_loadbsw4_pi:
2376 case Hexagon::L2_loadbsw4_pci:
2377 case Hexagon::L2_loadbsw4_pcr:
2378 case Hexagon::L4_loadrb_rr:
2379 case Hexagon::L2_ploadrbt_io:
2380 case Hexagon::L2_ploadrbt_pi:
2381 case Hexagon::L2_ploadrbf_io:
2382 case Hexagon::L2_ploadrbf_pi:
2383 case Hexagon::L2_ploadrbtnew_io:
2384 case Hexagon::L2_ploadrbfnew_io:
2385 case Hexagon::L4_ploadrbt_rr:
2386 case Hexagon::L4_ploadrbf_rr:
2387 case Hexagon::L4_ploadrbtnew_rr:
2388 case Hexagon::L4_ploadrbfnew_rr:
2389 case Hexagon::L2_ploadrbtnew_pi:
2390 case Hexagon::L2_ploadrbfnew_pi:
2391 case Hexagon::L4_ploadrbt_abs:
2392 case Hexagon::L4_ploadrbf_abs:
2393 case Hexagon::L4_ploadrbtnew_abs:
2394 case Hexagon::L4_ploadrbfnew_abs:
2395 case Hexagon::L2_loadrbgp:
2396 // Half
2397 case Hexagon::L2_loadrh_io:
2398 case Hexagon::L4_loadrh_ur:
2399 case Hexagon::L4_loadrh_ap:
2400 case Hexagon::L2_loadrh_pr:
2401 case Hexagon::L2_loadrh_pbr:
2402 case Hexagon::L2_loadrh_pi:
2403 case Hexagon::L2_loadrh_pci:
2404 case Hexagon::L2_loadrh_pcr:
2405 case Hexagon::L4_loadrh_rr:
2406 case Hexagon::L2_ploadrht_io:
2407 case Hexagon::L2_ploadrht_pi:
2408 case Hexagon::L2_ploadrhf_io:
2409 case Hexagon::L2_ploadrhf_pi:
2410 case Hexagon::L2_ploadrhtnew_io:
2411 case Hexagon::L2_ploadrhfnew_io:
2412 case Hexagon::L4_ploadrht_rr:
2413 case Hexagon::L4_ploadrhf_rr:
2414 case Hexagon::L4_ploadrhtnew_rr:
2415 case Hexagon::L4_ploadrhfnew_rr:
2416 case Hexagon::L2_ploadrhtnew_pi:
2417 case Hexagon::L2_ploadrhfnew_pi:
2418 case Hexagon::L4_ploadrht_abs:
2419 case Hexagon::L4_ploadrhf_abs:
2420 case Hexagon::L4_ploadrhtnew_abs:
2421 case Hexagon::L4_ploadrhfnew_abs:
2422 case Hexagon::L2_loadrhgp:
2423 return true;
2424 default:
2425 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002426 }
2427}
2428
2429
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002430bool HexagonInstrInfo::isSolo(const MachineInstr* MI) const {
2431 const uint64_t F = MI->getDesc().TSFlags;
2432 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2433}
2434
2435
2436bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr *MI) const {
2437 switch (MI->getOpcode()) {
2438 case Hexagon::STriw_pred :
2439 case Hexagon::LDriw_pred :
2440 return true;
2441 default:
2442 return false;
2443 }
2444}
2445
2446
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002447bool HexagonInstrInfo::isTailCall(const MachineInstr *MI) const {
2448 if (!MI->isBranch())
2449 return false;
2450
2451 for (auto &Op : MI->operands())
2452 if (Op.isGlobal() || Op.isSymbol())
2453 return true;
2454 return false;
2455}
2456
2457
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002458// Returns true when SU has a timing class TC1.
2459bool HexagonInstrInfo::isTC1(const MachineInstr *MI) const {
2460 unsigned SchedClass = MI->getDesc().getSchedClass();
2461 switch (SchedClass) {
2462 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2463 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2464 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2465 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2466 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2467 //case Hexagon::Sched::M_tc_1_SLOT23:
2468 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2469 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2470 return true;
2471
2472 default:
2473 return false;
2474 }
2475}
2476
2477
2478bool HexagonInstrInfo::isTC2(const MachineInstr *MI) const {
2479 unsigned SchedClass = MI->getDesc().getSchedClass();
2480 switch (SchedClass) {
2481 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
2482 case Hexagon::Sched::ALU64_tc_2_SLOT23:
2483 case Hexagon::Sched::CR_tc_2_SLOT3:
2484 case Hexagon::Sched::M_tc_2_SLOT23:
2485 case Hexagon::Sched::S_2op_tc_2_SLOT23:
2486 case Hexagon::Sched::S_3op_tc_2_SLOT23:
2487 return true;
2488
2489 default:
2490 return false;
2491 }
2492}
2493
2494
2495bool HexagonInstrInfo::isTC2Early(const MachineInstr *MI) const {
2496 unsigned SchedClass = MI->getDesc().getSchedClass();
2497 switch (SchedClass) {
2498 case Hexagon::Sched::ALU32_2op_tc_2early_SLOT0123:
2499 case Hexagon::Sched::ALU32_3op_tc_2early_SLOT0123:
2500 case Hexagon::Sched::ALU64_tc_2early_SLOT23:
2501 case Hexagon::Sched::CR_tc_2early_SLOT23:
2502 case Hexagon::Sched::CR_tc_2early_SLOT3:
2503 case Hexagon::Sched::J_tc_2early_SLOT0123:
2504 case Hexagon::Sched::J_tc_2early_SLOT2:
2505 case Hexagon::Sched::J_tc_2early_SLOT23:
2506 case Hexagon::Sched::S_2op_tc_2early_SLOT23:
2507 case Hexagon::Sched::S_3op_tc_2early_SLOT23:
2508 return true;
2509
2510 default:
2511 return false;
2512 }
2513}
2514
2515
2516bool HexagonInstrInfo::isTC4x(const MachineInstr *MI) const {
2517 if (!MI)
2518 return false;
2519
2520 unsigned SchedClass = MI->getDesc().getSchedClass();
2521 return SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23;
2522}
2523
2524
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002525// Schedule this ASAP.
2526bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr *MI1,
2527 const MachineInstr *MI2) const {
2528 if (!MI1 || !MI2)
2529 return false;
2530 if (mayBeCurLoad(MI1)) {
2531 // if (result of SU is used in Next) return true;
2532 unsigned DstReg = MI1->getOperand(0).getReg();
2533 int N = MI2->getNumOperands();
2534 for (int I = 0; I < N; I++)
2535 if (MI2->getOperand(I).isReg() && DstReg == MI2->getOperand(I).getReg())
2536 return true;
2537 }
2538 if (mayBeNewStore(MI2))
2539 if (MI2->getOpcode() == Hexagon::V6_vS32b_pi)
2540 if (MI1->getOperand(0).isReg() && MI2->getOperand(3).isReg() &&
2541 MI1->getOperand(0).getReg() == MI2->getOperand(3).getReg())
2542 return true;
2543 return false;
2544}
2545
2546
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002547bool HexagonInstrInfo::isV60VectorInstruction(const MachineInstr *MI) const {
2548 if (!MI)
2549 return false;
2550
2551 const uint64_t V = getType(MI);
2552 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2553}
2554
2555
2556// Check if the Offset is a valid auto-inc imm by Load/Store Type.
2557//
2558bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2559 if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2560 VT == MVT::v32i16 || VT == MVT::v64i8) {
2561 return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2562 Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2563 (Offset & 0x3f) == 0);
2564 }
2565 // 128B
2566 if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2567 VT == MVT::v64i16 || VT == MVT::v128i8) {
2568 return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2569 Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2570 (Offset & 0x7f) == 0);
2571 }
2572 if (VT == MVT::i64) {
2573 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2574 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2575 (Offset & 0x7) == 0);
2576 }
2577 if (VT == MVT::i32) {
2578 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2579 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2580 (Offset & 0x3) == 0);
2581 }
2582 if (VT == MVT::i16) {
2583 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2584 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2585 (Offset & 0x1) == 0);
2586 }
2587 if (VT == MVT::i8) {
2588 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2589 Offset <= Hexagon_MEMB_AUTOINC_MAX);
2590 }
2591 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002592}
2593
2594
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002595bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2596 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002597 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002598 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002599 // inserted to calculate the final address. Due to this reason, the function
2600 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002601 // We used to assert if the offset was not properly aligned, however,
2602 // there are cases where a misaligned pointer recast can cause this
2603 // problem, and we need to allow for it. The front end warns of such
2604 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002605
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002606 switch (Opcode) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002607 case Hexagon::STriq_pred_V6:
2608 case Hexagon::STriq_pred_vec_V6:
2609 case Hexagon::STriv_pseudo_V6:
2610 case Hexagon::STrivv_pseudo_V6:
2611 case Hexagon::LDriq_pred_V6:
2612 case Hexagon::LDriq_pred_vec_V6:
2613 case Hexagon::LDriv_pseudo_V6:
2614 case Hexagon::LDrivv_pseudo_V6:
2615 case Hexagon::LDrivv_indexed:
2616 case Hexagon::STrivv_indexed:
2617 case Hexagon::V6_vL32b_ai:
2618 case Hexagon::V6_vS32b_ai:
2619 case Hexagon::V6_vL32Ub_ai:
2620 case Hexagon::V6_vS32Ub_ai:
2621 return (Offset >= Hexagon_MEMV_OFFSET_MIN) &&
2622 (Offset <= Hexagon_MEMV_OFFSET_MAX);
2623
2624 case Hexagon::STriq_pred_V6_128B:
2625 case Hexagon::STriq_pred_vec_V6_128B:
2626 case Hexagon::STriv_pseudo_V6_128B:
2627 case Hexagon::STrivv_pseudo_V6_128B:
2628 case Hexagon::LDriq_pred_V6_128B:
2629 case Hexagon::LDriq_pred_vec_V6_128B:
2630 case Hexagon::LDriv_pseudo_V6_128B:
2631 case Hexagon::LDrivv_pseudo_V6_128B:
2632 case Hexagon::LDrivv_indexed_128B:
2633 case Hexagon::STrivv_indexed_128B:
2634 case Hexagon::V6_vL32b_ai_128B:
2635 case Hexagon::V6_vS32b_ai_128B:
2636 case Hexagon::V6_vL32Ub_ai_128B:
2637 case Hexagon::V6_vS32Ub_ai_128B:
2638 return (Offset >= Hexagon_MEMV_OFFSET_MIN_128B) &&
2639 (Offset <= Hexagon_MEMV_OFFSET_MAX_128B);
2640
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002641 case Hexagon::J2_loop0i:
2642 case Hexagon::J2_loop1i:
2643 return isUInt<10>(Offset);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00002644
2645 case Hexagon::S4_storeirb_io:
2646 case Hexagon::S4_storeirbt_io:
2647 case Hexagon::S4_storeirbf_io:
2648 return isUInt<6>(Offset);
2649
2650 case Hexagon::S4_storeirh_io:
2651 case Hexagon::S4_storeirht_io:
2652 case Hexagon::S4_storeirhf_io:
2653 return isShiftedUInt<6,1>(Offset);
2654
2655 case Hexagon::S4_storeiri_io:
2656 case Hexagon::S4_storeirit_io:
2657 case Hexagon::S4_storeirif_io:
2658 return isShiftedUInt<6,2>(Offset);
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002659 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002660
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002661 if (Extend)
2662 return true;
2663
2664 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002665 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002666 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002667 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2668 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2669
Colin LeMahieu947cd702014-12-23 20:44:59 +00002670 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002671 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002672 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2673 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2674
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002675 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002676 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002677 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002678 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2679 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2680
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002681 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002682 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002683 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002684 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2685 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2686
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002687 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002688 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2689 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2690
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002691 case Hexagon::L4_iadd_memopw_io :
2692 case Hexagon::L4_isub_memopw_io :
2693 case Hexagon::L4_add_memopw_io :
2694 case Hexagon::L4_sub_memopw_io :
2695 case Hexagon::L4_and_memopw_io :
2696 case Hexagon::L4_or_memopw_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002697 return (0 <= Offset && Offset <= 255);
2698
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002699 case Hexagon::L4_iadd_memoph_io :
2700 case Hexagon::L4_isub_memoph_io :
2701 case Hexagon::L4_add_memoph_io :
2702 case Hexagon::L4_sub_memoph_io :
2703 case Hexagon::L4_and_memoph_io :
2704 case Hexagon::L4_or_memoph_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002705 return (0 <= Offset && Offset <= 127);
2706
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002707 case Hexagon::L4_iadd_memopb_io :
2708 case Hexagon::L4_isub_memopb_io :
2709 case Hexagon::L4_add_memopb_io :
2710 case Hexagon::L4_sub_memopb_io :
2711 case Hexagon::L4_and_memopb_io :
2712 case Hexagon::L4_or_memopb_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002713 return (0 <= Offset && Offset <= 63);
2714
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002715 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002716 // any size. Later pass knows how to handle it.
2717 case Hexagon::STriw_pred:
2718 case Hexagon::LDriw_pred:
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00002719 case Hexagon::STriw_mod:
2720 case Hexagon::LDriw_mod:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002721 return true;
2722
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002723 case Hexagon::TFR_FI:
2724 case Hexagon::TFR_FIA:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002725 case Hexagon::INLINEASM:
2726 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002727
2728 case Hexagon::L2_ploadrbt_io:
2729 case Hexagon::L2_ploadrbf_io:
2730 case Hexagon::L2_ploadrubt_io:
2731 case Hexagon::L2_ploadrubf_io:
2732 case Hexagon::S2_pstorerbt_io:
2733 case Hexagon::S2_pstorerbf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002734 return isUInt<6>(Offset);
2735
2736 case Hexagon::L2_ploadrht_io:
2737 case Hexagon::L2_ploadrhf_io:
2738 case Hexagon::L2_ploadruht_io:
2739 case Hexagon::L2_ploadruhf_io:
2740 case Hexagon::S2_pstorerht_io:
2741 case Hexagon::S2_pstorerhf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002742 return isShiftedUInt<6,1>(Offset);
2743
2744 case Hexagon::L2_ploadrit_io:
2745 case Hexagon::L2_ploadrif_io:
2746 case Hexagon::S2_pstorerit_io:
2747 case Hexagon::S2_pstorerif_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002748 return isShiftedUInt<6,2>(Offset);
2749
2750 case Hexagon::L2_ploadrdt_io:
2751 case Hexagon::L2_ploadrdf_io:
2752 case Hexagon::S2_pstorerdt_io:
2753 case Hexagon::S2_pstorerdf_io:
2754 return isShiftedUInt<6,3>(Offset);
2755 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002756
Benjamin Kramerb6684012011-12-27 11:41:05 +00002757 llvm_unreachable("No offset range is defined for this opcode. "
2758 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002759}
2760
2761
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002762bool HexagonInstrInfo::isVecAcc(const MachineInstr *MI) const {
2763 return MI && isV60VectorInstruction(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002764}
2765
2766
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002767bool HexagonInstrInfo::isVecALU(const MachineInstr *MI) const {
2768 if (!MI)
Andrew Trickd06df962012-02-01 22:13:57 +00002769 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002770 const uint64_t F = get(MI->getOpcode()).TSFlags;
2771 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2772 return
2773 V == HexagonII::TypeCVI_VA ||
2774 V == HexagonII::TypeCVI_VA_DV;
2775}
Andrew Trickd06df962012-02-01 22:13:57 +00002776
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002777
2778bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr *ProdMI,
2779 const MachineInstr *ConsMI) const {
2780 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2781 return true;
2782
2783 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2784 return true;
2785
2786 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002787 return true;
2788
2789 return false;
2790}
Jyotsna Verma84256432013-03-01 17:37:13 +00002791
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002792bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2793 switch (MI.getOpcode()) {
2794 // Byte
2795 case Hexagon::L2_loadrub_io:
2796 case Hexagon::L4_loadrub_ur:
2797 case Hexagon::L4_loadrub_ap:
2798 case Hexagon::L2_loadrub_pr:
2799 case Hexagon::L2_loadrub_pbr:
2800 case Hexagon::L2_loadrub_pi:
2801 case Hexagon::L2_loadrub_pci:
2802 case Hexagon::L2_loadrub_pcr:
2803 case Hexagon::L2_loadbzw2_io:
2804 case Hexagon::L4_loadbzw2_ur:
2805 case Hexagon::L4_loadbzw2_ap:
2806 case Hexagon::L2_loadbzw2_pr:
2807 case Hexagon::L2_loadbzw2_pbr:
2808 case Hexagon::L2_loadbzw2_pi:
2809 case Hexagon::L2_loadbzw2_pci:
2810 case Hexagon::L2_loadbzw2_pcr:
2811 case Hexagon::L2_loadbzw4_io:
2812 case Hexagon::L4_loadbzw4_ur:
2813 case Hexagon::L4_loadbzw4_ap:
2814 case Hexagon::L2_loadbzw4_pr:
2815 case Hexagon::L2_loadbzw4_pbr:
2816 case Hexagon::L2_loadbzw4_pi:
2817 case Hexagon::L2_loadbzw4_pci:
2818 case Hexagon::L2_loadbzw4_pcr:
2819 case Hexagon::L4_loadrub_rr:
2820 case Hexagon::L2_ploadrubt_io:
2821 case Hexagon::L2_ploadrubt_pi:
2822 case Hexagon::L2_ploadrubf_io:
2823 case Hexagon::L2_ploadrubf_pi:
2824 case Hexagon::L2_ploadrubtnew_io:
2825 case Hexagon::L2_ploadrubfnew_io:
2826 case Hexagon::L4_ploadrubt_rr:
2827 case Hexagon::L4_ploadrubf_rr:
2828 case Hexagon::L4_ploadrubtnew_rr:
2829 case Hexagon::L4_ploadrubfnew_rr:
2830 case Hexagon::L2_ploadrubtnew_pi:
2831 case Hexagon::L2_ploadrubfnew_pi:
2832 case Hexagon::L4_ploadrubt_abs:
2833 case Hexagon::L4_ploadrubf_abs:
2834 case Hexagon::L4_ploadrubtnew_abs:
2835 case Hexagon::L4_ploadrubfnew_abs:
2836 case Hexagon::L2_loadrubgp:
2837 // Half
2838 case Hexagon::L2_loadruh_io:
2839 case Hexagon::L4_loadruh_ur:
2840 case Hexagon::L4_loadruh_ap:
2841 case Hexagon::L2_loadruh_pr:
2842 case Hexagon::L2_loadruh_pbr:
2843 case Hexagon::L2_loadruh_pi:
2844 case Hexagon::L2_loadruh_pci:
2845 case Hexagon::L2_loadruh_pcr:
2846 case Hexagon::L4_loadruh_rr:
2847 case Hexagon::L2_ploadruht_io:
2848 case Hexagon::L2_ploadruht_pi:
2849 case Hexagon::L2_ploadruhf_io:
2850 case Hexagon::L2_ploadruhf_pi:
2851 case Hexagon::L2_ploadruhtnew_io:
2852 case Hexagon::L2_ploadruhfnew_io:
2853 case Hexagon::L4_ploadruht_rr:
2854 case Hexagon::L4_ploadruhf_rr:
2855 case Hexagon::L4_ploadruhtnew_rr:
2856 case Hexagon::L4_ploadruhfnew_rr:
2857 case Hexagon::L2_ploadruhtnew_pi:
2858 case Hexagon::L2_ploadruhfnew_pi:
2859 case Hexagon::L4_ploadruht_abs:
2860 case Hexagon::L4_ploadruhf_abs:
2861 case Hexagon::L4_ploadruhtnew_abs:
2862 case Hexagon::L4_ploadruhfnew_abs:
2863 case Hexagon::L2_loadruhgp:
2864 return true;
2865 default:
2866 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002867 }
2868}
2869
2870
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002871// Add latency to instruction.
2872bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr *MI1,
2873 const MachineInstr *MI2) const {
2874 if (isV60VectorInstruction(MI1) && isV60VectorInstruction(MI2))
2875 if (!isVecUsableNextPacket(MI1, MI2))
2876 return true;
2877 return false;
2878}
2879
2880
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002881/// \brief Can these instructions execute at the same time in a bundle.
2882bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr *First,
2883 const MachineInstr *Second) const {
2884 if (DisableNVSchedule)
2885 return false;
2886 if (mayBeNewStore(Second)) {
2887 // Make sure the definition of the first instruction is the value being
2888 // stored.
2889 const MachineOperand &Stored =
2890 Second->getOperand(Second->getNumOperands() - 1);
2891 if (!Stored.isReg())
2892 return false;
2893 for (unsigned i = 0, e = First->getNumOperands(); i < e; ++i) {
2894 const MachineOperand &Op = First->getOperand(i);
2895 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2896 return true;
2897 }
2898 }
2899 return false;
2900}
2901
2902
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002903bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2904 for (auto &I : *B)
2905 if (I.isEHLabel())
2906 return true;
2907 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002908}
2909
Jyotsna Verma84256432013-03-01 17:37:13 +00002910
2911// Returns true if an instruction can be converted into a non-extended
2912// equivalent instruction.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002913bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr *MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002914 short NonExtOpcode;
2915 // Check if the instruction has a register form that uses register in place
2916 // of the extended operand, if so return that as the non-extended form.
2917 if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
2918 return true;
2919
2920 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002921 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002922
2923 switch (getAddrMode(MI)) {
2924 case HexagonII::Absolute :
2925 // Load/store with absolute addressing mode can be converted into
2926 // base+offset mode.
Krzysztof Parzyszek02579052015-10-20 19:21:05 +00002927 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI->getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002928 break;
2929 case HexagonII::BaseImmOffset :
2930 // Load/store with base+offset addressing mode can be converted into
2931 // base+register offset addressing mode. However left shift operand should
2932 // be set to 0.
2933 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
2934 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002935 case HexagonII::BaseLongOffset:
2936 NonExtOpcode = Hexagon::getRegShlForm(MI->getOpcode());
2937 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002938 default:
2939 return false;
2940 }
2941 if (NonExtOpcode < 0)
2942 return false;
2943 return true;
2944 }
2945 return false;
2946}
2947
Jyotsna Verma84256432013-03-01 17:37:13 +00002948
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002949bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002950 return Hexagon::getRealHWInstr(MI->getOpcode(),
2951 Hexagon::InstrType_Pseudo) >= 0;
2952}
2953
2954
2955bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2956 const {
2957 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2958 while (I != E) {
2959 if (I->isBarrier())
2960 return true;
2961 ++I;
2962 }
2963 return false;
2964}
2965
2966
2967// Returns true, if a LD insn can be promoted to a cur load.
2968bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr *MI) const {
2969 auto &HST = MI->getParent()->getParent()->getSubtarget<HexagonSubtarget>();
2970 const uint64_t F = MI->getDesc().TSFlags;
2971 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
2972 HST.hasV60TOps();
2973}
2974
2975
2976// Returns true, if a ST insn can be promoted to a new-value store.
2977bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
2978 const uint64_t F = MI->getDesc().TSFlags;
2979 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
2980}
2981
2982
2983bool HexagonInstrInfo::producesStall(const MachineInstr *ProdMI,
2984 const MachineInstr *ConsMI) const {
2985 // There is no stall when ProdMI is not a V60 vector.
2986 if (!isV60VectorInstruction(ProdMI))
2987 return false;
2988
2989 // There is no stall when ProdMI and ConsMI are not dependent.
2990 if (!isDependent(ProdMI, ConsMI))
2991 return false;
2992
2993 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
2994 // are scheduled in consecutive packets.
2995 if (isVecUsableNextPacket(ProdMI, ConsMI))
2996 return false;
2997
2998 return true;
2999}
3000
3001
3002bool HexagonInstrInfo::producesStall(const MachineInstr *MI,
3003 MachineBasicBlock::const_instr_iterator BII) const {
3004 // There is no stall when I is not a V60 vector.
3005 if (!isV60VectorInstruction(MI))
3006 return false;
3007
3008 MachineBasicBlock::const_instr_iterator MII = BII;
3009 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
3010
3011 if (!(*MII).isBundle()) {
3012 const MachineInstr *J = &*MII;
3013 if (!isV60VectorInstruction(J))
3014 return false;
3015 else if (isVecUsableNextPacket(J, MI))
3016 return false;
3017 return true;
3018 }
3019
3020 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
3021 const MachineInstr *J = &*MII;
3022 if (producesStall(J, MI))
3023 return true;
3024 }
3025 return false;
3026}
3027
3028
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003029bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr *MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003030 unsigned PredReg) const {
3031 for (unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++) {
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003032 const MachineOperand &MO = MI->getOperand(opNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003033 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
3034 return false; // Predicate register must be explicitly defined.
3035 }
3036
3037 // Hexagon Programmer's Reference says that decbin, memw_locked, and
3038 // memd_locked cannot be used as .new as well,
3039 // but we don't seem to have these instructions defined.
3040 return MI->getOpcode() != Hexagon::A4_tlbmatch;
3041}
3042
3043
3044bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
3045 return (Opcode == Hexagon::J2_jumpt) ||
3046 (Opcode == Hexagon::J2_jumpf) ||
3047 (Opcode == Hexagon::J2_jumptnew) ||
3048 (Opcode == Hexagon::J2_jumpfnew) ||
3049 (Opcode == Hexagon::J2_jumptnewpt) ||
3050 (Opcode == Hexagon::J2_jumpfnewpt);
3051}
3052
3053
3054bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
3055 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
3056 return false;
3057 return !isPredicatedTrue(Cond[0].getImm());
3058}
3059
3060
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003061short HexagonInstrInfo::getAbsoluteForm(const MachineInstr *MI) const {
3062 return Hexagon::getAbsoluteForm(MI->getOpcode());
3063}
3064
3065
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003066unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
3067 const uint64_t F = MI->getDesc().TSFlags;
3068 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3069}
3070
3071
3072// Returns the base register in a memory access (load/store). The offset is
3073// returned in Offset and the access size is returned in AccessSize.
3074unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr *MI,
3075 int &Offset, unsigned &AccessSize) const {
3076 // Return if it is not a base+offset type instruction or a MemOp.
3077 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3078 getAddrMode(MI) != HexagonII::BaseLongOffset &&
3079 !isMemOp(MI) && !isPostIncrement(MI))
3080 return 0;
3081
3082 // Since it is a memory access instruction, getMemAccessSize() should never
3083 // return 0.
3084 assert (getMemAccessSize(MI) &&
3085 "BaseImmOffset or BaseLongOffset or MemOp without accessSize");
3086
3087 // Return Values of getMemAccessSize() are
3088 // 0 - Checked in the assert above.
3089 // 1, 2, 3, 4 & 7, 8 - The statement below is correct for all these.
3090 // MemAccessSize is represented as 1+log2(N) where N is size in bits.
3091 AccessSize = (1U << (getMemAccessSize(MI) - 1));
3092
3093 unsigned basePos = 0, offsetPos = 0;
3094 if (!getBaseAndOffsetPosition(MI, basePos, offsetPos))
3095 return 0;
3096
3097 // Post increment updates its EA after the mem access,
3098 // so we need to treat its offset as zero.
3099 if (isPostIncrement(MI))
3100 Offset = 0;
3101 else {
3102 Offset = MI->getOperand(offsetPos).getImm();
3103 }
3104
3105 return MI->getOperand(basePos).getReg();
3106}
3107
3108
3109/// Return the position of the base and offset operands for this instruction.
3110bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr *MI,
3111 unsigned &BasePos, unsigned &OffsetPos) const {
3112 // Deal with memops first.
3113 if (isMemOp(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003114 BasePos = 0;
3115 OffsetPos = 1;
3116 } else if (MI->mayStore()) {
3117 BasePos = 0;
3118 OffsetPos = 1;
3119 } else if (MI->mayLoad()) {
3120 BasePos = 1;
3121 OffsetPos = 2;
3122 } else
3123 return false;
3124
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003125 if (isPredicated(*MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003126 BasePos++;
3127 OffsetPos++;
3128 }
3129 if (isPostIncrement(MI)) {
3130 BasePos++;
3131 OffsetPos++;
3132 }
3133
3134 if (!MI->getOperand(BasePos).isReg() || !MI->getOperand(OffsetPos).isImm())
3135 return false;
3136
3137 return true;
3138}
3139
3140
3141// Inserts branching instructions in reverse order of their occurence.
3142// e.g. jump_t t1 (i1)
3143// jump t2 (i2)
3144// Jumpers = {i2, i1}
3145SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3146 MachineBasicBlock& MBB) const {
3147 SmallVector<MachineInstr*, 2> Jumpers;
3148 // If the block has no terminators, it just falls into the block after it.
3149 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3150 if (I == MBB.instr_begin())
3151 return Jumpers;
3152
3153 // A basic block may looks like this:
3154 //
3155 // [ insn
3156 // EH_LABEL
3157 // insn
3158 // insn
3159 // insn
3160 // EH_LABEL
3161 // insn ]
3162 //
3163 // It has two succs but does not have a terminator
3164 // Don't know how to handle it.
3165 do {
3166 --I;
3167 if (I->isEHLabel())
3168 return Jumpers;
3169 } while (I != MBB.instr_begin());
3170
3171 I = MBB.instr_end();
3172 --I;
3173
3174 while (I->isDebugValue()) {
3175 if (I == MBB.instr_begin())
3176 return Jumpers;
3177 --I;
3178 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003179 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003180 return Jumpers;
3181
3182 // Get the last instruction in the block.
3183 MachineInstr *LastInst = &*I;
3184 Jumpers.push_back(LastInst);
3185 MachineInstr *SecondLastInst = nullptr;
3186 // Find one more terminator if present.
3187 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003188 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003189 if (!SecondLastInst) {
3190 SecondLastInst = &*I;
3191 Jumpers.push_back(SecondLastInst);
3192 } else // This is a third branch.
3193 return Jumpers;
3194 }
3195 if (I == MBB.instr_begin())
3196 break;
3197 --I;
3198 } while (true);
3199 return Jumpers;
3200}
3201
3202
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003203short HexagonInstrInfo::getBaseWithLongOffset(short Opcode) const {
3204 if (Opcode < 0)
3205 return -1;
3206 return Hexagon::getBaseWithLongOffset(Opcode);
3207}
3208
3209
3210short HexagonInstrInfo::getBaseWithLongOffset(const MachineInstr *MI) const {
3211 return Hexagon::getBaseWithLongOffset(MI->getOpcode());
3212}
3213
3214
3215short HexagonInstrInfo::getBaseWithRegOffset(const MachineInstr *MI) const {
3216 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
3217}
3218
3219
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003220// Returns Operand Index for the constant extended instruction.
3221unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
3222 const uint64_t F = MI->getDesc().TSFlags;
3223 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3224}
3225
3226// See if instruction could potentially be a duplex candidate.
3227// If so, return its group. Zero otherwise.
3228HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
3229 const MachineInstr *MI) const {
3230 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3231
3232 switch (MI->getOpcode()) {
3233 default:
3234 return HexagonII::HCG_None;
3235 //
3236 // Compound pairs.
3237 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3238 // "Rd16=#U6 ; jump #r9:2"
3239 // "Rd16=Rs16 ; jump #r9:2"
3240 //
3241 case Hexagon::C2_cmpeq:
3242 case Hexagon::C2_cmpgt:
3243 case Hexagon::C2_cmpgtu:
3244 DstReg = MI->getOperand(0).getReg();
3245 Src1Reg = MI->getOperand(1).getReg();
3246 Src2Reg = MI->getOperand(2).getReg();
3247 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3248 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3249 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3250 return HexagonII::HCG_A;
3251 break;
3252 case Hexagon::C2_cmpeqi:
3253 case Hexagon::C2_cmpgti:
3254 case Hexagon::C2_cmpgtui:
3255 // P0 = cmp.eq(Rs,#u2)
3256 DstReg = MI->getOperand(0).getReg();
3257 SrcReg = MI->getOperand(1).getReg();
3258 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3259 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3260 isIntRegForSubInst(SrcReg) && MI->getOperand(2).isImm() &&
3261 ((isUInt<5>(MI->getOperand(2).getImm())) ||
3262 (MI->getOperand(2).getImm() == -1)))
3263 return HexagonII::HCG_A;
3264 break;
3265 case Hexagon::A2_tfr:
3266 // Rd = Rs
3267 DstReg = MI->getOperand(0).getReg();
3268 SrcReg = MI->getOperand(1).getReg();
3269 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3270 return HexagonII::HCG_A;
3271 break;
3272 case Hexagon::A2_tfrsi:
3273 // Rd = #u6
3274 // Do not test for #u6 size since the const is getting extended
3275 // regardless and compound could be formed.
3276 DstReg = MI->getOperand(0).getReg();
3277 if (isIntRegForSubInst(DstReg))
3278 return HexagonII::HCG_A;
3279 break;
3280 case Hexagon::S2_tstbit_i:
3281 DstReg = MI->getOperand(0).getReg();
3282 Src1Reg = MI->getOperand(1).getReg();
3283 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3284 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3285 MI->getOperand(2).isImm() &&
3286 isIntRegForSubInst(Src1Reg) && (MI->getOperand(2).getImm() == 0))
3287 return HexagonII::HCG_A;
3288 break;
3289 // The fact that .new form is used pretty much guarantees
3290 // that predicate register will match. Nevertheless,
3291 // there could be some false positives without additional
3292 // checking.
3293 case Hexagon::J2_jumptnew:
3294 case Hexagon::J2_jumpfnew:
3295 case Hexagon::J2_jumptnewpt:
3296 case Hexagon::J2_jumpfnewpt:
3297 Src1Reg = MI->getOperand(0).getReg();
3298 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3299 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3300 return HexagonII::HCG_B;
3301 break;
3302 // Transfer and jump:
3303 // Rd=#U6 ; jump #r9:2
3304 // Rd=Rs ; jump #r9:2
3305 // Do not test for jump range here.
3306 case Hexagon::J2_jump:
3307 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3308 return HexagonII::HCG_C;
3309 break;
3310 }
3311
3312 return HexagonII::HCG_None;
3313}
3314
3315
3316// Returns -1 when there is no opcode found.
3317unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr *GA,
3318 const MachineInstr *GB) const {
3319 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3320 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
3321 if ((GA->getOpcode() != Hexagon::C2_cmpeqi) ||
3322 (GB->getOpcode() != Hexagon::J2_jumptnew))
3323 return -1;
3324 unsigned DestReg = GA->getOperand(0).getReg();
3325 if (!GB->readsRegister(DestReg))
3326 return -1;
3327 if (DestReg == Hexagon::P0)
3328 return Hexagon::J4_cmpeqi_tp0_jump_nt;
3329 if (DestReg == Hexagon::P1)
3330 return Hexagon::J4_cmpeqi_tp1_jump_nt;
3331 return -1;
3332}
3333
3334
3335int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3336 enum Hexagon::PredSense inPredSense;
3337 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3338 Hexagon::PredSense_true;
3339 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3340 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3341 return CondOpcode;
3342
3343 // This switch case will be removed once all the instructions have been
3344 // modified to use relation maps.
3345 switch(Opc) {
3346 case Hexagon::TFRI_f:
3347 return !invertPredicate ? Hexagon::TFRI_cPt_f :
3348 Hexagon::TFRI_cNotPt_f;
3349 }
3350
3351 llvm_unreachable("Unexpected predicable instruction");
3352}
3353
3354
3355// Return the cur value instruction for a given store.
3356int HexagonInstrInfo::getDotCurOp(const MachineInstr* MI) const {
3357 switch (MI->getOpcode()) {
3358 default: llvm_unreachable("Unknown .cur type");
3359 case Hexagon::V6_vL32b_pi:
3360 return Hexagon::V6_vL32b_cur_pi;
3361 case Hexagon::V6_vL32b_ai:
3362 return Hexagon::V6_vL32b_cur_ai;
3363 //128B
3364 case Hexagon::V6_vL32b_pi_128B:
3365 return Hexagon::V6_vL32b_cur_pi_128B;
3366 case Hexagon::V6_vL32b_ai_128B:
3367 return Hexagon::V6_vL32b_cur_ai_128B;
3368 }
3369 return 0;
3370}
3371
3372
3373
3374// The diagram below shows the steps involved in the conversion of a predicated
3375// store instruction to its .new predicated new-value form.
3376//
3377// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3378// ^ ^
3379// / \ (not OK. it will cause new-value store to be
3380// / X conditional on p0.new while R2 producer is
3381// / \ on p0)
3382// / \.
3383// p.new store p.old NV store
3384// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3385// ^ ^
3386// \ /
3387// \ /
3388// \ /
3389// p.old store
3390// [if (p0)memw(R0+#0)=R2]
3391//
3392//
3393// The following set of instructions further explains the scenario where
3394// conditional new-value store becomes invalid when promoted to .new predicate
3395// form.
3396//
3397// { 1) if (p0) r0 = add(r1, r2)
3398// 2) p0 = cmp.eq(r3, #0) }
3399//
3400// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3401// the first two instructions because in instr 1, r0 is conditional on old value
3402// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3403// is not valid for new-value stores.
3404// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3405// from the "Conditional Store" list. Because a predicated new value store
3406// would NOT be promoted to a double dot new store. See diagram below:
3407// This function returns yes for those stores that are predicated but not
3408// yet promoted to predicate dot new instructions.
3409//
3410// +---------------------+
3411// /-----| if (p0) memw(..)=r0 |---------\~
3412// || +---------------------+ ||
3413// promote || /\ /\ || promote
3414// || /||\ /||\ ||
3415// \||/ demote || \||/
3416// \/ || || \/
3417// +-------------------------+ || +-------------------------+
3418// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3419// +-------------------------+ || +-------------------------+
3420// || || ||
3421// || demote \||/
3422// promote || \/ NOT possible
3423// || || /\~
3424// \||/ || /||\~
3425// \/ || ||
3426// +-----------------------------+
3427// | if (p0.new) memw(..)=r0.new |
3428// +-----------------------------+
3429// Double Dot New Store
3430//
3431// Returns the most basic instruction for the .new predicated instructions and
3432// new-value stores.
3433// For example, all of the following instructions will be converted back to the
3434// same instruction:
3435// 1) if (p0.new) memw(R0+#0) = R1.new --->
3436// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3437// 3) if (p0.new) memw(R0+#0) = R1 --->
3438//
3439// To understand the translation of instruction 1 to its original form, consider
3440// a packet with 3 instructions.
3441// { p0 = cmp.eq(R0,R1)
3442// if (p0.new) R2 = add(R3, R4)
3443// R5 = add (R3, R1)
3444// }
3445// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3446//
3447// This instruction can be part of the previous packet only if both p0 and R2
3448// are promoted to .new values. This promotion happens in steps, first
3449// predicate register is promoted to .new and in the next iteration R2 is
3450// promoted. Therefore, in case of dependence check failure (due to R5) during
3451// next iteration, it should be converted back to its most basic form.
3452
3453
3454// Return the new value instruction for a given store.
3455int HexagonInstrInfo::getDotNewOp(const MachineInstr* MI) const {
3456 int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
3457 if (NVOpcode >= 0) // Valid new-value store instruction.
3458 return NVOpcode;
3459
3460 switch (MI->getOpcode()) {
3461 default: llvm_unreachable("Unknown .new type");
3462 case Hexagon::S4_storerb_ur:
3463 return Hexagon::S4_storerbnew_ur;
3464
3465 case Hexagon::S2_storerb_pci:
3466 return Hexagon::S2_storerb_pci;
3467
3468 case Hexagon::S2_storeri_pci:
3469 return Hexagon::S2_storeri_pci;
3470
3471 case Hexagon::S2_storerh_pci:
3472 return Hexagon::S2_storerh_pci;
3473
3474 case Hexagon::S2_storerd_pci:
3475 return Hexagon::S2_storerd_pci;
3476
3477 case Hexagon::S2_storerf_pci:
3478 return Hexagon::S2_storerf_pci;
3479
3480 case Hexagon::V6_vS32b_ai:
3481 return Hexagon::V6_vS32b_new_ai;
3482
3483 case Hexagon::V6_vS32b_pi:
3484 return Hexagon::V6_vS32b_new_pi;
3485
3486 // 128B
3487 case Hexagon::V6_vS32b_ai_128B:
3488 return Hexagon::V6_vS32b_new_ai_128B;
3489
3490 case Hexagon::V6_vS32b_pi_128B:
3491 return Hexagon::V6_vS32b_new_pi_128B;
3492 }
3493 return 0;
3494}
3495
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00003496
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003497// Returns the opcode to use when converting MI, which is a conditional jump,
3498// into a conditional instruction which uses the .new value of the predicate.
3499// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003500int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr *MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003501 const MachineBranchProbabilityInfo *MBPI) const {
3502 // We assume that block can have at most two successors.
3503 bool taken = false;
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003504 const MachineBasicBlock *Src = MI->getParent();
3505 const MachineOperand *BrTarget = &MI->getOperand(1);
3506 const MachineBasicBlock *Dst = BrTarget->getMBB();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003507
3508 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
3509 if (Prediction >= BranchProbability(1,2))
3510 taken = true;
3511
3512 switch (MI->getOpcode()) {
3513 case Hexagon::J2_jumpt:
3514 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3515 case Hexagon::J2_jumpf:
3516 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3517
3518 default:
3519 llvm_unreachable("Unexpected jump instruction.");
3520 }
3521}
3522
3523
3524// Return .new predicate version for an instruction.
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003525int HexagonInstrInfo::getDotNewPredOp(const MachineInstr *MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003526 const MachineBranchProbabilityInfo *MBPI) const {
3527 int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
3528 if (NewOpcode >= 0) // Valid predicate new instruction
3529 return NewOpcode;
3530
3531 switch (MI->getOpcode()) {
3532 // Condtional Jumps
3533 case Hexagon::J2_jumpt:
3534 case Hexagon::J2_jumpf:
3535 return getDotNewPredJumpOp(MI, MBPI);
3536
3537 default:
3538 assert(0 && "Unknown .new type");
3539 }
3540 return 0;
3541}
3542
3543
3544int HexagonInstrInfo::getDotOldOp(const int opc) const {
3545 int NewOp = opc;
3546 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3547 NewOp = Hexagon::getPredOldOpcode(NewOp);
3548 assert(NewOp >= 0 &&
3549 "Couldn't change predicate new instruction to its old form.");
3550 }
3551
3552 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3553 NewOp = Hexagon::getNonNVStore(NewOp);
3554 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3555 }
3556 return NewOp;
3557}
3558
3559
3560// See if instruction could potentially be a duplex candidate.
3561// If so, return its group. Zero otherwise.
3562HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
3563 const MachineInstr *MI) const {
3564 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3565 auto &HRI = getRegisterInfo();
3566
3567 switch (MI->getOpcode()) {
3568 default:
3569 return HexagonII::HSIG_None;
3570 //
3571 // Group L1:
3572 //
3573 // Rd = memw(Rs+#u4:2)
3574 // Rd = memub(Rs+#u4:0)
3575 case Hexagon::L2_loadri_io:
3576 DstReg = MI->getOperand(0).getReg();
3577 SrcReg = MI->getOperand(1).getReg();
3578 // Special case this one from Group L2.
3579 // Rd = memw(r29+#u5:2)
3580 if (isIntRegForSubInst(DstReg)) {
3581 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3582 HRI.getStackRegister() == SrcReg &&
3583 MI->getOperand(2).isImm() &&
3584 isShiftedUInt<5,2>(MI->getOperand(2).getImm()))
3585 return HexagonII::HSIG_L2;
3586 // Rd = memw(Rs+#u4:2)
3587 if (isIntRegForSubInst(SrcReg) &&
3588 (MI->getOperand(2).isImm() &&
3589 isShiftedUInt<4,2>(MI->getOperand(2).getImm())))
3590 return HexagonII::HSIG_L1;
3591 }
3592 break;
3593 case Hexagon::L2_loadrub_io:
3594 // Rd = memub(Rs+#u4:0)
3595 DstReg = MI->getOperand(0).getReg();
3596 SrcReg = MI->getOperand(1).getReg();
3597 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3598 MI->getOperand(2).isImm() && isUInt<4>(MI->getOperand(2).getImm()))
3599 return HexagonII::HSIG_L1;
3600 break;
3601 //
3602 // Group L2:
3603 //
3604 // Rd = memh/memuh(Rs+#u3:1)
3605 // Rd = memb(Rs+#u3:0)
3606 // Rd = memw(r29+#u5:2) - Handled above.
3607 // Rdd = memd(r29+#u5:3)
3608 // deallocframe
3609 // [if ([!]p0[.new])] dealloc_return
3610 // [if ([!]p0[.new])] jumpr r31
3611 case Hexagon::L2_loadrh_io:
3612 case Hexagon::L2_loadruh_io:
3613 // Rd = memh/memuh(Rs+#u3:1)
3614 DstReg = MI->getOperand(0).getReg();
3615 SrcReg = MI->getOperand(1).getReg();
3616 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3617 MI->getOperand(2).isImm() &&
3618 isShiftedUInt<3,1>(MI->getOperand(2).getImm()))
3619 return HexagonII::HSIG_L2;
3620 break;
3621 case Hexagon::L2_loadrb_io:
3622 // Rd = memb(Rs+#u3:0)
3623 DstReg = MI->getOperand(0).getReg();
3624 SrcReg = MI->getOperand(1).getReg();
3625 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3626 MI->getOperand(2).isImm() &&
3627 isUInt<3>(MI->getOperand(2).getImm()))
3628 return HexagonII::HSIG_L2;
3629 break;
3630 case Hexagon::L2_loadrd_io:
3631 // Rdd = memd(r29+#u5:3)
3632 DstReg = MI->getOperand(0).getReg();
3633 SrcReg = MI->getOperand(1).getReg();
3634 if (isDblRegForSubInst(DstReg, HRI) &&
3635 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3636 HRI.getStackRegister() == SrcReg &&
3637 MI->getOperand(2).isImm() &&
3638 isShiftedUInt<5,3>(MI->getOperand(2).getImm()))
3639 return HexagonII::HSIG_L2;
3640 break;
3641 // dealloc_return is not documented in Hexagon Manual, but marked
3642 // with A_SUBINSN attribute in iset_v4classic.py.
3643 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3644 case Hexagon::L4_return:
3645 case Hexagon::L2_deallocframe:
3646 return HexagonII::HSIG_L2;
3647 case Hexagon::EH_RETURN_JMPR:
3648 case Hexagon::JMPret :
3649 // jumpr r31
3650 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
3651 DstReg = MI->getOperand(0).getReg();
3652 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3653 return HexagonII::HSIG_L2;
3654 break;
3655 case Hexagon::JMPrett:
3656 case Hexagon::JMPretf:
3657 case Hexagon::JMPrettnewpt:
3658 case Hexagon::JMPretfnewpt :
3659 case Hexagon::JMPrettnew :
3660 case Hexagon::JMPretfnew :
3661 DstReg = MI->getOperand(1).getReg();
3662 SrcReg = MI->getOperand(0).getReg();
3663 // [if ([!]p0[.new])] jumpr r31
3664 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3665 (Hexagon::P0 == SrcReg)) &&
3666 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3667 return HexagonII::HSIG_L2;
3668 break;
3669 case Hexagon::L4_return_t :
3670 case Hexagon::L4_return_f :
3671 case Hexagon::L4_return_tnew_pnt :
3672 case Hexagon::L4_return_fnew_pnt :
3673 case Hexagon::L4_return_tnew_pt :
3674 case Hexagon::L4_return_fnew_pt :
3675 // [if ([!]p0[.new])] dealloc_return
3676 SrcReg = MI->getOperand(0).getReg();
3677 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3678 return HexagonII::HSIG_L2;
3679 break;
3680 //
3681 // Group S1:
3682 //
3683 // memw(Rs+#u4:2) = Rt
3684 // memb(Rs+#u4:0) = Rt
3685 case Hexagon::S2_storeri_io:
3686 // Special case this one from Group S2.
3687 // memw(r29+#u5:2) = Rt
3688 Src1Reg = MI->getOperand(0).getReg();
3689 Src2Reg = MI->getOperand(2).getReg();
3690 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3691 isIntRegForSubInst(Src2Reg) &&
3692 HRI.getStackRegister() == Src1Reg && MI->getOperand(1).isImm() &&
3693 isShiftedUInt<5,2>(MI->getOperand(1).getImm()))
3694 return HexagonII::HSIG_S2;
3695 // memw(Rs+#u4:2) = Rt
3696 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3697 MI->getOperand(1).isImm() &&
3698 isShiftedUInt<4,2>(MI->getOperand(1).getImm()))
3699 return HexagonII::HSIG_S1;
3700 break;
3701 case Hexagon::S2_storerb_io:
3702 // memb(Rs+#u4:0) = Rt
3703 Src1Reg = MI->getOperand(0).getReg();
3704 Src2Reg = MI->getOperand(2).getReg();
3705 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3706 MI->getOperand(1).isImm() && isUInt<4>(MI->getOperand(1).getImm()))
3707 return HexagonII::HSIG_S1;
3708 break;
3709 //
3710 // Group S2:
3711 //
3712 // memh(Rs+#u3:1) = Rt
3713 // memw(r29+#u5:2) = Rt
3714 // memd(r29+#s6:3) = Rtt
3715 // memw(Rs+#u4:2) = #U1
3716 // memb(Rs+#u4) = #U1
3717 // allocframe(#u5:3)
3718 case Hexagon::S2_storerh_io:
3719 // memh(Rs+#u3:1) = Rt
3720 Src1Reg = MI->getOperand(0).getReg();
3721 Src2Reg = MI->getOperand(2).getReg();
3722 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3723 MI->getOperand(1).isImm() &&
3724 isShiftedUInt<3,1>(MI->getOperand(1).getImm()))
3725 return HexagonII::HSIG_S1;
3726 break;
3727 case Hexagon::S2_storerd_io:
3728 // memd(r29+#s6:3) = Rtt
3729 Src1Reg = MI->getOperand(0).getReg();
3730 Src2Reg = MI->getOperand(2).getReg();
3731 if (isDblRegForSubInst(Src2Reg, HRI) &&
3732 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3733 HRI.getStackRegister() == Src1Reg && MI->getOperand(1).isImm() &&
3734 isShiftedInt<6,3>(MI->getOperand(1).getImm()))
3735 return HexagonII::HSIG_S2;
3736 break;
3737 case Hexagon::S4_storeiri_io:
3738 // memw(Rs+#u4:2) = #U1
3739 Src1Reg = MI->getOperand(0).getReg();
3740 if (isIntRegForSubInst(Src1Reg) && MI->getOperand(1).isImm() &&
3741 isShiftedUInt<4,2>(MI->getOperand(1).getImm()) &&
3742 MI->getOperand(2).isImm() && isUInt<1>(MI->getOperand(2).getImm()))
3743 return HexagonII::HSIG_S2;
3744 break;
3745 case Hexagon::S4_storeirb_io:
3746 // memb(Rs+#u4) = #U1
3747 Src1Reg = MI->getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003748 if (isIntRegForSubInst(Src1Reg) &&
3749 MI->getOperand(1).isImm() && isUInt<4>(MI->getOperand(1).getImm()) &&
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003750 MI->getOperand(2).isImm() && isUInt<1>(MI->getOperand(2).getImm()))
3751 return HexagonII::HSIG_S2;
3752 break;
3753 case Hexagon::S2_allocframe:
3754 if (MI->getOperand(0).isImm() &&
3755 isShiftedUInt<5,3>(MI->getOperand(0).getImm()))
3756 return HexagonII::HSIG_S1;
3757 break;
3758 //
3759 // Group A:
3760 //
3761 // Rx = add(Rx,#s7)
3762 // Rd = Rs
3763 // Rd = #u6
3764 // Rd = #-1
3765 // if ([!]P0[.new]) Rd = #0
3766 // Rd = add(r29,#u6:2)
3767 // Rx = add(Rx,Rs)
3768 // P0 = cmp.eq(Rs,#u2)
3769 // Rdd = combine(#0,Rs)
3770 // Rdd = combine(Rs,#0)
3771 // Rdd = combine(#u2,#U2)
3772 // Rd = add(Rs,#1)
3773 // Rd = add(Rs,#-1)
3774 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3775 // Rd = and(Rs,#1)
3776 case Hexagon::A2_addi:
3777 DstReg = MI->getOperand(0).getReg();
3778 SrcReg = MI->getOperand(1).getReg();
3779 if (isIntRegForSubInst(DstReg)) {
3780 // Rd = add(r29,#u6:2)
3781 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3782 HRI.getStackRegister() == SrcReg && MI->getOperand(2).isImm() &&
3783 isShiftedUInt<6,2>(MI->getOperand(2).getImm()))
3784 return HexagonII::HSIG_A;
3785 // Rx = add(Rx,#s7)
3786 if ((DstReg == SrcReg) && MI->getOperand(2).isImm() &&
3787 isInt<7>(MI->getOperand(2).getImm()))
3788 return HexagonII::HSIG_A;
3789 // Rd = add(Rs,#1)
3790 // Rd = add(Rs,#-1)
3791 if (isIntRegForSubInst(SrcReg) && MI->getOperand(2).isImm() &&
3792 ((MI->getOperand(2).getImm() == 1) ||
3793 (MI->getOperand(2).getImm() == -1)))
3794 return HexagonII::HSIG_A;
3795 }
3796 break;
3797 case Hexagon::A2_add:
3798 // Rx = add(Rx,Rs)
3799 DstReg = MI->getOperand(0).getReg();
3800 Src1Reg = MI->getOperand(1).getReg();
3801 Src2Reg = MI->getOperand(2).getReg();
3802 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3803 isIntRegForSubInst(Src2Reg))
3804 return HexagonII::HSIG_A;
3805 break;
3806 case Hexagon::A2_andir:
3807 // Same as zxtb.
3808 // Rd16=and(Rs16,#255)
3809 // Rd16=and(Rs16,#1)
3810 DstReg = MI->getOperand(0).getReg();
3811 SrcReg = MI->getOperand(1).getReg();
3812 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3813 MI->getOperand(2).isImm() &&
3814 ((MI->getOperand(2).getImm() == 1) ||
3815 (MI->getOperand(2).getImm() == 255)))
3816 return HexagonII::HSIG_A;
3817 break;
3818 case Hexagon::A2_tfr:
3819 // Rd = Rs
3820 DstReg = MI->getOperand(0).getReg();
3821 SrcReg = MI->getOperand(1).getReg();
3822 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3823 return HexagonII::HSIG_A;
3824 break;
3825 case Hexagon::A2_tfrsi:
3826 // Rd = #u6
3827 // Do not test for #u6 size since the const is getting extended
3828 // regardless and compound could be formed.
3829 // Rd = #-1
3830 DstReg = MI->getOperand(0).getReg();
3831 if (isIntRegForSubInst(DstReg))
3832 return HexagonII::HSIG_A;
3833 break;
3834 case Hexagon::C2_cmoveit:
3835 case Hexagon::C2_cmovenewit:
3836 case Hexagon::C2_cmoveif:
3837 case Hexagon::C2_cmovenewif:
3838 // if ([!]P0[.new]) Rd = #0
3839 // Actual form:
3840 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
3841 DstReg = MI->getOperand(0).getReg();
3842 SrcReg = MI->getOperand(1).getReg();
3843 if (isIntRegForSubInst(DstReg) &&
3844 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
3845 MI->getOperand(2).isImm() && MI->getOperand(2).getImm() == 0)
3846 return HexagonII::HSIG_A;
3847 break;
3848 case Hexagon::C2_cmpeqi:
3849 // P0 = cmp.eq(Rs,#u2)
3850 DstReg = MI->getOperand(0).getReg();
3851 SrcReg = MI->getOperand(1).getReg();
3852 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3853 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
3854 MI->getOperand(2).isImm() && isUInt<2>(MI->getOperand(2).getImm()))
3855 return HexagonII::HSIG_A;
3856 break;
3857 case Hexagon::A2_combineii:
3858 case Hexagon::A4_combineii:
3859 // Rdd = combine(#u2,#U2)
3860 DstReg = MI->getOperand(0).getReg();
3861 if (isDblRegForSubInst(DstReg, HRI) &&
3862 ((MI->getOperand(1).isImm() && isUInt<2>(MI->getOperand(1).getImm())) ||
3863 (MI->getOperand(1).isGlobal() &&
3864 isUInt<2>(MI->getOperand(1).getOffset()))) &&
3865 ((MI->getOperand(2).isImm() && isUInt<2>(MI->getOperand(2).getImm())) ||
3866 (MI->getOperand(2).isGlobal() &&
3867 isUInt<2>(MI->getOperand(2).getOffset()))))
3868 return HexagonII::HSIG_A;
3869 break;
3870 case Hexagon::A4_combineri:
3871 // Rdd = combine(Rs,#0)
3872 DstReg = MI->getOperand(0).getReg();
3873 SrcReg = MI->getOperand(1).getReg();
3874 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
3875 ((MI->getOperand(2).isImm() && MI->getOperand(2).getImm() == 0) ||
3876 (MI->getOperand(2).isGlobal() && MI->getOperand(2).getOffset() == 0)))
3877 return HexagonII::HSIG_A;
3878 break;
3879 case Hexagon::A4_combineir:
3880 // Rdd = combine(#0,Rs)
3881 DstReg = MI->getOperand(0).getReg();
3882 SrcReg = MI->getOperand(2).getReg();
3883 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
3884 ((MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0) ||
3885 (MI->getOperand(1).isGlobal() && MI->getOperand(1).getOffset() == 0)))
3886 return HexagonII::HSIG_A;
3887 break;
3888 case Hexagon::A2_sxtb:
3889 case Hexagon::A2_sxth:
3890 case Hexagon::A2_zxtb:
3891 case Hexagon::A2_zxth:
3892 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3893 DstReg = MI->getOperand(0).getReg();
3894 SrcReg = MI->getOperand(1).getReg();
3895 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3896 return HexagonII::HSIG_A;
3897 break;
3898 }
3899
3900 return HexagonII::HSIG_None;
3901}
3902
3903
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003904short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003905 return Hexagon::getRealHWInstr(MI->getOpcode(), Hexagon::InstrType_Real);
3906}
3907
3908
3909// Return first non-debug instruction in the basic block.
3910MachineInstr *HexagonInstrInfo::getFirstNonDbgInst(MachineBasicBlock *BB)
3911 const {
3912 for (auto MII = BB->instr_begin(), End = BB->instr_end(); MII != End; MII++) {
3913 MachineInstr *MI = &*MII;
3914 if (MI->isDebugValue())
3915 continue;
3916 return MI;
3917 }
3918 return nullptr;
3919}
3920
3921
3922unsigned HexagonInstrInfo::getInstrTimingClassLatency(
3923 const InstrItineraryData *ItinData, const MachineInstr *MI) const {
3924 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3925 // still have a MinLatency property, which getStageLatency checks.
3926 if (!ItinData)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003927 return getInstrLatency(ItinData, *MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003928
3929 // Get the latency embedded in the itinerary. If we're not using timing class
3930 // latencies or if we using BSB scheduling, then restrict the maximum latency
3931 // to 1 (that is, either 0 or 1).
3932 if (MI->isTransient())
3933 return 0;
3934 unsigned Latency = ItinData->getStageLatency(MI->getDesc().getSchedClass());
3935 if (!EnableTimingClassLatency ||
3936 MI->getParent()->getParent()->getSubtarget<HexagonSubtarget>().
3937 useBSBScheduling())
3938 if (Latency > 1)
3939 Latency = 1;
3940 return Latency;
3941}
3942
3943
3944// inverts the predication logic.
3945// p -> NotP
3946// NotP -> P
3947bool HexagonInstrInfo::getInvertedPredSense(
3948 SmallVectorImpl<MachineOperand> &Cond) const {
3949 if (Cond.empty())
3950 return false;
3951 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
3952 Cond[0].setImm(Opc);
3953 return true;
3954}
3955
3956
3957unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
3958 int InvPredOpcode;
3959 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
3960 : Hexagon::getTruePredOpcode(Opc);
3961 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
3962 return InvPredOpcode;
3963
3964 llvm_unreachable("Unexpected predicated instruction");
3965}
3966
3967
3968// Returns the max value that doesn't need to be extended.
3969int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
3970 const uint64_t F = MI->getDesc().TSFlags;
3971 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3972 & HexagonII::ExtentSignedMask;
3973 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3974 & HexagonII::ExtentBitsMask;
3975
3976 if (isSigned) // if value is signed
3977 return ~(-1U << (bits - 1));
3978 else
3979 return ~(-1U << bits);
3980}
3981
3982
3983unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr* MI) const {
3984 const uint64_t F = MI->getDesc().TSFlags;
3985 return (F >> HexagonII::MemAccessSizePos) & HexagonII::MemAccesSizeMask;
3986}
3987
3988
3989// Returns the min value that doesn't need to be extended.
3990int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
3991 const uint64_t F = MI->getDesc().TSFlags;
3992 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3993 & HexagonII::ExtentSignedMask;
3994 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3995 & HexagonII::ExtentBitsMask;
3996
3997 if (isSigned) // if value is signed
3998 return -1U << (bits - 1);
3999 else
4000 return 0;
4001}
4002
4003
4004// Returns opcode of the non-extended equivalent instruction.
4005short HexagonInstrInfo::getNonExtOpcode(const MachineInstr *MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00004006 // Check if the instruction has a register form that uses register in place
4007 // of the extended operand, if so return that as the non-extended form.
4008 short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
4009 if (NonExtOpcode >= 0)
4010 return NonExtOpcode;
4011
4012 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00004013 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00004014 switch (getAddrMode(MI)) {
4015 case HexagonII::Absolute :
Krzysztof Parzyszek02579052015-10-20 19:21:05 +00004016 return Hexagon::getBaseWithImmOffset(MI->getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004017 case HexagonII::BaseImmOffset :
4018 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004019 case HexagonII::BaseLongOffset:
4020 return Hexagon::getRegShlForm(MI->getOpcode());
4021
Jyotsna Verma84256432013-03-01 17:37:13 +00004022 default:
4023 return -1;
4024 }
4025 }
4026 return -1;
4027}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00004028
Brendon Cahoondf43e682015-05-08 16:16:29 +00004029
Ahmed Bougachac88bf542015-06-11 19:30:37 +00004030bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004031 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00004032 if (Cond.empty())
4033 return false;
4034 assert(Cond.size() == 2);
4035 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
4036 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
4037 return false;
4038 }
4039 PredReg = Cond[1].getReg();
4040 PredRegPos = 1;
4041 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4042 PredRegFlags = 0;
4043 if (Cond[1].isImplicit())
4044 PredRegFlags = RegState::Implicit;
4045 if (Cond[1].isUndef())
4046 PredRegFlags |= RegState::Undef;
4047 return true;
4048}
4049
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004050
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00004051short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004052 return Hexagon::getRealHWInstr(MI->getOpcode(), Hexagon::InstrType_Pseudo);
4053}
4054
4055
4056short HexagonInstrInfo::getRegForm(const MachineInstr *MI) const {
4057 return Hexagon::getRegForm(MI->getOpcode());
4058}
4059
4060
4061// Return the number of bytes required to encode the instruction.
4062// Hexagon instructions are fixed length, 4 bytes, unless they
4063// use a constant extender, which requires another 4 bytes.
4064// For debug instructions and prolog labels, return 0.
4065unsigned HexagonInstrInfo::getSize(const MachineInstr *MI) const {
4066 if (MI->isDebugValue() || MI->isPosition())
4067 return 0;
4068
4069 unsigned Size = MI->getDesc().getSize();
4070 if (!Size)
4071 // Assume the default insn size in case it cannot be determined
4072 // for whatever reason.
4073 Size = HEXAGON_INSTR_SIZE;
4074
4075 if (isConstExtended(MI) || isExtended(MI))
4076 Size += HEXAGON_INSTR_SIZE;
4077
4078 // Try and compute number of instructions in asm.
4079 if (BranchRelaxAsmLarge && MI->getOpcode() == Hexagon::INLINEASM) {
4080 const MachineBasicBlock &MBB = *MI->getParent();
4081 const MachineFunction *MF = MBB.getParent();
4082 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4083
4084 // Count the number of register definitions to find the asm string.
4085 unsigned NumDefs = 0;
4086 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
4087 ++NumDefs)
4088 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
4089
4090 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
4091 // Disassemble the AsmStr and approximate number of instructions.
4092 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
4093 Size = getInlineAsmLength(AsmStr, *MAI);
4094 }
4095
4096 return Size;
4097}
4098
4099
4100uint64_t HexagonInstrInfo::getType(const MachineInstr* MI) const {
4101 const uint64_t F = MI->getDesc().TSFlags;
4102 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4103}
4104
4105
4106unsigned HexagonInstrInfo::getUnits(const MachineInstr* MI) const {
4107 const TargetSubtargetInfo &ST = MI->getParent()->getParent()->getSubtarget();
4108 const InstrItineraryData &II = *ST.getInstrItineraryData();
4109 const InstrStage &IS = *II.beginStage(MI->getDesc().getSchedClass());
4110
4111 return IS.getUnits();
4112}
4113
4114
4115unsigned HexagonInstrInfo::getValidSubTargets(const unsigned Opcode) const {
4116 const uint64_t F = get(Opcode).TSFlags;
4117 return (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask;
4118}
4119
4120
4121// Calculate size of the basic block without debug instructions.
4122unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4123 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4124}
4125
4126
4127unsigned HexagonInstrInfo::nonDbgBundleSize(
4128 MachineBasicBlock::const_iterator BundleHead) const {
4129 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004130 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004131 // Skip the bundle header.
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00004132 return nonDbgMICount(++MII, getBundleEnd(*BundleHead));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004133}
4134
4135
4136/// immediateExtend - Changes the instruction in place to one using an immediate
4137/// extender.
4138void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
4139 assert((isExtendable(MI)||isConstExtended(MI)) &&
4140 "Instruction must be extendable");
4141 // Find which operand is extendable.
4142 short ExtOpNum = getCExtOpNum(MI);
4143 MachineOperand &MO = MI->getOperand(ExtOpNum);
4144 // This needs to be something we understand.
4145 assert((MO.isMBB() || MO.isImm()) &&
4146 "Branch with unknown extendable field type");
4147 // Mark given operand as extended.
4148 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4149}
4150
4151
4152bool HexagonInstrInfo::invertAndChangeJumpTarget(
4153 MachineInstr* MI, MachineBasicBlock* NewTarget) const {
4154 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
4155 << NewTarget->getNumber(); MI->dump(););
4156 assert(MI->isBranch());
4157 unsigned NewOpcode = getInvertedPredicatedOpcode(MI->getOpcode());
4158 int TargetPos = MI->getNumOperands() - 1;
4159 // In general branch target is the last operand,
4160 // but some implicit defs added at the end might change it.
4161 while ((TargetPos > -1) && !MI->getOperand(TargetPos).isMBB())
4162 --TargetPos;
4163 assert((TargetPos >= 0) && MI->getOperand(TargetPos).isMBB());
4164 MI->getOperand(TargetPos).setMBB(NewTarget);
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004165 if (EnableBranchPrediction && isPredicatedNew(*MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004166 NewOpcode = reversePrediction(NewOpcode);
4167 }
4168 MI->setDesc(get(NewOpcode));
4169 return true;
4170}
4171
4172
4173void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4174 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4175 MachineFunction::iterator A = MF.begin();
4176 MachineBasicBlock &B = *A;
4177 MachineBasicBlock::iterator I = B.begin();
4178 MachineInstr *MI = &*I;
4179 DebugLoc DL = MI->getDebugLoc();
4180 MachineInstr *NewMI;
4181
4182 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4183 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
4184 NewMI = BuildMI(B, MI, DL, get(insn));
4185 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
4186 " Class: " << NewMI->getDesc().getSchedClass());
4187 NewMI->eraseFromParent();
4188 }
4189 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4190}
4191
4192
4193// inverts the predication logic.
4194// p -> NotP
4195// NotP -> P
4196bool HexagonInstrInfo::reversePredSense(MachineInstr* MI) const {
4197 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI->dump());
4198 MI->setDesc(get(getInvertedPredicatedOpcode(MI->getOpcode())));
4199 return true;
4200}
4201
4202
4203// Reverse the branch prediction.
4204unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4205 int PredRevOpcode = -1;
4206 if (isPredictedTaken(Opcode))
4207 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4208 else
4209 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4210 assert(PredRevOpcode > 0);
4211 return PredRevOpcode;
4212}
4213
4214
4215// TODO: Add more rigorous validation.
4216bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4217 const {
4218 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4219}
4220
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004221
4222short HexagonInstrInfo::xformRegToImmOffset(const MachineInstr *MI) const {
4223 return Hexagon::xformRegToImmOffset(MI->getOpcode());
4224}