blob: d3c038a44cd828fde7fc98717d11aa2053cc6fa4 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonInstrInfo.cpp - Hexagon Instruction Information ------------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Hexagon implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000014#include "HexagonHazardRecognizer.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000015#include "HexagonInstrInfo.h"
Craig Topperb25fda92012-03-17 18:46:09 +000016#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000017#include "HexagonSubtarget.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000018#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SmallVector.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000020#include "llvm/CodeGen/DFAPacketizer.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000023#include "llvm/CodeGen/MachineMemOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000025#include "llvm/CodeGen/PseudoSourceValue.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000026#include "llvm/CodeGen/ScheduleDAG.h"
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000027#include "llvm/MC/MCAsmInfo.h"
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +000028#include "llvm/Support/CommandLine.h"
Jyotsna Verma5ed51812013-05-01 21:37:34 +000029#include "llvm/Support/Debug.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000030#include "llvm/Support/MathExtras.h"
Reid Kleckner1c76f1552013-05-03 00:54:56 +000031#include "llvm/Support/raw_ostream.h"
Krzysztof Parzyszekaa935752015-11-24 15:11:13 +000032#include <cctype>
Tony Linthicum1213a7a2011-12-12 21:14:40 +000033
Tony Linthicum1213a7a2011-12-12 21:14:40 +000034using namespace llvm;
35
Chandler Carruthe96dd892014-04-21 22:55:11 +000036#define DEBUG_TYPE "hexagon-instrinfo"
37
Chandler Carruthd174b722014-04-22 02:03:14 +000038#define GET_INSTRINFO_CTOR_DTOR
39#define GET_INSTRMAP_INFO
40#include "HexagonGenInstrInfo.inc"
41#include "HexagonGenDFAPacketizer.inc"
42
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000043cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000044 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
45 "packetization boundary."));
46
47static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
48 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
49
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000050static cl::opt<bool> DisableNVSchedule("disable-hexagon-nv-schedule",
51 cl::Hidden, cl::ZeroOrMore, cl::init(false),
52 cl::desc("Disable schedule adjustment for new value stores."));
53
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000054static cl::opt<bool> EnableTimingClassLatency(
55 "enable-timing-class-latency", cl::Hidden, cl::init(false),
56 cl::desc("Enable timing class latency"));
57
58static cl::opt<bool> EnableALUForwarding(
59 "enable-alu-forwarding", cl::Hidden, cl::init(true),
60 cl::desc("Enable vec alu forwarding"));
61
62static cl::opt<bool> EnableACCForwarding(
63 "enable-acc-forwarding", cl::Hidden, cl::init(true),
64 cl::desc("Enable vec acc forwarding"));
65
66static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
67 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
68
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000069static cl::opt<bool> UseDFAHazardRec("dfa-hazard-rec",
70 cl::init(true), cl::Hidden, cl::ZeroOrMore,
71 cl::desc("Use the DFA based hazard recognizer."));
72
Tony Linthicum1213a7a2011-12-12 21:14:40 +000073///
74/// Constants for Hexagon instructions.
75///
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +000076const int Hexagon_MEMV_OFFSET_MAX_128B = 896; // #s4: -8*128...7*128
77const int Hexagon_MEMV_OFFSET_MIN_128B = -1024; // #s4
78const int Hexagon_MEMV_OFFSET_MAX = 448; // #s4: -8*64...7*64
79const int Hexagon_MEMV_OFFSET_MIN = -512; // #s4
Tony Linthicum1213a7a2011-12-12 21:14:40 +000080const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000081const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000082const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000083const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000084const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000085const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000086const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000087const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000088const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000089const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000090const int Hexagon_MEMD_AUTOINC_MAX = 56;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000091const int Hexagon_MEMD_AUTOINC_MIN = -64;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000092const int Hexagon_MEMW_AUTOINC_MAX = 28;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000093const int Hexagon_MEMW_AUTOINC_MIN = -32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000094const int Hexagon_MEMH_AUTOINC_MAX = 14;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000095const int Hexagon_MEMH_AUTOINC_MIN = -16;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000096const int Hexagon_MEMB_AUTOINC_MAX = 7;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000097const int Hexagon_MEMB_AUTOINC_MIN = -8;
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +000098const int Hexagon_MEMV_AUTOINC_MAX = 192; // #s3
99const int Hexagon_MEMV_AUTOINC_MIN = -256; // #s3
100const int Hexagon_MEMV_AUTOINC_MAX_128B = 384; // #s3
101const int Hexagon_MEMV_AUTOINC_MIN_128B = -512; // #s3
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000102
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000103// Pin the vtable to this file.
104void HexagonInstrInfo::anchor() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000105
106HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
Eric Christopherc4d31402015-03-10 23:45:55 +0000107 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000108 RI() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000109
110
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000111static bool isIntRegForSubInst(unsigned Reg) {
112 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
113 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000114}
115
116
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000117static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
118 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::subreg_loreg)) &&
119 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::subreg_hireg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000120}
121
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000122
123/// Calculate number of instructions excluding the debug instructions.
124static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
125 MachineBasicBlock::const_instr_iterator MIE) {
126 unsigned Count = 0;
127 for (; MIB != MIE; ++MIB) {
128 if (!MIB->isDebugValue())
129 ++Count;
130 }
131 return Count;
132}
133
134
135/// Find the hardware loop instruction used to set-up the specified loop.
136/// On Hexagon, we have two instructions used to set-up the hardware loop
137/// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
138/// to indicate the end of a loop.
139static MachineInstr *findLoopInstr(MachineBasicBlock *BB, int EndLoopOp,
140 SmallPtrSet<MachineBasicBlock *, 8> &Visited) {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000141 int LOOPi;
142 int LOOPr;
143 if (EndLoopOp == Hexagon::ENDLOOP0) {
144 LOOPi = Hexagon::J2_loop0i;
145 LOOPr = Hexagon::J2_loop0r;
146 } else { // EndLoopOp == Hexagon::EndLOOP1
147 LOOPi = Hexagon::J2_loop1i;
148 LOOPr = Hexagon::J2_loop1r;
149 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000150
Brendon Cahoondf43e682015-05-08 16:16:29 +0000151 // The loop set-up instruction will be in a predecessor block
152 for (MachineBasicBlock::pred_iterator PB = BB->pred_begin(),
153 PE = BB->pred_end(); PB != PE; ++PB) {
154 // If this has been visited, already skip it.
155 if (!Visited.insert(*PB).second)
156 continue;
157 if (*PB == BB)
158 continue;
159 for (MachineBasicBlock::reverse_instr_iterator I = (*PB)->instr_rbegin(),
160 E = (*PB)->instr_rend(); I != E; ++I) {
161 int Opc = I->getOpcode();
162 if (Opc == LOOPi || Opc == LOOPr)
163 return &*I;
164 // We've reached a different loop, which means the loop0 has been removed.
165 if (Opc == EndLoopOp)
166 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000167 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000168 // Check the predecessors for the LOOP instruction.
169 MachineInstr *loop = findLoopInstr(*PB, EndLoopOp, Visited);
170 if (loop)
171 return loop;
172 }
173 return 0;
174}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000175
Brendon Cahoondf43e682015-05-08 16:16:29 +0000176
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000177/// Gather register def/uses from MI.
178/// This treats possible (predicated) defs as actually happening ones
179/// (conservatively).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000180static inline void parseOperands(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000181 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
182 Defs.clear();
183 Uses.clear();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000184
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000185 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
186 const MachineOperand &MO = MI.getOperand(i);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000187
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000188 if (!MO.isReg())
189 continue;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000190
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000191 unsigned Reg = MO.getReg();
192 if (!Reg)
193 continue;
194
195 if (MO.isUse())
196 Uses.push_back(MO.getReg());
197
198 if (MO.isDef())
199 Defs.push_back(MO.getReg());
200 }
201}
202
203
204// Position dependent, so check twice for swap.
205static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
206 switch (Ga) {
207 case HexagonII::HSIG_None:
208 default:
209 return false;
210 case HexagonII::HSIG_L1:
211 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
212 case HexagonII::HSIG_L2:
213 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
214 Gb == HexagonII::HSIG_A);
215 case HexagonII::HSIG_S1:
216 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
217 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
218 case HexagonII::HSIG_S2:
219 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
220 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
221 Gb == HexagonII::HSIG_A);
222 case HexagonII::HSIG_A:
223 return (Gb == HexagonII::HSIG_A);
224 case HexagonII::HSIG_Compound:
225 return (Gb == HexagonII::HSIG_Compound);
226 }
227 return false;
228}
229
230
231
232/// isLoadFromStackSlot - If the specified machine instruction is a direct
233/// load from a stack slot, return the virtual or physical register number of
234/// the destination along with the FrameIndex of the loaded stack slot. If
235/// not, return 0. This predicate must return 0 if the instruction has
236/// any side effects other than loading from the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000237unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000238 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000239 switch (MI.getOpcode()) {
240 default:
241 break;
242 case Hexagon::L2_loadrb_io:
243 case Hexagon::L2_loadrub_io:
244 case Hexagon::L2_loadrh_io:
245 case Hexagon::L2_loadruh_io:
246 case Hexagon::L2_loadri_io:
247 case Hexagon::L2_loadrd_io:
248 case Hexagon::V6_vL32b_ai:
249 case Hexagon::V6_vL32b_ai_128B:
250 case Hexagon::V6_vL32Ub_ai:
251 case Hexagon::V6_vL32Ub_ai_128B:
252 case Hexagon::LDriw_pred:
253 case Hexagon::LDriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000254 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000255 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000256 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000257 case Hexagon::PS_vloadrw_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000258 const MachineOperand OpFI = MI.getOperand(1);
259 if (!OpFI.isFI())
260 return 0;
261 const MachineOperand OpOff = MI.getOperand(2);
262 if (!OpOff.isImm() || OpOff.getImm() != 0)
263 return 0;
264 FrameIndex = OpFI.getIndex();
265 return MI.getOperand(0).getReg();
266 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000267
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000268 case Hexagon::L2_ploadrbt_io:
269 case Hexagon::L2_ploadrbf_io:
270 case Hexagon::L2_ploadrubt_io:
271 case Hexagon::L2_ploadrubf_io:
272 case Hexagon::L2_ploadrht_io:
273 case Hexagon::L2_ploadrhf_io:
274 case Hexagon::L2_ploadruht_io:
275 case Hexagon::L2_ploadruhf_io:
276 case Hexagon::L2_ploadrit_io:
277 case Hexagon::L2_ploadrif_io:
278 case Hexagon::L2_ploadrdt_io:
279 case Hexagon::L2_ploadrdf_io: {
280 const MachineOperand OpFI = MI.getOperand(2);
281 if (!OpFI.isFI())
282 return 0;
283 const MachineOperand OpOff = MI.getOperand(3);
284 if (!OpOff.isImm() || OpOff.getImm() != 0)
285 return 0;
286 FrameIndex = OpFI.getIndex();
287 return MI.getOperand(0).getReg();
288 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000289 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000290
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000291 return 0;
292}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000293
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000294
295/// isStoreToStackSlot - If the specified machine instruction is a direct
296/// store to a stack slot, return the virtual or physical register number of
297/// the source reg along with the FrameIndex of the loaded stack slot. If
298/// not, return 0. This predicate must return 0 if the instruction has
299/// any side effects other than storing to the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000300unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000301 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000302 switch (MI.getOpcode()) {
303 default:
304 break;
305 case Hexagon::S2_storerb_io:
306 case Hexagon::S2_storerh_io:
307 case Hexagon::S2_storeri_io:
308 case Hexagon::S2_storerd_io:
309 case Hexagon::V6_vS32b_ai:
310 case Hexagon::V6_vS32b_ai_128B:
311 case Hexagon::V6_vS32Ub_ai:
312 case Hexagon::V6_vS32Ub_ai_128B:
313 case Hexagon::STriw_pred:
314 case Hexagon::STriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000315 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000316 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000317 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000318 case Hexagon::PS_vstorerw_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000319 const MachineOperand &OpFI = MI.getOperand(0);
320 if (!OpFI.isFI())
321 return 0;
322 const MachineOperand &OpOff = MI.getOperand(1);
323 if (!OpOff.isImm() || OpOff.getImm() != 0)
324 return 0;
325 FrameIndex = OpFI.getIndex();
326 return MI.getOperand(2).getReg();
327 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000328
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000329 case Hexagon::S2_pstorerbt_io:
330 case Hexagon::S2_pstorerbf_io:
331 case Hexagon::S2_pstorerht_io:
332 case Hexagon::S2_pstorerhf_io:
333 case Hexagon::S2_pstorerit_io:
334 case Hexagon::S2_pstorerif_io:
335 case Hexagon::S2_pstorerdt_io:
336 case Hexagon::S2_pstorerdf_io: {
337 const MachineOperand &OpFI = MI.getOperand(1);
338 if (!OpFI.isFI())
339 return 0;
340 const MachineOperand &OpOff = MI.getOperand(2);
341 if (!OpOff.isImm() || OpOff.getImm() != 0)
342 return 0;
343 FrameIndex = OpFI.getIndex();
344 return MI.getOperand(3).getReg();
345 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000346 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000347
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000348 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000349}
350
351
Brendon Cahoondf43e682015-05-08 16:16:29 +0000352/// This function can analyze one/two way branching only and should (mostly) be
353/// called by target independent side.
354/// First entry is always the opcode of the branching instruction, except when
355/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
356/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
357/// e.g. Jump_c p will have
358/// Cond[0] = Jump_c
359/// Cond[1] = p
360/// HW-loop ENDLOOP:
361/// Cond[0] = ENDLOOP
362/// Cond[1] = MBB
363/// New value jump:
364/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
365/// Cond[1] = R
366/// Cond[2] = Imm
Brendon Cahoondf43e682015-05-08 16:16:29 +0000367///
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000368bool HexagonInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000369 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000370 MachineBasicBlock *&FBB,
371 SmallVectorImpl<MachineOperand> &Cond,
372 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000373 TBB = nullptr;
374 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000375 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000376
377 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000378 MachineBasicBlock::instr_iterator I = MBB.instr_end();
379 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000380 return false;
381
382 // A basic block may looks like this:
383 //
384 // [ insn
385 // EH_LABEL
386 // insn
387 // insn
388 // insn
389 // EH_LABEL
390 // insn ]
391 //
392 // It has two succs but does not have a terminator
393 // Don't know how to handle it.
394 do {
395 --I;
396 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000397 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000398 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000399 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000400
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000401 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000402 --I;
403
404 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000405 if (I == MBB.instr_begin())
406 return false;
407 --I;
408 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000409
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000410 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
411 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000412 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000413 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000414 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
415 DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
416 I->eraseFromParent();
417 I = MBB.instr_end();
418 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000419 return false;
420 --I;
421 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000422 if (!isUnpredicatedTerminator(*I))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000423 return false;
424
425 // Get the last instruction in the block.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000426 MachineInstr *LastInst = &*I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000427 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000428 // Find one more terminator if present.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000429 for (;;) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000430 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000431 if (!SecondLastInst)
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000432 SecondLastInst = &*I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000433 else
434 // This is a third branch.
435 return true;
436 }
437 if (I == MBB.instr_begin())
438 break;
439 --I;
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000440 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000441
442 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000443 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
444 // If the branch target is not a basic block, it could be a tail call.
445 // (It is, if the target is a function.)
446 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
447 return true;
448 if (SecLastOpcode == Hexagon::J2_jump &&
449 !SecondLastInst->getOperand(0).isMBB())
450 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000451
452 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000453 bool LastOpcodeHasNVJump = isNewValueJump(*LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000454
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000455 if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
456 return true;
457
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000458 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000459 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000460 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000461 TBB = LastInst->getOperand(0).getMBB();
462 return false;
463 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000464 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000465 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000466 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000467 Cond.push_back(LastInst->getOperand(0));
468 return false;
469 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000470 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000471 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000472 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000473 Cond.push_back(LastInst->getOperand(0));
474 return false;
475 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000476 // Only supporting rr/ri versions of new-value jumps.
477 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
478 TBB = LastInst->getOperand(2).getMBB();
479 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
480 Cond.push_back(LastInst->getOperand(0));
481 Cond.push_back(LastInst->getOperand(1));
482 return false;
483 }
484 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
485 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000486 // Otherwise, don't know what this is.
487 return true;
488 }
489
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000490 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000491 bool SecLastOpcodeHasNVJump = isNewValueJump(*SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000492 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000493 if (!SecondLastInst->getOperand(1).isMBB())
494 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000495 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000496 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000497 Cond.push_back(SecondLastInst->getOperand(0));
498 FBB = LastInst->getOperand(0).getMBB();
499 return false;
500 }
501
Brendon Cahoondf43e682015-05-08 16:16:29 +0000502 // Only supporting rr/ri versions of new-value jumps.
503 if (SecLastOpcodeHasNVJump &&
504 (SecondLastInst->getNumExplicitOperands() == 3) &&
505 (LastOpcode == Hexagon::J2_jump)) {
506 TBB = SecondLastInst->getOperand(2).getMBB();
507 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
508 Cond.push_back(SecondLastInst->getOperand(0));
509 Cond.push_back(SecondLastInst->getOperand(1));
510 FBB = LastInst->getOperand(0).getMBB();
511 return false;
512 }
513
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000514 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
515 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000516 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000517 TBB = SecondLastInst->getOperand(0).getMBB();
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000518 I = LastInst->getIterator();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000519 if (AllowModify)
520 I->eraseFromParent();
521 return false;
522 }
523
Brendon Cahoondf43e682015-05-08 16:16:29 +0000524 // If the block ends with an ENDLOOP, and J2_jump, handle it.
525 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000526 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000527 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000528 Cond.push_back(SecondLastInst->getOperand(0));
529 FBB = LastInst->getOperand(0).getMBB();
530 return false;
531 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000532 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
533 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000534 // Otherwise, can't handle this.
535 return true;
536}
537
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000538
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000539unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000540 DEBUG(dbgs() << "\nRemoving branches out of BB#" << MBB.getNumber());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000541 MachineBasicBlock::iterator I = MBB.end();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000542 unsigned Count = 0;
543 while (I != MBB.begin()) {
544 --I;
545 if (I->isDebugValue())
546 continue;
547 // Only removing branches from end of MBB.
548 if (!I->isBranch())
549 return Count;
550 if (Count && (I->getOpcode() == Hexagon::J2_jump))
551 llvm_unreachable("Malformed basic block: unconditional branch not last");
552 MBB.erase(&MBB.back());
553 I = MBB.end();
554 ++Count;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000555 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000556 return Count;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000557}
558
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000559unsigned HexagonInstrInfo::InsertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000560 MachineBasicBlock *TBB,
561 MachineBasicBlock *FBB,
562 ArrayRef<MachineOperand> Cond,
563 const DebugLoc &DL) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000564 unsigned BOpc = Hexagon::J2_jump;
565 unsigned BccOpc = Hexagon::J2_jumpt;
566 assert(validateBranchCond(Cond) && "Invalid branching condition");
567 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
568
569 // Check if ReverseBranchCondition has asked to reverse this branch
570 // If we want to reverse the branch an odd number of times, we want
571 // J2_jumpf.
572 if (!Cond.empty() && Cond[0].isImm())
573 BccOpc = Cond[0].getImm();
574
575 if (!FBB) {
576 if (Cond.empty()) {
577 // Due to a bug in TailMerging/CFG Optimization, we need to add a
578 // special case handling of a predicated jump followed by an
579 // unconditional jump. If not, Tail Merging and CFG Optimization go
580 // into an infinite loop.
581 MachineBasicBlock *NewTBB, *NewFBB;
582 SmallVector<MachineOperand, 4> Cond;
Duncan P. N. Exon Smith25b132e2016-07-08 18:26:20 +0000583 auto Term = MBB.getFirstTerminator();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000584 if (Term != MBB.end() && isPredicated(*Term) &&
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000585 !analyzeBranch(MBB, NewTBB, NewFBB, Cond, false)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000586 MachineBasicBlock *NextBB = &*++MBB.getIterator();
587 if (NewTBB == NextBB) {
588 ReverseBranchCondition(Cond);
589 RemoveBranch(MBB);
590 return InsertBranch(MBB, TBB, nullptr, Cond, DL);
591 }
592 }
593 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
594 } else if (isEndLoopN(Cond[0].getImm())) {
595 int EndLoopOp = Cond[0].getImm();
596 assert(Cond[1].isMBB());
597 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
598 // Check for it, and change the BB target if needed.
599 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
600 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
601 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
602 Loop->getOperand(0).setMBB(TBB);
603 // Add the ENDLOOP after the finding the LOOP0.
604 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
605 } else if (isNewValueJump(Cond[0].getImm())) {
606 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
607 // New value jump
608 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
609 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
610 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
611 DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
612 if (Cond[2].isReg()) {
613 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
614 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
615 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
616 } else if(Cond[2].isImm()) {
617 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
618 addImm(Cond[2].getImm()).addMBB(TBB);
619 } else
620 llvm_unreachable("Invalid condition for branching");
621 } else {
622 assert((Cond.size() == 2) && "Malformed cond vector");
623 const MachineOperand &RO = Cond[1];
624 unsigned Flags = getUndefRegState(RO.isUndef());
625 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
626 }
627 return 1;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000628 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000629 assert((!Cond.empty()) &&
630 "Cond. cannot be empty when multiple branchings are required");
631 assert((!isNewValueJump(Cond[0].getImm())) &&
632 "NV-jump cannot be inserted with another branch");
633 // Special case for hardware loops. The condition is a basic block.
634 if (isEndLoopN(Cond[0].getImm())) {
635 int EndLoopOp = Cond[0].getImm();
636 assert(Cond[1].isMBB());
637 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
638 // Check for it, and change the BB target if needed.
639 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
640 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
641 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
642 Loop->getOperand(0).setMBB(TBB);
643 // Add the ENDLOOP after the finding the LOOP0.
644 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
645 } else {
646 const MachineOperand &RO = Cond[1];
647 unsigned Flags = getUndefRegState(RO.isUndef());
648 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000649 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000650 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000651
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000652 return 2;
653}
654
Brendon Cahoon254f8892016-07-29 16:44:44 +0000655/// Analyze the loop code to find the loop induction variable and compare used
656/// to compute the number of iterations. Currently, we analyze loop that are
657/// controlled using hardware loops. In this case, the induction variable
658/// instruction is null. For all other cases, this function returns true, which
659/// means we're unable to analyze it.
660bool HexagonInstrInfo::analyzeLoop(MachineLoop &L,
661 MachineInstr *&IndVarInst,
662 MachineInstr *&CmpInst) const {
663
664 MachineBasicBlock *LoopEnd = L.getBottomBlock();
665 MachineBasicBlock::iterator I = LoopEnd->getFirstTerminator();
666 // We really "analyze" only hardware loops right now.
667 if (I != LoopEnd->end() && isEndLoopN(I->getOpcode())) {
668 IndVarInst = nullptr;
669 CmpInst = &*I;
670 return false;
671 }
672 return true;
673}
674
675/// Generate code to reduce the loop iteration by one and check if the loop is
676/// finished. Return the value/register of the new loop count. this function
677/// assumes the nth iteration is peeled first.
678unsigned HexagonInstrInfo::reduceLoopCount(MachineBasicBlock &MBB,
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000679 MachineInstr *IndVar, MachineInstr &Cmp,
Brendon Cahoon254f8892016-07-29 16:44:44 +0000680 SmallVectorImpl<MachineOperand> &Cond,
681 SmallVectorImpl<MachineInstr *> &PrevInsts,
682 unsigned Iter, unsigned MaxIter) const {
683 // We expect a hardware loop currently. This means that IndVar is set
684 // to null, and the compare is the ENDLOOP instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000685 assert((!IndVar) && isEndLoopN(Cmp.getOpcode())
Brendon Cahoon254f8892016-07-29 16:44:44 +0000686 && "Expecting a hardware loop");
687 MachineFunction *MF = MBB.getParent();
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000688 DebugLoc DL = Cmp.getDebugLoc();
Brendon Cahoon254f8892016-07-29 16:44:44 +0000689 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000690 MachineInstr *Loop = findLoopInstr(&MBB, Cmp.getOpcode(), VisitedBBs);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000691 if (!Loop)
692 return 0;
693 // If the loop trip count is a compile-time value, then just change the
694 // value.
695 if (Loop->getOpcode() == Hexagon::J2_loop0i ||
696 Loop->getOpcode() == Hexagon::J2_loop1i) {
697 int64_t Offset = Loop->getOperand(1).getImm();
698 if (Offset <= 1)
699 Loop->eraseFromParent();
700 else
701 Loop->getOperand(1).setImm(Offset - 1);
702 return Offset - 1;
703 }
704 // The loop trip count is a run-time value. We generate code to subtract
705 // one from the trip count, and update the loop instruction.
706 assert(Loop->getOpcode() == Hexagon::J2_loop0r && "Unexpected instruction");
707 unsigned LoopCount = Loop->getOperand(1).getReg();
708 // Check if we're done with the loop.
709 unsigned LoopEnd = createVR(MF, MVT::i1);
710 MachineInstr *NewCmp = BuildMI(&MBB, DL, get(Hexagon::C2_cmpgtui), LoopEnd).
711 addReg(LoopCount).addImm(1);
712 unsigned NewLoopCount = createVR(MF, MVT::i32);
713 MachineInstr *NewAdd = BuildMI(&MBB, DL, get(Hexagon::A2_addi), NewLoopCount).
714 addReg(LoopCount).addImm(-1);
715 // Update the previously generated instructions with the new loop counter.
716 for (SmallVectorImpl<MachineInstr *>::iterator I = PrevInsts.begin(),
717 E = PrevInsts.end(); I != E; ++I)
718 (*I)->substituteRegister(LoopCount, NewLoopCount, 0, getRegisterInfo());
719 PrevInsts.clear();
720 PrevInsts.push_back(NewCmp);
721 PrevInsts.push_back(NewAdd);
722 // Insert the new loop instruction if this is the last time the loop is
723 // decremented.
724 if (Iter == MaxIter)
725 BuildMI(&MBB, DL, get(Hexagon::J2_loop0r)).
726 addMBB(Loop->getOperand(0).getMBB()).addReg(NewLoopCount);
727 // Delete the old loop instruction.
728 if (Iter == 0)
729 Loop->eraseFromParent();
730 Cond.push_back(MachineOperand::CreateImm(Hexagon::J2_jumpf));
731 Cond.push_back(NewCmp->getOperand(0));
732 return NewLoopCount;
733}
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000734
735bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
736 unsigned NumCycles, unsigned ExtraPredCycles,
737 BranchProbability Probability) const {
738 return nonDbgBBSize(&MBB) <= 3;
739}
740
741
742bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
743 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
744 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
745 const {
746 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
747}
748
749
750bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
751 unsigned NumInstrs, BranchProbability Probability) const {
752 return NumInstrs <= 4;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000753}
754
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000755void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000756 MachineBasicBlock::iterator I,
757 const DebugLoc &DL, unsigned DestReg,
758 unsigned SrcReg, bool KillSrc) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000759 auto &HRI = getRegisterInfo();
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000760 unsigned KillFlag = getKillRegState(KillSrc);
761
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000762 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000763 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000764 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000765 return;
766 }
767 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000768 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg)
769 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000770 return;
771 }
772 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
773 // Map Pd = Ps to Pd = or(Ps, Ps).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000774 BuildMI(MBB, I, DL, get(Hexagon::C2_or), DestReg)
775 .addReg(SrcReg).addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000776 return;
777 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000778 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000779 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000780 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
781 .addReg(SrcReg, KillFlag);
782 return;
783 }
784 if (Hexagon::IntRegsRegClass.contains(DestReg) &&
785 Hexagon::CtrRegsRegClass.contains(SrcReg)) {
786 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrcrr), DestReg)
787 .addReg(SrcReg, KillFlag);
788 return;
789 }
790 if (Hexagon::ModRegsRegClass.contains(DestReg) &&
791 Hexagon::IntRegsRegClass.contains(SrcReg)) {
792 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
793 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000794 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000795 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000796 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
797 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000798 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
799 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000800 return;
801 }
802 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
803 Hexagon::PredRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000804 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg)
805 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000806 return;
807 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000808 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
809 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000810 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
811 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000812 return;
813 }
814 if (Hexagon::VectorRegsRegClass.contains(SrcReg, DestReg)) {
815 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000816 addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000817 return;
818 }
819 if (Hexagon::VecDblRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000820 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
821 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg), KillFlag)
822 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg), KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000823 return;
824 }
825 if (Hexagon::VecPredRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000826 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
827 .addReg(SrcReg)
828 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000829 return;
830 }
831 if (Hexagon::VecPredRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000832 Hexagon::VectorRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000833 llvm_unreachable("Unimplemented pred to vec");
834 return;
835 }
836 if (Hexagon::VecPredRegsRegClass.contains(DestReg) &&
837 Hexagon::VectorRegsRegClass.contains(SrcReg)) {
838 llvm_unreachable("Unimplemented vec to pred");
839 return;
840 }
841 if (Hexagon::VecPredRegs128BRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000842 unsigned DstHi = HRI.getSubReg(DestReg, Hexagon::subreg_hireg);
843 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DstHi)
844 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg), KillFlag);
845 unsigned DstLo = HRI.getSubReg(DestReg, Hexagon::subreg_loreg);
846 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DstLo)
847 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg), KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000848 return;
849 }
Sirish Pande30804c22012-02-15 18:52:27 +0000850
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000851#ifndef NDEBUG
852 // Show the invalid registers to ease debugging.
853 dbgs() << "Invalid registers for copy in BB#" << MBB.getNumber()
854 << ": " << PrintReg(DestReg, &HRI)
855 << " = " << PrintReg(SrcReg, &HRI) << '\n';
856#endif
Sirish Pande30804c22012-02-15 18:52:27 +0000857 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000858}
859
860
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000861void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
862 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
863 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000864 DebugLoc DL = MBB.findDebugLoc(I);
865 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000866 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000867 unsigned Align = MFI.getObjectAlignment(FI);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000868 unsigned KillFlag = getKillRegState(isKill);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000869
Alex Lorenze40c8a22015-08-11 23:09:45 +0000870 MachineMemOperand *MMO = MF.getMachineMemOperand(
871 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
872 MFI.getObjectSize(FI), Align);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000873
Craig Topperc7242e02012-04-20 07:30:17 +0000874 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000875 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000876 .addFrameIndex(FI).addImm(0)
877 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000878 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000879 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000880 .addFrameIndex(FI).addImm(0)
881 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000882 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000883 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000884 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000885 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000886 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
887 BuildMI(MBB, I, DL, get(Hexagon::STriw_mod))
888 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000889 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
890 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000891 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai_128B))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000892 .addFrameIndex(FI).addImm(0)
893 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
894 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000895 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000896 .addFrameIndex(FI).addImm(0)
897 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
898 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000899 unsigned Opc = Align < 128 ? Hexagon::V6_vS32Ub_ai_128B
900 : Hexagon::V6_vS32b_ai_128B;
901 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000902 .addFrameIndex(FI).addImm(0)
903 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
904 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000905 unsigned Opc = Align < 64 ? Hexagon::V6_vS32Ub_ai
906 : Hexagon::V6_vS32b_ai;
907 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000908 .addFrameIndex(FI).addImm(0)
909 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
910 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000911 unsigned Opc = Align < 64 ? Hexagon::PS_vstorerwu_ai
912 : Hexagon::PS_vstorerw_ai;
913 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000914 .addFrameIndex(FI).addImm(0)
915 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
916 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000917 unsigned Opc = Align < 128 ? Hexagon::PS_vstorerwu_ai_128B
918 : Hexagon::PS_vstorerw_ai_128B;
919 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000920 .addFrameIndex(FI).addImm(0)
921 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000922 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000923 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000924 }
925}
926
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000927void HexagonInstrInfo::loadRegFromStackSlot(
928 MachineBasicBlock &MBB, MachineBasicBlock::iterator I, unsigned DestReg,
929 int FI, const TargetRegisterClass *RC,
930 const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000931 DebugLoc DL = MBB.findDebugLoc(I);
932 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000933 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000934 unsigned Align = MFI.getObjectAlignment(FI);
935
Alex Lorenze40c8a22015-08-11 23:09:45 +0000936 MachineMemOperand *MMO = MF.getMachineMemOperand(
937 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
938 MFI.getObjectSize(FI), Align);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000939
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000940 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000941 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000942 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000943 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000944 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000945 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000946 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000947 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000948 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
949 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
950 BuildMI(MBB, I, DL, get(Hexagon::LDriw_mod), DestReg)
951 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000952 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000953 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai_128B), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000954 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
955 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000956 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000957 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
958 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000959 unsigned Opc = Align < 128 ? Hexagon::PS_vloadrwu_ai_128B
960 : Hexagon::PS_vloadrw_ai_128B;
961 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000962 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
963 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000964 unsigned Opc = Align < 128 ? Hexagon::V6_vL32Ub_ai_128B
965 : Hexagon::V6_vL32b_ai_128B;
966 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000967 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
968 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000969 unsigned Opc = Align < 64 ? Hexagon::V6_vL32Ub_ai
970 : Hexagon::V6_vL32b_ai;
971 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000972 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
973 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000974 unsigned Opc = Align < 64 ? Hexagon::PS_vloadrwu_ai
975 : Hexagon::PS_vloadrw_ai;
976 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000977 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000978 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000979 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000980 }
981}
982
983
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000984/// expandPostRAPseudo - This function is called for all pseudo instructions
985/// that remain after register allocation. Many pseudo instructions are
986/// created to help register allocation. This is the place to convert them
987/// into real instructions. The target can edit MI in place, or it can insert
988/// new instructions and erase MI. The function should return true if
989/// anything was changed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000990bool HexagonInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000991 const HexagonRegisterInfo &HRI = getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000992 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
993 MachineBasicBlock &MBB = *MI.getParent();
994 DebugLoc DL = MI.getDebugLoc();
995 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +0000996 const unsigned VecOffset = 1;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000997
998 switch (Opc) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000999 case TargetOpcode::COPY: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001000 MachineOperand &MD = MI.getOperand(0);
1001 MachineOperand &MS = MI.getOperand(1);
1002 MachineBasicBlock::iterator MBBI = MI.getIterator();
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001003 if (MD.getReg() != MS.getReg() && !MS.isUndef()) {
1004 copyPhysReg(MBB, MI, DL, MD.getReg(), MS.getReg(), MS.isKill());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001005 std::prev(MBBI)->copyImplicitOps(*MBB.getParent(), MI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001006 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001007 MBB.erase(MBBI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001008 return true;
1009 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001010 case Hexagon::PS_aligna:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001011 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI.getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001012 .addReg(HRI.getFrameRegister())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001013 .addImm(-MI.getOperand(1).getImm());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001014 MBB.erase(MI);
1015 return true;
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001016 case Hexagon::V6_vassignp_128B:
1017 case Hexagon::V6_vassignp: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001018 unsigned SrcReg = MI.getOperand(1).getReg();
1019 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001020 unsigned Kill = getKillRegState(MI.getOperand(1).isKill());
1021 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcombine), DstReg)
1022 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg), Kill)
1023 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg), Kill);
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001024 MBB.erase(MI);
1025 return true;
1026 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001027 case Hexagon::V6_lo_128B:
1028 case Hexagon::V6_lo: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001029 unsigned SrcReg = MI.getOperand(1).getReg();
1030 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001031 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001032 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001033 MBB.erase(MI);
1034 MRI.clearKillFlags(SrcSubLo);
1035 return true;
1036 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001037 case Hexagon::V6_hi_128B:
1038 case Hexagon::V6_hi: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001039 unsigned SrcReg = MI.getOperand(1).getReg();
1040 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001041 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::subreg_hireg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001042 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001043 MBB.erase(MI);
1044 MRI.clearKillFlags(SrcSubHi);
1045 return true;
1046 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001047 case Hexagon::PS_vstorerw_ai:
1048 case Hexagon::PS_vstorerwu_ai:
1049 case Hexagon::PS_vstorerw_ai_128B:
1050 case Hexagon::PS_vstorerwu_ai_128B: {
1051 bool Is128B = (Opc == Hexagon::PS_vstorerw_ai_128B ||
1052 Opc == Hexagon::PS_vstorerwu_ai_128B);
1053 bool Aligned = (Opc == Hexagon::PS_vstorerw_ai ||
1054 Opc == Hexagon::PS_vstorerw_ai_128B);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001055 unsigned SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001056 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::subreg_hireg);
1057 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::subreg_loreg);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001058 unsigned NewOpc;
1059 if (Aligned)
1060 NewOpc = Is128B ? Hexagon::V6_vS32b_ai_128B
1061 : Hexagon::V6_vS32b_ai;
1062 else
1063 NewOpc = Is128B ? Hexagon::V6_vS32Ub_ai_128B
1064 : Hexagon::V6_vS32Ub_ai;
1065
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001066 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001067 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001068 BuildMI(MBB, MI, DL, get(NewOpc))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001069 .addOperand(MI.getOperand(0))
1070 .addImm(MI.getOperand(1).getImm())
1071 .addReg(SrcSubLo)
1072 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001073 MI1New->getOperand(0).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001074 BuildMI(MBB, MI, DL, get(NewOpc))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001075 .addOperand(MI.getOperand(0))
1076 // The Vectors are indexed in multiples of vector size.
1077 .addImm(MI.getOperand(1).getImm() + Offset)
1078 .addReg(SrcSubHi)
1079 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001080 MBB.erase(MI);
1081 return true;
1082 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001083 case Hexagon::PS_vloadrw_ai:
1084 case Hexagon::PS_vloadrwu_ai:
1085 case Hexagon::PS_vloadrw_ai_128B:
1086 case Hexagon::PS_vloadrwu_ai_128B: {
1087 bool Is128B = (Opc == Hexagon::PS_vloadrw_ai_128B ||
1088 Opc == Hexagon::PS_vloadrwu_ai_128B);
1089 bool Aligned = (Opc == Hexagon::PS_vloadrw_ai ||
1090 Opc == Hexagon::PS_vloadrw_ai_128B);
1091 unsigned NewOpc;
1092 if (Aligned)
1093 NewOpc = Is128B ? Hexagon::V6_vL32b_ai_128B
1094 : Hexagon::V6_vL32b_ai;
1095 else
1096 NewOpc = Is128B ? Hexagon::V6_vL32Ub_ai_128B
1097 : Hexagon::V6_vL32Ub_ai;
1098
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001099 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001100 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
1101 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001102 BuildMI(MBB, MI, DL, get(NewOpc),
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001103 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001104 .addOperand(MI.getOperand(1))
1105 .addImm(MI.getOperand(2).getImm());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001106 MI1New->getOperand(1).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001107 BuildMI(MBB, MI, DL, get(NewOpc),
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001108 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001109 .addOperand(MI.getOperand(1))
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001110 // The Vectors are indexed in multiples of vector size.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001111 .addImm(MI.getOperand(2).getImm() + Offset)
1112 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001113 MBB.erase(MI);
1114 return true;
1115 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001116 case Hexagon::PS_true: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001117 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001118 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1119 .addReg(Reg, RegState::Undef)
1120 .addReg(Reg, RegState::Undef);
1121 MBB.erase(MI);
1122 return true;
1123 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001124 case Hexagon::PS_false: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001125 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001126 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1127 .addReg(Reg, RegState::Undef)
1128 .addReg(Reg, RegState::Undef);
1129 MBB.erase(MI);
1130 return true;
1131 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001132 case Hexagon::PS_vmulw: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001133 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001134 unsigned DstReg = MI.getOperand(0).getReg();
1135 unsigned Src1Reg = MI.getOperand(1).getReg();
1136 unsigned Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001137 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
1138 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
1139 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
1140 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001141 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
1142 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1143 .addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001144 .addReg(Src2SubHi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001145 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
1146 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1147 .addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001148 .addReg(Src2SubLo);
1149 MBB.erase(MI);
1150 MRI.clearKillFlags(Src1SubHi);
1151 MRI.clearKillFlags(Src1SubLo);
1152 MRI.clearKillFlags(Src2SubHi);
1153 MRI.clearKillFlags(Src2SubLo);
1154 return true;
1155 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001156 case Hexagon::PS_vmulw_acc: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001157 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001158 unsigned DstReg = MI.getOperand(0).getReg();
1159 unsigned Src1Reg = MI.getOperand(1).getReg();
1160 unsigned Src2Reg = MI.getOperand(2).getReg();
1161 unsigned Src3Reg = MI.getOperand(3).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001162 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
1163 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
1164 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
1165 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
1166 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::subreg_hireg);
1167 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001168 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
1169 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1170 .addReg(Src1SubHi)
1171 .addReg(Src2SubHi)
1172 .addReg(Src3SubHi);
1173 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
1174 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1175 .addReg(Src1SubLo)
1176 .addReg(Src2SubLo)
1177 .addReg(Src3SubLo);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001178 MBB.erase(MI);
1179 MRI.clearKillFlags(Src1SubHi);
1180 MRI.clearKillFlags(Src1SubLo);
1181 MRI.clearKillFlags(Src2SubHi);
1182 MRI.clearKillFlags(Src2SubLo);
1183 MRI.clearKillFlags(Src3SubHi);
1184 MRI.clearKillFlags(Src3SubLo);
1185 return true;
1186 }
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +00001187 case Hexagon::Insert4: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001188 unsigned DstReg = MI.getOperand(0).getReg();
1189 unsigned Src1Reg = MI.getOperand(1).getReg();
1190 unsigned Src2Reg = MI.getOperand(2).getReg();
1191 unsigned Src3Reg = MI.getOperand(3).getReg();
1192 unsigned Src4Reg = MI.getOperand(4).getReg();
1193 unsigned Src1RegIsKill = getKillRegState(MI.getOperand(1).isKill());
1194 unsigned Src2RegIsKill = getKillRegState(MI.getOperand(2).isKill());
1195 unsigned Src3RegIsKill = getKillRegState(MI.getOperand(3).isKill());
1196 unsigned Src4RegIsKill = getKillRegState(MI.getOperand(4).isKill());
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +00001197 unsigned DstSubHi = HRI.getSubReg(DstReg, Hexagon::subreg_hireg);
1198 unsigned DstSubLo = HRI.getSubReg(DstReg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001199 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1200 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1201 .addReg(DstSubLo)
1202 .addReg(Src1Reg, Src1RegIsKill)
1203 .addImm(16)
1204 .addImm(0);
1205 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1206 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1207 .addReg(DstSubLo)
1208 .addReg(Src2Reg, Src2RegIsKill)
1209 .addImm(16)
1210 .addImm(16);
1211 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1212 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1213 .addReg(DstSubHi)
1214 .addReg(Src3Reg, Src3RegIsKill)
1215 .addImm(16)
1216 .addImm(0);
1217 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1218 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1219 .addReg(DstSubHi)
1220 .addReg(Src4Reg, Src4RegIsKill)
1221 .addImm(16)
1222 .addImm(16);
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +00001223 MBB.erase(MI);
1224 MRI.clearKillFlags(DstReg);
1225 MRI.clearKillFlags(DstSubHi);
1226 MRI.clearKillFlags(DstSubLo);
1227 return true;
1228 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001229 case Hexagon::PS_pselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001230 const MachineOperand &Op0 = MI.getOperand(0);
1231 const MachineOperand &Op1 = MI.getOperand(1);
1232 const MachineOperand &Op2 = MI.getOperand(2);
1233 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001234 unsigned Rd = Op0.getReg();
1235 unsigned Pu = Op1.getReg();
1236 unsigned Rs = Op2.getReg();
1237 unsigned Rt = Op3.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001238 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001239 unsigned K1 = getKillRegState(Op1.isKill());
1240 unsigned K2 = getKillRegState(Op2.isKill());
1241 unsigned K3 = getKillRegState(Op3.isKill());
1242 if (Rd != Rs)
1243 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1244 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1245 .addReg(Rs, K2);
1246 if (Rd != Rt)
1247 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1248 .addReg(Pu, K1)
1249 .addReg(Rt, K3);
1250 MBB.erase(MI);
1251 return true;
1252 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001253 case Hexagon::PS_vselect:
1254 case Hexagon::PS_vselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001255 const MachineOperand &Op0 = MI.getOperand(0);
1256 const MachineOperand &Op1 = MI.getOperand(1);
1257 const MachineOperand &Op2 = MI.getOperand(2);
1258 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001259 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
1260 .addOperand(Op0)
1261 .addOperand(Op1)
1262 .addOperand(Op2);
1263 BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
1264 .addOperand(Op0)
1265 .addOperand(Op1)
1266 .addOperand(Op3);
1267 MBB.erase(MI);
1268 return true;
1269 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001270 case Hexagon::PS_wselect:
1271 case Hexagon::PS_wselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001272 MachineOperand &Op0 = MI.getOperand(0);
1273 MachineOperand &Op1 = MI.getOperand(1);
1274 MachineOperand &Op2 = MI.getOperand(2);
1275 MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001276 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::subreg_loreg);
1277 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::subreg_hireg);
1278 BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
1279 .addOperand(Op0)
1280 .addOperand(Op1)
1281 .addReg(SrcHi)
1282 .addReg(SrcLo);
1283 SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::subreg_loreg);
1284 SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::subreg_hireg);
1285 BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
1286 .addOperand(Op0)
1287 .addOperand(Op1)
1288 .addReg(SrcHi)
1289 .addReg(SrcLo);
1290 MBB.erase(MI);
1291 return true;
1292 }
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001293 case Hexagon::PS_tailcall_i:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001294 MI.setDesc(get(Hexagon::J2_jump));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001295 return true;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001296 case Hexagon::PS_tailcall_r:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001297 MI.setDesc(get(Hexagon::J2_jumpr));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001298 return true;
1299 }
1300
1301 return false;
1302}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001303
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001304
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001305// We indicate that we want to reverse the branch by
1306// inserting the reversed branching opcode.
1307bool HexagonInstrInfo::ReverseBranchCondition(
1308 SmallVectorImpl<MachineOperand> &Cond) const {
1309 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001310 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001311 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1312 unsigned opcode = Cond[0].getImm();
1313 //unsigned temp;
1314 assert(get(opcode).isBranch() && "Should be a branching condition.");
1315 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001316 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001317 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1318 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001319 return false;
1320}
1321
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001322
1323void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1324 MachineBasicBlock::iterator MI) const {
1325 DebugLoc DL;
1326 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1327}
1328
1329
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001330bool HexagonInstrInfo::isPostIncrement(const MachineInstr &MI) const {
1331 return getAddrMode(MI) == HexagonII::PostInc;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001332}
1333
1334
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001335// Returns true if an instruction is predicated irrespective of the predicate
1336// sense. For example, all of the following will return true.
1337// if (p0) R1 = add(R2, R3)
1338// if (!p0) R1 = add(R2, R3)
1339// if (p0.new) R1 = add(R2, R3)
1340// if (!p0.new) R1 = add(R2, R3)
1341// Note: New-value stores are not included here as in the current
1342// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001343bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1344 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001345 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001346}
1347
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001348
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001349bool HexagonInstrInfo::PredicateInstruction(
1350 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001351 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1352 isEndLoopN(Cond[0].getImm())) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001353 DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001354 return false;
1355 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001356 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001357 assert (isPredicable(MI) && "Expected predicable instruction");
1358 bool invertJump = predOpcodeHasNot(Cond);
1359
1360 // We have to predicate MI "in place", i.e. after this function returns,
1361 // MI will need to be transformed into a predicated form. To avoid com-
1362 // plicated manipulations with the operands (handling tied operands,
1363 // etc.), build a new temporary instruction, then overwrite MI with it.
1364
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001365 MachineBasicBlock &B = *MI.getParent();
1366 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001367 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1368 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001369 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001370 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001371 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001372 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1373 break;
1374 T.addOperand(Op);
1375 NOp++;
1376 }
1377
1378 unsigned PredReg, PredRegPos, PredRegFlags;
1379 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1380 (void)GotPredReg;
1381 assert(GotPredReg);
1382 T.addReg(PredReg, PredRegFlags);
1383 while (NOp < NumOps)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001384 T.addOperand(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001385
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001386 MI.setDesc(get(PredOpc));
1387 while (unsigned n = MI.getNumOperands())
1388 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001389 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001390 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001391
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001392 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001393 B.erase(TI);
1394
1395 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1396 MRI.clearKillFlags(PredReg);
1397 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001398}
1399
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001400
1401bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1402 ArrayRef<MachineOperand> Pred2) const {
1403 // TODO: Fix this
1404 return false;
1405}
1406
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001407
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001408bool HexagonInstrInfo::DefinesPredicate(
1409 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001410 auto &HRI = getRegisterInfo();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001411 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1412 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001413 if (MO.isReg() && MO.isDef()) {
1414 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1415 if (RC == &Hexagon::PredRegsRegClass) {
1416 Pred.push_back(MO);
1417 return true;
1418 }
1419 }
1420 }
1421 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001422}
Andrew Trickd06df962012-02-01 22:13:57 +00001423
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001424
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001425bool HexagonInstrInfo::isPredicable(MachineInstr &MI) const {
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001426 return MI.getDesc().isPredicable();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001427}
1428
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001429bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1430 const MachineBasicBlock *MBB,
1431 const MachineFunction &MF) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001432 // Debug info is never a scheduling boundary. It's necessary to be explicit
1433 // due to the special treatment of IT instructions below, otherwise a
1434 // dbg_value followed by an IT will result in the IT instruction being
1435 // considered a scheduling hazard, which is wrong. It should be the actual
1436 // instruction preceding the dbg_value instruction(s), just like it is
1437 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001438 if (MI.isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001439 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001440
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001441 // Throwing call is a boundary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001442 if (MI.isCall()) {
Krzysztof Parzyszekab9127c2016-08-12 11:01:10 +00001443 // Don't mess around with no return calls.
1444 if (doesNotReturn(MI))
1445 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001446 // If any of the block's successors is a landing pad, this could be a
1447 // throwing call.
1448 for (auto I : MBB->successors())
1449 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001450 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001451 }
1452
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001453 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001454 if (MI.getDesc().isTerminator() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001455 return true;
1456
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001457 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1458 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001459
1460 return false;
1461}
1462
1463
1464/// Measure the specified inline asm to determine an approximation of its
1465/// length.
1466/// Comments (which run till the next SeparatorString or newline) do not
1467/// count as an instruction.
1468/// Any other non-whitespace text is considered an instruction, with
1469/// multiple instructions separated by SeparatorString or newlines.
1470/// Variable-length instructions are not handled here; this function
1471/// may be overloaded in the target code to do that.
1472/// Hexagon counts the number of ##'s and adjust for that many
1473/// constant exenders.
1474unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1475 const MCAsmInfo &MAI) const {
1476 StringRef AStr(Str);
1477 // Count the number of instructions in the asm.
1478 bool atInsnStart = true;
1479 unsigned Length = 0;
1480 for (; *Str; ++Str) {
1481 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1482 strlen(MAI.getSeparatorString())) == 0)
1483 atInsnStart = true;
1484 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1485 Length += MAI.getMaxInstLength();
1486 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001487 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001488 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
1489 strlen(MAI.getCommentString())) == 0)
1490 atInsnStart = false;
1491 }
1492
1493 // Add to size number of constant extenders seen * 4.
1494 StringRef Occ("##");
1495 Length += AStr.count(Occ)*4;
1496 return Length;
1497}
1498
1499
1500ScheduleHazardRecognizer*
1501HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1502 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +00001503 if (UseDFAHazardRec) {
1504 auto &HST = DAG->MF.getSubtarget<HexagonSubtarget>();
1505 return new HexagonHazardRecognizer(II, this, HST);
1506 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001507 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1508}
1509
1510
1511/// \brief For a comparison instruction, return the source registers in
1512/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1513/// compares against in CmpValue. Return true if the comparison instruction
1514/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001515bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1516 unsigned &SrcReg2, int &Mask,
1517 int &Value) const {
1518 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001519
1520 // Set mask and the first source register.
1521 switch (Opc) {
1522 case Hexagon::C2_cmpeq:
1523 case Hexagon::C2_cmpeqp:
1524 case Hexagon::C2_cmpgt:
1525 case Hexagon::C2_cmpgtp:
1526 case Hexagon::C2_cmpgtu:
1527 case Hexagon::C2_cmpgtup:
1528 case Hexagon::C4_cmpneq:
1529 case Hexagon::C4_cmplte:
1530 case Hexagon::C4_cmplteu:
1531 case Hexagon::C2_cmpeqi:
1532 case Hexagon::C2_cmpgti:
1533 case Hexagon::C2_cmpgtui:
1534 case Hexagon::C4_cmpneqi:
1535 case Hexagon::C4_cmplteui:
1536 case Hexagon::C4_cmpltei:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001537 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001538 Mask = ~0;
1539 break;
1540 case Hexagon::A4_cmpbeq:
1541 case Hexagon::A4_cmpbgt:
1542 case Hexagon::A4_cmpbgtu:
1543 case Hexagon::A4_cmpbeqi:
1544 case Hexagon::A4_cmpbgti:
1545 case Hexagon::A4_cmpbgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001546 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001547 Mask = 0xFF;
1548 break;
1549 case Hexagon::A4_cmpheq:
1550 case Hexagon::A4_cmphgt:
1551 case Hexagon::A4_cmphgtu:
1552 case Hexagon::A4_cmpheqi:
1553 case Hexagon::A4_cmphgti:
1554 case Hexagon::A4_cmphgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001555 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001556 Mask = 0xFFFF;
1557 break;
1558 }
1559
1560 // Set the value/second source register.
1561 switch (Opc) {
1562 case Hexagon::C2_cmpeq:
1563 case Hexagon::C2_cmpeqp:
1564 case Hexagon::C2_cmpgt:
1565 case Hexagon::C2_cmpgtp:
1566 case Hexagon::C2_cmpgtu:
1567 case Hexagon::C2_cmpgtup:
1568 case Hexagon::A4_cmpbeq:
1569 case Hexagon::A4_cmpbgt:
1570 case Hexagon::A4_cmpbgtu:
1571 case Hexagon::A4_cmpheq:
1572 case Hexagon::A4_cmphgt:
1573 case Hexagon::A4_cmphgtu:
1574 case Hexagon::C4_cmpneq:
1575 case Hexagon::C4_cmplte:
1576 case Hexagon::C4_cmplteu:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001577 SrcReg2 = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001578 return true;
1579
1580 case Hexagon::C2_cmpeqi:
1581 case Hexagon::C2_cmpgtui:
1582 case Hexagon::C2_cmpgti:
1583 case Hexagon::C4_cmpneqi:
1584 case Hexagon::C4_cmplteui:
1585 case Hexagon::C4_cmpltei:
1586 case Hexagon::A4_cmpbeqi:
1587 case Hexagon::A4_cmpbgti:
1588 case Hexagon::A4_cmpbgtui:
1589 case Hexagon::A4_cmpheqi:
1590 case Hexagon::A4_cmphgti:
1591 case Hexagon::A4_cmphgtui:
1592 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001593 Value = MI.getOperand(2).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001594 return true;
1595 }
1596
1597 return false;
1598}
1599
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001600unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001601 const MachineInstr &MI,
1602 unsigned *PredCost) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001603 return getInstrTimingClassLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001604}
1605
1606
1607DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1608 const TargetSubtargetInfo &STI) const {
1609 const InstrItineraryData *II = STI.getInstrItineraryData();
1610 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1611}
1612
1613
1614// Inspired by this pair:
1615// %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1616// S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1617// Currently AA considers the addresses in these instructions to be aliasing.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001618bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1619 MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001620 int OffsetA = 0, OffsetB = 0;
1621 unsigned SizeA = 0, SizeB = 0;
1622
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001623 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1624 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001625 return false;
1626
1627 // Instructions that are pure loads, not loads and stores like memops are not
1628 // dependent.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001629 if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001630 return true;
1631
1632 // Get base, offset, and access size in MIa.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001633 unsigned BaseRegA = getBaseAndOffset(MIa, OffsetA, SizeA);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001634 if (!BaseRegA || !SizeA)
1635 return false;
1636
1637 // Get base, offset, and access size in MIb.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001638 unsigned BaseRegB = getBaseAndOffset(MIb, OffsetB, SizeB);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001639 if (!BaseRegB || !SizeB)
1640 return false;
1641
1642 if (BaseRegA != BaseRegB)
1643 return false;
1644
1645 // This is a mem access with the same base register and known offsets from it.
1646 // Reason about it.
1647 if (OffsetA > OffsetB) {
1648 uint64_t offDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1649 return (SizeB <= offDiff);
1650 } else if (OffsetA < OffsetB) {
1651 uint64_t offDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1652 return (SizeA <= offDiff);
1653 }
1654
1655 return false;
1656}
1657
1658
Brendon Cahoon254f8892016-07-29 16:44:44 +00001659/// If the instruction is an increment of a constant value, return the amount.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001660bool HexagonInstrInfo::getIncrementValue(const MachineInstr &MI,
Brendon Cahoon254f8892016-07-29 16:44:44 +00001661 int &Value) const {
1662 if (isPostIncrement(MI)) {
1663 unsigned AccessSize;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001664 return getBaseAndOffset(MI, Value, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00001665 }
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001666 if (MI.getOpcode() == Hexagon::A2_addi) {
1667 Value = MI.getOperand(2).getImm();
Brendon Cahoon254f8892016-07-29 16:44:44 +00001668 return true;
1669 }
1670
1671 return false;
1672}
1673
1674
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001675unsigned HexagonInstrInfo::createVR(MachineFunction *MF, MVT VT) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001676 MachineRegisterInfo &MRI = MF->getRegInfo();
1677 const TargetRegisterClass *TRC;
1678 if (VT == MVT::i1) {
1679 TRC = &Hexagon::PredRegsRegClass;
1680 } else if (VT == MVT::i32 || VT == MVT::f32) {
1681 TRC = &Hexagon::IntRegsRegClass;
1682 } else if (VT == MVT::i64 || VT == MVT::f64) {
1683 TRC = &Hexagon::DoubleRegsRegClass;
1684 } else {
1685 llvm_unreachable("Cannot handle this register class");
1686 }
1687
1688 unsigned NewReg = MRI.createVirtualRegister(TRC);
1689 return NewReg;
1690}
1691
1692
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001693bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001694 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1695}
1696
1697
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001698bool HexagonInstrInfo::isAccumulator(const MachineInstr &MI) const {
1699 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001700 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1701}
1702
1703
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001704bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
1705 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001706 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1707 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1708
1709 if (!(isTC1(MI))
1710 && !(QII->isTC2Early(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001711 && !(MI.getDesc().mayLoad())
1712 && !(MI.getDesc().mayStore())
1713 && (MI.getDesc().getOpcode() != Hexagon::S2_allocframe)
1714 && (MI.getDesc().getOpcode() != Hexagon::L2_deallocframe)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001715 && !(QII->isMemOp(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001716 && !(MI.isBranch())
1717 && !(MI.isReturn())
1718 && !MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001719 return true;
1720
1721 return false;
1722}
1723
1724
Sanjay Patele4b9f502015-12-07 19:21:39 +00001725// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001726bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
1727 return (getType(MI) == HexagonII::TypeCOMPOUND && MI.isBranch());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001728}
1729
1730
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001731bool HexagonInstrInfo::isCondInst(const MachineInstr &MI) const {
1732 return (MI.isBranch() && isPredicated(MI)) ||
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001733 isConditionalTransfer(MI) ||
1734 isConditionalALU32(MI) ||
1735 isConditionalLoad(MI) ||
1736 // Predicated stores which don't have a .new on any operands.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001737 (MI.mayStore() && isPredicated(MI) && !isNewValueStore(MI) &&
1738 !isPredicatedNew(MI));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001739}
1740
1741
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001742bool HexagonInstrInfo::isConditionalALU32(const MachineInstr &MI) const {
1743 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001744 case Hexagon::A2_paddf:
1745 case Hexagon::A2_paddfnew:
1746 case Hexagon::A2_paddif:
1747 case Hexagon::A2_paddifnew:
1748 case Hexagon::A2_paddit:
1749 case Hexagon::A2_padditnew:
1750 case Hexagon::A2_paddt:
1751 case Hexagon::A2_paddtnew:
1752 case Hexagon::A2_pandf:
1753 case Hexagon::A2_pandfnew:
1754 case Hexagon::A2_pandt:
1755 case Hexagon::A2_pandtnew:
1756 case Hexagon::A2_porf:
1757 case Hexagon::A2_porfnew:
1758 case Hexagon::A2_port:
1759 case Hexagon::A2_portnew:
1760 case Hexagon::A2_psubf:
1761 case Hexagon::A2_psubfnew:
1762 case Hexagon::A2_psubt:
1763 case Hexagon::A2_psubtnew:
1764 case Hexagon::A2_pxorf:
1765 case Hexagon::A2_pxorfnew:
1766 case Hexagon::A2_pxort:
1767 case Hexagon::A2_pxortnew:
1768 case Hexagon::A4_paslhf:
1769 case Hexagon::A4_paslhfnew:
1770 case Hexagon::A4_paslht:
1771 case Hexagon::A4_paslhtnew:
1772 case Hexagon::A4_pasrhf:
1773 case Hexagon::A4_pasrhfnew:
1774 case Hexagon::A4_pasrht:
1775 case Hexagon::A4_pasrhtnew:
1776 case Hexagon::A4_psxtbf:
1777 case Hexagon::A4_psxtbfnew:
1778 case Hexagon::A4_psxtbt:
1779 case Hexagon::A4_psxtbtnew:
1780 case Hexagon::A4_psxthf:
1781 case Hexagon::A4_psxthfnew:
1782 case Hexagon::A4_psxtht:
1783 case Hexagon::A4_psxthtnew:
1784 case Hexagon::A4_pzxtbf:
1785 case Hexagon::A4_pzxtbfnew:
1786 case Hexagon::A4_pzxtbt:
1787 case Hexagon::A4_pzxtbtnew:
1788 case Hexagon::A4_pzxthf:
1789 case Hexagon::A4_pzxthfnew:
1790 case Hexagon::A4_pzxtht:
1791 case Hexagon::A4_pzxthtnew:
1792 case Hexagon::C2_ccombinewf:
1793 case Hexagon::C2_ccombinewt:
1794 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001795 }
1796 return false;
1797}
1798
1799
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001800// FIXME - Function name and it's functionality don't match.
1801// It should be renamed to hasPredNewOpcode()
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001802bool HexagonInstrInfo::isConditionalLoad(const MachineInstr &MI) const {
1803 if (!MI.getDesc().mayLoad() || !isPredicated(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001804 return false;
1805
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001806 int PNewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001807 // Instruction with valid predicated-new opcode can be promoted to .new.
1808 return PNewOpcode >= 0;
1809}
1810
1811
1812// Returns true if an instruction is a conditional store.
1813//
1814// Note: It doesn't include conditional new-value stores as they can't be
1815// converted to .new predicate.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001816bool HexagonInstrInfo::isConditionalStore(const MachineInstr &MI) const {
1817 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001818 default: return false;
1819 case Hexagon::S4_storeirbt_io:
1820 case Hexagon::S4_storeirbf_io:
1821 case Hexagon::S4_pstorerbt_rr:
1822 case Hexagon::S4_pstorerbf_rr:
1823 case Hexagon::S2_pstorerbt_io:
1824 case Hexagon::S2_pstorerbf_io:
1825 case Hexagon::S2_pstorerbt_pi:
1826 case Hexagon::S2_pstorerbf_pi:
1827 case Hexagon::S2_pstorerdt_io:
1828 case Hexagon::S2_pstorerdf_io:
1829 case Hexagon::S4_pstorerdt_rr:
1830 case Hexagon::S4_pstorerdf_rr:
1831 case Hexagon::S2_pstorerdt_pi:
1832 case Hexagon::S2_pstorerdf_pi:
1833 case Hexagon::S2_pstorerht_io:
1834 case Hexagon::S2_pstorerhf_io:
1835 case Hexagon::S4_storeirht_io:
1836 case Hexagon::S4_storeirhf_io:
1837 case Hexagon::S4_pstorerht_rr:
1838 case Hexagon::S4_pstorerhf_rr:
1839 case Hexagon::S2_pstorerht_pi:
1840 case Hexagon::S2_pstorerhf_pi:
1841 case Hexagon::S2_pstorerit_io:
1842 case Hexagon::S2_pstorerif_io:
1843 case Hexagon::S4_storeirit_io:
1844 case Hexagon::S4_storeirif_io:
1845 case Hexagon::S4_pstorerit_rr:
1846 case Hexagon::S4_pstorerif_rr:
1847 case Hexagon::S2_pstorerit_pi:
1848 case Hexagon::S2_pstorerif_pi:
1849
1850 // V4 global address store before promoting to dot new.
1851 case Hexagon::S4_pstorerdt_abs:
1852 case Hexagon::S4_pstorerdf_abs:
1853 case Hexagon::S4_pstorerbt_abs:
1854 case Hexagon::S4_pstorerbf_abs:
1855 case Hexagon::S4_pstorerht_abs:
1856 case Hexagon::S4_pstorerhf_abs:
1857 case Hexagon::S4_pstorerit_abs:
1858 case Hexagon::S4_pstorerif_abs:
1859 return true;
1860
1861 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1862 // from the "Conditional Store" list. Because a predicated new value store
1863 // would NOT be promoted to a double dot new store.
1864 // This function returns yes for those stores that are predicated but not
1865 // yet promoted to predicate dot new instructions.
1866 }
1867}
1868
1869
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001870bool HexagonInstrInfo::isConditionalTransfer(const MachineInstr &MI) const {
1871 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001872 case Hexagon::A2_tfrt:
1873 case Hexagon::A2_tfrf:
1874 case Hexagon::C2_cmoveit:
1875 case Hexagon::C2_cmoveif:
1876 case Hexagon::A2_tfrtnew:
1877 case Hexagon::A2_tfrfnew:
1878 case Hexagon::C2_cmovenewit:
1879 case Hexagon::C2_cmovenewif:
1880 case Hexagon::A2_tfrpt:
1881 case Hexagon::A2_tfrpf:
1882 return true;
1883
1884 default:
1885 return false;
1886 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001887 return false;
1888}
1889
1890
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001891// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1892// isFPImm and later getFPImm as well.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001893bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
1894 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001895 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1896 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001897 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001898
1899 unsigned isExtendable =
1900 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1901 if (!isExtendable)
1902 return false;
1903
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001904 if (MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001905 return false;
1906
1907 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001908 const MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001909 // Use MO operand flags to determine if MO
1910 // has the HMOTF_ConstExtended flag set.
1911 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001912 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001913 // If this is a Machine BB address we are talking about, and it is
1914 // not marked as extended, say so.
1915 if (MO.isMBB())
1916 return false;
1917
1918 // We could be using an instruction with an extendable immediate and shoehorn
1919 // a global address into it. If it is a global address it will be constant
1920 // extended. We do this for COMBINE.
1921 // We currently only handle isGlobal() because it is the only kind of
1922 // object we are going to end up with here for now.
1923 // In the future we probably should add isSymbol(), etc.
1924 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001925 MO.isJTI() || MO.isCPI() || MO.isFPImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001926 return true;
1927
1928 // If the extendable operand is not 'Immediate' type, the instruction should
1929 // have 'isExtended' flag set.
1930 assert(MO.isImm() && "Extendable operand must be Immediate type");
1931
1932 int MinValue = getMinValue(MI);
1933 int MaxValue = getMaxValue(MI);
1934 int ImmValue = MO.getImm();
1935
1936 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001937}
1938
1939
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001940bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
1941 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001942 case Hexagon::L4_return :
1943 case Hexagon::L4_return_t :
1944 case Hexagon::L4_return_f :
1945 case Hexagon::L4_return_tnew_pnt :
1946 case Hexagon::L4_return_fnew_pnt :
1947 case Hexagon::L4_return_tnew_pt :
1948 case Hexagon::L4_return_fnew_pt :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001949 return true;
1950 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001951 return false;
1952}
1953
1954
1955// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001956bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
1957 const MachineInstr &ConsMI) const {
1958 if (!ProdMI.getDesc().getNumDefs())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001959 return false;
1960
1961 auto &HRI = getRegisterInfo();
1962
1963 SmallVector<unsigned, 4> DefsA;
1964 SmallVector<unsigned, 4> DefsB;
1965 SmallVector<unsigned, 8> UsesA;
1966 SmallVector<unsigned, 8> UsesB;
1967
1968 parseOperands(ProdMI, DefsA, UsesA);
1969 parseOperands(ConsMI, DefsB, UsesB);
1970
1971 for (auto &RegA : DefsA)
1972 for (auto &RegB : UsesB) {
1973 // True data dependency.
1974 if (RegA == RegB)
1975 return true;
1976
1977 if (Hexagon::DoubleRegsRegClass.contains(RegA))
1978 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1979 if (RegB == *SubRegs)
1980 return true;
1981
1982 if (Hexagon::DoubleRegsRegClass.contains(RegB))
1983 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1984 if (RegA == *SubRegs)
1985 return true;
1986 }
1987
1988 return false;
1989}
1990
1991
1992// Returns true if the instruction is alread a .cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001993bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
1994 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001995 case Hexagon::V6_vL32b_cur_pi:
1996 case Hexagon::V6_vL32b_cur_ai:
1997 case Hexagon::V6_vL32b_cur_pi_128B:
1998 case Hexagon::V6_vL32b_cur_ai_128B:
1999 return true;
2000 }
2001 return false;
2002}
2003
2004
2005// Returns true, if any one of the operands is a dot new
2006// insn, whether it is predicated dot new or register dot new.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002007bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
2008 if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002009 return true;
2010
2011 return false;
2012}
2013
2014
2015/// Symmetrical. See if these two instructions are fit for duplex pair.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002016bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
2017 const MachineInstr &MIb) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002018 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
2019 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
2020 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
2021}
2022
2023
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002024bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
2025 if (MI.mayLoad() || MI.mayStore() || MI.isCompare())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002026 return true;
2027
2028 // Multiply
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002029 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002030 if (SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23)
2031 return true;
2032 return false;
2033}
2034
2035
2036bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
2037 return (Opcode == Hexagon::ENDLOOP0 ||
2038 Opcode == Hexagon::ENDLOOP1);
2039}
2040
2041
2042bool HexagonInstrInfo::isExpr(unsigned OpType) const {
2043 switch(OpType) {
2044 case MachineOperand::MO_MachineBasicBlock:
2045 case MachineOperand::MO_GlobalAddress:
2046 case MachineOperand::MO_ExternalSymbol:
2047 case MachineOperand::MO_JumpTableIndex:
2048 case MachineOperand::MO_ConstantPoolIndex:
2049 case MachineOperand::MO_BlockAddress:
2050 return true;
2051 default:
2052 return false;
2053 }
2054}
2055
2056
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002057bool HexagonInstrInfo::isExtendable(const MachineInstr &MI) const {
2058 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002059 const uint64_t F = MID.TSFlags;
2060 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
2061 return true;
2062
2063 // TODO: This is largely obsolete now. Will need to be removed
2064 // in consecutive patches.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002065 switch (MI.getOpcode()) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002066 // PS_fi and PS_fia remain special cases.
2067 case Hexagon::PS_fi:
2068 case Hexagon::PS_fia:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002069 return true;
2070 default:
2071 return false;
2072 }
2073 return false;
2074}
2075
2076
2077// This returns true in two cases:
2078// - The OP code itself indicates that this is an extended instruction.
2079// - One of MOs has been marked with HMOTF_ConstExtended flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002080bool HexagonInstrInfo::isExtended(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002081 // First check if this is permanently extended op code.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002082 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002083 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2084 return true;
2085 // Use MO operand flags to determine if one of MI's operands
2086 // has HMOTF_ConstExtended flag set.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002087 for (MachineInstr::const_mop_iterator I = MI.operands_begin(),
2088 E = MI.operands_end(); I != E; ++I) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002089 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
2090 return true;
2091 }
2092 return false;
2093}
2094
2095
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002096bool HexagonInstrInfo::isFloat(const MachineInstr &MI) const {
2097 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002098 const uint64_t F = get(Opcode).TSFlags;
2099 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2100}
2101
2102
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002103// No V60 HVX VMEM with A_INDIRECT.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002104bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr &I,
2105 const MachineInstr &J) const {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002106 if (!isV60VectorInstruction(I))
2107 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002108 if (!I.mayLoad() && !I.mayStore())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002109 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002110 return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002111}
2112
2113
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002114bool HexagonInstrInfo::isIndirectCall(const MachineInstr &MI) const {
2115 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002116 case Hexagon::J2_callr :
2117 case Hexagon::J2_callrf :
2118 case Hexagon::J2_callrt :
2119 return true;
2120 }
2121 return false;
2122}
2123
2124
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002125bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr &MI) const {
2126 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002127 case Hexagon::L4_return :
2128 case Hexagon::L4_return_t :
2129 case Hexagon::L4_return_f :
2130 case Hexagon::L4_return_fnew_pnt :
2131 case Hexagon::L4_return_fnew_pt :
2132 case Hexagon::L4_return_tnew_pnt :
2133 case Hexagon::L4_return_tnew_pt :
2134 return true;
2135 }
2136 return false;
2137}
2138
2139
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002140bool HexagonInstrInfo::isJumpR(const MachineInstr &MI) const {
2141 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002142 case Hexagon::J2_jumpr :
2143 case Hexagon::J2_jumprt :
2144 case Hexagon::J2_jumprf :
2145 case Hexagon::J2_jumprtnewpt :
2146 case Hexagon::J2_jumprfnewpt :
2147 case Hexagon::J2_jumprtnew :
2148 case Hexagon::J2_jumprfnew :
2149 return true;
2150 }
2151 return false;
2152}
2153
2154
2155// Return true if a given MI can accomodate given offset.
2156// Use abs estimate as oppose to the exact number.
2157// TODO: This will need to be changed to use MC level
2158// definition of instruction extendable field size.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002159bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002160 unsigned offset) const {
2161 // This selection of jump instructions matches to that what
2162 // AnalyzeBranch can parse, plus NVJ.
2163 if (isNewValueJump(MI)) // r9:2
2164 return isInt<11>(offset);
2165
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002166 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002167 // Still missing Jump to address condition on register value.
2168 default:
2169 return false;
2170 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2171 case Hexagon::J2_call:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002172 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002173 return isInt<24>(offset);
2174 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2175 case Hexagon::J2_jumpf:
2176 case Hexagon::J2_jumptnew:
2177 case Hexagon::J2_jumptnewpt:
2178 case Hexagon::J2_jumpfnew:
2179 case Hexagon::J2_jumpfnewpt:
2180 case Hexagon::J2_callt:
2181 case Hexagon::J2_callf:
2182 return isInt<17>(offset);
2183 case Hexagon::J2_loop0i:
2184 case Hexagon::J2_loop0iext:
2185 case Hexagon::J2_loop0r:
2186 case Hexagon::J2_loop0rext:
2187 case Hexagon::J2_loop1i:
2188 case Hexagon::J2_loop1iext:
2189 case Hexagon::J2_loop1r:
2190 case Hexagon::J2_loop1rext:
2191 return isInt<9>(offset);
2192 // TODO: Add all the compound branches here. Can we do this in Relation model?
2193 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2194 case Hexagon::J4_cmpeqi_tp1_jump_nt:
2195 return isInt<11>(offset);
2196 }
2197}
2198
2199
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002200bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr &LRMI,
2201 const MachineInstr &ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002202 bool isLate = isLateResultInstr(LRMI);
2203 bool isEarly = isEarlySourceInstr(ESMI);
2204
2205 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002206 DEBUG(LRMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002207 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002208 DEBUG(ESMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002209
2210 if (isLate && isEarly) {
2211 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2212 return true;
2213 }
2214
2215 return false;
2216}
2217
2218
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002219bool HexagonInstrInfo::isLateResultInstr(const MachineInstr &MI) const {
2220 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002221 case TargetOpcode::EXTRACT_SUBREG:
2222 case TargetOpcode::INSERT_SUBREG:
2223 case TargetOpcode::SUBREG_TO_REG:
2224 case TargetOpcode::REG_SEQUENCE:
2225 case TargetOpcode::IMPLICIT_DEF:
2226 case TargetOpcode::COPY:
2227 case TargetOpcode::INLINEASM:
2228 case TargetOpcode::PHI:
2229 return false;
2230 default:
2231 break;
2232 }
2233
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002234 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002235
2236 switch (SchedClass) {
2237 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2238 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2239 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2240 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2241 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2242 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2243 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2244 case Hexagon::Sched::V2LDST_tc_ld_SLOT01:
2245 case Hexagon::Sched::V2LDST_tc_st_SLOT0:
2246 case Hexagon::Sched::V2LDST_tc_st_SLOT01:
2247 case Hexagon::Sched::V4LDST_tc_ld_SLOT01:
2248 case Hexagon::Sched::V4LDST_tc_st_SLOT0:
2249 case Hexagon::Sched::V4LDST_tc_st_SLOT01:
2250 return false;
2251 }
2252 return true;
2253}
2254
2255
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002256bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002257 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2258 // resource, but all operands can be received late like an ALU instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002259 return MI.getDesc().getSchedClass() == Hexagon::Sched::CVI_VX_LATE;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002260}
2261
2262
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002263bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2264 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002265 return Opcode == Hexagon::J2_loop0i ||
2266 Opcode == Hexagon::J2_loop0r ||
2267 Opcode == Hexagon::J2_loop0iext ||
2268 Opcode == Hexagon::J2_loop0rext ||
2269 Opcode == Hexagon::J2_loop1i ||
2270 Opcode == Hexagon::J2_loop1r ||
2271 Opcode == Hexagon::J2_loop1iext ||
2272 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002273}
2274
2275
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002276bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2277 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002278 default: return false;
2279 case Hexagon::L4_iadd_memopw_io :
2280 case Hexagon::L4_isub_memopw_io :
2281 case Hexagon::L4_add_memopw_io :
2282 case Hexagon::L4_sub_memopw_io :
2283 case Hexagon::L4_and_memopw_io :
2284 case Hexagon::L4_or_memopw_io :
2285 case Hexagon::L4_iadd_memoph_io :
2286 case Hexagon::L4_isub_memoph_io :
2287 case Hexagon::L4_add_memoph_io :
2288 case Hexagon::L4_sub_memoph_io :
2289 case Hexagon::L4_and_memoph_io :
2290 case Hexagon::L4_or_memoph_io :
2291 case Hexagon::L4_iadd_memopb_io :
2292 case Hexagon::L4_isub_memopb_io :
2293 case Hexagon::L4_add_memopb_io :
2294 case Hexagon::L4_sub_memopb_io :
2295 case Hexagon::L4_and_memopb_io :
2296 case Hexagon::L4_or_memopb_io :
2297 case Hexagon::L4_ior_memopb_io:
2298 case Hexagon::L4_ior_memoph_io:
2299 case Hexagon::L4_ior_memopw_io:
2300 case Hexagon::L4_iand_memopb_io:
2301 case Hexagon::L4_iand_memoph_io:
2302 case Hexagon::L4_iand_memopw_io:
2303 return true;
2304 }
2305 return false;
2306}
2307
2308
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002309bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2310 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002311 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2312}
2313
2314
2315bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2316 const uint64_t F = get(Opcode).TSFlags;
2317 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2318}
2319
2320
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002321bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002322 return isNewValueJump(MI) || isNewValueStore(MI);
2323}
2324
2325
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002326bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2327 return isNewValue(MI) && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002328}
2329
2330
2331bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2332 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2333}
2334
2335
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002336bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2337 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002338 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2339}
2340
2341
2342bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2343 const uint64_t F = get(Opcode).TSFlags;
2344 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2345}
2346
2347
2348// Returns true if a particular operand is extendable for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002349bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002350 unsigned OperandNum) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002351 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002352 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2353 == OperandNum;
2354}
2355
2356
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002357bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2358 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002359 assert(isPredicated(MI));
2360 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2361}
2362
2363
2364bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2365 const uint64_t F = get(Opcode).TSFlags;
2366 assert(isPredicated(Opcode));
2367 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2368}
2369
2370
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002371bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2372 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002373 return !((F >> HexagonII::PredicatedFalsePos) &
2374 HexagonII::PredicatedFalseMask);
2375}
2376
2377
2378bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2379 const uint64_t F = get(Opcode).TSFlags;
2380 // Make sure that the instruction is predicated.
2381 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2382 return !((F >> HexagonII::PredicatedFalsePos) &
2383 HexagonII::PredicatedFalseMask);
2384}
2385
2386
2387bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2388 const uint64_t F = get(Opcode).TSFlags;
2389 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2390}
2391
2392
2393bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2394 const uint64_t F = get(Opcode).TSFlags;
2395 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2396}
2397
2398
2399bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2400 const uint64_t F = get(Opcode).TSFlags;
2401 assert(get(Opcode).isBranch() &&
2402 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2403 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2404}
2405
2406
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002407bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2408 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2409 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2410 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2411 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002412}
2413
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002414bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2415 switch (MI.getOpcode()) {
2416 // Byte
2417 case Hexagon::L2_loadrb_io:
2418 case Hexagon::L4_loadrb_ur:
2419 case Hexagon::L4_loadrb_ap:
2420 case Hexagon::L2_loadrb_pr:
2421 case Hexagon::L2_loadrb_pbr:
2422 case Hexagon::L2_loadrb_pi:
2423 case Hexagon::L2_loadrb_pci:
2424 case Hexagon::L2_loadrb_pcr:
2425 case Hexagon::L2_loadbsw2_io:
2426 case Hexagon::L4_loadbsw2_ur:
2427 case Hexagon::L4_loadbsw2_ap:
2428 case Hexagon::L2_loadbsw2_pr:
2429 case Hexagon::L2_loadbsw2_pbr:
2430 case Hexagon::L2_loadbsw2_pi:
2431 case Hexagon::L2_loadbsw2_pci:
2432 case Hexagon::L2_loadbsw2_pcr:
2433 case Hexagon::L2_loadbsw4_io:
2434 case Hexagon::L4_loadbsw4_ur:
2435 case Hexagon::L4_loadbsw4_ap:
2436 case Hexagon::L2_loadbsw4_pr:
2437 case Hexagon::L2_loadbsw4_pbr:
2438 case Hexagon::L2_loadbsw4_pi:
2439 case Hexagon::L2_loadbsw4_pci:
2440 case Hexagon::L2_loadbsw4_pcr:
2441 case Hexagon::L4_loadrb_rr:
2442 case Hexagon::L2_ploadrbt_io:
2443 case Hexagon::L2_ploadrbt_pi:
2444 case Hexagon::L2_ploadrbf_io:
2445 case Hexagon::L2_ploadrbf_pi:
2446 case Hexagon::L2_ploadrbtnew_io:
2447 case Hexagon::L2_ploadrbfnew_io:
2448 case Hexagon::L4_ploadrbt_rr:
2449 case Hexagon::L4_ploadrbf_rr:
2450 case Hexagon::L4_ploadrbtnew_rr:
2451 case Hexagon::L4_ploadrbfnew_rr:
2452 case Hexagon::L2_ploadrbtnew_pi:
2453 case Hexagon::L2_ploadrbfnew_pi:
2454 case Hexagon::L4_ploadrbt_abs:
2455 case Hexagon::L4_ploadrbf_abs:
2456 case Hexagon::L4_ploadrbtnew_abs:
2457 case Hexagon::L4_ploadrbfnew_abs:
2458 case Hexagon::L2_loadrbgp:
2459 // Half
2460 case Hexagon::L2_loadrh_io:
2461 case Hexagon::L4_loadrh_ur:
2462 case Hexagon::L4_loadrh_ap:
2463 case Hexagon::L2_loadrh_pr:
2464 case Hexagon::L2_loadrh_pbr:
2465 case Hexagon::L2_loadrh_pi:
2466 case Hexagon::L2_loadrh_pci:
2467 case Hexagon::L2_loadrh_pcr:
2468 case Hexagon::L4_loadrh_rr:
2469 case Hexagon::L2_ploadrht_io:
2470 case Hexagon::L2_ploadrht_pi:
2471 case Hexagon::L2_ploadrhf_io:
2472 case Hexagon::L2_ploadrhf_pi:
2473 case Hexagon::L2_ploadrhtnew_io:
2474 case Hexagon::L2_ploadrhfnew_io:
2475 case Hexagon::L4_ploadrht_rr:
2476 case Hexagon::L4_ploadrhf_rr:
2477 case Hexagon::L4_ploadrhtnew_rr:
2478 case Hexagon::L4_ploadrhfnew_rr:
2479 case Hexagon::L2_ploadrhtnew_pi:
2480 case Hexagon::L2_ploadrhfnew_pi:
2481 case Hexagon::L4_ploadrht_abs:
2482 case Hexagon::L4_ploadrhf_abs:
2483 case Hexagon::L4_ploadrhtnew_abs:
2484 case Hexagon::L4_ploadrhfnew_abs:
2485 case Hexagon::L2_loadrhgp:
2486 return true;
2487 default:
2488 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002489 }
2490}
2491
2492
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002493bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2494 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002495 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2496}
2497
2498
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002499bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2500 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002501 case Hexagon::STriw_pred :
2502 case Hexagon::LDriw_pred :
2503 return true;
2504 default:
2505 return false;
2506 }
2507}
2508
2509
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002510bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2511 if (!MI.isBranch())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002512 return false;
2513
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002514 for (auto &Op : MI.operands())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002515 if (Op.isGlobal() || Op.isSymbol())
2516 return true;
2517 return false;
2518}
2519
2520
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002521// Returns true when SU has a timing class TC1.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002522bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2523 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002524 switch (SchedClass) {
2525 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2526 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2527 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2528 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2529 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2530 //case Hexagon::Sched::M_tc_1_SLOT23:
2531 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2532 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2533 return true;
2534
2535 default:
2536 return false;
2537 }
2538}
2539
2540
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002541bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2542 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002543 switch (SchedClass) {
2544 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
2545 case Hexagon::Sched::ALU64_tc_2_SLOT23:
2546 case Hexagon::Sched::CR_tc_2_SLOT3:
2547 case Hexagon::Sched::M_tc_2_SLOT23:
2548 case Hexagon::Sched::S_2op_tc_2_SLOT23:
2549 case Hexagon::Sched::S_3op_tc_2_SLOT23:
2550 return true;
2551
2552 default:
2553 return false;
2554 }
2555}
2556
2557
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002558bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2559 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002560 switch (SchedClass) {
2561 case Hexagon::Sched::ALU32_2op_tc_2early_SLOT0123:
2562 case Hexagon::Sched::ALU32_3op_tc_2early_SLOT0123:
2563 case Hexagon::Sched::ALU64_tc_2early_SLOT23:
2564 case Hexagon::Sched::CR_tc_2early_SLOT23:
2565 case Hexagon::Sched::CR_tc_2early_SLOT3:
2566 case Hexagon::Sched::J_tc_2early_SLOT0123:
2567 case Hexagon::Sched::J_tc_2early_SLOT2:
2568 case Hexagon::Sched::J_tc_2early_SLOT23:
2569 case Hexagon::Sched::S_2op_tc_2early_SLOT23:
2570 case Hexagon::Sched::S_3op_tc_2early_SLOT23:
2571 return true;
2572
2573 default:
2574 return false;
2575 }
2576}
2577
2578
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002579bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2580 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002581 return SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23;
2582}
2583
2584
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002585// Schedule this ASAP.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002586bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2587 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002588 if (mayBeCurLoad(MI1)) {
2589 // if (result of SU is used in Next) return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002590 unsigned DstReg = MI1.getOperand(0).getReg();
2591 int N = MI2.getNumOperands();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002592 for (int I = 0; I < N; I++)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002593 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002594 return true;
2595 }
2596 if (mayBeNewStore(MI2))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002597 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2598 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2599 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002600 return true;
2601 return false;
2602}
2603
2604
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002605bool HexagonInstrInfo::isV60VectorInstruction(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002606 const uint64_t V = getType(MI);
2607 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2608}
2609
2610
2611// Check if the Offset is a valid auto-inc imm by Load/Store Type.
2612//
2613bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2614 if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2615 VT == MVT::v32i16 || VT == MVT::v64i8) {
2616 return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2617 Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2618 (Offset & 0x3f) == 0);
2619 }
2620 // 128B
2621 if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2622 VT == MVT::v64i16 || VT == MVT::v128i8) {
2623 return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2624 Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2625 (Offset & 0x7f) == 0);
2626 }
2627 if (VT == MVT::i64) {
2628 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2629 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2630 (Offset & 0x7) == 0);
2631 }
2632 if (VT == MVT::i32) {
2633 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2634 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2635 (Offset & 0x3) == 0);
2636 }
2637 if (VT == MVT::i16) {
2638 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2639 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2640 (Offset & 0x1) == 0);
2641 }
2642 if (VT == MVT::i8) {
2643 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2644 Offset <= Hexagon_MEMB_AUTOINC_MAX);
2645 }
2646 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002647}
2648
2649
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002650bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2651 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002652 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002653 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002654 // inserted to calculate the final address. Due to this reason, the function
2655 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002656 // We used to assert if the offset was not properly aligned, however,
2657 // there are cases where a misaligned pointer recast can cause this
2658 // problem, and we need to allow for it. The front end warns of such
2659 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002660
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002661 switch (Opcode) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002662 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002663 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002664 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002665 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002666 case Hexagon::V6_vL32b_ai:
2667 case Hexagon::V6_vS32b_ai:
2668 case Hexagon::V6_vL32Ub_ai:
2669 case Hexagon::V6_vS32Ub_ai:
2670 return (Offset >= Hexagon_MEMV_OFFSET_MIN) &&
2671 (Offset <= Hexagon_MEMV_OFFSET_MAX);
2672
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002673 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002674 case Hexagon::PS_vstorerw_ai_128B:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002675 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002676 case Hexagon::PS_vloadrw_ai_128B:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002677 case Hexagon::V6_vL32b_ai_128B:
2678 case Hexagon::V6_vS32b_ai_128B:
2679 case Hexagon::V6_vL32Ub_ai_128B:
2680 case Hexagon::V6_vS32Ub_ai_128B:
2681 return (Offset >= Hexagon_MEMV_OFFSET_MIN_128B) &&
2682 (Offset <= Hexagon_MEMV_OFFSET_MAX_128B);
2683
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002684 case Hexagon::J2_loop0i:
2685 case Hexagon::J2_loop1i:
2686 return isUInt<10>(Offset);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00002687
2688 case Hexagon::S4_storeirb_io:
2689 case Hexagon::S4_storeirbt_io:
2690 case Hexagon::S4_storeirbf_io:
2691 return isUInt<6>(Offset);
2692
2693 case Hexagon::S4_storeirh_io:
2694 case Hexagon::S4_storeirht_io:
2695 case Hexagon::S4_storeirhf_io:
2696 return isShiftedUInt<6,1>(Offset);
2697
2698 case Hexagon::S4_storeiri_io:
2699 case Hexagon::S4_storeirit_io:
2700 case Hexagon::S4_storeirif_io:
2701 return isShiftedUInt<6,2>(Offset);
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002702 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002703
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002704 if (Extend)
2705 return true;
2706
2707 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002708 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002709 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002710 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2711 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2712
Colin LeMahieu947cd702014-12-23 20:44:59 +00002713 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002714 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002715 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2716 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2717
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002718 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002719 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002720 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002721 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2722 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2723
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002724 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002725 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002726 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002727 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2728 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2729
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002730 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002731 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2732 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2733
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002734 case Hexagon::L4_iadd_memopw_io :
2735 case Hexagon::L4_isub_memopw_io :
2736 case Hexagon::L4_add_memopw_io :
2737 case Hexagon::L4_sub_memopw_io :
2738 case Hexagon::L4_and_memopw_io :
2739 case Hexagon::L4_or_memopw_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002740 return (0 <= Offset && Offset <= 255);
2741
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002742 case Hexagon::L4_iadd_memoph_io :
2743 case Hexagon::L4_isub_memoph_io :
2744 case Hexagon::L4_add_memoph_io :
2745 case Hexagon::L4_sub_memoph_io :
2746 case Hexagon::L4_and_memoph_io :
2747 case Hexagon::L4_or_memoph_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002748 return (0 <= Offset && Offset <= 127);
2749
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002750 case Hexagon::L4_iadd_memopb_io :
2751 case Hexagon::L4_isub_memopb_io :
2752 case Hexagon::L4_add_memopb_io :
2753 case Hexagon::L4_sub_memopb_io :
2754 case Hexagon::L4_and_memopb_io :
2755 case Hexagon::L4_or_memopb_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002756 return (0 <= Offset && Offset <= 63);
2757
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002758 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002759 // any size. Later pass knows how to handle it.
2760 case Hexagon::STriw_pred:
2761 case Hexagon::LDriw_pred:
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00002762 case Hexagon::STriw_mod:
2763 case Hexagon::LDriw_mod:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002764 return true;
2765
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002766 case Hexagon::PS_fi:
2767 case Hexagon::PS_fia:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002768 case Hexagon::INLINEASM:
2769 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002770
2771 case Hexagon::L2_ploadrbt_io:
2772 case Hexagon::L2_ploadrbf_io:
2773 case Hexagon::L2_ploadrubt_io:
2774 case Hexagon::L2_ploadrubf_io:
2775 case Hexagon::S2_pstorerbt_io:
2776 case Hexagon::S2_pstorerbf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002777 return isUInt<6>(Offset);
2778
2779 case Hexagon::L2_ploadrht_io:
2780 case Hexagon::L2_ploadrhf_io:
2781 case Hexagon::L2_ploadruht_io:
2782 case Hexagon::L2_ploadruhf_io:
2783 case Hexagon::S2_pstorerht_io:
2784 case Hexagon::S2_pstorerhf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002785 return isShiftedUInt<6,1>(Offset);
2786
2787 case Hexagon::L2_ploadrit_io:
2788 case Hexagon::L2_ploadrif_io:
2789 case Hexagon::S2_pstorerit_io:
2790 case Hexagon::S2_pstorerif_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002791 return isShiftedUInt<6,2>(Offset);
2792
2793 case Hexagon::L2_ploadrdt_io:
2794 case Hexagon::L2_ploadrdf_io:
2795 case Hexagon::S2_pstorerdt_io:
2796 case Hexagon::S2_pstorerdf_io:
2797 return isShiftedUInt<6,3>(Offset);
2798 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002799
Benjamin Kramerb6684012011-12-27 11:41:05 +00002800 llvm_unreachable("No offset range is defined for this opcode. "
2801 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002802}
2803
2804
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002805bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
2806 return isV60VectorInstruction(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002807}
2808
2809
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002810bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2811 const uint64_t F = get(MI.getOpcode()).TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002812 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2813 return
2814 V == HexagonII::TypeCVI_VA ||
2815 V == HexagonII::TypeCVI_VA_DV;
2816}
Andrew Trickd06df962012-02-01 22:13:57 +00002817
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002818
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002819bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2820 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002821 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2822 return true;
2823
2824 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2825 return true;
2826
2827 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002828 return true;
2829
2830 return false;
2831}
Jyotsna Verma84256432013-03-01 17:37:13 +00002832
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002833bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2834 switch (MI.getOpcode()) {
2835 // Byte
2836 case Hexagon::L2_loadrub_io:
2837 case Hexagon::L4_loadrub_ur:
2838 case Hexagon::L4_loadrub_ap:
2839 case Hexagon::L2_loadrub_pr:
2840 case Hexagon::L2_loadrub_pbr:
2841 case Hexagon::L2_loadrub_pi:
2842 case Hexagon::L2_loadrub_pci:
2843 case Hexagon::L2_loadrub_pcr:
2844 case Hexagon::L2_loadbzw2_io:
2845 case Hexagon::L4_loadbzw2_ur:
2846 case Hexagon::L4_loadbzw2_ap:
2847 case Hexagon::L2_loadbzw2_pr:
2848 case Hexagon::L2_loadbzw2_pbr:
2849 case Hexagon::L2_loadbzw2_pi:
2850 case Hexagon::L2_loadbzw2_pci:
2851 case Hexagon::L2_loadbzw2_pcr:
2852 case Hexagon::L2_loadbzw4_io:
2853 case Hexagon::L4_loadbzw4_ur:
2854 case Hexagon::L4_loadbzw4_ap:
2855 case Hexagon::L2_loadbzw4_pr:
2856 case Hexagon::L2_loadbzw4_pbr:
2857 case Hexagon::L2_loadbzw4_pi:
2858 case Hexagon::L2_loadbzw4_pci:
2859 case Hexagon::L2_loadbzw4_pcr:
2860 case Hexagon::L4_loadrub_rr:
2861 case Hexagon::L2_ploadrubt_io:
2862 case Hexagon::L2_ploadrubt_pi:
2863 case Hexagon::L2_ploadrubf_io:
2864 case Hexagon::L2_ploadrubf_pi:
2865 case Hexagon::L2_ploadrubtnew_io:
2866 case Hexagon::L2_ploadrubfnew_io:
2867 case Hexagon::L4_ploadrubt_rr:
2868 case Hexagon::L4_ploadrubf_rr:
2869 case Hexagon::L4_ploadrubtnew_rr:
2870 case Hexagon::L4_ploadrubfnew_rr:
2871 case Hexagon::L2_ploadrubtnew_pi:
2872 case Hexagon::L2_ploadrubfnew_pi:
2873 case Hexagon::L4_ploadrubt_abs:
2874 case Hexagon::L4_ploadrubf_abs:
2875 case Hexagon::L4_ploadrubtnew_abs:
2876 case Hexagon::L4_ploadrubfnew_abs:
2877 case Hexagon::L2_loadrubgp:
2878 // Half
2879 case Hexagon::L2_loadruh_io:
2880 case Hexagon::L4_loadruh_ur:
2881 case Hexagon::L4_loadruh_ap:
2882 case Hexagon::L2_loadruh_pr:
2883 case Hexagon::L2_loadruh_pbr:
2884 case Hexagon::L2_loadruh_pi:
2885 case Hexagon::L2_loadruh_pci:
2886 case Hexagon::L2_loadruh_pcr:
2887 case Hexagon::L4_loadruh_rr:
2888 case Hexagon::L2_ploadruht_io:
2889 case Hexagon::L2_ploadruht_pi:
2890 case Hexagon::L2_ploadruhf_io:
2891 case Hexagon::L2_ploadruhf_pi:
2892 case Hexagon::L2_ploadruhtnew_io:
2893 case Hexagon::L2_ploadruhfnew_io:
2894 case Hexagon::L4_ploadruht_rr:
2895 case Hexagon::L4_ploadruhf_rr:
2896 case Hexagon::L4_ploadruhtnew_rr:
2897 case Hexagon::L4_ploadruhfnew_rr:
2898 case Hexagon::L2_ploadruhtnew_pi:
2899 case Hexagon::L2_ploadruhfnew_pi:
2900 case Hexagon::L4_ploadruht_abs:
2901 case Hexagon::L4_ploadruhf_abs:
2902 case Hexagon::L4_ploadruhtnew_abs:
2903 case Hexagon::L4_ploadruhfnew_abs:
2904 case Hexagon::L2_loadruhgp:
2905 return true;
2906 default:
2907 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002908 }
2909}
2910
2911
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002912// Add latency to instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002913bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2914 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002915 if (isV60VectorInstruction(MI1) && isV60VectorInstruction(MI2))
2916 if (!isVecUsableNextPacket(MI1, MI2))
2917 return true;
2918 return false;
2919}
2920
2921
Brendon Cahoon254f8892016-07-29 16:44:44 +00002922/// \brief Get the base register and byte offset of a load/store instr.
2923bool HexagonInstrInfo::getMemOpBaseRegImmOfs(MachineInstr &LdSt,
2924 unsigned &BaseReg, int64_t &Offset, const TargetRegisterInfo *TRI)
2925 const {
2926 unsigned AccessSize = 0;
2927 int OffsetVal = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002928 BaseReg = getBaseAndOffset(LdSt, OffsetVal, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002929 Offset = OffsetVal;
2930 return BaseReg != 0;
2931}
2932
2933
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002934/// \brief Can these instructions execute at the same time in a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002935bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2936 const MachineInstr &Second) const {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002937 if (DisableNVSchedule)
2938 return false;
2939 if (mayBeNewStore(Second)) {
2940 // Make sure the definition of the first instruction is the value being
2941 // stored.
2942 const MachineOperand &Stored =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002943 Second.getOperand(Second.getNumOperands() - 1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002944 if (!Stored.isReg())
2945 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002946 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
2947 const MachineOperand &Op = First.getOperand(i);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002948 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2949 return true;
2950 }
2951 }
2952 return false;
2953}
2954
2955
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002956bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
2957 unsigned Opc = CallMI.getOpcode();
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002958 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002959}
2960
2961
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002962bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2963 for (auto &I : *B)
2964 if (I.isEHLabel())
2965 return true;
2966 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002967}
2968
Jyotsna Verma84256432013-03-01 17:37:13 +00002969
2970// Returns true if an instruction can be converted into a non-extended
2971// equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002972bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002973 short NonExtOpcode;
2974 // Check if the instruction has a register form that uses register in place
2975 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002976 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
Jyotsna Verma84256432013-03-01 17:37:13 +00002977 return true;
2978
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002979 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002980 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002981
2982 switch (getAddrMode(MI)) {
2983 case HexagonII::Absolute :
2984 // Load/store with absolute addressing mode can be converted into
2985 // base+offset mode.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002986 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002987 break;
2988 case HexagonII::BaseImmOffset :
2989 // Load/store with base+offset addressing mode can be converted into
2990 // base+register offset addressing mode. However left shift operand should
2991 // be set to 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002992 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002993 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002994 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002995 NonExtOpcode = Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002996 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002997 default:
2998 return false;
2999 }
3000 if (NonExtOpcode < 0)
3001 return false;
3002 return true;
3003 }
3004 return false;
3005}
3006
Jyotsna Verma84256432013-03-01 17:37:13 +00003007
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003008bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
3009 return Hexagon::getRealHWInstr(MI.getOpcode(),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003010 Hexagon::InstrType_Pseudo) >= 0;
3011}
3012
3013
3014bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
3015 const {
3016 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
3017 while (I != E) {
3018 if (I->isBarrier())
3019 return true;
3020 ++I;
3021 }
3022 return false;
3023}
3024
3025
3026// Returns true, if a LD insn can be promoted to a cur load.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003027bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
3028 auto &HST = MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>();
3029 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003030 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
3031 HST.hasV60TOps();
3032}
3033
3034
3035// Returns true, if a ST insn can be promoted to a new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003036bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
3037 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003038 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
3039}
3040
3041
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003042bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
3043 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003044 // There is no stall when ProdMI is not a V60 vector.
3045 if (!isV60VectorInstruction(ProdMI))
3046 return false;
3047
3048 // There is no stall when ProdMI and ConsMI are not dependent.
3049 if (!isDependent(ProdMI, ConsMI))
3050 return false;
3051
3052 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
3053 // are scheduled in consecutive packets.
3054 if (isVecUsableNextPacket(ProdMI, ConsMI))
3055 return false;
3056
3057 return true;
3058}
3059
3060
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003061bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003062 MachineBasicBlock::const_instr_iterator BII) const {
3063 // There is no stall when I is not a V60 vector.
3064 if (!isV60VectorInstruction(MI))
3065 return false;
3066
3067 MachineBasicBlock::const_instr_iterator MII = BII;
3068 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
3069
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003070 if (!MII->isBundle()) {
3071 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003072 if (!isV60VectorInstruction(J))
3073 return false;
3074 else if (isVecUsableNextPacket(J, MI))
3075 return false;
3076 return true;
3077 }
3078
3079 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003080 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003081 if (producesStall(J, MI))
3082 return true;
3083 }
3084 return false;
3085}
3086
3087
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003088bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003089 unsigned PredReg) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003090 for (unsigned opNum = 0; opNum < MI.getNumOperands(); opNum++) {
3091 const MachineOperand &MO = MI.getOperand(opNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003092 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
3093 return false; // Predicate register must be explicitly defined.
3094 }
3095
3096 // Hexagon Programmer's Reference says that decbin, memw_locked, and
3097 // memd_locked cannot be used as .new as well,
3098 // but we don't seem to have these instructions defined.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003099 return MI.getOpcode() != Hexagon::A4_tlbmatch;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003100}
3101
3102
3103bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
3104 return (Opcode == Hexagon::J2_jumpt) ||
3105 (Opcode == Hexagon::J2_jumpf) ||
3106 (Opcode == Hexagon::J2_jumptnew) ||
3107 (Opcode == Hexagon::J2_jumpfnew) ||
3108 (Opcode == Hexagon::J2_jumptnewpt) ||
3109 (Opcode == Hexagon::J2_jumpfnewpt);
3110}
3111
3112
3113bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
3114 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
3115 return false;
3116 return !isPredicatedTrue(Cond[0].getImm());
3117}
3118
3119
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003120short HexagonInstrInfo::getAbsoluteForm(const MachineInstr &MI) const {
3121 return Hexagon::getAbsoluteForm(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003122}
3123
3124
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003125unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
3126 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003127 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3128}
3129
3130
3131// Returns the base register in a memory access (load/store). The offset is
3132// returned in Offset and the access size is returned in AccessSize.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003133unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003134 int &Offset, unsigned &AccessSize) const {
3135 // Return if it is not a base+offset type instruction or a MemOp.
3136 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3137 getAddrMode(MI) != HexagonII::BaseLongOffset &&
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003138 !isMemOp(MI) && !isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003139 return 0;
3140
3141 // Since it is a memory access instruction, getMemAccessSize() should never
3142 // return 0.
3143 assert (getMemAccessSize(MI) &&
3144 "BaseImmOffset or BaseLongOffset or MemOp without accessSize");
3145
3146 // Return Values of getMemAccessSize() are
3147 // 0 - Checked in the assert above.
3148 // 1, 2, 3, 4 & 7, 8 - The statement below is correct for all these.
3149 // MemAccessSize is represented as 1+log2(N) where N is size in bits.
3150 AccessSize = (1U << (getMemAccessSize(MI) - 1));
3151
3152 unsigned basePos = 0, offsetPos = 0;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003153 if (!getBaseAndOffsetPosition(MI, basePos, offsetPos))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003154 return 0;
3155
3156 // Post increment updates its EA after the mem access,
3157 // so we need to treat its offset as zero.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003158 if (isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003159 Offset = 0;
3160 else {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003161 Offset = MI.getOperand(offsetPos).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003162 }
3163
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003164 return MI.getOperand(basePos).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003165}
3166
3167
3168/// Return the position of the base and offset operands for this instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003169bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003170 unsigned &BasePos, unsigned &OffsetPos) const {
3171 // Deal with memops first.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003172 if (isMemOp(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003173 BasePos = 0;
3174 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003175 } else if (MI.mayStore()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003176 BasePos = 0;
3177 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003178 } else if (MI.mayLoad()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003179 BasePos = 1;
3180 OffsetPos = 2;
3181 } else
3182 return false;
3183
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003184 if (isPredicated(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003185 BasePos++;
3186 OffsetPos++;
3187 }
3188 if (isPostIncrement(MI)) {
3189 BasePos++;
3190 OffsetPos++;
3191 }
3192
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003193 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003194 return false;
3195
3196 return true;
3197}
3198
3199
3200// Inserts branching instructions in reverse order of their occurence.
3201// e.g. jump_t t1 (i1)
3202// jump t2 (i2)
3203// Jumpers = {i2, i1}
3204SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3205 MachineBasicBlock& MBB) const {
3206 SmallVector<MachineInstr*, 2> Jumpers;
3207 // If the block has no terminators, it just falls into the block after it.
3208 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3209 if (I == MBB.instr_begin())
3210 return Jumpers;
3211
3212 // A basic block may looks like this:
3213 //
3214 // [ insn
3215 // EH_LABEL
3216 // insn
3217 // insn
3218 // insn
3219 // EH_LABEL
3220 // insn ]
3221 //
3222 // It has two succs but does not have a terminator
3223 // Don't know how to handle it.
3224 do {
3225 --I;
3226 if (I->isEHLabel())
3227 return Jumpers;
3228 } while (I != MBB.instr_begin());
3229
3230 I = MBB.instr_end();
3231 --I;
3232
3233 while (I->isDebugValue()) {
3234 if (I == MBB.instr_begin())
3235 return Jumpers;
3236 --I;
3237 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003238 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003239 return Jumpers;
3240
3241 // Get the last instruction in the block.
3242 MachineInstr *LastInst = &*I;
3243 Jumpers.push_back(LastInst);
3244 MachineInstr *SecondLastInst = nullptr;
3245 // Find one more terminator if present.
3246 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003247 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003248 if (!SecondLastInst) {
3249 SecondLastInst = &*I;
3250 Jumpers.push_back(SecondLastInst);
3251 } else // This is a third branch.
3252 return Jumpers;
3253 }
3254 if (I == MBB.instr_begin())
3255 break;
3256 --I;
3257 } while (true);
3258 return Jumpers;
3259}
3260
3261
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003262short HexagonInstrInfo::getBaseWithLongOffset(short Opcode) const {
3263 if (Opcode < 0)
3264 return -1;
3265 return Hexagon::getBaseWithLongOffset(Opcode);
3266}
3267
3268
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003269short HexagonInstrInfo::getBaseWithLongOffset(const MachineInstr &MI) const {
3270 return Hexagon::getBaseWithLongOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003271}
3272
3273
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003274short HexagonInstrInfo::getBaseWithRegOffset(const MachineInstr &MI) const {
3275 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003276}
3277
3278
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003279// Returns Operand Index for the constant extended instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003280unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
3281 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003282 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3283}
3284
3285// See if instruction could potentially be a duplex candidate.
3286// If so, return its group. Zero otherwise.
3287HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003288 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003289 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3290
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003291 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003292 default:
3293 return HexagonII::HCG_None;
3294 //
3295 // Compound pairs.
3296 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3297 // "Rd16=#U6 ; jump #r9:2"
3298 // "Rd16=Rs16 ; jump #r9:2"
3299 //
3300 case Hexagon::C2_cmpeq:
3301 case Hexagon::C2_cmpgt:
3302 case Hexagon::C2_cmpgtu:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003303 DstReg = MI.getOperand(0).getReg();
3304 Src1Reg = MI.getOperand(1).getReg();
3305 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003306 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3307 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3308 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3309 return HexagonII::HCG_A;
3310 break;
3311 case Hexagon::C2_cmpeqi:
3312 case Hexagon::C2_cmpgti:
3313 case Hexagon::C2_cmpgtui:
3314 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003315 DstReg = MI.getOperand(0).getReg();
3316 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003317 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3318 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003319 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3320 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3321 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003322 return HexagonII::HCG_A;
3323 break;
3324 case Hexagon::A2_tfr:
3325 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003326 DstReg = MI.getOperand(0).getReg();
3327 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003328 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3329 return HexagonII::HCG_A;
3330 break;
3331 case Hexagon::A2_tfrsi:
3332 // Rd = #u6
3333 // Do not test for #u6 size since the const is getting extended
3334 // regardless and compound could be formed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003335 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003336 if (isIntRegForSubInst(DstReg))
3337 return HexagonII::HCG_A;
3338 break;
3339 case Hexagon::S2_tstbit_i:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003340 DstReg = MI.getOperand(0).getReg();
3341 Src1Reg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003342 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3343 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003344 MI.getOperand(2).isImm() &&
3345 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003346 return HexagonII::HCG_A;
3347 break;
3348 // The fact that .new form is used pretty much guarantees
3349 // that predicate register will match. Nevertheless,
3350 // there could be some false positives without additional
3351 // checking.
3352 case Hexagon::J2_jumptnew:
3353 case Hexagon::J2_jumpfnew:
3354 case Hexagon::J2_jumptnewpt:
3355 case Hexagon::J2_jumpfnewpt:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003356 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003357 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3358 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3359 return HexagonII::HCG_B;
3360 break;
3361 // Transfer and jump:
3362 // Rd=#U6 ; jump #r9:2
3363 // Rd=Rs ; jump #r9:2
3364 // Do not test for jump range here.
3365 case Hexagon::J2_jump:
3366 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3367 return HexagonII::HCG_C;
3368 break;
3369 }
3370
3371 return HexagonII::HCG_None;
3372}
3373
3374
3375// Returns -1 when there is no opcode found.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003376unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3377 const MachineInstr &GB) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003378 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3379 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003380 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3381 (GB.getOpcode() != Hexagon::J2_jumptnew))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003382 return -1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003383 unsigned DestReg = GA.getOperand(0).getReg();
3384 if (!GB.readsRegister(DestReg))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003385 return -1;
3386 if (DestReg == Hexagon::P0)
3387 return Hexagon::J4_cmpeqi_tp0_jump_nt;
3388 if (DestReg == Hexagon::P1)
3389 return Hexagon::J4_cmpeqi_tp1_jump_nt;
3390 return -1;
3391}
3392
3393
3394int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3395 enum Hexagon::PredSense inPredSense;
3396 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3397 Hexagon::PredSense_true;
3398 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3399 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3400 return CondOpcode;
3401
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003402 llvm_unreachable("Unexpected predicable instruction");
3403}
3404
3405
3406// Return the cur value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003407int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3408 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003409 default: llvm_unreachable("Unknown .cur type");
3410 case Hexagon::V6_vL32b_pi:
3411 return Hexagon::V6_vL32b_cur_pi;
3412 case Hexagon::V6_vL32b_ai:
3413 return Hexagon::V6_vL32b_cur_ai;
3414 //128B
3415 case Hexagon::V6_vL32b_pi_128B:
3416 return Hexagon::V6_vL32b_cur_pi_128B;
3417 case Hexagon::V6_vL32b_ai_128B:
3418 return Hexagon::V6_vL32b_cur_ai_128B;
3419 }
3420 return 0;
3421}
3422
3423
3424
3425// The diagram below shows the steps involved in the conversion of a predicated
3426// store instruction to its .new predicated new-value form.
3427//
3428// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3429// ^ ^
3430// / \ (not OK. it will cause new-value store to be
3431// / X conditional on p0.new while R2 producer is
3432// / \ on p0)
3433// / \.
3434// p.new store p.old NV store
3435// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3436// ^ ^
3437// \ /
3438// \ /
3439// \ /
3440// p.old store
3441// [if (p0)memw(R0+#0)=R2]
3442//
3443//
3444// The following set of instructions further explains the scenario where
3445// conditional new-value store becomes invalid when promoted to .new predicate
3446// form.
3447//
3448// { 1) if (p0) r0 = add(r1, r2)
3449// 2) p0 = cmp.eq(r3, #0) }
3450//
3451// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3452// the first two instructions because in instr 1, r0 is conditional on old value
3453// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3454// is not valid for new-value stores.
3455// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3456// from the "Conditional Store" list. Because a predicated new value store
3457// would NOT be promoted to a double dot new store. See diagram below:
3458// This function returns yes for those stores that are predicated but not
3459// yet promoted to predicate dot new instructions.
3460//
3461// +---------------------+
3462// /-----| if (p0) memw(..)=r0 |---------\~
3463// || +---------------------+ ||
3464// promote || /\ /\ || promote
3465// || /||\ /||\ ||
3466// \||/ demote || \||/
3467// \/ || || \/
3468// +-------------------------+ || +-------------------------+
3469// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3470// +-------------------------+ || +-------------------------+
3471// || || ||
3472// || demote \||/
3473// promote || \/ NOT possible
3474// || || /\~
3475// \||/ || /||\~
3476// \/ || ||
3477// +-----------------------------+
3478// | if (p0.new) memw(..)=r0.new |
3479// +-----------------------------+
3480// Double Dot New Store
3481//
3482// Returns the most basic instruction for the .new predicated instructions and
3483// new-value stores.
3484// For example, all of the following instructions will be converted back to the
3485// same instruction:
3486// 1) if (p0.new) memw(R0+#0) = R1.new --->
3487// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3488// 3) if (p0.new) memw(R0+#0) = R1 --->
3489//
3490// To understand the translation of instruction 1 to its original form, consider
3491// a packet with 3 instructions.
3492// { p0 = cmp.eq(R0,R1)
3493// if (p0.new) R2 = add(R3, R4)
3494// R5 = add (R3, R1)
3495// }
3496// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3497//
3498// This instruction can be part of the previous packet only if both p0 and R2
3499// are promoted to .new values. This promotion happens in steps, first
3500// predicate register is promoted to .new and in the next iteration R2 is
3501// promoted. Therefore, in case of dependence check failure (due to R5) during
3502// next iteration, it should be converted back to its most basic form.
3503
3504
3505// Return the new value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003506int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3507 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003508 if (NVOpcode >= 0) // Valid new-value store instruction.
3509 return NVOpcode;
3510
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003511 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003512 default: llvm_unreachable("Unknown .new type");
3513 case Hexagon::S4_storerb_ur:
3514 return Hexagon::S4_storerbnew_ur;
3515
3516 case Hexagon::S2_storerb_pci:
3517 return Hexagon::S2_storerb_pci;
3518
3519 case Hexagon::S2_storeri_pci:
3520 return Hexagon::S2_storeri_pci;
3521
3522 case Hexagon::S2_storerh_pci:
3523 return Hexagon::S2_storerh_pci;
3524
3525 case Hexagon::S2_storerd_pci:
3526 return Hexagon::S2_storerd_pci;
3527
3528 case Hexagon::S2_storerf_pci:
3529 return Hexagon::S2_storerf_pci;
3530
3531 case Hexagon::V6_vS32b_ai:
3532 return Hexagon::V6_vS32b_new_ai;
3533
3534 case Hexagon::V6_vS32b_pi:
3535 return Hexagon::V6_vS32b_new_pi;
3536
3537 // 128B
3538 case Hexagon::V6_vS32b_ai_128B:
3539 return Hexagon::V6_vS32b_new_ai_128B;
3540
3541 case Hexagon::V6_vS32b_pi_128B:
3542 return Hexagon::V6_vS32b_new_pi_128B;
3543 }
3544 return 0;
3545}
3546
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00003547
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003548// Returns the opcode to use when converting MI, which is a conditional jump,
3549// into a conditional instruction which uses the .new value of the predicate.
3550// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003551int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003552 const MachineBranchProbabilityInfo *MBPI) const {
3553 // We assume that block can have at most two successors.
3554 bool taken = false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003555 const MachineBasicBlock *Src = MI.getParent();
3556 const MachineOperand &BrTarget = MI.getOperand(1);
3557 const MachineBasicBlock *Dst = BrTarget.getMBB();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003558
3559 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
3560 if (Prediction >= BranchProbability(1,2))
3561 taken = true;
3562
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003563 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003564 case Hexagon::J2_jumpt:
3565 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3566 case Hexagon::J2_jumpf:
3567 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3568
3569 default:
3570 llvm_unreachable("Unexpected jump instruction.");
3571 }
3572}
3573
3574
3575// Return .new predicate version for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003576int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003577 const MachineBranchProbabilityInfo *MBPI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003578 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003579 if (NewOpcode >= 0) // Valid predicate new instruction
3580 return NewOpcode;
3581
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003582 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003583 // Condtional Jumps
3584 case Hexagon::J2_jumpt:
3585 case Hexagon::J2_jumpf:
3586 return getDotNewPredJumpOp(MI, MBPI);
3587
3588 default:
3589 assert(0 && "Unknown .new type");
3590 }
3591 return 0;
3592}
3593
3594
3595int HexagonInstrInfo::getDotOldOp(const int opc) const {
3596 int NewOp = opc;
3597 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3598 NewOp = Hexagon::getPredOldOpcode(NewOp);
3599 assert(NewOp >= 0 &&
3600 "Couldn't change predicate new instruction to its old form.");
3601 }
3602
3603 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3604 NewOp = Hexagon::getNonNVStore(NewOp);
3605 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3606 }
3607 return NewOp;
3608}
3609
3610
3611// See if instruction could potentially be a duplex candidate.
3612// If so, return its group. Zero otherwise.
3613HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003614 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003615 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3616 auto &HRI = getRegisterInfo();
3617
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003618 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003619 default:
3620 return HexagonII::HSIG_None;
3621 //
3622 // Group L1:
3623 //
3624 // Rd = memw(Rs+#u4:2)
3625 // Rd = memub(Rs+#u4:0)
3626 case Hexagon::L2_loadri_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003627 DstReg = MI.getOperand(0).getReg();
3628 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003629 // Special case this one from Group L2.
3630 // Rd = memw(r29+#u5:2)
3631 if (isIntRegForSubInst(DstReg)) {
3632 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3633 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003634 MI.getOperand(2).isImm() &&
3635 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003636 return HexagonII::HSIG_L2;
3637 // Rd = memw(Rs+#u4:2)
3638 if (isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003639 (MI.getOperand(2).isImm() &&
3640 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003641 return HexagonII::HSIG_L1;
3642 }
3643 break;
3644 case Hexagon::L2_loadrub_io:
3645 // Rd = memub(Rs+#u4:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003646 DstReg = MI.getOperand(0).getReg();
3647 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003648 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003649 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003650 return HexagonII::HSIG_L1;
3651 break;
3652 //
3653 // Group L2:
3654 //
3655 // Rd = memh/memuh(Rs+#u3:1)
3656 // Rd = memb(Rs+#u3:0)
3657 // Rd = memw(r29+#u5:2) - Handled above.
3658 // Rdd = memd(r29+#u5:3)
3659 // deallocframe
3660 // [if ([!]p0[.new])] dealloc_return
3661 // [if ([!]p0[.new])] jumpr r31
3662 case Hexagon::L2_loadrh_io:
3663 case Hexagon::L2_loadruh_io:
3664 // Rd = memh/memuh(Rs+#u3:1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003665 DstReg = MI.getOperand(0).getReg();
3666 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003667 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003668 MI.getOperand(2).isImm() &&
3669 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003670 return HexagonII::HSIG_L2;
3671 break;
3672 case Hexagon::L2_loadrb_io:
3673 // Rd = memb(Rs+#u3:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003674 DstReg = MI.getOperand(0).getReg();
3675 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003676 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003677 MI.getOperand(2).isImm() &&
3678 isUInt<3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003679 return HexagonII::HSIG_L2;
3680 break;
3681 case Hexagon::L2_loadrd_io:
3682 // Rdd = memd(r29+#u5:3)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003683 DstReg = MI.getOperand(0).getReg();
3684 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003685 if (isDblRegForSubInst(DstReg, HRI) &&
3686 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3687 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003688 MI.getOperand(2).isImm() &&
3689 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003690 return HexagonII::HSIG_L2;
3691 break;
3692 // dealloc_return is not documented in Hexagon Manual, but marked
3693 // with A_SUBINSN attribute in iset_v4classic.py.
3694 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3695 case Hexagon::L4_return:
3696 case Hexagon::L2_deallocframe:
3697 return HexagonII::HSIG_L2;
3698 case Hexagon::EH_RETURN_JMPR:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003699 case Hexagon::PS_jmpret:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003700 // jumpr r31
3701 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003702 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003703 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3704 return HexagonII::HSIG_L2;
3705 break;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003706 case Hexagon::PS_jmprett:
3707 case Hexagon::PS_jmpretf:
3708 case Hexagon::PS_jmprettnewpt:
3709 case Hexagon::PS_jmpretfnewpt:
3710 case Hexagon::PS_jmprettnew:
3711 case Hexagon::PS_jmpretfnew:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003712 DstReg = MI.getOperand(1).getReg();
3713 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003714 // [if ([!]p0[.new])] jumpr r31
3715 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3716 (Hexagon::P0 == SrcReg)) &&
3717 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3718 return HexagonII::HSIG_L2;
3719 break;
3720 case Hexagon::L4_return_t :
3721 case Hexagon::L4_return_f :
3722 case Hexagon::L4_return_tnew_pnt :
3723 case Hexagon::L4_return_fnew_pnt :
3724 case Hexagon::L4_return_tnew_pt :
3725 case Hexagon::L4_return_fnew_pt :
3726 // [if ([!]p0[.new])] dealloc_return
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003727 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003728 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3729 return HexagonII::HSIG_L2;
3730 break;
3731 //
3732 // Group S1:
3733 //
3734 // memw(Rs+#u4:2) = Rt
3735 // memb(Rs+#u4:0) = Rt
3736 case Hexagon::S2_storeri_io:
3737 // Special case this one from Group S2.
3738 // memw(r29+#u5:2) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003739 Src1Reg = MI.getOperand(0).getReg();
3740 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003741 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3742 isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003743 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3744 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003745 return HexagonII::HSIG_S2;
3746 // memw(Rs+#u4:2) = Rt
3747 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003748 MI.getOperand(1).isImm() &&
3749 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003750 return HexagonII::HSIG_S1;
3751 break;
3752 case Hexagon::S2_storerb_io:
3753 // memb(Rs+#u4:0) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003754 Src1Reg = MI.getOperand(0).getReg();
3755 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003756 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003757 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003758 return HexagonII::HSIG_S1;
3759 break;
3760 //
3761 // Group S2:
3762 //
3763 // memh(Rs+#u3:1) = Rt
3764 // memw(r29+#u5:2) = Rt
3765 // memd(r29+#s6:3) = Rtt
3766 // memw(Rs+#u4:2) = #U1
3767 // memb(Rs+#u4) = #U1
3768 // allocframe(#u5:3)
3769 case Hexagon::S2_storerh_io:
3770 // memh(Rs+#u3:1) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003771 Src1Reg = MI.getOperand(0).getReg();
3772 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003773 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003774 MI.getOperand(1).isImm() &&
3775 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003776 return HexagonII::HSIG_S1;
3777 break;
3778 case Hexagon::S2_storerd_io:
3779 // memd(r29+#s6:3) = Rtt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003780 Src1Reg = MI.getOperand(0).getReg();
3781 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003782 if (isDblRegForSubInst(Src2Reg, HRI) &&
3783 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003784 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3785 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003786 return HexagonII::HSIG_S2;
3787 break;
3788 case Hexagon::S4_storeiri_io:
3789 // memw(Rs+#u4:2) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003790 Src1Reg = MI.getOperand(0).getReg();
3791 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
3792 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
3793 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003794 return HexagonII::HSIG_S2;
3795 break;
3796 case Hexagon::S4_storeirb_io:
3797 // memb(Rs+#u4) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003798 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003799 if (isIntRegForSubInst(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003800 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
3801 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003802 return HexagonII::HSIG_S2;
3803 break;
3804 case Hexagon::S2_allocframe:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003805 if (MI.getOperand(0).isImm() &&
3806 isShiftedUInt<5,3>(MI.getOperand(0).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003807 return HexagonII::HSIG_S1;
3808 break;
3809 //
3810 // Group A:
3811 //
3812 // Rx = add(Rx,#s7)
3813 // Rd = Rs
3814 // Rd = #u6
3815 // Rd = #-1
3816 // if ([!]P0[.new]) Rd = #0
3817 // Rd = add(r29,#u6:2)
3818 // Rx = add(Rx,Rs)
3819 // P0 = cmp.eq(Rs,#u2)
3820 // Rdd = combine(#0,Rs)
3821 // Rdd = combine(Rs,#0)
3822 // Rdd = combine(#u2,#U2)
3823 // Rd = add(Rs,#1)
3824 // Rd = add(Rs,#-1)
3825 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3826 // Rd = and(Rs,#1)
3827 case Hexagon::A2_addi:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003828 DstReg = MI.getOperand(0).getReg();
3829 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003830 if (isIntRegForSubInst(DstReg)) {
3831 // Rd = add(r29,#u6:2)
3832 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003833 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
3834 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003835 return HexagonII::HSIG_A;
3836 // Rx = add(Rx,#s7)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003837 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
3838 isInt<7>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003839 return HexagonII::HSIG_A;
3840 // Rd = add(Rs,#1)
3841 // Rd = add(Rs,#-1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003842 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3843 ((MI.getOperand(2).getImm() == 1) ||
3844 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003845 return HexagonII::HSIG_A;
3846 }
3847 break;
3848 case Hexagon::A2_add:
3849 // Rx = add(Rx,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003850 DstReg = MI.getOperand(0).getReg();
3851 Src1Reg = MI.getOperand(1).getReg();
3852 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003853 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3854 isIntRegForSubInst(Src2Reg))
3855 return HexagonII::HSIG_A;
3856 break;
3857 case Hexagon::A2_andir:
3858 // Same as zxtb.
3859 // Rd16=and(Rs16,#255)
3860 // Rd16=and(Rs16,#1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003861 DstReg = MI.getOperand(0).getReg();
3862 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003863 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003864 MI.getOperand(2).isImm() &&
3865 ((MI.getOperand(2).getImm() == 1) ||
3866 (MI.getOperand(2).getImm() == 255)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003867 return HexagonII::HSIG_A;
3868 break;
3869 case Hexagon::A2_tfr:
3870 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003871 DstReg = MI.getOperand(0).getReg();
3872 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003873 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3874 return HexagonII::HSIG_A;
3875 break;
3876 case Hexagon::A2_tfrsi:
3877 // Rd = #u6
3878 // Do not test for #u6 size since the const is getting extended
3879 // regardless and compound could be formed.
3880 // Rd = #-1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003881 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003882 if (isIntRegForSubInst(DstReg))
3883 return HexagonII::HSIG_A;
3884 break;
3885 case Hexagon::C2_cmoveit:
3886 case Hexagon::C2_cmovenewit:
3887 case Hexagon::C2_cmoveif:
3888 case Hexagon::C2_cmovenewif:
3889 // if ([!]P0[.new]) Rd = #0
3890 // Actual form:
3891 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003892 DstReg = MI.getOperand(0).getReg();
3893 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003894 if (isIntRegForSubInst(DstReg) &&
3895 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003896 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003897 return HexagonII::HSIG_A;
3898 break;
3899 case Hexagon::C2_cmpeqi:
3900 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003901 DstReg = MI.getOperand(0).getReg();
3902 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003903 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3904 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003905 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003906 return HexagonII::HSIG_A;
3907 break;
3908 case Hexagon::A2_combineii:
3909 case Hexagon::A4_combineii:
3910 // Rdd = combine(#u2,#U2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003911 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003912 if (isDblRegForSubInst(DstReg, HRI) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003913 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
3914 (MI.getOperand(1).isGlobal() &&
3915 isUInt<2>(MI.getOperand(1).getOffset()))) &&
3916 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
3917 (MI.getOperand(2).isGlobal() &&
3918 isUInt<2>(MI.getOperand(2).getOffset()))))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003919 return HexagonII::HSIG_A;
3920 break;
3921 case Hexagon::A4_combineri:
3922 // Rdd = combine(Rs,#0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003923 DstReg = MI.getOperand(0).getReg();
3924 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003925 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003926 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
3927 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003928 return HexagonII::HSIG_A;
3929 break;
3930 case Hexagon::A4_combineir:
3931 // Rdd = combine(#0,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003932 DstReg = MI.getOperand(0).getReg();
3933 SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003934 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003935 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
3936 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003937 return HexagonII::HSIG_A;
3938 break;
3939 case Hexagon::A2_sxtb:
3940 case Hexagon::A2_sxth:
3941 case Hexagon::A2_zxtb:
3942 case Hexagon::A2_zxth:
3943 // Rd = sxth/sxtb/zxtb/zxth(Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003944 DstReg = MI.getOperand(0).getReg();
3945 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003946 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3947 return HexagonII::HSIG_A;
3948 break;
3949 }
3950
3951 return HexagonII::HSIG_None;
3952}
3953
3954
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003955short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
3956 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003957}
3958
3959
3960// Return first non-debug instruction in the basic block.
3961MachineInstr *HexagonInstrInfo::getFirstNonDbgInst(MachineBasicBlock *BB)
3962 const {
3963 for (auto MII = BB->instr_begin(), End = BB->instr_end(); MII != End; MII++) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003964 MachineInstr &MI = *MII;
3965 if (MI.isDebugValue())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003966 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003967 return &MI;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003968 }
3969 return nullptr;
3970}
3971
3972
3973unsigned HexagonInstrInfo::getInstrTimingClassLatency(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003974 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003975 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3976 // still have a MinLatency property, which getStageLatency checks.
3977 if (!ItinData)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003978 return getInstrLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003979
3980 // Get the latency embedded in the itinerary. If we're not using timing class
3981 // latencies or if we using BSB scheduling, then restrict the maximum latency
3982 // to 1 (that is, either 0 or 1).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003983 if (MI.isTransient())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003984 return 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003985 unsigned Latency = ItinData->getStageLatency(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003986 if (!EnableTimingClassLatency ||
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003987 MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>().
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003988 useBSBScheduling())
3989 if (Latency > 1)
3990 Latency = 1;
3991 return Latency;
3992}
3993
3994
3995// inverts the predication logic.
3996// p -> NotP
3997// NotP -> P
3998bool HexagonInstrInfo::getInvertedPredSense(
3999 SmallVectorImpl<MachineOperand> &Cond) const {
4000 if (Cond.empty())
4001 return false;
4002 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
4003 Cond[0].setImm(Opc);
4004 return true;
4005}
4006
4007
4008unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
4009 int InvPredOpcode;
4010 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
4011 : Hexagon::getTruePredOpcode(Opc);
4012 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
4013 return InvPredOpcode;
4014
4015 llvm_unreachable("Unexpected predicated instruction");
4016}
4017
4018
4019// Returns the max value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004020int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
4021 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004022 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4023 & HexagonII::ExtentSignedMask;
4024 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4025 & HexagonII::ExtentBitsMask;
4026
4027 if (isSigned) // if value is signed
4028 return ~(-1U << (bits - 1));
4029 else
4030 return ~(-1U << bits);
4031}
4032
4033
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004034unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
4035 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004036 return (F >> HexagonII::MemAccessSizePos) & HexagonII::MemAccesSizeMask;
4037}
4038
4039
4040// Returns the min value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004041int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
4042 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004043 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4044 & HexagonII::ExtentSignedMask;
4045 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4046 & HexagonII::ExtentBitsMask;
4047
4048 if (isSigned) // if value is signed
4049 return -1U << (bits - 1);
4050 else
4051 return 0;
4052}
4053
4054
4055// Returns opcode of the non-extended equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004056short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00004057 // Check if the instruction has a register form that uses register in place
4058 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004059 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004060 if (NonExtOpcode >= 0)
4061 return NonExtOpcode;
4062
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004063 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00004064 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00004065 switch (getAddrMode(MI)) {
4066 case HexagonII::Absolute :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004067 return Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004068 case HexagonII::BaseImmOffset :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004069 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004070 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004071 return Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004072
Jyotsna Verma84256432013-03-01 17:37:13 +00004073 default:
4074 return -1;
4075 }
4076 }
4077 return -1;
4078}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00004079
Brendon Cahoondf43e682015-05-08 16:16:29 +00004080
Ahmed Bougachac88bf542015-06-11 19:30:37 +00004081bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004082 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00004083 if (Cond.empty())
4084 return false;
4085 assert(Cond.size() == 2);
4086 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
4087 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
4088 return false;
4089 }
4090 PredReg = Cond[1].getReg();
4091 PredRegPos = 1;
4092 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4093 PredRegFlags = 0;
4094 if (Cond[1].isImplicit())
4095 PredRegFlags = RegState::Implicit;
4096 if (Cond[1].isUndef())
4097 PredRegFlags |= RegState::Undef;
4098 return true;
4099}
4100
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004101
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004102short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
4103 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004104}
4105
4106
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004107short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
4108 return Hexagon::getRegForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004109}
4110
4111
4112// Return the number of bytes required to encode the instruction.
4113// Hexagon instructions are fixed length, 4 bytes, unless they
4114// use a constant extender, which requires another 4 bytes.
4115// For debug instructions and prolog labels, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004116unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
4117 if (MI.isDebugValue() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004118 return 0;
4119
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004120 unsigned Size = MI.getDesc().getSize();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004121 if (!Size)
4122 // Assume the default insn size in case it cannot be determined
4123 // for whatever reason.
4124 Size = HEXAGON_INSTR_SIZE;
4125
4126 if (isConstExtended(MI) || isExtended(MI))
4127 Size += HEXAGON_INSTR_SIZE;
4128
4129 // Try and compute number of instructions in asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004130 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4131 const MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004132 const MachineFunction *MF = MBB.getParent();
4133 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4134
4135 // Count the number of register definitions to find the asm string.
4136 unsigned NumDefs = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004137 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004138 ++NumDefs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004139 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004140
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004141 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004142 // Disassemble the AsmStr and approximate number of instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004143 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004144 Size = getInlineAsmLength(AsmStr, *MAI);
4145 }
4146
4147 return Size;
4148}
4149
4150
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004151uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
4152 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004153 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4154}
4155
4156
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004157unsigned HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
4158 const TargetSubtargetInfo &ST = MI.getParent()->getParent()->getSubtarget();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004159 const InstrItineraryData &II = *ST.getInstrItineraryData();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004160 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004161
4162 return IS.getUnits();
4163}
4164
4165
4166unsigned HexagonInstrInfo::getValidSubTargets(const unsigned Opcode) const {
4167 const uint64_t F = get(Opcode).TSFlags;
4168 return (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask;
4169}
4170
4171
4172// Calculate size of the basic block without debug instructions.
4173unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4174 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4175}
4176
4177
4178unsigned HexagonInstrInfo::nonDbgBundleSize(
4179 MachineBasicBlock::const_iterator BundleHead) const {
4180 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004181 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004182 // Skip the bundle header.
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00004183 return nonDbgMICount(++MII, getBundleEnd(*BundleHead));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004184}
4185
4186
4187/// immediateExtend - Changes the instruction in place to one using an immediate
4188/// extender.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004189void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004190 assert((isExtendable(MI)||isConstExtended(MI)) &&
4191 "Instruction must be extendable");
4192 // Find which operand is extendable.
4193 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004194 MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004195 // This needs to be something we understand.
4196 assert((MO.isMBB() || MO.isImm()) &&
4197 "Branch with unknown extendable field type");
4198 // Mark given operand as extended.
4199 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4200}
4201
4202
4203bool HexagonInstrInfo::invertAndChangeJumpTarget(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004204 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004205 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004206 << NewTarget->getNumber(); MI.dump(););
4207 assert(MI.isBranch());
4208 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4209 int TargetPos = MI.getNumOperands() - 1;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004210 // In general branch target is the last operand,
4211 // but some implicit defs added at the end might change it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004212 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004213 --TargetPos;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004214 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4215 MI.getOperand(TargetPos).setMBB(NewTarget);
4216 if (EnableBranchPrediction && isPredicatedNew(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004217 NewOpcode = reversePrediction(NewOpcode);
4218 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004219 MI.setDesc(get(NewOpcode));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004220 return true;
4221}
4222
4223
4224void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4225 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4226 MachineFunction::iterator A = MF.begin();
4227 MachineBasicBlock &B = *A;
4228 MachineBasicBlock::iterator I = B.begin();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004229 DebugLoc DL = I->getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004230 MachineInstr *NewMI;
4231
4232 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4233 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004234 NewMI = BuildMI(B, I, DL, get(insn));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004235 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
4236 " Class: " << NewMI->getDesc().getSchedClass());
4237 NewMI->eraseFromParent();
4238 }
4239 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4240}
4241
4242
4243// inverts the predication logic.
4244// p -> NotP
4245// NotP -> P
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004246bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
4247 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
4248 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004249 return true;
4250}
4251
4252
4253// Reverse the branch prediction.
4254unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4255 int PredRevOpcode = -1;
4256 if (isPredictedTaken(Opcode))
4257 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4258 else
4259 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4260 assert(PredRevOpcode > 0);
4261 return PredRevOpcode;
4262}
4263
4264
4265// TODO: Add more rigorous validation.
4266bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4267 const {
4268 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4269}
4270
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004271
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004272short HexagonInstrInfo::xformRegToImmOffset(const MachineInstr &MI) const {
4273 return Hexagon::xformRegToImmOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004274}