blob: d3fd517fe4989960111695fc0392bf0eb1225675 [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
Tony Linthicum1213a7a2011-12-12 21:14:40 +000014#include "HexagonInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "Hexagon.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 Parzyszekb9a1c3a2015-11-24 14:55:26 +000026#include "llvm/MC/MCAsmInfo.h"
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +000027#include "llvm/Support/CommandLine.h"
Jyotsna Verma5ed51812013-05-01 21:37:34 +000028#include "llvm/Support/Debug.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000029#include "llvm/Support/MathExtras.h"
Reid Kleckner1c76f1552013-05-03 00:54:56 +000030#include "llvm/Support/raw_ostream.h"
Krzysztof Parzyszekaa935752015-11-24 15:11:13 +000031#include <cctype>
Tony Linthicum1213a7a2011-12-12 21:14:40 +000032
Tony Linthicum1213a7a2011-12-12 21:14:40 +000033using namespace llvm;
34
Chandler Carruthe96dd892014-04-21 22:55:11 +000035#define DEBUG_TYPE "hexagon-instrinfo"
36
Chandler Carruthd174b722014-04-22 02:03:14 +000037#define GET_INSTRINFO_CTOR_DTOR
38#define GET_INSTRMAP_INFO
39#include "HexagonGenInstrInfo.inc"
40#include "HexagonGenDFAPacketizer.inc"
41
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000042using namespace llvm;
43
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000044cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000045 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
46 "packetization boundary."));
47
48static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
49 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
50
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000051static cl::opt<bool> DisableNVSchedule("disable-hexagon-nv-schedule",
52 cl::Hidden, cl::ZeroOrMore, cl::init(false),
53 cl::desc("Disable schedule adjustment for new value stores."));
54
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000055static cl::opt<bool> EnableTimingClassLatency(
56 "enable-timing-class-latency", cl::Hidden, cl::init(false),
57 cl::desc("Enable timing class latency"));
58
59static cl::opt<bool> EnableALUForwarding(
60 "enable-alu-forwarding", cl::Hidden, cl::init(true),
61 cl::desc("Enable vec alu forwarding"));
62
63static cl::opt<bool> EnableACCForwarding(
64 "enable-acc-forwarding", cl::Hidden, cl::init(true),
65 cl::desc("Enable vec acc forwarding"));
66
67static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
68 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
69
Tony Linthicum1213a7a2011-12-12 21:14:40 +000070///
71/// Constants for Hexagon instructions.
72///
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +000073const int Hexagon_MEMV_OFFSET_MAX_128B = 896; // #s4: -8*128...7*128
74const int Hexagon_MEMV_OFFSET_MIN_128B = -1024; // #s4
75const int Hexagon_MEMV_OFFSET_MAX = 448; // #s4: -8*64...7*64
76const int Hexagon_MEMV_OFFSET_MIN = -512; // #s4
Tony Linthicum1213a7a2011-12-12 21:14:40 +000077const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000078const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000079const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000080const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000081const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000082const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000083const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000084const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000085const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000086const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000087const int Hexagon_MEMD_AUTOINC_MAX = 56;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000088const int Hexagon_MEMD_AUTOINC_MIN = -64;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000089const int Hexagon_MEMW_AUTOINC_MAX = 28;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000090const int Hexagon_MEMW_AUTOINC_MIN = -32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000091const int Hexagon_MEMH_AUTOINC_MAX = 14;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000092const int Hexagon_MEMH_AUTOINC_MIN = -16;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000093const int Hexagon_MEMB_AUTOINC_MAX = 7;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000094const int Hexagon_MEMB_AUTOINC_MIN = -8;
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +000095const int Hexagon_MEMV_AUTOINC_MAX = 192; // #s3
96const int Hexagon_MEMV_AUTOINC_MIN = -256; // #s3
97const int Hexagon_MEMV_AUTOINC_MAX_128B = 384; // #s3
98const int Hexagon_MEMV_AUTOINC_MIN_128B = -512; // #s3
Tony Linthicum1213a7a2011-12-12 21:14:40 +000099
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000100// Pin the vtable to this file.
101void HexagonInstrInfo::anchor() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000102
103HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
Eric Christopherc4d31402015-03-10 23:45:55 +0000104 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000105 RI() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000106
107
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000108static bool isIntRegForSubInst(unsigned Reg) {
109 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
110 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000111}
112
113
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000114static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
115 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::subreg_loreg)) &&
116 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::subreg_hireg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000117}
118
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000119
120/// Calculate number of instructions excluding the debug instructions.
121static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
122 MachineBasicBlock::const_instr_iterator MIE) {
123 unsigned Count = 0;
124 for (; MIB != MIE; ++MIB) {
125 if (!MIB->isDebugValue())
126 ++Count;
127 }
128 return Count;
129}
130
131
132/// Find the hardware loop instruction used to set-up the specified loop.
133/// On Hexagon, we have two instructions used to set-up the hardware loop
134/// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
135/// to indicate the end of a loop.
136static MachineInstr *findLoopInstr(MachineBasicBlock *BB, int EndLoopOp,
137 SmallPtrSet<MachineBasicBlock *, 8> &Visited) {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000138 int LOOPi;
139 int LOOPr;
140 if (EndLoopOp == Hexagon::ENDLOOP0) {
141 LOOPi = Hexagon::J2_loop0i;
142 LOOPr = Hexagon::J2_loop0r;
143 } else { // EndLoopOp == Hexagon::EndLOOP1
144 LOOPi = Hexagon::J2_loop1i;
145 LOOPr = Hexagon::J2_loop1r;
146 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000147
Brendon Cahoondf43e682015-05-08 16:16:29 +0000148 // The loop set-up instruction will be in a predecessor block
149 for (MachineBasicBlock::pred_iterator PB = BB->pred_begin(),
150 PE = BB->pred_end(); PB != PE; ++PB) {
151 // If this has been visited, already skip it.
152 if (!Visited.insert(*PB).second)
153 continue;
154 if (*PB == BB)
155 continue;
156 for (MachineBasicBlock::reverse_instr_iterator I = (*PB)->instr_rbegin(),
157 E = (*PB)->instr_rend(); I != E; ++I) {
158 int Opc = I->getOpcode();
159 if (Opc == LOOPi || Opc == LOOPr)
160 return &*I;
161 // We've reached a different loop, which means the loop0 has been removed.
162 if (Opc == EndLoopOp)
163 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000164 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000165 // Check the predecessors for the LOOP instruction.
166 MachineInstr *loop = findLoopInstr(*PB, EndLoopOp, Visited);
167 if (loop)
168 return loop;
169 }
170 return 0;
171}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000172
Brendon Cahoondf43e682015-05-08 16:16:29 +0000173
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000174/// Gather register def/uses from MI.
175/// This treats possible (predicated) defs as actually happening ones
176/// (conservatively).
177static inline void parseOperands(const MachineInstr *MI,
178 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
179 Defs.clear();
180 Uses.clear();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000181
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000182 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
183 const MachineOperand &MO = MI->getOperand(i);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000184
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000185 if (!MO.isReg())
186 continue;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000187
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000188 unsigned Reg = MO.getReg();
189 if (!Reg)
190 continue;
191
192 if (MO.isUse())
193 Uses.push_back(MO.getReg());
194
195 if (MO.isDef())
196 Defs.push_back(MO.getReg());
197 }
198}
199
200
201// Position dependent, so check twice for swap.
202static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
203 switch (Ga) {
204 case HexagonII::HSIG_None:
205 default:
206 return false;
207 case HexagonII::HSIG_L1:
208 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
209 case HexagonII::HSIG_L2:
210 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
211 Gb == HexagonII::HSIG_A);
212 case HexagonII::HSIG_S1:
213 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
214 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
215 case HexagonII::HSIG_S2:
216 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
217 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
218 Gb == HexagonII::HSIG_A);
219 case HexagonII::HSIG_A:
220 return (Gb == HexagonII::HSIG_A);
221 case HexagonII::HSIG_Compound:
222 return (Gb == HexagonII::HSIG_Compound);
223 }
224 return false;
225}
226
227
228
229/// isLoadFromStackSlot - If the specified machine instruction is a direct
230/// load from a stack slot, return the virtual or physical register number of
231/// the destination along with the FrameIndex of the loaded stack slot. If
232/// not, return 0. This predicate must return 0 if the instruction has
233/// any side effects other than loading from the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000234unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000235 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000236 switch (MI.getOpcode()) {
237 default:
238 break;
239 case Hexagon::L2_loadrb_io:
240 case Hexagon::L2_loadrub_io:
241 case Hexagon::L2_loadrh_io:
242 case Hexagon::L2_loadruh_io:
243 case Hexagon::L2_loadri_io:
244 case Hexagon::L2_loadrd_io:
245 case Hexagon::V6_vL32b_ai:
246 case Hexagon::V6_vL32b_ai_128B:
247 case Hexagon::V6_vL32Ub_ai:
248 case Hexagon::V6_vL32Ub_ai_128B:
249 case Hexagon::LDriw_pred:
250 case Hexagon::LDriw_mod:
251 case Hexagon::LDriq_pred_V6:
252 case Hexagon::LDriq_pred_vec_V6:
253 case Hexagon::LDriv_pseudo_V6:
254 case Hexagon::LDrivv_pseudo_V6:
255 case Hexagon::LDriq_pred_V6_128B:
256 case Hexagon::LDriq_pred_vec_V6_128B:
257 case Hexagon::LDriv_pseudo_V6_128B:
258 case Hexagon::LDrivv_pseudo_V6_128B: {
259 const MachineOperand OpFI = MI.getOperand(1);
260 if (!OpFI.isFI())
261 return 0;
262 const MachineOperand OpOff = MI.getOperand(2);
263 if (!OpOff.isImm() || OpOff.getImm() != 0)
264 return 0;
265 FrameIndex = OpFI.getIndex();
266 return MI.getOperand(0).getReg();
267 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000268
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000269 case Hexagon::L2_ploadrbt_io:
270 case Hexagon::L2_ploadrbf_io:
271 case Hexagon::L2_ploadrubt_io:
272 case Hexagon::L2_ploadrubf_io:
273 case Hexagon::L2_ploadrht_io:
274 case Hexagon::L2_ploadrhf_io:
275 case Hexagon::L2_ploadruht_io:
276 case Hexagon::L2_ploadruhf_io:
277 case Hexagon::L2_ploadrit_io:
278 case Hexagon::L2_ploadrif_io:
279 case Hexagon::L2_ploadrdt_io:
280 case Hexagon::L2_ploadrdf_io: {
281 const MachineOperand OpFI = MI.getOperand(2);
282 if (!OpFI.isFI())
283 return 0;
284 const MachineOperand OpOff = MI.getOperand(3);
285 if (!OpOff.isImm() || OpOff.getImm() != 0)
286 return 0;
287 FrameIndex = OpFI.getIndex();
288 return MI.getOperand(0).getReg();
289 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000290 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000291
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000292 return 0;
293}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000294
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000295
296/// isStoreToStackSlot - If the specified machine instruction is a direct
297/// store to a stack slot, return the virtual or physical register number of
298/// the source reg along with the FrameIndex of the loaded stack slot. If
299/// not, return 0. This predicate must return 0 if the instruction has
300/// any side effects other than storing to the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000301unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000302 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000303 switch (MI.getOpcode()) {
304 default:
305 break;
306 case Hexagon::S2_storerb_io:
307 case Hexagon::S2_storerh_io:
308 case Hexagon::S2_storeri_io:
309 case Hexagon::S2_storerd_io:
310 case Hexagon::V6_vS32b_ai:
311 case Hexagon::V6_vS32b_ai_128B:
312 case Hexagon::V6_vS32Ub_ai:
313 case Hexagon::V6_vS32Ub_ai_128B:
314 case Hexagon::STriw_pred:
315 case Hexagon::STriw_mod:
316 case Hexagon::STriq_pred_V6:
317 case Hexagon::STriq_pred_vec_V6:
318 case Hexagon::STriv_pseudo_V6:
319 case Hexagon::STrivv_pseudo_V6:
320 case Hexagon::STriq_pred_V6_128B:
321 case Hexagon::STriq_pred_vec_V6_128B:
322 case Hexagon::STriv_pseudo_V6_128B:
323 case Hexagon::STrivv_pseudo_V6_128B: {
324 const MachineOperand &OpFI = MI.getOperand(0);
325 if (!OpFI.isFI())
326 return 0;
327 const MachineOperand &OpOff = MI.getOperand(1);
328 if (!OpOff.isImm() || OpOff.getImm() != 0)
329 return 0;
330 FrameIndex = OpFI.getIndex();
331 return MI.getOperand(2).getReg();
332 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000333
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000334 case Hexagon::S2_pstorerbt_io:
335 case Hexagon::S2_pstorerbf_io:
336 case Hexagon::S2_pstorerht_io:
337 case Hexagon::S2_pstorerhf_io:
338 case Hexagon::S2_pstorerit_io:
339 case Hexagon::S2_pstorerif_io:
340 case Hexagon::S2_pstorerdt_io:
341 case Hexagon::S2_pstorerdf_io: {
342 const MachineOperand &OpFI = MI.getOperand(1);
343 if (!OpFI.isFI())
344 return 0;
345 const MachineOperand &OpOff = MI.getOperand(2);
346 if (!OpOff.isImm() || OpOff.getImm() != 0)
347 return 0;
348 FrameIndex = OpFI.getIndex();
349 return MI.getOperand(3).getReg();
350 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000351 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000352
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000353 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000354}
355
356
Brendon Cahoondf43e682015-05-08 16:16:29 +0000357/// This function can analyze one/two way branching only and should (mostly) be
358/// called by target independent side.
359/// First entry is always the opcode of the branching instruction, except when
360/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
361/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
362/// e.g. Jump_c p will have
363/// Cond[0] = Jump_c
364/// Cond[1] = p
365/// HW-loop ENDLOOP:
366/// Cond[0] = ENDLOOP
367/// Cond[1] = MBB
368/// New value jump:
369/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
370/// Cond[1] = R
371/// Cond[2] = Imm
Brendon Cahoondf43e682015-05-08 16:16:29 +0000372///
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000373bool HexagonInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
374 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000375 MachineBasicBlock *&FBB,
376 SmallVectorImpl<MachineOperand> &Cond,
377 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000378 TBB = nullptr;
379 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000380 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000381
382 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000383 MachineBasicBlock::instr_iterator I = MBB.instr_end();
384 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000385 return false;
386
387 // A basic block may looks like this:
388 //
389 // [ insn
390 // EH_LABEL
391 // insn
392 // insn
393 // insn
394 // EH_LABEL
395 // insn ]
396 //
397 // It has two succs but does not have a terminator
398 // Don't know how to handle it.
399 do {
400 --I;
401 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000402 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000403 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000404 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000405
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000406 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000407 --I;
408
409 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000410 if (I == MBB.instr_begin())
411 return false;
412 --I;
413 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000414
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000415 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
416 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000417 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000418 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000419 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
420 DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
421 I->eraseFromParent();
422 I = MBB.instr_end();
423 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000424 return false;
425 --I;
426 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000427 if (!isUnpredicatedTerminator(*I))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000428 return false;
429
430 // Get the last instruction in the block.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000431 MachineInstr *LastInst = &*I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000432 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000433 // Find one more terminator if present.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000434 for (;;) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000435 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000436 if (!SecondLastInst)
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000437 SecondLastInst = &*I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000438 else
439 // This is a third branch.
440 return true;
441 }
442 if (I == MBB.instr_begin())
443 break;
444 --I;
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000445 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000446
447 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000448 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
449 // If the branch target is not a basic block, it could be a tail call.
450 // (It is, if the target is a function.)
451 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
452 return true;
453 if (SecLastOpcode == Hexagon::J2_jump &&
454 !SecondLastInst->getOperand(0).isMBB())
455 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000456
457 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000458 bool LastOpcodeHasNVJump = isNewValueJump(LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000459
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000460 if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
461 return true;
462
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000463 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000464 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000465 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000466 TBB = LastInst->getOperand(0).getMBB();
467 return false;
468 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000469 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000470 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000471 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000472 Cond.push_back(LastInst->getOperand(0));
473 return false;
474 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000475 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000476 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000477 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000478 Cond.push_back(LastInst->getOperand(0));
479 return false;
480 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000481 // Only supporting rr/ri versions of new-value jumps.
482 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
483 TBB = LastInst->getOperand(2).getMBB();
484 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
485 Cond.push_back(LastInst->getOperand(0));
486 Cond.push_back(LastInst->getOperand(1));
487 return false;
488 }
489 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
490 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000491 // Otherwise, don't know what this is.
492 return true;
493 }
494
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000495 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000496 bool SecLastOpcodeHasNVJump = isNewValueJump(SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000497 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000498 if (!SecondLastInst->getOperand(1).isMBB())
499 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000500 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000501 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000502 Cond.push_back(SecondLastInst->getOperand(0));
503 FBB = LastInst->getOperand(0).getMBB();
504 return false;
505 }
506
Brendon Cahoondf43e682015-05-08 16:16:29 +0000507 // Only supporting rr/ri versions of new-value jumps.
508 if (SecLastOpcodeHasNVJump &&
509 (SecondLastInst->getNumExplicitOperands() == 3) &&
510 (LastOpcode == Hexagon::J2_jump)) {
511 TBB = SecondLastInst->getOperand(2).getMBB();
512 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
513 Cond.push_back(SecondLastInst->getOperand(0));
514 Cond.push_back(SecondLastInst->getOperand(1));
515 FBB = LastInst->getOperand(0).getMBB();
516 return false;
517 }
518
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000519 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
520 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000521 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000522 TBB = SecondLastInst->getOperand(0).getMBB();
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000523 I = LastInst->getIterator();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000524 if (AllowModify)
525 I->eraseFromParent();
526 return false;
527 }
528
Brendon Cahoondf43e682015-05-08 16:16:29 +0000529 // If the block ends with an ENDLOOP, and J2_jump, handle it.
530 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000531 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000532 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000533 Cond.push_back(SecondLastInst->getOperand(0));
534 FBB = LastInst->getOperand(0).getMBB();
535 return false;
536 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000537 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
538 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000539 // Otherwise, can't handle this.
540 return true;
541}
542
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000543
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000544unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000545 DEBUG(dbgs() << "\nRemoving branches out of BB#" << MBB.getNumber());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000546 MachineBasicBlock::iterator I = MBB.end();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000547 unsigned Count = 0;
548 while (I != MBB.begin()) {
549 --I;
550 if (I->isDebugValue())
551 continue;
552 // Only removing branches from end of MBB.
553 if (!I->isBranch())
554 return Count;
555 if (Count && (I->getOpcode() == Hexagon::J2_jump))
556 llvm_unreachable("Malformed basic block: unconditional branch not last");
557 MBB.erase(&MBB.back());
558 I = MBB.end();
559 ++Count;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000560 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000561 return Count;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000562}
563
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000564unsigned HexagonInstrInfo::InsertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000565 MachineBasicBlock *TBB,
566 MachineBasicBlock *FBB,
567 ArrayRef<MachineOperand> Cond,
568 const DebugLoc &DL) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000569 unsigned BOpc = Hexagon::J2_jump;
570 unsigned BccOpc = Hexagon::J2_jumpt;
571 assert(validateBranchCond(Cond) && "Invalid branching condition");
572 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
573
574 // Check if ReverseBranchCondition has asked to reverse this branch
575 // If we want to reverse the branch an odd number of times, we want
576 // J2_jumpf.
577 if (!Cond.empty() && Cond[0].isImm())
578 BccOpc = Cond[0].getImm();
579
580 if (!FBB) {
581 if (Cond.empty()) {
582 // Due to a bug in TailMerging/CFG Optimization, we need to add a
583 // special case handling of a predicated jump followed by an
584 // unconditional jump. If not, Tail Merging and CFG Optimization go
585 // into an infinite loop.
586 MachineBasicBlock *NewTBB, *NewFBB;
587 SmallVector<MachineOperand, 4> Cond;
Duncan P. N. Exon Smith25b132e2016-07-08 18:26:20 +0000588 auto Term = MBB.getFirstTerminator();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000589 if (Term != MBB.end() && isPredicated(*Term) &&
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000590 !AnalyzeBranch(MBB, NewTBB, NewFBB, Cond, false)) {
591 MachineBasicBlock *NextBB = &*++MBB.getIterator();
592 if (NewTBB == NextBB) {
593 ReverseBranchCondition(Cond);
594 RemoveBranch(MBB);
595 return InsertBranch(MBB, TBB, nullptr, Cond, DL);
596 }
597 }
598 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
599 } else if (isEndLoopN(Cond[0].getImm())) {
600 int EndLoopOp = Cond[0].getImm();
601 assert(Cond[1].isMBB());
602 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
603 // Check for it, and change the BB target if needed.
604 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
605 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
606 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
607 Loop->getOperand(0).setMBB(TBB);
608 // Add the ENDLOOP after the finding the LOOP0.
609 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
610 } else if (isNewValueJump(Cond[0].getImm())) {
611 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
612 // New value jump
613 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
614 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
615 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
616 DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
617 if (Cond[2].isReg()) {
618 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
619 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
620 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
621 } else if(Cond[2].isImm()) {
622 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
623 addImm(Cond[2].getImm()).addMBB(TBB);
624 } else
625 llvm_unreachable("Invalid condition for branching");
626 } else {
627 assert((Cond.size() == 2) && "Malformed cond vector");
628 const MachineOperand &RO = Cond[1];
629 unsigned Flags = getUndefRegState(RO.isUndef());
630 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
631 }
632 return 1;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000633 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000634 assert((!Cond.empty()) &&
635 "Cond. cannot be empty when multiple branchings are required");
636 assert((!isNewValueJump(Cond[0].getImm())) &&
637 "NV-jump cannot be inserted with another branch");
638 // Special case for hardware loops. The condition is a basic block.
639 if (isEndLoopN(Cond[0].getImm())) {
640 int EndLoopOp = Cond[0].getImm();
641 assert(Cond[1].isMBB());
642 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
643 // Check for it, and change the BB target if needed.
644 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
645 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
646 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
647 Loop->getOperand(0).setMBB(TBB);
648 // Add the ENDLOOP after the finding the LOOP0.
649 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
650 } else {
651 const MachineOperand &RO = Cond[1];
652 unsigned Flags = getUndefRegState(RO.isUndef());
653 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000654 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000655 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000656
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000657 return 2;
658}
659
660
661bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
662 unsigned NumCycles, unsigned ExtraPredCycles,
663 BranchProbability Probability) const {
664 return nonDbgBBSize(&MBB) <= 3;
665}
666
667
668bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
669 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
670 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
671 const {
672 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
673}
674
675
676bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
677 unsigned NumInstrs, BranchProbability Probability) const {
678 return NumInstrs <= 4;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000679}
680
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000681void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000682 MachineBasicBlock::iterator I,
683 const DebugLoc &DL, unsigned DestReg,
684 unsigned SrcReg, bool KillSrc) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000685 auto &HRI = getRegisterInfo();
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000686 unsigned KillFlag = getKillRegState(KillSrc);
687
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000688 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000689 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000690 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000691 return;
692 }
693 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000694 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg)
695 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000696 return;
697 }
698 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
699 // Map Pd = Ps to Pd = or(Ps, Ps).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000700 BuildMI(MBB, I, DL, get(Hexagon::C2_or), DestReg)
701 .addReg(SrcReg).addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000702 return;
703 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000704 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000705 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000706 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
707 .addReg(SrcReg, KillFlag);
708 return;
709 }
710 if (Hexagon::IntRegsRegClass.contains(DestReg) &&
711 Hexagon::CtrRegsRegClass.contains(SrcReg)) {
712 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrcrr), DestReg)
713 .addReg(SrcReg, KillFlag);
714 return;
715 }
716 if (Hexagon::ModRegsRegClass.contains(DestReg) &&
717 Hexagon::IntRegsRegClass.contains(SrcReg)) {
718 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
719 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000720 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000721 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000722 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
723 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000724 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
725 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000726 return;
727 }
728 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
729 Hexagon::PredRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000730 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg)
731 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000732 return;
733 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000734 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
735 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000736 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
737 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000738 return;
739 }
740 if (Hexagon::VectorRegsRegClass.contains(SrcReg, DestReg)) {
741 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000742 addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000743 return;
744 }
745 if (Hexagon::VecDblRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000746 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
747 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg), KillFlag)
748 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg), KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000749 return;
750 }
751 if (Hexagon::VecPredRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000752 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
753 .addReg(SrcReg)
754 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000755 return;
756 }
757 if (Hexagon::VecPredRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000758 Hexagon::VectorRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000759 llvm_unreachable("Unimplemented pred to vec");
760 return;
761 }
762 if (Hexagon::VecPredRegsRegClass.contains(DestReg) &&
763 Hexagon::VectorRegsRegClass.contains(SrcReg)) {
764 llvm_unreachable("Unimplemented vec to pred");
765 return;
766 }
767 if (Hexagon::VecPredRegs128BRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000768 unsigned DstHi = HRI.getSubReg(DestReg, Hexagon::subreg_hireg);
769 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DstHi)
770 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg), KillFlag);
771 unsigned DstLo = HRI.getSubReg(DestReg, Hexagon::subreg_loreg);
772 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DstLo)
773 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg), KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000774 return;
775 }
Sirish Pande30804c22012-02-15 18:52:27 +0000776
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000777#ifndef NDEBUG
778 // Show the invalid registers to ease debugging.
779 dbgs() << "Invalid registers for copy in BB#" << MBB.getNumber()
780 << ": " << PrintReg(DestReg, &HRI)
781 << " = " << PrintReg(SrcReg, &HRI) << '\n';
782#endif
Sirish Pande30804c22012-02-15 18:52:27 +0000783 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000784}
785
786
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000787void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
788 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
789 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000790 DebugLoc DL = MBB.findDebugLoc(I);
791 MachineFunction &MF = *MBB.getParent();
792 MachineFrameInfo &MFI = *MF.getFrameInfo();
793 unsigned Align = MFI.getObjectAlignment(FI);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000794 unsigned KillFlag = getKillRegState(isKill);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000795
Alex Lorenze40c8a22015-08-11 23:09:45 +0000796 MachineMemOperand *MMO = MF.getMachineMemOperand(
797 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
798 MFI.getObjectSize(FI), Align);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000799
Craig Topperc7242e02012-04-20 07:30:17 +0000800 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000801 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000802 .addFrameIndex(FI).addImm(0)
803 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000804 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000805 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000806 .addFrameIndex(FI).addImm(0)
807 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000808 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000809 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000810 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000811 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000812 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
813 BuildMI(MBB, I, DL, get(Hexagon::STriw_mod))
814 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000815 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
816 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
817 BuildMI(MBB, I, DL, get(Hexagon::STriq_pred_V6_128B))
818 .addFrameIndex(FI).addImm(0)
819 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
820 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
821 BuildMI(MBB, I, DL, get(Hexagon::STriq_pred_V6))
822 .addFrameIndex(FI).addImm(0)
823 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
824 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
825 DEBUG(dbgs() << "++Generating 128B vector spill");
826 BuildMI(MBB, I, DL, get(Hexagon::STriv_pseudo_V6_128B))
827 .addFrameIndex(FI).addImm(0)
828 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
829 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
830 DEBUG(dbgs() << "++Generating vector spill");
831 BuildMI(MBB, I, DL, get(Hexagon::STriv_pseudo_V6))
832 .addFrameIndex(FI).addImm(0)
833 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
834 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
835 DEBUG(dbgs() << "++Generating double vector spill");
836 BuildMI(MBB, I, DL, get(Hexagon::STrivv_pseudo_V6))
837 .addFrameIndex(FI).addImm(0)
838 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
839 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
840 DEBUG(dbgs() << "++Generating 128B double vector spill");
841 BuildMI(MBB, I, DL, get(Hexagon::STrivv_pseudo_V6_128B))
842 .addFrameIndex(FI).addImm(0)
843 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000844 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000845 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000846 }
847}
848
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000849void HexagonInstrInfo::loadRegFromStackSlot(
850 MachineBasicBlock &MBB, MachineBasicBlock::iterator I, unsigned DestReg,
851 int FI, const TargetRegisterClass *RC,
852 const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000853 DebugLoc DL = MBB.findDebugLoc(I);
854 MachineFunction &MF = *MBB.getParent();
855 MachineFrameInfo &MFI = *MF.getFrameInfo();
856 unsigned Align = MFI.getObjectAlignment(FI);
857
Alex Lorenze40c8a22015-08-11 23:09:45 +0000858 MachineMemOperand *MMO = MF.getMachineMemOperand(
859 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
860 MFI.getObjectSize(FI), Align);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000861
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000862 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000863 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000864 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000865 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000866 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000867 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000868 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000869 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000870 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
871 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
872 BuildMI(MBB, I, DL, get(Hexagon::LDriw_mod), DestReg)
873 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000874 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
875 BuildMI(MBB, I, DL, get(Hexagon::LDriq_pred_V6_128B), DestReg)
876 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
877 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
878 BuildMI(MBB, I, DL, get(Hexagon::LDriq_pred_V6), DestReg)
879 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
880 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
881 DEBUG(dbgs() << "++Generating 128B double vector restore");
882 BuildMI(MBB, I, DL, get(Hexagon::LDrivv_pseudo_V6_128B), DestReg)
883 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
884 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
885 DEBUG(dbgs() << "++Generating 128B vector restore");
886 BuildMI(MBB, I, DL, get(Hexagon::LDriv_pseudo_V6_128B), DestReg)
887 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
888 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
889 DEBUG(dbgs() << "++Generating vector restore");
890 BuildMI(MBB, I, DL, get(Hexagon::LDriv_pseudo_V6), DestReg)
891 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
892 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
893 DEBUG(dbgs() << "++Generating double vector restore");
894 BuildMI(MBB, I, DL, get(Hexagon::LDrivv_pseudo_V6), DestReg)
895 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000896 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000897 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000898 }
899}
900
901
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000902/// expandPostRAPseudo - This function is called for all pseudo instructions
903/// that remain after register allocation. Many pseudo instructions are
904/// created to help register allocation. This is the place to convert them
905/// into real instructions. The target can edit MI in place, or it can insert
906/// new instructions and erase MI. The function should return true if
907/// anything was changed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000908bool HexagonInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000909 const HexagonRegisterInfo &HRI = getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000910 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
911 MachineBasicBlock &MBB = *MI.getParent();
912 DebugLoc DL = MI.getDebugLoc();
913 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +0000914 const unsigned VecOffset = 1;
915 bool Is128B = false;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000916
917 switch (Opc) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000918 case TargetOpcode::COPY: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000919 MachineOperand &MD = MI.getOperand(0);
920 MachineOperand &MS = MI.getOperand(1);
921 MachineBasicBlock::iterator MBBI = MI.getIterator();
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000922 if (MD.getReg() != MS.getReg() && !MS.isUndef()) {
923 copyPhysReg(MBB, MI, DL, MD.getReg(), MS.getReg(), MS.isKill());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000924 std::prev(MBBI)->copyImplicitOps(*MBB.getParent(), MI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000925 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000926 MBB.erase(MBBI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000927 return true;
928 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000929 case Hexagon::ALIGNA:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000930 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI.getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000931 .addReg(HRI.getFrameRegister())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000932 .addImm(-MI.getOperand(1).getImm());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000933 MBB.erase(MI);
934 return true;
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +0000935 case Hexagon::HEXAGON_V6_vassignp_128B:
936 case Hexagon::HEXAGON_V6_vassignp: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000937 unsigned SrcReg = MI.getOperand(1).getReg();
938 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +0000939 if (SrcReg != DstReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000940 copyPhysReg(MBB, MI, DL, DstReg, SrcReg, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +0000941 MBB.erase(MI);
942 return true;
943 }
944 case Hexagon::HEXAGON_V6_lo_128B:
945 case Hexagon::HEXAGON_V6_lo: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000946 unsigned SrcReg = MI.getOperand(1).getReg();
947 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +0000948 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000949 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +0000950 MBB.erase(MI);
951 MRI.clearKillFlags(SrcSubLo);
952 return true;
953 }
954 case Hexagon::HEXAGON_V6_hi_128B:
955 case Hexagon::HEXAGON_V6_hi: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000956 unsigned SrcReg = MI.getOperand(1).getReg();
957 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +0000958 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::subreg_hireg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000959 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +0000960 MBB.erase(MI);
961 MRI.clearKillFlags(SrcSubHi);
962 return true;
963 }
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +0000964 case Hexagon::STrivv_indexed_128B:
965 Is128B = true;
966 case Hexagon::STrivv_indexed: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000967 unsigned SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +0000968 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::subreg_hireg);
969 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::subreg_loreg);
970 unsigned NewOpcd = Is128B ? Hexagon::V6_vS32b_ai_128B
971 : Hexagon::V6_vS32b_ai;
972 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000973 MachineInstr *MI1New =
974 BuildMI(MBB, MI, DL, get(NewOpcd))
975 .addOperand(MI.getOperand(0))
976 .addImm(MI.getOperand(1).getImm())
977 .addReg(SrcSubLo)
978 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +0000979 MI1New->getOperand(0).setIsKill(false);
980 BuildMI(MBB, MI, DL, get(NewOpcd))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000981 .addOperand(MI.getOperand(0))
982 // The Vectors are indexed in multiples of vector size.
983 .addImm(MI.getOperand(1).getImm() + Offset)
984 .addReg(SrcSubHi)
985 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +0000986 MBB.erase(MI);
987 return true;
988 }
989 case Hexagon::LDrivv_pseudo_V6_128B:
990 case Hexagon::LDrivv_indexed_128B:
991 Is128B = true;
992 case Hexagon::LDrivv_pseudo_V6:
993 case Hexagon::LDrivv_indexed: {
994 unsigned NewOpcd = Is128B ? Hexagon::V6_vL32b_ai_128B
995 : Hexagon::V6_vL32b_ai;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000996 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +0000997 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
998 MachineInstr *MI1New =
999 BuildMI(MBB, MI, DL, get(NewOpcd),
1000 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001001 .addOperand(MI.getOperand(1))
1002 .addImm(MI.getOperand(2).getImm());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001003 MI1New->getOperand(1).setIsKill(false);
1004 BuildMI(MBB, MI, DL, get(NewOpcd),
1005 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001006 .addOperand(MI.getOperand(1))
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001007 // The Vectors are indexed in multiples of vector size.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001008 .addImm(MI.getOperand(2).getImm() + Offset)
1009 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001010 MBB.erase(MI);
1011 return true;
1012 }
1013 case Hexagon::LDriv_pseudo_V6_128B:
1014 Is128B = true;
1015 case Hexagon::LDriv_pseudo_V6: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001016 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001017 unsigned NewOpc = Is128B ? Hexagon::V6_vL32b_ai_128B
1018 : Hexagon::V6_vL32b_ai;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001019 int32_t Off = MI.getOperand(2).getImm();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001020 BuildMI(MBB, MI, DL, get(NewOpc), DstReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001021 .addOperand(MI.getOperand(1))
1022 .addImm(Off)
1023 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001024 MBB.erase(MI);
1025 return true;
1026 }
1027 case Hexagon::STriv_pseudo_V6_128B:
1028 Is128B = true;
1029 case Hexagon::STriv_pseudo_V6: {
1030 unsigned NewOpc = Is128B ? Hexagon::V6_vS32b_ai_128B
1031 : Hexagon::V6_vS32b_ai;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001032 int32_t Off = MI.getOperand(1).getImm();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001033 BuildMI(MBB, MI, DL, get(NewOpc))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001034 .addOperand(MI.getOperand(0))
1035 .addImm(Off)
1036 .addOperand(MI.getOperand(2))
1037 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001038 MBB.erase(MI);
1039 return true;
1040 }
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001041 case Hexagon::TFR_PdTrue: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001042 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001043 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1044 .addReg(Reg, RegState::Undef)
1045 .addReg(Reg, RegState::Undef);
1046 MBB.erase(MI);
1047 return true;
1048 }
1049 case Hexagon::TFR_PdFalse: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001050 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001051 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1052 .addReg(Reg, RegState::Undef)
1053 .addReg(Reg, RegState::Undef);
1054 MBB.erase(MI);
1055 return true;
1056 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001057 case Hexagon::VMULW: {
1058 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001059 unsigned DstReg = MI.getOperand(0).getReg();
1060 unsigned Src1Reg = MI.getOperand(1).getReg();
1061 unsigned Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001062 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
1063 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
1064 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
1065 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001066 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
1067 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1068 .addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001069 .addReg(Src2SubHi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001070 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
1071 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1072 .addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001073 .addReg(Src2SubLo);
1074 MBB.erase(MI);
1075 MRI.clearKillFlags(Src1SubHi);
1076 MRI.clearKillFlags(Src1SubLo);
1077 MRI.clearKillFlags(Src2SubHi);
1078 MRI.clearKillFlags(Src2SubLo);
1079 return true;
1080 }
1081 case Hexagon::VMULW_ACC: {
1082 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001083 unsigned DstReg = MI.getOperand(0).getReg();
1084 unsigned Src1Reg = MI.getOperand(1).getReg();
1085 unsigned Src2Reg = MI.getOperand(2).getReg();
1086 unsigned Src3Reg = MI.getOperand(3).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001087 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
1088 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
1089 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
1090 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
1091 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::subreg_hireg);
1092 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001093 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
1094 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1095 .addReg(Src1SubHi)
1096 .addReg(Src2SubHi)
1097 .addReg(Src3SubHi);
1098 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
1099 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1100 .addReg(Src1SubLo)
1101 .addReg(Src2SubLo)
1102 .addReg(Src3SubLo);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001103 MBB.erase(MI);
1104 MRI.clearKillFlags(Src1SubHi);
1105 MRI.clearKillFlags(Src1SubLo);
1106 MRI.clearKillFlags(Src2SubHi);
1107 MRI.clearKillFlags(Src2SubLo);
1108 MRI.clearKillFlags(Src3SubHi);
1109 MRI.clearKillFlags(Src3SubLo);
1110 return true;
1111 }
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +00001112 case Hexagon::Insert4: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001113 unsigned DstReg = MI.getOperand(0).getReg();
1114 unsigned Src1Reg = MI.getOperand(1).getReg();
1115 unsigned Src2Reg = MI.getOperand(2).getReg();
1116 unsigned Src3Reg = MI.getOperand(3).getReg();
1117 unsigned Src4Reg = MI.getOperand(4).getReg();
1118 unsigned Src1RegIsKill = getKillRegState(MI.getOperand(1).isKill());
1119 unsigned Src2RegIsKill = getKillRegState(MI.getOperand(2).isKill());
1120 unsigned Src3RegIsKill = getKillRegState(MI.getOperand(3).isKill());
1121 unsigned Src4RegIsKill = getKillRegState(MI.getOperand(4).isKill());
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +00001122 unsigned DstSubHi = HRI.getSubReg(DstReg, Hexagon::subreg_hireg);
1123 unsigned DstSubLo = HRI.getSubReg(DstReg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001124 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1125 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1126 .addReg(DstSubLo)
1127 .addReg(Src1Reg, Src1RegIsKill)
1128 .addImm(16)
1129 .addImm(0);
1130 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1131 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1132 .addReg(DstSubLo)
1133 .addReg(Src2Reg, Src2RegIsKill)
1134 .addImm(16)
1135 .addImm(16);
1136 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1137 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1138 .addReg(DstSubHi)
1139 .addReg(Src3Reg, Src3RegIsKill)
1140 .addImm(16)
1141 .addImm(0);
1142 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1143 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1144 .addReg(DstSubHi)
1145 .addReg(Src4Reg, Src4RegIsKill)
1146 .addImm(16)
1147 .addImm(16);
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +00001148 MBB.erase(MI);
1149 MRI.clearKillFlags(DstReg);
1150 MRI.clearKillFlags(DstSubHi);
1151 MRI.clearKillFlags(DstSubLo);
1152 return true;
1153 }
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001154 case Hexagon::MUX64_rr: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001155 const MachineOperand &Op0 = MI.getOperand(0);
1156 const MachineOperand &Op1 = MI.getOperand(1);
1157 const MachineOperand &Op2 = MI.getOperand(2);
1158 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001159 unsigned Rd = Op0.getReg();
1160 unsigned Pu = Op1.getReg();
1161 unsigned Rs = Op2.getReg();
1162 unsigned Rt = Op3.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001163 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001164 unsigned K1 = getKillRegState(Op1.isKill());
1165 unsigned K2 = getKillRegState(Op2.isKill());
1166 unsigned K3 = getKillRegState(Op3.isKill());
1167 if (Rd != Rs)
1168 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1169 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1170 .addReg(Rs, K2);
1171 if (Rd != Rt)
1172 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1173 .addReg(Pu, K1)
1174 .addReg(Rt, K3);
1175 MBB.erase(MI);
1176 return true;
1177 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001178 case Hexagon::VSelectPseudo_V6: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001179 const MachineOperand &Op0 = MI.getOperand(0);
1180 const MachineOperand &Op1 = MI.getOperand(1);
1181 const MachineOperand &Op2 = MI.getOperand(2);
1182 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001183 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
1184 .addOperand(Op0)
1185 .addOperand(Op1)
1186 .addOperand(Op2);
1187 BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
1188 .addOperand(Op0)
1189 .addOperand(Op1)
1190 .addOperand(Op3);
1191 MBB.erase(MI);
1192 return true;
1193 }
1194 case Hexagon::VSelectDblPseudo_V6: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001195 MachineOperand &Op0 = MI.getOperand(0);
1196 MachineOperand &Op1 = MI.getOperand(1);
1197 MachineOperand &Op2 = MI.getOperand(2);
1198 MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001199 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::subreg_loreg);
1200 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::subreg_hireg);
1201 BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
1202 .addOperand(Op0)
1203 .addOperand(Op1)
1204 .addReg(SrcHi)
1205 .addReg(SrcLo);
1206 SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::subreg_loreg);
1207 SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::subreg_hireg);
1208 BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
1209 .addOperand(Op0)
1210 .addOperand(Op1)
1211 .addReg(SrcHi)
1212 .addReg(SrcLo);
1213 MBB.erase(MI);
1214 return true;
1215 }
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001216 case Hexagon::TCRETURNi:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001217 MI.setDesc(get(Hexagon::J2_jump));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001218 return true;
1219 case Hexagon::TCRETURNr:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001220 MI.setDesc(get(Hexagon::J2_jumpr));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001221 return true;
Krzysztof Parzyszek70a134d2015-11-25 21:40:03 +00001222 case Hexagon::TFRI_f:
1223 case Hexagon::TFRI_cPt_f:
1224 case Hexagon::TFRI_cNotPt_f: {
1225 unsigned Opx = (Opc == Hexagon::TFRI_f) ? 1 : 2;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001226 APFloat FVal = MI.getOperand(Opx).getFPImm()->getValueAPF();
Krzysztof Parzyszek70a134d2015-11-25 21:40:03 +00001227 APInt IVal = FVal.bitcastToAPInt();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001228 MI.RemoveOperand(Opx);
Krzysztof Parzyszek70a134d2015-11-25 21:40:03 +00001229 unsigned NewOpc = (Opc == Hexagon::TFRI_f) ? Hexagon::A2_tfrsi :
1230 (Opc == Hexagon::TFRI_cPt_f) ? Hexagon::C2_cmoveit :
1231 Hexagon::C2_cmoveif;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001232 MI.setDesc(get(NewOpc));
1233 MI.addOperand(MachineOperand::CreateImm(IVal.getZExtValue()));
Krzysztof Parzyszek70a134d2015-11-25 21:40:03 +00001234 return true;
1235 }
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001236 }
1237
1238 return false;
1239}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001240
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001241
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001242// We indicate that we want to reverse the branch by
1243// inserting the reversed branching opcode.
1244bool HexagonInstrInfo::ReverseBranchCondition(
1245 SmallVectorImpl<MachineOperand> &Cond) const {
1246 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001247 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001248 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1249 unsigned opcode = Cond[0].getImm();
1250 //unsigned temp;
1251 assert(get(opcode).isBranch() && "Should be a branching condition.");
1252 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001253 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001254 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1255 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001256 return false;
1257}
1258
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001259
1260void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1261 MachineBasicBlock::iterator MI) const {
1262 DebugLoc DL;
1263 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1264}
1265
1266
1267// Returns true if an instruction is predicated irrespective of the predicate
1268// sense. For example, all of the following will return true.
1269// if (p0) R1 = add(R2, R3)
1270// if (!p0) R1 = add(R2, R3)
1271// if (p0.new) R1 = add(R2, R3)
1272// if (!p0.new) R1 = add(R2, R3)
1273// Note: New-value stores are not included here as in the current
1274// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001275bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1276 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001277 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001278}
1279
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001280
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001281bool HexagonInstrInfo::PredicateInstruction(
1282 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001283 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1284 isEndLoopN(Cond[0].getImm())) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001285 DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001286 return false;
1287 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001288 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001289 assert (isPredicable(MI) && "Expected predicable instruction");
1290 bool invertJump = predOpcodeHasNot(Cond);
1291
1292 // We have to predicate MI "in place", i.e. after this function returns,
1293 // MI will need to be transformed into a predicated form. To avoid com-
1294 // plicated manipulations with the operands (handling tied operands,
1295 // etc.), build a new temporary instruction, then overwrite MI with it.
1296
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001297 MachineBasicBlock &B = *MI.getParent();
1298 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001299 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1300 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001301 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001302 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001303 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001304 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1305 break;
1306 T.addOperand(Op);
1307 NOp++;
1308 }
1309
1310 unsigned PredReg, PredRegPos, PredRegFlags;
1311 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1312 (void)GotPredReg;
1313 assert(GotPredReg);
1314 T.addReg(PredReg, PredRegFlags);
1315 while (NOp < NumOps)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001316 T.addOperand(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001317
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001318 MI.setDesc(get(PredOpc));
1319 while (unsigned n = MI.getNumOperands())
1320 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001321 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001322 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001323
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001324 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001325 B.erase(TI);
1326
1327 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1328 MRI.clearKillFlags(PredReg);
1329 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001330}
1331
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001332
1333bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1334 ArrayRef<MachineOperand> Pred2) const {
1335 // TODO: Fix this
1336 return false;
1337}
1338
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001339
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001340bool HexagonInstrInfo::DefinesPredicate(
1341 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001342 auto &HRI = getRegisterInfo();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001343 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1344 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001345 if (MO.isReg() && MO.isDef()) {
1346 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1347 if (RC == &Hexagon::PredRegsRegClass) {
1348 Pred.push_back(MO);
1349 return true;
1350 }
1351 }
1352 }
1353 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001354}
Andrew Trickd06df962012-02-01 22:13:57 +00001355
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001356
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001357bool HexagonInstrInfo::isPredicable(MachineInstr &MI) const {
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001358 return MI.getDesc().isPredicable();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001359}
1360
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001361bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1362 const MachineBasicBlock *MBB,
1363 const MachineFunction &MF) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001364 // Debug info is never a scheduling boundary. It's necessary to be explicit
1365 // due to the special treatment of IT instructions below, otherwise a
1366 // dbg_value followed by an IT will result in the IT instruction being
1367 // considered a scheduling hazard, which is wrong. It should be the actual
1368 // instruction preceding the dbg_value instruction(s), just like it is
1369 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001370 if (MI.isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001371 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001372
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001373 // Throwing call is a boundary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001374 if (MI.isCall()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001375 // If any of the block's successors is a landing pad, this could be a
1376 // throwing call.
1377 for (auto I : MBB->successors())
1378 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001379 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001380 }
1381
1382 // Don't mess around with no return calls.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001383 if (MI.getOpcode() == Hexagon::CALLv3nr)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001384 return true;
1385
1386 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001387 if (MI.getDesc().isTerminator() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001388 return true;
1389
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001390 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1391 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001392
1393 return false;
1394}
1395
1396
1397/// Measure the specified inline asm to determine an approximation of its
1398/// length.
1399/// Comments (which run till the next SeparatorString or newline) do not
1400/// count as an instruction.
1401/// Any other non-whitespace text is considered an instruction, with
1402/// multiple instructions separated by SeparatorString or newlines.
1403/// Variable-length instructions are not handled here; this function
1404/// may be overloaded in the target code to do that.
1405/// Hexagon counts the number of ##'s and adjust for that many
1406/// constant exenders.
1407unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1408 const MCAsmInfo &MAI) const {
1409 StringRef AStr(Str);
1410 // Count the number of instructions in the asm.
1411 bool atInsnStart = true;
1412 unsigned Length = 0;
1413 for (; *Str; ++Str) {
1414 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1415 strlen(MAI.getSeparatorString())) == 0)
1416 atInsnStart = true;
1417 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1418 Length += MAI.getMaxInstLength();
1419 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001420 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001421 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
1422 strlen(MAI.getCommentString())) == 0)
1423 atInsnStart = false;
1424 }
1425
1426 // Add to size number of constant extenders seen * 4.
1427 StringRef Occ("##");
1428 Length += AStr.count(Occ)*4;
1429 return Length;
1430}
1431
1432
1433ScheduleHazardRecognizer*
1434HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1435 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
1436 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1437}
1438
1439
1440/// \brief For a comparison instruction, return the source registers in
1441/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1442/// compares against in CmpValue. Return true if the comparison instruction
1443/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001444bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1445 unsigned &SrcReg2, int &Mask,
1446 int &Value) const {
1447 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001448
1449 // Set mask and the first source register.
1450 switch (Opc) {
1451 case Hexagon::C2_cmpeq:
1452 case Hexagon::C2_cmpeqp:
1453 case Hexagon::C2_cmpgt:
1454 case Hexagon::C2_cmpgtp:
1455 case Hexagon::C2_cmpgtu:
1456 case Hexagon::C2_cmpgtup:
1457 case Hexagon::C4_cmpneq:
1458 case Hexagon::C4_cmplte:
1459 case Hexagon::C4_cmplteu:
1460 case Hexagon::C2_cmpeqi:
1461 case Hexagon::C2_cmpgti:
1462 case Hexagon::C2_cmpgtui:
1463 case Hexagon::C4_cmpneqi:
1464 case Hexagon::C4_cmplteui:
1465 case Hexagon::C4_cmpltei:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001466 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001467 Mask = ~0;
1468 break;
1469 case Hexagon::A4_cmpbeq:
1470 case Hexagon::A4_cmpbgt:
1471 case Hexagon::A4_cmpbgtu:
1472 case Hexagon::A4_cmpbeqi:
1473 case Hexagon::A4_cmpbgti:
1474 case Hexagon::A4_cmpbgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001475 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001476 Mask = 0xFF;
1477 break;
1478 case Hexagon::A4_cmpheq:
1479 case Hexagon::A4_cmphgt:
1480 case Hexagon::A4_cmphgtu:
1481 case Hexagon::A4_cmpheqi:
1482 case Hexagon::A4_cmphgti:
1483 case Hexagon::A4_cmphgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001484 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001485 Mask = 0xFFFF;
1486 break;
1487 }
1488
1489 // Set the value/second source register.
1490 switch (Opc) {
1491 case Hexagon::C2_cmpeq:
1492 case Hexagon::C2_cmpeqp:
1493 case Hexagon::C2_cmpgt:
1494 case Hexagon::C2_cmpgtp:
1495 case Hexagon::C2_cmpgtu:
1496 case Hexagon::C2_cmpgtup:
1497 case Hexagon::A4_cmpbeq:
1498 case Hexagon::A4_cmpbgt:
1499 case Hexagon::A4_cmpbgtu:
1500 case Hexagon::A4_cmpheq:
1501 case Hexagon::A4_cmphgt:
1502 case Hexagon::A4_cmphgtu:
1503 case Hexagon::C4_cmpneq:
1504 case Hexagon::C4_cmplte:
1505 case Hexagon::C4_cmplteu:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001506 SrcReg2 = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001507 return true;
1508
1509 case Hexagon::C2_cmpeqi:
1510 case Hexagon::C2_cmpgtui:
1511 case Hexagon::C2_cmpgti:
1512 case Hexagon::C4_cmpneqi:
1513 case Hexagon::C4_cmplteui:
1514 case Hexagon::C4_cmpltei:
1515 case Hexagon::A4_cmpbeqi:
1516 case Hexagon::A4_cmpbgti:
1517 case Hexagon::A4_cmpbgtui:
1518 case Hexagon::A4_cmpheqi:
1519 case Hexagon::A4_cmphgti:
1520 case Hexagon::A4_cmphgtui:
1521 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001522 Value = MI.getOperand(2).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001523 return true;
1524 }
1525
1526 return false;
1527}
1528
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001529unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001530 const MachineInstr &MI,
1531 unsigned *PredCost) const {
1532 return getInstrTimingClassLatency(ItinData, &MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001533}
1534
1535
1536DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1537 const TargetSubtargetInfo &STI) const {
1538 const InstrItineraryData *II = STI.getInstrItineraryData();
1539 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1540}
1541
1542
1543// Inspired by this pair:
1544// %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1545// S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1546// Currently AA considers the addresses in these instructions to be aliasing.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001547bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1548 MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001549 int OffsetA = 0, OffsetB = 0;
1550 unsigned SizeA = 0, SizeB = 0;
1551
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001552 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1553 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001554 return false;
1555
1556 // Instructions that are pure loads, not loads and stores like memops are not
1557 // dependent.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001558 if (MIa.mayLoad() && !isMemOp(&MIa) && MIb.mayLoad() && !isMemOp(&MIb))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001559 return true;
1560
1561 // Get base, offset, and access size in MIa.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001562 unsigned BaseRegA = getBaseAndOffset(&MIa, OffsetA, SizeA);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001563 if (!BaseRegA || !SizeA)
1564 return false;
1565
1566 // Get base, offset, and access size in MIb.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001567 unsigned BaseRegB = getBaseAndOffset(&MIb, OffsetB, SizeB);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001568 if (!BaseRegB || !SizeB)
1569 return false;
1570
1571 if (BaseRegA != BaseRegB)
1572 return false;
1573
1574 // This is a mem access with the same base register and known offsets from it.
1575 // Reason about it.
1576 if (OffsetA > OffsetB) {
1577 uint64_t offDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1578 return (SizeB <= offDiff);
1579 } else if (OffsetA < OffsetB) {
1580 uint64_t offDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1581 return (SizeA <= offDiff);
1582 }
1583
1584 return false;
1585}
1586
1587
1588unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
1589 MachineRegisterInfo &MRI = MF->getRegInfo();
1590 const TargetRegisterClass *TRC;
1591 if (VT == MVT::i1) {
1592 TRC = &Hexagon::PredRegsRegClass;
1593 } else if (VT == MVT::i32 || VT == MVT::f32) {
1594 TRC = &Hexagon::IntRegsRegClass;
1595 } else if (VT == MVT::i64 || VT == MVT::f64) {
1596 TRC = &Hexagon::DoubleRegsRegClass;
1597 } else {
1598 llvm_unreachable("Cannot handle this register class");
1599 }
1600
1601 unsigned NewReg = MRI.createVirtualRegister(TRC);
1602 return NewReg;
1603}
1604
1605
1606bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr* MI) const {
1607 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1608}
1609
1610
1611bool HexagonInstrInfo::isAccumulator(const MachineInstr *MI) const {
1612 const uint64_t F = MI->getDesc().TSFlags;
1613 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1614}
1615
1616
1617bool HexagonInstrInfo::isComplex(const MachineInstr *MI) const {
1618 const MachineFunction *MF = MI->getParent()->getParent();
1619 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1620 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1621
1622 if (!(isTC1(MI))
1623 && !(QII->isTC2Early(MI))
1624 && !(MI->getDesc().mayLoad())
1625 && !(MI->getDesc().mayStore())
1626 && (MI->getDesc().getOpcode() != Hexagon::S2_allocframe)
1627 && (MI->getDesc().getOpcode() != Hexagon::L2_deallocframe)
1628 && !(QII->isMemOp(MI))
1629 && !(MI->isBranch())
1630 && !(MI->isReturn())
1631 && !MI->isCall())
1632 return true;
1633
1634 return false;
1635}
1636
1637
Sanjay Patele4b9f502015-12-07 19:21:39 +00001638// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001639bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr *MI) const {
1640 return (getType(MI) == HexagonII::TypeCOMPOUND && MI->isBranch());
1641}
1642
1643
1644bool HexagonInstrInfo::isCondInst(const MachineInstr *MI) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001645 return (MI->isBranch() && isPredicated(*MI)) ||
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001646 isConditionalTransfer(MI) ||
1647 isConditionalALU32(MI) ||
1648 isConditionalLoad(MI) ||
1649 // Predicated stores which don't have a .new on any operands.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001650 (MI->mayStore() && isPredicated(*MI) && !isNewValueStore(MI) &&
1651 !isPredicatedNew(*MI));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001652}
1653
1654
1655bool HexagonInstrInfo::isConditionalALU32(const MachineInstr* MI) const {
1656 switch (MI->getOpcode()) {
1657 case Hexagon::A2_paddf:
1658 case Hexagon::A2_paddfnew:
1659 case Hexagon::A2_paddif:
1660 case Hexagon::A2_paddifnew:
1661 case Hexagon::A2_paddit:
1662 case Hexagon::A2_padditnew:
1663 case Hexagon::A2_paddt:
1664 case Hexagon::A2_paddtnew:
1665 case Hexagon::A2_pandf:
1666 case Hexagon::A2_pandfnew:
1667 case Hexagon::A2_pandt:
1668 case Hexagon::A2_pandtnew:
1669 case Hexagon::A2_porf:
1670 case Hexagon::A2_porfnew:
1671 case Hexagon::A2_port:
1672 case Hexagon::A2_portnew:
1673 case Hexagon::A2_psubf:
1674 case Hexagon::A2_psubfnew:
1675 case Hexagon::A2_psubt:
1676 case Hexagon::A2_psubtnew:
1677 case Hexagon::A2_pxorf:
1678 case Hexagon::A2_pxorfnew:
1679 case Hexagon::A2_pxort:
1680 case Hexagon::A2_pxortnew:
1681 case Hexagon::A4_paslhf:
1682 case Hexagon::A4_paslhfnew:
1683 case Hexagon::A4_paslht:
1684 case Hexagon::A4_paslhtnew:
1685 case Hexagon::A4_pasrhf:
1686 case Hexagon::A4_pasrhfnew:
1687 case Hexagon::A4_pasrht:
1688 case Hexagon::A4_pasrhtnew:
1689 case Hexagon::A4_psxtbf:
1690 case Hexagon::A4_psxtbfnew:
1691 case Hexagon::A4_psxtbt:
1692 case Hexagon::A4_psxtbtnew:
1693 case Hexagon::A4_psxthf:
1694 case Hexagon::A4_psxthfnew:
1695 case Hexagon::A4_psxtht:
1696 case Hexagon::A4_psxthtnew:
1697 case Hexagon::A4_pzxtbf:
1698 case Hexagon::A4_pzxtbfnew:
1699 case Hexagon::A4_pzxtbt:
1700 case Hexagon::A4_pzxtbtnew:
1701 case Hexagon::A4_pzxthf:
1702 case Hexagon::A4_pzxthfnew:
1703 case Hexagon::A4_pzxtht:
1704 case Hexagon::A4_pzxthtnew:
1705 case Hexagon::C2_ccombinewf:
1706 case Hexagon::C2_ccombinewt:
1707 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001708 }
1709 return false;
1710}
1711
1712
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001713// FIXME - Function name and it's functionality don't match.
1714// It should be renamed to hasPredNewOpcode()
1715bool HexagonInstrInfo::isConditionalLoad(const MachineInstr* MI) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001716 if (!MI->getDesc().mayLoad() || !isPredicated(*MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001717 return false;
1718
1719 int PNewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1720 // Instruction with valid predicated-new opcode can be promoted to .new.
1721 return PNewOpcode >= 0;
1722}
1723
1724
1725// Returns true if an instruction is a conditional store.
1726//
1727// Note: It doesn't include conditional new-value stores as they can't be
1728// converted to .new predicate.
1729bool HexagonInstrInfo::isConditionalStore(const MachineInstr* MI) const {
1730 switch (MI->getOpcode()) {
1731 default: return false;
1732 case Hexagon::S4_storeirbt_io:
1733 case Hexagon::S4_storeirbf_io:
1734 case Hexagon::S4_pstorerbt_rr:
1735 case Hexagon::S4_pstorerbf_rr:
1736 case Hexagon::S2_pstorerbt_io:
1737 case Hexagon::S2_pstorerbf_io:
1738 case Hexagon::S2_pstorerbt_pi:
1739 case Hexagon::S2_pstorerbf_pi:
1740 case Hexagon::S2_pstorerdt_io:
1741 case Hexagon::S2_pstorerdf_io:
1742 case Hexagon::S4_pstorerdt_rr:
1743 case Hexagon::S4_pstorerdf_rr:
1744 case Hexagon::S2_pstorerdt_pi:
1745 case Hexagon::S2_pstorerdf_pi:
1746 case Hexagon::S2_pstorerht_io:
1747 case Hexagon::S2_pstorerhf_io:
1748 case Hexagon::S4_storeirht_io:
1749 case Hexagon::S4_storeirhf_io:
1750 case Hexagon::S4_pstorerht_rr:
1751 case Hexagon::S4_pstorerhf_rr:
1752 case Hexagon::S2_pstorerht_pi:
1753 case Hexagon::S2_pstorerhf_pi:
1754 case Hexagon::S2_pstorerit_io:
1755 case Hexagon::S2_pstorerif_io:
1756 case Hexagon::S4_storeirit_io:
1757 case Hexagon::S4_storeirif_io:
1758 case Hexagon::S4_pstorerit_rr:
1759 case Hexagon::S4_pstorerif_rr:
1760 case Hexagon::S2_pstorerit_pi:
1761 case Hexagon::S2_pstorerif_pi:
1762
1763 // V4 global address store before promoting to dot new.
1764 case Hexagon::S4_pstorerdt_abs:
1765 case Hexagon::S4_pstorerdf_abs:
1766 case Hexagon::S4_pstorerbt_abs:
1767 case Hexagon::S4_pstorerbf_abs:
1768 case Hexagon::S4_pstorerht_abs:
1769 case Hexagon::S4_pstorerhf_abs:
1770 case Hexagon::S4_pstorerit_abs:
1771 case Hexagon::S4_pstorerif_abs:
1772 return true;
1773
1774 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1775 // from the "Conditional Store" list. Because a predicated new value store
1776 // would NOT be promoted to a double dot new store.
1777 // This function returns yes for those stores that are predicated but not
1778 // yet promoted to predicate dot new instructions.
1779 }
1780}
1781
1782
1783bool HexagonInstrInfo::isConditionalTransfer(const MachineInstr *MI) const {
1784 switch (MI->getOpcode()) {
1785 case Hexagon::A2_tfrt:
1786 case Hexagon::A2_tfrf:
1787 case Hexagon::C2_cmoveit:
1788 case Hexagon::C2_cmoveif:
1789 case Hexagon::A2_tfrtnew:
1790 case Hexagon::A2_tfrfnew:
1791 case Hexagon::C2_cmovenewit:
1792 case Hexagon::C2_cmovenewif:
1793 case Hexagon::A2_tfrpt:
1794 case Hexagon::A2_tfrpf:
1795 return true;
1796
1797 default:
1798 return false;
1799 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001800 return false;
1801}
1802
1803
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001804// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1805// isFPImm and later getFPImm as well.
1806bool HexagonInstrInfo::isConstExtended(const MachineInstr *MI) const {
1807 const uint64_t F = MI->getDesc().TSFlags;
1808 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1809 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001810 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001811
1812 unsigned isExtendable =
1813 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1814 if (!isExtendable)
1815 return false;
1816
1817 if (MI->isCall())
1818 return false;
1819
1820 short ExtOpNum = getCExtOpNum(MI);
1821 const MachineOperand &MO = MI->getOperand(ExtOpNum);
1822 // Use MO operand flags to determine if MO
1823 // has the HMOTF_ConstExtended flag set.
1824 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001825 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001826 // If this is a Machine BB address we are talking about, and it is
1827 // not marked as extended, say so.
1828 if (MO.isMBB())
1829 return false;
1830
1831 // We could be using an instruction with an extendable immediate and shoehorn
1832 // a global address into it. If it is a global address it will be constant
1833 // extended. We do this for COMBINE.
1834 // We currently only handle isGlobal() because it is the only kind of
1835 // object we are going to end up with here for now.
1836 // In the future we probably should add isSymbol(), etc.
1837 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
1838 MO.isJTI() || MO.isCPI())
1839 return true;
1840
1841 // If the extendable operand is not 'Immediate' type, the instruction should
1842 // have 'isExtended' flag set.
1843 assert(MO.isImm() && "Extendable operand must be Immediate type");
1844
1845 int MinValue = getMinValue(MI);
1846 int MaxValue = getMaxValue(MI);
1847 int ImmValue = MO.getImm();
1848
1849 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001850}
1851
1852
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001853bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1854 switch (MI->getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001855 case Hexagon::L4_return :
1856 case Hexagon::L4_return_t :
1857 case Hexagon::L4_return_f :
1858 case Hexagon::L4_return_tnew_pnt :
1859 case Hexagon::L4_return_fnew_pnt :
1860 case Hexagon::L4_return_tnew_pt :
1861 case Hexagon::L4_return_fnew_pt :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001862 return true;
1863 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001864 return false;
1865}
1866
1867
1868// Return true when ConsMI uses a register defined by ProdMI.
1869bool HexagonInstrInfo::isDependent(const MachineInstr *ProdMI,
1870 const MachineInstr *ConsMI) const {
1871 const MCInstrDesc &ProdMCID = ProdMI->getDesc();
1872 if (!ProdMCID.getNumDefs())
1873 return false;
1874
1875 auto &HRI = getRegisterInfo();
1876
1877 SmallVector<unsigned, 4> DefsA;
1878 SmallVector<unsigned, 4> DefsB;
1879 SmallVector<unsigned, 8> UsesA;
1880 SmallVector<unsigned, 8> UsesB;
1881
1882 parseOperands(ProdMI, DefsA, UsesA);
1883 parseOperands(ConsMI, DefsB, UsesB);
1884
1885 for (auto &RegA : DefsA)
1886 for (auto &RegB : UsesB) {
1887 // True data dependency.
1888 if (RegA == RegB)
1889 return true;
1890
1891 if (Hexagon::DoubleRegsRegClass.contains(RegA))
1892 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1893 if (RegB == *SubRegs)
1894 return true;
1895
1896 if (Hexagon::DoubleRegsRegClass.contains(RegB))
1897 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1898 if (RegA == *SubRegs)
1899 return true;
1900 }
1901
1902 return false;
1903}
1904
1905
1906// Returns true if the instruction is alread a .cur.
1907bool HexagonInstrInfo::isDotCurInst(const MachineInstr* MI) const {
1908 switch (MI->getOpcode()) {
1909 case Hexagon::V6_vL32b_cur_pi:
1910 case Hexagon::V6_vL32b_cur_ai:
1911 case Hexagon::V6_vL32b_cur_pi_128B:
1912 case Hexagon::V6_vL32b_cur_ai_128B:
1913 return true;
1914 }
1915 return false;
1916}
1917
1918
1919// Returns true, if any one of the operands is a dot new
1920// insn, whether it is predicated dot new or register dot new.
1921bool HexagonInstrInfo::isDotNewInst(const MachineInstr* MI) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001922 if (isNewValueInst(MI) || (isPredicated(*MI) && isPredicatedNew(*MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001923 return true;
1924
1925 return false;
1926}
1927
1928
1929/// Symmetrical. See if these two instructions are fit for duplex pair.
1930bool HexagonInstrInfo::isDuplexPair(const MachineInstr *MIa,
1931 const MachineInstr *MIb) const {
1932 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
1933 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
1934 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
1935}
1936
1937
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00001938bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001939 if (!MI)
1940 return false;
1941
1942 if (MI->mayLoad() || MI->mayStore() || MI->isCompare())
1943 return true;
1944
1945 // Multiply
1946 unsigned SchedClass = MI->getDesc().getSchedClass();
1947 if (SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23)
1948 return true;
1949 return false;
1950}
1951
1952
1953bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
1954 return (Opcode == Hexagon::ENDLOOP0 ||
1955 Opcode == Hexagon::ENDLOOP1);
1956}
1957
1958
1959bool HexagonInstrInfo::isExpr(unsigned OpType) const {
1960 switch(OpType) {
1961 case MachineOperand::MO_MachineBasicBlock:
1962 case MachineOperand::MO_GlobalAddress:
1963 case MachineOperand::MO_ExternalSymbol:
1964 case MachineOperand::MO_JumpTableIndex:
1965 case MachineOperand::MO_ConstantPoolIndex:
1966 case MachineOperand::MO_BlockAddress:
1967 return true;
1968 default:
1969 return false;
1970 }
1971}
1972
1973
1974bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
1975 const MCInstrDesc &MID = MI->getDesc();
1976 const uint64_t F = MID.TSFlags;
1977 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
1978 return true;
1979
1980 // TODO: This is largely obsolete now. Will need to be removed
1981 // in consecutive patches.
1982 switch(MI->getOpcode()) {
1983 // TFR_FI Remains a special case.
1984 case Hexagon::TFR_FI:
1985 return true;
1986 default:
1987 return false;
1988 }
1989 return false;
1990}
1991
1992
1993// This returns true in two cases:
1994// - The OP code itself indicates that this is an extended instruction.
1995// - One of MOs has been marked with HMOTF_ConstExtended flag.
1996bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
1997 // First check if this is permanently extended op code.
1998 const uint64_t F = MI->getDesc().TSFlags;
1999 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2000 return true;
2001 // Use MO operand flags to determine if one of MI's operands
2002 // has HMOTF_ConstExtended flag set.
2003 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
2004 E = MI->operands_end(); I != E; ++I) {
2005 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
2006 return true;
2007 }
2008 return false;
2009}
2010
2011
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002012bool HexagonInstrInfo::isFloat(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002013 unsigned Opcode = MI->getOpcode();
2014 const uint64_t F = get(Opcode).TSFlags;
2015 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2016}
2017
2018
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002019// No V60 HVX VMEM with A_INDIRECT.
2020bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr *I,
2021 const MachineInstr *J) const {
2022 if (!isV60VectorInstruction(I))
2023 return false;
2024 if (!I->mayLoad() && !I->mayStore())
2025 return false;
2026 return J->isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
2027}
2028
2029
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002030bool HexagonInstrInfo::isIndirectCall(const MachineInstr *MI) const {
2031 switch (MI->getOpcode()) {
2032 case Hexagon::J2_callr :
2033 case Hexagon::J2_callrf :
2034 case Hexagon::J2_callrt :
2035 return true;
2036 }
2037 return false;
2038}
2039
2040
2041bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr *MI) const {
2042 switch (MI->getOpcode()) {
2043 case Hexagon::L4_return :
2044 case Hexagon::L4_return_t :
2045 case Hexagon::L4_return_f :
2046 case Hexagon::L4_return_fnew_pnt :
2047 case Hexagon::L4_return_fnew_pt :
2048 case Hexagon::L4_return_tnew_pnt :
2049 case Hexagon::L4_return_tnew_pt :
2050 return true;
2051 }
2052 return false;
2053}
2054
2055
2056bool HexagonInstrInfo::isJumpR(const MachineInstr *MI) const {
2057 switch (MI->getOpcode()) {
2058 case Hexagon::J2_jumpr :
2059 case Hexagon::J2_jumprt :
2060 case Hexagon::J2_jumprf :
2061 case Hexagon::J2_jumprtnewpt :
2062 case Hexagon::J2_jumprfnewpt :
2063 case Hexagon::J2_jumprtnew :
2064 case Hexagon::J2_jumprfnew :
2065 return true;
2066 }
2067 return false;
2068}
2069
2070
2071// Return true if a given MI can accomodate given offset.
2072// Use abs estimate as oppose to the exact number.
2073// TODO: This will need to be changed to use MC level
2074// definition of instruction extendable field size.
2075bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr *MI,
2076 unsigned offset) const {
2077 // This selection of jump instructions matches to that what
2078 // AnalyzeBranch can parse, plus NVJ.
2079 if (isNewValueJump(MI)) // r9:2
2080 return isInt<11>(offset);
2081
2082 switch (MI->getOpcode()) {
2083 // Still missing Jump to address condition on register value.
2084 default:
2085 return false;
2086 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2087 case Hexagon::J2_call:
2088 case Hexagon::CALLv3nr:
2089 return isInt<24>(offset);
2090 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2091 case Hexagon::J2_jumpf:
2092 case Hexagon::J2_jumptnew:
2093 case Hexagon::J2_jumptnewpt:
2094 case Hexagon::J2_jumpfnew:
2095 case Hexagon::J2_jumpfnewpt:
2096 case Hexagon::J2_callt:
2097 case Hexagon::J2_callf:
2098 return isInt<17>(offset);
2099 case Hexagon::J2_loop0i:
2100 case Hexagon::J2_loop0iext:
2101 case Hexagon::J2_loop0r:
2102 case Hexagon::J2_loop0rext:
2103 case Hexagon::J2_loop1i:
2104 case Hexagon::J2_loop1iext:
2105 case Hexagon::J2_loop1r:
2106 case Hexagon::J2_loop1rext:
2107 return isInt<9>(offset);
2108 // TODO: Add all the compound branches here. Can we do this in Relation model?
2109 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2110 case Hexagon::J4_cmpeqi_tp1_jump_nt:
2111 return isInt<11>(offset);
2112 }
2113}
2114
2115
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002116bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr *LRMI,
2117 const MachineInstr *ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002118 if (!LRMI || !ESMI)
2119 return false;
2120
2121 bool isLate = isLateResultInstr(LRMI);
2122 bool isEarly = isEarlySourceInstr(ESMI);
2123
2124 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
2125 DEBUG(LRMI->dump());
2126 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
2127 DEBUG(ESMI->dump());
2128
2129 if (isLate && isEarly) {
2130 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2131 return true;
2132 }
2133
2134 return false;
2135}
2136
2137
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002138bool HexagonInstrInfo::isLateResultInstr(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002139 if (!MI)
2140 return false;
2141
2142 switch (MI->getOpcode()) {
2143 case TargetOpcode::EXTRACT_SUBREG:
2144 case TargetOpcode::INSERT_SUBREG:
2145 case TargetOpcode::SUBREG_TO_REG:
2146 case TargetOpcode::REG_SEQUENCE:
2147 case TargetOpcode::IMPLICIT_DEF:
2148 case TargetOpcode::COPY:
2149 case TargetOpcode::INLINEASM:
2150 case TargetOpcode::PHI:
2151 return false;
2152 default:
2153 break;
2154 }
2155
2156 unsigned SchedClass = MI->getDesc().getSchedClass();
2157
2158 switch (SchedClass) {
2159 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2160 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2161 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2162 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2163 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2164 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2165 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2166 case Hexagon::Sched::V2LDST_tc_ld_SLOT01:
2167 case Hexagon::Sched::V2LDST_tc_st_SLOT0:
2168 case Hexagon::Sched::V2LDST_tc_st_SLOT01:
2169 case Hexagon::Sched::V4LDST_tc_ld_SLOT01:
2170 case Hexagon::Sched::V4LDST_tc_st_SLOT0:
2171 case Hexagon::Sched::V4LDST_tc_st_SLOT01:
2172 return false;
2173 }
2174 return true;
2175}
2176
2177
2178bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr *MI) const {
2179 if (!MI)
2180 return false;
2181
2182 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2183 // resource, but all operands can be received late like an ALU instruction.
2184 return MI->getDesc().getSchedClass() == Hexagon::Sched::CVI_VX_LATE;
2185}
2186
2187
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002188bool HexagonInstrInfo::isLoopN(const MachineInstr *MI) const {
2189 unsigned Opcode = MI->getOpcode();
2190 return Opcode == Hexagon::J2_loop0i ||
2191 Opcode == Hexagon::J2_loop0r ||
2192 Opcode == Hexagon::J2_loop0iext ||
2193 Opcode == Hexagon::J2_loop0rext ||
2194 Opcode == Hexagon::J2_loop1i ||
2195 Opcode == Hexagon::J2_loop1r ||
2196 Opcode == Hexagon::J2_loop1iext ||
2197 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002198}
2199
2200
2201bool HexagonInstrInfo::isMemOp(const MachineInstr *MI) const {
2202 switch (MI->getOpcode()) {
2203 default: return false;
2204 case Hexagon::L4_iadd_memopw_io :
2205 case Hexagon::L4_isub_memopw_io :
2206 case Hexagon::L4_add_memopw_io :
2207 case Hexagon::L4_sub_memopw_io :
2208 case Hexagon::L4_and_memopw_io :
2209 case Hexagon::L4_or_memopw_io :
2210 case Hexagon::L4_iadd_memoph_io :
2211 case Hexagon::L4_isub_memoph_io :
2212 case Hexagon::L4_add_memoph_io :
2213 case Hexagon::L4_sub_memoph_io :
2214 case Hexagon::L4_and_memoph_io :
2215 case Hexagon::L4_or_memoph_io :
2216 case Hexagon::L4_iadd_memopb_io :
2217 case Hexagon::L4_isub_memopb_io :
2218 case Hexagon::L4_add_memopb_io :
2219 case Hexagon::L4_sub_memopb_io :
2220 case Hexagon::L4_and_memopb_io :
2221 case Hexagon::L4_or_memopb_io :
2222 case Hexagon::L4_ior_memopb_io:
2223 case Hexagon::L4_ior_memoph_io:
2224 case Hexagon::L4_ior_memopw_io:
2225 case Hexagon::L4_iand_memopb_io:
2226 case Hexagon::L4_iand_memoph_io:
2227 case Hexagon::L4_iand_memopw_io:
2228 return true;
2229 }
2230 return false;
2231}
2232
2233
2234bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
2235 const uint64_t F = MI->getDesc().TSFlags;
2236 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2237}
2238
2239
2240bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2241 const uint64_t F = get(Opcode).TSFlags;
2242 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2243}
2244
2245
2246bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
2247 return isNewValueJump(MI) || isNewValueStore(MI);
2248}
2249
2250
2251bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
2252 return isNewValue(MI) && MI->isBranch();
2253}
2254
2255
2256bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2257 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2258}
2259
2260
2261bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
2262 const uint64_t F = MI->getDesc().TSFlags;
2263 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2264}
2265
2266
2267bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2268 const uint64_t F = get(Opcode).TSFlags;
2269 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2270}
2271
2272
2273// Returns true if a particular operand is extendable for an instruction.
2274bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
2275 unsigned OperandNum) const {
2276 const uint64_t F = MI->getDesc().TSFlags;
2277 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2278 == OperandNum;
2279}
2280
2281
2282bool HexagonInstrInfo::isPostIncrement(const MachineInstr* MI) const {
2283 return getAddrMode(MI) == HexagonII::PostInc;
2284}
2285
2286
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002287bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2288 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002289 assert(isPredicated(MI));
2290 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2291}
2292
2293
2294bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2295 const uint64_t F = get(Opcode).TSFlags;
2296 assert(isPredicated(Opcode));
2297 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2298}
2299
2300
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002301bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2302 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002303 return !((F >> HexagonII::PredicatedFalsePos) &
2304 HexagonII::PredicatedFalseMask);
2305}
2306
2307
2308bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2309 const uint64_t F = get(Opcode).TSFlags;
2310 // Make sure that the instruction is predicated.
2311 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2312 return !((F >> HexagonII::PredicatedFalsePos) &
2313 HexagonII::PredicatedFalseMask);
2314}
2315
2316
2317bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2318 const uint64_t F = get(Opcode).TSFlags;
2319 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2320}
2321
2322
2323bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2324 const uint64_t F = get(Opcode).TSFlags;
2325 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2326}
2327
2328
2329bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2330 const uint64_t F = get(Opcode).TSFlags;
2331 assert(get(Opcode).isBranch() &&
2332 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2333 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2334}
2335
2336
2337bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
2338 return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
Krzysztof Parzyszek181fdbd2016-03-24 19:18:48 +00002339 MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2340 MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2341 MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002342}
2343
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002344bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2345 switch (MI.getOpcode()) {
2346 // Byte
2347 case Hexagon::L2_loadrb_io:
2348 case Hexagon::L4_loadrb_ur:
2349 case Hexagon::L4_loadrb_ap:
2350 case Hexagon::L2_loadrb_pr:
2351 case Hexagon::L2_loadrb_pbr:
2352 case Hexagon::L2_loadrb_pi:
2353 case Hexagon::L2_loadrb_pci:
2354 case Hexagon::L2_loadrb_pcr:
2355 case Hexagon::L2_loadbsw2_io:
2356 case Hexagon::L4_loadbsw2_ur:
2357 case Hexagon::L4_loadbsw2_ap:
2358 case Hexagon::L2_loadbsw2_pr:
2359 case Hexagon::L2_loadbsw2_pbr:
2360 case Hexagon::L2_loadbsw2_pi:
2361 case Hexagon::L2_loadbsw2_pci:
2362 case Hexagon::L2_loadbsw2_pcr:
2363 case Hexagon::L2_loadbsw4_io:
2364 case Hexagon::L4_loadbsw4_ur:
2365 case Hexagon::L4_loadbsw4_ap:
2366 case Hexagon::L2_loadbsw4_pr:
2367 case Hexagon::L2_loadbsw4_pbr:
2368 case Hexagon::L2_loadbsw4_pi:
2369 case Hexagon::L2_loadbsw4_pci:
2370 case Hexagon::L2_loadbsw4_pcr:
2371 case Hexagon::L4_loadrb_rr:
2372 case Hexagon::L2_ploadrbt_io:
2373 case Hexagon::L2_ploadrbt_pi:
2374 case Hexagon::L2_ploadrbf_io:
2375 case Hexagon::L2_ploadrbf_pi:
2376 case Hexagon::L2_ploadrbtnew_io:
2377 case Hexagon::L2_ploadrbfnew_io:
2378 case Hexagon::L4_ploadrbt_rr:
2379 case Hexagon::L4_ploadrbf_rr:
2380 case Hexagon::L4_ploadrbtnew_rr:
2381 case Hexagon::L4_ploadrbfnew_rr:
2382 case Hexagon::L2_ploadrbtnew_pi:
2383 case Hexagon::L2_ploadrbfnew_pi:
2384 case Hexagon::L4_ploadrbt_abs:
2385 case Hexagon::L4_ploadrbf_abs:
2386 case Hexagon::L4_ploadrbtnew_abs:
2387 case Hexagon::L4_ploadrbfnew_abs:
2388 case Hexagon::L2_loadrbgp:
2389 // Half
2390 case Hexagon::L2_loadrh_io:
2391 case Hexagon::L4_loadrh_ur:
2392 case Hexagon::L4_loadrh_ap:
2393 case Hexagon::L2_loadrh_pr:
2394 case Hexagon::L2_loadrh_pbr:
2395 case Hexagon::L2_loadrh_pi:
2396 case Hexagon::L2_loadrh_pci:
2397 case Hexagon::L2_loadrh_pcr:
2398 case Hexagon::L4_loadrh_rr:
2399 case Hexagon::L2_ploadrht_io:
2400 case Hexagon::L2_ploadrht_pi:
2401 case Hexagon::L2_ploadrhf_io:
2402 case Hexagon::L2_ploadrhf_pi:
2403 case Hexagon::L2_ploadrhtnew_io:
2404 case Hexagon::L2_ploadrhfnew_io:
2405 case Hexagon::L4_ploadrht_rr:
2406 case Hexagon::L4_ploadrhf_rr:
2407 case Hexagon::L4_ploadrhtnew_rr:
2408 case Hexagon::L4_ploadrhfnew_rr:
2409 case Hexagon::L2_ploadrhtnew_pi:
2410 case Hexagon::L2_ploadrhfnew_pi:
2411 case Hexagon::L4_ploadrht_abs:
2412 case Hexagon::L4_ploadrhf_abs:
2413 case Hexagon::L4_ploadrhtnew_abs:
2414 case Hexagon::L4_ploadrhfnew_abs:
2415 case Hexagon::L2_loadrhgp:
2416 return true;
2417 default:
2418 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002419 }
2420}
2421
2422
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002423bool HexagonInstrInfo::isSolo(const MachineInstr* MI) const {
2424 const uint64_t F = MI->getDesc().TSFlags;
2425 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2426}
2427
2428
2429bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr *MI) const {
2430 switch (MI->getOpcode()) {
2431 case Hexagon::STriw_pred :
2432 case Hexagon::LDriw_pred :
2433 return true;
2434 default:
2435 return false;
2436 }
2437}
2438
2439
2440// Returns true when SU has a timing class TC1.
2441bool HexagonInstrInfo::isTC1(const MachineInstr *MI) const {
2442 unsigned SchedClass = MI->getDesc().getSchedClass();
2443 switch (SchedClass) {
2444 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2445 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2446 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2447 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2448 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2449 //case Hexagon::Sched::M_tc_1_SLOT23:
2450 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2451 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2452 return true;
2453
2454 default:
2455 return false;
2456 }
2457}
2458
2459
2460bool HexagonInstrInfo::isTC2(const MachineInstr *MI) const {
2461 unsigned SchedClass = MI->getDesc().getSchedClass();
2462 switch (SchedClass) {
2463 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
2464 case Hexagon::Sched::ALU64_tc_2_SLOT23:
2465 case Hexagon::Sched::CR_tc_2_SLOT3:
2466 case Hexagon::Sched::M_tc_2_SLOT23:
2467 case Hexagon::Sched::S_2op_tc_2_SLOT23:
2468 case Hexagon::Sched::S_3op_tc_2_SLOT23:
2469 return true;
2470
2471 default:
2472 return false;
2473 }
2474}
2475
2476
2477bool HexagonInstrInfo::isTC2Early(const MachineInstr *MI) const {
2478 unsigned SchedClass = MI->getDesc().getSchedClass();
2479 switch (SchedClass) {
2480 case Hexagon::Sched::ALU32_2op_tc_2early_SLOT0123:
2481 case Hexagon::Sched::ALU32_3op_tc_2early_SLOT0123:
2482 case Hexagon::Sched::ALU64_tc_2early_SLOT23:
2483 case Hexagon::Sched::CR_tc_2early_SLOT23:
2484 case Hexagon::Sched::CR_tc_2early_SLOT3:
2485 case Hexagon::Sched::J_tc_2early_SLOT0123:
2486 case Hexagon::Sched::J_tc_2early_SLOT2:
2487 case Hexagon::Sched::J_tc_2early_SLOT23:
2488 case Hexagon::Sched::S_2op_tc_2early_SLOT23:
2489 case Hexagon::Sched::S_3op_tc_2early_SLOT23:
2490 return true;
2491
2492 default:
2493 return false;
2494 }
2495}
2496
2497
2498bool HexagonInstrInfo::isTC4x(const MachineInstr *MI) const {
2499 if (!MI)
2500 return false;
2501
2502 unsigned SchedClass = MI->getDesc().getSchedClass();
2503 return SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23;
2504}
2505
2506
2507bool HexagonInstrInfo::isV60VectorInstruction(const MachineInstr *MI) const {
2508 if (!MI)
2509 return false;
2510
2511 const uint64_t V = getType(MI);
2512 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2513}
2514
2515
2516// Check if the Offset is a valid auto-inc imm by Load/Store Type.
2517//
2518bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2519 if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2520 VT == MVT::v32i16 || VT == MVT::v64i8) {
2521 return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2522 Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2523 (Offset & 0x3f) == 0);
2524 }
2525 // 128B
2526 if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2527 VT == MVT::v64i16 || VT == MVT::v128i8) {
2528 return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2529 Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2530 (Offset & 0x7f) == 0);
2531 }
2532 if (VT == MVT::i64) {
2533 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2534 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2535 (Offset & 0x7) == 0);
2536 }
2537 if (VT == MVT::i32) {
2538 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2539 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2540 (Offset & 0x3) == 0);
2541 }
2542 if (VT == MVT::i16) {
2543 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2544 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2545 (Offset & 0x1) == 0);
2546 }
2547 if (VT == MVT::i8) {
2548 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2549 Offset <= Hexagon_MEMB_AUTOINC_MAX);
2550 }
2551 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002552}
2553
2554
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002555bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2556 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002557 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002558 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002559 // inserted to calculate the final address. Due to this reason, the function
2560 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002561 // We used to assert if the offset was not properly aligned, however,
2562 // there are cases where a misaligned pointer recast can cause this
2563 // problem, and we need to allow for it. The front end warns of such
2564 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002565
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002566 switch (Opcode) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002567 case Hexagon::STriq_pred_V6:
2568 case Hexagon::STriq_pred_vec_V6:
2569 case Hexagon::STriv_pseudo_V6:
2570 case Hexagon::STrivv_pseudo_V6:
2571 case Hexagon::LDriq_pred_V6:
2572 case Hexagon::LDriq_pred_vec_V6:
2573 case Hexagon::LDriv_pseudo_V6:
2574 case Hexagon::LDrivv_pseudo_V6:
2575 case Hexagon::LDrivv_indexed:
2576 case Hexagon::STrivv_indexed:
2577 case Hexagon::V6_vL32b_ai:
2578 case Hexagon::V6_vS32b_ai:
2579 case Hexagon::V6_vL32Ub_ai:
2580 case Hexagon::V6_vS32Ub_ai:
2581 return (Offset >= Hexagon_MEMV_OFFSET_MIN) &&
2582 (Offset <= Hexagon_MEMV_OFFSET_MAX);
2583
2584 case Hexagon::STriq_pred_V6_128B:
2585 case Hexagon::STriq_pred_vec_V6_128B:
2586 case Hexagon::STriv_pseudo_V6_128B:
2587 case Hexagon::STrivv_pseudo_V6_128B:
2588 case Hexagon::LDriq_pred_V6_128B:
2589 case Hexagon::LDriq_pred_vec_V6_128B:
2590 case Hexagon::LDriv_pseudo_V6_128B:
2591 case Hexagon::LDrivv_pseudo_V6_128B:
2592 case Hexagon::LDrivv_indexed_128B:
2593 case Hexagon::STrivv_indexed_128B:
2594 case Hexagon::V6_vL32b_ai_128B:
2595 case Hexagon::V6_vS32b_ai_128B:
2596 case Hexagon::V6_vL32Ub_ai_128B:
2597 case Hexagon::V6_vS32Ub_ai_128B:
2598 return (Offset >= Hexagon_MEMV_OFFSET_MIN_128B) &&
2599 (Offset <= Hexagon_MEMV_OFFSET_MAX_128B);
2600
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002601 case Hexagon::J2_loop0i:
2602 case Hexagon::J2_loop1i:
2603 return isUInt<10>(Offset);
2604 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002605
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002606 if (Extend)
2607 return true;
2608
2609 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002610 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002611 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002612 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2613 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2614
Colin LeMahieu947cd702014-12-23 20:44:59 +00002615 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002616 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002617 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2618 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2619
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002620 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002621 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002622 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002623 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2624 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2625
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002626 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002627 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002628 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002629 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2630 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2631
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002632 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002633 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2634 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2635
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002636 case Hexagon::L4_iadd_memopw_io :
2637 case Hexagon::L4_isub_memopw_io :
2638 case Hexagon::L4_add_memopw_io :
2639 case Hexagon::L4_sub_memopw_io :
2640 case Hexagon::L4_and_memopw_io :
2641 case Hexagon::L4_or_memopw_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002642 return (0 <= Offset && Offset <= 255);
2643
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002644 case Hexagon::L4_iadd_memoph_io :
2645 case Hexagon::L4_isub_memoph_io :
2646 case Hexagon::L4_add_memoph_io :
2647 case Hexagon::L4_sub_memoph_io :
2648 case Hexagon::L4_and_memoph_io :
2649 case Hexagon::L4_or_memoph_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002650 return (0 <= Offset && Offset <= 127);
2651
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002652 case Hexagon::L4_iadd_memopb_io :
2653 case Hexagon::L4_isub_memopb_io :
2654 case Hexagon::L4_add_memopb_io :
2655 case Hexagon::L4_sub_memopb_io :
2656 case Hexagon::L4_and_memopb_io :
2657 case Hexagon::L4_or_memopb_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002658 return (0 <= Offset && Offset <= 63);
2659
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002660 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002661 // any size. Later pass knows how to handle it.
2662 case Hexagon::STriw_pred:
2663 case Hexagon::LDriw_pred:
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00002664 case Hexagon::STriw_mod:
2665 case Hexagon::LDriw_mod:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002666 return true;
2667
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002668 case Hexagon::TFR_FI:
2669 case Hexagon::TFR_FIA:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002670 case Hexagon::INLINEASM:
2671 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002672
2673 case Hexagon::L2_ploadrbt_io:
2674 case Hexagon::L2_ploadrbf_io:
2675 case Hexagon::L2_ploadrubt_io:
2676 case Hexagon::L2_ploadrubf_io:
2677 case Hexagon::S2_pstorerbt_io:
2678 case Hexagon::S2_pstorerbf_io:
2679 case Hexagon::S4_storeirb_io:
2680 case Hexagon::S4_storeirbt_io:
2681 case Hexagon::S4_storeirbf_io:
2682 return isUInt<6>(Offset);
2683
2684 case Hexagon::L2_ploadrht_io:
2685 case Hexagon::L2_ploadrhf_io:
2686 case Hexagon::L2_ploadruht_io:
2687 case Hexagon::L2_ploadruhf_io:
2688 case Hexagon::S2_pstorerht_io:
2689 case Hexagon::S2_pstorerhf_io:
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::L2_ploadrit_io:
2696 case Hexagon::L2_ploadrif_io:
2697 case Hexagon::S2_pstorerit_io:
2698 case Hexagon::S2_pstorerif_io:
2699 case Hexagon::S4_storeiri_io:
2700 case Hexagon::S4_storeirit_io:
2701 case Hexagon::S4_storeirif_io:
2702 return isShiftedUInt<6,2>(Offset);
2703
2704 case Hexagon::L2_ploadrdt_io:
2705 case Hexagon::L2_ploadrdf_io:
2706 case Hexagon::S2_pstorerdt_io:
2707 case Hexagon::S2_pstorerdf_io:
2708 return isShiftedUInt<6,3>(Offset);
2709 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002710
Benjamin Kramerb6684012011-12-27 11:41:05 +00002711 llvm_unreachable("No offset range is defined for this opcode. "
2712 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002713}
2714
2715
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002716bool HexagonInstrInfo::isVecAcc(const MachineInstr *MI) const {
2717 return MI && isV60VectorInstruction(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002718}
2719
2720
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002721bool HexagonInstrInfo::isVecALU(const MachineInstr *MI) const {
2722 if (!MI)
Andrew Trickd06df962012-02-01 22:13:57 +00002723 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002724 const uint64_t F = get(MI->getOpcode()).TSFlags;
2725 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2726 return
2727 V == HexagonII::TypeCVI_VA ||
2728 V == HexagonII::TypeCVI_VA_DV;
2729}
Andrew Trickd06df962012-02-01 22:13:57 +00002730
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002731
2732bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr *ProdMI,
2733 const MachineInstr *ConsMI) const {
2734 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2735 return true;
2736
2737 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2738 return true;
2739
2740 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002741 return true;
2742
2743 return false;
2744}
Jyotsna Verma84256432013-03-01 17:37:13 +00002745
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002746bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2747 switch (MI.getOpcode()) {
2748 // Byte
2749 case Hexagon::L2_loadrub_io:
2750 case Hexagon::L4_loadrub_ur:
2751 case Hexagon::L4_loadrub_ap:
2752 case Hexagon::L2_loadrub_pr:
2753 case Hexagon::L2_loadrub_pbr:
2754 case Hexagon::L2_loadrub_pi:
2755 case Hexagon::L2_loadrub_pci:
2756 case Hexagon::L2_loadrub_pcr:
2757 case Hexagon::L2_loadbzw2_io:
2758 case Hexagon::L4_loadbzw2_ur:
2759 case Hexagon::L4_loadbzw2_ap:
2760 case Hexagon::L2_loadbzw2_pr:
2761 case Hexagon::L2_loadbzw2_pbr:
2762 case Hexagon::L2_loadbzw2_pi:
2763 case Hexagon::L2_loadbzw2_pci:
2764 case Hexagon::L2_loadbzw2_pcr:
2765 case Hexagon::L2_loadbzw4_io:
2766 case Hexagon::L4_loadbzw4_ur:
2767 case Hexagon::L4_loadbzw4_ap:
2768 case Hexagon::L2_loadbzw4_pr:
2769 case Hexagon::L2_loadbzw4_pbr:
2770 case Hexagon::L2_loadbzw4_pi:
2771 case Hexagon::L2_loadbzw4_pci:
2772 case Hexagon::L2_loadbzw4_pcr:
2773 case Hexagon::L4_loadrub_rr:
2774 case Hexagon::L2_ploadrubt_io:
2775 case Hexagon::L2_ploadrubt_pi:
2776 case Hexagon::L2_ploadrubf_io:
2777 case Hexagon::L2_ploadrubf_pi:
2778 case Hexagon::L2_ploadrubtnew_io:
2779 case Hexagon::L2_ploadrubfnew_io:
2780 case Hexagon::L4_ploadrubt_rr:
2781 case Hexagon::L4_ploadrubf_rr:
2782 case Hexagon::L4_ploadrubtnew_rr:
2783 case Hexagon::L4_ploadrubfnew_rr:
2784 case Hexagon::L2_ploadrubtnew_pi:
2785 case Hexagon::L2_ploadrubfnew_pi:
2786 case Hexagon::L4_ploadrubt_abs:
2787 case Hexagon::L4_ploadrubf_abs:
2788 case Hexagon::L4_ploadrubtnew_abs:
2789 case Hexagon::L4_ploadrubfnew_abs:
2790 case Hexagon::L2_loadrubgp:
2791 // Half
2792 case Hexagon::L2_loadruh_io:
2793 case Hexagon::L4_loadruh_ur:
2794 case Hexagon::L4_loadruh_ap:
2795 case Hexagon::L2_loadruh_pr:
2796 case Hexagon::L2_loadruh_pbr:
2797 case Hexagon::L2_loadruh_pi:
2798 case Hexagon::L2_loadruh_pci:
2799 case Hexagon::L2_loadruh_pcr:
2800 case Hexagon::L4_loadruh_rr:
2801 case Hexagon::L2_ploadruht_io:
2802 case Hexagon::L2_ploadruht_pi:
2803 case Hexagon::L2_ploadruhf_io:
2804 case Hexagon::L2_ploadruhf_pi:
2805 case Hexagon::L2_ploadruhtnew_io:
2806 case Hexagon::L2_ploadruhfnew_io:
2807 case Hexagon::L4_ploadruht_rr:
2808 case Hexagon::L4_ploadruhf_rr:
2809 case Hexagon::L4_ploadruhtnew_rr:
2810 case Hexagon::L4_ploadruhfnew_rr:
2811 case Hexagon::L2_ploadruhtnew_pi:
2812 case Hexagon::L2_ploadruhfnew_pi:
2813 case Hexagon::L4_ploadruht_abs:
2814 case Hexagon::L4_ploadruhf_abs:
2815 case Hexagon::L4_ploadruhtnew_abs:
2816 case Hexagon::L4_ploadruhfnew_abs:
2817 case Hexagon::L2_loadruhgp:
2818 return true;
2819 default:
2820 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002821 }
2822}
2823
2824
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002825/// \brief Can these instructions execute at the same time in a bundle.
2826bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr *First,
2827 const MachineInstr *Second) const {
2828 if (DisableNVSchedule)
2829 return false;
2830 if (mayBeNewStore(Second)) {
2831 // Make sure the definition of the first instruction is the value being
2832 // stored.
2833 const MachineOperand &Stored =
2834 Second->getOperand(Second->getNumOperands() - 1);
2835 if (!Stored.isReg())
2836 return false;
2837 for (unsigned i = 0, e = First->getNumOperands(); i < e; ++i) {
2838 const MachineOperand &Op = First->getOperand(i);
2839 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2840 return true;
2841 }
2842 }
2843 return false;
2844}
2845
2846
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002847bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2848 for (auto &I : *B)
2849 if (I.isEHLabel())
2850 return true;
2851 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002852}
2853
Jyotsna Verma84256432013-03-01 17:37:13 +00002854
2855// Returns true if an instruction can be converted into a non-extended
2856// equivalent instruction.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002857bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr *MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002858 short NonExtOpcode;
2859 // Check if the instruction has a register form that uses register in place
2860 // of the extended operand, if so return that as the non-extended form.
2861 if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
2862 return true;
2863
2864 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002865 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002866
2867 switch (getAddrMode(MI)) {
2868 case HexagonII::Absolute :
2869 // Load/store with absolute addressing mode can be converted into
2870 // base+offset mode.
Krzysztof Parzyszek02579052015-10-20 19:21:05 +00002871 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI->getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002872 break;
2873 case HexagonII::BaseImmOffset :
2874 // Load/store with base+offset addressing mode can be converted into
2875 // base+register offset addressing mode. However left shift operand should
2876 // be set to 0.
2877 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
2878 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002879 case HexagonII::BaseLongOffset:
2880 NonExtOpcode = Hexagon::getRegShlForm(MI->getOpcode());
2881 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002882 default:
2883 return false;
2884 }
2885 if (NonExtOpcode < 0)
2886 return false;
2887 return true;
2888 }
2889 return false;
2890}
2891
Jyotsna Verma84256432013-03-01 17:37:13 +00002892
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002893bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002894 return Hexagon::getRealHWInstr(MI->getOpcode(),
2895 Hexagon::InstrType_Pseudo) >= 0;
2896}
2897
2898
2899bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2900 const {
2901 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2902 while (I != E) {
2903 if (I->isBarrier())
2904 return true;
2905 ++I;
2906 }
2907 return false;
2908}
2909
2910
2911// Returns true, if a LD insn can be promoted to a cur load.
2912bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr *MI) const {
2913 auto &HST = MI->getParent()->getParent()->getSubtarget<HexagonSubtarget>();
2914 const uint64_t F = MI->getDesc().TSFlags;
2915 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
2916 HST.hasV60TOps();
2917}
2918
2919
2920// Returns true, if a ST insn can be promoted to a new-value store.
2921bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
2922 const uint64_t F = MI->getDesc().TSFlags;
2923 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
2924}
2925
2926
2927bool HexagonInstrInfo::producesStall(const MachineInstr *ProdMI,
2928 const MachineInstr *ConsMI) const {
2929 // There is no stall when ProdMI is not a V60 vector.
2930 if (!isV60VectorInstruction(ProdMI))
2931 return false;
2932
2933 // There is no stall when ProdMI and ConsMI are not dependent.
2934 if (!isDependent(ProdMI, ConsMI))
2935 return false;
2936
2937 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
2938 // are scheduled in consecutive packets.
2939 if (isVecUsableNextPacket(ProdMI, ConsMI))
2940 return false;
2941
2942 return true;
2943}
2944
2945
2946bool HexagonInstrInfo::producesStall(const MachineInstr *MI,
2947 MachineBasicBlock::const_instr_iterator BII) const {
2948 // There is no stall when I is not a V60 vector.
2949 if (!isV60VectorInstruction(MI))
2950 return false;
2951
2952 MachineBasicBlock::const_instr_iterator MII = BII;
2953 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
2954
2955 if (!(*MII).isBundle()) {
2956 const MachineInstr *J = &*MII;
2957 if (!isV60VectorInstruction(J))
2958 return false;
2959 else if (isVecUsableNextPacket(J, MI))
2960 return false;
2961 return true;
2962 }
2963
2964 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
2965 const MachineInstr *J = &*MII;
2966 if (producesStall(J, MI))
2967 return true;
2968 }
2969 return false;
2970}
2971
2972
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002973bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr *MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002974 unsigned PredReg) const {
2975 for (unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++) {
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002976 const MachineOperand &MO = MI->getOperand(opNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002977 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
2978 return false; // Predicate register must be explicitly defined.
2979 }
2980
2981 // Hexagon Programmer's Reference says that decbin, memw_locked, and
2982 // memd_locked cannot be used as .new as well,
2983 // but we don't seem to have these instructions defined.
2984 return MI->getOpcode() != Hexagon::A4_tlbmatch;
2985}
2986
2987
2988bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
2989 return (Opcode == Hexagon::J2_jumpt) ||
2990 (Opcode == Hexagon::J2_jumpf) ||
2991 (Opcode == Hexagon::J2_jumptnew) ||
2992 (Opcode == Hexagon::J2_jumpfnew) ||
2993 (Opcode == Hexagon::J2_jumptnewpt) ||
2994 (Opcode == Hexagon::J2_jumpfnewpt);
2995}
2996
2997
2998bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
2999 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
3000 return false;
3001 return !isPredicatedTrue(Cond[0].getImm());
3002}
3003
3004
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003005short HexagonInstrInfo::getAbsoluteForm(const MachineInstr *MI) const {
3006 return Hexagon::getAbsoluteForm(MI->getOpcode());
3007}
3008
3009
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003010unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
3011 const uint64_t F = MI->getDesc().TSFlags;
3012 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3013}
3014
3015
3016// Returns the base register in a memory access (load/store). The offset is
3017// returned in Offset and the access size is returned in AccessSize.
3018unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr *MI,
3019 int &Offset, unsigned &AccessSize) const {
3020 // Return if it is not a base+offset type instruction or a MemOp.
3021 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3022 getAddrMode(MI) != HexagonII::BaseLongOffset &&
3023 !isMemOp(MI) && !isPostIncrement(MI))
3024 return 0;
3025
3026 // Since it is a memory access instruction, getMemAccessSize() should never
3027 // return 0.
3028 assert (getMemAccessSize(MI) &&
3029 "BaseImmOffset or BaseLongOffset or MemOp without accessSize");
3030
3031 // Return Values of getMemAccessSize() are
3032 // 0 - Checked in the assert above.
3033 // 1, 2, 3, 4 & 7, 8 - The statement below is correct for all these.
3034 // MemAccessSize is represented as 1+log2(N) where N is size in bits.
3035 AccessSize = (1U << (getMemAccessSize(MI) - 1));
3036
3037 unsigned basePos = 0, offsetPos = 0;
3038 if (!getBaseAndOffsetPosition(MI, basePos, offsetPos))
3039 return 0;
3040
3041 // Post increment updates its EA after the mem access,
3042 // so we need to treat its offset as zero.
3043 if (isPostIncrement(MI))
3044 Offset = 0;
3045 else {
3046 Offset = MI->getOperand(offsetPos).getImm();
3047 }
3048
3049 return MI->getOperand(basePos).getReg();
3050}
3051
3052
3053/// Return the position of the base and offset operands for this instruction.
3054bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr *MI,
3055 unsigned &BasePos, unsigned &OffsetPos) const {
3056 // Deal with memops first.
3057 if (isMemOp(MI)) {
3058 assert (MI->getOperand(0).isReg() && MI->getOperand(1).isImm() &&
3059 "Bad Memop.");
3060 BasePos = 0;
3061 OffsetPos = 1;
3062 } else if (MI->mayStore()) {
3063 BasePos = 0;
3064 OffsetPos = 1;
3065 } else if (MI->mayLoad()) {
3066 BasePos = 1;
3067 OffsetPos = 2;
3068 } else
3069 return false;
3070
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003071 if (isPredicated(*MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003072 BasePos++;
3073 OffsetPos++;
3074 }
3075 if (isPostIncrement(MI)) {
3076 BasePos++;
3077 OffsetPos++;
3078 }
3079
3080 if (!MI->getOperand(BasePos).isReg() || !MI->getOperand(OffsetPos).isImm())
3081 return false;
3082
3083 return true;
3084}
3085
3086
3087// Inserts branching instructions in reverse order of their occurence.
3088// e.g. jump_t t1 (i1)
3089// jump t2 (i2)
3090// Jumpers = {i2, i1}
3091SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3092 MachineBasicBlock& MBB) const {
3093 SmallVector<MachineInstr*, 2> Jumpers;
3094 // If the block has no terminators, it just falls into the block after it.
3095 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3096 if (I == MBB.instr_begin())
3097 return Jumpers;
3098
3099 // A basic block may looks like this:
3100 //
3101 // [ insn
3102 // EH_LABEL
3103 // insn
3104 // insn
3105 // insn
3106 // EH_LABEL
3107 // insn ]
3108 //
3109 // It has two succs but does not have a terminator
3110 // Don't know how to handle it.
3111 do {
3112 --I;
3113 if (I->isEHLabel())
3114 return Jumpers;
3115 } while (I != MBB.instr_begin());
3116
3117 I = MBB.instr_end();
3118 --I;
3119
3120 while (I->isDebugValue()) {
3121 if (I == MBB.instr_begin())
3122 return Jumpers;
3123 --I;
3124 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003125 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003126 return Jumpers;
3127
3128 // Get the last instruction in the block.
3129 MachineInstr *LastInst = &*I;
3130 Jumpers.push_back(LastInst);
3131 MachineInstr *SecondLastInst = nullptr;
3132 // Find one more terminator if present.
3133 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003134 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003135 if (!SecondLastInst) {
3136 SecondLastInst = &*I;
3137 Jumpers.push_back(SecondLastInst);
3138 } else // This is a third branch.
3139 return Jumpers;
3140 }
3141 if (I == MBB.instr_begin())
3142 break;
3143 --I;
3144 } while (true);
3145 return Jumpers;
3146}
3147
3148
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003149short HexagonInstrInfo::getBaseWithLongOffset(short Opcode) const {
3150 if (Opcode < 0)
3151 return -1;
3152 return Hexagon::getBaseWithLongOffset(Opcode);
3153}
3154
3155
3156short HexagonInstrInfo::getBaseWithLongOffset(const MachineInstr *MI) const {
3157 return Hexagon::getBaseWithLongOffset(MI->getOpcode());
3158}
3159
3160
3161short HexagonInstrInfo::getBaseWithRegOffset(const MachineInstr *MI) const {
3162 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
3163}
3164
3165
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003166// Returns Operand Index for the constant extended instruction.
3167unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
3168 const uint64_t F = MI->getDesc().TSFlags;
3169 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3170}
3171
3172// See if instruction could potentially be a duplex candidate.
3173// If so, return its group. Zero otherwise.
3174HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
3175 const MachineInstr *MI) const {
3176 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3177
3178 switch (MI->getOpcode()) {
3179 default:
3180 return HexagonII::HCG_None;
3181 //
3182 // Compound pairs.
3183 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3184 // "Rd16=#U6 ; jump #r9:2"
3185 // "Rd16=Rs16 ; jump #r9:2"
3186 //
3187 case Hexagon::C2_cmpeq:
3188 case Hexagon::C2_cmpgt:
3189 case Hexagon::C2_cmpgtu:
3190 DstReg = MI->getOperand(0).getReg();
3191 Src1Reg = MI->getOperand(1).getReg();
3192 Src2Reg = MI->getOperand(2).getReg();
3193 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3194 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3195 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3196 return HexagonII::HCG_A;
3197 break;
3198 case Hexagon::C2_cmpeqi:
3199 case Hexagon::C2_cmpgti:
3200 case Hexagon::C2_cmpgtui:
3201 // P0 = cmp.eq(Rs,#u2)
3202 DstReg = MI->getOperand(0).getReg();
3203 SrcReg = MI->getOperand(1).getReg();
3204 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3205 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3206 isIntRegForSubInst(SrcReg) && MI->getOperand(2).isImm() &&
3207 ((isUInt<5>(MI->getOperand(2).getImm())) ||
3208 (MI->getOperand(2).getImm() == -1)))
3209 return HexagonII::HCG_A;
3210 break;
3211 case Hexagon::A2_tfr:
3212 // Rd = Rs
3213 DstReg = MI->getOperand(0).getReg();
3214 SrcReg = MI->getOperand(1).getReg();
3215 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3216 return HexagonII::HCG_A;
3217 break;
3218 case Hexagon::A2_tfrsi:
3219 // Rd = #u6
3220 // Do not test for #u6 size since the const is getting extended
3221 // regardless and compound could be formed.
3222 DstReg = MI->getOperand(0).getReg();
3223 if (isIntRegForSubInst(DstReg))
3224 return HexagonII::HCG_A;
3225 break;
3226 case Hexagon::S2_tstbit_i:
3227 DstReg = MI->getOperand(0).getReg();
3228 Src1Reg = MI->getOperand(1).getReg();
3229 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3230 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3231 MI->getOperand(2).isImm() &&
3232 isIntRegForSubInst(Src1Reg) && (MI->getOperand(2).getImm() == 0))
3233 return HexagonII::HCG_A;
3234 break;
3235 // The fact that .new form is used pretty much guarantees
3236 // that predicate register will match. Nevertheless,
3237 // there could be some false positives without additional
3238 // checking.
3239 case Hexagon::J2_jumptnew:
3240 case Hexagon::J2_jumpfnew:
3241 case Hexagon::J2_jumptnewpt:
3242 case Hexagon::J2_jumpfnewpt:
3243 Src1Reg = MI->getOperand(0).getReg();
3244 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3245 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3246 return HexagonII::HCG_B;
3247 break;
3248 // Transfer and jump:
3249 // Rd=#U6 ; jump #r9:2
3250 // Rd=Rs ; jump #r9:2
3251 // Do not test for jump range here.
3252 case Hexagon::J2_jump:
3253 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3254 return HexagonII::HCG_C;
3255 break;
3256 }
3257
3258 return HexagonII::HCG_None;
3259}
3260
3261
3262// Returns -1 when there is no opcode found.
3263unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr *GA,
3264 const MachineInstr *GB) const {
3265 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3266 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
3267 if ((GA->getOpcode() != Hexagon::C2_cmpeqi) ||
3268 (GB->getOpcode() != Hexagon::J2_jumptnew))
3269 return -1;
3270 unsigned DestReg = GA->getOperand(0).getReg();
3271 if (!GB->readsRegister(DestReg))
3272 return -1;
3273 if (DestReg == Hexagon::P0)
3274 return Hexagon::J4_cmpeqi_tp0_jump_nt;
3275 if (DestReg == Hexagon::P1)
3276 return Hexagon::J4_cmpeqi_tp1_jump_nt;
3277 return -1;
3278}
3279
3280
3281int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3282 enum Hexagon::PredSense inPredSense;
3283 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3284 Hexagon::PredSense_true;
3285 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3286 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3287 return CondOpcode;
3288
3289 // This switch case will be removed once all the instructions have been
3290 // modified to use relation maps.
3291 switch(Opc) {
3292 case Hexagon::TFRI_f:
3293 return !invertPredicate ? Hexagon::TFRI_cPt_f :
3294 Hexagon::TFRI_cNotPt_f;
3295 }
3296
3297 llvm_unreachable("Unexpected predicable instruction");
3298}
3299
3300
3301// Return the cur value instruction for a given store.
3302int HexagonInstrInfo::getDotCurOp(const MachineInstr* MI) const {
3303 switch (MI->getOpcode()) {
3304 default: llvm_unreachable("Unknown .cur type");
3305 case Hexagon::V6_vL32b_pi:
3306 return Hexagon::V6_vL32b_cur_pi;
3307 case Hexagon::V6_vL32b_ai:
3308 return Hexagon::V6_vL32b_cur_ai;
3309 //128B
3310 case Hexagon::V6_vL32b_pi_128B:
3311 return Hexagon::V6_vL32b_cur_pi_128B;
3312 case Hexagon::V6_vL32b_ai_128B:
3313 return Hexagon::V6_vL32b_cur_ai_128B;
3314 }
3315 return 0;
3316}
3317
3318
3319
3320// The diagram below shows the steps involved in the conversion of a predicated
3321// store instruction to its .new predicated new-value form.
3322//
3323// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3324// ^ ^
3325// / \ (not OK. it will cause new-value store to be
3326// / X conditional on p0.new while R2 producer is
3327// / \ on p0)
3328// / \.
3329// p.new store p.old NV store
3330// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3331// ^ ^
3332// \ /
3333// \ /
3334// \ /
3335// p.old store
3336// [if (p0)memw(R0+#0)=R2]
3337//
3338//
3339// The following set of instructions further explains the scenario where
3340// conditional new-value store becomes invalid when promoted to .new predicate
3341// form.
3342//
3343// { 1) if (p0) r0 = add(r1, r2)
3344// 2) p0 = cmp.eq(r3, #0) }
3345//
3346// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3347// the first two instructions because in instr 1, r0 is conditional on old value
3348// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3349// is not valid for new-value stores.
3350// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3351// from the "Conditional Store" list. Because a predicated new value store
3352// would NOT be promoted to a double dot new store. See diagram below:
3353// This function returns yes for those stores that are predicated but not
3354// yet promoted to predicate dot new instructions.
3355//
3356// +---------------------+
3357// /-----| if (p0) memw(..)=r0 |---------\~
3358// || +---------------------+ ||
3359// promote || /\ /\ || promote
3360// || /||\ /||\ ||
3361// \||/ demote || \||/
3362// \/ || || \/
3363// +-------------------------+ || +-------------------------+
3364// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3365// +-------------------------+ || +-------------------------+
3366// || || ||
3367// || demote \||/
3368// promote || \/ NOT possible
3369// || || /\~
3370// \||/ || /||\~
3371// \/ || ||
3372// +-----------------------------+
3373// | if (p0.new) memw(..)=r0.new |
3374// +-----------------------------+
3375// Double Dot New Store
3376//
3377// Returns the most basic instruction for the .new predicated instructions and
3378// new-value stores.
3379// For example, all of the following instructions will be converted back to the
3380// same instruction:
3381// 1) if (p0.new) memw(R0+#0) = R1.new --->
3382// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3383// 3) if (p0.new) memw(R0+#0) = R1 --->
3384//
3385// To understand the translation of instruction 1 to its original form, consider
3386// a packet with 3 instructions.
3387// { p0 = cmp.eq(R0,R1)
3388// if (p0.new) R2 = add(R3, R4)
3389// R5 = add (R3, R1)
3390// }
3391// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3392//
3393// This instruction can be part of the previous packet only if both p0 and R2
3394// are promoted to .new values. This promotion happens in steps, first
3395// predicate register is promoted to .new and in the next iteration R2 is
3396// promoted. Therefore, in case of dependence check failure (due to R5) during
3397// next iteration, it should be converted back to its most basic form.
3398
3399
3400// Return the new value instruction for a given store.
3401int HexagonInstrInfo::getDotNewOp(const MachineInstr* MI) const {
3402 int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
3403 if (NVOpcode >= 0) // Valid new-value store instruction.
3404 return NVOpcode;
3405
3406 switch (MI->getOpcode()) {
3407 default: llvm_unreachable("Unknown .new type");
3408 case Hexagon::S4_storerb_ur:
3409 return Hexagon::S4_storerbnew_ur;
3410
3411 case Hexagon::S2_storerb_pci:
3412 return Hexagon::S2_storerb_pci;
3413
3414 case Hexagon::S2_storeri_pci:
3415 return Hexagon::S2_storeri_pci;
3416
3417 case Hexagon::S2_storerh_pci:
3418 return Hexagon::S2_storerh_pci;
3419
3420 case Hexagon::S2_storerd_pci:
3421 return Hexagon::S2_storerd_pci;
3422
3423 case Hexagon::S2_storerf_pci:
3424 return Hexagon::S2_storerf_pci;
3425
3426 case Hexagon::V6_vS32b_ai:
3427 return Hexagon::V6_vS32b_new_ai;
3428
3429 case Hexagon::V6_vS32b_pi:
3430 return Hexagon::V6_vS32b_new_pi;
3431
3432 // 128B
3433 case Hexagon::V6_vS32b_ai_128B:
3434 return Hexagon::V6_vS32b_new_ai_128B;
3435
3436 case Hexagon::V6_vS32b_pi_128B:
3437 return Hexagon::V6_vS32b_new_pi_128B;
3438 }
3439 return 0;
3440}
3441
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00003442
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003443// Returns the opcode to use when converting MI, which is a conditional jump,
3444// into a conditional instruction which uses the .new value of the predicate.
3445// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003446int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr *MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003447 const MachineBranchProbabilityInfo *MBPI) const {
3448 // We assume that block can have at most two successors.
3449 bool taken = false;
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003450 const MachineBasicBlock *Src = MI->getParent();
3451 const MachineOperand *BrTarget = &MI->getOperand(1);
3452 const MachineBasicBlock *Dst = BrTarget->getMBB();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003453
3454 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
3455 if (Prediction >= BranchProbability(1,2))
3456 taken = true;
3457
3458 switch (MI->getOpcode()) {
3459 case Hexagon::J2_jumpt:
3460 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3461 case Hexagon::J2_jumpf:
3462 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3463
3464 default:
3465 llvm_unreachable("Unexpected jump instruction.");
3466 }
3467}
3468
3469
3470// Return .new predicate version for an instruction.
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003471int HexagonInstrInfo::getDotNewPredOp(const MachineInstr *MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003472 const MachineBranchProbabilityInfo *MBPI) const {
3473 int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
3474 if (NewOpcode >= 0) // Valid predicate new instruction
3475 return NewOpcode;
3476
3477 switch (MI->getOpcode()) {
3478 // Condtional Jumps
3479 case Hexagon::J2_jumpt:
3480 case Hexagon::J2_jumpf:
3481 return getDotNewPredJumpOp(MI, MBPI);
3482
3483 default:
3484 assert(0 && "Unknown .new type");
3485 }
3486 return 0;
3487}
3488
3489
3490int HexagonInstrInfo::getDotOldOp(const int opc) const {
3491 int NewOp = opc;
3492 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3493 NewOp = Hexagon::getPredOldOpcode(NewOp);
3494 assert(NewOp >= 0 &&
3495 "Couldn't change predicate new instruction to its old form.");
3496 }
3497
3498 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3499 NewOp = Hexagon::getNonNVStore(NewOp);
3500 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3501 }
3502 return NewOp;
3503}
3504
3505
3506// See if instruction could potentially be a duplex candidate.
3507// If so, return its group. Zero otherwise.
3508HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
3509 const MachineInstr *MI) const {
3510 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3511 auto &HRI = getRegisterInfo();
3512
3513 switch (MI->getOpcode()) {
3514 default:
3515 return HexagonII::HSIG_None;
3516 //
3517 // Group L1:
3518 //
3519 // Rd = memw(Rs+#u4:2)
3520 // Rd = memub(Rs+#u4:0)
3521 case Hexagon::L2_loadri_io:
3522 DstReg = MI->getOperand(0).getReg();
3523 SrcReg = MI->getOperand(1).getReg();
3524 // Special case this one from Group L2.
3525 // Rd = memw(r29+#u5:2)
3526 if (isIntRegForSubInst(DstReg)) {
3527 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3528 HRI.getStackRegister() == SrcReg &&
3529 MI->getOperand(2).isImm() &&
3530 isShiftedUInt<5,2>(MI->getOperand(2).getImm()))
3531 return HexagonII::HSIG_L2;
3532 // Rd = memw(Rs+#u4:2)
3533 if (isIntRegForSubInst(SrcReg) &&
3534 (MI->getOperand(2).isImm() &&
3535 isShiftedUInt<4,2>(MI->getOperand(2).getImm())))
3536 return HexagonII::HSIG_L1;
3537 }
3538 break;
3539 case Hexagon::L2_loadrub_io:
3540 // Rd = memub(Rs+#u4:0)
3541 DstReg = MI->getOperand(0).getReg();
3542 SrcReg = MI->getOperand(1).getReg();
3543 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3544 MI->getOperand(2).isImm() && isUInt<4>(MI->getOperand(2).getImm()))
3545 return HexagonII::HSIG_L1;
3546 break;
3547 //
3548 // Group L2:
3549 //
3550 // Rd = memh/memuh(Rs+#u3:1)
3551 // Rd = memb(Rs+#u3:0)
3552 // Rd = memw(r29+#u5:2) - Handled above.
3553 // Rdd = memd(r29+#u5:3)
3554 // deallocframe
3555 // [if ([!]p0[.new])] dealloc_return
3556 // [if ([!]p0[.new])] jumpr r31
3557 case Hexagon::L2_loadrh_io:
3558 case Hexagon::L2_loadruh_io:
3559 // Rd = memh/memuh(Rs+#u3:1)
3560 DstReg = MI->getOperand(0).getReg();
3561 SrcReg = MI->getOperand(1).getReg();
3562 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3563 MI->getOperand(2).isImm() &&
3564 isShiftedUInt<3,1>(MI->getOperand(2).getImm()))
3565 return HexagonII::HSIG_L2;
3566 break;
3567 case Hexagon::L2_loadrb_io:
3568 // Rd = memb(Rs+#u3:0)
3569 DstReg = MI->getOperand(0).getReg();
3570 SrcReg = MI->getOperand(1).getReg();
3571 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3572 MI->getOperand(2).isImm() &&
3573 isUInt<3>(MI->getOperand(2).getImm()))
3574 return HexagonII::HSIG_L2;
3575 break;
3576 case Hexagon::L2_loadrd_io:
3577 // Rdd = memd(r29+#u5:3)
3578 DstReg = MI->getOperand(0).getReg();
3579 SrcReg = MI->getOperand(1).getReg();
3580 if (isDblRegForSubInst(DstReg, HRI) &&
3581 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3582 HRI.getStackRegister() == SrcReg &&
3583 MI->getOperand(2).isImm() &&
3584 isShiftedUInt<5,3>(MI->getOperand(2).getImm()))
3585 return HexagonII::HSIG_L2;
3586 break;
3587 // dealloc_return is not documented in Hexagon Manual, but marked
3588 // with A_SUBINSN attribute in iset_v4classic.py.
3589 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3590 case Hexagon::L4_return:
3591 case Hexagon::L2_deallocframe:
3592 return HexagonII::HSIG_L2;
3593 case Hexagon::EH_RETURN_JMPR:
3594 case Hexagon::JMPret :
3595 // jumpr r31
3596 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
3597 DstReg = MI->getOperand(0).getReg();
3598 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3599 return HexagonII::HSIG_L2;
3600 break;
3601 case Hexagon::JMPrett:
3602 case Hexagon::JMPretf:
3603 case Hexagon::JMPrettnewpt:
3604 case Hexagon::JMPretfnewpt :
3605 case Hexagon::JMPrettnew :
3606 case Hexagon::JMPretfnew :
3607 DstReg = MI->getOperand(1).getReg();
3608 SrcReg = MI->getOperand(0).getReg();
3609 // [if ([!]p0[.new])] jumpr r31
3610 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3611 (Hexagon::P0 == SrcReg)) &&
3612 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3613 return HexagonII::HSIG_L2;
3614 break;
3615 case Hexagon::L4_return_t :
3616 case Hexagon::L4_return_f :
3617 case Hexagon::L4_return_tnew_pnt :
3618 case Hexagon::L4_return_fnew_pnt :
3619 case Hexagon::L4_return_tnew_pt :
3620 case Hexagon::L4_return_fnew_pt :
3621 // [if ([!]p0[.new])] dealloc_return
3622 SrcReg = MI->getOperand(0).getReg();
3623 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3624 return HexagonII::HSIG_L2;
3625 break;
3626 //
3627 // Group S1:
3628 //
3629 // memw(Rs+#u4:2) = Rt
3630 // memb(Rs+#u4:0) = Rt
3631 case Hexagon::S2_storeri_io:
3632 // Special case this one from Group S2.
3633 // memw(r29+#u5:2) = Rt
3634 Src1Reg = MI->getOperand(0).getReg();
3635 Src2Reg = MI->getOperand(2).getReg();
3636 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3637 isIntRegForSubInst(Src2Reg) &&
3638 HRI.getStackRegister() == Src1Reg && MI->getOperand(1).isImm() &&
3639 isShiftedUInt<5,2>(MI->getOperand(1).getImm()))
3640 return HexagonII::HSIG_S2;
3641 // memw(Rs+#u4:2) = Rt
3642 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3643 MI->getOperand(1).isImm() &&
3644 isShiftedUInt<4,2>(MI->getOperand(1).getImm()))
3645 return HexagonII::HSIG_S1;
3646 break;
3647 case Hexagon::S2_storerb_io:
3648 // memb(Rs+#u4:0) = Rt
3649 Src1Reg = MI->getOperand(0).getReg();
3650 Src2Reg = MI->getOperand(2).getReg();
3651 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3652 MI->getOperand(1).isImm() && isUInt<4>(MI->getOperand(1).getImm()))
3653 return HexagonII::HSIG_S1;
3654 break;
3655 //
3656 // Group S2:
3657 //
3658 // memh(Rs+#u3:1) = Rt
3659 // memw(r29+#u5:2) = Rt
3660 // memd(r29+#s6:3) = Rtt
3661 // memw(Rs+#u4:2) = #U1
3662 // memb(Rs+#u4) = #U1
3663 // allocframe(#u5:3)
3664 case Hexagon::S2_storerh_io:
3665 // memh(Rs+#u3:1) = Rt
3666 Src1Reg = MI->getOperand(0).getReg();
3667 Src2Reg = MI->getOperand(2).getReg();
3668 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3669 MI->getOperand(1).isImm() &&
3670 isShiftedUInt<3,1>(MI->getOperand(1).getImm()))
3671 return HexagonII::HSIG_S1;
3672 break;
3673 case Hexagon::S2_storerd_io:
3674 // memd(r29+#s6:3) = Rtt
3675 Src1Reg = MI->getOperand(0).getReg();
3676 Src2Reg = MI->getOperand(2).getReg();
3677 if (isDblRegForSubInst(Src2Reg, HRI) &&
3678 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3679 HRI.getStackRegister() == Src1Reg && MI->getOperand(1).isImm() &&
3680 isShiftedInt<6,3>(MI->getOperand(1).getImm()))
3681 return HexagonII::HSIG_S2;
3682 break;
3683 case Hexagon::S4_storeiri_io:
3684 // memw(Rs+#u4:2) = #U1
3685 Src1Reg = MI->getOperand(0).getReg();
3686 if (isIntRegForSubInst(Src1Reg) && MI->getOperand(1).isImm() &&
3687 isShiftedUInt<4,2>(MI->getOperand(1).getImm()) &&
3688 MI->getOperand(2).isImm() && isUInt<1>(MI->getOperand(2).getImm()))
3689 return HexagonII::HSIG_S2;
3690 break;
3691 case Hexagon::S4_storeirb_io:
3692 // memb(Rs+#u4) = #U1
3693 Src1Reg = MI->getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003694 if (isIntRegForSubInst(Src1Reg) &&
3695 MI->getOperand(1).isImm() && isUInt<4>(MI->getOperand(1).getImm()) &&
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003696 MI->getOperand(2).isImm() && isUInt<1>(MI->getOperand(2).getImm()))
3697 return HexagonII::HSIG_S2;
3698 break;
3699 case Hexagon::S2_allocframe:
3700 if (MI->getOperand(0).isImm() &&
3701 isShiftedUInt<5,3>(MI->getOperand(0).getImm()))
3702 return HexagonII::HSIG_S1;
3703 break;
3704 //
3705 // Group A:
3706 //
3707 // Rx = add(Rx,#s7)
3708 // Rd = Rs
3709 // Rd = #u6
3710 // Rd = #-1
3711 // if ([!]P0[.new]) Rd = #0
3712 // Rd = add(r29,#u6:2)
3713 // Rx = add(Rx,Rs)
3714 // P0 = cmp.eq(Rs,#u2)
3715 // Rdd = combine(#0,Rs)
3716 // Rdd = combine(Rs,#0)
3717 // Rdd = combine(#u2,#U2)
3718 // Rd = add(Rs,#1)
3719 // Rd = add(Rs,#-1)
3720 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3721 // Rd = and(Rs,#1)
3722 case Hexagon::A2_addi:
3723 DstReg = MI->getOperand(0).getReg();
3724 SrcReg = MI->getOperand(1).getReg();
3725 if (isIntRegForSubInst(DstReg)) {
3726 // Rd = add(r29,#u6:2)
3727 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3728 HRI.getStackRegister() == SrcReg && MI->getOperand(2).isImm() &&
3729 isShiftedUInt<6,2>(MI->getOperand(2).getImm()))
3730 return HexagonII::HSIG_A;
3731 // Rx = add(Rx,#s7)
3732 if ((DstReg == SrcReg) && MI->getOperand(2).isImm() &&
3733 isInt<7>(MI->getOperand(2).getImm()))
3734 return HexagonII::HSIG_A;
3735 // Rd = add(Rs,#1)
3736 // Rd = add(Rs,#-1)
3737 if (isIntRegForSubInst(SrcReg) && MI->getOperand(2).isImm() &&
3738 ((MI->getOperand(2).getImm() == 1) ||
3739 (MI->getOperand(2).getImm() == -1)))
3740 return HexagonII::HSIG_A;
3741 }
3742 break;
3743 case Hexagon::A2_add:
3744 // Rx = add(Rx,Rs)
3745 DstReg = MI->getOperand(0).getReg();
3746 Src1Reg = MI->getOperand(1).getReg();
3747 Src2Reg = MI->getOperand(2).getReg();
3748 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3749 isIntRegForSubInst(Src2Reg))
3750 return HexagonII::HSIG_A;
3751 break;
3752 case Hexagon::A2_andir:
3753 // Same as zxtb.
3754 // Rd16=and(Rs16,#255)
3755 // Rd16=and(Rs16,#1)
3756 DstReg = MI->getOperand(0).getReg();
3757 SrcReg = MI->getOperand(1).getReg();
3758 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3759 MI->getOperand(2).isImm() &&
3760 ((MI->getOperand(2).getImm() == 1) ||
3761 (MI->getOperand(2).getImm() == 255)))
3762 return HexagonII::HSIG_A;
3763 break;
3764 case Hexagon::A2_tfr:
3765 // Rd = Rs
3766 DstReg = MI->getOperand(0).getReg();
3767 SrcReg = MI->getOperand(1).getReg();
3768 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3769 return HexagonII::HSIG_A;
3770 break;
3771 case Hexagon::A2_tfrsi:
3772 // Rd = #u6
3773 // Do not test for #u6 size since the const is getting extended
3774 // regardless and compound could be formed.
3775 // Rd = #-1
3776 DstReg = MI->getOperand(0).getReg();
3777 if (isIntRegForSubInst(DstReg))
3778 return HexagonII::HSIG_A;
3779 break;
3780 case Hexagon::C2_cmoveit:
3781 case Hexagon::C2_cmovenewit:
3782 case Hexagon::C2_cmoveif:
3783 case Hexagon::C2_cmovenewif:
3784 // if ([!]P0[.new]) Rd = #0
3785 // Actual form:
3786 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
3787 DstReg = MI->getOperand(0).getReg();
3788 SrcReg = MI->getOperand(1).getReg();
3789 if (isIntRegForSubInst(DstReg) &&
3790 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
3791 MI->getOperand(2).isImm() && MI->getOperand(2).getImm() == 0)
3792 return HexagonII::HSIG_A;
3793 break;
3794 case Hexagon::C2_cmpeqi:
3795 // P0 = cmp.eq(Rs,#u2)
3796 DstReg = MI->getOperand(0).getReg();
3797 SrcReg = MI->getOperand(1).getReg();
3798 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3799 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
3800 MI->getOperand(2).isImm() && isUInt<2>(MI->getOperand(2).getImm()))
3801 return HexagonII::HSIG_A;
3802 break;
3803 case Hexagon::A2_combineii:
3804 case Hexagon::A4_combineii:
3805 // Rdd = combine(#u2,#U2)
3806 DstReg = MI->getOperand(0).getReg();
3807 if (isDblRegForSubInst(DstReg, HRI) &&
3808 ((MI->getOperand(1).isImm() && isUInt<2>(MI->getOperand(1).getImm())) ||
3809 (MI->getOperand(1).isGlobal() &&
3810 isUInt<2>(MI->getOperand(1).getOffset()))) &&
3811 ((MI->getOperand(2).isImm() && isUInt<2>(MI->getOperand(2).getImm())) ||
3812 (MI->getOperand(2).isGlobal() &&
3813 isUInt<2>(MI->getOperand(2).getOffset()))))
3814 return HexagonII::HSIG_A;
3815 break;
3816 case Hexagon::A4_combineri:
3817 // Rdd = combine(Rs,#0)
3818 DstReg = MI->getOperand(0).getReg();
3819 SrcReg = MI->getOperand(1).getReg();
3820 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
3821 ((MI->getOperand(2).isImm() && MI->getOperand(2).getImm() == 0) ||
3822 (MI->getOperand(2).isGlobal() && MI->getOperand(2).getOffset() == 0)))
3823 return HexagonII::HSIG_A;
3824 break;
3825 case Hexagon::A4_combineir:
3826 // Rdd = combine(#0,Rs)
3827 DstReg = MI->getOperand(0).getReg();
3828 SrcReg = MI->getOperand(2).getReg();
3829 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
3830 ((MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0) ||
3831 (MI->getOperand(1).isGlobal() && MI->getOperand(1).getOffset() == 0)))
3832 return HexagonII::HSIG_A;
3833 break;
3834 case Hexagon::A2_sxtb:
3835 case Hexagon::A2_sxth:
3836 case Hexagon::A2_zxtb:
3837 case Hexagon::A2_zxth:
3838 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3839 DstReg = MI->getOperand(0).getReg();
3840 SrcReg = MI->getOperand(1).getReg();
3841 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3842 return HexagonII::HSIG_A;
3843 break;
3844 }
3845
3846 return HexagonII::HSIG_None;
3847}
3848
3849
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003850short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003851 return Hexagon::getRealHWInstr(MI->getOpcode(), Hexagon::InstrType_Real);
3852}
3853
3854
3855// Return first non-debug instruction in the basic block.
3856MachineInstr *HexagonInstrInfo::getFirstNonDbgInst(MachineBasicBlock *BB)
3857 const {
3858 for (auto MII = BB->instr_begin(), End = BB->instr_end(); MII != End; MII++) {
3859 MachineInstr *MI = &*MII;
3860 if (MI->isDebugValue())
3861 continue;
3862 return MI;
3863 }
3864 return nullptr;
3865}
3866
3867
3868unsigned HexagonInstrInfo::getInstrTimingClassLatency(
3869 const InstrItineraryData *ItinData, const MachineInstr *MI) const {
3870 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3871 // still have a MinLatency property, which getStageLatency checks.
3872 if (!ItinData)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003873 return getInstrLatency(ItinData, *MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003874
3875 // Get the latency embedded in the itinerary. If we're not using timing class
3876 // latencies or if we using BSB scheduling, then restrict the maximum latency
3877 // to 1 (that is, either 0 or 1).
3878 if (MI->isTransient())
3879 return 0;
3880 unsigned Latency = ItinData->getStageLatency(MI->getDesc().getSchedClass());
3881 if (!EnableTimingClassLatency ||
3882 MI->getParent()->getParent()->getSubtarget<HexagonSubtarget>().
3883 useBSBScheduling())
3884 if (Latency > 1)
3885 Latency = 1;
3886 return Latency;
3887}
3888
3889
3890// inverts the predication logic.
3891// p -> NotP
3892// NotP -> P
3893bool HexagonInstrInfo::getInvertedPredSense(
3894 SmallVectorImpl<MachineOperand> &Cond) const {
3895 if (Cond.empty())
3896 return false;
3897 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
3898 Cond[0].setImm(Opc);
3899 return true;
3900}
3901
3902
3903unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
3904 int InvPredOpcode;
3905 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
3906 : Hexagon::getTruePredOpcode(Opc);
3907 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
3908 return InvPredOpcode;
3909
3910 llvm_unreachable("Unexpected predicated instruction");
3911}
3912
3913
3914// Returns the max value that doesn't need to be extended.
3915int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
3916 const uint64_t F = MI->getDesc().TSFlags;
3917 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3918 & HexagonII::ExtentSignedMask;
3919 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3920 & HexagonII::ExtentBitsMask;
3921
3922 if (isSigned) // if value is signed
3923 return ~(-1U << (bits - 1));
3924 else
3925 return ~(-1U << bits);
3926}
3927
3928
3929unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr* MI) const {
3930 const uint64_t F = MI->getDesc().TSFlags;
3931 return (F >> HexagonII::MemAccessSizePos) & HexagonII::MemAccesSizeMask;
3932}
3933
3934
3935// Returns the min value that doesn't need to be extended.
3936int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
3937 const uint64_t F = MI->getDesc().TSFlags;
3938 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3939 & HexagonII::ExtentSignedMask;
3940 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3941 & HexagonII::ExtentBitsMask;
3942
3943 if (isSigned) // if value is signed
3944 return -1U << (bits - 1);
3945 else
3946 return 0;
3947}
3948
3949
3950// Returns opcode of the non-extended equivalent instruction.
3951short HexagonInstrInfo::getNonExtOpcode(const MachineInstr *MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00003952 // Check if the instruction has a register form that uses register in place
3953 // of the extended operand, if so return that as the non-extended form.
3954 short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
3955 if (NonExtOpcode >= 0)
3956 return NonExtOpcode;
3957
3958 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00003959 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00003960 switch (getAddrMode(MI)) {
3961 case HexagonII::Absolute :
Krzysztof Parzyszek02579052015-10-20 19:21:05 +00003962 return Hexagon::getBaseWithImmOffset(MI->getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00003963 case HexagonII::BaseImmOffset :
3964 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003965 case HexagonII::BaseLongOffset:
3966 return Hexagon::getRegShlForm(MI->getOpcode());
3967
Jyotsna Verma84256432013-03-01 17:37:13 +00003968 default:
3969 return -1;
3970 }
3971 }
3972 return -1;
3973}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00003974
Brendon Cahoondf43e682015-05-08 16:16:29 +00003975
Ahmed Bougachac88bf542015-06-11 19:30:37 +00003976bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003977 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00003978 if (Cond.empty())
3979 return false;
3980 assert(Cond.size() == 2);
3981 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
3982 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
3983 return false;
3984 }
3985 PredReg = Cond[1].getReg();
3986 PredRegPos = 1;
3987 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
3988 PredRegFlags = 0;
3989 if (Cond[1].isImplicit())
3990 PredRegFlags = RegState::Implicit;
3991 if (Cond[1].isUndef())
3992 PredRegFlags |= RegState::Undef;
3993 return true;
3994}
3995
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003996
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003997short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003998 return Hexagon::getRealHWInstr(MI->getOpcode(), Hexagon::InstrType_Pseudo);
3999}
4000
4001
4002short HexagonInstrInfo::getRegForm(const MachineInstr *MI) const {
4003 return Hexagon::getRegForm(MI->getOpcode());
4004}
4005
4006
4007// Return the number of bytes required to encode the instruction.
4008// Hexagon instructions are fixed length, 4 bytes, unless they
4009// use a constant extender, which requires another 4 bytes.
4010// For debug instructions and prolog labels, return 0.
4011unsigned HexagonInstrInfo::getSize(const MachineInstr *MI) const {
4012 if (MI->isDebugValue() || MI->isPosition())
4013 return 0;
4014
4015 unsigned Size = MI->getDesc().getSize();
4016 if (!Size)
4017 // Assume the default insn size in case it cannot be determined
4018 // for whatever reason.
4019 Size = HEXAGON_INSTR_SIZE;
4020
4021 if (isConstExtended(MI) || isExtended(MI))
4022 Size += HEXAGON_INSTR_SIZE;
4023
4024 // Try and compute number of instructions in asm.
4025 if (BranchRelaxAsmLarge && MI->getOpcode() == Hexagon::INLINEASM) {
4026 const MachineBasicBlock &MBB = *MI->getParent();
4027 const MachineFunction *MF = MBB.getParent();
4028 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4029
4030 // Count the number of register definitions to find the asm string.
4031 unsigned NumDefs = 0;
4032 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
4033 ++NumDefs)
4034 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
4035
4036 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
4037 // Disassemble the AsmStr and approximate number of instructions.
4038 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
4039 Size = getInlineAsmLength(AsmStr, *MAI);
4040 }
4041
4042 return Size;
4043}
4044
4045
4046uint64_t HexagonInstrInfo::getType(const MachineInstr* MI) const {
4047 const uint64_t F = MI->getDesc().TSFlags;
4048 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4049}
4050
4051
4052unsigned HexagonInstrInfo::getUnits(const MachineInstr* MI) const {
4053 const TargetSubtargetInfo &ST = MI->getParent()->getParent()->getSubtarget();
4054 const InstrItineraryData &II = *ST.getInstrItineraryData();
4055 const InstrStage &IS = *II.beginStage(MI->getDesc().getSchedClass());
4056
4057 return IS.getUnits();
4058}
4059
4060
4061unsigned HexagonInstrInfo::getValidSubTargets(const unsigned Opcode) const {
4062 const uint64_t F = get(Opcode).TSFlags;
4063 return (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask;
4064}
4065
4066
4067// Calculate size of the basic block without debug instructions.
4068unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4069 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4070}
4071
4072
4073unsigned HexagonInstrInfo::nonDbgBundleSize(
4074 MachineBasicBlock::const_iterator BundleHead) const {
4075 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004076 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004077 // Skip the bundle header.
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00004078 return nonDbgMICount(++MII, getBundleEnd(*BundleHead));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004079}
4080
4081
4082/// immediateExtend - Changes the instruction in place to one using an immediate
4083/// extender.
4084void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
4085 assert((isExtendable(MI)||isConstExtended(MI)) &&
4086 "Instruction must be extendable");
4087 // Find which operand is extendable.
4088 short ExtOpNum = getCExtOpNum(MI);
4089 MachineOperand &MO = MI->getOperand(ExtOpNum);
4090 // This needs to be something we understand.
4091 assert((MO.isMBB() || MO.isImm()) &&
4092 "Branch with unknown extendable field type");
4093 // Mark given operand as extended.
4094 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4095}
4096
4097
4098bool HexagonInstrInfo::invertAndChangeJumpTarget(
4099 MachineInstr* MI, MachineBasicBlock* NewTarget) const {
4100 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
4101 << NewTarget->getNumber(); MI->dump(););
4102 assert(MI->isBranch());
4103 unsigned NewOpcode = getInvertedPredicatedOpcode(MI->getOpcode());
4104 int TargetPos = MI->getNumOperands() - 1;
4105 // In general branch target is the last operand,
4106 // but some implicit defs added at the end might change it.
4107 while ((TargetPos > -1) && !MI->getOperand(TargetPos).isMBB())
4108 --TargetPos;
4109 assert((TargetPos >= 0) && MI->getOperand(TargetPos).isMBB());
4110 MI->getOperand(TargetPos).setMBB(NewTarget);
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004111 if (EnableBranchPrediction && isPredicatedNew(*MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004112 NewOpcode = reversePrediction(NewOpcode);
4113 }
4114 MI->setDesc(get(NewOpcode));
4115 return true;
4116}
4117
4118
4119void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4120 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4121 MachineFunction::iterator A = MF.begin();
4122 MachineBasicBlock &B = *A;
4123 MachineBasicBlock::iterator I = B.begin();
4124 MachineInstr *MI = &*I;
4125 DebugLoc DL = MI->getDebugLoc();
4126 MachineInstr *NewMI;
4127
4128 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4129 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
4130 NewMI = BuildMI(B, MI, DL, get(insn));
4131 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
4132 " Class: " << NewMI->getDesc().getSchedClass());
4133 NewMI->eraseFromParent();
4134 }
4135 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4136}
4137
4138
4139// inverts the predication logic.
4140// p -> NotP
4141// NotP -> P
4142bool HexagonInstrInfo::reversePredSense(MachineInstr* MI) const {
4143 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI->dump());
4144 MI->setDesc(get(getInvertedPredicatedOpcode(MI->getOpcode())));
4145 return true;
4146}
4147
4148
4149// Reverse the branch prediction.
4150unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4151 int PredRevOpcode = -1;
4152 if (isPredictedTaken(Opcode))
4153 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4154 else
4155 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4156 assert(PredRevOpcode > 0);
4157 return PredRevOpcode;
4158}
4159
4160
4161// TODO: Add more rigorous validation.
4162bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4163 const {
4164 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4165}
4166
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004167
4168short HexagonInstrInfo::xformRegToImmOffset(const MachineInstr *MI) const {
4169 return Hexagon::xformRegToImmOffset(MI->getOpcode());
4170}