blob: 6cdc810fdf5aa6421543677d18b409776db5d7f2 [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 Parzyszek4fa2a9f2015-04-22 16:43:53 +00001010 case Hexagon::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 Parzyszek4eb6d4d2015-11-26 16:54:33 +00001016 case Hexagon::HEXAGON_V6_vassignp_128B:
1017 case Hexagon::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 Parzyszek4eb6d4d2015-11-26 16:54:33 +00001020 if (SrcReg != DstReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001021 copyPhysReg(MBB, MI, DL, DstReg, SrcReg, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001022 MBB.erase(MI);
1023 return true;
1024 }
1025 case Hexagon::HEXAGON_V6_lo_128B:
1026 case Hexagon::HEXAGON_V6_lo: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001027 unsigned SrcReg = MI.getOperand(1).getReg();
1028 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001029 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001030 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001031 MBB.erase(MI);
1032 MRI.clearKillFlags(SrcSubLo);
1033 return true;
1034 }
1035 case Hexagon::HEXAGON_V6_hi_128B:
1036 case Hexagon::HEXAGON_V6_hi: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001037 unsigned SrcReg = MI.getOperand(1).getReg();
1038 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001039 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::subreg_hireg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001040 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001041 MBB.erase(MI);
1042 MRI.clearKillFlags(SrcSubHi);
1043 return true;
1044 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001045 case Hexagon::PS_vstorerw_ai:
1046 case Hexagon::PS_vstorerwu_ai:
1047 case Hexagon::PS_vstorerw_ai_128B:
1048 case Hexagon::PS_vstorerwu_ai_128B: {
1049 bool Is128B = (Opc == Hexagon::PS_vstorerw_ai_128B ||
1050 Opc == Hexagon::PS_vstorerwu_ai_128B);
1051 bool Aligned = (Opc == Hexagon::PS_vstorerw_ai ||
1052 Opc == Hexagon::PS_vstorerw_ai_128B);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001053 unsigned SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001054 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::subreg_hireg);
1055 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::subreg_loreg);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001056 unsigned NewOpc;
1057 if (Aligned)
1058 NewOpc = Is128B ? Hexagon::V6_vS32b_ai_128B
1059 : Hexagon::V6_vS32b_ai;
1060 else
1061 NewOpc = Is128B ? Hexagon::V6_vS32Ub_ai_128B
1062 : Hexagon::V6_vS32Ub_ai;
1063
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001064 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001065 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001066 BuildMI(MBB, MI, DL, get(NewOpc))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001067 .addOperand(MI.getOperand(0))
1068 .addImm(MI.getOperand(1).getImm())
1069 .addReg(SrcSubLo)
1070 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001071 MI1New->getOperand(0).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001072 BuildMI(MBB, MI, DL, get(NewOpc))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001073 .addOperand(MI.getOperand(0))
1074 // The Vectors are indexed in multiples of vector size.
1075 .addImm(MI.getOperand(1).getImm() + Offset)
1076 .addReg(SrcSubHi)
1077 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001078 MBB.erase(MI);
1079 return true;
1080 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001081 case Hexagon::PS_vloadrw_ai:
1082 case Hexagon::PS_vloadrwu_ai:
1083 case Hexagon::PS_vloadrw_ai_128B:
1084 case Hexagon::PS_vloadrwu_ai_128B: {
1085 bool Is128B = (Opc == Hexagon::PS_vloadrw_ai_128B ||
1086 Opc == Hexagon::PS_vloadrwu_ai_128B);
1087 bool Aligned = (Opc == Hexagon::PS_vloadrw_ai ||
1088 Opc == Hexagon::PS_vloadrw_ai_128B);
1089 unsigned NewOpc;
1090 if (Aligned)
1091 NewOpc = Is128B ? Hexagon::V6_vL32b_ai_128B
1092 : Hexagon::V6_vL32b_ai;
1093 else
1094 NewOpc = Is128B ? Hexagon::V6_vL32Ub_ai_128B
1095 : Hexagon::V6_vL32Ub_ai;
1096
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001097 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001098 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
1099 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001100 BuildMI(MBB, MI, DL, get(NewOpc),
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001101 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001102 .addOperand(MI.getOperand(1))
1103 .addImm(MI.getOperand(2).getImm());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001104 MI1New->getOperand(1).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001105 BuildMI(MBB, MI, DL, get(NewOpc),
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001106 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001107 .addOperand(MI.getOperand(1))
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001108 // The Vectors are indexed in multiples of vector size.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001109 .addImm(MI.getOperand(2).getImm() + Offset)
1110 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001111 MBB.erase(MI);
1112 return true;
1113 }
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001114 case Hexagon::TFR_PdTrue: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001115 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001116 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1117 .addReg(Reg, RegState::Undef)
1118 .addReg(Reg, RegState::Undef);
1119 MBB.erase(MI);
1120 return true;
1121 }
1122 case Hexagon::TFR_PdFalse: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001123 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001124 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1125 .addReg(Reg, RegState::Undef)
1126 .addReg(Reg, RegState::Undef);
1127 MBB.erase(MI);
1128 return true;
1129 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001130 case Hexagon::VMULW: {
1131 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001132 unsigned DstReg = MI.getOperand(0).getReg();
1133 unsigned Src1Reg = MI.getOperand(1).getReg();
1134 unsigned Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001135 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
1136 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
1137 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
1138 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001139 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
1140 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1141 .addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001142 .addReg(Src2SubHi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001143 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
1144 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1145 .addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001146 .addReg(Src2SubLo);
1147 MBB.erase(MI);
1148 MRI.clearKillFlags(Src1SubHi);
1149 MRI.clearKillFlags(Src1SubLo);
1150 MRI.clearKillFlags(Src2SubHi);
1151 MRI.clearKillFlags(Src2SubLo);
1152 return true;
1153 }
1154 case Hexagon::VMULW_ACC: {
1155 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001156 unsigned DstReg = MI.getOperand(0).getReg();
1157 unsigned Src1Reg = MI.getOperand(1).getReg();
1158 unsigned Src2Reg = MI.getOperand(2).getReg();
1159 unsigned Src3Reg = MI.getOperand(3).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001160 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
1161 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
1162 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
1163 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
1164 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::subreg_hireg);
1165 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001166 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
1167 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1168 .addReg(Src1SubHi)
1169 .addReg(Src2SubHi)
1170 .addReg(Src3SubHi);
1171 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
1172 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1173 .addReg(Src1SubLo)
1174 .addReg(Src2SubLo)
1175 .addReg(Src3SubLo);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001176 MBB.erase(MI);
1177 MRI.clearKillFlags(Src1SubHi);
1178 MRI.clearKillFlags(Src1SubLo);
1179 MRI.clearKillFlags(Src2SubHi);
1180 MRI.clearKillFlags(Src2SubLo);
1181 MRI.clearKillFlags(Src3SubHi);
1182 MRI.clearKillFlags(Src3SubLo);
1183 return true;
1184 }
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +00001185 case Hexagon::Insert4: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001186 unsigned DstReg = MI.getOperand(0).getReg();
1187 unsigned Src1Reg = MI.getOperand(1).getReg();
1188 unsigned Src2Reg = MI.getOperand(2).getReg();
1189 unsigned Src3Reg = MI.getOperand(3).getReg();
1190 unsigned Src4Reg = MI.getOperand(4).getReg();
1191 unsigned Src1RegIsKill = getKillRegState(MI.getOperand(1).isKill());
1192 unsigned Src2RegIsKill = getKillRegState(MI.getOperand(2).isKill());
1193 unsigned Src3RegIsKill = getKillRegState(MI.getOperand(3).isKill());
1194 unsigned Src4RegIsKill = getKillRegState(MI.getOperand(4).isKill());
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +00001195 unsigned DstSubHi = HRI.getSubReg(DstReg, Hexagon::subreg_hireg);
1196 unsigned DstSubLo = HRI.getSubReg(DstReg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001197 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1198 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1199 .addReg(DstSubLo)
1200 .addReg(Src1Reg, Src1RegIsKill)
1201 .addImm(16)
1202 .addImm(0);
1203 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1204 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1205 .addReg(DstSubLo)
1206 .addReg(Src2Reg, Src2RegIsKill)
1207 .addImm(16)
1208 .addImm(16);
1209 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1210 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1211 .addReg(DstSubHi)
1212 .addReg(Src3Reg, Src3RegIsKill)
1213 .addImm(16)
1214 .addImm(0);
1215 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1216 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1217 .addReg(DstSubHi)
1218 .addReg(Src4Reg, Src4RegIsKill)
1219 .addImm(16)
1220 .addImm(16);
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +00001221 MBB.erase(MI);
1222 MRI.clearKillFlags(DstReg);
1223 MRI.clearKillFlags(DstSubHi);
1224 MRI.clearKillFlags(DstSubLo);
1225 return true;
1226 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001227 case Hexagon::PS_pselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001228 const MachineOperand &Op0 = MI.getOperand(0);
1229 const MachineOperand &Op1 = MI.getOperand(1);
1230 const MachineOperand &Op2 = MI.getOperand(2);
1231 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001232 unsigned Rd = Op0.getReg();
1233 unsigned Pu = Op1.getReg();
1234 unsigned Rs = Op2.getReg();
1235 unsigned Rt = Op3.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001236 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001237 unsigned K1 = getKillRegState(Op1.isKill());
1238 unsigned K2 = getKillRegState(Op2.isKill());
1239 unsigned K3 = getKillRegState(Op3.isKill());
1240 if (Rd != Rs)
1241 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1242 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1243 .addReg(Rs, K2);
1244 if (Rd != Rt)
1245 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1246 .addReg(Pu, K1)
1247 .addReg(Rt, K3);
1248 MBB.erase(MI);
1249 return true;
1250 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001251 case Hexagon::PS_vselect:
1252 case Hexagon::PS_vselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001253 const MachineOperand &Op0 = MI.getOperand(0);
1254 const MachineOperand &Op1 = MI.getOperand(1);
1255 const MachineOperand &Op2 = MI.getOperand(2);
1256 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001257 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
1258 .addOperand(Op0)
1259 .addOperand(Op1)
1260 .addOperand(Op2);
1261 BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
1262 .addOperand(Op0)
1263 .addOperand(Op1)
1264 .addOperand(Op3);
1265 MBB.erase(MI);
1266 return true;
1267 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001268 case Hexagon::PS_wselect:
1269 case Hexagon::PS_wselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001270 MachineOperand &Op0 = MI.getOperand(0);
1271 MachineOperand &Op1 = MI.getOperand(1);
1272 MachineOperand &Op2 = MI.getOperand(2);
1273 MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001274 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::subreg_loreg);
1275 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::subreg_hireg);
1276 BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
1277 .addOperand(Op0)
1278 .addOperand(Op1)
1279 .addReg(SrcHi)
1280 .addReg(SrcLo);
1281 SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::subreg_loreg);
1282 SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::subreg_hireg);
1283 BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
1284 .addOperand(Op0)
1285 .addOperand(Op1)
1286 .addReg(SrcHi)
1287 .addReg(SrcLo);
1288 MBB.erase(MI);
1289 return true;
1290 }
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001291 case Hexagon::PS_tailcall_i:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001292 MI.setDesc(get(Hexagon::J2_jump));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001293 return true;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001294 case Hexagon::PS_tailcall_r:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001295 MI.setDesc(get(Hexagon::J2_jumpr));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001296 return true;
1297 }
1298
1299 return false;
1300}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001301
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001302
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001303// We indicate that we want to reverse the branch by
1304// inserting the reversed branching opcode.
1305bool HexagonInstrInfo::ReverseBranchCondition(
1306 SmallVectorImpl<MachineOperand> &Cond) const {
1307 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001308 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001309 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1310 unsigned opcode = Cond[0].getImm();
1311 //unsigned temp;
1312 assert(get(opcode).isBranch() && "Should be a branching condition.");
1313 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001314 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001315 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1316 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001317 return false;
1318}
1319
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001320
1321void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1322 MachineBasicBlock::iterator MI) const {
1323 DebugLoc DL;
1324 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1325}
1326
1327
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001328bool HexagonInstrInfo::isPostIncrement(const MachineInstr &MI) const {
1329 return getAddrMode(MI) == HexagonII::PostInc;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001330}
1331
1332
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001333// Returns true if an instruction is predicated irrespective of the predicate
1334// sense. For example, all of the following will return true.
1335// if (p0) R1 = add(R2, R3)
1336// if (!p0) R1 = add(R2, R3)
1337// if (p0.new) R1 = add(R2, R3)
1338// if (!p0.new) R1 = add(R2, R3)
1339// Note: New-value stores are not included here as in the current
1340// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001341bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1342 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001343 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001344}
1345
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001346
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001347bool HexagonInstrInfo::PredicateInstruction(
1348 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001349 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1350 isEndLoopN(Cond[0].getImm())) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001351 DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001352 return false;
1353 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001354 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001355 assert (isPredicable(MI) && "Expected predicable instruction");
1356 bool invertJump = predOpcodeHasNot(Cond);
1357
1358 // We have to predicate MI "in place", i.e. after this function returns,
1359 // MI will need to be transformed into a predicated form. To avoid com-
1360 // plicated manipulations with the operands (handling tied operands,
1361 // etc.), build a new temporary instruction, then overwrite MI with it.
1362
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001363 MachineBasicBlock &B = *MI.getParent();
1364 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001365 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1366 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001367 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001368 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001369 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001370 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1371 break;
1372 T.addOperand(Op);
1373 NOp++;
1374 }
1375
1376 unsigned PredReg, PredRegPos, PredRegFlags;
1377 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1378 (void)GotPredReg;
1379 assert(GotPredReg);
1380 T.addReg(PredReg, PredRegFlags);
1381 while (NOp < NumOps)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001382 T.addOperand(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001383
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001384 MI.setDesc(get(PredOpc));
1385 while (unsigned n = MI.getNumOperands())
1386 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001387 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001388 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001389
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001390 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001391 B.erase(TI);
1392
1393 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1394 MRI.clearKillFlags(PredReg);
1395 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001396}
1397
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001398
1399bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1400 ArrayRef<MachineOperand> Pred2) const {
1401 // TODO: Fix this
1402 return false;
1403}
1404
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001405
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001406bool HexagonInstrInfo::DefinesPredicate(
1407 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001408 auto &HRI = getRegisterInfo();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001409 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1410 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001411 if (MO.isReg() && MO.isDef()) {
1412 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1413 if (RC == &Hexagon::PredRegsRegClass) {
1414 Pred.push_back(MO);
1415 return true;
1416 }
1417 }
1418 }
1419 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001420}
Andrew Trickd06df962012-02-01 22:13:57 +00001421
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001422
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001423bool HexagonInstrInfo::isPredicable(MachineInstr &MI) const {
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001424 return MI.getDesc().isPredicable();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001425}
1426
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001427bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1428 const MachineBasicBlock *MBB,
1429 const MachineFunction &MF) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001430 // Debug info is never a scheduling boundary. It's necessary to be explicit
1431 // due to the special treatment of IT instructions below, otherwise a
1432 // dbg_value followed by an IT will result in the IT instruction being
1433 // considered a scheduling hazard, which is wrong. It should be the actual
1434 // instruction preceding the dbg_value instruction(s), just like it is
1435 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001436 if (MI.isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001437 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001438
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001439 // Throwing call is a boundary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001440 if (MI.isCall()) {
Krzysztof Parzyszekab9127c2016-08-12 11:01:10 +00001441 // Don't mess around with no return calls.
1442 if (doesNotReturn(MI))
1443 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001444 // If any of the block's successors is a landing pad, this could be a
1445 // throwing call.
1446 for (auto I : MBB->successors())
1447 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001448 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001449 }
1450
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001451 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001452 if (MI.getDesc().isTerminator() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001453 return true;
1454
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001455 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1456 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001457
1458 return false;
1459}
1460
1461
1462/// Measure the specified inline asm to determine an approximation of its
1463/// length.
1464/// Comments (which run till the next SeparatorString or newline) do not
1465/// count as an instruction.
1466/// Any other non-whitespace text is considered an instruction, with
1467/// multiple instructions separated by SeparatorString or newlines.
1468/// Variable-length instructions are not handled here; this function
1469/// may be overloaded in the target code to do that.
1470/// Hexagon counts the number of ##'s and adjust for that many
1471/// constant exenders.
1472unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1473 const MCAsmInfo &MAI) const {
1474 StringRef AStr(Str);
1475 // Count the number of instructions in the asm.
1476 bool atInsnStart = true;
1477 unsigned Length = 0;
1478 for (; *Str; ++Str) {
1479 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1480 strlen(MAI.getSeparatorString())) == 0)
1481 atInsnStart = true;
1482 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1483 Length += MAI.getMaxInstLength();
1484 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001485 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001486 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
1487 strlen(MAI.getCommentString())) == 0)
1488 atInsnStart = false;
1489 }
1490
1491 // Add to size number of constant extenders seen * 4.
1492 StringRef Occ("##");
1493 Length += AStr.count(Occ)*4;
1494 return Length;
1495}
1496
1497
1498ScheduleHazardRecognizer*
1499HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1500 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +00001501 if (UseDFAHazardRec) {
1502 auto &HST = DAG->MF.getSubtarget<HexagonSubtarget>();
1503 return new HexagonHazardRecognizer(II, this, HST);
1504 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001505 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1506}
1507
1508
1509/// \brief For a comparison instruction, return the source registers in
1510/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1511/// compares against in CmpValue. Return true if the comparison instruction
1512/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001513bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1514 unsigned &SrcReg2, int &Mask,
1515 int &Value) const {
1516 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001517
1518 // Set mask and the first source register.
1519 switch (Opc) {
1520 case Hexagon::C2_cmpeq:
1521 case Hexagon::C2_cmpeqp:
1522 case Hexagon::C2_cmpgt:
1523 case Hexagon::C2_cmpgtp:
1524 case Hexagon::C2_cmpgtu:
1525 case Hexagon::C2_cmpgtup:
1526 case Hexagon::C4_cmpneq:
1527 case Hexagon::C4_cmplte:
1528 case Hexagon::C4_cmplteu:
1529 case Hexagon::C2_cmpeqi:
1530 case Hexagon::C2_cmpgti:
1531 case Hexagon::C2_cmpgtui:
1532 case Hexagon::C4_cmpneqi:
1533 case Hexagon::C4_cmplteui:
1534 case Hexagon::C4_cmpltei:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001535 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001536 Mask = ~0;
1537 break;
1538 case Hexagon::A4_cmpbeq:
1539 case Hexagon::A4_cmpbgt:
1540 case Hexagon::A4_cmpbgtu:
1541 case Hexagon::A4_cmpbeqi:
1542 case Hexagon::A4_cmpbgti:
1543 case Hexagon::A4_cmpbgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001544 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001545 Mask = 0xFF;
1546 break;
1547 case Hexagon::A4_cmpheq:
1548 case Hexagon::A4_cmphgt:
1549 case Hexagon::A4_cmphgtu:
1550 case Hexagon::A4_cmpheqi:
1551 case Hexagon::A4_cmphgti:
1552 case Hexagon::A4_cmphgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001553 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001554 Mask = 0xFFFF;
1555 break;
1556 }
1557
1558 // Set the value/second source register.
1559 switch (Opc) {
1560 case Hexagon::C2_cmpeq:
1561 case Hexagon::C2_cmpeqp:
1562 case Hexagon::C2_cmpgt:
1563 case Hexagon::C2_cmpgtp:
1564 case Hexagon::C2_cmpgtu:
1565 case Hexagon::C2_cmpgtup:
1566 case Hexagon::A4_cmpbeq:
1567 case Hexagon::A4_cmpbgt:
1568 case Hexagon::A4_cmpbgtu:
1569 case Hexagon::A4_cmpheq:
1570 case Hexagon::A4_cmphgt:
1571 case Hexagon::A4_cmphgtu:
1572 case Hexagon::C4_cmpneq:
1573 case Hexagon::C4_cmplte:
1574 case Hexagon::C4_cmplteu:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001575 SrcReg2 = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001576 return true;
1577
1578 case Hexagon::C2_cmpeqi:
1579 case Hexagon::C2_cmpgtui:
1580 case Hexagon::C2_cmpgti:
1581 case Hexagon::C4_cmpneqi:
1582 case Hexagon::C4_cmplteui:
1583 case Hexagon::C4_cmpltei:
1584 case Hexagon::A4_cmpbeqi:
1585 case Hexagon::A4_cmpbgti:
1586 case Hexagon::A4_cmpbgtui:
1587 case Hexagon::A4_cmpheqi:
1588 case Hexagon::A4_cmphgti:
1589 case Hexagon::A4_cmphgtui:
1590 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001591 Value = MI.getOperand(2).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001592 return true;
1593 }
1594
1595 return false;
1596}
1597
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001598unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001599 const MachineInstr &MI,
1600 unsigned *PredCost) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001601 return getInstrTimingClassLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001602}
1603
1604
1605DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1606 const TargetSubtargetInfo &STI) const {
1607 const InstrItineraryData *II = STI.getInstrItineraryData();
1608 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1609}
1610
1611
1612// Inspired by this pair:
1613// %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1614// S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1615// Currently AA considers the addresses in these instructions to be aliasing.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001616bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1617 MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001618 int OffsetA = 0, OffsetB = 0;
1619 unsigned SizeA = 0, SizeB = 0;
1620
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001621 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1622 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001623 return false;
1624
1625 // Instructions that are pure loads, not loads and stores like memops are not
1626 // dependent.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001627 if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001628 return true;
1629
1630 // Get base, offset, and access size in MIa.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001631 unsigned BaseRegA = getBaseAndOffset(MIa, OffsetA, SizeA);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001632 if (!BaseRegA || !SizeA)
1633 return false;
1634
1635 // Get base, offset, and access size in MIb.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001636 unsigned BaseRegB = getBaseAndOffset(MIb, OffsetB, SizeB);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001637 if (!BaseRegB || !SizeB)
1638 return false;
1639
1640 if (BaseRegA != BaseRegB)
1641 return false;
1642
1643 // This is a mem access with the same base register and known offsets from it.
1644 // Reason about it.
1645 if (OffsetA > OffsetB) {
1646 uint64_t offDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1647 return (SizeB <= offDiff);
1648 } else if (OffsetA < OffsetB) {
1649 uint64_t offDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1650 return (SizeA <= offDiff);
1651 }
1652
1653 return false;
1654}
1655
1656
Brendon Cahoon254f8892016-07-29 16:44:44 +00001657/// If the instruction is an increment of a constant value, return the amount.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001658bool HexagonInstrInfo::getIncrementValue(const MachineInstr &MI,
Brendon Cahoon254f8892016-07-29 16:44:44 +00001659 int &Value) const {
1660 if (isPostIncrement(MI)) {
1661 unsigned AccessSize;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001662 return getBaseAndOffset(MI, Value, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00001663 }
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001664 if (MI.getOpcode() == Hexagon::A2_addi) {
1665 Value = MI.getOperand(2).getImm();
Brendon Cahoon254f8892016-07-29 16:44:44 +00001666 return true;
1667 }
1668
1669 return false;
1670}
1671
1672
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001673unsigned HexagonInstrInfo::createVR(MachineFunction *MF, MVT VT) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001674 MachineRegisterInfo &MRI = MF->getRegInfo();
1675 const TargetRegisterClass *TRC;
1676 if (VT == MVT::i1) {
1677 TRC = &Hexagon::PredRegsRegClass;
1678 } else if (VT == MVT::i32 || VT == MVT::f32) {
1679 TRC = &Hexagon::IntRegsRegClass;
1680 } else if (VT == MVT::i64 || VT == MVT::f64) {
1681 TRC = &Hexagon::DoubleRegsRegClass;
1682 } else {
1683 llvm_unreachable("Cannot handle this register class");
1684 }
1685
1686 unsigned NewReg = MRI.createVirtualRegister(TRC);
1687 return NewReg;
1688}
1689
1690
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001691bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001692 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1693}
1694
1695
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001696bool HexagonInstrInfo::isAccumulator(const MachineInstr &MI) const {
1697 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001698 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1699}
1700
1701
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001702bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
1703 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001704 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1705 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1706
1707 if (!(isTC1(MI))
1708 && !(QII->isTC2Early(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001709 && !(MI.getDesc().mayLoad())
1710 && !(MI.getDesc().mayStore())
1711 && (MI.getDesc().getOpcode() != Hexagon::S2_allocframe)
1712 && (MI.getDesc().getOpcode() != Hexagon::L2_deallocframe)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001713 && !(QII->isMemOp(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001714 && !(MI.isBranch())
1715 && !(MI.isReturn())
1716 && !MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001717 return true;
1718
1719 return false;
1720}
1721
1722
Sanjay Patele4b9f502015-12-07 19:21:39 +00001723// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001724bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
1725 return (getType(MI) == HexagonII::TypeCOMPOUND && MI.isBranch());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001726}
1727
1728
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001729bool HexagonInstrInfo::isCondInst(const MachineInstr &MI) const {
1730 return (MI.isBranch() && isPredicated(MI)) ||
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001731 isConditionalTransfer(MI) ||
1732 isConditionalALU32(MI) ||
1733 isConditionalLoad(MI) ||
1734 // Predicated stores which don't have a .new on any operands.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001735 (MI.mayStore() && isPredicated(MI) && !isNewValueStore(MI) &&
1736 !isPredicatedNew(MI));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001737}
1738
1739
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001740bool HexagonInstrInfo::isConditionalALU32(const MachineInstr &MI) const {
1741 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001742 case Hexagon::A2_paddf:
1743 case Hexagon::A2_paddfnew:
1744 case Hexagon::A2_paddif:
1745 case Hexagon::A2_paddifnew:
1746 case Hexagon::A2_paddit:
1747 case Hexagon::A2_padditnew:
1748 case Hexagon::A2_paddt:
1749 case Hexagon::A2_paddtnew:
1750 case Hexagon::A2_pandf:
1751 case Hexagon::A2_pandfnew:
1752 case Hexagon::A2_pandt:
1753 case Hexagon::A2_pandtnew:
1754 case Hexagon::A2_porf:
1755 case Hexagon::A2_porfnew:
1756 case Hexagon::A2_port:
1757 case Hexagon::A2_portnew:
1758 case Hexagon::A2_psubf:
1759 case Hexagon::A2_psubfnew:
1760 case Hexagon::A2_psubt:
1761 case Hexagon::A2_psubtnew:
1762 case Hexagon::A2_pxorf:
1763 case Hexagon::A2_pxorfnew:
1764 case Hexagon::A2_pxort:
1765 case Hexagon::A2_pxortnew:
1766 case Hexagon::A4_paslhf:
1767 case Hexagon::A4_paslhfnew:
1768 case Hexagon::A4_paslht:
1769 case Hexagon::A4_paslhtnew:
1770 case Hexagon::A4_pasrhf:
1771 case Hexagon::A4_pasrhfnew:
1772 case Hexagon::A4_pasrht:
1773 case Hexagon::A4_pasrhtnew:
1774 case Hexagon::A4_psxtbf:
1775 case Hexagon::A4_psxtbfnew:
1776 case Hexagon::A4_psxtbt:
1777 case Hexagon::A4_psxtbtnew:
1778 case Hexagon::A4_psxthf:
1779 case Hexagon::A4_psxthfnew:
1780 case Hexagon::A4_psxtht:
1781 case Hexagon::A4_psxthtnew:
1782 case Hexagon::A4_pzxtbf:
1783 case Hexagon::A4_pzxtbfnew:
1784 case Hexagon::A4_pzxtbt:
1785 case Hexagon::A4_pzxtbtnew:
1786 case Hexagon::A4_pzxthf:
1787 case Hexagon::A4_pzxthfnew:
1788 case Hexagon::A4_pzxtht:
1789 case Hexagon::A4_pzxthtnew:
1790 case Hexagon::C2_ccombinewf:
1791 case Hexagon::C2_ccombinewt:
1792 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001793 }
1794 return false;
1795}
1796
1797
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001798// FIXME - Function name and it's functionality don't match.
1799// It should be renamed to hasPredNewOpcode()
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001800bool HexagonInstrInfo::isConditionalLoad(const MachineInstr &MI) const {
1801 if (!MI.getDesc().mayLoad() || !isPredicated(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001802 return false;
1803
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001804 int PNewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001805 // Instruction with valid predicated-new opcode can be promoted to .new.
1806 return PNewOpcode >= 0;
1807}
1808
1809
1810// Returns true if an instruction is a conditional store.
1811//
1812// Note: It doesn't include conditional new-value stores as they can't be
1813// converted to .new predicate.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001814bool HexagonInstrInfo::isConditionalStore(const MachineInstr &MI) const {
1815 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001816 default: return false;
1817 case Hexagon::S4_storeirbt_io:
1818 case Hexagon::S4_storeirbf_io:
1819 case Hexagon::S4_pstorerbt_rr:
1820 case Hexagon::S4_pstorerbf_rr:
1821 case Hexagon::S2_pstorerbt_io:
1822 case Hexagon::S2_pstorerbf_io:
1823 case Hexagon::S2_pstorerbt_pi:
1824 case Hexagon::S2_pstorerbf_pi:
1825 case Hexagon::S2_pstorerdt_io:
1826 case Hexagon::S2_pstorerdf_io:
1827 case Hexagon::S4_pstorerdt_rr:
1828 case Hexagon::S4_pstorerdf_rr:
1829 case Hexagon::S2_pstorerdt_pi:
1830 case Hexagon::S2_pstorerdf_pi:
1831 case Hexagon::S2_pstorerht_io:
1832 case Hexagon::S2_pstorerhf_io:
1833 case Hexagon::S4_storeirht_io:
1834 case Hexagon::S4_storeirhf_io:
1835 case Hexagon::S4_pstorerht_rr:
1836 case Hexagon::S4_pstorerhf_rr:
1837 case Hexagon::S2_pstorerht_pi:
1838 case Hexagon::S2_pstorerhf_pi:
1839 case Hexagon::S2_pstorerit_io:
1840 case Hexagon::S2_pstorerif_io:
1841 case Hexagon::S4_storeirit_io:
1842 case Hexagon::S4_storeirif_io:
1843 case Hexagon::S4_pstorerit_rr:
1844 case Hexagon::S4_pstorerif_rr:
1845 case Hexagon::S2_pstorerit_pi:
1846 case Hexagon::S2_pstorerif_pi:
1847
1848 // V4 global address store before promoting to dot new.
1849 case Hexagon::S4_pstorerdt_abs:
1850 case Hexagon::S4_pstorerdf_abs:
1851 case Hexagon::S4_pstorerbt_abs:
1852 case Hexagon::S4_pstorerbf_abs:
1853 case Hexagon::S4_pstorerht_abs:
1854 case Hexagon::S4_pstorerhf_abs:
1855 case Hexagon::S4_pstorerit_abs:
1856 case Hexagon::S4_pstorerif_abs:
1857 return true;
1858
1859 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1860 // from the "Conditional Store" list. Because a predicated new value store
1861 // would NOT be promoted to a double dot new store.
1862 // This function returns yes for those stores that are predicated but not
1863 // yet promoted to predicate dot new instructions.
1864 }
1865}
1866
1867
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001868bool HexagonInstrInfo::isConditionalTransfer(const MachineInstr &MI) const {
1869 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001870 case Hexagon::A2_tfrt:
1871 case Hexagon::A2_tfrf:
1872 case Hexagon::C2_cmoveit:
1873 case Hexagon::C2_cmoveif:
1874 case Hexagon::A2_tfrtnew:
1875 case Hexagon::A2_tfrfnew:
1876 case Hexagon::C2_cmovenewit:
1877 case Hexagon::C2_cmovenewif:
1878 case Hexagon::A2_tfrpt:
1879 case Hexagon::A2_tfrpf:
1880 return true;
1881
1882 default:
1883 return false;
1884 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001885 return false;
1886}
1887
1888
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001889// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1890// isFPImm and later getFPImm as well.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001891bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
1892 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001893 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1894 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001895 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001896
1897 unsigned isExtendable =
1898 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1899 if (!isExtendable)
1900 return false;
1901
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001902 if (MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001903 return false;
1904
1905 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001906 const MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001907 // Use MO operand flags to determine if MO
1908 // has the HMOTF_ConstExtended flag set.
1909 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001910 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001911 // If this is a Machine BB address we are talking about, and it is
1912 // not marked as extended, say so.
1913 if (MO.isMBB())
1914 return false;
1915
1916 // We could be using an instruction with an extendable immediate and shoehorn
1917 // a global address into it. If it is a global address it will be constant
1918 // extended. We do this for COMBINE.
1919 // We currently only handle isGlobal() because it is the only kind of
1920 // object we are going to end up with here for now.
1921 // In the future we probably should add isSymbol(), etc.
1922 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001923 MO.isJTI() || MO.isCPI() || MO.isFPImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001924 return true;
1925
1926 // If the extendable operand is not 'Immediate' type, the instruction should
1927 // have 'isExtended' flag set.
1928 assert(MO.isImm() && "Extendable operand must be Immediate type");
1929
1930 int MinValue = getMinValue(MI);
1931 int MaxValue = getMaxValue(MI);
1932 int ImmValue = MO.getImm();
1933
1934 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001935}
1936
1937
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001938bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
1939 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001940 case Hexagon::L4_return :
1941 case Hexagon::L4_return_t :
1942 case Hexagon::L4_return_f :
1943 case Hexagon::L4_return_tnew_pnt :
1944 case Hexagon::L4_return_fnew_pnt :
1945 case Hexagon::L4_return_tnew_pt :
1946 case Hexagon::L4_return_fnew_pt :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001947 return true;
1948 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001949 return false;
1950}
1951
1952
1953// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001954bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
1955 const MachineInstr &ConsMI) const {
1956 if (!ProdMI.getDesc().getNumDefs())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001957 return false;
1958
1959 auto &HRI = getRegisterInfo();
1960
1961 SmallVector<unsigned, 4> DefsA;
1962 SmallVector<unsigned, 4> DefsB;
1963 SmallVector<unsigned, 8> UsesA;
1964 SmallVector<unsigned, 8> UsesB;
1965
1966 parseOperands(ProdMI, DefsA, UsesA);
1967 parseOperands(ConsMI, DefsB, UsesB);
1968
1969 for (auto &RegA : DefsA)
1970 for (auto &RegB : UsesB) {
1971 // True data dependency.
1972 if (RegA == RegB)
1973 return true;
1974
1975 if (Hexagon::DoubleRegsRegClass.contains(RegA))
1976 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1977 if (RegB == *SubRegs)
1978 return true;
1979
1980 if (Hexagon::DoubleRegsRegClass.contains(RegB))
1981 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1982 if (RegA == *SubRegs)
1983 return true;
1984 }
1985
1986 return false;
1987}
1988
1989
1990// Returns true if the instruction is alread a .cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001991bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
1992 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001993 case Hexagon::V6_vL32b_cur_pi:
1994 case Hexagon::V6_vL32b_cur_ai:
1995 case Hexagon::V6_vL32b_cur_pi_128B:
1996 case Hexagon::V6_vL32b_cur_ai_128B:
1997 return true;
1998 }
1999 return false;
2000}
2001
2002
2003// Returns true, if any one of the operands is a dot new
2004// insn, whether it is predicated dot new or register dot new.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002005bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
2006 if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002007 return true;
2008
2009 return false;
2010}
2011
2012
2013/// Symmetrical. See if these two instructions are fit for duplex pair.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002014bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
2015 const MachineInstr &MIb) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002016 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
2017 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
2018 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
2019}
2020
2021
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002022bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
2023 if (MI.mayLoad() || MI.mayStore() || MI.isCompare())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002024 return true;
2025
2026 // Multiply
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002027 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002028 if (SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23)
2029 return true;
2030 return false;
2031}
2032
2033
2034bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
2035 return (Opcode == Hexagon::ENDLOOP0 ||
2036 Opcode == Hexagon::ENDLOOP1);
2037}
2038
2039
2040bool HexagonInstrInfo::isExpr(unsigned OpType) const {
2041 switch(OpType) {
2042 case MachineOperand::MO_MachineBasicBlock:
2043 case MachineOperand::MO_GlobalAddress:
2044 case MachineOperand::MO_ExternalSymbol:
2045 case MachineOperand::MO_JumpTableIndex:
2046 case MachineOperand::MO_ConstantPoolIndex:
2047 case MachineOperand::MO_BlockAddress:
2048 return true;
2049 default:
2050 return false;
2051 }
2052}
2053
2054
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002055bool HexagonInstrInfo::isExtendable(const MachineInstr &MI) const {
2056 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002057 const uint64_t F = MID.TSFlags;
2058 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
2059 return true;
2060
2061 // TODO: This is largely obsolete now. Will need to be removed
2062 // in consecutive patches.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002063 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002064 // TFR_FI Remains a special case.
2065 case Hexagon::TFR_FI:
2066 return true;
2067 default:
2068 return false;
2069 }
2070 return false;
2071}
2072
2073
2074// This returns true in two cases:
2075// - The OP code itself indicates that this is an extended instruction.
2076// - One of MOs has been marked with HMOTF_ConstExtended flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002077bool HexagonInstrInfo::isExtended(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002078 // First check if this is permanently extended op code.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002079 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002080 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2081 return true;
2082 // Use MO operand flags to determine if one of MI's operands
2083 // has HMOTF_ConstExtended flag set.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002084 for (MachineInstr::const_mop_iterator I = MI.operands_begin(),
2085 E = MI.operands_end(); I != E; ++I) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002086 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
2087 return true;
2088 }
2089 return false;
2090}
2091
2092
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002093bool HexagonInstrInfo::isFloat(const MachineInstr &MI) const {
2094 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002095 const uint64_t F = get(Opcode).TSFlags;
2096 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2097}
2098
2099
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002100// No V60 HVX VMEM with A_INDIRECT.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002101bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr &I,
2102 const MachineInstr &J) const {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002103 if (!isV60VectorInstruction(I))
2104 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002105 if (!I.mayLoad() && !I.mayStore())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002106 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002107 return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002108}
2109
2110
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002111bool HexagonInstrInfo::isIndirectCall(const MachineInstr &MI) const {
2112 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002113 case Hexagon::J2_callr :
2114 case Hexagon::J2_callrf :
2115 case Hexagon::J2_callrt :
2116 return true;
2117 }
2118 return false;
2119}
2120
2121
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002122bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr &MI) const {
2123 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002124 case Hexagon::L4_return :
2125 case Hexagon::L4_return_t :
2126 case Hexagon::L4_return_f :
2127 case Hexagon::L4_return_fnew_pnt :
2128 case Hexagon::L4_return_fnew_pt :
2129 case Hexagon::L4_return_tnew_pnt :
2130 case Hexagon::L4_return_tnew_pt :
2131 return true;
2132 }
2133 return false;
2134}
2135
2136
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002137bool HexagonInstrInfo::isJumpR(const MachineInstr &MI) const {
2138 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002139 case Hexagon::J2_jumpr :
2140 case Hexagon::J2_jumprt :
2141 case Hexagon::J2_jumprf :
2142 case Hexagon::J2_jumprtnewpt :
2143 case Hexagon::J2_jumprfnewpt :
2144 case Hexagon::J2_jumprtnew :
2145 case Hexagon::J2_jumprfnew :
2146 return true;
2147 }
2148 return false;
2149}
2150
2151
2152// Return true if a given MI can accomodate given offset.
2153// Use abs estimate as oppose to the exact number.
2154// TODO: This will need to be changed to use MC level
2155// definition of instruction extendable field size.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002156bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002157 unsigned offset) const {
2158 // This selection of jump instructions matches to that what
2159 // AnalyzeBranch can parse, plus NVJ.
2160 if (isNewValueJump(MI)) // r9:2
2161 return isInt<11>(offset);
2162
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002163 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002164 // Still missing Jump to address condition on register value.
2165 default:
2166 return false;
2167 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2168 case Hexagon::J2_call:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002169 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002170 return isInt<24>(offset);
2171 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2172 case Hexagon::J2_jumpf:
2173 case Hexagon::J2_jumptnew:
2174 case Hexagon::J2_jumptnewpt:
2175 case Hexagon::J2_jumpfnew:
2176 case Hexagon::J2_jumpfnewpt:
2177 case Hexagon::J2_callt:
2178 case Hexagon::J2_callf:
2179 return isInt<17>(offset);
2180 case Hexagon::J2_loop0i:
2181 case Hexagon::J2_loop0iext:
2182 case Hexagon::J2_loop0r:
2183 case Hexagon::J2_loop0rext:
2184 case Hexagon::J2_loop1i:
2185 case Hexagon::J2_loop1iext:
2186 case Hexagon::J2_loop1r:
2187 case Hexagon::J2_loop1rext:
2188 return isInt<9>(offset);
2189 // TODO: Add all the compound branches here. Can we do this in Relation model?
2190 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2191 case Hexagon::J4_cmpeqi_tp1_jump_nt:
2192 return isInt<11>(offset);
2193 }
2194}
2195
2196
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002197bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr &LRMI,
2198 const MachineInstr &ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002199 bool isLate = isLateResultInstr(LRMI);
2200 bool isEarly = isEarlySourceInstr(ESMI);
2201
2202 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002203 DEBUG(LRMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002204 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002205 DEBUG(ESMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002206
2207 if (isLate && isEarly) {
2208 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2209 return true;
2210 }
2211
2212 return false;
2213}
2214
2215
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002216bool HexagonInstrInfo::isLateResultInstr(const MachineInstr &MI) const {
2217 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002218 case TargetOpcode::EXTRACT_SUBREG:
2219 case TargetOpcode::INSERT_SUBREG:
2220 case TargetOpcode::SUBREG_TO_REG:
2221 case TargetOpcode::REG_SEQUENCE:
2222 case TargetOpcode::IMPLICIT_DEF:
2223 case TargetOpcode::COPY:
2224 case TargetOpcode::INLINEASM:
2225 case TargetOpcode::PHI:
2226 return false;
2227 default:
2228 break;
2229 }
2230
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002231 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002232
2233 switch (SchedClass) {
2234 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2235 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2236 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2237 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2238 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2239 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2240 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2241 case Hexagon::Sched::V2LDST_tc_ld_SLOT01:
2242 case Hexagon::Sched::V2LDST_tc_st_SLOT0:
2243 case Hexagon::Sched::V2LDST_tc_st_SLOT01:
2244 case Hexagon::Sched::V4LDST_tc_ld_SLOT01:
2245 case Hexagon::Sched::V4LDST_tc_st_SLOT0:
2246 case Hexagon::Sched::V4LDST_tc_st_SLOT01:
2247 return false;
2248 }
2249 return true;
2250}
2251
2252
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002253bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002254 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2255 // resource, but all operands can be received late like an ALU instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002256 return MI.getDesc().getSchedClass() == Hexagon::Sched::CVI_VX_LATE;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002257}
2258
2259
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002260bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2261 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002262 return Opcode == Hexagon::J2_loop0i ||
2263 Opcode == Hexagon::J2_loop0r ||
2264 Opcode == Hexagon::J2_loop0iext ||
2265 Opcode == Hexagon::J2_loop0rext ||
2266 Opcode == Hexagon::J2_loop1i ||
2267 Opcode == Hexagon::J2_loop1r ||
2268 Opcode == Hexagon::J2_loop1iext ||
2269 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002270}
2271
2272
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002273bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2274 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002275 default: return false;
2276 case Hexagon::L4_iadd_memopw_io :
2277 case Hexagon::L4_isub_memopw_io :
2278 case Hexagon::L4_add_memopw_io :
2279 case Hexagon::L4_sub_memopw_io :
2280 case Hexagon::L4_and_memopw_io :
2281 case Hexagon::L4_or_memopw_io :
2282 case Hexagon::L4_iadd_memoph_io :
2283 case Hexagon::L4_isub_memoph_io :
2284 case Hexagon::L4_add_memoph_io :
2285 case Hexagon::L4_sub_memoph_io :
2286 case Hexagon::L4_and_memoph_io :
2287 case Hexagon::L4_or_memoph_io :
2288 case Hexagon::L4_iadd_memopb_io :
2289 case Hexagon::L4_isub_memopb_io :
2290 case Hexagon::L4_add_memopb_io :
2291 case Hexagon::L4_sub_memopb_io :
2292 case Hexagon::L4_and_memopb_io :
2293 case Hexagon::L4_or_memopb_io :
2294 case Hexagon::L4_ior_memopb_io:
2295 case Hexagon::L4_ior_memoph_io:
2296 case Hexagon::L4_ior_memopw_io:
2297 case Hexagon::L4_iand_memopb_io:
2298 case Hexagon::L4_iand_memoph_io:
2299 case Hexagon::L4_iand_memopw_io:
2300 return true;
2301 }
2302 return false;
2303}
2304
2305
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002306bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2307 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002308 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2309}
2310
2311
2312bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2313 const uint64_t F = get(Opcode).TSFlags;
2314 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2315}
2316
2317
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002318bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002319 return isNewValueJump(MI) || isNewValueStore(MI);
2320}
2321
2322
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002323bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2324 return isNewValue(MI) && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002325}
2326
2327
2328bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2329 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2330}
2331
2332
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002333bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2334 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002335 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2336}
2337
2338
2339bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2340 const uint64_t F = get(Opcode).TSFlags;
2341 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2342}
2343
2344
2345// Returns true if a particular operand is extendable for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002346bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002347 unsigned OperandNum) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002348 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002349 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2350 == OperandNum;
2351}
2352
2353
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002354bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2355 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002356 assert(isPredicated(MI));
2357 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2358}
2359
2360
2361bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2362 const uint64_t F = get(Opcode).TSFlags;
2363 assert(isPredicated(Opcode));
2364 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2365}
2366
2367
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002368bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2369 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002370 return !((F >> HexagonII::PredicatedFalsePos) &
2371 HexagonII::PredicatedFalseMask);
2372}
2373
2374
2375bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2376 const uint64_t F = get(Opcode).TSFlags;
2377 // Make sure that the instruction is predicated.
2378 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2379 return !((F >> HexagonII::PredicatedFalsePos) &
2380 HexagonII::PredicatedFalseMask);
2381}
2382
2383
2384bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2385 const uint64_t F = get(Opcode).TSFlags;
2386 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2387}
2388
2389
2390bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2391 const uint64_t F = get(Opcode).TSFlags;
2392 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2393}
2394
2395
2396bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2397 const uint64_t F = get(Opcode).TSFlags;
2398 assert(get(Opcode).isBranch() &&
2399 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2400 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2401}
2402
2403
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002404bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2405 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2406 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2407 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2408 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002409}
2410
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002411bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2412 switch (MI.getOpcode()) {
2413 // Byte
2414 case Hexagon::L2_loadrb_io:
2415 case Hexagon::L4_loadrb_ur:
2416 case Hexagon::L4_loadrb_ap:
2417 case Hexagon::L2_loadrb_pr:
2418 case Hexagon::L2_loadrb_pbr:
2419 case Hexagon::L2_loadrb_pi:
2420 case Hexagon::L2_loadrb_pci:
2421 case Hexagon::L2_loadrb_pcr:
2422 case Hexagon::L2_loadbsw2_io:
2423 case Hexagon::L4_loadbsw2_ur:
2424 case Hexagon::L4_loadbsw2_ap:
2425 case Hexagon::L2_loadbsw2_pr:
2426 case Hexagon::L2_loadbsw2_pbr:
2427 case Hexagon::L2_loadbsw2_pi:
2428 case Hexagon::L2_loadbsw2_pci:
2429 case Hexagon::L2_loadbsw2_pcr:
2430 case Hexagon::L2_loadbsw4_io:
2431 case Hexagon::L4_loadbsw4_ur:
2432 case Hexagon::L4_loadbsw4_ap:
2433 case Hexagon::L2_loadbsw4_pr:
2434 case Hexagon::L2_loadbsw4_pbr:
2435 case Hexagon::L2_loadbsw4_pi:
2436 case Hexagon::L2_loadbsw4_pci:
2437 case Hexagon::L2_loadbsw4_pcr:
2438 case Hexagon::L4_loadrb_rr:
2439 case Hexagon::L2_ploadrbt_io:
2440 case Hexagon::L2_ploadrbt_pi:
2441 case Hexagon::L2_ploadrbf_io:
2442 case Hexagon::L2_ploadrbf_pi:
2443 case Hexagon::L2_ploadrbtnew_io:
2444 case Hexagon::L2_ploadrbfnew_io:
2445 case Hexagon::L4_ploadrbt_rr:
2446 case Hexagon::L4_ploadrbf_rr:
2447 case Hexagon::L4_ploadrbtnew_rr:
2448 case Hexagon::L4_ploadrbfnew_rr:
2449 case Hexagon::L2_ploadrbtnew_pi:
2450 case Hexagon::L2_ploadrbfnew_pi:
2451 case Hexagon::L4_ploadrbt_abs:
2452 case Hexagon::L4_ploadrbf_abs:
2453 case Hexagon::L4_ploadrbtnew_abs:
2454 case Hexagon::L4_ploadrbfnew_abs:
2455 case Hexagon::L2_loadrbgp:
2456 // Half
2457 case Hexagon::L2_loadrh_io:
2458 case Hexagon::L4_loadrh_ur:
2459 case Hexagon::L4_loadrh_ap:
2460 case Hexagon::L2_loadrh_pr:
2461 case Hexagon::L2_loadrh_pbr:
2462 case Hexagon::L2_loadrh_pi:
2463 case Hexagon::L2_loadrh_pci:
2464 case Hexagon::L2_loadrh_pcr:
2465 case Hexagon::L4_loadrh_rr:
2466 case Hexagon::L2_ploadrht_io:
2467 case Hexagon::L2_ploadrht_pi:
2468 case Hexagon::L2_ploadrhf_io:
2469 case Hexagon::L2_ploadrhf_pi:
2470 case Hexagon::L2_ploadrhtnew_io:
2471 case Hexagon::L2_ploadrhfnew_io:
2472 case Hexagon::L4_ploadrht_rr:
2473 case Hexagon::L4_ploadrhf_rr:
2474 case Hexagon::L4_ploadrhtnew_rr:
2475 case Hexagon::L4_ploadrhfnew_rr:
2476 case Hexagon::L2_ploadrhtnew_pi:
2477 case Hexagon::L2_ploadrhfnew_pi:
2478 case Hexagon::L4_ploadrht_abs:
2479 case Hexagon::L4_ploadrhf_abs:
2480 case Hexagon::L4_ploadrhtnew_abs:
2481 case Hexagon::L4_ploadrhfnew_abs:
2482 case Hexagon::L2_loadrhgp:
2483 return true;
2484 default:
2485 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002486 }
2487}
2488
2489
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002490bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2491 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002492 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2493}
2494
2495
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002496bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2497 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002498 case Hexagon::STriw_pred :
2499 case Hexagon::LDriw_pred :
2500 return true;
2501 default:
2502 return false;
2503 }
2504}
2505
2506
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002507bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2508 if (!MI.isBranch())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002509 return false;
2510
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002511 for (auto &Op : MI.operands())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002512 if (Op.isGlobal() || Op.isSymbol())
2513 return true;
2514 return false;
2515}
2516
2517
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002518// Returns true when SU has a timing class TC1.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002519bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2520 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002521 switch (SchedClass) {
2522 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2523 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2524 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2525 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2526 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2527 //case Hexagon::Sched::M_tc_1_SLOT23:
2528 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2529 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2530 return true;
2531
2532 default:
2533 return false;
2534 }
2535}
2536
2537
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002538bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2539 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002540 switch (SchedClass) {
2541 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
2542 case Hexagon::Sched::ALU64_tc_2_SLOT23:
2543 case Hexagon::Sched::CR_tc_2_SLOT3:
2544 case Hexagon::Sched::M_tc_2_SLOT23:
2545 case Hexagon::Sched::S_2op_tc_2_SLOT23:
2546 case Hexagon::Sched::S_3op_tc_2_SLOT23:
2547 return true;
2548
2549 default:
2550 return false;
2551 }
2552}
2553
2554
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002555bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2556 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002557 switch (SchedClass) {
2558 case Hexagon::Sched::ALU32_2op_tc_2early_SLOT0123:
2559 case Hexagon::Sched::ALU32_3op_tc_2early_SLOT0123:
2560 case Hexagon::Sched::ALU64_tc_2early_SLOT23:
2561 case Hexagon::Sched::CR_tc_2early_SLOT23:
2562 case Hexagon::Sched::CR_tc_2early_SLOT3:
2563 case Hexagon::Sched::J_tc_2early_SLOT0123:
2564 case Hexagon::Sched::J_tc_2early_SLOT2:
2565 case Hexagon::Sched::J_tc_2early_SLOT23:
2566 case Hexagon::Sched::S_2op_tc_2early_SLOT23:
2567 case Hexagon::Sched::S_3op_tc_2early_SLOT23:
2568 return true;
2569
2570 default:
2571 return false;
2572 }
2573}
2574
2575
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002576bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2577 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002578 return SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23;
2579}
2580
2581
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002582// Schedule this ASAP.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002583bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2584 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002585 if (mayBeCurLoad(MI1)) {
2586 // if (result of SU is used in Next) return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002587 unsigned DstReg = MI1.getOperand(0).getReg();
2588 int N = MI2.getNumOperands();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002589 for (int I = 0; I < N; I++)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002590 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002591 return true;
2592 }
2593 if (mayBeNewStore(MI2))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002594 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2595 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2596 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002597 return true;
2598 return false;
2599}
2600
2601
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002602bool HexagonInstrInfo::isV60VectorInstruction(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002603 const uint64_t V = getType(MI);
2604 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2605}
2606
2607
2608// Check if the Offset is a valid auto-inc imm by Load/Store Type.
2609//
2610bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2611 if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2612 VT == MVT::v32i16 || VT == MVT::v64i8) {
2613 return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2614 Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2615 (Offset & 0x3f) == 0);
2616 }
2617 // 128B
2618 if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2619 VT == MVT::v64i16 || VT == MVT::v128i8) {
2620 return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2621 Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2622 (Offset & 0x7f) == 0);
2623 }
2624 if (VT == MVT::i64) {
2625 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2626 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2627 (Offset & 0x7) == 0);
2628 }
2629 if (VT == MVT::i32) {
2630 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2631 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2632 (Offset & 0x3) == 0);
2633 }
2634 if (VT == MVT::i16) {
2635 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2636 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2637 (Offset & 0x1) == 0);
2638 }
2639 if (VT == MVT::i8) {
2640 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2641 Offset <= Hexagon_MEMB_AUTOINC_MAX);
2642 }
2643 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002644}
2645
2646
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002647bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2648 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002649 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002650 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002651 // inserted to calculate the final address. Due to this reason, the function
2652 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002653 // We used to assert if the offset was not properly aligned, however,
2654 // there are cases where a misaligned pointer recast can cause this
2655 // problem, and we need to allow for it. The front end warns of such
2656 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002657
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002658 switch (Opcode) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002659 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002660 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002661 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002662 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002663 case Hexagon::V6_vL32b_ai:
2664 case Hexagon::V6_vS32b_ai:
2665 case Hexagon::V6_vL32Ub_ai:
2666 case Hexagon::V6_vS32Ub_ai:
2667 return (Offset >= Hexagon_MEMV_OFFSET_MIN) &&
2668 (Offset <= Hexagon_MEMV_OFFSET_MAX);
2669
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002670 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002671 case Hexagon::PS_vstorerw_ai_128B:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002672 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002673 case Hexagon::PS_vloadrw_ai_128B:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002674 case Hexagon::V6_vL32b_ai_128B:
2675 case Hexagon::V6_vS32b_ai_128B:
2676 case Hexagon::V6_vL32Ub_ai_128B:
2677 case Hexagon::V6_vS32Ub_ai_128B:
2678 return (Offset >= Hexagon_MEMV_OFFSET_MIN_128B) &&
2679 (Offset <= Hexagon_MEMV_OFFSET_MAX_128B);
2680
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002681 case Hexagon::J2_loop0i:
2682 case Hexagon::J2_loop1i:
2683 return isUInt<10>(Offset);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00002684
2685 case Hexagon::S4_storeirb_io:
2686 case Hexagon::S4_storeirbt_io:
2687 case Hexagon::S4_storeirbf_io:
2688 return isUInt<6>(Offset);
2689
2690 case Hexagon::S4_storeirh_io:
2691 case Hexagon::S4_storeirht_io:
2692 case Hexagon::S4_storeirhf_io:
2693 return isShiftedUInt<6,1>(Offset);
2694
2695 case Hexagon::S4_storeiri_io:
2696 case Hexagon::S4_storeirit_io:
2697 case Hexagon::S4_storeirif_io:
2698 return isShiftedUInt<6,2>(Offset);
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002699 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002700
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002701 if (Extend)
2702 return true;
2703
2704 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002705 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002706 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002707 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2708 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2709
Colin LeMahieu947cd702014-12-23 20:44:59 +00002710 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002711 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002712 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2713 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2714
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002715 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002716 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002717 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002718 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2719 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2720
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002721 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002722 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002723 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002724 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2725 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2726
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002727 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002728 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2729 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2730
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002731 case Hexagon::L4_iadd_memopw_io :
2732 case Hexagon::L4_isub_memopw_io :
2733 case Hexagon::L4_add_memopw_io :
2734 case Hexagon::L4_sub_memopw_io :
2735 case Hexagon::L4_and_memopw_io :
2736 case Hexagon::L4_or_memopw_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002737 return (0 <= Offset && Offset <= 255);
2738
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002739 case Hexagon::L4_iadd_memoph_io :
2740 case Hexagon::L4_isub_memoph_io :
2741 case Hexagon::L4_add_memoph_io :
2742 case Hexagon::L4_sub_memoph_io :
2743 case Hexagon::L4_and_memoph_io :
2744 case Hexagon::L4_or_memoph_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002745 return (0 <= Offset && Offset <= 127);
2746
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002747 case Hexagon::L4_iadd_memopb_io :
2748 case Hexagon::L4_isub_memopb_io :
2749 case Hexagon::L4_add_memopb_io :
2750 case Hexagon::L4_sub_memopb_io :
2751 case Hexagon::L4_and_memopb_io :
2752 case Hexagon::L4_or_memopb_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002753 return (0 <= Offset && Offset <= 63);
2754
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002755 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002756 // any size. Later pass knows how to handle it.
2757 case Hexagon::STriw_pred:
2758 case Hexagon::LDriw_pred:
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00002759 case Hexagon::STriw_mod:
2760 case Hexagon::LDriw_mod:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002761 return true;
2762
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002763 case Hexagon::TFR_FI:
2764 case Hexagon::TFR_FIA:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002765 case Hexagon::INLINEASM:
2766 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002767
2768 case Hexagon::L2_ploadrbt_io:
2769 case Hexagon::L2_ploadrbf_io:
2770 case Hexagon::L2_ploadrubt_io:
2771 case Hexagon::L2_ploadrubf_io:
2772 case Hexagon::S2_pstorerbt_io:
2773 case Hexagon::S2_pstorerbf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002774 return isUInt<6>(Offset);
2775
2776 case Hexagon::L2_ploadrht_io:
2777 case Hexagon::L2_ploadrhf_io:
2778 case Hexagon::L2_ploadruht_io:
2779 case Hexagon::L2_ploadruhf_io:
2780 case Hexagon::S2_pstorerht_io:
2781 case Hexagon::S2_pstorerhf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002782 return isShiftedUInt<6,1>(Offset);
2783
2784 case Hexagon::L2_ploadrit_io:
2785 case Hexagon::L2_ploadrif_io:
2786 case Hexagon::S2_pstorerit_io:
2787 case Hexagon::S2_pstorerif_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002788 return isShiftedUInt<6,2>(Offset);
2789
2790 case Hexagon::L2_ploadrdt_io:
2791 case Hexagon::L2_ploadrdf_io:
2792 case Hexagon::S2_pstorerdt_io:
2793 case Hexagon::S2_pstorerdf_io:
2794 return isShiftedUInt<6,3>(Offset);
2795 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002796
Benjamin Kramerb6684012011-12-27 11:41:05 +00002797 llvm_unreachable("No offset range is defined for this opcode. "
2798 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002799}
2800
2801
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002802bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
2803 return isV60VectorInstruction(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002804}
2805
2806
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002807bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2808 const uint64_t F = get(MI.getOpcode()).TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002809 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2810 return
2811 V == HexagonII::TypeCVI_VA ||
2812 V == HexagonII::TypeCVI_VA_DV;
2813}
Andrew Trickd06df962012-02-01 22:13:57 +00002814
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002815
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002816bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2817 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002818 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2819 return true;
2820
2821 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2822 return true;
2823
2824 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002825 return true;
2826
2827 return false;
2828}
Jyotsna Verma84256432013-03-01 17:37:13 +00002829
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002830bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2831 switch (MI.getOpcode()) {
2832 // Byte
2833 case Hexagon::L2_loadrub_io:
2834 case Hexagon::L4_loadrub_ur:
2835 case Hexagon::L4_loadrub_ap:
2836 case Hexagon::L2_loadrub_pr:
2837 case Hexagon::L2_loadrub_pbr:
2838 case Hexagon::L2_loadrub_pi:
2839 case Hexagon::L2_loadrub_pci:
2840 case Hexagon::L2_loadrub_pcr:
2841 case Hexagon::L2_loadbzw2_io:
2842 case Hexagon::L4_loadbzw2_ur:
2843 case Hexagon::L4_loadbzw2_ap:
2844 case Hexagon::L2_loadbzw2_pr:
2845 case Hexagon::L2_loadbzw2_pbr:
2846 case Hexagon::L2_loadbzw2_pi:
2847 case Hexagon::L2_loadbzw2_pci:
2848 case Hexagon::L2_loadbzw2_pcr:
2849 case Hexagon::L2_loadbzw4_io:
2850 case Hexagon::L4_loadbzw4_ur:
2851 case Hexagon::L4_loadbzw4_ap:
2852 case Hexagon::L2_loadbzw4_pr:
2853 case Hexagon::L2_loadbzw4_pbr:
2854 case Hexagon::L2_loadbzw4_pi:
2855 case Hexagon::L2_loadbzw4_pci:
2856 case Hexagon::L2_loadbzw4_pcr:
2857 case Hexagon::L4_loadrub_rr:
2858 case Hexagon::L2_ploadrubt_io:
2859 case Hexagon::L2_ploadrubt_pi:
2860 case Hexagon::L2_ploadrubf_io:
2861 case Hexagon::L2_ploadrubf_pi:
2862 case Hexagon::L2_ploadrubtnew_io:
2863 case Hexagon::L2_ploadrubfnew_io:
2864 case Hexagon::L4_ploadrubt_rr:
2865 case Hexagon::L4_ploadrubf_rr:
2866 case Hexagon::L4_ploadrubtnew_rr:
2867 case Hexagon::L4_ploadrubfnew_rr:
2868 case Hexagon::L2_ploadrubtnew_pi:
2869 case Hexagon::L2_ploadrubfnew_pi:
2870 case Hexagon::L4_ploadrubt_abs:
2871 case Hexagon::L4_ploadrubf_abs:
2872 case Hexagon::L4_ploadrubtnew_abs:
2873 case Hexagon::L4_ploadrubfnew_abs:
2874 case Hexagon::L2_loadrubgp:
2875 // Half
2876 case Hexagon::L2_loadruh_io:
2877 case Hexagon::L4_loadruh_ur:
2878 case Hexagon::L4_loadruh_ap:
2879 case Hexagon::L2_loadruh_pr:
2880 case Hexagon::L2_loadruh_pbr:
2881 case Hexagon::L2_loadruh_pi:
2882 case Hexagon::L2_loadruh_pci:
2883 case Hexagon::L2_loadruh_pcr:
2884 case Hexagon::L4_loadruh_rr:
2885 case Hexagon::L2_ploadruht_io:
2886 case Hexagon::L2_ploadruht_pi:
2887 case Hexagon::L2_ploadruhf_io:
2888 case Hexagon::L2_ploadruhf_pi:
2889 case Hexagon::L2_ploadruhtnew_io:
2890 case Hexagon::L2_ploadruhfnew_io:
2891 case Hexagon::L4_ploadruht_rr:
2892 case Hexagon::L4_ploadruhf_rr:
2893 case Hexagon::L4_ploadruhtnew_rr:
2894 case Hexagon::L4_ploadruhfnew_rr:
2895 case Hexagon::L2_ploadruhtnew_pi:
2896 case Hexagon::L2_ploadruhfnew_pi:
2897 case Hexagon::L4_ploadruht_abs:
2898 case Hexagon::L4_ploadruhf_abs:
2899 case Hexagon::L4_ploadruhtnew_abs:
2900 case Hexagon::L4_ploadruhfnew_abs:
2901 case Hexagon::L2_loadruhgp:
2902 return true;
2903 default:
2904 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002905 }
2906}
2907
2908
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002909// Add latency to instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002910bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2911 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002912 if (isV60VectorInstruction(MI1) && isV60VectorInstruction(MI2))
2913 if (!isVecUsableNextPacket(MI1, MI2))
2914 return true;
2915 return false;
2916}
2917
2918
Brendon Cahoon254f8892016-07-29 16:44:44 +00002919/// \brief Get the base register and byte offset of a load/store instr.
2920bool HexagonInstrInfo::getMemOpBaseRegImmOfs(MachineInstr &LdSt,
2921 unsigned &BaseReg, int64_t &Offset, const TargetRegisterInfo *TRI)
2922 const {
2923 unsigned AccessSize = 0;
2924 int OffsetVal = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002925 BaseReg = getBaseAndOffset(LdSt, OffsetVal, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002926 Offset = OffsetVal;
2927 return BaseReg != 0;
2928}
2929
2930
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002931/// \brief Can these instructions execute at the same time in a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002932bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2933 const MachineInstr &Second) const {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002934 if (DisableNVSchedule)
2935 return false;
2936 if (mayBeNewStore(Second)) {
2937 // Make sure the definition of the first instruction is the value being
2938 // stored.
2939 const MachineOperand &Stored =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002940 Second.getOperand(Second.getNumOperands() - 1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002941 if (!Stored.isReg())
2942 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002943 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
2944 const MachineOperand &Op = First.getOperand(i);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002945 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2946 return true;
2947 }
2948 }
2949 return false;
2950}
2951
2952
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002953bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
2954 unsigned Opc = CallMI.getOpcode();
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002955 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002956}
2957
2958
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002959bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2960 for (auto &I : *B)
2961 if (I.isEHLabel())
2962 return true;
2963 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002964}
2965
Jyotsna Verma84256432013-03-01 17:37:13 +00002966
2967// Returns true if an instruction can be converted into a non-extended
2968// equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002969bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002970 short NonExtOpcode;
2971 // Check if the instruction has a register form that uses register in place
2972 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002973 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
Jyotsna Verma84256432013-03-01 17:37:13 +00002974 return true;
2975
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002976 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002977 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002978
2979 switch (getAddrMode(MI)) {
2980 case HexagonII::Absolute :
2981 // Load/store with absolute addressing mode can be converted into
2982 // base+offset mode.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002983 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002984 break;
2985 case HexagonII::BaseImmOffset :
2986 // Load/store with base+offset addressing mode can be converted into
2987 // base+register offset addressing mode. However left shift operand should
2988 // be set to 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002989 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002990 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002991 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002992 NonExtOpcode = Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002993 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002994 default:
2995 return false;
2996 }
2997 if (NonExtOpcode < 0)
2998 return false;
2999 return true;
3000 }
3001 return false;
3002}
3003
Jyotsna Verma84256432013-03-01 17:37:13 +00003004
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003005bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
3006 return Hexagon::getRealHWInstr(MI.getOpcode(),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003007 Hexagon::InstrType_Pseudo) >= 0;
3008}
3009
3010
3011bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
3012 const {
3013 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
3014 while (I != E) {
3015 if (I->isBarrier())
3016 return true;
3017 ++I;
3018 }
3019 return false;
3020}
3021
3022
3023// Returns true, if a LD insn can be promoted to a cur load.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003024bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
3025 auto &HST = MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>();
3026 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003027 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
3028 HST.hasV60TOps();
3029}
3030
3031
3032// Returns true, if a ST insn can be promoted to a new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003033bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
3034 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003035 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
3036}
3037
3038
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003039bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
3040 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003041 // There is no stall when ProdMI is not a V60 vector.
3042 if (!isV60VectorInstruction(ProdMI))
3043 return false;
3044
3045 // There is no stall when ProdMI and ConsMI are not dependent.
3046 if (!isDependent(ProdMI, ConsMI))
3047 return false;
3048
3049 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
3050 // are scheduled in consecutive packets.
3051 if (isVecUsableNextPacket(ProdMI, ConsMI))
3052 return false;
3053
3054 return true;
3055}
3056
3057
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003058bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003059 MachineBasicBlock::const_instr_iterator BII) const {
3060 // There is no stall when I is not a V60 vector.
3061 if (!isV60VectorInstruction(MI))
3062 return false;
3063
3064 MachineBasicBlock::const_instr_iterator MII = BII;
3065 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
3066
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003067 if (!MII->isBundle()) {
3068 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003069 if (!isV60VectorInstruction(J))
3070 return false;
3071 else if (isVecUsableNextPacket(J, MI))
3072 return false;
3073 return true;
3074 }
3075
3076 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003077 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003078 if (producesStall(J, MI))
3079 return true;
3080 }
3081 return false;
3082}
3083
3084
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003085bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003086 unsigned PredReg) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003087 for (unsigned opNum = 0; opNum < MI.getNumOperands(); opNum++) {
3088 const MachineOperand &MO = MI.getOperand(opNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003089 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
3090 return false; // Predicate register must be explicitly defined.
3091 }
3092
3093 // Hexagon Programmer's Reference says that decbin, memw_locked, and
3094 // memd_locked cannot be used as .new as well,
3095 // but we don't seem to have these instructions defined.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003096 return MI.getOpcode() != Hexagon::A4_tlbmatch;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003097}
3098
3099
3100bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
3101 return (Opcode == Hexagon::J2_jumpt) ||
3102 (Opcode == Hexagon::J2_jumpf) ||
3103 (Opcode == Hexagon::J2_jumptnew) ||
3104 (Opcode == Hexagon::J2_jumpfnew) ||
3105 (Opcode == Hexagon::J2_jumptnewpt) ||
3106 (Opcode == Hexagon::J2_jumpfnewpt);
3107}
3108
3109
3110bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
3111 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
3112 return false;
3113 return !isPredicatedTrue(Cond[0].getImm());
3114}
3115
3116
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003117short HexagonInstrInfo::getAbsoluteForm(const MachineInstr &MI) const {
3118 return Hexagon::getAbsoluteForm(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003119}
3120
3121
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003122unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
3123 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003124 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3125}
3126
3127
3128// Returns the base register in a memory access (load/store). The offset is
3129// returned in Offset and the access size is returned in AccessSize.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003130unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003131 int &Offset, unsigned &AccessSize) const {
3132 // Return if it is not a base+offset type instruction or a MemOp.
3133 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3134 getAddrMode(MI) != HexagonII::BaseLongOffset &&
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003135 !isMemOp(MI) && !isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003136 return 0;
3137
3138 // Since it is a memory access instruction, getMemAccessSize() should never
3139 // return 0.
3140 assert (getMemAccessSize(MI) &&
3141 "BaseImmOffset or BaseLongOffset or MemOp without accessSize");
3142
3143 // Return Values of getMemAccessSize() are
3144 // 0 - Checked in the assert above.
3145 // 1, 2, 3, 4 & 7, 8 - The statement below is correct for all these.
3146 // MemAccessSize is represented as 1+log2(N) where N is size in bits.
3147 AccessSize = (1U << (getMemAccessSize(MI) - 1));
3148
3149 unsigned basePos = 0, offsetPos = 0;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003150 if (!getBaseAndOffsetPosition(MI, basePos, offsetPos))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003151 return 0;
3152
3153 // Post increment updates its EA after the mem access,
3154 // so we need to treat its offset as zero.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003155 if (isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003156 Offset = 0;
3157 else {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003158 Offset = MI.getOperand(offsetPos).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003159 }
3160
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003161 return MI.getOperand(basePos).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003162}
3163
3164
3165/// Return the position of the base and offset operands for this instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003166bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003167 unsigned &BasePos, unsigned &OffsetPos) const {
3168 // Deal with memops first.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003169 if (isMemOp(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003170 BasePos = 0;
3171 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003172 } else if (MI.mayStore()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003173 BasePos = 0;
3174 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003175 } else if (MI.mayLoad()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003176 BasePos = 1;
3177 OffsetPos = 2;
3178 } else
3179 return false;
3180
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003181 if (isPredicated(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003182 BasePos++;
3183 OffsetPos++;
3184 }
3185 if (isPostIncrement(MI)) {
3186 BasePos++;
3187 OffsetPos++;
3188 }
3189
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003190 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003191 return false;
3192
3193 return true;
3194}
3195
3196
3197// Inserts branching instructions in reverse order of their occurence.
3198// e.g. jump_t t1 (i1)
3199// jump t2 (i2)
3200// Jumpers = {i2, i1}
3201SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3202 MachineBasicBlock& MBB) const {
3203 SmallVector<MachineInstr*, 2> Jumpers;
3204 // If the block has no terminators, it just falls into the block after it.
3205 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3206 if (I == MBB.instr_begin())
3207 return Jumpers;
3208
3209 // A basic block may looks like this:
3210 //
3211 // [ insn
3212 // EH_LABEL
3213 // insn
3214 // insn
3215 // insn
3216 // EH_LABEL
3217 // insn ]
3218 //
3219 // It has two succs but does not have a terminator
3220 // Don't know how to handle it.
3221 do {
3222 --I;
3223 if (I->isEHLabel())
3224 return Jumpers;
3225 } while (I != MBB.instr_begin());
3226
3227 I = MBB.instr_end();
3228 --I;
3229
3230 while (I->isDebugValue()) {
3231 if (I == MBB.instr_begin())
3232 return Jumpers;
3233 --I;
3234 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003235 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003236 return Jumpers;
3237
3238 // Get the last instruction in the block.
3239 MachineInstr *LastInst = &*I;
3240 Jumpers.push_back(LastInst);
3241 MachineInstr *SecondLastInst = nullptr;
3242 // Find one more terminator if present.
3243 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003244 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003245 if (!SecondLastInst) {
3246 SecondLastInst = &*I;
3247 Jumpers.push_back(SecondLastInst);
3248 } else // This is a third branch.
3249 return Jumpers;
3250 }
3251 if (I == MBB.instr_begin())
3252 break;
3253 --I;
3254 } while (true);
3255 return Jumpers;
3256}
3257
3258
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003259short HexagonInstrInfo::getBaseWithLongOffset(short Opcode) const {
3260 if (Opcode < 0)
3261 return -1;
3262 return Hexagon::getBaseWithLongOffset(Opcode);
3263}
3264
3265
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003266short HexagonInstrInfo::getBaseWithLongOffset(const MachineInstr &MI) const {
3267 return Hexagon::getBaseWithLongOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003268}
3269
3270
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003271short HexagonInstrInfo::getBaseWithRegOffset(const MachineInstr &MI) const {
3272 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003273}
3274
3275
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003276// Returns Operand Index for the constant extended instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003277unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
3278 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003279 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3280}
3281
3282// See if instruction could potentially be a duplex candidate.
3283// If so, return its group. Zero otherwise.
3284HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003285 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003286 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3287
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003288 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003289 default:
3290 return HexagonII::HCG_None;
3291 //
3292 // Compound pairs.
3293 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3294 // "Rd16=#U6 ; jump #r9:2"
3295 // "Rd16=Rs16 ; jump #r9:2"
3296 //
3297 case Hexagon::C2_cmpeq:
3298 case Hexagon::C2_cmpgt:
3299 case Hexagon::C2_cmpgtu:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003300 DstReg = MI.getOperand(0).getReg();
3301 Src1Reg = MI.getOperand(1).getReg();
3302 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003303 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3304 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3305 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3306 return HexagonII::HCG_A;
3307 break;
3308 case Hexagon::C2_cmpeqi:
3309 case Hexagon::C2_cmpgti:
3310 case Hexagon::C2_cmpgtui:
3311 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003312 DstReg = MI.getOperand(0).getReg();
3313 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003314 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3315 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003316 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3317 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3318 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003319 return HexagonII::HCG_A;
3320 break;
3321 case Hexagon::A2_tfr:
3322 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003323 DstReg = MI.getOperand(0).getReg();
3324 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003325 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3326 return HexagonII::HCG_A;
3327 break;
3328 case Hexagon::A2_tfrsi:
3329 // Rd = #u6
3330 // Do not test for #u6 size since the const is getting extended
3331 // regardless and compound could be formed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003332 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003333 if (isIntRegForSubInst(DstReg))
3334 return HexagonII::HCG_A;
3335 break;
3336 case Hexagon::S2_tstbit_i:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003337 DstReg = MI.getOperand(0).getReg();
3338 Src1Reg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003339 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3340 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003341 MI.getOperand(2).isImm() &&
3342 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003343 return HexagonII::HCG_A;
3344 break;
3345 // The fact that .new form is used pretty much guarantees
3346 // that predicate register will match. Nevertheless,
3347 // there could be some false positives without additional
3348 // checking.
3349 case Hexagon::J2_jumptnew:
3350 case Hexagon::J2_jumpfnew:
3351 case Hexagon::J2_jumptnewpt:
3352 case Hexagon::J2_jumpfnewpt:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003353 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003354 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3355 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3356 return HexagonII::HCG_B;
3357 break;
3358 // Transfer and jump:
3359 // Rd=#U6 ; jump #r9:2
3360 // Rd=Rs ; jump #r9:2
3361 // Do not test for jump range here.
3362 case Hexagon::J2_jump:
3363 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3364 return HexagonII::HCG_C;
3365 break;
3366 }
3367
3368 return HexagonII::HCG_None;
3369}
3370
3371
3372// Returns -1 when there is no opcode found.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003373unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3374 const MachineInstr &GB) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003375 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3376 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003377 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3378 (GB.getOpcode() != Hexagon::J2_jumptnew))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003379 return -1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003380 unsigned DestReg = GA.getOperand(0).getReg();
3381 if (!GB.readsRegister(DestReg))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003382 return -1;
3383 if (DestReg == Hexagon::P0)
3384 return Hexagon::J4_cmpeqi_tp0_jump_nt;
3385 if (DestReg == Hexagon::P1)
3386 return Hexagon::J4_cmpeqi_tp1_jump_nt;
3387 return -1;
3388}
3389
3390
3391int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3392 enum Hexagon::PredSense inPredSense;
3393 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3394 Hexagon::PredSense_true;
3395 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3396 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3397 return CondOpcode;
3398
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003399 llvm_unreachable("Unexpected predicable instruction");
3400}
3401
3402
3403// Return the cur value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003404int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3405 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003406 default: llvm_unreachable("Unknown .cur type");
3407 case Hexagon::V6_vL32b_pi:
3408 return Hexagon::V6_vL32b_cur_pi;
3409 case Hexagon::V6_vL32b_ai:
3410 return Hexagon::V6_vL32b_cur_ai;
3411 //128B
3412 case Hexagon::V6_vL32b_pi_128B:
3413 return Hexagon::V6_vL32b_cur_pi_128B;
3414 case Hexagon::V6_vL32b_ai_128B:
3415 return Hexagon::V6_vL32b_cur_ai_128B;
3416 }
3417 return 0;
3418}
3419
3420
3421
3422// The diagram below shows the steps involved in the conversion of a predicated
3423// store instruction to its .new predicated new-value form.
3424//
3425// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3426// ^ ^
3427// / \ (not OK. it will cause new-value store to be
3428// / X conditional on p0.new while R2 producer is
3429// / \ on p0)
3430// / \.
3431// p.new store p.old NV store
3432// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3433// ^ ^
3434// \ /
3435// \ /
3436// \ /
3437// p.old store
3438// [if (p0)memw(R0+#0)=R2]
3439//
3440//
3441// The following set of instructions further explains the scenario where
3442// conditional new-value store becomes invalid when promoted to .new predicate
3443// form.
3444//
3445// { 1) if (p0) r0 = add(r1, r2)
3446// 2) p0 = cmp.eq(r3, #0) }
3447//
3448// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3449// the first two instructions because in instr 1, r0 is conditional on old value
3450// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3451// is not valid for new-value stores.
3452// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3453// from the "Conditional Store" list. Because a predicated new value store
3454// would NOT be promoted to a double dot new store. See diagram below:
3455// This function returns yes for those stores that are predicated but not
3456// yet promoted to predicate dot new instructions.
3457//
3458// +---------------------+
3459// /-----| if (p0) memw(..)=r0 |---------\~
3460// || +---------------------+ ||
3461// promote || /\ /\ || promote
3462// || /||\ /||\ ||
3463// \||/ demote || \||/
3464// \/ || || \/
3465// +-------------------------+ || +-------------------------+
3466// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3467// +-------------------------+ || +-------------------------+
3468// || || ||
3469// || demote \||/
3470// promote || \/ NOT possible
3471// || || /\~
3472// \||/ || /||\~
3473// \/ || ||
3474// +-----------------------------+
3475// | if (p0.new) memw(..)=r0.new |
3476// +-----------------------------+
3477// Double Dot New Store
3478//
3479// Returns the most basic instruction for the .new predicated instructions and
3480// new-value stores.
3481// For example, all of the following instructions will be converted back to the
3482// same instruction:
3483// 1) if (p0.new) memw(R0+#0) = R1.new --->
3484// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3485// 3) if (p0.new) memw(R0+#0) = R1 --->
3486//
3487// To understand the translation of instruction 1 to its original form, consider
3488// a packet with 3 instructions.
3489// { p0 = cmp.eq(R0,R1)
3490// if (p0.new) R2 = add(R3, R4)
3491// R5 = add (R3, R1)
3492// }
3493// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3494//
3495// This instruction can be part of the previous packet only if both p0 and R2
3496// are promoted to .new values. This promotion happens in steps, first
3497// predicate register is promoted to .new and in the next iteration R2 is
3498// promoted. Therefore, in case of dependence check failure (due to R5) during
3499// next iteration, it should be converted back to its most basic form.
3500
3501
3502// Return the new value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003503int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3504 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003505 if (NVOpcode >= 0) // Valid new-value store instruction.
3506 return NVOpcode;
3507
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003508 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003509 default: llvm_unreachable("Unknown .new type");
3510 case Hexagon::S4_storerb_ur:
3511 return Hexagon::S4_storerbnew_ur;
3512
3513 case Hexagon::S2_storerb_pci:
3514 return Hexagon::S2_storerb_pci;
3515
3516 case Hexagon::S2_storeri_pci:
3517 return Hexagon::S2_storeri_pci;
3518
3519 case Hexagon::S2_storerh_pci:
3520 return Hexagon::S2_storerh_pci;
3521
3522 case Hexagon::S2_storerd_pci:
3523 return Hexagon::S2_storerd_pci;
3524
3525 case Hexagon::S2_storerf_pci:
3526 return Hexagon::S2_storerf_pci;
3527
3528 case Hexagon::V6_vS32b_ai:
3529 return Hexagon::V6_vS32b_new_ai;
3530
3531 case Hexagon::V6_vS32b_pi:
3532 return Hexagon::V6_vS32b_new_pi;
3533
3534 // 128B
3535 case Hexagon::V6_vS32b_ai_128B:
3536 return Hexagon::V6_vS32b_new_ai_128B;
3537
3538 case Hexagon::V6_vS32b_pi_128B:
3539 return Hexagon::V6_vS32b_new_pi_128B;
3540 }
3541 return 0;
3542}
3543
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00003544
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003545// Returns the opcode to use when converting MI, which is a conditional jump,
3546// into a conditional instruction which uses the .new value of the predicate.
3547// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003548int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003549 const MachineBranchProbabilityInfo *MBPI) const {
3550 // We assume that block can have at most two successors.
3551 bool taken = false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003552 const MachineBasicBlock *Src = MI.getParent();
3553 const MachineOperand &BrTarget = MI.getOperand(1);
3554 const MachineBasicBlock *Dst = BrTarget.getMBB();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003555
3556 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
3557 if (Prediction >= BranchProbability(1,2))
3558 taken = true;
3559
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003560 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003561 case Hexagon::J2_jumpt:
3562 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3563 case Hexagon::J2_jumpf:
3564 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3565
3566 default:
3567 llvm_unreachable("Unexpected jump instruction.");
3568 }
3569}
3570
3571
3572// Return .new predicate version for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003573int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003574 const MachineBranchProbabilityInfo *MBPI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003575 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003576 if (NewOpcode >= 0) // Valid predicate new instruction
3577 return NewOpcode;
3578
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003579 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003580 // Condtional Jumps
3581 case Hexagon::J2_jumpt:
3582 case Hexagon::J2_jumpf:
3583 return getDotNewPredJumpOp(MI, MBPI);
3584
3585 default:
3586 assert(0 && "Unknown .new type");
3587 }
3588 return 0;
3589}
3590
3591
3592int HexagonInstrInfo::getDotOldOp(const int opc) const {
3593 int NewOp = opc;
3594 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3595 NewOp = Hexagon::getPredOldOpcode(NewOp);
3596 assert(NewOp >= 0 &&
3597 "Couldn't change predicate new instruction to its old form.");
3598 }
3599
3600 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3601 NewOp = Hexagon::getNonNVStore(NewOp);
3602 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3603 }
3604 return NewOp;
3605}
3606
3607
3608// See if instruction could potentially be a duplex candidate.
3609// If so, return its group. Zero otherwise.
3610HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003611 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003612 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3613 auto &HRI = getRegisterInfo();
3614
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003615 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003616 default:
3617 return HexagonII::HSIG_None;
3618 //
3619 // Group L1:
3620 //
3621 // Rd = memw(Rs+#u4:2)
3622 // Rd = memub(Rs+#u4:0)
3623 case Hexagon::L2_loadri_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003624 DstReg = MI.getOperand(0).getReg();
3625 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003626 // Special case this one from Group L2.
3627 // Rd = memw(r29+#u5:2)
3628 if (isIntRegForSubInst(DstReg)) {
3629 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3630 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003631 MI.getOperand(2).isImm() &&
3632 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003633 return HexagonII::HSIG_L2;
3634 // Rd = memw(Rs+#u4:2)
3635 if (isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003636 (MI.getOperand(2).isImm() &&
3637 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003638 return HexagonII::HSIG_L1;
3639 }
3640 break;
3641 case Hexagon::L2_loadrub_io:
3642 // Rd = memub(Rs+#u4:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003643 DstReg = MI.getOperand(0).getReg();
3644 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003645 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003646 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003647 return HexagonII::HSIG_L1;
3648 break;
3649 //
3650 // Group L2:
3651 //
3652 // Rd = memh/memuh(Rs+#u3:1)
3653 // Rd = memb(Rs+#u3:0)
3654 // Rd = memw(r29+#u5:2) - Handled above.
3655 // Rdd = memd(r29+#u5:3)
3656 // deallocframe
3657 // [if ([!]p0[.new])] dealloc_return
3658 // [if ([!]p0[.new])] jumpr r31
3659 case Hexagon::L2_loadrh_io:
3660 case Hexagon::L2_loadruh_io:
3661 // Rd = memh/memuh(Rs+#u3:1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003662 DstReg = MI.getOperand(0).getReg();
3663 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003664 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003665 MI.getOperand(2).isImm() &&
3666 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003667 return HexagonII::HSIG_L2;
3668 break;
3669 case Hexagon::L2_loadrb_io:
3670 // Rd = memb(Rs+#u3:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003671 DstReg = MI.getOperand(0).getReg();
3672 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003673 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003674 MI.getOperand(2).isImm() &&
3675 isUInt<3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003676 return HexagonII::HSIG_L2;
3677 break;
3678 case Hexagon::L2_loadrd_io:
3679 // Rdd = memd(r29+#u5:3)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003680 DstReg = MI.getOperand(0).getReg();
3681 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003682 if (isDblRegForSubInst(DstReg, HRI) &&
3683 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3684 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003685 MI.getOperand(2).isImm() &&
3686 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003687 return HexagonII::HSIG_L2;
3688 break;
3689 // dealloc_return is not documented in Hexagon Manual, but marked
3690 // with A_SUBINSN attribute in iset_v4classic.py.
3691 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3692 case Hexagon::L4_return:
3693 case Hexagon::L2_deallocframe:
3694 return HexagonII::HSIG_L2;
3695 case Hexagon::EH_RETURN_JMPR:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003696 case Hexagon::PS_jmpret:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003697 // jumpr r31
3698 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003699 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003700 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3701 return HexagonII::HSIG_L2;
3702 break;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003703 case Hexagon::PS_jmprett:
3704 case Hexagon::PS_jmpretf:
3705 case Hexagon::PS_jmprettnewpt:
3706 case Hexagon::PS_jmpretfnewpt:
3707 case Hexagon::PS_jmprettnew:
3708 case Hexagon::PS_jmpretfnew:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003709 DstReg = MI.getOperand(1).getReg();
3710 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003711 // [if ([!]p0[.new])] jumpr r31
3712 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3713 (Hexagon::P0 == SrcReg)) &&
3714 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3715 return HexagonII::HSIG_L2;
3716 break;
3717 case Hexagon::L4_return_t :
3718 case Hexagon::L4_return_f :
3719 case Hexagon::L4_return_tnew_pnt :
3720 case Hexagon::L4_return_fnew_pnt :
3721 case Hexagon::L4_return_tnew_pt :
3722 case Hexagon::L4_return_fnew_pt :
3723 // [if ([!]p0[.new])] dealloc_return
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003724 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003725 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3726 return HexagonII::HSIG_L2;
3727 break;
3728 //
3729 // Group S1:
3730 //
3731 // memw(Rs+#u4:2) = Rt
3732 // memb(Rs+#u4:0) = Rt
3733 case Hexagon::S2_storeri_io:
3734 // Special case this one from Group S2.
3735 // memw(r29+#u5:2) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003736 Src1Reg = MI.getOperand(0).getReg();
3737 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003738 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3739 isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003740 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3741 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003742 return HexagonII::HSIG_S2;
3743 // memw(Rs+#u4:2) = Rt
3744 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003745 MI.getOperand(1).isImm() &&
3746 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003747 return HexagonII::HSIG_S1;
3748 break;
3749 case Hexagon::S2_storerb_io:
3750 // memb(Rs+#u4:0) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003751 Src1Reg = MI.getOperand(0).getReg();
3752 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003753 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003754 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003755 return HexagonII::HSIG_S1;
3756 break;
3757 //
3758 // Group S2:
3759 //
3760 // memh(Rs+#u3:1) = Rt
3761 // memw(r29+#u5:2) = Rt
3762 // memd(r29+#s6:3) = Rtt
3763 // memw(Rs+#u4:2) = #U1
3764 // memb(Rs+#u4) = #U1
3765 // allocframe(#u5:3)
3766 case Hexagon::S2_storerh_io:
3767 // memh(Rs+#u3:1) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003768 Src1Reg = MI.getOperand(0).getReg();
3769 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003770 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003771 MI.getOperand(1).isImm() &&
3772 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003773 return HexagonII::HSIG_S1;
3774 break;
3775 case Hexagon::S2_storerd_io:
3776 // memd(r29+#s6:3) = Rtt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003777 Src1Reg = MI.getOperand(0).getReg();
3778 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003779 if (isDblRegForSubInst(Src2Reg, HRI) &&
3780 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003781 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3782 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003783 return HexagonII::HSIG_S2;
3784 break;
3785 case Hexagon::S4_storeiri_io:
3786 // memw(Rs+#u4:2) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003787 Src1Reg = MI.getOperand(0).getReg();
3788 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
3789 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
3790 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003791 return HexagonII::HSIG_S2;
3792 break;
3793 case Hexagon::S4_storeirb_io:
3794 // memb(Rs+#u4) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003795 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003796 if (isIntRegForSubInst(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003797 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
3798 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003799 return HexagonII::HSIG_S2;
3800 break;
3801 case Hexagon::S2_allocframe:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003802 if (MI.getOperand(0).isImm() &&
3803 isShiftedUInt<5,3>(MI.getOperand(0).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003804 return HexagonII::HSIG_S1;
3805 break;
3806 //
3807 // Group A:
3808 //
3809 // Rx = add(Rx,#s7)
3810 // Rd = Rs
3811 // Rd = #u6
3812 // Rd = #-1
3813 // if ([!]P0[.new]) Rd = #0
3814 // Rd = add(r29,#u6:2)
3815 // Rx = add(Rx,Rs)
3816 // P0 = cmp.eq(Rs,#u2)
3817 // Rdd = combine(#0,Rs)
3818 // Rdd = combine(Rs,#0)
3819 // Rdd = combine(#u2,#U2)
3820 // Rd = add(Rs,#1)
3821 // Rd = add(Rs,#-1)
3822 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3823 // Rd = and(Rs,#1)
3824 case Hexagon::A2_addi:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003825 DstReg = MI.getOperand(0).getReg();
3826 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003827 if (isIntRegForSubInst(DstReg)) {
3828 // Rd = add(r29,#u6:2)
3829 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003830 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
3831 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003832 return HexagonII::HSIG_A;
3833 // Rx = add(Rx,#s7)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003834 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
3835 isInt<7>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003836 return HexagonII::HSIG_A;
3837 // Rd = add(Rs,#1)
3838 // Rd = add(Rs,#-1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003839 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3840 ((MI.getOperand(2).getImm() == 1) ||
3841 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003842 return HexagonII::HSIG_A;
3843 }
3844 break;
3845 case Hexagon::A2_add:
3846 // Rx = add(Rx,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003847 DstReg = MI.getOperand(0).getReg();
3848 Src1Reg = MI.getOperand(1).getReg();
3849 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003850 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3851 isIntRegForSubInst(Src2Reg))
3852 return HexagonII::HSIG_A;
3853 break;
3854 case Hexagon::A2_andir:
3855 // Same as zxtb.
3856 // Rd16=and(Rs16,#255)
3857 // Rd16=and(Rs16,#1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003858 DstReg = MI.getOperand(0).getReg();
3859 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003860 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003861 MI.getOperand(2).isImm() &&
3862 ((MI.getOperand(2).getImm() == 1) ||
3863 (MI.getOperand(2).getImm() == 255)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003864 return HexagonII::HSIG_A;
3865 break;
3866 case Hexagon::A2_tfr:
3867 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003868 DstReg = MI.getOperand(0).getReg();
3869 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003870 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3871 return HexagonII::HSIG_A;
3872 break;
3873 case Hexagon::A2_tfrsi:
3874 // Rd = #u6
3875 // Do not test for #u6 size since the const is getting extended
3876 // regardless and compound could be formed.
3877 // Rd = #-1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003878 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003879 if (isIntRegForSubInst(DstReg))
3880 return HexagonII::HSIG_A;
3881 break;
3882 case Hexagon::C2_cmoveit:
3883 case Hexagon::C2_cmovenewit:
3884 case Hexagon::C2_cmoveif:
3885 case Hexagon::C2_cmovenewif:
3886 // if ([!]P0[.new]) Rd = #0
3887 // Actual form:
3888 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003889 DstReg = MI.getOperand(0).getReg();
3890 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003891 if (isIntRegForSubInst(DstReg) &&
3892 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003893 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003894 return HexagonII::HSIG_A;
3895 break;
3896 case Hexagon::C2_cmpeqi:
3897 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003898 DstReg = MI.getOperand(0).getReg();
3899 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003900 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3901 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003902 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003903 return HexagonII::HSIG_A;
3904 break;
3905 case Hexagon::A2_combineii:
3906 case Hexagon::A4_combineii:
3907 // Rdd = combine(#u2,#U2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003908 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003909 if (isDblRegForSubInst(DstReg, HRI) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003910 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
3911 (MI.getOperand(1).isGlobal() &&
3912 isUInt<2>(MI.getOperand(1).getOffset()))) &&
3913 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
3914 (MI.getOperand(2).isGlobal() &&
3915 isUInt<2>(MI.getOperand(2).getOffset()))))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003916 return HexagonII::HSIG_A;
3917 break;
3918 case Hexagon::A4_combineri:
3919 // Rdd = combine(Rs,#0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003920 DstReg = MI.getOperand(0).getReg();
3921 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003922 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003923 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
3924 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003925 return HexagonII::HSIG_A;
3926 break;
3927 case Hexagon::A4_combineir:
3928 // Rdd = combine(#0,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003929 DstReg = MI.getOperand(0).getReg();
3930 SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003931 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003932 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
3933 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003934 return HexagonII::HSIG_A;
3935 break;
3936 case Hexagon::A2_sxtb:
3937 case Hexagon::A2_sxth:
3938 case Hexagon::A2_zxtb:
3939 case Hexagon::A2_zxth:
3940 // Rd = sxth/sxtb/zxtb/zxth(Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003941 DstReg = MI.getOperand(0).getReg();
3942 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003943 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3944 return HexagonII::HSIG_A;
3945 break;
3946 }
3947
3948 return HexagonII::HSIG_None;
3949}
3950
3951
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003952short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
3953 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003954}
3955
3956
3957// Return first non-debug instruction in the basic block.
3958MachineInstr *HexagonInstrInfo::getFirstNonDbgInst(MachineBasicBlock *BB)
3959 const {
3960 for (auto MII = BB->instr_begin(), End = BB->instr_end(); MII != End; MII++) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003961 MachineInstr &MI = *MII;
3962 if (MI.isDebugValue())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003963 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003964 return &MI;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003965 }
3966 return nullptr;
3967}
3968
3969
3970unsigned HexagonInstrInfo::getInstrTimingClassLatency(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003971 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003972 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3973 // still have a MinLatency property, which getStageLatency checks.
3974 if (!ItinData)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003975 return getInstrLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003976
3977 // Get the latency embedded in the itinerary. If we're not using timing class
3978 // latencies or if we using BSB scheduling, then restrict the maximum latency
3979 // to 1 (that is, either 0 or 1).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003980 if (MI.isTransient())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003981 return 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003982 unsigned Latency = ItinData->getStageLatency(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003983 if (!EnableTimingClassLatency ||
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003984 MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>().
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003985 useBSBScheduling())
3986 if (Latency > 1)
3987 Latency = 1;
3988 return Latency;
3989}
3990
3991
3992// inverts the predication logic.
3993// p -> NotP
3994// NotP -> P
3995bool HexagonInstrInfo::getInvertedPredSense(
3996 SmallVectorImpl<MachineOperand> &Cond) const {
3997 if (Cond.empty())
3998 return false;
3999 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
4000 Cond[0].setImm(Opc);
4001 return true;
4002}
4003
4004
4005unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
4006 int InvPredOpcode;
4007 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
4008 : Hexagon::getTruePredOpcode(Opc);
4009 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
4010 return InvPredOpcode;
4011
4012 llvm_unreachable("Unexpected predicated instruction");
4013}
4014
4015
4016// Returns the max value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004017int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
4018 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004019 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4020 & HexagonII::ExtentSignedMask;
4021 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4022 & HexagonII::ExtentBitsMask;
4023
4024 if (isSigned) // if value is signed
4025 return ~(-1U << (bits - 1));
4026 else
4027 return ~(-1U << bits);
4028}
4029
4030
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004031unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
4032 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004033 return (F >> HexagonII::MemAccessSizePos) & HexagonII::MemAccesSizeMask;
4034}
4035
4036
4037// Returns the min value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004038int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
4039 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004040 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4041 & HexagonII::ExtentSignedMask;
4042 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4043 & HexagonII::ExtentBitsMask;
4044
4045 if (isSigned) // if value is signed
4046 return -1U << (bits - 1);
4047 else
4048 return 0;
4049}
4050
4051
4052// Returns opcode of the non-extended equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004053short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00004054 // Check if the instruction has a register form that uses register in place
4055 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004056 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004057 if (NonExtOpcode >= 0)
4058 return NonExtOpcode;
4059
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004060 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00004061 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00004062 switch (getAddrMode(MI)) {
4063 case HexagonII::Absolute :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004064 return Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004065 case HexagonII::BaseImmOffset :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004066 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004067 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004068 return Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004069
Jyotsna Verma84256432013-03-01 17:37:13 +00004070 default:
4071 return -1;
4072 }
4073 }
4074 return -1;
4075}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00004076
Brendon Cahoondf43e682015-05-08 16:16:29 +00004077
Ahmed Bougachac88bf542015-06-11 19:30:37 +00004078bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004079 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00004080 if (Cond.empty())
4081 return false;
4082 assert(Cond.size() == 2);
4083 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
4084 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
4085 return false;
4086 }
4087 PredReg = Cond[1].getReg();
4088 PredRegPos = 1;
4089 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4090 PredRegFlags = 0;
4091 if (Cond[1].isImplicit())
4092 PredRegFlags = RegState::Implicit;
4093 if (Cond[1].isUndef())
4094 PredRegFlags |= RegState::Undef;
4095 return true;
4096}
4097
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004098
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004099short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
4100 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004101}
4102
4103
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004104short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
4105 return Hexagon::getRegForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004106}
4107
4108
4109// Return the number of bytes required to encode the instruction.
4110// Hexagon instructions are fixed length, 4 bytes, unless they
4111// use a constant extender, which requires another 4 bytes.
4112// For debug instructions and prolog labels, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004113unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
4114 if (MI.isDebugValue() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004115 return 0;
4116
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004117 unsigned Size = MI.getDesc().getSize();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004118 if (!Size)
4119 // Assume the default insn size in case it cannot be determined
4120 // for whatever reason.
4121 Size = HEXAGON_INSTR_SIZE;
4122
4123 if (isConstExtended(MI) || isExtended(MI))
4124 Size += HEXAGON_INSTR_SIZE;
4125
4126 // Try and compute number of instructions in asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004127 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4128 const MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004129 const MachineFunction *MF = MBB.getParent();
4130 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4131
4132 // Count the number of register definitions to find the asm string.
4133 unsigned NumDefs = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004134 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004135 ++NumDefs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004136 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004137
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004138 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004139 // Disassemble the AsmStr and approximate number of instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004140 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004141 Size = getInlineAsmLength(AsmStr, *MAI);
4142 }
4143
4144 return Size;
4145}
4146
4147
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004148uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
4149 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004150 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4151}
4152
4153
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004154unsigned HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
4155 const TargetSubtargetInfo &ST = MI.getParent()->getParent()->getSubtarget();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004156 const InstrItineraryData &II = *ST.getInstrItineraryData();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004157 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004158
4159 return IS.getUnits();
4160}
4161
4162
4163unsigned HexagonInstrInfo::getValidSubTargets(const unsigned Opcode) const {
4164 const uint64_t F = get(Opcode).TSFlags;
4165 return (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask;
4166}
4167
4168
4169// Calculate size of the basic block without debug instructions.
4170unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4171 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4172}
4173
4174
4175unsigned HexagonInstrInfo::nonDbgBundleSize(
4176 MachineBasicBlock::const_iterator BundleHead) const {
4177 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004178 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004179 // Skip the bundle header.
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00004180 return nonDbgMICount(++MII, getBundleEnd(*BundleHead));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004181}
4182
4183
4184/// immediateExtend - Changes the instruction in place to one using an immediate
4185/// extender.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004186void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004187 assert((isExtendable(MI)||isConstExtended(MI)) &&
4188 "Instruction must be extendable");
4189 // Find which operand is extendable.
4190 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004191 MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004192 // This needs to be something we understand.
4193 assert((MO.isMBB() || MO.isImm()) &&
4194 "Branch with unknown extendable field type");
4195 // Mark given operand as extended.
4196 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4197}
4198
4199
4200bool HexagonInstrInfo::invertAndChangeJumpTarget(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004201 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004202 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004203 << NewTarget->getNumber(); MI.dump(););
4204 assert(MI.isBranch());
4205 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4206 int TargetPos = MI.getNumOperands() - 1;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004207 // In general branch target is the last operand,
4208 // but some implicit defs added at the end might change it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004209 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004210 --TargetPos;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004211 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4212 MI.getOperand(TargetPos).setMBB(NewTarget);
4213 if (EnableBranchPrediction && isPredicatedNew(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004214 NewOpcode = reversePrediction(NewOpcode);
4215 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004216 MI.setDesc(get(NewOpcode));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004217 return true;
4218}
4219
4220
4221void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4222 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4223 MachineFunction::iterator A = MF.begin();
4224 MachineBasicBlock &B = *A;
4225 MachineBasicBlock::iterator I = B.begin();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004226 DebugLoc DL = I->getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004227 MachineInstr *NewMI;
4228
4229 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4230 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004231 NewMI = BuildMI(B, I, DL, get(insn));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004232 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
4233 " Class: " << NewMI->getDesc().getSchedClass());
4234 NewMI->eraseFromParent();
4235 }
4236 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4237}
4238
4239
4240// inverts the predication logic.
4241// p -> NotP
4242// NotP -> P
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004243bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
4244 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
4245 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004246 return true;
4247}
4248
4249
4250// Reverse the branch prediction.
4251unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4252 int PredRevOpcode = -1;
4253 if (isPredictedTaken(Opcode))
4254 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4255 else
4256 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4257 assert(PredRevOpcode > 0);
4258 return PredRevOpcode;
4259}
4260
4261
4262// TODO: Add more rigorous validation.
4263bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4264 const {
4265 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4266}
4267
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004268
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004269short HexagonInstrInfo::xformRegToImmOffset(const MachineInstr &MI) const {
4270 return Hexagon::xformRegToImmOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004271}