blob: df315c4f5e00395227853d462031de4f6c047300 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonInstrInfo.cpp - Hexagon Instruction Information ------------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Hexagon implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000014#include "HexagonHazardRecognizer.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000015#include "HexagonInstrInfo.h"
Craig Topperb25fda92012-03-17 18:46:09 +000016#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000017#include "HexagonSubtarget.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000018#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SmallVector.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000020#include "llvm/CodeGen/DFAPacketizer.h"
Ron Lieberman88159e52016-09-02 22:56:24 +000021#include "llvm/CodeGen/LivePhysRegs.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000024#include "llvm/CodeGen/MachineMemOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000026#include "llvm/CodeGen/PseudoSourceValue.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000027#include "llvm/CodeGen/ScheduleDAG.h"
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000028#include "llvm/MC/MCAsmInfo.h"
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +000029#include "llvm/Support/CommandLine.h"
Jyotsna Verma5ed51812013-05-01 21:37:34 +000030#include "llvm/Support/Debug.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000031#include "llvm/Support/MathExtras.h"
Reid Kleckner1c76f1552013-05-03 00:54:56 +000032#include "llvm/Support/raw_ostream.h"
Krzysztof Parzyszekaa935752015-11-24 15:11:13 +000033#include <cctype>
Tony Linthicum1213a7a2011-12-12 21:14:40 +000034
Tony Linthicum1213a7a2011-12-12 21:14:40 +000035using namespace llvm;
36
Chandler Carruthe96dd892014-04-21 22:55:11 +000037#define DEBUG_TYPE "hexagon-instrinfo"
38
Chandler Carruthd174b722014-04-22 02:03:14 +000039#define GET_INSTRINFO_CTOR_DTOR
40#define GET_INSTRMAP_INFO
41#include "HexagonGenInstrInfo.inc"
42#include "HexagonGenDFAPacketizer.inc"
43
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000044cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000045 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
46 "packetization boundary."));
47
48static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
49 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
50
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000051static cl::opt<bool> DisableNVSchedule("disable-hexagon-nv-schedule",
52 cl::Hidden, cl::ZeroOrMore, cl::init(false),
53 cl::desc("Disable schedule adjustment for new value stores."));
54
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000055static cl::opt<bool> EnableTimingClassLatency(
56 "enable-timing-class-latency", cl::Hidden, cl::init(false),
57 cl::desc("Enable timing class latency"));
58
59static cl::opt<bool> EnableALUForwarding(
60 "enable-alu-forwarding", cl::Hidden, cl::init(true),
61 cl::desc("Enable vec alu forwarding"));
62
63static cl::opt<bool> EnableACCForwarding(
64 "enable-acc-forwarding", cl::Hidden, cl::init(true),
65 cl::desc("Enable vec acc forwarding"));
66
67static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
68 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
69
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000070static cl::opt<bool> UseDFAHazardRec("dfa-hazard-rec",
71 cl::init(true), cl::Hidden, cl::ZeroOrMore,
72 cl::desc("Use the DFA based hazard recognizer."));
73
Tony Linthicum1213a7a2011-12-12 21:14:40 +000074///
75/// Constants for Hexagon instructions.
76///
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +000077const int Hexagon_MEMV_OFFSET_MAX_128B = 896; // #s4: -8*128...7*128
78const int Hexagon_MEMV_OFFSET_MIN_128B = -1024; // #s4
79const int Hexagon_MEMV_OFFSET_MAX = 448; // #s4: -8*64...7*64
80const int Hexagon_MEMV_OFFSET_MIN = -512; // #s4
Tony Linthicum1213a7a2011-12-12 21:14:40 +000081const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000082const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000083const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000084const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000085const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000086const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000087const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000088const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000089const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000090const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000091const int Hexagon_MEMD_AUTOINC_MAX = 56;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000092const int Hexagon_MEMD_AUTOINC_MIN = -64;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000093const int Hexagon_MEMW_AUTOINC_MAX = 28;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000094const int Hexagon_MEMW_AUTOINC_MIN = -32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000095const int Hexagon_MEMH_AUTOINC_MAX = 14;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000096const int Hexagon_MEMH_AUTOINC_MIN = -16;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000097const int Hexagon_MEMB_AUTOINC_MAX = 7;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000098const int Hexagon_MEMB_AUTOINC_MIN = -8;
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +000099const int Hexagon_MEMV_AUTOINC_MAX = 192; // #s3
100const int Hexagon_MEMV_AUTOINC_MIN = -256; // #s3
101const int Hexagon_MEMV_AUTOINC_MAX_128B = 384; // #s3
102const int Hexagon_MEMV_AUTOINC_MIN_128B = -512; // #s3
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000103
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000104// Pin the vtable to this file.
105void HexagonInstrInfo::anchor() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000106
107HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
Eric Christopherc4d31402015-03-10 23:45:55 +0000108 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000109 RI() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000110
111
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000112static bool isIntRegForSubInst(unsigned Reg) {
113 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
114 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000115}
116
117
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000118static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
119 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::subreg_loreg)) &&
120 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::subreg_hireg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000121}
122
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000123
124/// Calculate number of instructions excluding the debug instructions.
125static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
126 MachineBasicBlock::const_instr_iterator MIE) {
127 unsigned Count = 0;
128 for (; MIB != MIE; ++MIB) {
129 if (!MIB->isDebugValue())
130 ++Count;
131 }
132 return Count;
133}
134
135
136/// Find the hardware loop instruction used to set-up the specified loop.
137/// On Hexagon, we have two instructions used to set-up the hardware loop
138/// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
139/// to indicate the end of a loop.
140static MachineInstr *findLoopInstr(MachineBasicBlock *BB, int EndLoopOp,
141 SmallPtrSet<MachineBasicBlock *, 8> &Visited) {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000142 int LOOPi;
143 int LOOPr;
144 if (EndLoopOp == Hexagon::ENDLOOP0) {
145 LOOPi = Hexagon::J2_loop0i;
146 LOOPr = Hexagon::J2_loop0r;
147 } else { // EndLoopOp == Hexagon::EndLOOP1
148 LOOPi = Hexagon::J2_loop1i;
149 LOOPr = Hexagon::J2_loop1r;
150 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000151
Brendon Cahoondf43e682015-05-08 16:16:29 +0000152 // The loop set-up instruction will be in a predecessor block
153 for (MachineBasicBlock::pred_iterator PB = BB->pred_begin(),
154 PE = BB->pred_end(); PB != PE; ++PB) {
155 // If this has been visited, already skip it.
156 if (!Visited.insert(*PB).second)
157 continue;
158 if (*PB == BB)
159 continue;
160 for (MachineBasicBlock::reverse_instr_iterator I = (*PB)->instr_rbegin(),
161 E = (*PB)->instr_rend(); I != E; ++I) {
162 int Opc = I->getOpcode();
163 if (Opc == LOOPi || Opc == LOOPr)
164 return &*I;
165 // We've reached a different loop, which means the loop0 has been removed.
166 if (Opc == EndLoopOp)
167 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000168 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000169 // Check the predecessors for the LOOP instruction.
170 MachineInstr *loop = findLoopInstr(*PB, EndLoopOp, Visited);
171 if (loop)
172 return loop;
173 }
174 return 0;
175}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000176
Brendon Cahoondf43e682015-05-08 16:16:29 +0000177
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000178/// Gather register def/uses from MI.
179/// This treats possible (predicated) defs as actually happening ones
180/// (conservatively).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000181static inline void parseOperands(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000182 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
183 Defs.clear();
184 Uses.clear();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000185
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000186 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
187 const MachineOperand &MO = MI.getOperand(i);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000188
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000189 if (!MO.isReg())
190 continue;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000191
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000192 unsigned Reg = MO.getReg();
193 if (!Reg)
194 continue;
195
196 if (MO.isUse())
197 Uses.push_back(MO.getReg());
198
199 if (MO.isDef())
200 Defs.push_back(MO.getReg());
201 }
202}
203
204
205// Position dependent, so check twice for swap.
206static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
207 switch (Ga) {
208 case HexagonII::HSIG_None:
209 default:
210 return false;
211 case HexagonII::HSIG_L1:
212 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
213 case HexagonII::HSIG_L2:
214 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
215 Gb == HexagonII::HSIG_A);
216 case HexagonII::HSIG_S1:
217 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
218 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
219 case HexagonII::HSIG_S2:
220 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
221 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
222 Gb == HexagonII::HSIG_A);
223 case HexagonII::HSIG_A:
224 return (Gb == HexagonII::HSIG_A);
225 case HexagonII::HSIG_Compound:
226 return (Gb == HexagonII::HSIG_Compound);
227 }
228 return false;
229}
230
231
232
233/// isLoadFromStackSlot - If the specified machine instruction is a direct
234/// load from a stack slot, return the virtual or physical register number of
235/// the destination along with the FrameIndex of the loaded stack slot. If
236/// not, return 0. This predicate must return 0 if the instruction has
237/// any side effects other than loading from the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000238unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000239 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000240 switch (MI.getOpcode()) {
241 default:
242 break;
243 case Hexagon::L2_loadrb_io:
244 case Hexagon::L2_loadrub_io:
245 case Hexagon::L2_loadrh_io:
246 case Hexagon::L2_loadruh_io:
247 case Hexagon::L2_loadri_io:
248 case Hexagon::L2_loadrd_io:
249 case Hexagon::V6_vL32b_ai:
250 case Hexagon::V6_vL32b_ai_128B:
251 case Hexagon::V6_vL32Ub_ai:
252 case Hexagon::V6_vL32Ub_ai_128B:
253 case Hexagon::LDriw_pred:
254 case Hexagon::LDriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000255 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000256 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000257 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000258 case Hexagon::PS_vloadrw_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000259 const MachineOperand OpFI = MI.getOperand(1);
260 if (!OpFI.isFI())
261 return 0;
262 const MachineOperand OpOff = MI.getOperand(2);
263 if (!OpOff.isImm() || OpOff.getImm() != 0)
264 return 0;
265 FrameIndex = OpFI.getIndex();
266 return MI.getOperand(0).getReg();
267 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000268
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000269 case Hexagon::L2_ploadrbt_io:
270 case Hexagon::L2_ploadrbf_io:
271 case Hexagon::L2_ploadrubt_io:
272 case Hexagon::L2_ploadrubf_io:
273 case Hexagon::L2_ploadrht_io:
274 case Hexagon::L2_ploadrhf_io:
275 case Hexagon::L2_ploadruht_io:
276 case Hexagon::L2_ploadruhf_io:
277 case Hexagon::L2_ploadrit_io:
278 case Hexagon::L2_ploadrif_io:
279 case Hexagon::L2_ploadrdt_io:
280 case Hexagon::L2_ploadrdf_io: {
281 const MachineOperand OpFI = MI.getOperand(2);
282 if (!OpFI.isFI())
283 return 0;
284 const MachineOperand OpOff = MI.getOperand(3);
285 if (!OpOff.isImm() || OpOff.getImm() != 0)
286 return 0;
287 FrameIndex = OpFI.getIndex();
288 return MI.getOperand(0).getReg();
289 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000290 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000291
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000292 return 0;
293}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000294
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000295
296/// isStoreToStackSlot - If the specified machine instruction is a direct
297/// store to a stack slot, return the virtual or physical register number of
298/// the source reg along with the FrameIndex of the loaded stack slot. If
299/// not, return 0. This predicate must return 0 if the instruction has
300/// any side effects other than storing to the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000301unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000302 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000303 switch (MI.getOpcode()) {
304 default:
305 break;
306 case Hexagon::S2_storerb_io:
307 case Hexagon::S2_storerh_io:
308 case Hexagon::S2_storeri_io:
309 case Hexagon::S2_storerd_io:
310 case Hexagon::V6_vS32b_ai:
311 case Hexagon::V6_vS32b_ai_128B:
312 case Hexagon::V6_vS32Ub_ai:
313 case Hexagon::V6_vS32Ub_ai_128B:
314 case Hexagon::STriw_pred:
315 case Hexagon::STriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000316 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000317 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000318 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000319 case Hexagon::PS_vstorerw_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000320 const MachineOperand &OpFI = MI.getOperand(0);
321 if (!OpFI.isFI())
322 return 0;
323 const MachineOperand &OpOff = MI.getOperand(1);
324 if (!OpOff.isImm() || OpOff.getImm() != 0)
325 return 0;
326 FrameIndex = OpFI.getIndex();
327 return MI.getOperand(2).getReg();
328 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000329
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000330 case Hexagon::S2_pstorerbt_io:
331 case Hexagon::S2_pstorerbf_io:
332 case Hexagon::S2_pstorerht_io:
333 case Hexagon::S2_pstorerhf_io:
334 case Hexagon::S2_pstorerit_io:
335 case Hexagon::S2_pstorerif_io:
336 case Hexagon::S2_pstorerdt_io:
337 case Hexagon::S2_pstorerdf_io: {
338 const MachineOperand &OpFI = MI.getOperand(1);
339 if (!OpFI.isFI())
340 return 0;
341 const MachineOperand &OpOff = MI.getOperand(2);
342 if (!OpOff.isImm() || OpOff.getImm() != 0)
343 return 0;
344 FrameIndex = OpFI.getIndex();
345 return MI.getOperand(3).getReg();
346 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000347 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000348
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000349 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000350}
351
352
Brendon Cahoondf43e682015-05-08 16:16:29 +0000353/// This function can analyze one/two way branching only and should (mostly) be
354/// called by target independent side.
355/// First entry is always the opcode of the branching instruction, except when
356/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
357/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
358/// e.g. Jump_c p will have
359/// Cond[0] = Jump_c
360/// Cond[1] = p
361/// HW-loop ENDLOOP:
362/// Cond[0] = ENDLOOP
363/// Cond[1] = MBB
364/// New value jump:
365/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
366/// Cond[1] = R
367/// Cond[2] = Imm
Brendon Cahoondf43e682015-05-08 16:16:29 +0000368///
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000369bool HexagonInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000370 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000371 MachineBasicBlock *&FBB,
372 SmallVectorImpl<MachineOperand> &Cond,
373 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000374 TBB = nullptr;
375 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000376 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000377
378 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000379 MachineBasicBlock::instr_iterator I = MBB.instr_end();
380 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000381 return false;
382
383 // A basic block may looks like this:
384 //
385 // [ insn
386 // EH_LABEL
387 // insn
388 // insn
389 // insn
390 // EH_LABEL
391 // insn ]
392 //
393 // It has two succs but does not have a terminator
394 // Don't know how to handle it.
395 do {
396 --I;
397 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000398 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000399 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000400 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000401
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000402 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000403 --I;
404
405 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000406 if (I == MBB.instr_begin())
407 return false;
408 --I;
409 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000410
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000411 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
412 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000413 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000414 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000415 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
416 DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
417 I->eraseFromParent();
418 I = MBB.instr_end();
419 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000420 return false;
421 --I;
422 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000423 if (!isUnpredicatedTerminator(*I))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000424 return false;
425
426 // Get the last instruction in the block.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000427 MachineInstr *LastInst = &*I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000428 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000429 // Find one more terminator if present.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000430 for (;;) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000431 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000432 if (!SecondLastInst)
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000433 SecondLastInst = &*I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000434 else
435 // This is a third branch.
436 return true;
437 }
438 if (I == MBB.instr_begin())
439 break;
440 --I;
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000441 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000442
443 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000444 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
445 // If the branch target is not a basic block, it could be a tail call.
446 // (It is, if the target is a function.)
447 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
448 return true;
449 if (SecLastOpcode == Hexagon::J2_jump &&
450 !SecondLastInst->getOperand(0).isMBB())
451 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000452
453 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000454 bool LastOpcodeHasNVJump = isNewValueJump(*LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000455
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000456 if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
457 return true;
458
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000459 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000460 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000461 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000462 TBB = LastInst->getOperand(0).getMBB();
463 return false;
464 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000465 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000466 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000467 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000468 Cond.push_back(LastInst->getOperand(0));
469 return false;
470 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000471 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000472 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000473 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000474 Cond.push_back(LastInst->getOperand(0));
475 return false;
476 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000477 // Only supporting rr/ri versions of new-value jumps.
478 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
479 TBB = LastInst->getOperand(2).getMBB();
480 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
481 Cond.push_back(LastInst->getOperand(0));
482 Cond.push_back(LastInst->getOperand(1));
483 return false;
484 }
485 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
486 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000487 // Otherwise, don't know what this is.
488 return true;
489 }
490
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000491 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000492 bool SecLastOpcodeHasNVJump = isNewValueJump(*SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000493 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000494 if (!SecondLastInst->getOperand(1).isMBB())
495 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000496 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000497 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000498 Cond.push_back(SecondLastInst->getOperand(0));
499 FBB = LastInst->getOperand(0).getMBB();
500 return false;
501 }
502
Brendon Cahoondf43e682015-05-08 16:16:29 +0000503 // Only supporting rr/ri versions of new-value jumps.
504 if (SecLastOpcodeHasNVJump &&
505 (SecondLastInst->getNumExplicitOperands() == 3) &&
506 (LastOpcode == Hexagon::J2_jump)) {
507 TBB = SecondLastInst->getOperand(2).getMBB();
508 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
509 Cond.push_back(SecondLastInst->getOperand(0));
510 Cond.push_back(SecondLastInst->getOperand(1));
511 FBB = LastInst->getOperand(0).getMBB();
512 return false;
513 }
514
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000515 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
516 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000517 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000518 TBB = SecondLastInst->getOperand(0).getMBB();
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000519 I = LastInst->getIterator();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000520 if (AllowModify)
521 I->eraseFromParent();
522 return false;
523 }
524
Brendon Cahoondf43e682015-05-08 16:16:29 +0000525 // If the block ends with an ENDLOOP, and J2_jump, handle it.
526 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000527 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000528 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000529 Cond.push_back(SecondLastInst->getOperand(0));
530 FBB = LastInst->getOperand(0).getMBB();
531 return false;
532 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000533 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
534 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000535 // Otherwise, can't handle this.
536 return true;
537}
538
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000539
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000540unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000541 DEBUG(dbgs() << "\nRemoving branches out of BB#" << MBB.getNumber());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000542 MachineBasicBlock::iterator I = MBB.end();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000543 unsigned Count = 0;
544 while (I != MBB.begin()) {
545 --I;
546 if (I->isDebugValue())
547 continue;
548 // Only removing branches from end of MBB.
549 if (!I->isBranch())
550 return Count;
551 if (Count && (I->getOpcode() == Hexagon::J2_jump))
552 llvm_unreachable("Malformed basic block: unconditional branch not last");
553 MBB.erase(&MBB.back());
554 I = MBB.end();
555 ++Count;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000556 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000557 return Count;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000558}
559
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000560unsigned HexagonInstrInfo::InsertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000561 MachineBasicBlock *TBB,
562 MachineBasicBlock *FBB,
563 ArrayRef<MachineOperand> Cond,
564 const DebugLoc &DL) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000565 unsigned BOpc = Hexagon::J2_jump;
566 unsigned BccOpc = Hexagon::J2_jumpt;
567 assert(validateBranchCond(Cond) && "Invalid branching condition");
568 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
569
570 // Check if ReverseBranchCondition has asked to reverse this branch
571 // If we want to reverse the branch an odd number of times, we want
572 // J2_jumpf.
573 if (!Cond.empty() && Cond[0].isImm())
574 BccOpc = Cond[0].getImm();
575
576 if (!FBB) {
577 if (Cond.empty()) {
578 // Due to a bug in TailMerging/CFG Optimization, we need to add a
579 // special case handling of a predicated jump followed by an
580 // unconditional jump. If not, Tail Merging and CFG Optimization go
581 // into an infinite loop.
582 MachineBasicBlock *NewTBB, *NewFBB;
583 SmallVector<MachineOperand, 4> Cond;
Duncan P. N. Exon Smith25b132e2016-07-08 18:26:20 +0000584 auto Term = MBB.getFirstTerminator();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000585 if (Term != MBB.end() && isPredicated(*Term) &&
Duncan P. N. Exon Smithe04fe1a2016-08-17 00:34:00 +0000586 !analyzeBranch(MBB, NewTBB, NewFBB, Cond, false) &&
587 MachineFunction::iterator(NewTBB) == ++MBB.getIterator()) {
588 ReverseBranchCondition(Cond);
589 RemoveBranch(MBB);
590 return InsertBranch(MBB, TBB, nullptr, Cond, DL);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000591 }
592 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
593 } else if (isEndLoopN(Cond[0].getImm())) {
594 int EndLoopOp = Cond[0].getImm();
595 assert(Cond[1].isMBB());
596 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
597 // Check for it, and change the BB target if needed.
598 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
599 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
600 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
601 Loop->getOperand(0).setMBB(TBB);
602 // Add the ENDLOOP after the finding the LOOP0.
603 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
604 } else if (isNewValueJump(Cond[0].getImm())) {
605 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
606 // New value jump
607 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
608 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
609 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
610 DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
611 if (Cond[2].isReg()) {
612 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
613 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
614 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
615 } else if(Cond[2].isImm()) {
616 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
617 addImm(Cond[2].getImm()).addMBB(TBB);
618 } else
619 llvm_unreachable("Invalid condition for branching");
620 } else {
621 assert((Cond.size() == 2) && "Malformed cond vector");
622 const MachineOperand &RO = Cond[1];
623 unsigned Flags = getUndefRegState(RO.isUndef());
624 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
625 }
626 return 1;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000627 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000628 assert((!Cond.empty()) &&
629 "Cond. cannot be empty when multiple branchings are required");
630 assert((!isNewValueJump(Cond[0].getImm())) &&
631 "NV-jump cannot be inserted with another branch");
632 // Special case for hardware loops. The condition is a basic block.
633 if (isEndLoopN(Cond[0].getImm())) {
634 int EndLoopOp = Cond[0].getImm();
635 assert(Cond[1].isMBB());
636 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
637 // Check for it, and change the BB target if needed.
638 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
639 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
640 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
641 Loop->getOperand(0).setMBB(TBB);
642 // Add the ENDLOOP after the finding the LOOP0.
643 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
644 } else {
645 const MachineOperand &RO = Cond[1];
646 unsigned Flags = getUndefRegState(RO.isUndef());
647 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000648 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000649 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000650
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000651 return 2;
652}
653
Brendon Cahoon254f8892016-07-29 16:44:44 +0000654/// Analyze the loop code to find the loop induction variable and compare used
655/// to compute the number of iterations. Currently, we analyze loop that are
656/// controlled using hardware loops. In this case, the induction variable
657/// instruction is null. For all other cases, this function returns true, which
658/// means we're unable to analyze it.
659bool HexagonInstrInfo::analyzeLoop(MachineLoop &L,
660 MachineInstr *&IndVarInst,
661 MachineInstr *&CmpInst) const {
662
663 MachineBasicBlock *LoopEnd = L.getBottomBlock();
664 MachineBasicBlock::iterator I = LoopEnd->getFirstTerminator();
665 // We really "analyze" only hardware loops right now.
666 if (I != LoopEnd->end() && isEndLoopN(I->getOpcode())) {
667 IndVarInst = nullptr;
668 CmpInst = &*I;
669 return false;
670 }
671 return true;
672}
673
674/// Generate code to reduce the loop iteration by one and check if the loop is
675/// finished. Return the value/register of the new loop count. this function
676/// assumes the nth iteration is peeled first.
677unsigned HexagonInstrInfo::reduceLoopCount(MachineBasicBlock &MBB,
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000678 MachineInstr *IndVar, MachineInstr &Cmp,
Brendon Cahoon254f8892016-07-29 16:44:44 +0000679 SmallVectorImpl<MachineOperand> &Cond,
680 SmallVectorImpl<MachineInstr *> &PrevInsts,
681 unsigned Iter, unsigned MaxIter) const {
682 // We expect a hardware loop currently. This means that IndVar is set
683 // to null, and the compare is the ENDLOOP instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000684 assert((!IndVar) && isEndLoopN(Cmp.getOpcode())
Brendon Cahoon254f8892016-07-29 16:44:44 +0000685 && "Expecting a hardware loop");
686 MachineFunction *MF = MBB.getParent();
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000687 DebugLoc DL = Cmp.getDebugLoc();
Brendon Cahoon254f8892016-07-29 16:44:44 +0000688 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000689 MachineInstr *Loop = findLoopInstr(&MBB, Cmp.getOpcode(), VisitedBBs);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000690 if (!Loop)
691 return 0;
692 // If the loop trip count is a compile-time value, then just change the
693 // value.
694 if (Loop->getOpcode() == Hexagon::J2_loop0i ||
695 Loop->getOpcode() == Hexagon::J2_loop1i) {
696 int64_t Offset = Loop->getOperand(1).getImm();
697 if (Offset <= 1)
698 Loop->eraseFromParent();
699 else
700 Loop->getOperand(1).setImm(Offset - 1);
701 return Offset - 1;
702 }
703 // The loop trip count is a run-time value. We generate code to subtract
704 // one from the trip count, and update the loop instruction.
705 assert(Loop->getOpcode() == Hexagon::J2_loop0r && "Unexpected instruction");
706 unsigned LoopCount = Loop->getOperand(1).getReg();
707 // Check if we're done with the loop.
708 unsigned LoopEnd = createVR(MF, MVT::i1);
709 MachineInstr *NewCmp = BuildMI(&MBB, DL, get(Hexagon::C2_cmpgtui), LoopEnd).
710 addReg(LoopCount).addImm(1);
711 unsigned NewLoopCount = createVR(MF, MVT::i32);
712 MachineInstr *NewAdd = BuildMI(&MBB, DL, get(Hexagon::A2_addi), NewLoopCount).
713 addReg(LoopCount).addImm(-1);
714 // Update the previously generated instructions with the new loop counter.
715 for (SmallVectorImpl<MachineInstr *>::iterator I = PrevInsts.begin(),
716 E = PrevInsts.end(); I != E; ++I)
717 (*I)->substituteRegister(LoopCount, NewLoopCount, 0, getRegisterInfo());
718 PrevInsts.clear();
719 PrevInsts.push_back(NewCmp);
720 PrevInsts.push_back(NewAdd);
721 // Insert the new loop instruction if this is the last time the loop is
722 // decremented.
723 if (Iter == MaxIter)
724 BuildMI(&MBB, DL, get(Hexagon::J2_loop0r)).
725 addMBB(Loop->getOperand(0).getMBB()).addReg(NewLoopCount);
726 // Delete the old loop instruction.
727 if (Iter == 0)
728 Loop->eraseFromParent();
729 Cond.push_back(MachineOperand::CreateImm(Hexagon::J2_jumpf));
730 Cond.push_back(NewCmp->getOperand(0));
731 return NewLoopCount;
732}
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000733
734bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
735 unsigned NumCycles, unsigned ExtraPredCycles,
736 BranchProbability Probability) const {
737 return nonDbgBBSize(&MBB) <= 3;
738}
739
740
741bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
742 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
743 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
744 const {
745 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
746}
747
748
749bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
750 unsigned NumInstrs, BranchProbability Probability) const {
751 return NumInstrs <= 4;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000752}
753
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000754void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000755 MachineBasicBlock::iterator I,
756 const DebugLoc &DL, unsigned DestReg,
757 unsigned SrcReg, bool KillSrc) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000758 auto &HRI = getRegisterInfo();
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000759 unsigned KillFlag = getKillRegState(KillSrc);
760
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000761 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000762 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000763 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000764 return;
765 }
766 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000767 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg)
768 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000769 return;
770 }
771 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
772 // Map Pd = Ps to Pd = or(Ps, Ps).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000773 BuildMI(MBB, I, DL, get(Hexagon::C2_or), DestReg)
774 .addReg(SrcReg).addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000775 return;
776 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000777 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000778 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000779 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
780 .addReg(SrcReg, KillFlag);
781 return;
782 }
783 if (Hexagon::IntRegsRegClass.contains(DestReg) &&
784 Hexagon::CtrRegsRegClass.contains(SrcReg)) {
785 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrcrr), DestReg)
786 .addReg(SrcReg, KillFlag);
787 return;
788 }
789 if (Hexagon::ModRegsRegClass.contains(DestReg) &&
790 Hexagon::IntRegsRegClass.contains(SrcReg)) {
791 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
792 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000793 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000794 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000795 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
796 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000797 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
798 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000799 return;
800 }
801 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
802 Hexagon::PredRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000803 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg)
804 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000805 return;
806 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000807 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
808 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000809 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
810 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000811 return;
812 }
813 if (Hexagon::VectorRegsRegClass.contains(SrcReg, DestReg)) {
814 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000815 addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000816 return;
817 }
818 if (Hexagon::VecDblRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000819 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
820 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg), KillFlag)
821 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg), KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000822 return;
823 }
824 if (Hexagon::VecPredRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000825 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
826 .addReg(SrcReg)
827 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000828 return;
829 }
830 if (Hexagon::VecPredRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000831 Hexagon::VectorRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000832 llvm_unreachable("Unimplemented pred to vec");
833 return;
834 }
835 if (Hexagon::VecPredRegsRegClass.contains(DestReg) &&
836 Hexagon::VectorRegsRegClass.contains(SrcReg)) {
837 llvm_unreachable("Unimplemented vec to pred");
838 return;
839 }
840 if (Hexagon::VecPredRegs128BRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000841 unsigned DstHi = HRI.getSubReg(DestReg, Hexagon::subreg_hireg);
842 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DstHi)
843 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg), KillFlag);
844 unsigned DstLo = HRI.getSubReg(DestReg, Hexagon::subreg_loreg);
845 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DstLo)
846 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg), KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000847 return;
848 }
Sirish Pande30804c22012-02-15 18:52:27 +0000849
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000850#ifndef NDEBUG
851 // Show the invalid registers to ease debugging.
852 dbgs() << "Invalid registers for copy in BB#" << MBB.getNumber()
853 << ": " << PrintReg(DestReg, &HRI)
854 << " = " << PrintReg(SrcReg, &HRI) << '\n';
855#endif
Sirish Pande30804c22012-02-15 18:52:27 +0000856 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000857}
858
859
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000860void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
861 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
862 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000863 DebugLoc DL = MBB.findDebugLoc(I);
864 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000865 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000866 unsigned Align = MFI.getObjectAlignment(FI);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000867 unsigned KillFlag = getKillRegState(isKill);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000868
Alex Lorenze40c8a22015-08-11 23:09:45 +0000869 MachineMemOperand *MMO = MF.getMachineMemOperand(
870 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
871 MFI.getObjectSize(FI), Align);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000872
Craig Topperc7242e02012-04-20 07:30:17 +0000873 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000874 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000875 .addFrameIndex(FI).addImm(0)
876 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000877 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000878 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000879 .addFrameIndex(FI).addImm(0)
880 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000881 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000882 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000883 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000884 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000885 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
886 BuildMI(MBB, I, DL, get(Hexagon::STriw_mod))
887 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000888 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
889 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000890 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai_128B))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000891 .addFrameIndex(FI).addImm(0)
892 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
893 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000894 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000895 .addFrameIndex(FI).addImm(0)
896 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
897 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000898 unsigned Opc = Align < 128 ? Hexagon::V6_vS32Ub_ai_128B
899 : Hexagon::V6_vS32b_ai_128B;
900 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000901 .addFrameIndex(FI).addImm(0)
902 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
903 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000904 unsigned Opc = Align < 64 ? Hexagon::V6_vS32Ub_ai
905 : Hexagon::V6_vS32b_ai;
906 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000907 .addFrameIndex(FI).addImm(0)
908 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
909 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000910 unsigned Opc = Align < 64 ? Hexagon::PS_vstorerwu_ai
911 : Hexagon::PS_vstorerw_ai;
912 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000913 .addFrameIndex(FI).addImm(0)
914 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
915 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000916 unsigned Opc = Align < 128 ? Hexagon::PS_vstorerwu_ai_128B
917 : Hexagon::PS_vstorerw_ai_128B;
918 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000919 .addFrameIndex(FI).addImm(0)
920 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000921 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000922 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000923 }
924}
925
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000926void HexagonInstrInfo::loadRegFromStackSlot(
927 MachineBasicBlock &MBB, MachineBasicBlock::iterator I, unsigned DestReg,
928 int FI, const TargetRegisterClass *RC,
929 const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000930 DebugLoc DL = MBB.findDebugLoc(I);
931 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000932 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000933 unsigned Align = MFI.getObjectAlignment(FI);
934
Alex Lorenze40c8a22015-08-11 23:09:45 +0000935 MachineMemOperand *MMO = MF.getMachineMemOperand(
936 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
937 MFI.getObjectSize(FI), Align);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000938
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000939 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000940 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000941 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000942 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000943 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000944 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000945 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000946 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000947 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
948 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
949 BuildMI(MBB, I, DL, get(Hexagon::LDriw_mod), DestReg)
950 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000951 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000952 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai_128B), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000953 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
954 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000955 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000956 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
957 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000958 unsigned Opc = Align < 128 ? Hexagon::PS_vloadrwu_ai_128B
959 : Hexagon::PS_vloadrw_ai_128B;
960 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000961 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
962 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000963 unsigned Opc = Align < 128 ? Hexagon::V6_vL32Ub_ai_128B
964 : Hexagon::V6_vL32b_ai_128B;
965 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000966 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
967 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000968 unsigned Opc = Align < 64 ? Hexagon::V6_vL32Ub_ai
969 : Hexagon::V6_vL32b_ai;
970 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000971 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
972 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000973 unsigned Opc = Align < 64 ? Hexagon::PS_vloadrwu_ai
974 : Hexagon::PS_vloadrw_ai;
975 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000976 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000977 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000978 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000979 }
980}
981
982
Ron Lieberman88159e52016-09-02 22:56:24 +0000983static void getLiveRegsAt(LivePhysRegs &Regs, const MachineInstr &MI) {
984 const MachineBasicBlock &B = *MI.getParent();
985 Regs.addLiveOuts(B);
Duncan P. N. Exon Smith18720962016-09-11 18:51:28 +0000986 auto E = ++MachineBasicBlock::const_iterator(MI.getIterator()).getReverse();
Ron Lieberman88159e52016-09-02 22:56:24 +0000987 for (auto I = B.rbegin(); I != E; ++I)
988 Regs.stepBackward(*I);
989}
990
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000991/// expandPostRAPseudo - This function is called for all pseudo instructions
992/// that remain after register allocation. Many pseudo instructions are
993/// created to help register allocation. This is the place to convert them
994/// into real instructions. The target can edit MI in place, or it can insert
995/// new instructions and erase MI. The function should return true if
996/// anything was changed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000997bool HexagonInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000998 const HexagonRegisterInfo &HRI = getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000999 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1000 MachineBasicBlock &MBB = *MI.getParent();
1001 DebugLoc DL = MI.getDebugLoc();
1002 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001003 const unsigned VecOffset = 1;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001004
1005 switch (Opc) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001006 case TargetOpcode::COPY: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001007 MachineOperand &MD = MI.getOperand(0);
1008 MachineOperand &MS = MI.getOperand(1);
1009 MachineBasicBlock::iterator MBBI = MI.getIterator();
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001010 if (MD.getReg() != MS.getReg() && !MS.isUndef()) {
1011 copyPhysReg(MBB, MI, DL, MD.getReg(), MS.getReg(), MS.isKill());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001012 std::prev(MBBI)->copyImplicitOps(*MBB.getParent(), MI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001013 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001014 MBB.erase(MBBI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001015 return true;
1016 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001017 case Hexagon::PS_aligna:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001018 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI.getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001019 .addReg(HRI.getFrameRegister())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001020 .addImm(-MI.getOperand(1).getImm());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001021 MBB.erase(MI);
1022 return true;
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001023 case Hexagon::V6_vassignp_128B:
1024 case Hexagon::V6_vassignp: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001025 unsigned SrcReg = MI.getOperand(1).getReg();
1026 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001027 unsigned Kill = getKillRegState(MI.getOperand(1).isKill());
1028 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcombine), DstReg)
1029 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg), Kill)
1030 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg), Kill);
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001031 MBB.erase(MI);
1032 return true;
1033 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001034 case Hexagon::V6_lo_128B:
1035 case Hexagon::V6_lo: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001036 unsigned SrcReg = MI.getOperand(1).getReg();
1037 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001038 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001039 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001040 MBB.erase(MI);
1041 MRI.clearKillFlags(SrcSubLo);
1042 return true;
1043 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001044 case Hexagon::V6_hi_128B:
1045 case Hexagon::V6_hi: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001046 unsigned SrcReg = MI.getOperand(1).getReg();
1047 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001048 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::subreg_hireg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001049 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001050 MBB.erase(MI);
1051 MRI.clearKillFlags(SrcSubHi);
1052 return true;
1053 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001054 case Hexagon::PS_vstorerw_ai:
1055 case Hexagon::PS_vstorerwu_ai:
1056 case Hexagon::PS_vstorerw_ai_128B:
1057 case Hexagon::PS_vstorerwu_ai_128B: {
1058 bool Is128B = (Opc == Hexagon::PS_vstorerw_ai_128B ||
1059 Opc == Hexagon::PS_vstorerwu_ai_128B);
1060 bool Aligned = (Opc == Hexagon::PS_vstorerw_ai ||
1061 Opc == Hexagon::PS_vstorerw_ai_128B);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001062 unsigned SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001063 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::subreg_hireg);
1064 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::subreg_loreg);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001065 unsigned NewOpc;
1066 if (Aligned)
1067 NewOpc = Is128B ? Hexagon::V6_vS32b_ai_128B
1068 : Hexagon::V6_vS32b_ai;
1069 else
1070 NewOpc = Is128B ? Hexagon::V6_vS32Ub_ai_128B
1071 : Hexagon::V6_vS32Ub_ai;
1072
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001073 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001074 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001075 BuildMI(MBB, MI, DL, get(NewOpc))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001076 .addOperand(MI.getOperand(0))
1077 .addImm(MI.getOperand(1).getImm())
1078 .addReg(SrcSubLo)
1079 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001080 MI1New->getOperand(0).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001081 BuildMI(MBB, MI, DL, get(NewOpc))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001082 .addOperand(MI.getOperand(0))
1083 // The Vectors are indexed in multiples of vector size.
1084 .addImm(MI.getOperand(1).getImm() + Offset)
1085 .addReg(SrcSubHi)
1086 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001087 MBB.erase(MI);
1088 return true;
1089 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001090 case Hexagon::PS_vloadrw_ai:
1091 case Hexagon::PS_vloadrwu_ai:
1092 case Hexagon::PS_vloadrw_ai_128B:
1093 case Hexagon::PS_vloadrwu_ai_128B: {
1094 bool Is128B = (Opc == Hexagon::PS_vloadrw_ai_128B ||
1095 Opc == Hexagon::PS_vloadrwu_ai_128B);
1096 bool Aligned = (Opc == Hexagon::PS_vloadrw_ai ||
1097 Opc == Hexagon::PS_vloadrw_ai_128B);
1098 unsigned NewOpc;
1099 if (Aligned)
1100 NewOpc = Is128B ? Hexagon::V6_vL32b_ai_128B
1101 : Hexagon::V6_vL32b_ai;
1102 else
1103 NewOpc = Is128B ? Hexagon::V6_vL32Ub_ai_128B
1104 : Hexagon::V6_vL32Ub_ai;
1105
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001106 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001107 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
1108 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001109 BuildMI(MBB, MI, DL, get(NewOpc),
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001110 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001111 .addOperand(MI.getOperand(1))
1112 .addImm(MI.getOperand(2).getImm());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001113 MI1New->getOperand(1).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001114 BuildMI(MBB, MI, DL, get(NewOpc),
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001115 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001116 .addOperand(MI.getOperand(1))
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001117 // The Vectors are indexed in multiples of vector size.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001118 .addImm(MI.getOperand(2).getImm() + Offset)
1119 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001120 MBB.erase(MI);
1121 return true;
1122 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001123 case Hexagon::PS_true: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001124 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001125 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1126 .addReg(Reg, RegState::Undef)
1127 .addReg(Reg, RegState::Undef);
1128 MBB.erase(MI);
1129 return true;
1130 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001131 case Hexagon::PS_false: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001132 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001133 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1134 .addReg(Reg, RegState::Undef)
1135 .addReg(Reg, RegState::Undef);
1136 MBB.erase(MI);
1137 return true;
1138 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001139 case Hexagon::PS_vmulw: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001140 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001141 unsigned DstReg = MI.getOperand(0).getReg();
1142 unsigned Src1Reg = MI.getOperand(1).getReg();
1143 unsigned Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001144 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
1145 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
1146 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
1147 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001148 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
1149 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1150 .addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001151 .addReg(Src2SubHi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001152 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
1153 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1154 .addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001155 .addReg(Src2SubLo);
1156 MBB.erase(MI);
1157 MRI.clearKillFlags(Src1SubHi);
1158 MRI.clearKillFlags(Src1SubLo);
1159 MRI.clearKillFlags(Src2SubHi);
1160 MRI.clearKillFlags(Src2SubLo);
1161 return true;
1162 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001163 case Hexagon::PS_vmulw_acc: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001164 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001165 unsigned DstReg = MI.getOperand(0).getReg();
1166 unsigned Src1Reg = MI.getOperand(1).getReg();
1167 unsigned Src2Reg = MI.getOperand(2).getReg();
1168 unsigned Src3Reg = MI.getOperand(3).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001169 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
1170 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
1171 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
1172 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
1173 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::subreg_hireg);
1174 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001175 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
1176 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1177 .addReg(Src1SubHi)
1178 .addReg(Src2SubHi)
1179 .addReg(Src3SubHi);
1180 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
1181 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1182 .addReg(Src1SubLo)
1183 .addReg(Src2SubLo)
1184 .addReg(Src3SubLo);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001185 MBB.erase(MI);
1186 MRI.clearKillFlags(Src1SubHi);
1187 MRI.clearKillFlags(Src1SubLo);
1188 MRI.clearKillFlags(Src2SubHi);
1189 MRI.clearKillFlags(Src2SubLo);
1190 MRI.clearKillFlags(Src3SubHi);
1191 MRI.clearKillFlags(Src3SubLo);
1192 return true;
1193 }
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +00001194 case Hexagon::Insert4: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001195 unsigned DstReg = MI.getOperand(0).getReg();
1196 unsigned Src1Reg = MI.getOperand(1).getReg();
1197 unsigned Src2Reg = MI.getOperand(2).getReg();
1198 unsigned Src3Reg = MI.getOperand(3).getReg();
1199 unsigned Src4Reg = MI.getOperand(4).getReg();
1200 unsigned Src1RegIsKill = getKillRegState(MI.getOperand(1).isKill());
1201 unsigned Src2RegIsKill = getKillRegState(MI.getOperand(2).isKill());
1202 unsigned Src3RegIsKill = getKillRegState(MI.getOperand(3).isKill());
1203 unsigned Src4RegIsKill = getKillRegState(MI.getOperand(4).isKill());
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +00001204 unsigned DstSubHi = HRI.getSubReg(DstReg, Hexagon::subreg_hireg);
1205 unsigned DstSubLo = HRI.getSubReg(DstReg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001206 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1207 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1208 .addReg(DstSubLo)
1209 .addReg(Src1Reg, Src1RegIsKill)
1210 .addImm(16)
1211 .addImm(0);
1212 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1213 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1214 .addReg(DstSubLo)
1215 .addReg(Src2Reg, Src2RegIsKill)
1216 .addImm(16)
1217 .addImm(16);
1218 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1219 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1220 .addReg(DstSubHi)
1221 .addReg(Src3Reg, Src3RegIsKill)
1222 .addImm(16)
1223 .addImm(0);
1224 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1225 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1226 .addReg(DstSubHi)
1227 .addReg(Src4Reg, Src4RegIsKill)
1228 .addImm(16)
1229 .addImm(16);
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +00001230 MBB.erase(MI);
1231 MRI.clearKillFlags(DstReg);
1232 MRI.clearKillFlags(DstSubHi);
1233 MRI.clearKillFlags(DstSubLo);
1234 return true;
1235 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001236 case Hexagon::PS_pselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001237 const MachineOperand &Op0 = MI.getOperand(0);
1238 const MachineOperand &Op1 = MI.getOperand(1);
1239 const MachineOperand &Op2 = MI.getOperand(2);
1240 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001241 unsigned Rd = Op0.getReg();
1242 unsigned Pu = Op1.getReg();
1243 unsigned Rs = Op2.getReg();
1244 unsigned Rt = Op3.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001245 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001246 unsigned K1 = getKillRegState(Op1.isKill());
1247 unsigned K2 = getKillRegState(Op2.isKill());
1248 unsigned K3 = getKillRegState(Op3.isKill());
1249 if (Rd != Rs)
1250 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1251 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1252 .addReg(Rs, K2);
1253 if (Rd != Rt)
1254 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1255 .addReg(Pu, K1)
1256 .addReg(Rt, K3);
1257 MBB.erase(MI);
1258 return true;
1259 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001260 case Hexagon::PS_vselect:
1261 case Hexagon::PS_vselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001262 const MachineOperand &Op0 = MI.getOperand(0);
1263 const MachineOperand &Op1 = MI.getOperand(1);
1264 const MachineOperand &Op2 = MI.getOperand(2);
1265 const MachineOperand &Op3 = MI.getOperand(3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001266 LivePhysRegs LiveAtMI(&HRI);
1267 getLiveRegsAt(LiveAtMI, MI);
1268 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
1269 if (Op0.getReg() != Op2.getReg()) {
1270 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
1271 .addOperand(Op0)
1272 .addOperand(Op1)
1273 .addOperand(Op2);
1274 if (IsDestLive)
1275 T.addReg(Op0.getReg(), RegState::Implicit);
1276 IsDestLive = true;
1277 }
1278 if (Op0.getReg() != Op3.getReg()) {
1279 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
1280 .addOperand(Op0)
1281 .addOperand(Op1)
1282 .addOperand(Op3);
1283 if (IsDestLive)
1284 T.addReg(Op0.getReg(), RegState::Implicit);
1285 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001286 MBB.erase(MI);
1287 return true;
1288 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001289 case Hexagon::PS_wselect:
1290 case Hexagon::PS_wselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001291 MachineOperand &Op0 = MI.getOperand(0);
1292 MachineOperand &Op1 = MI.getOperand(1);
1293 MachineOperand &Op2 = MI.getOperand(2);
1294 MachineOperand &Op3 = MI.getOperand(3);
Ron Lieberman88159e52016-09-02 22:56:24 +00001295 LivePhysRegs LiveAtMI(&HRI);
1296 getLiveRegsAt(LiveAtMI, MI);
1297 bool IsDestLive = !LiveAtMI.available(MRI, Op0.getReg());
1298
1299 if (Op0.getReg() != Op2.getReg()) {
1300 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::subreg_loreg);
1301 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::subreg_hireg);
1302 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
1303 .addOperand(Op0)
1304 .addOperand(Op1)
1305 .addReg(SrcHi)
1306 .addReg(SrcLo);
1307 if (IsDestLive)
1308 T.addReg(Op0.getReg(), RegState::Implicit);
1309 IsDestLive = true;
1310 }
1311 if (Op0.getReg() != Op3.getReg()) {
1312 unsigned SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::subreg_loreg);
1313 unsigned SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::subreg_hireg);
1314 auto T = BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
1315 .addOperand(Op0)
1316 .addOperand(Op1)
1317 .addReg(SrcHi)
1318 .addReg(SrcLo);
1319 if (IsDestLive)
1320 T.addReg(Op0.getReg(), RegState::Implicit);
1321 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001322 MBB.erase(MI);
1323 return true;
1324 }
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001325 case Hexagon::PS_tailcall_i:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001326 MI.setDesc(get(Hexagon::J2_jump));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001327 return true;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001328 case Hexagon::PS_tailcall_r:
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001329 case Hexagon::PS_jmpret:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001330 MI.setDesc(get(Hexagon::J2_jumpr));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001331 return true;
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001332 case Hexagon::PS_jmprett:
1333 MI.setDesc(get(Hexagon::J2_jumprt));
1334 return true;
1335 case Hexagon::PS_jmpretf:
1336 MI.setDesc(get(Hexagon::J2_jumprf));
1337 return true;
1338 case Hexagon::PS_jmprettnewpt:
1339 MI.setDesc(get(Hexagon::J2_jumprtnewpt));
1340 return true;
1341 case Hexagon::PS_jmpretfnewpt:
1342 MI.setDesc(get(Hexagon::J2_jumprfnewpt));
1343 return true;
1344 case Hexagon::PS_jmprettnew:
1345 MI.setDesc(get(Hexagon::J2_jumprtnew));
1346 return true;
1347 case Hexagon::PS_jmpretfnew:
1348 MI.setDesc(get(Hexagon::J2_jumprfnew));
1349 return true;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001350 }
1351
1352 return false;
1353}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001354
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001355
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001356// We indicate that we want to reverse the branch by
1357// inserting the reversed branching opcode.
1358bool HexagonInstrInfo::ReverseBranchCondition(
1359 SmallVectorImpl<MachineOperand> &Cond) const {
1360 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001361 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001362 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1363 unsigned opcode = Cond[0].getImm();
1364 //unsigned temp;
1365 assert(get(opcode).isBranch() && "Should be a branching condition.");
1366 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001367 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001368 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1369 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001370 return false;
1371}
1372
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001373
1374void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1375 MachineBasicBlock::iterator MI) const {
1376 DebugLoc DL;
1377 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1378}
1379
1380
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001381bool HexagonInstrInfo::isPostIncrement(const MachineInstr &MI) const {
1382 return getAddrMode(MI) == HexagonII::PostInc;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001383}
1384
1385
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001386// Returns true if an instruction is predicated irrespective of the predicate
1387// sense. For example, all of the following will return true.
1388// if (p0) R1 = add(R2, R3)
1389// if (!p0) R1 = add(R2, R3)
1390// if (p0.new) R1 = add(R2, R3)
1391// if (!p0.new) R1 = add(R2, R3)
1392// Note: New-value stores are not included here as in the current
1393// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001394bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1395 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001396 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001397}
1398
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001399
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001400bool HexagonInstrInfo::PredicateInstruction(
1401 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001402 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1403 isEndLoopN(Cond[0].getImm())) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001404 DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001405 return false;
1406 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001407 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001408 assert (isPredicable(MI) && "Expected predicable instruction");
1409 bool invertJump = predOpcodeHasNot(Cond);
1410
1411 // We have to predicate MI "in place", i.e. after this function returns,
1412 // MI will need to be transformed into a predicated form. To avoid com-
1413 // plicated manipulations with the operands (handling tied operands,
1414 // etc.), build a new temporary instruction, then overwrite MI with it.
1415
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001416 MachineBasicBlock &B = *MI.getParent();
1417 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001418 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1419 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001420 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001421 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001422 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001423 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1424 break;
1425 T.addOperand(Op);
1426 NOp++;
1427 }
1428
1429 unsigned PredReg, PredRegPos, PredRegFlags;
1430 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1431 (void)GotPredReg;
1432 assert(GotPredReg);
1433 T.addReg(PredReg, PredRegFlags);
1434 while (NOp < NumOps)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001435 T.addOperand(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001436
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001437 MI.setDesc(get(PredOpc));
1438 while (unsigned n = MI.getNumOperands())
1439 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001440 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001441 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001442
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001443 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001444 B.erase(TI);
1445
1446 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1447 MRI.clearKillFlags(PredReg);
1448 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001449}
1450
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001451
1452bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1453 ArrayRef<MachineOperand> Pred2) const {
1454 // TODO: Fix this
1455 return false;
1456}
1457
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001458
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001459bool HexagonInstrInfo::DefinesPredicate(
1460 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001461 auto &HRI = getRegisterInfo();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001462 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1463 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001464 if (MO.isReg() && MO.isDef()) {
1465 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1466 if (RC == &Hexagon::PredRegsRegClass) {
1467 Pred.push_back(MO);
1468 return true;
1469 }
1470 }
1471 }
1472 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001473}
Andrew Trickd06df962012-02-01 22:13:57 +00001474
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001475
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001476bool HexagonInstrInfo::isPredicable(MachineInstr &MI) const {
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001477 return MI.getDesc().isPredicable();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001478}
1479
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001480bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1481 const MachineBasicBlock *MBB,
1482 const MachineFunction &MF) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001483 // Debug info is never a scheduling boundary. It's necessary to be explicit
1484 // due to the special treatment of IT instructions below, otherwise a
1485 // dbg_value followed by an IT will result in the IT instruction being
1486 // considered a scheduling hazard, which is wrong. It should be the actual
1487 // instruction preceding the dbg_value instruction(s), just like it is
1488 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001489 if (MI.isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001490 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001491
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001492 // Throwing call is a boundary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001493 if (MI.isCall()) {
Krzysztof Parzyszekab9127c2016-08-12 11:01:10 +00001494 // Don't mess around with no return calls.
1495 if (doesNotReturn(MI))
1496 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001497 // If any of the block's successors is a landing pad, this could be a
1498 // throwing call.
1499 for (auto I : MBB->successors())
1500 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001501 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001502 }
1503
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001504 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001505 if (MI.getDesc().isTerminator() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001506 return true;
1507
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001508 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1509 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001510
1511 return false;
1512}
1513
1514
1515/// Measure the specified inline asm to determine an approximation of its
1516/// length.
1517/// Comments (which run till the next SeparatorString or newline) do not
1518/// count as an instruction.
1519/// Any other non-whitespace text is considered an instruction, with
1520/// multiple instructions separated by SeparatorString or newlines.
1521/// Variable-length instructions are not handled here; this function
1522/// may be overloaded in the target code to do that.
1523/// Hexagon counts the number of ##'s and adjust for that many
1524/// constant exenders.
1525unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1526 const MCAsmInfo &MAI) const {
1527 StringRef AStr(Str);
1528 // Count the number of instructions in the asm.
1529 bool atInsnStart = true;
1530 unsigned Length = 0;
1531 for (; *Str; ++Str) {
1532 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1533 strlen(MAI.getSeparatorString())) == 0)
1534 atInsnStart = true;
1535 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1536 Length += MAI.getMaxInstLength();
1537 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001538 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001539 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
1540 strlen(MAI.getCommentString())) == 0)
1541 atInsnStart = false;
1542 }
1543
1544 // Add to size number of constant extenders seen * 4.
1545 StringRef Occ("##");
1546 Length += AStr.count(Occ)*4;
1547 return Length;
1548}
1549
1550
1551ScheduleHazardRecognizer*
1552HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1553 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +00001554 if (UseDFAHazardRec) {
1555 auto &HST = DAG->MF.getSubtarget<HexagonSubtarget>();
1556 return new HexagonHazardRecognizer(II, this, HST);
1557 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001558 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1559}
1560
1561
1562/// \brief For a comparison instruction, return the source registers in
1563/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1564/// compares against in CmpValue. Return true if the comparison instruction
1565/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001566bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1567 unsigned &SrcReg2, int &Mask,
1568 int &Value) const {
1569 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001570
1571 // Set mask and the first source register.
1572 switch (Opc) {
1573 case Hexagon::C2_cmpeq:
1574 case Hexagon::C2_cmpeqp:
1575 case Hexagon::C2_cmpgt:
1576 case Hexagon::C2_cmpgtp:
1577 case Hexagon::C2_cmpgtu:
1578 case Hexagon::C2_cmpgtup:
1579 case Hexagon::C4_cmpneq:
1580 case Hexagon::C4_cmplte:
1581 case Hexagon::C4_cmplteu:
1582 case Hexagon::C2_cmpeqi:
1583 case Hexagon::C2_cmpgti:
1584 case Hexagon::C2_cmpgtui:
1585 case Hexagon::C4_cmpneqi:
1586 case Hexagon::C4_cmplteui:
1587 case Hexagon::C4_cmpltei:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001588 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001589 Mask = ~0;
1590 break;
1591 case Hexagon::A4_cmpbeq:
1592 case Hexagon::A4_cmpbgt:
1593 case Hexagon::A4_cmpbgtu:
1594 case Hexagon::A4_cmpbeqi:
1595 case Hexagon::A4_cmpbgti:
1596 case Hexagon::A4_cmpbgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001597 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001598 Mask = 0xFF;
1599 break;
1600 case Hexagon::A4_cmpheq:
1601 case Hexagon::A4_cmphgt:
1602 case Hexagon::A4_cmphgtu:
1603 case Hexagon::A4_cmpheqi:
1604 case Hexagon::A4_cmphgti:
1605 case Hexagon::A4_cmphgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001606 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001607 Mask = 0xFFFF;
1608 break;
1609 }
1610
1611 // Set the value/second source register.
1612 switch (Opc) {
1613 case Hexagon::C2_cmpeq:
1614 case Hexagon::C2_cmpeqp:
1615 case Hexagon::C2_cmpgt:
1616 case Hexagon::C2_cmpgtp:
1617 case Hexagon::C2_cmpgtu:
1618 case Hexagon::C2_cmpgtup:
1619 case Hexagon::A4_cmpbeq:
1620 case Hexagon::A4_cmpbgt:
1621 case Hexagon::A4_cmpbgtu:
1622 case Hexagon::A4_cmpheq:
1623 case Hexagon::A4_cmphgt:
1624 case Hexagon::A4_cmphgtu:
1625 case Hexagon::C4_cmpneq:
1626 case Hexagon::C4_cmplte:
1627 case Hexagon::C4_cmplteu:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001628 SrcReg2 = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001629 return true;
1630
1631 case Hexagon::C2_cmpeqi:
1632 case Hexagon::C2_cmpgtui:
1633 case Hexagon::C2_cmpgti:
1634 case Hexagon::C4_cmpneqi:
1635 case Hexagon::C4_cmplteui:
1636 case Hexagon::C4_cmpltei:
1637 case Hexagon::A4_cmpbeqi:
1638 case Hexagon::A4_cmpbgti:
1639 case Hexagon::A4_cmpbgtui:
1640 case Hexagon::A4_cmpheqi:
1641 case Hexagon::A4_cmphgti:
1642 case Hexagon::A4_cmphgtui:
1643 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001644 Value = MI.getOperand(2).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001645 return true;
1646 }
1647
1648 return false;
1649}
1650
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001651unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001652 const MachineInstr &MI,
1653 unsigned *PredCost) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001654 return getInstrTimingClassLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001655}
1656
1657
1658DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1659 const TargetSubtargetInfo &STI) const {
1660 const InstrItineraryData *II = STI.getInstrItineraryData();
1661 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1662}
1663
1664
1665// Inspired by this pair:
1666// %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1667// S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1668// Currently AA considers the addresses in these instructions to be aliasing.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001669bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1670 MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001671 int OffsetA = 0, OffsetB = 0;
1672 unsigned SizeA = 0, SizeB = 0;
1673
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001674 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1675 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001676 return false;
1677
1678 // Instructions that are pure loads, not loads and stores like memops are not
1679 // dependent.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001680 if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001681 return true;
1682
1683 // Get base, offset, and access size in MIa.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001684 unsigned BaseRegA = getBaseAndOffset(MIa, OffsetA, SizeA);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001685 if (!BaseRegA || !SizeA)
1686 return false;
1687
1688 // Get base, offset, and access size in MIb.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001689 unsigned BaseRegB = getBaseAndOffset(MIb, OffsetB, SizeB);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001690 if (!BaseRegB || !SizeB)
1691 return false;
1692
1693 if (BaseRegA != BaseRegB)
1694 return false;
1695
1696 // This is a mem access with the same base register and known offsets from it.
1697 // Reason about it.
1698 if (OffsetA > OffsetB) {
1699 uint64_t offDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1700 return (SizeB <= offDiff);
1701 } else if (OffsetA < OffsetB) {
1702 uint64_t offDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1703 return (SizeA <= offDiff);
1704 }
1705
1706 return false;
1707}
1708
1709
Brendon Cahoon254f8892016-07-29 16:44:44 +00001710/// If the instruction is an increment of a constant value, return the amount.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001711bool HexagonInstrInfo::getIncrementValue(const MachineInstr &MI,
Brendon Cahoon254f8892016-07-29 16:44:44 +00001712 int &Value) const {
1713 if (isPostIncrement(MI)) {
1714 unsigned AccessSize;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001715 return getBaseAndOffset(MI, Value, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00001716 }
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001717 if (MI.getOpcode() == Hexagon::A2_addi) {
1718 Value = MI.getOperand(2).getImm();
Brendon Cahoon254f8892016-07-29 16:44:44 +00001719 return true;
1720 }
1721
1722 return false;
1723}
1724
1725
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001726unsigned HexagonInstrInfo::createVR(MachineFunction *MF, MVT VT) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001727 MachineRegisterInfo &MRI = MF->getRegInfo();
1728 const TargetRegisterClass *TRC;
1729 if (VT == MVT::i1) {
1730 TRC = &Hexagon::PredRegsRegClass;
1731 } else if (VT == MVT::i32 || VT == MVT::f32) {
1732 TRC = &Hexagon::IntRegsRegClass;
1733 } else if (VT == MVT::i64 || VT == MVT::f64) {
1734 TRC = &Hexagon::DoubleRegsRegClass;
1735 } else {
1736 llvm_unreachable("Cannot handle this register class");
1737 }
1738
1739 unsigned NewReg = MRI.createVirtualRegister(TRC);
1740 return NewReg;
1741}
1742
1743
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001744bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001745 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1746}
1747
1748
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001749bool HexagonInstrInfo::isAccumulator(const MachineInstr &MI) const {
1750 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001751 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1752}
1753
1754
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001755bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
1756 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001757 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1758 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1759
1760 if (!(isTC1(MI))
1761 && !(QII->isTC2Early(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001762 && !(MI.getDesc().mayLoad())
1763 && !(MI.getDesc().mayStore())
1764 && (MI.getDesc().getOpcode() != Hexagon::S2_allocframe)
1765 && (MI.getDesc().getOpcode() != Hexagon::L2_deallocframe)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001766 && !(QII->isMemOp(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001767 && !(MI.isBranch())
1768 && !(MI.isReturn())
1769 && !MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001770 return true;
1771
1772 return false;
1773}
1774
1775
Sanjay Patele4b9f502015-12-07 19:21:39 +00001776// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001777bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
1778 return (getType(MI) == HexagonII::TypeCOMPOUND && MI.isBranch());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001779}
1780
1781
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001782bool HexagonInstrInfo::isCondInst(const MachineInstr &MI) const {
1783 return (MI.isBranch() && isPredicated(MI)) ||
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001784 isConditionalTransfer(MI) ||
1785 isConditionalALU32(MI) ||
1786 isConditionalLoad(MI) ||
1787 // Predicated stores which don't have a .new on any operands.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001788 (MI.mayStore() && isPredicated(MI) && !isNewValueStore(MI) &&
1789 !isPredicatedNew(MI));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001790}
1791
1792
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001793bool HexagonInstrInfo::isConditionalALU32(const MachineInstr &MI) const {
1794 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001795 case Hexagon::A2_paddf:
1796 case Hexagon::A2_paddfnew:
1797 case Hexagon::A2_paddif:
1798 case Hexagon::A2_paddifnew:
1799 case Hexagon::A2_paddit:
1800 case Hexagon::A2_padditnew:
1801 case Hexagon::A2_paddt:
1802 case Hexagon::A2_paddtnew:
1803 case Hexagon::A2_pandf:
1804 case Hexagon::A2_pandfnew:
1805 case Hexagon::A2_pandt:
1806 case Hexagon::A2_pandtnew:
1807 case Hexagon::A2_porf:
1808 case Hexagon::A2_porfnew:
1809 case Hexagon::A2_port:
1810 case Hexagon::A2_portnew:
1811 case Hexagon::A2_psubf:
1812 case Hexagon::A2_psubfnew:
1813 case Hexagon::A2_psubt:
1814 case Hexagon::A2_psubtnew:
1815 case Hexagon::A2_pxorf:
1816 case Hexagon::A2_pxorfnew:
1817 case Hexagon::A2_pxort:
1818 case Hexagon::A2_pxortnew:
1819 case Hexagon::A4_paslhf:
1820 case Hexagon::A4_paslhfnew:
1821 case Hexagon::A4_paslht:
1822 case Hexagon::A4_paslhtnew:
1823 case Hexagon::A4_pasrhf:
1824 case Hexagon::A4_pasrhfnew:
1825 case Hexagon::A4_pasrht:
1826 case Hexagon::A4_pasrhtnew:
1827 case Hexagon::A4_psxtbf:
1828 case Hexagon::A4_psxtbfnew:
1829 case Hexagon::A4_psxtbt:
1830 case Hexagon::A4_psxtbtnew:
1831 case Hexagon::A4_psxthf:
1832 case Hexagon::A4_psxthfnew:
1833 case Hexagon::A4_psxtht:
1834 case Hexagon::A4_psxthtnew:
1835 case Hexagon::A4_pzxtbf:
1836 case Hexagon::A4_pzxtbfnew:
1837 case Hexagon::A4_pzxtbt:
1838 case Hexagon::A4_pzxtbtnew:
1839 case Hexagon::A4_pzxthf:
1840 case Hexagon::A4_pzxthfnew:
1841 case Hexagon::A4_pzxtht:
1842 case Hexagon::A4_pzxthtnew:
1843 case Hexagon::C2_ccombinewf:
1844 case Hexagon::C2_ccombinewt:
1845 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001846 }
1847 return false;
1848}
1849
1850
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001851// FIXME - Function name and it's functionality don't match.
1852// It should be renamed to hasPredNewOpcode()
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001853bool HexagonInstrInfo::isConditionalLoad(const MachineInstr &MI) const {
1854 if (!MI.getDesc().mayLoad() || !isPredicated(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001855 return false;
1856
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001857 int PNewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001858 // Instruction with valid predicated-new opcode can be promoted to .new.
1859 return PNewOpcode >= 0;
1860}
1861
1862
1863// Returns true if an instruction is a conditional store.
1864//
1865// Note: It doesn't include conditional new-value stores as they can't be
1866// converted to .new predicate.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001867bool HexagonInstrInfo::isConditionalStore(const MachineInstr &MI) const {
1868 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001869 default: return false;
1870 case Hexagon::S4_storeirbt_io:
1871 case Hexagon::S4_storeirbf_io:
1872 case Hexagon::S4_pstorerbt_rr:
1873 case Hexagon::S4_pstorerbf_rr:
1874 case Hexagon::S2_pstorerbt_io:
1875 case Hexagon::S2_pstorerbf_io:
1876 case Hexagon::S2_pstorerbt_pi:
1877 case Hexagon::S2_pstorerbf_pi:
1878 case Hexagon::S2_pstorerdt_io:
1879 case Hexagon::S2_pstorerdf_io:
1880 case Hexagon::S4_pstorerdt_rr:
1881 case Hexagon::S4_pstorerdf_rr:
1882 case Hexagon::S2_pstorerdt_pi:
1883 case Hexagon::S2_pstorerdf_pi:
1884 case Hexagon::S2_pstorerht_io:
1885 case Hexagon::S2_pstorerhf_io:
1886 case Hexagon::S4_storeirht_io:
1887 case Hexagon::S4_storeirhf_io:
1888 case Hexagon::S4_pstorerht_rr:
1889 case Hexagon::S4_pstorerhf_rr:
1890 case Hexagon::S2_pstorerht_pi:
1891 case Hexagon::S2_pstorerhf_pi:
1892 case Hexagon::S2_pstorerit_io:
1893 case Hexagon::S2_pstorerif_io:
1894 case Hexagon::S4_storeirit_io:
1895 case Hexagon::S4_storeirif_io:
1896 case Hexagon::S4_pstorerit_rr:
1897 case Hexagon::S4_pstorerif_rr:
1898 case Hexagon::S2_pstorerit_pi:
1899 case Hexagon::S2_pstorerif_pi:
1900
1901 // V4 global address store before promoting to dot new.
1902 case Hexagon::S4_pstorerdt_abs:
1903 case Hexagon::S4_pstorerdf_abs:
1904 case Hexagon::S4_pstorerbt_abs:
1905 case Hexagon::S4_pstorerbf_abs:
1906 case Hexagon::S4_pstorerht_abs:
1907 case Hexagon::S4_pstorerhf_abs:
1908 case Hexagon::S4_pstorerit_abs:
1909 case Hexagon::S4_pstorerif_abs:
1910 return true;
1911
1912 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1913 // from the "Conditional Store" list. Because a predicated new value store
1914 // would NOT be promoted to a double dot new store.
1915 // This function returns yes for those stores that are predicated but not
1916 // yet promoted to predicate dot new instructions.
1917 }
1918}
1919
1920
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001921bool HexagonInstrInfo::isConditionalTransfer(const MachineInstr &MI) const {
1922 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001923 case Hexagon::A2_tfrt:
1924 case Hexagon::A2_tfrf:
1925 case Hexagon::C2_cmoveit:
1926 case Hexagon::C2_cmoveif:
1927 case Hexagon::A2_tfrtnew:
1928 case Hexagon::A2_tfrfnew:
1929 case Hexagon::C2_cmovenewit:
1930 case Hexagon::C2_cmovenewif:
1931 case Hexagon::A2_tfrpt:
1932 case Hexagon::A2_tfrpf:
1933 return true;
1934
1935 default:
1936 return false;
1937 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001938 return false;
1939}
1940
1941
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001942// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1943// isFPImm and later getFPImm as well.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001944bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
1945 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001946 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1947 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001948 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001949
1950 unsigned isExtendable =
1951 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1952 if (!isExtendable)
1953 return false;
1954
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001955 if (MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001956 return false;
1957
1958 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001959 const MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001960 // Use MO operand flags to determine if MO
1961 // has the HMOTF_ConstExtended flag set.
1962 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001963 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001964 // If this is a Machine BB address we are talking about, and it is
1965 // not marked as extended, say so.
1966 if (MO.isMBB())
1967 return false;
1968
1969 // We could be using an instruction with an extendable immediate and shoehorn
1970 // a global address into it. If it is a global address it will be constant
1971 // extended. We do this for COMBINE.
1972 // We currently only handle isGlobal() because it is the only kind of
1973 // object we are going to end up with here for now.
1974 // In the future we probably should add isSymbol(), etc.
1975 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001976 MO.isJTI() || MO.isCPI() || MO.isFPImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001977 return true;
1978
1979 // If the extendable operand is not 'Immediate' type, the instruction should
1980 // have 'isExtended' flag set.
1981 assert(MO.isImm() && "Extendable operand must be Immediate type");
1982
1983 int MinValue = getMinValue(MI);
1984 int MaxValue = getMaxValue(MI);
1985 int ImmValue = MO.getImm();
1986
1987 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001988}
1989
1990
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001991bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
1992 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001993 case Hexagon::L4_return :
1994 case Hexagon::L4_return_t :
1995 case Hexagon::L4_return_f :
1996 case Hexagon::L4_return_tnew_pnt :
1997 case Hexagon::L4_return_fnew_pnt :
1998 case Hexagon::L4_return_tnew_pt :
1999 case Hexagon::L4_return_fnew_pt :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002000 return true;
2001 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002002 return false;
2003}
2004
2005
2006// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002007bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
2008 const MachineInstr &ConsMI) const {
2009 if (!ProdMI.getDesc().getNumDefs())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002010 return false;
2011
2012 auto &HRI = getRegisterInfo();
2013
2014 SmallVector<unsigned, 4> DefsA;
2015 SmallVector<unsigned, 4> DefsB;
2016 SmallVector<unsigned, 8> UsesA;
2017 SmallVector<unsigned, 8> UsesB;
2018
2019 parseOperands(ProdMI, DefsA, UsesA);
2020 parseOperands(ConsMI, DefsB, UsesB);
2021
2022 for (auto &RegA : DefsA)
2023 for (auto &RegB : UsesB) {
2024 // True data dependency.
2025 if (RegA == RegB)
2026 return true;
2027
2028 if (Hexagon::DoubleRegsRegClass.contains(RegA))
2029 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
2030 if (RegB == *SubRegs)
2031 return true;
2032
2033 if (Hexagon::DoubleRegsRegClass.contains(RegB))
2034 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
2035 if (RegA == *SubRegs)
2036 return true;
2037 }
2038
2039 return false;
2040}
2041
2042
2043// Returns true if the instruction is alread a .cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002044bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
2045 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002046 case Hexagon::V6_vL32b_cur_pi:
2047 case Hexagon::V6_vL32b_cur_ai:
2048 case Hexagon::V6_vL32b_cur_pi_128B:
2049 case Hexagon::V6_vL32b_cur_ai_128B:
2050 return true;
2051 }
2052 return false;
2053}
2054
2055
2056// Returns true, if any one of the operands is a dot new
2057// insn, whether it is predicated dot new or register dot new.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002058bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
2059 if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002060 return true;
2061
2062 return false;
2063}
2064
2065
2066/// Symmetrical. See if these two instructions are fit for duplex pair.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002067bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
2068 const MachineInstr &MIb) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002069 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
2070 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
2071 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
2072}
2073
2074
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002075bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
2076 if (MI.mayLoad() || MI.mayStore() || MI.isCompare())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002077 return true;
2078
2079 // Multiply
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002080 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002081 if (SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23)
2082 return true;
2083 return false;
2084}
2085
2086
2087bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
2088 return (Opcode == Hexagon::ENDLOOP0 ||
2089 Opcode == Hexagon::ENDLOOP1);
2090}
2091
2092
2093bool HexagonInstrInfo::isExpr(unsigned OpType) const {
2094 switch(OpType) {
2095 case MachineOperand::MO_MachineBasicBlock:
2096 case MachineOperand::MO_GlobalAddress:
2097 case MachineOperand::MO_ExternalSymbol:
2098 case MachineOperand::MO_JumpTableIndex:
2099 case MachineOperand::MO_ConstantPoolIndex:
2100 case MachineOperand::MO_BlockAddress:
2101 return true;
2102 default:
2103 return false;
2104 }
2105}
2106
2107
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002108bool HexagonInstrInfo::isExtendable(const MachineInstr &MI) const {
2109 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002110 const uint64_t F = MID.TSFlags;
2111 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
2112 return true;
2113
2114 // TODO: This is largely obsolete now. Will need to be removed
2115 // in consecutive patches.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002116 switch (MI.getOpcode()) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002117 // PS_fi and PS_fia remain special cases.
2118 case Hexagon::PS_fi:
2119 case Hexagon::PS_fia:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002120 return true;
2121 default:
2122 return false;
2123 }
2124 return false;
2125}
2126
2127
2128// This returns true in two cases:
2129// - The OP code itself indicates that this is an extended instruction.
2130// - One of MOs has been marked with HMOTF_ConstExtended flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002131bool HexagonInstrInfo::isExtended(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002132 // First check if this is permanently extended op code.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002133 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002134 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2135 return true;
2136 // Use MO operand flags to determine if one of MI's operands
2137 // has HMOTF_ConstExtended flag set.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002138 for (MachineInstr::const_mop_iterator I = MI.operands_begin(),
2139 E = MI.operands_end(); I != E; ++I) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002140 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
2141 return true;
2142 }
2143 return false;
2144}
2145
2146
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002147bool HexagonInstrInfo::isFloat(const MachineInstr &MI) const {
2148 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002149 const uint64_t F = get(Opcode).TSFlags;
2150 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2151}
2152
2153
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002154// No V60 HVX VMEM with A_INDIRECT.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002155bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr &I,
2156 const MachineInstr &J) const {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002157 if (!isV60VectorInstruction(I))
2158 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002159 if (!I.mayLoad() && !I.mayStore())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002160 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002161 return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002162}
2163
2164
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002165bool HexagonInstrInfo::isIndirectCall(const MachineInstr &MI) const {
2166 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002167 case Hexagon::J2_callr :
2168 case Hexagon::J2_callrf :
2169 case Hexagon::J2_callrt :
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00002170 case Hexagon::PS_call_nr :
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002171 return true;
2172 }
2173 return false;
2174}
2175
2176
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002177bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr &MI) const {
2178 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002179 case Hexagon::L4_return :
2180 case Hexagon::L4_return_t :
2181 case Hexagon::L4_return_f :
2182 case Hexagon::L4_return_fnew_pnt :
2183 case Hexagon::L4_return_fnew_pt :
2184 case Hexagon::L4_return_tnew_pnt :
2185 case Hexagon::L4_return_tnew_pt :
2186 return true;
2187 }
2188 return false;
2189}
2190
2191
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002192bool HexagonInstrInfo::isJumpR(const MachineInstr &MI) const {
2193 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002194 case Hexagon::J2_jumpr :
2195 case Hexagon::J2_jumprt :
2196 case Hexagon::J2_jumprf :
2197 case Hexagon::J2_jumprtnewpt :
2198 case Hexagon::J2_jumprfnewpt :
2199 case Hexagon::J2_jumprtnew :
2200 case Hexagon::J2_jumprfnew :
2201 return true;
2202 }
2203 return false;
2204}
2205
2206
2207// Return true if a given MI can accomodate given offset.
2208// Use abs estimate as oppose to the exact number.
2209// TODO: This will need to be changed to use MC level
2210// definition of instruction extendable field size.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002211bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002212 unsigned offset) const {
2213 // This selection of jump instructions matches to that what
2214 // AnalyzeBranch can parse, plus NVJ.
2215 if (isNewValueJump(MI)) // r9:2
2216 return isInt<11>(offset);
2217
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002218 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002219 // Still missing Jump to address condition on register value.
2220 default:
2221 return false;
2222 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2223 case Hexagon::J2_call:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002224 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002225 return isInt<24>(offset);
2226 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2227 case Hexagon::J2_jumpf:
2228 case Hexagon::J2_jumptnew:
2229 case Hexagon::J2_jumptnewpt:
2230 case Hexagon::J2_jumpfnew:
2231 case Hexagon::J2_jumpfnewpt:
2232 case Hexagon::J2_callt:
2233 case Hexagon::J2_callf:
2234 return isInt<17>(offset);
2235 case Hexagon::J2_loop0i:
2236 case Hexagon::J2_loop0iext:
2237 case Hexagon::J2_loop0r:
2238 case Hexagon::J2_loop0rext:
2239 case Hexagon::J2_loop1i:
2240 case Hexagon::J2_loop1iext:
2241 case Hexagon::J2_loop1r:
2242 case Hexagon::J2_loop1rext:
2243 return isInt<9>(offset);
2244 // TODO: Add all the compound branches here. Can we do this in Relation model?
2245 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2246 case Hexagon::J4_cmpeqi_tp1_jump_nt:
2247 return isInt<11>(offset);
2248 }
2249}
2250
2251
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002252bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr &LRMI,
2253 const MachineInstr &ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002254 bool isLate = isLateResultInstr(LRMI);
2255 bool isEarly = isEarlySourceInstr(ESMI);
2256
2257 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002258 DEBUG(LRMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002259 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002260 DEBUG(ESMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002261
2262 if (isLate && isEarly) {
2263 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2264 return true;
2265 }
2266
2267 return false;
2268}
2269
2270
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002271bool HexagonInstrInfo::isLateResultInstr(const MachineInstr &MI) const {
2272 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002273 case TargetOpcode::EXTRACT_SUBREG:
2274 case TargetOpcode::INSERT_SUBREG:
2275 case TargetOpcode::SUBREG_TO_REG:
2276 case TargetOpcode::REG_SEQUENCE:
2277 case TargetOpcode::IMPLICIT_DEF:
2278 case TargetOpcode::COPY:
2279 case TargetOpcode::INLINEASM:
2280 case TargetOpcode::PHI:
2281 return false;
2282 default:
2283 break;
2284 }
2285
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002286 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002287
2288 switch (SchedClass) {
2289 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2290 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2291 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2292 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2293 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2294 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2295 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2296 case Hexagon::Sched::V2LDST_tc_ld_SLOT01:
2297 case Hexagon::Sched::V2LDST_tc_st_SLOT0:
2298 case Hexagon::Sched::V2LDST_tc_st_SLOT01:
2299 case Hexagon::Sched::V4LDST_tc_ld_SLOT01:
2300 case Hexagon::Sched::V4LDST_tc_st_SLOT0:
2301 case Hexagon::Sched::V4LDST_tc_st_SLOT01:
2302 return false;
2303 }
2304 return true;
2305}
2306
2307
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002308bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002309 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2310 // resource, but all operands can be received late like an ALU instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002311 return MI.getDesc().getSchedClass() == Hexagon::Sched::CVI_VX_LATE;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002312}
2313
2314
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002315bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2316 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002317 return Opcode == Hexagon::J2_loop0i ||
2318 Opcode == Hexagon::J2_loop0r ||
2319 Opcode == Hexagon::J2_loop0iext ||
2320 Opcode == Hexagon::J2_loop0rext ||
2321 Opcode == Hexagon::J2_loop1i ||
2322 Opcode == Hexagon::J2_loop1r ||
2323 Opcode == Hexagon::J2_loop1iext ||
2324 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002325}
2326
2327
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002328bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2329 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002330 default: return false;
2331 case Hexagon::L4_iadd_memopw_io :
2332 case Hexagon::L4_isub_memopw_io :
2333 case Hexagon::L4_add_memopw_io :
2334 case Hexagon::L4_sub_memopw_io :
2335 case Hexagon::L4_and_memopw_io :
2336 case Hexagon::L4_or_memopw_io :
2337 case Hexagon::L4_iadd_memoph_io :
2338 case Hexagon::L4_isub_memoph_io :
2339 case Hexagon::L4_add_memoph_io :
2340 case Hexagon::L4_sub_memoph_io :
2341 case Hexagon::L4_and_memoph_io :
2342 case Hexagon::L4_or_memoph_io :
2343 case Hexagon::L4_iadd_memopb_io :
2344 case Hexagon::L4_isub_memopb_io :
2345 case Hexagon::L4_add_memopb_io :
2346 case Hexagon::L4_sub_memopb_io :
2347 case Hexagon::L4_and_memopb_io :
2348 case Hexagon::L4_or_memopb_io :
2349 case Hexagon::L4_ior_memopb_io:
2350 case Hexagon::L4_ior_memoph_io:
2351 case Hexagon::L4_ior_memopw_io:
2352 case Hexagon::L4_iand_memopb_io:
2353 case Hexagon::L4_iand_memoph_io:
2354 case Hexagon::L4_iand_memopw_io:
2355 return true;
2356 }
2357 return false;
2358}
2359
2360
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002361bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2362 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002363 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2364}
2365
2366
2367bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2368 const uint64_t F = get(Opcode).TSFlags;
2369 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2370}
2371
2372
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002373bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002374 return isNewValueJump(MI) || isNewValueStore(MI);
2375}
2376
2377
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002378bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2379 return isNewValue(MI) && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002380}
2381
2382
2383bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2384 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2385}
2386
2387
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002388bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2389 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002390 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2391}
2392
2393
2394bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2395 const uint64_t F = get(Opcode).TSFlags;
2396 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2397}
2398
2399
2400// Returns true if a particular operand is extendable for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002401bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002402 unsigned OperandNum) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002403 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002404 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2405 == OperandNum;
2406}
2407
2408
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002409bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2410 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002411 assert(isPredicated(MI));
2412 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2413}
2414
2415
2416bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2417 const uint64_t F = get(Opcode).TSFlags;
2418 assert(isPredicated(Opcode));
2419 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2420}
2421
2422
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002423bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2424 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002425 return !((F >> HexagonII::PredicatedFalsePos) &
2426 HexagonII::PredicatedFalseMask);
2427}
2428
2429
2430bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2431 const uint64_t F = get(Opcode).TSFlags;
2432 // Make sure that the instruction is predicated.
2433 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2434 return !((F >> HexagonII::PredicatedFalsePos) &
2435 HexagonII::PredicatedFalseMask);
2436}
2437
2438
2439bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2440 const uint64_t F = get(Opcode).TSFlags;
2441 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2442}
2443
2444
2445bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2446 const uint64_t F = get(Opcode).TSFlags;
2447 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2448}
2449
2450
2451bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2452 const uint64_t F = get(Opcode).TSFlags;
2453 assert(get(Opcode).isBranch() &&
2454 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2455 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2456}
2457
2458
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002459bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2460 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2461 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2462 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2463 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002464}
2465
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002466bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2467 switch (MI.getOpcode()) {
2468 // Byte
2469 case Hexagon::L2_loadrb_io:
2470 case Hexagon::L4_loadrb_ur:
2471 case Hexagon::L4_loadrb_ap:
2472 case Hexagon::L2_loadrb_pr:
2473 case Hexagon::L2_loadrb_pbr:
2474 case Hexagon::L2_loadrb_pi:
2475 case Hexagon::L2_loadrb_pci:
2476 case Hexagon::L2_loadrb_pcr:
2477 case Hexagon::L2_loadbsw2_io:
2478 case Hexagon::L4_loadbsw2_ur:
2479 case Hexagon::L4_loadbsw2_ap:
2480 case Hexagon::L2_loadbsw2_pr:
2481 case Hexagon::L2_loadbsw2_pbr:
2482 case Hexagon::L2_loadbsw2_pi:
2483 case Hexagon::L2_loadbsw2_pci:
2484 case Hexagon::L2_loadbsw2_pcr:
2485 case Hexagon::L2_loadbsw4_io:
2486 case Hexagon::L4_loadbsw4_ur:
2487 case Hexagon::L4_loadbsw4_ap:
2488 case Hexagon::L2_loadbsw4_pr:
2489 case Hexagon::L2_loadbsw4_pbr:
2490 case Hexagon::L2_loadbsw4_pi:
2491 case Hexagon::L2_loadbsw4_pci:
2492 case Hexagon::L2_loadbsw4_pcr:
2493 case Hexagon::L4_loadrb_rr:
2494 case Hexagon::L2_ploadrbt_io:
2495 case Hexagon::L2_ploadrbt_pi:
2496 case Hexagon::L2_ploadrbf_io:
2497 case Hexagon::L2_ploadrbf_pi:
2498 case Hexagon::L2_ploadrbtnew_io:
2499 case Hexagon::L2_ploadrbfnew_io:
2500 case Hexagon::L4_ploadrbt_rr:
2501 case Hexagon::L4_ploadrbf_rr:
2502 case Hexagon::L4_ploadrbtnew_rr:
2503 case Hexagon::L4_ploadrbfnew_rr:
2504 case Hexagon::L2_ploadrbtnew_pi:
2505 case Hexagon::L2_ploadrbfnew_pi:
2506 case Hexagon::L4_ploadrbt_abs:
2507 case Hexagon::L4_ploadrbf_abs:
2508 case Hexagon::L4_ploadrbtnew_abs:
2509 case Hexagon::L4_ploadrbfnew_abs:
2510 case Hexagon::L2_loadrbgp:
2511 // Half
2512 case Hexagon::L2_loadrh_io:
2513 case Hexagon::L4_loadrh_ur:
2514 case Hexagon::L4_loadrh_ap:
2515 case Hexagon::L2_loadrh_pr:
2516 case Hexagon::L2_loadrh_pbr:
2517 case Hexagon::L2_loadrh_pi:
2518 case Hexagon::L2_loadrh_pci:
2519 case Hexagon::L2_loadrh_pcr:
2520 case Hexagon::L4_loadrh_rr:
2521 case Hexagon::L2_ploadrht_io:
2522 case Hexagon::L2_ploadrht_pi:
2523 case Hexagon::L2_ploadrhf_io:
2524 case Hexagon::L2_ploadrhf_pi:
2525 case Hexagon::L2_ploadrhtnew_io:
2526 case Hexagon::L2_ploadrhfnew_io:
2527 case Hexagon::L4_ploadrht_rr:
2528 case Hexagon::L4_ploadrhf_rr:
2529 case Hexagon::L4_ploadrhtnew_rr:
2530 case Hexagon::L4_ploadrhfnew_rr:
2531 case Hexagon::L2_ploadrhtnew_pi:
2532 case Hexagon::L2_ploadrhfnew_pi:
2533 case Hexagon::L4_ploadrht_abs:
2534 case Hexagon::L4_ploadrhf_abs:
2535 case Hexagon::L4_ploadrhtnew_abs:
2536 case Hexagon::L4_ploadrhfnew_abs:
2537 case Hexagon::L2_loadrhgp:
2538 return true;
2539 default:
2540 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002541 }
2542}
2543
2544
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002545bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2546 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002547 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2548}
2549
2550
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002551bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2552 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002553 case Hexagon::STriw_pred :
2554 case Hexagon::LDriw_pred :
2555 return true;
2556 default:
2557 return false;
2558 }
2559}
2560
2561
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002562bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2563 if (!MI.isBranch())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002564 return false;
2565
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002566 for (auto &Op : MI.operands())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002567 if (Op.isGlobal() || Op.isSymbol())
2568 return true;
2569 return false;
2570}
2571
2572
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002573// Returns true when SU has a timing class TC1.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002574bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2575 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002576 switch (SchedClass) {
2577 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2578 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2579 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2580 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2581 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2582 //case Hexagon::Sched::M_tc_1_SLOT23:
2583 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2584 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2585 return true;
2586
2587 default:
2588 return false;
2589 }
2590}
2591
2592
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002593bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2594 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002595 switch (SchedClass) {
2596 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
2597 case Hexagon::Sched::ALU64_tc_2_SLOT23:
2598 case Hexagon::Sched::CR_tc_2_SLOT3:
2599 case Hexagon::Sched::M_tc_2_SLOT23:
2600 case Hexagon::Sched::S_2op_tc_2_SLOT23:
2601 case Hexagon::Sched::S_3op_tc_2_SLOT23:
2602 return true;
2603
2604 default:
2605 return false;
2606 }
2607}
2608
2609
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002610bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2611 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002612 switch (SchedClass) {
2613 case Hexagon::Sched::ALU32_2op_tc_2early_SLOT0123:
2614 case Hexagon::Sched::ALU32_3op_tc_2early_SLOT0123:
2615 case Hexagon::Sched::ALU64_tc_2early_SLOT23:
2616 case Hexagon::Sched::CR_tc_2early_SLOT23:
2617 case Hexagon::Sched::CR_tc_2early_SLOT3:
2618 case Hexagon::Sched::J_tc_2early_SLOT0123:
2619 case Hexagon::Sched::J_tc_2early_SLOT2:
2620 case Hexagon::Sched::J_tc_2early_SLOT23:
2621 case Hexagon::Sched::S_2op_tc_2early_SLOT23:
2622 case Hexagon::Sched::S_3op_tc_2early_SLOT23:
2623 return true;
2624
2625 default:
2626 return false;
2627 }
2628}
2629
2630
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002631bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2632 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002633 return SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23;
2634}
2635
2636
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002637// Schedule this ASAP.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002638bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2639 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002640 if (mayBeCurLoad(MI1)) {
2641 // if (result of SU is used in Next) return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002642 unsigned DstReg = MI1.getOperand(0).getReg();
2643 int N = MI2.getNumOperands();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002644 for (int I = 0; I < N; I++)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002645 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002646 return true;
2647 }
2648 if (mayBeNewStore(MI2))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002649 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2650 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2651 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002652 return true;
2653 return false;
2654}
2655
2656
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002657bool HexagonInstrInfo::isV60VectorInstruction(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002658 const uint64_t V = getType(MI);
2659 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2660}
2661
2662
2663// Check if the Offset is a valid auto-inc imm by Load/Store Type.
2664//
2665bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2666 if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2667 VT == MVT::v32i16 || VT == MVT::v64i8) {
2668 return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2669 Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2670 (Offset & 0x3f) == 0);
2671 }
2672 // 128B
2673 if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2674 VT == MVT::v64i16 || VT == MVT::v128i8) {
2675 return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2676 Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2677 (Offset & 0x7f) == 0);
2678 }
2679 if (VT == MVT::i64) {
2680 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2681 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2682 (Offset & 0x7) == 0);
2683 }
2684 if (VT == MVT::i32) {
2685 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2686 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2687 (Offset & 0x3) == 0);
2688 }
2689 if (VT == MVT::i16) {
2690 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2691 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2692 (Offset & 0x1) == 0);
2693 }
2694 if (VT == MVT::i8) {
2695 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2696 Offset <= Hexagon_MEMB_AUTOINC_MAX);
2697 }
2698 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002699}
2700
2701
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002702bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2703 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002704 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002705 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002706 // inserted to calculate the final address. Due to this reason, the function
2707 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002708 // We used to assert if the offset was not properly aligned, however,
2709 // there are cases where a misaligned pointer recast can cause this
2710 // problem, and we need to allow for it. The front end warns of such
2711 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002712
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002713 switch (Opcode) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002714 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002715 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002716 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002717 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002718 case Hexagon::V6_vL32b_ai:
2719 case Hexagon::V6_vS32b_ai:
2720 case Hexagon::V6_vL32Ub_ai:
2721 case Hexagon::V6_vS32Ub_ai:
2722 return (Offset >= Hexagon_MEMV_OFFSET_MIN) &&
2723 (Offset <= Hexagon_MEMV_OFFSET_MAX);
2724
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002725 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002726 case Hexagon::PS_vstorerw_ai_128B:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002727 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002728 case Hexagon::PS_vloadrw_ai_128B:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002729 case Hexagon::V6_vL32b_ai_128B:
2730 case Hexagon::V6_vS32b_ai_128B:
2731 case Hexagon::V6_vL32Ub_ai_128B:
2732 case Hexagon::V6_vS32Ub_ai_128B:
2733 return (Offset >= Hexagon_MEMV_OFFSET_MIN_128B) &&
2734 (Offset <= Hexagon_MEMV_OFFSET_MAX_128B);
2735
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002736 case Hexagon::J2_loop0i:
2737 case Hexagon::J2_loop1i:
2738 return isUInt<10>(Offset);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00002739
2740 case Hexagon::S4_storeirb_io:
2741 case Hexagon::S4_storeirbt_io:
2742 case Hexagon::S4_storeirbf_io:
2743 return isUInt<6>(Offset);
2744
2745 case Hexagon::S4_storeirh_io:
2746 case Hexagon::S4_storeirht_io:
2747 case Hexagon::S4_storeirhf_io:
2748 return isShiftedUInt<6,1>(Offset);
2749
2750 case Hexagon::S4_storeiri_io:
2751 case Hexagon::S4_storeirit_io:
2752 case Hexagon::S4_storeirif_io:
2753 return isShiftedUInt<6,2>(Offset);
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002754 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002755
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002756 if (Extend)
2757 return true;
2758
2759 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002760 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002761 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002762 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2763 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2764
Colin LeMahieu947cd702014-12-23 20:44:59 +00002765 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002766 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002767 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2768 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2769
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002770 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002771 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002772 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002773 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2774 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2775
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002776 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002777 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002778 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002779 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2780 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2781
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002782 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002783 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2784 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2785
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002786 case Hexagon::L4_iadd_memopw_io :
2787 case Hexagon::L4_isub_memopw_io :
2788 case Hexagon::L4_add_memopw_io :
2789 case Hexagon::L4_sub_memopw_io :
2790 case Hexagon::L4_and_memopw_io :
2791 case Hexagon::L4_or_memopw_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002792 return (0 <= Offset && Offset <= 255);
2793
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002794 case Hexagon::L4_iadd_memoph_io :
2795 case Hexagon::L4_isub_memoph_io :
2796 case Hexagon::L4_add_memoph_io :
2797 case Hexagon::L4_sub_memoph_io :
2798 case Hexagon::L4_and_memoph_io :
2799 case Hexagon::L4_or_memoph_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002800 return (0 <= Offset && Offset <= 127);
2801
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002802 case Hexagon::L4_iadd_memopb_io :
2803 case Hexagon::L4_isub_memopb_io :
2804 case Hexagon::L4_add_memopb_io :
2805 case Hexagon::L4_sub_memopb_io :
2806 case Hexagon::L4_and_memopb_io :
2807 case Hexagon::L4_or_memopb_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002808 return (0 <= Offset && Offset <= 63);
2809
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002810 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002811 // any size. Later pass knows how to handle it.
2812 case Hexagon::STriw_pred:
2813 case Hexagon::LDriw_pred:
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00002814 case Hexagon::STriw_mod:
2815 case Hexagon::LDriw_mod:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002816 return true;
2817
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002818 case Hexagon::PS_fi:
2819 case Hexagon::PS_fia:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002820 case Hexagon::INLINEASM:
2821 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002822
2823 case Hexagon::L2_ploadrbt_io:
2824 case Hexagon::L2_ploadrbf_io:
2825 case Hexagon::L2_ploadrubt_io:
2826 case Hexagon::L2_ploadrubf_io:
2827 case Hexagon::S2_pstorerbt_io:
2828 case Hexagon::S2_pstorerbf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002829 return isUInt<6>(Offset);
2830
2831 case Hexagon::L2_ploadrht_io:
2832 case Hexagon::L2_ploadrhf_io:
2833 case Hexagon::L2_ploadruht_io:
2834 case Hexagon::L2_ploadruhf_io:
2835 case Hexagon::S2_pstorerht_io:
2836 case Hexagon::S2_pstorerhf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002837 return isShiftedUInt<6,1>(Offset);
2838
2839 case Hexagon::L2_ploadrit_io:
2840 case Hexagon::L2_ploadrif_io:
2841 case Hexagon::S2_pstorerit_io:
2842 case Hexagon::S2_pstorerif_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002843 return isShiftedUInt<6,2>(Offset);
2844
2845 case Hexagon::L2_ploadrdt_io:
2846 case Hexagon::L2_ploadrdf_io:
2847 case Hexagon::S2_pstorerdt_io:
2848 case Hexagon::S2_pstorerdf_io:
2849 return isShiftedUInt<6,3>(Offset);
2850 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002851
Benjamin Kramerb6684012011-12-27 11:41:05 +00002852 llvm_unreachable("No offset range is defined for this opcode. "
2853 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002854}
2855
2856
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002857bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
2858 return isV60VectorInstruction(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002859}
2860
2861
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002862bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2863 const uint64_t F = get(MI.getOpcode()).TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002864 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2865 return
2866 V == HexagonII::TypeCVI_VA ||
2867 V == HexagonII::TypeCVI_VA_DV;
2868}
Andrew Trickd06df962012-02-01 22:13:57 +00002869
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002870
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002871bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2872 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002873 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2874 return true;
2875
2876 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2877 return true;
2878
2879 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002880 return true;
2881
2882 return false;
2883}
Jyotsna Verma84256432013-03-01 17:37:13 +00002884
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002885bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2886 switch (MI.getOpcode()) {
2887 // Byte
2888 case Hexagon::L2_loadrub_io:
2889 case Hexagon::L4_loadrub_ur:
2890 case Hexagon::L4_loadrub_ap:
2891 case Hexagon::L2_loadrub_pr:
2892 case Hexagon::L2_loadrub_pbr:
2893 case Hexagon::L2_loadrub_pi:
2894 case Hexagon::L2_loadrub_pci:
2895 case Hexagon::L2_loadrub_pcr:
2896 case Hexagon::L2_loadbzw2_io:
2897 case Hexagon::L4_loadbzw2_ur:
2898 case Hexagon::L4_loadbzw2_ap:
2899 case Hexagon::L2_loadbzw2_pr:
2900 case Hexagon::L2_loadbzw2_pbr:
2901 case Hexagon::L2_loadbzw2_pi:
2902 case Hexagon::L2_loadbzw2_pci:
2903 case Hexagon::L2_loadbzw2_pcr:
2904 case Hexagon::L2_loadbzw4_io:
2905 case Hexagon::L4_loadbzw4_ur:
2906 case Hexagon::L4_loadbzw4_ap:
2907 case Hexagon::L2_loadbzw4_pr:
2908 case Hexagon::L2_loadbzw4_pbr:
2909 case Hexagon::L2_loadbzw4_pi:
2910 case Hexagon::L2_loadbzw4_pci:
2911 case Hexagon::L2_loadbzw4_pcr:
2912 case Hexagon::L4_loadrub_rr:
2913 case Hexagon::L2_ploadrubt_io:
2914 case Hexagon::L2_ploadrubt_pi:
2915 case Hexagon::L2_ploadrubf_io:
2916 case Hexagon::L2_ploadrubf_pi:
2917 case Hexagon::L2_ploadrubtnew_io:
2918 case Hexagon::L2_ploadrubfnew_io:
2919 case Hexagon::L4_ploadrubt_rr:
2920 case Hexagon::L4_ploadrubf_rr:
2921 case Hexagon::L4_ploadrubtnew_rr:
2922 case Hexagon::L4_ploadrubfnew_rr:
2923 case Hexagon::L2_ploadrubtnew_pi:
2924 case Hexagon::L2_ploadrubfnew_pi:
2925 case Hexagon::L4_ploadrubt_abs:
2926 case Hexagon::L4_ploadrubf_abs:
2927 case Hexagon::L4_ploadrubtnew_abs:
2928 case Hexagon::L4_ploadrubfnew_abs:
2929 case Hexagon::L2_loadrubgp:
2930 // Half
2931 case Hexagon::L2_loadruh_io:
2932 case Hexagon::L4_loadruh_ur:
2933 case Hexagon::L4_loadruh_ap:
2934 case Hexagon::L2_loadruh_pr:
2935 case Hexagon::L2_loadruh_pbr:
2936 case Hexagon::L2_loadruh_pi:
2937 case Hexagon::L2_loadruh_pci:
2938 case Hexagon::L2_loadruh_pcr:
2939 case Hexagon::L4_loadruh_rr:
2940 case Hexagon::L2_ploadruht_io:
2941 case Hexagon::L2_ploadruht_pi:
2942 case Hexagon::L2_ploadruhf_io:
2943 case Hexagon::L2_ploadruhf_pi:
2944 case Hexagon::L2_ploadruhtnew_io:
2945 case Hexagon::L2_ploadruhfnew_io:
2946 case Hexagon::L4_ploadruht_rr:
2947 case Hexagon::L4_ploadruhf_rr:
2948 case Hexagon::L4_ploadruhtnew_rr:
2949 case Hexagon::L4_ploadruhfnew_rr:
2950 case Hexagon::L2_ploadruhtnew_pi:
2951 case Hexagon::L2_ploadruhfnew_pi:
2952 case Hexagon::L4_ploadruht_abs:
2953 case Hexagon::L4_ploadruhf_abs:
2954 case Hexagon::L4_ploadruhtnew_abs:
2955 case Hexagon::L4_ploadruhfnew_abs:
2956 case Hexagon::L2_loadruhgp:
2957 return true;
2958 default:
2959 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002960 }
2961}
2962
2963
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002964// Add latency to instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002965bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2966 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002967 if (isV60VectorInstruction(MI1) && isV60VectorInstruction(MI2))
2968 if (!isVecUsableNextPacket(MI1, MI2))
2969 return true;
2970 return false;
2971}
2972
2973
Brendon Cahoon254f8892016-07-29 16:44:44 +00002974/// \brief Get the base register and byte offset of a load/store instr.
2975bool HexagonInstrInfo::getMemOpBaseRegImmOfs(MachineInstr &LdSt,
2976 unsigned &BaseReg, int64_t &Offset, const TargetRegisterInfo *TRI)
2977 const {
2978 unsigned AccessSize = 0;
2979 int OffsetVal = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002980 BaseReg = getBaseAndOffset(LdSt, OffsetVal, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002981 Offset = OffsetVal;
2982 return BaseReg != 0;
2983}
2984
2985
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002986/// \brief Can these instructions execute at the same time in a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002987bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2988 const MachineInstr &Second) const {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002989 if (DisableNVSchedule)
2990 return false;
2991 if (mayBeNewStore(Second)) {
2992 // Make sure the definition of the first instruction is the value being
2993 // stored.
2994 const MachineOperand &Stored =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002995 Second.getOperand(Second.getNumOperands() - 1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002996 if (!Stored.isReg())
2997 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002998 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
2999 const MachineOperand &Op = First.getOperand(i);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00003000 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
3001 return true;
3002 }
3003 }
3004 return false;
3005}
3006
3007
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00003008bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
3009 unsigned Opc = CallMI.getOpcode();
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003010 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00003011}
3012
3013
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003014bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
3015 for (auto &I : *B)
3016 if (I.isEHLabel())
3017 return true;
3018 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00003019}
3020
Jyotsna Verma84256432013-03-01 17:37:13 +00003021
3022// Returns true if an instruction can be converted into a non-extended
3023// equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003024bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00003025 short NonExtOpcode;
3026 // Check if the instruction has a register form that uses register in place
3027 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003028 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
Jyotsna Verma84256432013-03-01 17:37:13 +00003029 return true;
3030
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003031 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00003032 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00003033
3034 switch (getAddrMode(MI)) {
3035 case HexagonII::Absolute :
3036 // Load/store with absolute addressing mode can be converted into
3037 // base+offset mode.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003038 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00003039 break;
3040 case HexagonII::BaseImmOffset :
3041 // Load/store with base+offset addressing mode can be converted into
3042 // base+register offset addressing mode. However left shift operand should
3043 // be set to 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003044 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00003045 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003046 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003047 NonExtOpcode = Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003048 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00003049 default:
3050 return false;
3051 }
3052 if (NonExtOpcode < 0)
3053 return false;
3054 return true;
3055 }
3056 return false;
3057}
3058
Jyotsna Verma84256432013-03-01 17:37:13 +00003059
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003060bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
3061 return Hexagon::getRealHWInstr(MI.getOpcode(),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003062 Hexagon::InstrType_Pseudo) >= 0;
3063}
3064
3065
3066bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
3067 const {
3068 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
3069 while (I != E) {
3070 if (I->isBarrier())
3071 return true;
3072 ++I;
3073 }
3074 return false;
3075}
3076
3077
3078// Returns true, if a LD insn can be promoted to a cur load.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003079bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
3080 auto &HST = MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>();
3081 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003082 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
3083 HST.hasV60TOps();
3084}
3085
3086
3087// Returns true, if a ST insn can be promoted to a new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003088bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
3089 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003090 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
3091}
3092
3093
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003094bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
3095 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003096 // There is no stall when ProdMI is not a V60 vector.
3097 if (!isV60VectorInstruction(ProdMI))
3098 return false;
3099
3100 // There is no stall when ProdMI and ConsMI are not dependent.
3101 if (!isDependent(ProdMI, ConsMI))
3102 return false;
3103
3104 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
3105 // are scheduled in consecutive packets.
3106 if (isVecUsableNextPacket(ProdMI, ConsMI))
3107 return false;
3108
3109 return true;
3110}
3111
3112
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003113bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003114 MachineBasicBlock::const_instr_iterator BII) const {
3115 // There is no stall when I is not a V60 vector.
3116 if (!isV60VectorInstruction(MI))
3117 return false;
3118
3119 MachineBasicBlock::const_instr_iterator MII = BII;
3120 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
3121
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003122 if (!MII->isBundle()) {
3123 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003124 if (!isV60VectorInstruction(J))
3125 return false;
3126 else if (isVecUsableNextPacket(J, MI))
3127 return false;
3128 return true;
3129 }
3130
3131 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003132 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003133 if (producesStall(J, MI))
3134 return true;
3135 }
3136 return false;
3137}
3138
3139
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003140bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003141 unsigned PredReg) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003142 for (unsigned opNum = 0; opNum < MI.getNumOperands(); opNum++) {
3143 const MachineOperand &MO = MI.getOperand(opNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003144 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
3145 return false; // Predicate register must be explicitly defined.
3146 }
3147
3148 // Hexagon Programmer's Reference says that decbin, memw_locked, and
3149 // memd_locked cannot be used as .new as well,
3150 // but we don't seem to have these instructions defined.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003151 return MI.getOpcode() != Hexagon::A4_tlbmatch;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003152}
3153
3154
3155bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
3156 return (Opcode == Hexagon::J2_jumpt) ||
3157 (Opcode == Hexagon::J2_jumpf) ||
3158 (Opcode == Hexagon::J2_jumptnew) ||
3159 (Opcode == Hexagon::J2_jumpfnew) ||
3160 (Opcode == Hexagon::J2_jumptnewpt) ||
3161 (Opcode == Hexagon::J2_jumpfnewpt);
3162}
3163
3164
3165bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
3166 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
3167 return false;
3168 return !isPredicatedTrue(Cond[0].getImm());
3169}
3170
3171
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003172short HexagonInstrInfo::getAbsoluteForm(const MachineInstr &MI) const {
3173 return Hexagon::getAbsoluteForm(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003174}
3175
3176
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003177unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
3178 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003179 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3180}
3181
3182
3183// Returns the base register in a memory access (load/store). The offset is
3184// returned in Offset and the access size is returned in AccessSize.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003185unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003186 int &Offset, unsigned &AccessSize) const {
3187 // Return if it is not a base+offset type instruction or a MemOp.
3188 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3189 getAddrMode(MI) != HexagonII::BaseLongOffset &&
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003190 !isMemOp(MI) && !isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003191 return 0;
3192
3193 // Since it is a memory access instruction, getMemAccessSize() should never
3194 // return 0.
3195 assert (getMemAccessSize(MI) &&
3196 "BaseImmOffset or BaseLongOffset or MemOp without accessSize");
3197
3198 // Return Values of getMemAccessSize() are
3199 // 0 - Checked in the assert above.
3200 // 1, 2, 3, 4 & 7, 8 - The statement below is correct for all these.
3201 // MemAccessSize is represented as 1+log2(N) where N is size in bits.
3202 AccessSize = (1U << (getMemAccessSize(MI) - 1));
3203
3204 unsigned basePos = 0, offsetPos = 0;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003205 if (!getBaseAndOffsetPosition(MI, basePos, offsetPos))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003206 return 0;
3207
3208 // Post increment updates its EA after the mem access,
3209 // so we need to treat its offset as zero.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003210 if (isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003211 Offset = 0;
3212 else {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003213 Offset = MI.getOperand(offsetPos).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003214 }
3215
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003216 return MI.getOperand(basePos).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003217}
3218
3219
3220/// Return the position of the base and offset operands for this instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003221bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003222 unsigned &BasePos, unsigned &OffsetPos) const {
3223 // Deal with memops first.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003224 if (isMemOp(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003225 BasePos = 0;
3226 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003227 } else if (MI.mayStore()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003228 BasePos = 0;
3229 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003230 } else if (MI.mayLoad()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003231 BasePos = 1;
3232 OffsetPos = 2;
3233 } else
3234 return false;
3235
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003236 if (isPredicated(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003237 BasePos++;
3238 OffsetPos++;
3239 }
3240 if (isPostIncrement(MI)) {
3241 BasePos++;
3242 OffsetPos++;
3243 }
3244
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003245 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003246 return false;
3247
3248 return true;
3249}
3250
3251
3252// Inserts branching instructions in reverse order of their occurence.
3253// e.g. jump_t t1 (i1)
3254// jump t2 (i2)
3255// Jumpers = {i2, i1}
3256SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3257 MachineBasicBlock& MBB) const {
3258 SmallVector<MachineInstr*, 2> Jumpers;
3259 // If the block has no terminators, it just falls into the block after it.
3260 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3261 if (I == MBB.instr_begin())
3262 return Jumpers;
3263
3264 // A basic block may looks like this:
3265 //
3266 // [ insn
3267 // EH_LABEL
3268 // insn
3269 // insn
3270 // insn
3271 // EH_LABEL
3272 // insn ]
3273 //
3274 // It has two succs but does not have a terminator
3275 // Don't know how to handle it.
3276 do {
3277 --I;
3278 if (I->isEHLabel())
3279 return Jumpers;
3280 } while (I != MBB.instr_begin());
3281
3282 I = MBB.instr_end();
3283 --I;
3284
3285 while (I->isDebugValue()) {
3286 if (I == MBB.instr_begin())
3287 return Jumpers;
3288 --I;
3289 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003290 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003291 return Jumpers;
3292
3293 // Get the last instruction in the block.
3294 MachineInstr *LastInst = &*I;
3295 Jumpers.push_back(LastInst);
3296 MachineInstr *SecondLastInst = nullptr;
3297 // Find one more terminator if present.
3298 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003299 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003300 if (!SecondLastInst) {
3301 SecondLastInst = &*I;
3302 Jumpers.push_back(SecondLastInst);
3303 } else // This is a third branch.
3304 return Jumpers;
3305 }
3306 if (I == MBB.instr_begin())
3307 break;
3308 --I;
3309 } while (true);
3310 return Jumpers;
3311}
3312
3313
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003314short HexagonInstrInfo::getBaseWithLongOffset(short Opcode) const {
3315 if (Opcode < 0)
3316 return -1;
3317 return Hexagon::getBaseWithLongOffset(Opcode);
3318}
3319
3320
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003321short HexagonInstrInfo::getBaseWithLongOffset(const MachineInstr &MI) const {
3322 return Hexagon::getBaseWithLongOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003323}
3324
3325
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003326short HexagonInstrInfo::getBaseWithRegOffset(const MachineInstr &MI) const {
3327 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003328}
3329
3330
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003331// Returns Operand Index for the constant extended instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003332unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
3333 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003334 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3335}
3336
3337// See if instruction could potentially be a duplex candidate.
3338// If so, return its group. Zero otherwise.
3339HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003340 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003341 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3342
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003343 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003344 default:
3345 return HexagonII::HCG_None;
3346 //
3347 // Compound pairs.
3348 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3349 // "Rd16=#U6 ; jump #r9:2"
3350 // "Rd16=Rs16 ; jump #r9:2"
3351 //
3352 case Hexagon::C2_cmpeq:
3353 case Hexagon::C2_cmpgt:
3354 case Hexagon::C2_cmpgtu:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003355 DstReg = MI.getOperand(0).getReg();
3356 Src1Reg = MI.getOperand(1).getReg();
3357 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003358 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3359 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3360 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3361 return HexagonII::HCG_A;
3362 break;
3363 case Hexagon::C2_cmpeqi:
3364 case Hexagon::C2_cmpgti:
3365 case Hexagon::C2_cmpgtui:
3366 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003367 DstReg = MI.getOperand(0).getReg();
3368 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003369 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3370 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003371 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3372 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3373 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003374 return HexagonII::HCG_A;
3375 break;
3376 case Hexagon::A2_tfr:
3377 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003378 DstReg = MI.getOperand(0).getReg();
3379 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003380 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3381 return HexagonII::HCG_A;
3382 break;
3383 case Hexagon::A2_tfrsi:
3384 // Rd = #u6
3385 // Do not test for #u6 size since the const is getting extended
3386 // regardless and compound could be formed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003387 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003388 if (isIntRegForSubInst(DstReg))
3389 return HexagonII::HCG_A;
3390 break;
3391 case Hexagon::S2_tstbit_i:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003392 DstReg = MI.getOperand(0).getReg();
3393 Src1Reg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003394 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3395 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003396 MI.getOperand(2).isImm() &&
3397 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003398 return HexagonII::HCG_A;
3399 break;
3400 // The fact that .new form is used pretty much guarantees
3401 // that predicate register will match. Nevertheless,
3402 // there could be some false positives without additional
3403 // checking.
3404 case Hexagon::J2_jumptnew:
3405 case Hexagon::J2_jumpfnew:
3406 case Hexagon::J2_jumptnewpt:
3407 case Hexagon::J2_jumpfnewpt:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003408 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003409 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3410 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3411 return HexagonII::HCG_B;
3412 break;
3413 // Transfer and jump:
3414 // Rd=#U6 ; jump #r9:2
3415 // Rd=Rs ; jump #r9:2
3416 // Do not test for jump range here.
3417 case Hexagon::J2_jump:
3418 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003419 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003420 return HexagonII::HCG_C;
3421 break;
3422 }
3423
3424 return HexagonII::HCG_None;
3425}
3426
3427
3428// Returns -1 when there is no opcode found.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003429unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3430 const MachineInstr &GB) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003431 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3432 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003433 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3434 (GB.getOpcode() != Hexagon::J2_jumptnew))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003435 return -1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003436 unsigned DestReg = GA.getOperand(0).getReg();
3437 if (!GB.readsRegister(DestReg))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003438 return -1;
3439 if (DestReg == Hexagon::P0)
3440 return Hexagon::J4_cmpeqi_tp0_jump_nt;
3441 if (DestReg == Hexagon::P1)
3442 return Hexagon::J4_cmpeqi_tp1_jump_nt;
3443 return -1;
3444}
3445
3446
3447int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3448 enum Hexagon::PredSense inPredSense;
3449 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3450 Hexagon::PredSense_true;
3451 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3452 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3453 return CondOpcode;
3454
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003455 llvm_unreachable("Unexpected predicable instruction");
3456}
3457
3458
3459// Return the cur value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003460int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3461 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003462 default: llvm_unreachable("Unknown .cur type");
3463 case Hexagon::V6_vL32b_pi:
3464 return Hexagon::V6_vL32b_cur_pi;
3465 case Hexagon::V6_vL32b_ai:
3466 return Hexagon::V6_vL32b_cur_ai;
3467 //128B
3468 case Hexagon::V6_vL32b_pi_128B:
3469 return Hexagon::V6_vL32b_cur_pi_128B;
3470 case Hexagon::V6_vL32b_ai_128B:
3471 return Hexagon::V6_vL32b_cur_ai_128B;
3472 }
3473 return 0;
3474}
3475
3476
3477
3478// The diagram below shows the steps involved in the conversion of a predicated
3479// store instruction to its .new predicated new-value form.
3480//
3481// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3482// ^ ^
3483// / \ (not OK. it will cause new-value store to be
3484// / X conditional on p0.new while R2 producer is
3485// / \ on p0)
3486// / \.
3487// p.new store p.old NV store
3488// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3489// ^ ^
3490// \ /
3491// \ /
3492// \ /
3493// p.old store
3494// [if (p0)memw(R0+#0)=R2]
3495//
3496//
3497// The following set of instructions further explains the scenario where
3498// conditional new-value store becomes invalid when promoted to .new predicate
3499// form.
3500//
3501// { 1) if (p0) r0 = add(r1, r2)
3502// 2) p0 = cmp.eq(r3, #0) }
3503//
3504// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3505// the first two instructions because in instr 1, r0 is conditional on old value
3506// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3507// is not valid for new-value stores.
3508// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3509// from the "Conditional Store" list. Because a predicated new value store
3510// would NOT be promoted to a double dot new store. See diagram below:
3511// This function returns yes for those stores that are predicated but not
3512// yet promoted to predicate dot new instructions.
3513//
3514// +---------------------+
3515// /-----| if (p0) memw(..)=r0 |---------\~
3516// || +---------------------+ ||
3517// promote || /\ /\ || promote
3518// || /||\ /||\ ||
3519// \||/ demote || \||/
3520// \/ || || \/
3521// +-------------------------+ || +-------------------------+
3522// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3523// +-------------------------+ || +-------------------------+
3524// || || ||
3525// || demote \||/
3526// promote || \/ NOT possible
3527// || || /\~
3528// \||/ || /||\~
3529// \/ || ||
3530// +-----------------------------+
3531// | if (p0.new) memw(..)=r0.new |
3532// +-----------------------------+
3533// Double Dot New Store
3534//
3535// Returns the most basic instruction for the .new predicated instructions and
3536// new-value stores.
3537// For example, all of the following instructions will be converted back to the
3538// same instruction:
3539// 1) if (p0.new) memw(R0+#0) = R1.new --->
3540// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3541// 3) if (p0.new) memw(R0+#0) = R1 --->
3542//
3543// To understand the translation of instruction 1 to its original form, consider
3544// a packet with 3 instructions.
3545// { p0 = cmp.eq(R0,R1)
3546// if (p0.new) R2 = add(R3, R4)
3547// R5 = add (R3, R1)
3548// }
3549// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3550//
3551// This instruction can be part of the previous packet only if both p0 and R2
3552// are promoted to .new values. This promotion happens in steps, first
3553// predicate register is promoted to .new and in the next iteration R2 is
3554// promoted. Therefore, in case of dependence check failure (due to R5) during
3555// next iteration, it should be converted back to its most basic form.
3556
3557
3558// Return the new value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003559int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3560 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003561 if (NVOpcode >= 0) // Valid new-value store instruction.
3562 return NVOpcode;
3563
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003564 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003565 default: llvm_unreachable("Unknown .new type");
3566 case Hexagon::S4_storerb_ur:
3567 return Hexagon::S4_storerbnew_ur;
3568
3569 case Hexagon::S2_storerb_pci:
3570 return Hexagon::S2_storerb_pci;
3571
3572 case Hexagon::S2_storeri_pci:
3573 return Hexagon::S2_storeri_pci;
3574
3575 case Hexagon::S2_storerh_pci:
3576 return Hexagon::S2_storerh_pci;
3577
3578 case Hexagon::S2_storerd_pci:
3579 return Hexagon::S2_storerd_pci;
3580
3581 case Hexagon::S2_storerf_pci:
3582 return Hexagon::S2_storerf_pci;
3583
3584 case Hexagon::V6_vS32b_ai:
3585 return Hexagon::V6_vS32b_new_ai;
3586
3587 case Hexagon::V6_vS32b_pi:
3588 return Hexagon::V6_vS32b_new_pi;
3589
3590 // 128B
3591 case Hexagon::V6_vS32b_ai_128B:
3592 return Hexagon::V6_vS32b_new_ai_128B;
3593
3594 case Hexagon::V6_vS32b_pi_128B:
3595 return Hexagon::V6_vS32b_new_pi_128B;
3596 }
3597 return 0;
3598}
3599
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00003600
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003601// Returns the opcode to use when converting MI, which is a conditional jump,
3602// into a conditional instruction which uses the .new value of the predicate.
3603// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003604int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003605 const MachineBranchProbabilityInfo *MBPI) const {
3606 // We assume that block can have at most two successors.
3607 bool taken = false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003608 const MachineBasicBlock *Src = MI.getParent();
3609 const MachineOperand &BrTarget = MI.getOperand(1);
3610 const MachineBasicBlock *Dst = BrTarget.getMBB();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003611
3612 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
3613 if (Prediction >= BranchProbability(1,2))
3614 taken = true;
3615
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003616 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003617 case Hexagon::J2_jumpt:
3618 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3619 case Hexagon::J2_jumpf:
3620 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3621
3622 default:
3623 llvm_unreachable("Unexpected jump instruction.");
3624 }
3625}
3626
3627
3628// Return .new predicate version for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003629int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003630 const MachineBranchProbabilityInfo *MBPI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003631 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003632 if (NewOpcode >= 0) // Valid predicate new instruction
3633 return NewOpcode;
3634
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003635 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003636 // Condtional Jumps
3637 case Hexagon::J2_jumpt:
3638 case Hexagon::J2_jumpf:
3639 return getDotNewPredJumpOp(MI, MBPI);
3640
3641 default:
3642 assert(0 && "Unknown .new type");
3643 }
3644 return 0;
3645}
3646
3647
3648int HexagonInstrInfo::getDotOldOp(const int opc) const {
3649 int NewOp = opc;
3650 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3651 NewOp = Hexagon::getPredOldOpcode(NewOp);
3652 assert(NewOp >= 0 &&
3653 "Couldn't change predicate new instruction to its old form.");
3654 }
3655
3656 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3657 NewOp = Hexagon::getNonNVStore(NewOp);
3658 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3659 }
3660 return NewOp;
3661}
3662
3663
3664// See if instruction could potentially be a duplex candidate.
3665// If so, return its group. Zero otherwise.
3666HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003667 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003668 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3669 auto &HRI = getRegisterInfo();
3670
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003671 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003672 default:
3673 return HexagonII::HSIG_None;
3674 //
3675 // Group L1:
3676 //
3677 // Rd = memw(Rs+#u4:2)
3678 // Rd = memub(Rs+#u4:0)
3679 case Hexagon::L2_loadri_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003680 DstReg = MI.getOperand(0).getReg();
3681 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003682 // Special case this one from Group L2.
3683 // Rd = memw(r29+#u5:2)
3684 if (isIntRegForSubInst(DstReg)) {
3685 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3686 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003687 MI.getOperand(2).isImm() &&
3688 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003689 return HexagonII::HSIG_L2;
3690 // Rd = memw(Rs+#u4:2)
3691 if (isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003692 (MI.getOperand(2).isImm() &&
3693 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003694 return HexagonII::HSIG_L1;
3695 }
3696 break;
3697 case Hexagon::L2_loadrub_io:
3698 // Rd = memub(Rs+#u4:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003699 DstReg = MI.getOperand(0).getReg();
3700 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003701 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003702 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003703 return HexagonII::HSIG_L1;
3704 break;
3705 //
3706 // Group L2:
3707 //
3708 // Rd = memh/memuh(Rs+#u3:1)
3709 // Rd = memb(Rs+#u3:0)
3710 // Rd = memw(r29+#u5:2) - Handled above.
3711 // Rdd = memd(r29+#u5:3)
3712 // deallocframe
3713 // [if ([!]p0[.new])] dealloc_return
3714 // [if ([!]p0[.new])] jumpr r31
3715 case Hexagon::L2_loadrh_io:
3716 case Hexagon::L2_loadruh_io:
3717 // Rd = memh/memuh(Rs+#u3:1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003718 DstReg = MI.getOperand(0).getReg();
3719 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003720 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003721 MI.getOperand(2).isImm() &&
3722 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003723 return HexagonII::HSIG_L2;
3724 break;
3725 case Hexagon::L2_loadrb_io:
3726 // Rd = memb(Rs+#u3:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003727 DstReg = MI.getOperand(0).getReg();
3728 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003729 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003730 MI.getOperand(2).isImm() &&
3731 isUInt<3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003732 return HexagonII::HSIG_L2;
3733 break;
3734 case Hexagon::L2_loadrd_io:
3735 // Rdd = memd(r29+#u5:3)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003736 DstReg = MI.getOperand(0).getReg();
3737 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003738 if (isDblRegForSubInst(DstReg, HRI) &&
3739 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3740 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003741 MI.getOperand(2).isImm() &&
3742 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003743 return HexagonII::HSIG_L2;
3744 break;
3745 // dealloc_return is not documented in Hexagon Manual, but marked
3746 // with A_SUBINSN attribute in iset_v4classic.py.
3747 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
Krzysztof Parzyszek5a7bef92016-08-19 17:20:57 +00003748 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4_PIC:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003749 case Hexagon::L4_return:
3750 case Hexagon::L2_deallocframe:
3751 return HexagonII::HSIG_L2;
3752 case Hexagon::EH_RETURN_JMPR:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003753 case Hexagon::PS_jmpret:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003754 // jumpr r31
3755 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003756 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003757 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3758 return HexagonII::HSIG_L2;
3759 break;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003760 case Hexagon::PS_jmprett:
3761 case Hexagon::PS_jmpretf:
3762 case Hexagon::PS_jmprettnewpt:
3763 case Hexagon::PS_jmpretfnewpt:
3764 case Hexagon::PS_jmprettnew:
3765 case Hexagon::PS_jmpretfnew:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003766 DstReg = MI.getOperand(1).getReg();
3767 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003768 // [if ([!]p0[.new])] jumpr r31
3769 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3770 (Hexagon::P0 == SrcReg)) &&
3771 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3772 return HexagonII::HSIG_L2;
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00003773 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003774 case Hexagon::L4_return_t :
3775 case Hexagon::L4_return_f :
3776 case Hexagon::L4_return_tnew_pnt :
3777 case Hexagon::L4_return_fnew_pnt :
3778 case Hexagon::L4_return_tnew_pt :
3779 case Hexagon::L4_return_fnew_pt :
3780 // [if ([!]p0[.new])] dealloc_return
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003781 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003782 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3783 return HexagonII::HSIG_L2;
3784 break;
3785 //
3786 // Group S1:
3787 //
3788 // memw(Rs+#u4:2) = Rt
3789 // memb(Rs+#u4:0) = Rt
3790 case Hexagon::S2_storeri_io:
3791 // Special case this one from Group S2.
3792 // memw(r29+#u5:2) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003793 Src1Reg = MI.getOperand(0).getReg();
3794 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003795 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3796 isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003797 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3798 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003799 return HexagonII::HSIG_S2;
3800 // memw(Rs+#u4:2) = Rt
3801 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003802 MI.getOperand(1).isImm() &&
3803 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003804 return HexagonII::HSIG_S1;
3805 break;
3806 case Hexagon::S2_storerb_io:
3807 // memb(Rs+#u4:0) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003808 Src1Reg = MI.getOperand(0).getReg();
3809 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003810 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003811 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003812 return HexagonII::HSIG_S1;
3813 break;
3814 //
3815 // Group S2:
3816 //
3817 // memh(Rs+#u3:1) = Rt
3818 // memw(r29+#u5:2) = Rt
3819 // memd(r29+#s6:3) = Rtt
3820 // memw(Rs+#u4:2) = #U1
3821 // memb(Rs+#u4) = #U1
3822 // allocframe(#u5:3)
3823 case Hexagon::S2_storerh_io:
3824 // memh(Rs+#u3:1) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003825 Src1Reg = MI.getOperand(0).getReg();
3826 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003827 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003828 MI.getOperand(1).isImm() &&
3829 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003830 return HexagonII::HSIG_S1;
3831 break;
3832 case Hexagon::S2_storerd_io:
3833 // memd(r29+#s6:3) = Rtt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003834 Src1Reg = MI.getOperand(0).getReg();
3835 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003836 if (isDblRegForSubInst(Src2Reg, HRI) &&
3837 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003838 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3839 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003840 return HexagonII::HSIG_S2;
3841 break;
3842 case Hexagon::S4_storeiri_io:
3843 // memw(Rs+#u4:2) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003844 Src1Reg = MI.getOperand(0).getReg();
3845 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
3846 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
3847 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003848 return HexagonII::HSIG_S2;
3849 break;
3850 case Hexagon::S4_storeirb_io:
3851 // memb(Rs+#u4) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003852 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003853 if (isIntRegForSubInst(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003854 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
3855 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003856 return HexagonII::HSIG_S2;
3857 break;
3858 case Hexagon::S2_allocframe:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003859 if (MI.getOperand(0).isImm() &&
3860 isShiftedUInt<5,3>(MI.getOperand(0).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003861 return HexagonII::HSIG_S1;
3862 break;
3863 //
3864 // Group A:
3865 //
3866 // Rx = add(Rx,#s7)
3867 // Rd = Rs
3868 // Rd = #u6
3869 // Rd = #-1
3870 // if ([!]P0[.new]) Rd = #0
3871 // Rd = add(r29,#u6:2)
3872 // Rx = add(Rx,Rs)
3873 // P0 = cmp.eq(Rs,#u2)
3874 // Rdd = combine(#0,Rs)
3875 // Rdd = combine(Rs,#0)
3876 // Rdd = combine(#u2,#U2)
3877 // Rd = add(Rs,#1)
3878 // Rd = add(Rs,#-1)
3879 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3880 // Rd = and(Rs,#1)
3881 case Hexagon::A2_addi:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003882 DstReg = MI.getOperand(0).getReg();
3883 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003884 if (isIntRegForSubInst(DstReg)) {
3885 // Rd = add(r29,#u6:2)
3886 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003887 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
3888 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003889 return HexagonII::HSIG_A;
3890 // Rx = add(Rx,#s7)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003891 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
3892 isInt<7>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003893 return HexagonII::HSIG_A;
3894 // Rd = add(Rs,#1)
3895 // Rd = add(Rs,#-1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003896 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3897 ((MI.getOperand(2).getImm() == 1) ||
3898 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003899 return HexagonII::HSIG_A;
3900 }
3901 break;
3902 case Hexagon::A2_add:
3903 // Rx = add(Rx,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003904 DstReg = MI.getOperand(0).getReg();
3905 Src1Reg = MI.getOperand(1).getReg();
3906 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003907 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3908 isIntRegForSubInst(Src2Reg))
3909 return HexagonII::HSIG_A;
3910 break;
3911 case Hexagon::A2_andir:
3912 // Same as zxtb.
3913 // Rd16=and(Rs16,#255)
3914 // Rd16=and(Rs16,#1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003915 DstReg = MI.getOperand(0).getReg();
3916 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003917 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003918 MI.getOperand(2).isImm() &&
3919 ((MI.getOperand(2).getImm() == 1) ||
3920 (MI.getOperand(2).getImm() == 255)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003921 return HexagonII::HSIG_A;
3922 break;
3923 case Hexagon::A2_tfr:
3924 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003925 DstReg = MI.getOperand(0).getReg();
3926 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003927 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3928 return HexagonII::HSIG_A;
3929 break;
3930 case Hexagon::A2_tfrsi:
3931 // Rd = #u6
3932 // Do not test for #u6 size since the const is getting extended
3933 // regardless and compound could be formed.
3934 // Rd = #-1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003935 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003936 if (isIntRegForSubInst(DstReg))
3937 return HexagonII::HSIG_A;
3938 break;
3939 case Hexagon::C2_cmoveit:
3940 case Hexagon::C2_cmovenewit:
3941 case Hexagon::C2_cmoveif:
3942 case Hexagon::C2_cmovenewif:
3943 // if ([!]P0[.new]) Rd = #0
3944 // Actual form:
3945 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003946 DstReg = MI.getOperand(0).getReg();
3947 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003948 if (isIntRegForSubInst(DstReg) &&
3949 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003950 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003951 return HexagonII::HSIG_A;
3952 break;
3953 case Hexagon::C2_cmpeqi:
3954 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003955 DstReg = MI.getOperand(0).getReg();
3956 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003957 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3958 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003959 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003960 return HexagonII::HSIG_A;
3961 break;
3962 case Hexagon::A2_combineii:
3963 case Hexagon::A4_combineii:
3964 // Rdd = combine(#u2,#U2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003965 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003966 if (isDblRegForSubInst(DstReg, HRI) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003967 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
3968 (MI.getOperand(1).isGlobal() &&
3969 isUInt<2>(MI.getOperand(1).getOffset()))) &&
3970 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
3971 (MI.getOperand(2).isGlobal() &&
3972 isUInt<2>(MI.getOperand(2).getOffset()))))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003973 return HexagonII::HSIG_A;
3974 break;
3975 case Hexagon::A4_combineri:
3976 // Rdd = combine(Rs,#0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003977 DstReg = MI.getOperand(0).getReg();
3978 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003979 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003980 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
3981 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003982 return HexagonII::HSIG_A;
3983 break;
3984 case Hexagon::A4_combineir:
3985 // Rdd = combine(#0,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003986 DstReg = MI.getOperand(0).getReg();
3987 SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003988 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003989 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
3990 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003991 return HexagonII::HSIG_A;
3992 break;
3993 case Hexagon::A2_sxtb:
3994 case Hexagon::A2_sxth:
3995 case Hexagon::A2_zxtb:
3996 case Hexagon::A2_zxth:
3997 // Rd = sxth/sxtb/zxtb/zxth(Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003998 DstReg = MI.getOperand(0).getReg();
3999 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004000 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
4001 return HexagonII::HSIG_A;
4002 break;
4003 }
4004
4005 return HexagonII::HSIG_None;
4006}
4007
4008
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004009short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
4010 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004011}
4012
4013
4014// Return first non-debug instruction in the basic block.
4015MachineInstr *HexagonInstrInfo::getFirstNonDbgInst(MachineBasicBlock *BB)
4016 const {
4017 for (auto MII = BB->instr_begin(), End = BB->instr_end(); MII != End; MII++) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004018 MachineInstr &MI = *MII;
4019 if (MI.isDebugValue())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004020 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004021 return &MI;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004022 }
4023 return nullptr;
4024}
4025
4026
4027unsigned HexagonInstrInfo::getInstrTimingClassLatency(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004028 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004029 // Default to one cycle for no itinerary. However, an "empty" itinerary may
4030 // still have a MinLatency property, which getStageLatency checks.
4031 if (!ItinData)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004032 return getInstrLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004033
4034 // Get the latency embedded in the itinerary. If we're not using timing class
4035 // latencies or if we using BSB scheduling, then restrict the maximum latency
4036 // to 1 (that is, either 0 or 1).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004037 if (MI.isTransient())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004038 return 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004039 unsigned Latency = ItinData->getStageLatency(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004040 if (!EnableTimingClassLatency ||
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004041 MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>().
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004042 useBSBScheduling())
4043 if (Latency > 1)
4044 Latency = 1;
4045 return Latency;
4046}
4047
4048
4049// inverts the predication logic.
4050// p -> NotP
4051// NotP -> P
4052bool HexagonInstrInfo::getInvertedPredSense(
4053 SmallVectorImpl<MachineOperand> &Cond) const {
4054 if (Cond.empty())
4055 return false;
4056 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
4057 Cond[0].setImm(Opc);
4058 return true;
4059}
4060
4061
4062unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
4063 int InvPredOpcode;
4064 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
4065 : Hexagon::getTruePredOpcode(Opc);
4066 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
4067 return InvPredOpcode;
4068
4069 llvm_unreachable("Unexpected predicated instruction");
4070}
4071
4072
4073// Returns the max value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004074int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
4075 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004076 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4077 & HexagonII::ExtentSignedMask;
4078 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4079 & HexagonII::ExtentBitsMask;
4080
4081 if (isSigned) // if value is signed
4082 return ~(-1U << (bits - 1));
4083 else
4084 return ~(-1U << bits);
4085}
4086
4087
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004088unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
4089 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004090 return (F >> HexagonII::MemAccessSizePos) & HexagonII::MemAccesSizeMask;
4091}
4092
4093
4094// Returns the min value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004095int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
4096 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004097 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4098 & HexagonII::ExtentSignedMask;
4099 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4100 & HexagonII::ExtentBitsMask;
4101
4102 if (isSigned) // if value is signed
4103 return -1U << (bits - 1);
4104 else
4105 return 0;
4106}
4107
4108
4109// Returns opcode of the non-extended equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004110short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00004111 // Check if the instruction has a register form that uses register in place
4112 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004113 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004114 if (NonExtOpcode >= 0)
4115 return NonExtOpcode;
4116
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004117 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00004118 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00004119 switch (getAddrMode(MI)) {
4120 case HexagonII::Absolute :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004121 return Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004122 case HexagonII::BaseImmOffset :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004123 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004124 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004125 return Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004126
Jyotsna Verma84256432013-03-01 17:37:13 +00004127 default:
4128 return -1;
4129 }
4130 }
4131 return -1;
4132}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00004133
Brendon Cahoondf43e682015-05-08 16:16:29 +00004134
Ahmed Bougachac88bf542015-06-11 19:30:37 +00004135bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004136 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00004137 if (Cond.empty())
4138 return false;
4139 assert(Cond.size() == 2);
4140 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00004141 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
4142 return false;
Brendon Cahoondf43e682015-05-08 16:16:29 +00004143 }
4144 PredReg = Cond[1].getReg();
4145 PredRegPos = 1;
4146 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4147 PredRegFlags = 0;
4148 if (Cond[1].isImplicit())
4149 PredRegFlags = RegState::Implicit;
4150 if (Cond[1].isUndef())
4151 PredRegFlags |= RegState::Undef;
4152 return true;
4153}
4154
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004155
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004156short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
4157 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004158}
4159
4160
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004161short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
4162 return Hexagon::getRegForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004163}
4164
4165
4166// Return the number of bytes required to encode the instruction.
4167// Hexagon instructions are fixed length, 4 bytes, unless they
4168// use a constant extender, which requires another 4 bytes.
4169// For debug instructions and prolog labels, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004170unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
4171 if (MI.isDebugValue() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004172 return 0;
4173
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004174 unsigned Size = MI.getDesc().getSize();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004175 if (!Size)
4176 // Assume the default insn size in case it cannot be determined
4177 // for whatever reason.
4178 Size = HEXAGON_INSTR_SIZE;
4179
4180 if (isConstExtended(MI) || isExtended(MI))
4181 Size += HEXAGON_INSTR_SIZE;
4182
4183 // Try and compute number of instructions in asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004184 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4185 const MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004186 const MachineFunction *MF = MBB.getParent();
4187 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4188
4189 // Count the number of register definitions to find the asm string.
4190 unsigned NumDefs = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004191 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004192 ++NumDefs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004193 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004194
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004195 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004196 // Disassemble the AsmStr and approximate number of instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004197 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004198 Size = getInlineAsmLength(AsmStr, *MAI);
4199 }
4200
4201 return Size;
4202}
4203
4204
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004205uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
4206 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004207 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4208}
4209
4210
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004211unsigned HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
4212 const TargetSubtargetInfo &ST = MI.getParent()->getParent()->getSubtarget();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004213 const InstrItineraryData &II = *ST.getInstrItineraryData();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004214 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004215
4216 return IS.getUnits();
4217}
4218
4219
4220unsigned HexagonInstrInfo::getValidSubTargets(const unsigned Opcode) const {
4221 const uint64_t F = get(Opcode).TSFlags;
4222 return (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask;
4223}
4224
4225
4226// Calculate size of the basic block without debug instructions.
4227unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4228 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4229}
4230
4231
4232unsigned HexagonInstrInfo::nonDbgBundleSize(
4233 MachineBasicBlock::const_iterator BundleHead) const {
4234 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004235 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004236 // Skip the bundle header.
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00004237 return nonDbgMICount(++MII, getBundleEnd(*BundleHead));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004238}
4239
4240
4241/// immediateExtend - Changes the instruction in place to one using an immediate
4242/// extender.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004243void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004244 assert((isExtendable(MI)||isConstExtended(MI)) &&
4245 "Instruction must be extendable");
4246 // Find which operand is extendable.
4247 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004248 MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004249 // This needs to be something we understand.
4250 assert((MO.isMBB() || MO.isImm()) &&
4251 "Branch with unknown extendable field type");
4252 // Mark given operand as extended.
4253 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4254}
4255
4256
4257bool HexagonInstrInfo::invertAndChangeJumpTarget(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004258 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004259 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004260 << NewTarget->getNumber(); MI.dump(););
4261 assert(MI.isBranch());
4262 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4263 int TargetPos = MI.getNumOperands() - 1;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004264 // In general branch target is the last operand,
4265 // but some implicit defs added at the end might change it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004266 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004267 --TargetPos;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004268 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4269 MI.getOperand(TargetPos).setMBB(NewTarget);
4270 if (EnableBranchPrediction && isPredicatedNew(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004271 NewOpcode = reversePrediction(NewOpcode);
4272 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004273 MI.setDesc(get(NewOpcode));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004274 return true;
4275}
4276
4277
4278void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4279 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4280 MachineFunction::iterator A = MF.begin();
4281 MachineBasicBlock &B = *A;
4282 MachineBasicBlock::iterator I = B.begin();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004283 DebugLoc DL = I->getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004284 MachineInstr *NewMI;
4285
4286 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4287 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004288 NewMI = BuildMI(B, I, DL, get(insn));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004289 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
4290 " Class: " << NewMI->getDesc().getSchedClass());
4291 NewMI->eraseFromParent();
4292 }
4293 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4294}
4295
4296
4297// inverts the predication logic.
4298// p -> NotP
4299// NotP -> P
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004300bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
4301 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
4302 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004303 return true;
4304}
4305
4306
4307// Reverse the branch prediction.
4308unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4309 int PredRevOpcode = -1;
4310 if (isPredictedTaken(Opcode))
4311 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4312 else
4313 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4314 assert(PredRevOpcode > 0);
4315 return PredRevOpcode;
4316}
4317
4318
4319// TODO: Add more rigorous validation.
4320bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4321 const {
4322 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4323}
4324
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004325
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004326short HexagonInstrInfo::xformRegToImmOffset(const MachineInstr &MI) const {
4327 return Hexagon::xformRegToImmOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004328}