blob: 9f477542f11a294333b9a48f13b28fcd4f4c0d02 [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.
234unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
235 int &FrameIndex) const {
236 switch (MI->getOpcode()) {
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000237 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();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000266 return MI->getOperand(0).getReg();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000267 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000268
269 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.
301unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
302 int &FrameIndex) const {
303 switch (MI->getOpcode()) {
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000304 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();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000331 return MI->getOperand(2).getReg();
332 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000333
334 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 Parzyszekcfe285e2013-02-11 20:04:29 +0000564
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000565unsigned HexagonInstrInfo::InsertBranch(MachineBasicBlock &MBB,
566 MachineBasicBlock *TBB, MachineBasicBlock *FBB,
567 ArrayRef<MachineOperand> Cond, DebugLoc DL) const {
568 unsigned BOpc = Hexagon::J2_jump;
569 unsigned BccOpc = Hexagon::J2_jumpt;
570 assert(validateBranchCond(Cond) && "Invalid branching condition");
571 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
572
573 // Check if ReverseBranchCondition has asked to reverse this branch
574 // If we want to reverse the branch an odd number of times, we want
575 // J2_jumpf.
576 if (!Cond.empty() && Cond[0].isImm())
577 BccOpc = Cond[0].getImm();
578
579 if (!FBB) {
580 if (Cond.empty()) {
581 // Due to a bug in TailMerging/CFG Optimization, we need to add a
582 // special case handling of a predicated jump followed by an
583 // unconditional jump. If not, Tail Merging and CFG Optimization go
584 // into an infinite loop.
585 MachineBasicBlock *NewTBB, *NewFBB;
586 SmallVector<MachineOperand, 4> Cond;
587 MachineInstr *Term = MBB.getFirstTerminator();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000588 if (Term != MBB.end() && isPredicated(*Term) &&
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000589 !AnalyzeBranch(MBB, NewTBB, NewFBB, Cond, false)) {
590 MachineBasicBlock *NextBB = &*++MBB.getIterator();
591 if (NewTBB == NextBB) {
592 ReverseBranchCondition(Cond);
593 RemoveBranch(MBB);
594 return InsertBranch(MBB, TBB, nullptr, Cond, DL);
595 }
596 }
597 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
598 } else if (isEndLoopN(Cond[0].getImm())) {
599 int EndLoopOp = Cond[0].getImm();
600 assert(Cond[1].isMBB());
601 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
602 // Check for it, and change the BB target if needed.
603 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
604 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
605 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
606 Loop->getOperand(0).setMBB(TBB);
607 // Add the ENDLOOP after the finding the LOOP0.
608 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
609 } else if (isNewValueJump(Cond[0].getImm())) {
610 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
611 // New value jump
612 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
613 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
614 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
615 DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
616 if (Cond[2].isReg()) {
617 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
618 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
619 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
620 } else if(Cond[2].isImm()) {
621 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
622 addImm(Cond[2].getImm()).addMBB(TBB);
623 } else
624 llvm_unreachable("Invalid condition for branching");
625 } else {
626 assert((Cond.size() == 2) && "Malformed cond vector");
627 const MachineOperand &RO = Cond[1];
628 unsigned Flags = getUndefRegState(RO.isUndef());
629 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
630 }
631 return 1;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000632 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000633 assert((!Cond.empty()) &&
634 "Cond. cannot be empty when multiple branchings are required");
635 assert((!isNewValueJump(Cond[0].getImm())) &&
636 "NV-jump cannot be inserted with another branch");
637 // Special case for hardware loops. The condition is a basic block.
638 if (isEndLoopN(Cond[0].getImm())) {
639 int EndLoopOp = Cond[0].getImm();
640 assert(Cond[1].isMBB());
641 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
642 // Check for it, and change the BB target if needed.
643 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
644 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
645 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
646 Loop->getOperand(0).setMBB(TBB);
647 // Add the ENDLOOP after the finding the LOOP0.
648 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
649 } else {
650 const MachineOperand &RO = Cond[1];
651 unsigned Flags = getUndefRegState(RO.isUndef());
652 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000653 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000654 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000655
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000656 return 2;
657}
658
659
660bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
661 unsigned NumCycles, unsigned ExtraPredCycles,
662 BranchProbability Probability) const {
663 return nonDbgBBSize(&MBB) <= 3;
664}
665
666
667bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
668 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
669 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
670 const {
671 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
672}
673
674
675bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
676 unsigned NumInstrs, BranchProbability Probability) const {
677 return NumInstrs <= 4;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000678}
679
680
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000681void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000682 MachineBasicBlock::iterator I, DebugLoc DL, unsigned DestReg,
683 unsigned SrcReg, bool KillSrc) const {
684 auto &HRI = getRegisterInfo();
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000685 unsigned KillFlag = getKillRegState(KillSrc);
686
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000687 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000688 auto MIB = BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg)
689 .addReg(SrcReg, KillFlag);
690 // We could have a R12 = COPY R2, D1<imp-use, kill> instruction.
691 // Transfer the kill flags.
692 for (auto &Op : I->operands())
693 if (Op.isReg() && Op.isKill() && Op.isImplicit() && Op.isUse())
694 MIB.addReg(Op.getReg(), RegState::Kill | RegState::Implicit);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000695 return;
696 }
697 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000698 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg)
699 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000700 return;
701 }
702 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
703 // Map Pd = Ps to Pd = or(Ps, Ps).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000704 BuildMI(MBB, I, DL, get(Hexagon::C2_or), DestReg)
705 .addReg(SrcReg).addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000706 return;
707 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000708 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000709 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000710 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
711 .addReg(SrcReg, KillFlag);
712 return;
713 }
714 if (Hexagon::IntRegsRegClass.contains(DestReg) &&
715 Hexagon::CtrRegsRegClass.contains(SrcReg)) {
716 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrcrr), DestReg)
717 .addReg(SrcReg, KillFlag);
718 return;
719 }
720 if (Hexagon::ModRegsRegClass.contains(DestReg) &&
721 Hexagon::IntRegsRegClass.contains(SrcReg)) {
722 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
723 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000724 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000725 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000726 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
727 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000728 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
729 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000730 return;
731 }
732 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
733 Hexagon::PredRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000734 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg)
735 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000736 return;
737 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000738 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
739 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000740 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
741 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000742 return;
743 }
744 if (Hexagon::VectorRegsRegClass.contains(SrcReg, DestReg)) {
745 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000746 addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000747 return;
748 }
749 if (Hexagon::VecDblRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000750 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
751 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg), KillFlag)
752 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg), KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000753 return;
754 }
755 if (Hexagon::VecPredRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000756 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
757 .addReg(SrcReg)
758 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000759 return;
760 }
761 if (Hexagon::VecPredRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000762 Hexagon::VectorRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000763 llvm_unreachable("Unimplemented pred to vec");
764 return;
765 }
766 if (Hexagon::VecPredRegsRegClass.contains(DestReg) &&
767 Hexagon::VectorRegsRegClass.contains(SrcReg)) {
768 llvm_unreachable("Unimplemented vec to pred");
769 return;
770 }
771 if (Hexagon::VecPredRegs128BRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000772 unsigned DstHi = HRI.getSubReg(DestReg, Hexagon::subreg_hireg);
773 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DstHi)
774 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg), KillFlag);
775 unsigned DstLo = HRI.getSubReg(DestReg, Hexagon::subreg_loreg);
776 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DstLo)
777 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg), KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000778 return;
779 }
Sirish Pande30804c22012-02-15 18:52:27 +0000780
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000781#ifndef NDEBUG
782 // Show the invalid registers to ease debugging.
783 dbgs() << "Invalid registers for copy in BB#" << MBB.getNumber()
784 << ": " << PrintReg(DestReg, &HRI)
785 << " = " << PrintReg(SrcReg, &HRI) << '\n';
786#endif
Sirish Pande30804c22012-02-15 18:52:27 +0000787 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000788}
789
790
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000791void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
792 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
793 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000794 DebugLoc DL = MBB.findDebugLoc(I);
795 MachineFunction &MF = *MBB.getParent();
796 MachineFrameInfo &MFI = *MF.getFrameInfo();
797 unsigned Align = MFI.getObjectAlignment(FI);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000798 unsigned KillFlag = getKillRegState(isKill);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000799
Alex Lorenze40c8a22015-08-11 23:09:45 +0000800 MachineMemOperand *MMO = MF.getMachineMemOperand(
801 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
802 MFI.getObjectSize(FI), Align);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000803
Craig Topperc7242e02012-04-20 07:30:17 +0000804 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000805 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_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::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000809 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000810 .addFrameIndex(FI).addImm(0)
811 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000812 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000813 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000814 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000815 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000816 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
817 BuildMI(MBB, I, DL, get(Hexagon::STriw_mod))
818 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000819 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
820 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
821 BuildMI(MBB, I, DL, get(Hexagon::STriq_pred_V6_128B))
822 .addFrameIndex(FI).addImm(0)
823 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
824 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
825 BuildMI(MBB, I, DL, get(Hexagon::STriq_pred_V6))
826 .addFrameIndex(FI).addImm(0)
827 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
828 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
829 DEBUG(dbgs() << "++Generating 128B vector spill");
830 BuildMI(MBB, I, DL, get(Hexagon::STriv_pseudo_V6_128B))
831 .addFrameIndex(FI).addImm(0)
832 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
833 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
834 DEBUG(dbgs() << "++Generating vector spill");
835 BuildMI(MBB, I, DL, get(Hexagon::STriv_pseudo_V6))
836 .addFrameIndex(FI).addImm(0)
837 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
838 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
839 DEBUG(dbgs() << "++Generating double vector spill");
840 BuildMI(MBB, I, DL, get(Hexagon::STrivv_pseudo_V6))
841 .addFrameIndex(FI).addImm(0)
842 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
843 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
844 DEBUG(dbgs() << "++Generating 128B double vector spill");
845 BuildMI(MBB, I, DL, get(Hexagon::STrivv_pseudo_V6_128B))
846 .addFrameIndex(FI).addImm(0)
847 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000848 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000849 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000850 }
851}
852
853
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000854void HexagonInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
855 MachineBasicBlock::iterator I, unsigned DestReg, int FI,
856 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000857 DebugLoc DL = MBB.findDebugLoc(I);
858 MachineFunction &MF = *MBB.getParent();
859 MachineFrameInfo &MFI = *MF.getFrameInfo();
860 unsigned Align = MFI.getObjectAlignment(FI);
861
Alex Lorenze40c8a22015-08-11 23:09:45 +0000862 MachineMemOperand *MMO = MF.getMachineMemOperand(
863 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
864 MFI.getObjectSize(FI), Align);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000865
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000866 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000867 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000868 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000869 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000870 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000871 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000872 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000873 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000874 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
875 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
876 BuildMI(MBB, I, DL, get(Hexagon::LDriw_mod), DestReg)
877 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000878 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
879 BuildMI(MBB, I, DL, get(Hexagon::LDriq_pred_V6_128B), DestReg)
880 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
881 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
882 BuildMI(MBB, I, DL, get(Hexagon::LDriq_pred_V6), DestReg)
883 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
884 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
885 DEBUG(dbgs() << "++Generating 128B double vector restore");
886 BuildMI(MBB, I, DL, get(Hexagon::LDrivv_pseudo_V6_128B), DestReg)
887 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
888 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
889 DEBUG(dbgs() << "++Generating 128B vector restore");
890 BuildMI(MBB, I, DL, get(Hexagon::LDriv_pseudo_V6_128B), DestReg)
891 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
892 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
893 DEBUG(dbgs() << "++Generating vector restore");
894 BuildMI(MBB, I, DL, get(Hexagon::LDriv_pseudo_V6), DestReg)
895 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
896 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
897 DEBUG(dbgs() << "++Generating double vector restore");
898 BuildMI(MBB, I, DL, get(Hexagon::LDrivv_pseudo_V6), DestReg)
899 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000900 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000901 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000902 }
903}
904
905
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000906/// expandPostRAPseudo - This function is called for all pseudo instructions
907/// that remain after register allocation. Many pseudo instructions are
908/// created to help register allocation. This is the place to convert them
909/// into real instructions. The target can edit MI in place, or it can insert
910/// new instructions and erase MI. The function should return true if
911/// anything was changed.
912bool HexagonInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI)
913 const {
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000914 const HexagonRegisterInfo &HRI = getRegisterInfo();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000915 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +0000916 MachineBasicBlock &MBB = *MI->getParent();
917 DebugLoc DL = MI->getDebugLoc();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000918 unsigned Opc = MI->getOpcode();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +0000919 const unsigned VecOffset = 1;
920 bool Is128B = false;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000921
922 switch (Opc) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000923 case Hexagon::ALIGNA:
924 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI->getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000925 .addReg(HRI.getFrameRegister())
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000926 .addImm(-MI->getOperand(1).getImm());
927 MBB.erase(MI);
928 return true;
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +0000929 case Hexagon::HEXAGON_V6_vassignp_128B:
930 case Hexagon::HEXAGON_V6_vassignp: {
931 unsigned SrcReg = MI->getOperand(1).getReg();
932 unsigned DstReg = MI->getOperand(0).getReg();
933 if (SrcReg != DstReg)
934 copyPhysReg(MBB, MI, DL, DstReg, SrcReg, MI->getOperand(1).isKill());
935 MBB.erase(MI);
936 return true;
937 }
938 case Hexagon::HEXAGON_V6_lo_128B:
939 case Hexagon::HEXAGON_V6_lo: {
940 unsigned SrcReg = MI->getOperand(1).getReg();
941 unsigned DstReg = MI->getOperand(0).getReg();
942 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::subreg_loreg);
943 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI->getOperand(1).isKill());
944 MBB.erase(MI);
945 MRI.clearKillFlags(SrcSubLo);
946 return true;
947 }
948 case Hexagon::HEXAGON_V6_hi_128B:
949 case Hexagon::HEXAGON_V6_hi: {
950 unsigned SrcReg = MI->getOperand(1).getReg();
951 unsigned DstReg = MI->getOperand(0).getReg();
952 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::subreg_hireg);
953 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI->getOperand(1).isKill());
954 MBB.erase(MI);
955 MRI.clearKillFlags(SrcSubHi);
956 return true;
957 }
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +0000958 case Hexagon::STrivv_indexed_128B:
959 Is128B = true;
960 case Hexagon::STrivv_indexed: {
961 unsigned SrcReg = MI->getOperand(2).getReg();
962 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::subreg_hireg);
963 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::subreg_loreg);
964 unsigned NewOpcd = Is128B ? Hexagon::V6_vS32b_ai_128B
965 : Hexagon::V6_vS32b_ai;
966 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
967 MachineInstr *MI1New = BuildMI(MBB, MI, DL, get(NewOpcd))
968 .addOperand(MI->getOperand(0))
969 .addImm(MI->getOperand(1).getImm())
970 .addReg(SrcSubLo)
971 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
972 MI1New->getOperand(0).setIsKill(false);
973 BuildMI(MBB, MI, DL, get(NewOpcd))
974 .addOperand(MI->getOperand(0))
975 // The Vectors are indexed in multiples of vector size.
976 .addImm(MI->getOperand(1).getImm()+Offset)
977 .addReg(SrcSubHi)
978 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
979 MBB.erase(MI);
980 return true;
981 }
982 case Hexagon::LDrivv_pseudo_V6_128B:
983 case Hexagon::LDrivv_indexed_128B:
984 Is128B = true;
985 case Hexagon::LDrivv_pseudo_V6:
986 case Hexagon::LDrivv_indexed: {
987 unsigned NewOpcd = Is128B ? Hexagon::V6_vL32b_ai_128B
988 : Hexagon::V6_vL32b_ai;
989 unsigned DstReg = MI->getOperand(0).getReg();
990 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
991 MachineInstr *MI1New =
992 BuildMI(MBB, MI, DL, get(NewOpcd),
993 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
994 .addOperand(MI->getOperand(1))
995 .addImm(MI->getOperand(2).getImm());
996 MI1New->getOperand(1).setIsKill(false);
997 BuildMI(MBB, MI, DL, get(NewOpcd),
998 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
999 .addOperand(MI->getOperand(1))
1000 // The Vectors are indexed in multiples of vector size.
1001 .addImm(MI->getOperand(2).getImm() + Offset)
1002 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
1003 MBB.erase(MI);
1004 return true;
1005 }
1006 case Hexagon::LDriv_pseudo_V6_128B:
1007 Is128B = true;
1008 case Hexagon::LDriv_pseudo_V6: {
1009 unsigned DstReg = MI->getOperand(0).getReg();
1010 unsigned NewOpc = Is128B ? Hexagon::V6_vL32b_ai_128B
1011 : Hexagon::V6_vL32b_ai;
1012 int32_t Off = MI->getOperand(2).getImm();
1013 int32_t Idx = Off;
1014 BuildMI(MBB, MI, DL, get(NewOpc), DstReg)
1015 .addOperand(MI->getOperand(1))
1016 .addImm(Idx)
1017 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
1018 MBB.erase(MI);
1019 return true;
1020 }
1021 case Hexagon::STriv_pseudo_V6_128B:
1022 Is128B = true;
1023 case Hexagon::STriv_pseudo_V6: {
1024 unsigned NewOpc = Is128B ? Hexagon::V6_vS32b_ai_128B
1025 : Hexagon::V6_vS32b_ai;
1026 int32_t Off = MI->getOperand(1).getImm();
1027 int32_t Idx = Is128B ? (Off >> 7) : (Off >> 6);
1028 BuildMI(MBB, MI, DL, get(NewOpc))
1029 .addOperand(MI->getOperand(0))
1030 .addImm(Idx)
1031 .addOperand(MI->getOperand(2))
1032 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
1033 MBB.erase(MI);
1034 return true;
1035 }
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001036 case Hexagon::TFR_PdTrue: {
1037 unsigned Reg = MI->getOperand(0).getReg();
1038 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1039 .addReg(Reg, RegState::Undef)
1040 .addReg(Reg, RegState::Undef);
1041 MBB.erase(MI);
1042 return true;
1043 }
1044 case Hexagon::TFR_PdFalse: {
1045 unsigned Reg = MI->getOperand(0).getReg();
1046 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1047 .addReg(Reg, RegState::Undef)
1048 .addReg(Reg, RegState::Undef);
1049 MBB.erase(MI);
1050 return true;
1051 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001052 case Hexagon::VMULW: {
1053 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
1054 unsigned DstReg = MI->getOperand(0).getReg();
1055 unsigned Src1Reg = MI->getOperand(1).getReg();
1056 unsigned Src2Reg = MI->getOperand(2).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001057 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
1058 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
1059 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
1060 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001061 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001062 HRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001063 .addReg(Src2SubHi);
1064 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_mpyi),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001065 HRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001066 .addReg(Src2SubLo);
1067 MBB.erase(MI);
1068 MRI.clearKillFlags(Src1SubHi);
1069 MRI.clearKillFlags(Src1SubLo);
1070 MRI.clearKillFlags(Src2SubHi);
1071 MRI.clearKillFlags(Src2SubLo);
1072 return true;
1073 }
1074 case Hexagon::VMULW_ACC: {
1075 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
1076 unsigned DstReg = MI->getOperand(0).getReg();
1077 unsigned Src1Reg = MI->getOperand(1).getReg();
1078 unsigned Src2Reg = MI->getOperand(2).getReg();
1079 unsigned Src3Reg = MI->getOperand(3).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001080 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
1081 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
1082 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
1083 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
1084 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::subreg_hireg);
1085 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::subreg_loreg);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001086 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001087 HRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001088 .addReg(Src2SubHi).addReg(Src3SubHi);
1089 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::M2_maci),
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001090 HRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001091 .addReg(Src2SubLo).addReg(Src3SubLo);
1092 MBB.erase(MI);
1093 MRI.clearKillFlags(Src1SubHi);
1094 MRI.clearKillFlags(Src1SubLo);
1095 MRI.clearKillFlags(Src2SubHi);
1096 MRI.clearKillFlags(Src2SubLo);
1097 MRI.clearKillFlags(Src3SubHi);
1098 MRI.clearKillFlags(Src3SubLo);
1099 return true;
1100 }
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +00001101 case Hexagon::Insert4: {
1102 unsigned DstReg = MI->getOperand(0).getReg();
1103 unsigned Src1Reg = MI->getOperand(1).getReg();
1104 unsigned Src2Reg = MI->getOperand(2).getReg();
1105 unsigned Src3Reg = MI->getOperand(3).getReg();
1106 unsigned Src4Reg = MI->getOperand(4).getReg();
1107 unsigned Src1RegIsKill = getKillRegState(MI->getOperand(1).isKill());
1108 unsigned Src2RegIsKill = getKillRegState(MI->getOperand(2).isKill());
1109 unsigned Src3RegIsKill = getKillRegState(MI->getOperand(3).isKill());
1110 unsigned Src4RegIsKill = getKillRegState(MI->getOperand(4).isKill());
1111 unsigned DstSubHi = HRI.getSubReg(DstReg, Hexagon::subreg_hireg);
1112 unsigned DstSubLo = HRI.getSubReg(DstReg, Hexagon::subreg_loreg);
1113 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::S2_insert),
1114 HRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(DstSubLo)
1115 .addReg(Src1Reg, Src1RegIsKill).addImm(16).addImm(0);
1116 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::S2_insert),
1117 HRI.getSubReg(DstReg, Hexagon::subreg_loreg)).addReg(DstSubLo)
1118 .addReg(Src2Reg, Src2RegIsKill).addImm(16).addImm(16);
1119 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::S2_insert),
1120 HRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(DstSubHi)
1121 .addReg(Src3Reg, Src3RegIsKill).addImm(16).addImm(0);
1122 BuildMI(MBB, MI, MI->getDebugLoc(), get(Hexagon::S2_insert),
1123 HRI.getSubReg(DstReg, Hexagon::subreg_hireg)).addReg(DstSubHi)
1124 .addReg(Src4Reg, Src4RegIsKill).addImm(16).addImm(16);
1125 MBB.erase(MI);
1126 MRI.clearKillFlags(DstReg);
1127 MRI.clearKillFlags(DstSubHi);
1128 MRI.clearKillFlags(DstSubLo);
1129 return true;
1130 }
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001131 case Hexagon::MUX64_rr: {
1132 const MachineOperand &Op0 = MI->getOperand(0);
1133 const MachineOperand &Op1 = MI->getOperand(1);
1134 const MachineOperand &Op2 = MI->getOperand(2);
1135 const MachineOperand &Op3 = MI->getOperand(3);
1136 unsigned Rd = Op0.getReg();
1137 unsigned Pu = Op1.getReg();
1138 unsigned Rs = Op2.getReg();
1139 unsigned Rt = Op3.getReg();
1140 DebugLoc DL = MI->getDebugLoc();
1141 unsigned K1 = getKillRegState(Op1.isKill());
1142 unsigned K2 = getKillRegState(Op2.isKill());
1143 unsigned K3 = getKillRegState(Op3.isKill());
1144 if (Rd != Rs)
1145 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1146 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1147 .addReg(Rs, K2);
1148 if (Rd != Rt)
1149 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1150 .addReg(Pu, K1)
1151 .addReg(Rt, K3);
1152 MBB.erase(MI);
1153 return true;
1154 }
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001155 case Hexagon::VSelectPseudo_V6: {
1156 const MachineOperand &Op0 = MI->getOperand(0);
1157 const MachineOperand &Op1 = MI->getOperand(1);
1158 const MachineOperand &Op2 = MI->getOperand(2);
1159 const MachineOperand &Op3 = MI->getOperand(3);
1160 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
1161 .addOperand(Op0)
1162 .addOperand(Op1)
1163 .addOperand(Op2);
1164 BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
1165 .addOperand(Op0)
1166 .addOperand(Op1)
1167 .addOperand(Op3);
1168 MBB.erase(MI);
1169 return true;
1170 }
1171 case Hexagon::VSelectDblPseudo_V6: {
1172 MachineOperand &Op0 = MI->getOperand(0);
1173 MachineOperand &Op1 = MI->getOperand(1);
1174 MachineOperand &Op2 = MI->getOperand(2);
1175 MachineOperand &Op3 = MI->getOperand(3);
1176 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::subreg_loreg);
1177 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::subreg_hireg);
1178 BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
1179 .addOperand(Op0)
1180 .addOperand(Op1)
1181 .addReg(SrcHi)
1182 .addReg(SrcLo);
1183 SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::subreg_loreg);
1184 SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::subreg_hireg);
1185 BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
1186 .addOperand(Op0)
1187 .addOperand(Op1)
1188 .addReg(SrcHi)
1189 .addReg(SrcLo);
1190 MBB.erase(MI);
1191 return true;
1192 }
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001193 case Hexagon::TCRETURNi:
1194 MI->setDesc(get(Hexagon::J2_jump));
1195 return true;
1196 case Hexagon::TCRETURNr:
1197 MI->setDesc(get(Hexagon::J2_jumpr));
1198 return true;
Krzysztof Parzyszek70a134d2015-11-25 21:40:03 +00001199 case Hexagon::TFRI_f:
1200 case Hexagon::TFRI_cPt_f:
1201 case Hexagon::TFRI_cNotPt_f: {
1202 unsigned Opx = (Opc == Hexagon::TFRI_f) ? 1 : 2;
1203 APFloat FVal = MI->getOperand(Opx).getFPImm()->getValueAPF();
1204 APInt IVal = FVal.bitcastToAPInt();
1205 MI->RemoveOperand(Opx);
1206 unsigned NewOpc = (Opc == Hexagon::TFRI_f) ? Hexagon::A2_tfrsi :
1207 (Opc == Hexagon::TFRI_cPt_f) ? Hexagon::C2_cmoveit :
1208 Hexagon::C2_cmoveif;
1209 MI->setDesc(get(NewOpc));
1210 MI->addOperand(MachineOperand::CreateImm(IVal.getZExtValue()));
1211 return true;
1212 }
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001213 }
1214
1215 return false;
1216}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001217
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001218
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001219// We indicate that we want to reverse the branch by
1220// inserting the reversed branching opcode.
1221bool HexagonInstrInfo::ReverseBranchCondition(
1222 SmallVectorImpl<MachineOperand> &Cond) const {
1223 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001224 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001225 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1226 unsigned opcode = Cond[0].getImm();
1227 //unsigned temp;
1228 assert(get(opcode).isBranch() && "Should be a branching condition.");
1229 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001230 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001231 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1232 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001233 return false;
1234}
1235
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001236
1237void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1238 MachineBasicBlock::iterator MI) const {
1239 DebugLoc DL;
1240 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1241}
1242
1243
1244// Returns true if an instruction is predicated irrespective of the predicate
1245// sense. For example, all of the following will return true.
1246// if (p0) R1 = add(R2, R3)
1247// if (!p0) R1 = add(R2, R3)
1248// if (p0.new) R1 = add(R2, R3)
1249// if (!p0.new) R1 = add(R2, R3)
1250// Note: New-value stores are not included here as in the current
1251// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001252bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1253 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001254 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001255}
1256
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001257bool HexagonInstrInfo::PredicateInstruction(
1258 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001259 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1260 isEndLoopN(Cond[0].getImm())) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001261 DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001262 return false;
1263 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001264 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001265 assert (isPredicable(MI) && "Expected predicable instruction");
1266 bool invertJump = predOpcodeHasNot(Cond);
1267
1268 // We have to predicate MI "in place", i.e. after this function returns,
1269 // MI will need to be transformed into a predicated form. To avoid com-
1270 // plicated manipulations with the operands (handling tied operands,
1271 // etc.), build a new temporary instruction, then overwrite MI with it.
1272
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001273 MachineBasicBlock &B = *MI.getParent();
1274 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001275 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1276 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001277 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001278 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001279 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001280 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1281 break;
1282 T.addOperand(Op);
1283 NOp++;
1284 }
1285
1286 unsigned PredReg, PredRegPos, PredRegFlags;
1287 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1288 (void)GotPredReg;
1289 assert(GotPredReg);
1290 T.addReg(PredReg, PredRegFlags);
1291 while (NOp < NumOps)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001292 T.addOperand(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001293
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001294 MI.setDesc(get(PredOpc));
1295 while (unsigned n = MI.getNumOperands())
1296 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001297 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001298 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001299
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001300 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001301 B.erase(TI);
1302
1303 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1304 MRI.clearKillFlags(PredReg);
1305 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001306}
1307
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001308
1309bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1310 ArrayRef<MachineOperand> Pred2) const {
1311 // TODO: Fix this
1312 return false;
1313}
1314
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001315bool HexagonInstrInfo::DefinesPredicate(
1316 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001317 auto &HRI = getRegisterInfo();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001318 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1319 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001320 if (MO.isReg() && MO.isDef()) {
1321 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1322 if (RC == &Hexagon::PredRegsRegClass) {
1323 Pred.push_back(MO);
1324 return true;
1325 }
1326 }
1327 }
1328 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001329}
Andrew Trickd06df962012-02-01 22:13:57 +00001330
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001331bool HexagonInstrInfo::isPredicable(MachineInstr &MI) const {
1332 bool isPred = MI.getDesc().isPredicable();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001333
1334 if (!isPred)
1335 return false;
1336
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001337 const int Opc = MI.getOpcode();
1338 int NumOperands = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001339
1340 // Keep a flag for upto 4 operands in the instructions, to indicate if
1341 // that operand has been constant extended.
Krzysztof Parzyszek89757432016-05-05 22:00:44 +00001342 bool OpCExtended[4] = {false};
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001343 if (NumOperands > 4)
1344 NumOperands = 4;
1345
1346 for (int i = 0; i < NumOperands; i++)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001347 OpCExtended[i] = (isOperandExtended(&MI, i) && isConstExtended(&MI));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001348
1349 switch(Opc) {
Colin LeMahieu4af437f2014-12-09 20:23:30 +00001350 case Hexagon::A2_tfrsi:
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001351 return (isOperandExtended(&MI, 1) && isConstExtended(&MI)) ||
1352 isInt<12>(MI.getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001353
Colin LeMahieubda31b42014-12-29 20:44:51 +00001354 case Hexagon::S2_storerd_io:
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001355 return isShiftedUInt<6,3>(MI.getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001356
Colin LeMahieubda31b42014-12-29 20:44:51 +00001357 case Hexagon::S2_storeri_io:
Colin LeMahieu90148902014-12-30 22:28:31 +00001358 case Hexagon::S2_storerinew_io:
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001359 return isShiftedUInt<6,2>(MI.getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001360
Colin LeMahieubda31b42014-12-29 20:44:51 +00001361 case Hexagon::S2_storerh_io:
Colin LeMahieu90148902014-12-30 22:28:31 +00001362 case Hexagon::S2_storerhnew_io:
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001363 return isShiftedUInt<6,1>(MI.getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001364
Colin LeMahieubda31b42014-12-29 20:44:51 +00001365 case Hexagon::S2_storerb_io:
Colin LeMahieu90148902014-12-30 22:28:31 +00001366 case Hexagon::S2_storerbnew_io:
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001367 return isUInt<6>(MI.getOperand(1).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001368
Colin LeMahieu947cd702014-12-23 20:44:59 +00001369 case Hexagon::L2_loadrd_io:
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001370 return isShiftedUInt<6,3>(MI.getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001371
Colin LeMahieu026e88d2014-12-23 20:02:16 +00001372 case Hexagon::L2_loadri_io:
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001373 return isShiftedUInt<6,2>(MI.getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001374
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00001375 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00001376 case Hexagon::L2_loadruh_io:
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001377 return isShiftedUInt<6,1>(MI.getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001378
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00001379 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00001380 case Hexagon::L2_loadrub_io:
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001381 return isUInt<6>(MI.getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001382
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +00001383 case Hexagon::L2_loadrd_pi:
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001384 return isShiftedInt<4,3>(MI.getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001385
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +00001386 case Hexagon::L2_loadri_pi:
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001387 return isShiftedInt<4,2>(MI.getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001388
Colin LeMahieuc83cbbf2014-12-26 19:31:46 +00001389 case Hexagon::L2_loadrh_pi:
1390 case Hexagon::L2_loadruh_pi:
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001391 return isShiftedInt<4,1>(MI.getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001392
Colin LeMahieu96976a12014-12-26 18:57:13 +00001393 case Hexagon::L2_loadrb_pi:
Colin LeMahieufe9612e2014-12-26 19:12:11 +00001394 case Hexagon::L2_loadrub_pi:
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001395 return isInt<4>(MI.getOperand(3).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001396
Colin LeMahieu2bad4a72014-12-30 21:01:38 +00001397 case Hexagon::S4_storeirb_io:
1398 case Hexagon::S4_storeirh_io:
1399 case Hexagon::S4_storeiri_io:
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001400 return (OpCExtended[1] || isUInt<6>(MI.getOperand(1).getImm())) &&
1401 (OpCExtended[2] || isInt<6>(MI.getOperand(2).getImm()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001402
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00001403 case Hexagon::A2_addi:
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001404 return isInt<8>(MI.getOperand(2).getImm());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001405
Colin LeMahieu3b3197e2014-11-24 17:44:19 +00001406 case Hexagon::A2_aslh:
Colin LeMahieu397a25e2014-11-24 18:04:42 +00001407 case Hexagon::A2_asrh:
Colin LeMahieu91ffec92014-11-21 21:35:52 +00001408 case Hexagon::A2_sxtb:
Colin LeMahieu310991c2014-11-21 21:54:59 +00001409 case Hexagon::A2_sxth:
Colin LeMahieubb7d6f52014-11-24 16:48:43 +00001410 case Hexagon::A2_zxtb:
Colin LeMahieu098256c2014-11-24 17:11:34 +00001411 case Hexagon::A2_zxth:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001412 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001413 }
1414
1415 return true;
1416}
1417
Jyotsna Verma84c47102013-05-06 18:49:23 +00001418
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001419bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1420 const MachineBasicBlock *MBB, const MachineFunction &MF) const {
1421 // Debug info is never a scheduling boundary. It's necessary to be explicit
1422 // due to the special treatment of IT instructions below, otherwise a
1423 // dbg_value followed by an IT will result in the IT instruction being
1424 // considered a scheduling hazard, which is wrong. It should be the actual
1425 // instruction preceding the dbg_value instruction(s), just like it is
1426 // when debug info is not present.
1427 if (MI->isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001428 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001429
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001430 // Throwing call is a boundary.
1431 if (MI->isCall()) {
1432 // If any of the block's successors is a landing pad, this could be a
1433 // throwing call.
1434 for (auto I : MBB->successors())
1435 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001436 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001437 }
1438
1439 // Don't mess around with no return calls.
1440 if (MI->getOpcode() == Hexagon::CALLv3nr)
1441 return true;
1442
1443 // Terminators and labels can't be scheduled around.
1444 if (MI->getDesc().isTerminator() || MI->isPosition())
1445 return true;
1446
1447 if (MI->isInlineAsm() && !ScheduleInlineAsm)
1448 return true;
1449
1450 return false;
1451}
1452
1453
1454/// Measure the specified inline asm to determine an approximation of its
1455/// length.
1456/// Comments (which run till the next SeparatorString or newline) do not
1457/// count as an instruction.
1458/// Any other non-whitespace text is considered an instruction, with
1459/// multiple instructions separated by SeparatorString or newlines.
1460/// Variable-length instructions are not handled here; this function
1461/// may be overloaded in the target code to do that.
1462/// Hexagon counts the number of ##'s and adjust for that many
1463/// constant exenders.
1464unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1465 const MCAsmInfo &MAI) const {
1466 StringRef AStr(Str);
1467 // Count the number of instructions in the asm.
1468 bool atInsnStart = true;
1469 unsigned Length = 0;
1470 for (; *Str; ++Str) {
1471 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1472 strlen(MAI.getSeparatorString())) == 0)
1473 atInsnStart = true;
1474 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1475 Length += MAI.getMaxInstLength();
1476 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001477 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001478 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
1479 strlen(MAI.getCommentString())) == 0)
1480 atInsnStart = false;
1481 }
1482
1483 // Add to size number of constant extenders seen * 4.
1484 StringRef Occ("##");
1485 Length += AStr.count(Occ)*4;
1486 return Length;
1487}
1488
1489
1490ScheduleHazardRecognizer*
1491HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1492 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
1493 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1494}
1495
1496
1497/// \brief For a comparison instruction, return the source registers in
1498/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1499/// compares against in CmpValue. Return true if the comparison instruction
1500/// can be analyzed.
1501bool HexagonInstrInfo::analyzeCompare(const MachineInstr *MI,
1502 unsigned &SrcReg, unsigned &SrcReg2, int &Mask, int &Value) const {
1503 unsigned Opc = MI->getOpcode();
1504
1505 // Set mask and the first source register.
1506 switch (Opc) {
1507 case Hexagon::C2_cmpeq:
1508 case Hexagon::C2_cmpeqp:
1509 case Hexagon::C2_cmpgt:
1510 case Hexagon::C2_cmpgtp:
1511 case Hexagon::C2_cmpgtu:
1512 case Hexagon::C2_cmpgtup:
1513 case Hexagon::C4_cmpneq:
1514 case Hexagon::C4_cmplte:
1515 case Hexagon::C4_cmplteu:
1516 case Hexagon::C2_cmpeqi:
1517 case Hexagon::C2_cmpgti:
1518 case Hexagon::C2_cmpgtui:
1519 case Hexagon::C4_cmpneqi:
1520 case Hexagon::C4_cmplteui:
1521 case Hexagon::C4_cmpltei:
1522 SrcReg = MI->getOperand(1).getReg();
1523 Mask = ~0;
1524 break;
1525 case Hexagon::A4_cmpbeq:
1526 case Hexagon::A4_cmpbgt:
1527 case Hexagon::A4_cmpbgtu:
1528 case Hexagon::A4_cmpbeqi:
1529 case Hexagon::A4_cmpbgti:
1530 case Hexagon::A4_cmpbgtui:
1531 SrcReg = MI->getOperand(1).getReg();
1532 Mask = 0xFF;
1533 break;
1534 case Hexagon::A4_cmpheq:
1535 case Hexagon::A4_cmphgt:
1536 case Hexagon::A4_cmphgtu:
1537 case Hexagon::A4_cmpheqi:
1538 case Hexagon::A4_cmphgti:
1539 case Hexagon::A4_cmphgtui:
1540 SrcReg = MI->getOperand(1).getReg();
1541 Mask = 0xFFFF;
1542 break;
1543 }
1544
1545 // Set the value/second source register.
1546 switch (Opc) {
1547 case Hexagon::C2_cmpeq:
1548 case Hexagon::C2_cmpeqp:
1549 case Hexagon::C2_cmpgt:
1550 case Hexagon::C2_cmpgtp:
1551 case Hexagon::C2_cmpgtu:
1552 case Hexagon::C2_cmpgtup:
1553 case Hexagon::A4_cmpbeq:
1554 case Hexagon::A4_cmpbgt:
1555 case Hexagon::A4_cmpbgtu:
1556 case Hexagon::A4_cmpheq:
1557 case Hexagon::A4_cmphgt:
1558 case Hexagon::A4_cmphgtu:
1559 case Hexagon::C4_cmpneq:
1560 case Hexagon::C4_cmplte:
1561 case Hexagon::C4_cmplteu:
1562 SrcReg2 = MI->getOperand(2).getReg();
1563 return true;
1564
1565 case Hexagon::C2_cmpeqi:
1566 case Hexagon::C2_cmpgtui:
1567 case Hexagon::C2_cmpgti:
1568 case Hexagon::C4_cmpneqi:
1569 case Hexagon::C4_cmplteui:
1570 case Hexagon::C4_cmpltei:
1571 case Hexagon::A4_cmpbeqi:
1572 case Hexagon::A4_cmpbgti:
1573 case Hexagon::A4_cmpbgtui:
1574 case Hexagon::A4_cmpheqi:
1575 case Hexagon::A4_cmphgti:
1576 case Hexagon::A4_cmphgtui:
1577 SrcReg2 = 0;
1578 Value = MI->getOperand(2).getImm();
1579 return true;
1580 }
1581
1582 return false;
1583}
1584
1585
1586unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
1587 const MachineInstr *MI, unsigned *PredCost) const {
1588 return getInstrTimingClassLatency(ItinData, MI);
1589}
1590
1591
1592DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1593 const TargetSubtargetInfo &STI) const {
1594 const InstrItineraryData *II = STI.getInstrItineraryData();
1595 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1596}
1597
1598
1599// Inspired by this pair:
1600// %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1601// S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1602// Currently AA considers the addresses in these instructions to be aliasing.
1603bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(MachineInstr *MIa,
1604 MachineInstr *MIb, AliasAnalysis *AA) const {
1605 int OffsetA = 0, OffsetB = 0;
1606 unsigned SizeA = 0, SizeB = 0;
1607
1608 if (MIa->hasUnmodeledSideEffects() || MIb->hasUnmodeledSideEffects() ||
1609 MIa->hasOrderedMemoryRef() || MIa->hasOrderedMemoryRef())
1610 return false;
1611
1612 // Instructions that are pure loads, not loads and stores like memops are not
1613 // dependent.
1614 if (MIa->mayLoad() && !isMemOp(MIa) && MIb->mayLoad() && !isMemOp(MIb))
1615 return true;
1616
1617 // Get base, offset, and access size in MIa.
1618 unsigned BaseRegA = getBaseAndOffset(MIa, OffsetA, SizeA);
1619 if (!BaseRegA || !SizeA)
1620 return false;
1621
1622 // Get base, offset, and access size in MIb.
1623 unsigned BaseRegB = getBaseAndOffset(MIb, OffsetB, SizeB);
1624 if (!BaseRegB || !SizeB)
1625 return false;
1626
1627 if (BaseRegA != BaseRegB)
1628 return false;
1629
1630 // This is a mem access with the same base register and known offsets from it.
1631 // Reason about it.
1632 if (OffsetA > OffsetB) {
1633 uint64_t offDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1634 return (SizeB <= offDiff);
1635 } else if (OffsetA < OffsetB) {
1636 uint64_t offDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1637 return (SizeA <= offDiff);
1638 }
1639
1640 return false;
1641}
1642
1643
1644unsigned HexagonInstrInfo::createVR(MachineFunction* MF, MVT VT) const {
1645 MachineRegisterInfo &MRI = MF->getRegInfo();
1646 const TargetRegisterClass *TRC;
1647 if (VT == MVT::i1) {
1648 TRC = &Hexagon::PredRegsRegClass;
1649 } else if (VT == MVT::i32 || VT == MVT::f32) {
1650 TRC = &Hexagon::IntRegsRegClass;
1651 } else if (VT == MVT::i64 || VT == MVT::f64) {
1652 TRC = &Hexagon::DoubleRegsRegClass;
1653 } else {
1654 llvm_unreachable("Cannot handle this register class");
1655 }
1656
1657 unsigned NewReg = MRI.createVirtualRegister(TRC);
1658 return NewReg;
1659}
1660
1661
1662bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr* MI) const {
1663 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1664}
1665
1666
1667bool HexagonInstrInfo::isAccumulator(const MachineInstr *MI) const {
1668 const uint64_t F = MI->getDesc().TSFlags;
1669 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1670}
1671
1672
1673bool HexagonInstrInfo::isComplex(const MachineInstr *MI) const {
1674 const MachineFunction *MF = MI->getParent()->getParent();
1675 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1676 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1677
1678 if (!(isTC1(MI))
1679 && !(QII->isTC2Early(MI))
1680 && !(MI->getDesc().mayLoad())
1681 && !(MI->getDesc().mayStore())
1682 && (MI->getDesc().getOpcode() != Hexagon::S2_allocframe)
1683 && (MI->getDesc().getOpcode() != Hexagon::L2_deallocframe)
1684 && !(QII->isMemOp(MI))
1685 && !(MI->isBranch())
1686 && !(MI->isReturn())
1687 && !MI->isCall())
1688 return true;
1689
1690 return false;
1691}
1692
1693
Sanjay Patele4b9f502015-12-07 19:21:39 +00001694// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001695bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr *MI) const {
1696 return (getType(MI) == HexagonII::TypeCOMPOUND && MI->isBranch());
1697}
1698
1699
1700bool HexagonInstrInfo::isCondInst(const MachineInstr *MI) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001701 return (MI->isBranch() && isPredicated(*MI)) ||
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001702 isConditionalTransfer(MI) ||
1703 isConditionalALU32(MI) ||
1704 isConditionalLoad(MI) ||
1705 // Predicated stores which don't have a .new on any operands.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001706 (MI->mayStore() && isPredicated(*MI) && !isNewValueStore(MI) &&
1707 !isPredicatedNew(*MI));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001708}
1709
1710
1711bool HexagonInstrInfo::isConditionalALU32(const MachineInstr* MI) const {
1712 switch (MI->getOpcode()) {
1713 case Hexagon::A2_paddf:
1714 case Hexagon::A2_paddfnew:
1715 case Hexagon::A2_paddif:
1716 case Hexagon::A2_paddifnew:
1717 case Hexagon::A2_paddit:
1718 case Hexagon::A2_padditnew:
1719 case Hexagon::A2_paddt:
1720 case Hexagon::A2_paddtnew:
1721 case Hexagon::A2_pandf:
1722 case Hexagon::A2_pandfnew:
1723 case Hexagon::A2_pandt:
1724 case Hexagon::A2_pandtnew:
1725 case Hexagon::A2_porf:
1726 case Hexagon::A2_porfnew:
1727 case Hexagon::A2_port:
1728 case Hexagon::A2_portnew:
1729 case Hexagon::A2_psubf:
1730 case Hexagon::A2_psubfnew:
1731 case Hexagon::A2_psubt:
1732 case Hexagon::A2_psubtnew:
1733 case Hexagon::A2_pxorf:
1734 case Hexagon::A2_pxorfnew:
1735 case Hexagon::A2_pxort:
1736 case Hexagon::A2_pxortnew:
1737 case Hexagon::A4_paslhf:
1738 case Hexagon::A4_paslhfnew:
1739 case Hexagon::A4_paslht:
1740 case Hexagon::A4_paslhtnew:
1741 case Hexagon::A4_pasrhf:
1742 case Hexagon::A4_pasrhfnew:
1743 case Hexagon::A4_pasrht:
1744 case Hexagon::A4_pasrhtnew:
1745 case Hexagon::A4_psxtbf:
1746 case Hexagon::A4_psxtbfnew:
1747 case Hexagon::A4_psxtbt:
1748 case Hexagon::A4_psxtbtnew:
1749 case Hexagon::A4_psxthf:
1750 case Hexagon::A4_psxthfnew:
1751 case Hexagon::A4_psxtht:
1752 case Hexagon::A4_psxthtnew:
1753 case Hexagon::A4_pzxtbf:
1754 case Hexagon::A4_pzxtbfnew:
1755 case Hexagon::A4_pzxtbt:
1756 case Hexagon::A4_pzxtbtnew:
1757 case Hexagon::A4_pzxthf:
1758 case Hexagon::A4_pzxthfnew:
1759 case Hexagon::A4_pzxtht:
1760 case Hexagon::A4_pzxthtnew:
1761 case Hexagon::C2_ccombinewf:
1762 case Hexagon::C2_ccombinewt:
1763 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001764 }
1765 return false;
1766}
1767
1768
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001769// FIXME - Function name and it's functionality don't match.
1770// It should be renamed to hasPredNewOpcode()
1771bool HexagonInstrInfo::isConditionalLoad(const MachineInstr* MI) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001772 if (!MI->getDesc().mayLoad() || !isPredicated(*MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001773 return false;
1774
1775 int PNewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
1776 // Instruction with valid predicated-new opcode can be promoted to .new.
1777 return PNewOpcode >= 0;
1778}
1779
1780
1781// Returns true if an instruction is a conditional store.
1782//
1783// Note: It doesn't include conditional new-value stores as they can't be
1784// converted to .new predicate.
1785bool HexagonInstrInfo::isConditionalStore(const MachineInstr* MI) const {
1786 switch (MI->getOpcode()) {
1787 default: return false;
1788 case Hexagon::S4_storeirbt_io:
1789 case Hexagon::S4_storeirbf_io:
1790 case Hexagon::S4_pstorerbt_rr:
1791 case Hexagon::S4_pstorerbf_rr:
1792 case Hexagon::S2_pstorerbt_io:
1793 case Hexagon::S2_pstorerbf_io:
1794 case Hexagon::S2_pstorerbt_pi:
1795 case Hexagon::S2_pstorerbf_pi:
1796 case Hexagon::S2_pstorerdt_io:
1797 case Hexagon::S2_pstorerdf_io:
1798 case Hexagon::S4_pstorerdt_rr:
1799 case Hexagon::S4_pstorerdf_rr:
1800 case Hexagon::S2_pstorerdt_pi:
1801 case Hexagon::S2_pstorerdf_pi:
1802 case Hexagon::S2_pstorerht_io:
1803 case Hexagon::S2_pstorerhf_io:
1804 case Hexagon::S4_storeirht_io:
1805 case Hexagon::S4_storeirhf_io:
1806 case Hexagon::S4_pstorerht_rr:
1807 case Hexagon::S4_pstorerhf_rr:
1808 case Hexagon::S2_pstorerht_pi:
1809 case Hexagon::S2_pstorerhf_pi:
1810 case Hexagon::S2_pstorerit_io:
1811 case Hexagon::S2_pstorerif_io:
1812 case Hexagon::S4_storeirit_io:
1813 case Hexagon::S4_storeirif_io:
1814 case Hexagon::S4_pstorerit_rr:
1815 case Hexagon::S4_pstorerif_rr:
1816 case Hexagon::S2_pstorerit_pi:
1817 case Hexagon::S2_pstorerif_pi:
1818
1819 // V4 global address store before promoting to dot new.
1820 case Hexagon::S4_pstorerdt_abs:
1821 case Hexagon::S4_pstorerdf_abs:
1822 case Hexagon::S4_pstorerbt_abs:
1823 case Hexagon::S4_pstorerbf_abs:
1824 case Hexagon::S4_pstorerht_abs:
1825 case Hexagon::S4_pstorerhf_abs:
1826 case Hexagon::S4_pstorerit_abs:
1827 case Hexagon::S4_pstorerif_abs:
1828 return true;
1829
1830 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1831 // from the "Conditional Store" list. Because a predicated new value store
1832 // would NOT be promoted to a double dot new store.
1833 // This function returns yes for those stores that are predicated but not
1834 // yet promoted to predicate dot new instructions.
1835 }
1836}
1837
1838
1839bool HexagonInstrInfo::isConditionalTransfer(const MachineInstr *MI) const {
1840 switch (MI->getOpcode()) {
1841 case Hexagon::A2_tfrt:
1842 case Hexagon::A2_tfrf:
1843 case Hexagon::C2_cmoveit:
1844 case Hexagon::C2_cmoveif:
1845 case Hexagon::A2_tfrtnew:
1846 case Hexagon::A2_tfrfnew:
1847 case Hexagon::C2_cmovenewit:
1848 case Hexagon::C2_cmovenewif:
1849 case Hexagon::A2_tfrpt:
1850 case Hexagon::A2_tfrpf:
1851 return true;
1852
1853 default:
1854 return false;
1855 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001856 return false;
1857}
1858
1859
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001860// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1861// isFPImm and later getFPImm as well.
1862bool HexagonInstrInfo::isConstExtended(const MachineInstr *MI) const {
1863 const uint64_t F = MI->getDesc().TSFlags;
1864 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1865 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001866 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001867
1868 unsigned isExtendable =
1869 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1870 if (!isExtendable)
1871 return false;
1872
1873 if (MI->isCall())
1874 return false;
1875
1876 short ExtOpNum = getCExtOpNum(MI);
1877 const MachineOperand &MO = MI->getOperand(ExtOpNum);
1878 // Use MO operand flags to determine if MO
1879 // has the HMOTF_ConstExtended flag set.
1880 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001881 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001882 // If this is a Machine BB address we are talking about, and it is
1883 // not marked as extended, say so.
1884 if (MO.isMBB())
1885 return false;
1886
1887 // We could be using an instruction with an extendable immediate and shoehorn
1888 // a global address into it. If it is a global address it will be constant
1889 // extended. We do this for COMBINE.
1890 // We currently only handle isGlobal() because it is the only kind of
1891 // object we are going to end up with here for now.
1892 // In the future we probably should add isSymbol(), etc.
1893 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
1894 MO.isJTI() || MO.isCPI())
1895 return true;
1896
1897 // If the extendable operand is not 'Immediate' type, the instruction should
1898 // have 'isExtended' flag set.
1899 assert(MO.isImm() && "Extendable operand must be Immediate type");
1900
1901 int MinValue = getMinValue(MI);
1902 int MaxValue = getMaxValue(MI);
1903 int ImmValue = MO.getImm();
1904
1905 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001906}
1907
1908
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001909bool HexagonInstrInfo::isDeallocRet(const MachineInstr *MI) const {
1910 switch (MI->getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001911 case Hexagon::L4_return :
1912 case Hexagon::L4_return_t :
1913 case Hexagon::L4_return_f :
1914 case Hexagon::L4_return_tnew_pnt :
1915 case Hexagon::L4_return_fnew_pnt :
1916 case Hexagon::L4_return_tnew_pt :
1917 case Hexagon::L4_return_fnew_pt :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001918 return true;
1919 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001920 return false;
1921}
1922
1923
1924// Return true when ConsMI uses a register defined by ProdMI.
1925bool HexagonInstrInfo::isDependent(const MachineInstr *ProdMI,
1926 const MachineInstr *ConsMI) const {
1927 const MCInstrDesc &ProdMCID = ProdMI->getDesc();
1928 if (!ProdMCID.getNumDefs())
1929 return false;
1930
1931 auto &HRI = getRegisterInfo();
1932
1933 SmallVector<unsigned, 4> DefsA;
1934 SmallVector<unsigned, 4> DefsB;
1935 SmallVector<unsigned, 8> UsesA;
1936 SmallVector<unsigned, 8> UsesB;
1937
1938 parseOperands(ProdMI, DefsA, UsesA);
1939 parseOperands(ConsMI, DefsB, UsesB);
1940
1941 for (auto &RegA : DefsA)
1942 for (auto &RegB : UsesB) {
1943 // True data dependency.
1944 if (RegA == RegB)
1945 return true;
1946
1947 if (Hexagon::DoubleRegsRegClass.contains(RegA))
1948 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1949 if (RegB == *SubRegs)
1950 return true;
1951
1952 if (Hexagon::DoubleRegsRegClass.contains(RegB))
1953 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1954 if (RegA == *SubRegs)
1955 return true;
1956 }
1957
1958 return false;
1959}
1960
1961
1962// Returns true if the instruction is alread a .cur.
1963bool HexagonInstrInfo::isDotCurInst(const MachineInstr* MI) const {
1964 switch (MI->getOpcode()) {
1965 case Hexagon::V6_vL32b_cur_pi:
1966 case Hexagon::V6_vL32b_cur_ai:
1967 case Hexagon::V6_vL32b_cur_pi_128B:
1968 case Hexagon::V6_vL32b_cur_ai_128B:
1969 return true;
1970 }
1971 return false;
1972}
1973
1974
1975// Returns true, if any one of the operands is a dot new
1976// insn, whether it is predicated dot new or register dot new.
1977bool HexagonInstrInfo::isDotNewInst(const MachineInstr* MI) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001978 if (isNewValueInst(MI) || (isPredicated(*MI) && isPredicatedNew(*MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001979 return true;
1980
1981 return false;
1982}
1983
1984
1985/// Symmetrical. See if these two instructions are fit for duplex pair.
1986bool HexagonInstrInfo::isDuplexPair(const MachineInstr *MIa,
1987 const MachineInstr *MIb) const {
1988 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
1989 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
1990 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
1991}
1992
1993
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00001994bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001995 if (!MI)
1996 return false;
1997
1998 if (MI->mayLoad() || MI->mayStore() || MI->isCompare())
1999 return true;
2000
2001 // Multiply
2002 unsigned SchedClass = MI->getDesc().getSchedClass();
2003 if (SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23)
2004 return true;
2005 return false;
2006}
2007
2008
2009bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
2010 return (Opcode == Hexagon::ENDLOOP0 ||
2011 Opcode == Hexagon::ENDLOOP1);
2012}
2013
2014
2015bool HexagonInstrInfo::isExpr(unsigned OpType) const {
2016 switch(OpType) {
2017 case MachineOperand::MO_MachineBasicBlock:
2018 case MachineOperand::MO_GlobalAddress:
2019 case MachineOperand::MO_ExternalSymbol:
2020 case MachineOperand::MO_JumpTableIndex:
2021 case MachineOperand::MO_ConstantPoolIndex:
2022 case MachineOperand::MO_BlockAddress:
2023 return true;
2024 default:
2025 return false;
2026 }
2027}
2028
2029
2030bool HexagonInstrInfo::isExtendable(const MachineInstr *MI) const {
2031 const MCInstrDesc &MID = MI->getDesc();
2032 const uint64_t F = MID.TSFlags;
2033 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
2034 return true;
2035
2036 // TODO: This is largely obsolete now. Will need to be removed
2037 // in consecutive patches.
2038 switch(MI->getOpcode()) {
2039 // TFR_FI Remains a special case.
2040 case Hexagon::TFR_FI:
2041 return true;
2042 default:
2043 return false;
2044 }
2045 return false;
2046}
2047
2048
2049// This returns true in two cases:
2050// - The OP code itself indicates that this is an extended instruction.
2051// - One of MOs has been marked with HMOTF_ConstExtended flag.
2052bool HexagonInstrInfo::isExtended(const MachineInstr *MI) const {
2053 // First check if this is permanently extended op code.
2054 const uint64_t F = MI->getDesc().TSFlags;
2055 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2056 return true;
2057 // Use MO operand flags to determine if one of MI's operands
2058 // has HMOTF_ConstExtended flag set.
2059 for (MachineInstr::const_mop_iterator I = MI->operands_begin(),
2060 E = MI->operands_end(); I != E; ++I) {
2061 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
2062 return true;
2063 }
2064 return false;
2065}
2066
2067
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002068bool HexagonInstrInfo::isFloat(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002069 unsigned Opcode = MI->getOpcode();
2070 const uint64_t F = get(Opcode).TSFlags;
2071 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2072}
2073
2074
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002075// No V60 HVX VMEM with A_INDIRECT.
2076bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr *I,
2077 const MachineInstr *J) const {
2078 if (!isV60VectorInstruction(I))
2079 return false;
2080 if (!I->mayLoad() && !I->mayStore())
2081 return false;
2082 return J->isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
2083}
2084
2085
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002086bool HexagonInstrInfo::isIndirectCall(const MachineInstr *MI) const {
2087 switch (MI->getOpcode()) {
2088 case Hexagon::J2_callr :
2089 case Hexagon::J2_callrf :
2090 case Hexagon::J2_callrt :
2091 return true;
2092 }
2093 return false;
2094}
2095
2096
2097bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr *MI) const {
2098 switch (MI->getOpcode()) {
2099 case Hexagon::L4_return :
2100 case Hexagon::L4_return_t :
2101 case Hexagon::L4_return_f :
2102 case Hexagon::L4_return_fnew_pnt :
2103 case Hexagon::L4_return_fnew_pt :
2104 case Hexagon::L4_return_tnew_pnt :
2105 case Hexagon::L4_return_tnew_pt :
2106 return true;
2107 }
2108 return false;
2109}
2110
2111
2112bool HexagonInstrInfo::isJumpR(const MachineInstr *MI) const {
2113 switch (MI->getOpcode()) {
2114 case Hexagon::J2_jumpr :
2115 case Hexagon::J2_jumprt :
2116 case Hexagon::J2_jumprf :
2117 case Hexagon::J2_jumprtnewpt :
2118 case Hexagon::J2_jumprfnewpt :
2119 case Hexagon::J2_jumprtnew :
2120 case Hexagon::J2_jumprfnew :
2121 return true;
2122 }
2123 return false;
2124}
2125
2126
2127// Return true if a given MI can accomodate given offset.
2128// Use abs estimate as oppose to the exact number.
2129// TODO: This will need to be changed to use MC level
2130// definition of instruction extendable field size.
2131bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr *MI,
2132 unsigned offset) const {
2133 // This selection of jump instructions matches to that what
2134 // AnalyzeBranch can parse, plus NVJ.
2135 if (isNewValueJump(MI)) // r9:2
2136 return isInt<11>(offset);
2137
2138 switch (MI->getOpcode()) {
2139 // Still missing Jump to address condition on register value.
2140 default:
2141 return false;
2142 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2143 case Hexagon::J2_call:
2144 case Hexagon::CALLv3nr:
2145 return isInt<24>(offset);
2146 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2147 case Hexagon::J2_jumpf:
2148 case Hexagon::J2_jumptnew:
2149 case Hexagon::J2_jumptnewpt:
2150 case Hexagon::J2_jumpfnew:
2151 case Hexagon::J2_jumpfnewpt:
2152 case Hexagon::J2_callt:
2153 case Hexagon::J2_callf:
2154 return isInt<17>(offset);
2155 case Hexagon::J2_loop0i:
2156 case Hexagon::J2_loop0iext:
2157 case Hexagon::J2_loop0r:
2158 case Hexagon::J2_loop0rext:
2159 case Hexagon::J2_loop1i:
2160 case Hexagon::J2_loop1iext:
2161 case Hexagon::J2_loop1r:
2162 case Hexagon::J2_loop1rext:
2163 return isInt<9>(offset);
2164 // TODO: Add all the compound branches here. Can we do this in Relation model?
2165 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2166 case Hexagon::J4_cmpeqi_tp1_jump_nt:
2167 return isInt<11>(offset);
2168 }
2169}
2170
2171
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002172bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr *LRMI,
2173 const MachineInstr *ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002174 if (!LRMI || !ESMI)
2175 return false;
2176
2177 bool isLate = isLateResultInstr(LRMI);
2178 bool isEarly = isEarlySourceInstr(ESMI);
2179
2180 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
2181 DEBUG(LRMI->dump());
2182 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
2183 DEBUG(ESMI->dump());
2184
2185 if (isLate && isEarly) {
2186 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2187 return true;
2188 }
2189
2190 return false;
2191}
2192
2193
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002194bool HexagonInstrInfo::isLateResultInstr(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002195 if (!MI)
2196 return false;
2197
2198 switch (MI->getOpcode()) {
2199 case TargetOpcode::EXTRACT_SUBREG:
2200 case TargetOpcode::INSERT_SUBREG:
2201 case TargetOpcode::SUBREG_TO_REG:
2202 case TargetOpcode::REG_SEQUENCE:
2203 case TargetOpcode::IMPLICIT_DEF:
2204 case TargetOpcode::COPY:
2205 case TargetOpcode::INLINEASM:
2206 case TargetOpcode::PHI:
2207 return false;
2208 default:
2209 break;
2210 }
2211
2212 unsigned SchedClass = MI->getDesc().getSchedClass();
2213
2214 switch (SchedClass) {
2215 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2216 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2217 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2218 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2219 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2220 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2221 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2222 case Hexagon::Sched::V2LDST_tc_ld_SLOT01:
2223 case Hexagon::Sched::V2LDST_tc_st_SLOT0:
2224 case Hexagon::Sched::V2LDST_tc_st_SLOT01:
2225 case Hexagon::Sched::V4LDST_tc_ld_SLOT01:
2226 case Hexagon::Sched::V4LDST_tc_st_SLOT0:
2227 case Hexagon::Sched::V4LDST_tc_st_SLOT01:
2228 return false;
2229 }
2230 return true;
2231}
2232
2233
2234bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr *MI) const {
2235 if (!MI)
2236 return false;
2237
2238 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2239 // resource, but all operands can be received late like an ALU instruction.
2240 return MI->getDesc().getSchedClass() == Hexagon::Sched::CVI_VX_LATE;
2241}
2242
2243
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002244bool HexagonInstrInfo::isLoopN(const MachineInstr *MI) const {
2245 unsigned Opcode = MI->getOpcode();
2246 return Opcode == Hexagon::J2_loop0i ||
2247 Opcode == Hexagon::J2_loop0r ||
2248 Opcode == Hexagon::J2_loop0iext ||
2249 Opcode == Hexagon::J2_loop0rext ||
2250 Opcode == Hexagon::J2_loop1i ||
2251 Opcode == Hexagon::J2_loop1r ||
2252 Opcode == Hexagon::J2_loop1iext ||
2253 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002254}
2255
2256
2257bool HexagonInstrInfo::isMemOp(const MachineInstr *MI) const {
2258 switch (MI->getOpcode()) {
2259 default: return false;
2260 case Hexagon::L4_iadd_memopw_io :
2261 case Hexagon::L4_isub_memopw_io :
2262 case Hexagon::L4_add_memopw_io :
2263 case Hexagon::L4_sub_memopw_io :
2264 case Hexagon::L4_and_memopw_io :
2265 case Hexagon::L4_or_memopw_io :
2266 case Hexagon::L4_iadd_memoph_io :
2267 case Hexagon::L4_isub_memoph_io :
2268 case Hexagon::L4_add_memoph_io :
2269 case Hexagon::L4_sub_memoph_io :
2270 case Hexagon::L4_and_memoph_io :
2271 case Hexagon::L4_or_memoph_io :
2272 case Hexagon::L4_iadd_memopb_io :
2273 case Hexagon::L4_isub_memopb_io :
2274 case Hexagon::L4_add_memopb_io :
2275 case Hexagon::L4_sub_memopb_io :
2276 case Hexagon::L4_and_memopb_io :
2277 case Hexagon::L4_or_memopb_io :
2278 case Hexagon::L4_ior_memopb_io:
2279 case Hexagon::L4_ior_memoph_io:
2280 case Hexagon::L4_ior_memopw_io:
2281 case Hexagon::L4_iand_memopb_io:
2282 case Hexagon::L4_iand_memoph_io:
2283 case Hexagon::L4_iand_memopw_io:
2284 return true;
2285 }
2286 return false;
2287}
2288
2289
2290bool HexagonInstrInfo::isNewValue(const MachineInstr* MI) const {
2291 const uint64_t F = MI->getDesc().TSFlags;
2292 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2293}
2294
2295
2296bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2297 const uint64_t F = get(Opcode).TSFlags;
2298 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2299}
2300
2301
2302bool HexagonInstrInfo::isNewValueInst(const MachineInstr *MI) const {
2303 return isNewValueJump(MI) || isNewValueStore(MI);
2304}
2305
2306
2307bool HexagonInstrInfo::isNewValueJump(const MachineInstr *MI) const {
2308 return isNewValue(MI) && MI->isBranch();
2309}
2310
2311
2312bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2313 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2314}
2315
2316
2317bool HexagonInstrInfo::isNewValueStore(const MachineInstr *MI) const {
2318 const uint64_t F = MI->getDesc().TSFlags;
2319 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2320}
2321
2322
2323bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2324 const uint64_t F = get(Opcode).TSFlags;
2325 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2326}
2327
2328
2329// Returns true if a particular operand is extendable for an instruction.
2330bool HexagonInstrInfo::isOperandExtended(const MachineInstr *MI,
2331 unsigned OperandNum) const {
2332 const uint64_t F = MI->getDesc().TSFlags;
2333 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2334 == OperandNum;
2335}
2336
2337
2338bool HexagonInstrInfo::isPostIncrement(const MachineInstr* MI) const {
2339 return getAddrMode(MI) == HexagonII::PostInc;
2340}
2341
2342
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002343bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2344 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002345 assert(isPredicated(MI));
2346 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2347}
2348
2349
2350bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2351 const uint64_t F = get(Opcode).TSFlags;
2352 assert(isPredicated(Opcode));
2353 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2354}
2355
2356
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002357bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2358 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002359 return !((F >> HexagonII::PredicatedFalsePos) &
2360 HexagonII::PredicatedFalseMask);
2361}
2362
2363
2364bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2365 const uint64_t F = get(Opcode).TSFlags;
2366 // Make sure that the instruction is predicated.
2367 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2368 return !((F >> HexagonII::PredicatedFalsePos) &
2369 HexagonII::PredicatedFalseMask);
2370}
2371
2372
2373bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2374 const uint64_t F = get(Opcode).TSFlags;
2375 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2376}
2377
2378
2379bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2380 const uint64_t F = get(Opcode).TSFlags;
2381 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2382}
2383
2384
2385bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2386 const uint64_t F = get(Opcode).TSFlags;
2387 assert(get(Opcode).isBranch() &&
2388 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2389 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2390}
2391
2392
2393bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr *MI) const {
2394 return MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
Krzysztof Parzyszek181fdbd2016-03-24 19:18:48 +00002395 MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2396 MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2397 MI->getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002398}
2399
2400
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002401bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr* MI) const {
2402 switch (MI->getOpcode()) {
2403 // Byte
2404 case Hexagon::L2_loadrb_io:
2405 case Hexagon::L4_loadrb_ur:
2406 case Hexagon::L4_loadrb_ap:
2407 case Hexagon::L2_loadrb_pr:
2408 case Hexagon::L2_loadrb_pbr:
2409 case Hexagon::L2_loadrb_pi:
2410 case Hexagon::L2_loadrb_pci:
2411 case Hexagon::L2_loadrb_pcr:
2412 case Hexagon::L2_loadbsw2_io:
2413 case Hexagon::L4_loadbsw2_ur:
2414 case Hexagon::L4_loadbsw2_ap:
2415 case Hexagon::L2_loadbsw2_pr:
2416 case Hexagon::L2_loadbsw2_pbr:
2417 case Hexagon::L2_loadbsw2_pi:
2418 case Hexagon::L2_loadbsw2_pci:
2419 case Hexagon::L2_loadbsw2_pcr:
2420 case Hexagon::L2_loadbsw4_io:
2421 case Hexagon::L4_loadbsw4_ur:
2422 case Hexagon::L4_loadbsw4_ap:
2423 case Hexagon::L2_loadbsw4_pr:
2424 case Hexagon::L2_loadbsw4_pbr:
2425 case Hexagon::L2_loadbsw4_pi:
2426 case Hexagon::L2_loadbsw4_pci:
2427 case Hexagon::L2_loadbsw4_pcr:
2428 case Hexagon::L4_loadrb_rr:
2429 case Hexagon::L2_ploadrbt_io:
2430 case Hexagon::L2_ploadrbt_pi:
2431 case Hexagon::L2_ploadrbf_io:
2432 case Hexagon::L2_ploadrbf_pi:
2433 case Hexagon::L2_ploadrbtnew_io:
2434 case Hexagon::L2_ploadrbfnew_io:
2435 case Hexagon::L4_ploadrbt_rr:
2436 case Hexagon::L4_ploadrbf_rr:
2437 case Hexagon::L4_ploadrbtnew_rr:
2438 case Hexagon::L4_ploadrbfnew_rr:
2439 case Hexagon::L2_ploadrbtnew_pi:
2440 case Hexagon::L2_ploadrbfnew_pi:
2441 case Hexagon::L4_ploadrbt_abs:
2442 case Hexagon::L4_ploadrbf_abs:
2443 case Hexagon::L4_ploadrbtnew_abs:
2444 case Hexagon::L4_ploadrbfnew_abs:
2445 case Hexagon::L2_loadrbgp:
2446 // Half
2447 case Hexagon::L2_loadrh_io:
2448 case Hexagon::L4_loadrh_ur:
2449 case Hexagon::L4_loadrh_ap:
2450 case Hexagon::L2_loadrh_pr:
2451 case Hexagon::L2_loadrh_pbr:
2452 case Hexagon::L2_loadrh_pi:
2453 case Hexagon::L2_loadrh_pci:
2454 case Hexagon::L2_loadrh_pcr:
2455 case Hexagon::L4_loadrh_rr:
2456 case Hexagon::L2_ploadrht_io:
2457 case Hexagon::L2_ploadrht_pi:
2458 case Hexagon::L2_ploadrhf_io:
2459 case Hexagon::L2_ploadrhf_pi:
2460 case Hexagon::L2_ploadrhtnew_io:
2461 case Hexagon::L2_ploadrhfnew_io:
2462 case Hexagon::L4_ploadrht_rr:
2463 case Hexagon::L4_ploadrhf_rr:
2464 case Hexagon::L4_ploadrhtnew_rr:
2465 case Hexagon::L4_ploadrhfnew_rr:
2466 case Hexagon::L2_ploadrhtnew_pi:
2467 case Hexagon::L2_ploadrhfnew_pi:
2468 case Hexagon::L4_ploadrht_abs:
2469 case Hexagon::L4_ploadrhf_abs:
2470 case Hexagon::L4_ploadrhtnew_abs:
2471 case Hexagon::L4_ploadrhfnew_abs:
2472 case Hexagon::L2_loadrhgp:
2473 return true;
2474 default:
2475 return false;
2476 }
2477}
2478
2479
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002480bool HexagonInstrInfo::isSolo(const MachineInstr* MI) const {
2481 const uint64_t F = MI->getDesc().TSFlags;
2482 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2483}
2484
2485
2486bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr *MI) const {
2487 switch (MI->getOpcode()) {
2488 case Hexagon::STriw_pred :
2489 case Hexagon::LDriw_pred :
2490 return true;
2491 default:
2492 return false;
2493 }
2494}
2495
2496
2497// Returns true when SU has a timing class TC1.
2498bool HexagonInstrInfo::isTC1(const MachineInstr *MI) const {
2499 unsigned SchedClass = MI->getDesc().getSchedClass();
2500 switch (SchedClass) {
2501 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2502 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2503 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2504 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2505 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2506 //case Hexagon::Sched::M_tc_1_SLOT23:
2507 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2508 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2509 return true;
2510
2511 default:
2512 return false;
2513 }
2514}
2515
2516
2517bool HexagonInstrInfo::isTC2(const MachineInstr *MI) const {
2518 unsigned SchedClass = MI->getDesc().getSchedClass();
2519 switch (SchedClass) {
2520 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
2521 case Hexagon::Sched::ALU64_tc_2_SLOT23:
2522 case Hexagon::Sched::CR_tc_2_SLOT3:
2523 case Hexagon::Sched::M_tc_2_SLOT23:
2524 case Hexagon::Sched::S_2op_tc_2_SLOT23:
2525 case Hexagon::Sched::S_3op_tc_2_SLOT23:
2526 return true;
2527
2528 default:
2529 return false;
2530 }
2531}
2532
2533
2534bool HexagonInstrInfo::isTC2Early(const MachineInstr *MI) const {
2535 unsigned SchedClass = MI->getDesc().getSchedClass();
2536 switch (SchedClass) {
2537 case Hexagon::Sched::ALU32_2op_tc_2early_SLOT0123:
2538 case Hexagon::Sched::ALU32_3op_tc_2early_SLOT0123:
2539 case Hexagon::Sched::ALU64_tc_2early_SLOT23:
2540 case Hexagon::Sched::CR_tc_2early_SLOT23:
2541 case Hexagon::Sched::CR_tc_2early_SLOT3:
2542 case Hexagon::Sched::J_tc_2early_SLOT0123:
2543 case Hexagon::Sched::J_tc_2early_SLOT2:
2544 case Hexagon::Sched::J_tc_2early_SLOT23:
2545 case Hexagon::Sched::S_2op_tc_2early_SLOT23:
2546 case Hexagon::Sched::S_3op_tc_2early_SLOT23:
2547 return true;
2548
2549 default:
2550 return false;
2551 }
2552}
2553
2554
2555bool HexagonInstrInfo::isTC4x(const MachineInstr *MI) const {
2556 if (!MI)
2557 return false;
2558
2559 unsigned SchedClass = MI->getDesc().getSchedClass();
2560 return SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23;
2561}
2562
2563
2564bool HexagonInstrInfo::isV60VectorInstruction(const MachineInstr *MI) const {
2565 if (!MI)
2566 return false;
2567
2568 const uint64_t V = getType(MI);
2569 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2570}
2571
2572
2573// Check if the Offset is a valid auto-inc imm by Load/Store Type.
2574//
2575bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2576 if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2577 VT == MVT::v32i16 || VT == MVT::v64i8) {
2578 return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2579 Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2580 (Offset & 0x3f) == 0);
2581 }
2582 // 128B
2583 if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2584 VT == MVT::v64i16 || VT == MVT::v128i8) {
2585 return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2586 Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2587 (Offset & 0x7f) == 0);
2588 }
2589 if (VT == MVT::i64) {
2590 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2591 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2592 (Offset & 0x7) == 0);
2593 }
2594 if (VT == MVT::i32) {
2595 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2596 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2597 (Offset & 0x3) == 0);
2598 }
2599 if (VT == MVT::i16) {
2600 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2601 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2602 (Offset & 0x1) == 0);
2603 }
2604 if (VT == MVT::i8) {
2605 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2606 Offset <= Hexagon_MEMB_AUTOINC_MAX);
2607 }
2608 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002609}
2610
2611
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002612bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2613 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002614 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002615 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002616 // inserted to calculate the final address. Due to this reason, the function
2617 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002618 // We used to assert if the offset was not properly aligned, however,
2619 // there are cases where a misaligned pointer recast can cause this
2620 // problem, and we need to allow for it. The front end warns of such
2621 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002622
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002623 switch (Opcode) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002624 case Hexagon::STriq_pred_V6:
2625 case Hexagon::STriq_pred_vec_V6:
2626 case Hexagon::STriv_pseudo_V6:
2627 case Hexagon::STrivv_pseudo_V6:
2628 case Hexagon::LDriq_pred_V6:
2629 case Hexagon::LDriq_pred_vec_V6:
2630 case Hexagon::LDriv_pseudo_V6:
2631 case Hexagon::LDrivv_pseudo_V6:
2632 case Hexagon::LDrivv_indexed:
2633 case Hexagon::STrivv_indexed:
2634 case Hexagon::V6_vL32b_ai:
2635 case Hexagon::V6_vS32b_ai:
2636 case Hexagon::V6_vL32Ub_ai:
2637 case Hexagon::V6_vS32Ub_ai:
2638 return (Offset >= Hexagon_MEMV_OFFSET_MIN) &&
2639 (Offset <= Hexagon_MEMV_OFFSET_MAX);
2640
2641 case Hexagon::STriq_pred_V6_128B:
2642 case Hexagon::STriq_pred_vec_V6_128B:
2643 case Hexagon::STriv_pseudo_V6_128B:
2644 case Hexagon::STrivv_pseudo_V6_128B:
2645 case Hexagon::LDriq_pred_V6_128B:
2646 case Hexagon::LDriq_pred_vec_V6_128B:
2647 case Hexagon::LDriv_pseudo_V6_128B:
2648 case Hexagon::LDrivv_pseudo_V6_128B:
2649 case Hexagon::LDrivv_indexed_128B:
2650 case Hexagon::STrivv_indexed_128B:
2651 case Hexagon::V6_vL32b_ai_128B:
2652 case Hexagon::V6_vS32b_ai_128B:
2653 case Hexagon::V6_vL32Ub_ai_128B:
2654 case Hexagon::V6_vS32Ub_ai_128B:
2655 return (Offset >= Hexagon_MEMV_OFFSET_MIN_128B) &&
2656 (Offset <= Hexagon_MEMV_OFFSET_MAX_128B);
2657
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002658 case Hexagon::J2_loop0i:
2659 case Hexagon::J2_loop1i:
2660 return isUInt<10>(Offset);
2661 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002662
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002663 if (Extend)
2664 return true;
2665
2666 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002667 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002668 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002669 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2670 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2671
Colin LeMahieu947cd702014-12-23 20:44:59 +00002672 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002673 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002674 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2675 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2676
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002677 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002678 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002679 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002680 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2681 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2682
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002683 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002684 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002685 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002686 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2687 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2688
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002689 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002690 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2691 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2692
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002693 case Hexagon::L4_iadd_memopw_io :
2694 case Hexagon::L4_isub_memopw_io :
2695 case Hexagon::L4_add_memopw_io :
2696 case Hexagon::L4_sub_memopw_io :
2697 case Hexagon::L4_and_memopw_io :
2698 case Hexagon::L4_or_memopw_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002699 return (0 <= Offset && Offset <= 255);
2700
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002701 case Hexagon::L4_iadd_memoph_io :
2702 case Hexagon::L4_isub_memoph_io :
2703 case Hexagon::L4_add_memoph_io :
2704 case Hexagon::L4_sub_memoph_io :
2705 case Hexagon::L4_and_memoph_io :
2706 case Hexagon::L4_or_memoph_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002707 return (0 <= Offset && Offset <= 127);
2708
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002709 case Hexagon::L4_iadd_memopb_io :
2710 case Hexagon::L4_isub_memopb_io :
2711 case Hexagon::L4_add_memopb_io :
2712 case Hexagon::L4_sub_memopb_io :
2713 case Hexagon::L4_and_memopb_io :
2714 case Hexagon::L4_or_memopb_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002715 return (0 <= Offset && Offset <= 63);
2716
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002717 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002718 // any size. Later pass knows how to handle it.
2719 case Hexagon::STriw_pred:
2720 case Hexagon::LDriw_pred:
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00002721 case Hexagon::STriw_mod:
2722 case Hexagon::LDriw_mod:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002723 return true;
2724
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002725 case Hexagon::TFR_FI:
2726 case Hexagon::TFR_FIA:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002727 case Hexagon::INLINEASM:
2728 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002729
2730 case Hexagon::L2_ploadrbt_io:
2731 case Hexagon::L2_ploadrbf_io:
2732 case Hexagon::L2_ploadrubt_io:
2733 case Hexagon::L2_ploadrubf_io:
2734 case Hexagon::S2_pstorerbt_io:
2735 case Hexagon::S2_pstorerbf_io:
2736 case Hexagon::S4_storeirb_io:
2737 case Hexagon::S4_storeirbt_io:
2738 case Hexagon::S4_storeirbf_io:
2739 return isUInt<6>(Offset);
2740
2741 case Hexagon::L2_ploadrht_io:
2742 case Hexagon::L2_ploadrhf_io:
2743 case Hexagon::L2_ploadruht_io:
2744 case Hexagon::L2_ploadruhf_io:
2745 case Hexagon::S2_pstorerht_io:
2746 case Hexagon::S2_pstorerhf_io:
2747 case Hexagon::S4_storeirh_io:
2748 case Hexagon::S4_storeirht_io:
2749 case Hexagon::S4_storeirhf_io:
2750 return isShiftedUInt<6,1>(Offset);
2751
2752 case Hexagon::L2_ploadrit_io:
2753 case Hexagon::L2_ploadrif_io:
2754 case Hexagon::S2_pstorerit_io:
2755 case Hexagon::S2_pstorerif_io:
2756 case Hexagon::S4_storeiri_io:
2757 case Hexagon::S4_storeirit_io:
2758 case Hexagon::S4_storeirif_io:
2759 return isShiftedUInt<6,2>(Offset);
2760
2761 case Hexagon::L2_ploadrdt_io:
2762 case Hexagon::L2_ploadrdf_io:
2763 case Hexagon::S2_pstorerdt_io:
2764 case Hexagon::S2_pstorerdf_io:
2765 return isShiftedUInt<6,3>(Offset);
2766 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002767
Benjamin Kramerb6684012011-12-27 11:41:05 +00002768 llvm_unreachable("No offset range is defined for this opcode. "
2769 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002770}
2771
2772
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002773bool HexagonInstrInfo::isVecAcc(const MachineInstr *MI) const {
2774 return MI && isV60VectorInstruction(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002775}
2776
2777
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002778bool HexagonInstrInfo::isVecALU(const MachineInstr *MI) const {
2779 if (!MI)
Andrew Trickd06df962012-02-01 22:13:57 +00002780 return false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002781 const uint64_t F = get(MI->getOpcode()).TSFlags;
2782 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2783 return
2784 V == HexagonII::TypeCVI_VA ||
2785 V == HexagonII::TypeCVI_VA_DV;
2786}
Andrew Trickd06df962012-02-01 22:13:57 +00002787
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002788
2789bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr *ProdMI,
2790 const MachineInstr *ConsMI) const {
2791 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2792 return true;
2793
2794 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2795 return true;
2796
2797 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002798 return true;
2799
2800 return false;
2801}
Jyotsna Verma84256432013-03-01 17:37:13 +00002802
Jyotsna Verma84256432013-03-01 17:37:13 +00002803
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002804bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr* MI) const {
2805 switch (MI->getOpcode()) {
2806 // Byte
2807 case Hexagon::L2_loadrub_io:
2808 case Hexagon::L4_loadrub_ur:
2809 case Hexagon::L4_loadrub_ap:
2810 case Hexagon::L2_loadrub_pr:
2811 case Hexagon::L2_loadrub_pbr:
2812 case Hexagon::L2_loadrub_pi:
2813 case Hexagon::L2_loadrub_pci:
2814 case Hexagon::L2_loadrub_pcr:
2815 case Hexagon::L2_loadbzw2_io:
2816 case Hexagon::L4_loadbzw2_ur:
2817 case Hexagon::L4_loadbzw2_ap:
2818 case Hexagon::L2_loadbzw2_pr:
2819 case Hexagon::L2_loadbzw2_pbr:
2820 case Hexagon::L2_loadbzw2_pi:
2821 case Hexagon::L2_loadbzw2_pci:
2822 case Hexagon::L2_loadbzw2_pcr:
2823 case Hexagon::L2_loadbzw4_io:
2824 case Hexagon::L4_loadbzw4_ur:
2825 case Hexagon::L4_loadbzw4_ap:
2826 case Hexagon::L2_loadbzw4_pr:
2827 case Hexagon::L2_loadbzw4_pbr:
2828 case Hexagon::L2_loadbzw4_pi:
2829 case Hexagon::L2_loadbzw4_pci:
2830 case Hexagon::L2_loadbzw4_pcr:
2831 case Hexagon::L4_loadrub_rr:
2832 case Hexagon::L2_ploadrubt_io:
2833 case Hexagon::L2_ploadrubt_pi:
2834 case Hexagon::L2_ploadrubf_io:
2835 case Hexagon::L2_ploadrubf_pi:
2836 case Hexagon::L2_ploadrubtnew_io:
2837 case Hexagon::L2_ploadrubfnew_io:
2838 case Hexagon::L4_ploadrubt_rr:
2839 case Hexagon::L4_ploadrubf_rr:
2840 case Hexagon::L4_ploadrubtnew_rr:
2841 case Hexagon::L4_ploadrubfnew_rr:
2842 case Hexagon::L2_ploadrubtnew_pi:
2843 case Hexagon::L2_ploadrubfnew_pi:
2844 case Hexagon::L4_ploadrubt_abs:
2845 case Hexagon::L4_ploadrubf_abs:
2846 case Hexagon::L4_ploadrubtnew_abs:
2847 case Hexagon::L4_ploadrubfnew_abs:
2848 case Hexagon::L2_loadrubgp:
2849 // Half
2850 case Hexagon::L2_loadruh_io:
2851 case Hexagon::L4_loadruh_ur:
2852 case Hexagon::L4_loadruh_ap:
2853 case Hexagon::L2_loadruh_pr:
2854 case Hexagon::L2_loadruh_pbr:
2855 case Hexagon::L2_loadruh_pi:
2856 case Hexagon::L2_loadruh_pci:
2857 case Hexagon::L2_loadruh_pcr:
2858 case Hexagon::L4_loadruh_rr:
2859 case Hexagon::L2_ploadruht_io:
2860 case Hexagon::L2_ploadruht_pi:
2861 case Hexagon::L2_ploadruhf_io:
2862 case Hexagon::L2_ploadruhf_pi:
2863 case Hexagon::L2_ploadruhtnew_io:
2864 case Hexagon::L2_ploadruhfnew_io:
2865 case Hexagon::L4_ploadruht_rr:
2866 case Hexagon::L4_ploadruhf_rr:
2867 case Hexagon::L4_ploadruhtnew_rr:
2868 case Hexagon::L4_ploadruhfnew_rr:
2869 case Hexagon::L2_ploadruhtnew_pi:
2870 case Hexagon::L2_ploadruhfnew_pi:
2871 case Hexagon::L4_ploadruht_abs:
2872 case Hexagon::L4_ploadruhf_abs:
2873 case Hexagon::L4_ploadruhtnew_abs:
2874 case Hexagon::L4_ploadruhfnew_abs:
2875 case Hexagon::L2_loadruhgp:
2876 return true;
2877 default:
2878 return false;
2879 }
2880}
2881
2882
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002883/// \brief Can these instructions execute at the same time in a bundle.
2884bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr *First,
2885 const MachineInstr *Second) const {
2886 if (DisableNVSchedule)
2887 return false;
2888 if (mayBeNewStore(Second)) {
2889 // Make sure the definition of the first instruction is the value being
2890 // stored.
2891 const MachineOperand &Stored =
2892 Second->getOperand(Second->getNumOperands() - 1);
2893 if (!Stored.isReg())
2894 return false;
2895 for (unsigned i = 0, e = First->getNumOperands(); i < e; ++i) {
2896 const MachineOperand &Op = First->getOperand(i);
2897 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2898 return true;
2899 }
2900 }
2901 return false;
2902}
2903
2904
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002905bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2906 for (auto &I : *B)
2907 if (I.isEHLabel())
2908 return true;
2909 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002910}
2911
Jyotsna Verma84256432013-03-01 17:37:13 +00002912
2913// Returns true if an instruction can be converted into a non-extended
2914// equivalent instruction.
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002915bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr *MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002916 short NonExtOpcode;
2917 // Check if the instruction has a register form that uses register in place
2918 // of the extended operand, if so return that as the non-extended form.
2919 if (Hexagon::getRegForm(MI->getOpcode()) >= 0)
2920 return true;
2921
2922 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002923 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002924
2925 switch (getAddrMode(MI)) {
2926 case HexagonII::Absolute :
2927 // Load/store with absolute addressing mode can be converted into
2928 // base+offset mode.
Krzysztof Parzyszek02579052015-10-20 19:21:05 +00002929 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI->getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002930 break;
2931 case HexagonII::BaseImmOffset :
2932 // Load/store with base+offset addressing mode can be converted into
2933 // base+register offset addressing mode. However left shift operand should
2934 // be set to 0.
2935 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI->getOpcode());
2936 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002937 case HexagonII::BaseLongOffset:
2938 NonExtOpcode = Hexagon::getRegShlForm(MI->getOpcode());
2939 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002940 default:
2941 return false;
2942 }
2943 if (NonExtOpcode < 0)
2944 return false;
2945 return true;
2946 }
2947 return false;
2948}
2949
Jyotsna Verma84256432013-03-01 17:37:13 +00002950
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002951bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002952 return Hexagon::getRealHWInstr(MI->getOpcode(),
2953 Hexagon::InstrType_Pseudo) >= 0;
2954}
2955
2956
2957bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
2958 const {
2959 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
2960 while (I != E) {
2961 if (I->isBarrier())
2962 return true;
2963 ++I;
2964 }
2965 return false;
2966}
2967
2968
2969// Returns true, if a LD insn can be promoted to a cur load.
2970bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr *MI) const {
2971 auto &HST = MI->getParent()->getParent()->getSubtarget<HexagonSubtarget>();
2972 const uint64_t F = MI->getDesc().TSFlags;
2973 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
2974 HST.hasV60TOps();
2975}
2976
2977
2978// Returns true, if a ST insn can be promoted to a new-value store.
2979bool HexagonInstrInfo::mayBeNewStore(const MachineInstr *MI) const {
2980 const uint64_t F = MI->getDesc().TSFlags;
2981 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
2982}
2983
2984
2985bool HexagonInstrInfo::producesStall(const MachineInstr *ProdMI,
2986 const MachineInstr *ConsMI) const {
2987 // There is no stall when ProdMI is not a V60 vector.
2988 if (!isV60VectorInstruction(ProdMI))
2989 return false;
2990
2991 // There is no stall when ProdMI and ConsMI are not dependent.
2992 if (!isDependent(ProdMI, ConsMI))
2993 return false;
2994
2995 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
2996 // are scheduled in consecutive packets.
2997 if (isVecUsableNextPacket(ProdMI, ConsMI))
2998 return false;
2999
3000 return true;
3001}
3002
3003
3004bool HexagonInstrInfo::producesStall(const MachineInstr *MI,
3005 MachineBasicBlock::const_instr_iterator BII) const {
3006 // There is no stall when I is not a V60 vector.
3007 if (!isV60VectorInstruction(MI))
3008 return false;
3009
3010 MachineBasicBlock::const_instr_iterator MII = BII;
3011 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
3012
3013 if (!(*MII).isBundle()) {
3014 const MachineInstr *J = &*MII;
3015 if (!isV60VectorInstruction(J))
3016 return false;
3017 else if (isVecUsableNextPacket(J, MI))
3018 return false;
3019 return true;
3020 }
3021
3022 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
3023 const MachineInstr *J = &*MII;
3024 if (producesStall(J, MI))
3025 return true;
3026 }
3027 return false;
3028}
3029
3030
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003031bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr *MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003032 unsigned PredReg) const {
3033 for (unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++) {
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003034 const MachineOperand &MO = MI->getOperand(opNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003035 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
3036 return false; // Predicate register must be explicitly defined.
3037 }
3038
3039 // Hexagon Programmer's Reference says that decbin, memw_locked, and
3040 // memd_locked cannot be used as .new as well,
3041 // but we don't seem to have these instructions defined.
3042 return MI->getOpcode() != Hexagon::A4_tlbmatch;
3043}
3044
3045
3046bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
3047 return (Opcode == Hexagon::J2_jumpt) ||
3048 (Opcode == Hexagon::J2_jumpf) ||
3049 (Opcode == Hexagon::J2_jumptnew) ||
3050 (Opcode == Hexagon::J2_jumpfnew) ||
3051 (Opcode == Hexagon::J2_jumptnewpt) ||
3052 (Opcode == Hexagon::J2_jumpfnewpt);
3053}
3054
3055
3056bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
3057 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
3058 return false;
3059 return !isPredicatedTrue(Cond[0].getImm());
3060}
3061
3062
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003063short HexagonInstrInfo::getAbsoluteForm(const MachineInstr *MI) const {
3064 return Hexagon::getAbsoluteForm(MI->getOpcode());
3065}
3066
3067
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003068unsigned HexagonInstrInfo::getAddrMode(const MachineInstr* MI) const {
3069 const uint64_t F = MI->getDesc().TSFlags;
3070 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3071}
3072
3073
3074// Returns the base register in a memory access (load/store). The offset is
3075// returned in Offset and the access size is returned in AccessSize.
3076unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr *MI,
3077 int &Offset, unsigned &AccessSize) const {
3078 // Return if it is not a base+offset type instruction or a MemOp.
3079 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3080 getAddrMode(MI) != HexagonII::BaseLongOffset &&
3081 !isMemOp(MI) && !isPostIncrement(MI))
3082 return 0;
3083
3084 // Since it is a memory access instruction, getMemAccessSize() should never
3085 // return 0.
3086 assert (getMemAccessSize(MI) &&
3087 "BaseImmOffset or BaseLongOffset or MemOp without accessSize");
3088
3089 // Return Values of getMemAccessSize() are
3090 // 0 - Checked in the assert above.
3091 // 1, 2, 3, 4 & 7, 8 - The statement below is correct for all these.
3092 // MemAccessSize is represented as 1+log2(N) where N is size in bits.
3093 AccessSize = (1U << (getMemAccessSize(MI) - 1));
3094
3095 unsigned basePos = 0, offsetPos = 0;
3096 if (!getBaseAndOffsetPosition(MI, basePos, offsetPos))
3097 return 0;
3098
3099 // Post increment updates its EA after the mem access,
3100 // so we need to treat its offset as zero.
3101 if (isPostIncrement(MI))
3102 Offset = 0;
3103 else {
3104 Offset = MI->getOperand(offsetPos).getImm();
3105 }
3106
3107 return MI->getOperand(basePos).getReg();
3108}
3109
3110
3111/// Return the position of the base and offset operands for this instruction.
3112bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr *MI,
3113 unsigned &BasePos, unsigned &OffsetPos) const {
3114 // Deal with memops first.
3115 if (isMemOp(MI)) {
3116 assert (MI->getOperand(0).isReg() && MI->getOperand(1).isImm() &&
3117 "Bad Memop.");
3118 BasePos = 0;
3119 OffsetPos = 1;
3120 } else if (MI->mayStore()) {
3121 BasePos = 0;
3122 OffsetPos = 1;
3123 } else if (MI->mayLoad()) {
3124 BasePos = 1;
3125 OffsetPos = 2;
3126 } else
3127 return false;
3128
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003129 if (isPredicated(*MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003130 BasePos++;
3131 OffsetPos++;
3132 }
3133 if (isPostIncrement(MI)) {
3134 BasePos++;
3135 OffsetPos++;
3136 }
3137
3138 if (!MI->getOperand(BasePos).isReg() || !MI->getOperand(OffsetPos).isImm())
3139 return false;
3140
3141 return true;
3142}
3143
3144
3145// Inserts branching instructions in reverse order of their occurence.
3146// e.g. jump_t t1 (i1)
3147// jump t2 (i2)
3148// Jumpers = {i2, i1}
3149SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3150 MachineBasicBlock& MBB) const {
3151 SmallVector<MachineInstr*, 2> Jumpers;
3152 // If the block has no terminators, it just falls into the block after it.
3153 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3154 if (I == MBB.instr_begin())
3155 return Jumpers;
3156
3157 // A basic block may looks like this:
3158 //
3159 // [ insn
3160 // EH_LABEL
3161 // insn
3162 // insn
3163 // insn
3164 // EH_LABEL
3165 // insn ]
3166 //
3167 // It has two succs but does not have a terminator
3168 // Don't know how to handle it.
3169 do {
3170 --I;
3171 if (I->isEHLabel())
3172 return Jumpers;
3173 } while (I != MBB.instr_begin());
3174
3175 I = MBB.instr_end();
3176 --I;
3177
3178 while (I->isDebugValue()) {
3179 if (I == MBB.instr_begin())
3180 return Jumpers;
3181 --I;
3182 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003183 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003184 return Jumpers;
3185
3186 // Get the last instruction in the block.
3187 MachineInstr *LastInst = &*I;
3188 Jumpers.push_back(LastInst);
3189 MachineInstr *SecondLastInst = nullptr;
3190 // Find one more terminator if present.
3191 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003192 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003193 if (!SecondLastInst) {
3194 SecondLastInst = &*I;
3195 Jumpers.push_back(SecondLastInst);
3196 } else // This is a third branch.
3197 return Jumpers;
3198 }
3199 if (I == MBB.instr_begin())
3200 break;
3201 --I;
3202 } while (true);
3203 return Jumpers;
3204}
3205
3206
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003207short HexagonInstrInfo::getBaseWithLongOffset(short Opcode) const {
3208 if (Opcode < 0)
3209 return -1;
3210 return Hexagon::getBaseWithLongOffset(Opcode);
3211}
3212
3213
3214short HexagonInstrInfo::getBaseWithLongOffset(const MachineInstr *MI) const {
3215 return Hexagon::getBaseWithLongOffset(MI->getOpcode());
3216}
3217
3218
3219short HexagonInstrInfo::getBaseWithRegOffset(const MachineInstr *MI) const {
3220 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
3221}
3222
3223
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003224// Returns Operand Index for the constant extended instruction.
3225unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr *MI) const {
3226 const uint64_t F = MI->getDesc().TSFlags;
3227 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3228}
3229
3230// See if instruction could potentially be a duplex candidate.
3231// If so, return its group. Zero otherwise.
3232HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
3233 const MachineInstr *MI) const {
3234 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3235
3236 switch (MI->getOpcode()) {
3237 default:
3238 return HexagonII::HCG_None;
3239 //
3240 // Compound pairs.
3241 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3242 // "Rd16=#U6 ; jump #r9:2"
3243 // "Rd16=Rs16 ; jump #r9:2"
3244 //
3245 case Hexagon::C2_cmpeq:
3246 case Hexagon::C2_cmpgt:
3247 case Hexagon::C2_cmpgtu:
3248 DstReg = MI->getOperand(0).getReg();
3249 Src1Reg = MI->getOperand(1).getReg();
3250 Src2Reg = MI->getOperand(2).getReg();
3251 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3252 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3253 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3254 return HexagonII::HCG_A;
3255 break;
3256 case Hexagon::C2_cmpeqi:
3257 case Hexagon::C2_cmpgti:
3258 case Hexagon::C2_cmpgtui:
3259 // P0 = cmp.eq(Rs,#u2)
3260 DstReg = MI->getOperand(0).getReg();
3261 SrcReg = MI->getOperand(1).getReg();
3262 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3263 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3264 isIntRegForSubInst(SrcReg) && MI->getOperand(2).isImm() &&
3265 ((isUInt<5>(MI->getOperand(2).getImm())) ||
3266 (MI->getOperand(2).getImm() == -1)))
3267 return HexagonII::HCG_A;
3268 break;
3269 case Hexagon::A2_tfr:
3270 // Rd = Rs
3271 DstReg = MI->getOperand(0).getReg();
3272 SrcReg = MI->getOperand(1).getReg();
3273 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3274 return HexagonII::HCG_A;
3275 break;
3276 case Hexagon::A2_tfrsi:
3277 // Rd = #u6
3278 // Do not test for #u6 size since the const is getting extended
3279 // regardless and compound could be formed.
3280 DstReg = MI->getOperand(0).getReg();
3281 if (isIntRegForSubInst(DstReg))
3282 return HexagonII::HCG_A;
3283 break;
3284 case Hexagon::S2_tstbit_i:
3285 DstReg = MI->getOperand(0).getReg();
3286 Src1Reg = MI->getOperand(1).getReg();
3287 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3288 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3289 MI->getOperand(2).isImm() &&
3290 isIntRegForSubInst(Src1Reg) && (MI->getOperand(2).getImm() == 0))
3291 return HexagonII::HCG_A;
3292 break;
3293 // The fact that .new form is used pretty much guarantees
3294 // that predicate register will match. Nevertheless,
3295 // there could be some false positives without additional
3296 // checking.
3297 case Hexagon::J2_jumptnew:
3298 case Hexagon::J2_jumpfnew:
3299 case Hexagon::J2_jumptnewpt:
3300 case Hexagon::J2_jumpfnewpt:
3301 Src1Reg = MI->getOperand(0).getReg();
3302 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3303 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3304 return HexagonII::HCG_B;
3305 break;
3306 // Transfer and jump:
3307 // Rd=#U6 ; jump #r9:2
3308 // Rd=Rs ; jump #r9:2
3309 // Do not test for jump range here.
3310 case Hexagon::J2_jump:
3311 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3312 return HexagonII::HCG_C;
3313 break;
3314 }
3315
3316 return HexagonII::HCG_None;
3317}
3318
3319
3320// Returns -1 when there is no opcode found.
3321unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr *GA,
3322 const MachineInstr *GB) const {
3323 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3324 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
3325 if ((GA->getOpcode() != Hexagon::C2_cmpeqi) ||
3326 (GB->getOpcode() != Hexagon::J2_jumptnew))
3327 return -1;
3328 unsigned DestReg = GA->getOperand(0).getReg();
3329 if (!GB->readsRegister(DestReg))
3330 return -1;
3331 if (DestReg == Hexagon::P0)
3332 return Hexagon::J4_cmpeqi_tp0_jump_nt;
3333 if (DestReg == Hexagon::P1)
3334 return Hexagon::J4_cmpeqi_tp1_jump_nt;
3335 return -1;
3336}
3337
3338
3339int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3340 enum Hexagon::PredSense inPredSense;
3341 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3342 Hexagon::PredSense_true;
3343 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3344 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3345 return CondOpcode;
3346
3347 // This switch case will be removed once all the instructions have been
3348 // modified to use relation maps.
3349 switch(Opc) {
3350 case Hexagon::TFRI_f:
3351 return !invertPredicate ? Hexagon::TFRI_cPt_f :
3352 Hexagon::TFRI_cNotPt_f;
3353 }
3354
3355 llvm_unreachable("Unexpected predicable instruction");
3356}
3357
3358
3359// Return the cur value instruction for a given store.
3360int HexagonInstrInfo::getDotCurOp(const MachineInstr* MI) const {
3361 switch (MI->getOpcode()) {
3362 default: llvm_unreachable("Unknown .cur type");
3363 case Hexagon::V6_vL32b_pi:
3364 return Hexagon::V6_vL32b_cur_pi;
3365 case Hexagon::V6_vL32b_ai:
3366 return Hexagon::V6_vL32b_cur_ai;
3367 //128B
3368 case Hexagon::V6_vL32b_pi_128B:
3369 return Hexagon::V6_vL32b_cur_pi_128B;
3370 case Hexagon::V6_vL32b_ai_128B:
3371 return Hexagon::V6_vL32b_cur_ai_128B;
3372 }
3373 return 0;
3374}
3375
3376
3377
3378// The diagram below shows the steps involved in the conversion of a predicated
3379// store instruction to its .new predicated new-value form.
3380//
3381// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3382// ^ ^
3383// / \ (not OK. it will cause new-value store to be
3384// / X conditional on p0.new while R2 producer is
3385// / \ on p0)
3386// / \.
3387// p.new store p.old NV store
3388// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3389// ^ ^
3390// \ /
3391// \ /
3392// \ /
3393// p.old store
3394// [if (p0)memw(R0+#0)=R2]
3395//
3396//
3397// The following set of instructions further explains the scenario where
3398// conditional new-value store becomes invalid when promoted to .new predicate
3399// form.
3400//
3401// { 1) if (p0) r0 = add(r1, r2)
3402// 2) p0 = cmp.eq(r3, #0) }
3403//
3404// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3405// the first two instructions because in instr 1, r0 is conditional on old value
3406// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3407// is not valid for new-value stores.
3408// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3409// from the "Conditional Store" list. Because a predicated new value store
3410// would NOT be promoted to a double dot new store. See diagram below:
3411// This function returns yes for those stores that are predicated but not
3412// yet promoted to predicate dot new instructions.
3413//
3414// +---------------------+
3415// /-----| if (p0) memw(..)=r0 |---------\~
3416// || +---------------------+ ||
3417// promote || /\ /\ || promote
3418// || /||\ /||\ ||
3419// \||/ demote || \||/
3420// \/ || || \/
3421// +-------------------------+ || +-------------------------+
3422// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3423// +-------------------------+ || +-------------------------+
3424// || || ||
3425// || demote \||/
3426// promote || \/ NOT possible
3427// || || /\~
3428// \||/ || /||\~
3429// \/ || ||
3430// +-----------------------------+
3431// | if (p0.new) memw(..)=r0.new |
3432// +-----------------------------+
3433// Double Dot New Store
3434//
3435// Returns the most basic instruction for the .new predicated instructions and
3436// new-value stores.
3437// For example, all of the following instructions will be converted back to the
3438// same instruction:
3439// 1) if (p0.new) memw(R0+#0) = R1.new --->
3440// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3441// 3) if (p0.new) memw(R0+#0) = R1 --->
3442//
3443// To understand the translation of instruction 1 to its original form, consider
3444// a packet with 3 instructions.
3445// { p0 = cmp.eq(R0,R1)
3446// if (p0.new) R2 = add(R3, R4)
3447// R5 = add (R3, R1)
3448// }
3449// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3450//
3451// This instruction can be part of the previous packet only if both p0 and R2
3452// are promoted to .new values. This promotion happens in steps, first
3453// predicate register is promoted to .new and in the next iteration R2 is
3454// promoted. Therefore, in case of dependence check failure (due to R5) during
3455// next iteration, it should be converted back to its most basic form.
3456
3457
3458// Return the new value instruction for a given store.
3459int HexagonInstrInfo::getDotNewOp(const MachineInstr* MI) const {
3460 int NVOpcode = Hexagon::getNewValueOpcode(MI->getOpcode());
3461 if (NVOpcode >= 0) // Valid new-value store instruction.
3462 return NVOpcode;
3463
3464 switch (MI->getOpcode()) {
3465 default: llvm_unreachable("Unknown .new type");
3466 case Hexagon::S4_storerb_ur:
3467 return Hexagon::S4_storerbnew_ur;
3468
3469 case Hexagon::S2_storerb_pci:
3470 return Hexagon::S2_storerb_pci;
3471
3472 case Hexagon::S2_storeri_pci:
3473 return Hexagon::S2_storeri_pci;
3474
3475 case Hexagon::S2_storerh_pci:
3476 return Hexagon::S2_storerh_pci;
3477
3478 case Hexagon::S2_storerd_pci:
3479 return Hexagon::S2_storerd_pci;
3480
3481 case Hexagon::S2_storerf_pci:
3482 return Hexagon::S2_storerf_pci;
3483
3484 case Hexagon::V6_vS32b_ai:
3485 return Hexagon::V6_vS32b_new_ai;
3486
3487 case Hexagon::V6_vS32b_pi:
3488 return Hexagon::V6_vS32b_new_pi;
3489
3490 // 128B
3491 case Hexagon::V6_vS32b_ai_128B:
3492 return Hexagon::V6_vS32b_new_ai_128B;
3493
3494 case Hexagon::V6_vS32b_pi_128B:
3495 return Hexagon::V6_vS32b_new_pi_128B;
3496 }
3497 return 0;
3498}
3499
3500// Returns the opcode to use when converting MI, which is a conditional jump,
3501// into a conditional instruction which uses the .new value of the predicate.
3502// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003503int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr *MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003504 const MachineBranchProbabilityInfo *MBPI) const {
3505 // We assume that block can have at most two successors.
3506 bool taken = false;
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003507 const MachineBasicBlock *Src = MI->getParent();
3508 const MachineOperand *BrTarget = &MI->getOperand(1);
3509 const MachineBasicBlock *Dst = BrTarget->getMBB();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003510
3511 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
3512 if (Prediction >= BranchProbability(1,2))
3513 taken = true;
3514
3515 switch (MI->getOpcode()) {
3516 case Hexagon::J2_jumpt:
3517 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3518 case Hexagon::J2_jumpf:
3519 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3520
3521 default:
3522 llvm_unreachable("Unexpected jump instruction.");
3523 }
3524}
3525
3526
3527// Return .new predicate version for an instruction.
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003528int HexagonInstrInfo::getDotNewPredOp(const MachineInstr *MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003529 const MachineBranchProbabilityInfo *MBPI) const {
3530 int NewOpcode = Hexagon::getPredNewOpcode(MI->getOpcode());
3531 if (NewOpcode >= 0) // Valid predicate new instruction
3532 return NewOpcode;
3533
3534 switch (MI->getOpcode()) {
3535 // Condtional Jumps
3536 case Hexagon::J2_jumpt:
3537 case Hexagon::J2_jumpf:
3538 return getDotNewPredJumpOp(MI, MBPI);
3539
3540 default:
3541 assert(0 && "Unknown .new type");
3542 }
3543 return 0;
3544}
3545
3546
3547int HexagonInstrInfo::getDotOldOp(const int opc) const {
3548 int NewOp = opc;
3549 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3550 NewOp = Hexagon::getPredOldOpcode(NewOp);
3551 assert(NewOp >= 0 &&
3552 "Couldn't change predicate new instruction to its old form.");
3553 }
3554
3555 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3556 NewOp = Hexagon::getNonNVStore(NewOp);
3557 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3558 }
3559 return NewOp;
3560}
3561
3562
3563// See if instruction could potentially be a duplex candidate.
3564// If so, return its group. Zero otherwise.
3565HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
3566 const MachineInstr *MI) const {
3567 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3568 auto &HRI = getRegisterInfo();
3569
3570 switch (MI->getOpcode()) {
3571 default:
3572 return HexagonII::HSIG_None;
3573 //
3574 // Group L1:
3575 //
3576 // Rd = memw(Rs+#u4:2)
3577 // Rd = memub(Rs+#u4:0)
3578 case Hexagon::L2_loadri_io:
3579 DstReg = MI->getOperand(0).getReg();
3580 SrcReg = MI->getOperand(1).getReg();
3581 // Special case this one from Group L2.
3582 // Rd = memw(r29+#u5:2)
3583 if (isIntRegForSubInst(DstReg)) {
3584 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3585 HRI.getStackRegister() == SrcReg &&
3586 MI->getOperand(2).isImm() &&
3587 isShiftedUInt<5,2>(MI->getOperand(2).getImm()))
3588 return HexagonII::HSIG_L2;
3589 // Rd = memw(Rs+#u4:2)
3590 if (isIntRegForSubInst(SrcReg) &&
3591 (MI->getOperand(2).isImm() &&
3592 isShiftedUInt<4,2>(MI->getOperand(2).getImm())))
3593 return HexagonII::HSIG_L1;
3594 }
3595 break;
3596 case Hexagon::L2_loadrub_io:
3597 // Rd = memub(Rs+#u4:0)
3598 DstReg = MI->getOperand(0).getReg();
3599 SrcReg = MI->getOperand(1).getReg();
3600 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3601 MI->getOperand(2).isImm() && isUInt<4>(MI->getOperand(2).getImm()))
3602 return HexagonII::HSIG_L1;
3603 break;
3604 //
3605 // Group L2:
3606 //
3607 // Rd = memh/memuh(Rs+#u3:1)
3608 // Rd = memb(Rs+#u3:0)
3609 // Rd = memw(r29+#u5:2) - Handled above.
3610 // Rdd = memd(r29+#u5:3)
3611 // deallocframe
3612 // [if ([!]p0[.new])] dealloc_return
3613 // [if ([!]p0[.new])] jumpr r31
3614 case Hexagon::L2_loadrh_io:
3615 case Hexagon::L2_loadruh_io:
3616 // Rd = memh/memuh(Rs+#u3:1)
3617 DstReg = MI->getOperand(0).getReg();
3618 SrcReg = MI->getOperand(1).getReg();
3619 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3620 MI->getOperand(2).isImm() &&
3621 isShiftedUInt<3,1>(MI->getOperand(2).getImm()))
3622 return HexagonII::HSIG_L2;
3623 break;
3624 case Hexagon::L2_loadrb_io:
3625 // Rd = memb(Rs+#u3:0)
3626 DstReg = MI->getOperand(0).getReg();
3627 SrcReg = MI->getOperand(1).getReg();
3628 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3629 MI->getOperand(2).isImm() &&
3630 isUInt<3>(MI->getOperand(2).getImm()))
3631 return HexagonII::HSIG_L2;
3632 break;
3633 case Hexagon::L2_loadrd_io:
3634 // Rdd = memd(r29+#u5:3)
3635 DstReg = MI->getOperand(0).getReg();
3636 SrcReg = MI->getOperand(1).getReg();
3637 if (isDblRegForSubInst(DstReg, HRI) &&
3638 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3639 HRI.getStackRegister() == SrcReg &&
3640 MI->getOperand(2).isImm() &&
3641 isShiftedUInt<5,3>(MI->getOperand(2).getImm()))
3642 return HexagonII::HSIG_L2;
3643 break;
3644 // dealloc_return is not documented in Hexagon Manual, but marked
3645 // with A_SUBINSN attribute in iset_v4classic.py.
3646 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3647 case Hexagon::L4_return:
3648 case Hexagon::L2_deallocframe:
3649 return HexagonII::HSIG_L2;
3650 case Hexagon::EH_RETURN_JMPR:
3651 case Hexagon::JMPret :
3652 // jumpr r31
3653 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
3654 DstReg = MI->getOperand(0).getReg();
3655 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3656 return HexagonII::HSIG_L2;
3657 break;
3658 case Hexagon::JMPrett:
3659 case Hexagon::JMPretf:
3660 case Hexagon::JMPrettnewpt:
3661 case Hexagon::JMPretfnewpt :
3662 case Hexagon::JMPrettnew :
3663 case Hexagon::JMPretfnew :
3664 DstReg = MI->getOperand(1).getReg();
3665 SrcReg = MI->getOperand(0).getReg();
3666 // [if ([!]p0[.new])] jumpr r31
3667 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3668 (Hexagon::P0 == SrcReg)) &&
3669 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3670 return HexagonII::HSIG_L2;
3671 break;
3672 case Hexagon::L4_return_t :
3673 case Hexagon::L4_return_f :
3674 case Hexagon::L4_return_tnew_pnt :
3675 case Hexagon::L4_return_fnew_pnt :
3676 case Hexagon::L4_return_tnew_pt :
3677 case Hexagon::L4_return_fnew_pt :
3678 // [if ([!]p0[.new])] dealloc_return
3679 SrcReg = MI->getOperand(0).getReg();
3680 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3681 return HexagonII::HSIG_L2;
3682 break;
3683 //
3684 // Group S1:
3685 //
3686 // memw(Rs+#u4:2) = Rt
3687 // memb(Rs+#u4:0) = Rt
3688 case Hexagon::S2_storeri_io:
3689 // Special case this one from Group S2.
3690 // memw(r29+#u5:2) = Rt
3691 Src1Reg = MI->getOperand(0).getReg();
3692 Src2Reg = MI->getOperand(2).getReg();
3693 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3694 isIntRegForSubInst(Src2Reg) &&
3695 HRI.getStackRegister() == Src1Reg && MI->getOperand(1).isImm() &&
3696 isShiftedUInt<5,2>(MI->getOperand(1).getImm()))
3697 return HexagonII::HSIG_S2;
3698 // memw(Rs+#u4:2) = Rt
3699 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3700 MI->getOperand(1).isImm() &&
3701 isShiftedUInt<4,2>(MI->getOperand(1).getImm()))
3702 return HexagonII::HSIG_S1;
3703 break;
3704 case Hexagon::S2_storerb_io:
3705 // memb(Rs+#u4:0) = Rt
3706 Src1Reg = MI->getOperand(0).getReg();
3707 Src2Reg = MI->getOperand(2).getReg();
3708 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3709 MI->getOperand(1).isImm() && isUInt<4>(MI->getOperand(1).getImm()))
3710 return HexagonII::HSIG_S1;
3711 break;
3712 //
3713 // Group S2:
3714 //
3715 // memh(Rs+#u3:1) = Rt
3716 // memw(r29+#u5:2) = Rt
3717 // memd(r29+#s6:3) = Rtt
3718 // memw(Rs+#u4:2) = #U1
3719 // memb(Rs+#u4) = #U1
3720 // allocframe(#u5:3)
3721 case Hexagon::S2_storerh_io:
3722 // memh(Rs+#u3:1) = Rt
3723 Src1Reg = MI->getOperand(0).getReg();
3724 Src2Reg = MI->getOperand(2).getReg();
3725 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
3726 MI->getOperand(1).isImm() &&
3727 isShiftedUInt<3,1>(MI->getOperand(1).getImm()))
3728 return HexagonII::HSIG_S1;
3729 break;
3730 case Hexagon::S2_storerd_io:
3731 // memd(r29+#s6:3) = Rtt
3732 Src1Reg = MI->getOperand(0).getReg();
3733 Src2Reg = MI->getOperand(2).getReg();
3734 if (isDblRegForSubInst(Src2Reg, HRI) &&
3735 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3736 HRI.getStackRegister() == Src1Reg && MI->getOperand(1).isImm() &&
3737 isShiftedInt<6,3>(MI->getOperand(1).getImm()))
3738 return HexagonII::HSIG_S2;
3739 break;
3740 case Hexagon::S4_storeiri_io:
3741 // memw(Rs+#u4:2) = #U1
3742 Src1Reg = MI->getOperand(0).getReg();
3743 if (isIntRegForSubInst(Src1Reg) && MI->getOperand(1).isImm() &&
3744 isShiftedUInt<4,2>(MI->getOperand(1).getImm()) &&
3745 MI->getOperand(2).isImm() && isUInt<1>(MI->getOperand(2).getImm()))
3746 return HexagonII::HSIG_S2;
3747 break;
3748 case Hexagon::S4_storeirb_io:
3749 // memb(Rs+#u4) = #U1
3750 Src1Reg = MI->getOperand(0).getReg();
3751 if (isIntRegForSubInst(Src1Reg) && MI->getOperand(1).isImm() &&
3752 isUInt<4>(MI->getOperand(1).getImm()) && MI->getOperand(2).isImm() &&
3753 MI->getOperand(2).isImm() && isUInt<1>(MI->getOperand(2).getImm()))
3754 return HexagonII::HSIG_S2;
3755 break;
3756 case Hexagon::S2_allocframe:
3757 if (MI->getOperand(0).isImm() &&
3758 isShiftedUInt<5,3>(MI->getOperand(0).getImm()))
3759 return HexagonII::HSIG_S1;
3760 break;
3761 //
3762 // Group A:
3763 //
3764 // Rx = add(Rx,#s7)
3765 // Rd = Rs
3766 // Rd = #u6
3767 // Rd = #-1
3768 // if ([!]P0[.new]) Rd = #0
3769 // Rd = add(r29,#u6:2)
3770 // Rx = add(Rx,Rs)
3771 // P0 = cmp.eq(Rs,#u2)
3772 // Rdd = combine(#0,Rs)
3773 // Rdd = combine(Rs,#0)
3774 // Rdd = combine(#u2,#U2)
3775 // Rd = add(Rs,#1)
3776 // Rd = add(Rs,#-1)
3777 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3778 // Rd = and(Rs,#1)
3779 case Hexagon::A2_addi:
3780 DstReg = MI->getOperand(0).getReg();
3781 SrcReg = MI->getOperand(1).getReg();
3782 if (isIntRegForSubInst(DstReg)) {
3783 // Rd = add(r29,#u6:2)
3784 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3785 HRI.getStackRegister() == SrcReg && MI->getOperand(2).isImm() &&
3786 isShiftedUInt<6,2>(MI->getOperand(2).getImm()))
3787 return HexagonII::HSIG_A;
3788 // Rx = add(Rx,#s7)
3789 if ((DstReg == SrcReg) && MI->getOperand(2).isImm() &&
3790 isInt<7>(MI->getOperand(2).getImm()))
3791 return HexagonII::HSIG_A;
3792 // Rd = add(Rs,#1)
3793 // Rd = add(Rs,#-1)
3794 if (isIntRegForSubInst(SrcReg) && MI->getOperand(2).isImm() &&
3795 ((MI->getOperand(2).getImm() == 1) ||
3796 (MI->getOperand(2).getImm() == -1)))
3797 return HexagonII::HSIG_A;
3798 }
3799 break;
3800 case Hexagon::A2_add:
3801 // Rx = add(Rx,Rs)
3802 DstReg = MI->getOperand(0).getReg();
3803 Src1Reg = MI->getOperand(1).getReg();
3804 Src2Reg = MI->getOperand(2).getReg();
3805 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3806 isIntRegForSubInst(Src2Reg))
3807 return HexagonII::HSIG_A;
3808 break;
3809 case Hexagon::A2_andir:
3810 // Same as zxtb.
3811 // Rd16=and(Rs16,#255)
3812 // Rd16=and(Rs16,#1)
3813 DstReg = MI->getOperand(0).getReg();
3814 SrcReg = MI->getOperand(1).getReg();
3815 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
3816 MI->getOperand(2).isImm() &&
3817 ((MI->getOperand(2).getImm() == 1) ||
3818 (MI->getOperand(2).getImm() == 255)))
3819 return HexagonII::HSIG_A;
3820 break;
3821 case Hexagon::A2_tfr:
3822 // Rd = Rs
3823 DstReg = MI->getOperand(0).getReg();
3824 SrcReg = MI->getOperand(1).getReg();
3825 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3826 return HexagonII::HSIG_A;
3827 break;
3828 case Hexagon::A2_tfrsi:
3829 // Rd = #u6
3830 // Do not test for #u6 size since the const is getting extended
3831 // regardless and compound could be formed.
3832 // Rd = #-1
3833 DstReg = MI->getOperand(0).getReg();
3834 if (isIntRegForSubInst(DstReg))
3835 return HexagonII::HSIG_A;
3836 break;
3837 case Hexagon::C2_cmoveit:
3838 case Hexagon::C2_cmovenewit:
3839 case Hexagon::C2_cmoveif:
3840 case Hexagon::C2_cmovenewif:
3841 // if ([!]P0[.new]) Rd = #0
3842 // Actual form:
3843 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
3844 DstReg = MI->getOperand(0).getReg();
3845 SrcReg = MI->getOperand(1).getReg();
3846 if (isIntRegForSubInst(DstReg) &&
3847 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
3848 MI->getOperand(2).isImm() && MI->getOperand(2).getImm() == 0)
3849 return HexagonII::HSIG_A;
3850 break;
3851 case Hexagon::C2_cmpeqi:
3852 // P0 = cmp.eq(Rs,#u2)
3853 DstReg = MI->getOperand(0).getReg();
3854 SrcReg = MI->getOperand(1).getReg();
3855 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3856 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
3857 MI->getOperand(2).isImm() && isUInt<2>(MI->getOperand(2).getImm()))
3858 return HexagonII::HSIG_A;
3859 break;
3860 case Hexagon::A2_combineii:
3861 case Hexagon::A4_combineii:
3862 // Rdd = combine(#u2,#U2)
3863 DstReg = MI->getOperand(0).getReg();
3864 if (isDblRegForSubInst(DstReg, HRI) &&
3865 ((MI->getOperand(1).isImm() && isUInt<2>(MI->getOperand(1).getImm())) ||
3866 (MI->getOperand(1).isGlobal() &&
3867 isUInt<2>(MI->getOperand(1).getOffset()))) &&
3868 ((MI->getOperand(2).isImm() && isUInt<2>(MI->getOperand(2).getImm())) ||
3869 (MI->getOperand(2).isGlobal() &&
3870 isUInt<2>(MI->getOperand(2).getOffset()))))
3871 return HexagonII::HSIG_A;
3872 break;
3873 case Hexagon::A4_combineri:
3874 // Rdd = combine(Rs,#0)
3875 DstReg = MI->getOperand(0).getReg();
3876 SrcReg = MI->getOperand(1).getReg();
3877 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
3878 ((MI->getOperand(2).isImm() && MI->getOperand(2).getImm() == 0) ||
3879 (MI->getOperand(2).isGlobal() && MI->getOperand(2).getOffset() == 0)))
3880 return HexagonII::HSIG_A;
3881 break;
3882 case Hexagon::A4_combineir:
3883 // Rdd = combine(#0,Rs)
3884 DstReg = MI->getOperand(0).getReg();
3885 SrcReg = MI->getOperand(2).getReg();
3886 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
3887 ((MI->getOperand(1).isImm() && MI->getOperand(1).getImm() == 0) ||
3888 (MI->getOperand(1).isGlobal() && MI->getOperand(1).getOffset() == 0)))
3889 return HexagonII::HSIG_A;
3890 break;
3891 case Hexagon::A2_sxtb:
3892 case Hexagon::A2_sxth:
3893 case Hexagon::A2_zxtb:
3894 case Hexagon::A2_zxth:
3895 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3896 DstReg = MI->getOperand(0).getReg();
3897 SrcReg = MI->getOperand(1).getReg();
3898 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3899 return HexagonII::HSIG_A;
3900 break;
3901 }
3902
3903 return HexagonII::HSIG_None;
3904}
3905
3906
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00003907short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003908 return Hexagon::getRealHWInstr(MI->getOpcode(), Hexagon::InstrType_Real);
3909}
3910
3911
3912// Return first non-debug instruction in the basic block.
3913MachineInstr *HexagonInstrInfo::getFirstNonDbgInst(MachineBasicBlock *BB)
3914 const {
3915 for (auto MII = BB->instr_begin(), End = BB->instr_end(); MII != End; MII++) {
3916 MachineInstr *MI = &*MII;
3917 if (MI->isDebugValue())
3918 continue;
3919 return MI;
3920 }
3921 return nullptr;
3922}
3923
3924
3925unsigned HexagonInstrInfo::getInstrTimingClassLatency(
3926 const InstrItineraryData *ItinData, const MachineInstr *MI) const {
3927 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3928 // still have a MinLatency property, which getStageLatency checks.
3929 if (!ItinData)
3930 return getInstrLatency(ItinData, MI);
3931
3932 // Get the latency embedded in the itinerary. If we're not using timing class
3933 // latencies or if we using BSB scheduling, then restrict the maximum latency
3934 // to 1 (that is, either 0 or 1).
3935 if (MI->isTransient())
3936 return 0;
3937 unsigned Latency = ItinData->getStageLatency(MI->getDesc().getSchedClass());
3938 if (!EnableTimingClassLatency ||
3939 MI->getParent()->getParent()->getSubtarget<HexagonSubtarget>().
3940 useBSBScheduling())
3941 if (Latency > 1)
3942 Latency = 1;
3943 return Latency;
3944}
3945
3946
3947// inverts the predication logic.
3948// p -> NotP
3949// NotP -> P
3950bool HexagonInstrInfo::getInvertedPredSense(
3951 SmallVectorImpl<MachineOperand> &Cond) const {
3952 if (Cond.empty())
3953 return false;
3954 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
3955 Cond[0].setImm(Opc);
3956 return true;
3957}
3958
3959
3960unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
3961 int InvPredOpcode;
3962 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
3963 : Hexagon::getTruePredOpcode(Opc);
3964 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
3965 return InvPredOpcode;
3966
3967 llvm_unreachable("Unexpected predicated instruction");
3968}
3969
3970
3971// Returns the max value that doesn't need to be extended.
3972int HexagonInstrInfo::getMaxValue(const MachineInstr *MI) const {
3973 const uint64_t F = MI->getDesc().TSFlags;
3974 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3975 & HexagonII::ExtentSignedMask;
3976 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3977 & HexagonII::ExtentBitsMask;
3978
3979 if (isSigned) // if value is signed
3980 return ~(-1U << (bits - 1));
3981 else
3982 return ~(-1U << bits);
3983}
3984
3985
3986unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr* MI) const {
3987 const uint64_t F = MI->getDesc().TSFlags;
3988 return (F >> HexagonII::MemAccessSizePos) & HexagonII::MemAccesSizeMask;
3989}
3990
3991
3992// Returns the min value that doesn't need to be extended.
3993int HexagonInstrInfo::getMinValue(const MachineInstr *MI) const {
3994 const uint64_t F = MI->getDesc().TSFlags;
3995 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
3996 & HexagonII::ExtentSignedMask;
3997 unsigned bits = (F >> HexagonII::ExtentBitsPos)
3998 & HexagonII::ExtentBitsMask;
3999
4000 if (isSigned) // if value is signed
4001 return -1U << (bits - 1);
4002 else
4003 return 0;
4004}
4005
4006
4007// Returns opcode of the non-extended equivalent instruction.
4008short HexagonInstrInfo::getNonExtOpcode(const MachineInstr *MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00004009 // Check if the instruction has a register form that uses register in place
4010 // of the extended operand, if so return that as the non-extended form.
4011 short NonExtOpcode = Hexagon::getRegForm(MI->getOpcode());
4012 if (NonExtOpcode >= 0)
4013 return NonExtOpcode;
4014
4015 if (MI->getDesc().mayLoad() || MI->getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00004016 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00004017 switch (getAddrMode(MI)) {
4018 case HexagonII::Absolute :
Krzysztof Parzyszek02579052015-10-20 19:21:05 +00004019 return Hexagon::getBaseWithImmOffset(MI->getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004020 case HexagonII::BaseImmOffset :
4021 return Hexagon::getBaseWithRegOffset(MI->getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004022 case HexagonII::BaseLongOffset:
4023 return Hexagon::getRegShlForm(MI->getOpcode());
4024
Jyotsna Verma84256432013-03-01 17:37:13 +00004025 default:
4026 return -1;
4027 }
4028 }
4029 return -1;
4030}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00004031
Brendon Cahoondf43e682015-05-08 16:16:29 +00004032
Ahmed Bougachac88bf542015-06-11 19:30:37 +00004033bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004034 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00004035 if (Cond.empty())
4036 return false;
4037 assert(Cond.size() == 2);
4038 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
4039 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
4040 return false;
4041 }
4042 PredReg = Cond[1].getReg();
4043 PredRegPos = 1;
4044 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4045 PredRegFlags = 0;
4046 if (Cond[1].isImplicit())
4047 PredRegFlags = RegState::Implicit;
4048 if (Cond[1].isUndef())
4049 PredRegFlags |= RegState::Undef;
4050 return true;
4051}
4052
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004053
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00004054short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr *MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004055 return Hexagon::getRealHWInstr(MI->getOpcode(), Hexagon::InstrType_Pseudo);
4056}
4057
4058
4059short HexagonInstrInfo::getRegForm(const MachineInstr *MI) const {
4060 return Hexagon::getRegForm(MI->getOpcode());
4061}
4062
4063
4064// Return the number of bytes required to encode the instruction.
4065// Hexagon instructions are fixed length, 4 bytes, unless they
4066// use a constant extender, which requires another 4 bytes.
4067// For debug instructions and prolog labels, return 0.
4068unsigned HexagonInstrInfo::getSize(const MachineInstr *MI) const {
4069 if (MI->isDebugValue() || MI->isPosition())
4070 return 0;
4071
4072 unsigned Size = MI->getDesc().getSize();
4073 if (!Size)
4074 // Assume the default insn size in case it cannot be determined
4075 // for whatever reason.
4076 Size = HEXAGON_INSTR_SIZE;
4077
4078 if (isConstExtended(MI) || isExtended(MI))
4079 Size += HEXAGON_INSTR_SIZE;
4080
4081 // Try and compute number of instructions in asm.
4082 if (BranchRelaxAsmLarge && MI->getOpcode() == Hexagon::INLINEASM) {
4083 const MachineBasicBlock &MBB = *MI->getParent();
4084 const MachineFunction *MF = MBB.getParent();
4085 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4086
4087 // Count the number of register definitions to find the asm string.
4088 unsigned NumDefs = 0;
4089 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
4090 ++NumDefs)
4091 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
4092
4093 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
4094 // Disassemble the AsmStr and approximate number of instructions.
4095 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
4096 Size = getInlineAsmLength(AsmStr, *MAI);
4097 }
4098
4099 return Size;
4100}
4101
4102
4103uint64_t HexagonInstrInfo::getType(const MachineInstr* MI) const {
4104 const uint64_t F = MI->getDesc().TSFlags;
4105 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4106}
4107
4108
4109unsigned HexagonInstrInfo::getUnits(const MachineInstr* MI) const {
4110 const TargetSubtargetInfo &ST = MI->getParent()->getParent()->getSubtarget();
4111 const InstrItineraryData &II = *ST.getInstrItineraryData();
4112 const InstrStage &IS = *II.beginStage(MI->getDesc().getSchedClass());
4113
4114 return IS.getUnits();
4115}
4116
4117
4118unsigned HexagonInstrInfo::getValidSubTargets(const unsigned Opcode) const {
4119 const uint64_t F = get(Opcode).TSFlags;
4120 return (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask;
4121}
4122
4123
4124// Calculate size of the basic block without debug instructions.
4125unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4126 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4127}
4128
4129
4130unsigned HexagonInstrInfo::nonDbgBundleSize(
4131 MachineBasicBlock::const_iterator BundleHead) const {
4132 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004133 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004134 // Skip the bundle header.
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00004135 return nonDbgMICount(++MII, getBundleEnd(*BundleHead));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004136}
4137
4138
4139/// immediateExtend - Changes the instruction in place to one using an immediate
4140/// extender.
4141void HexagonInstrInfo::immediateExtend(MachineInstr *MI) const {
4142 assert((isExtendable(MI)||isConstExtended(MI)) &&
4143 "Instruction must be extendable");
4144 // Find which operand is extendable.
4145 short ExtOpNum = getCExtOpNum(MI);
4146 MachineOperand &MO = MI->getOperand(ExtOpNum);
4147 // This needs to be something we understand.
4148 assert((MO.isMBB() || MO.isImm()) &&
4149 "Branch with unknown extendable field type");
4150 // Mark given operand as extended.
4151 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4152}
4153
4154
4155bool HexagonInstrInfo::invertAndChangeJumpTarget(
4156 MachineInstr* MI, MachineBasicBlock* NewTarget) const {
4157 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
4158 << NewTarget->getNumber(); MI->dump(););
4159 assert(MI->isBranch());
4160 unsigned NewOpcode = getInvertedPredicatedOpcode(MI->getOpcode());
4161 int TargetPos = MI->getNumOperands() - 1;
4162 // In general branch target is the last operand,
4163 // but some implicit defs added at the end might change it.
4164 while ((TargetPos > -1) && !MI->getOperand(TargetPos).isMBB())
4165 --TargetPos;
4166 assert((TargetPos >= 0) && MI->getOperand(TargetPos).isMBB());
4167 MI->getOperand(TargetPos).setMBB(NewTarget);
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004168 if (EnableBranchPrediction && isPredicatedNew(*MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004169 NewOpcode = reversePrediction(NewOpcode);
4170 }
4171 MI->setDesc(get(NewOpcode));
4172 return true;
4173}
4174
4175
4176void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4177 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4178 MachineFunction::iterator A = MF.begin();
4179 MachineBasicBlock &B = *A;
4180 MachineBasicBlock::iterator I = B.begin();
4181 MachineInstr *MI = &*I;
4182 DebugLoc DL = MI->getDebugLoc();
4183 MachineInstr *NewMI;
4184
4185 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4186 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
4187 NewMI = BuildMI(B, MI, DL, get(insn));
4188 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
4189 " Class: " << NewMI->getDesc().getSchedClass());
4190 NewMI->eraseFromParent();
4191 }
4192 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4193}
4194
4195
4196// inverts the predication logic.
4197// p -> NotP
4198// NotP -> P
4199bool HexagonInstrInfo::reversePredSense(MachineInstr* MI) const {
4200 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI->dump());
4201 MI->setDesc(get(getInvertedPredicatedOpcode(MI->getOpcode())));
4202 return true;
4203}
4204
4205
4206// Reverse the branch prediction.
4207unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4208 int PredRevOpcode = -1;
4209 if (isPredictedTaken(Opcode))
4210 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4211 else
4212 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4213 assert(PredRevOpcode > 0);
4214 return PredRevOpcode;
4215}
4216
4217
4218// TODO: Add more rigorous validation.
4219bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4220 const {
4221 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4222}
4223
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004224
4225short HexagonInstrInfo::xformRegToImmOffset(const MachineInstr *MI) const {
4226 return Hexagon::xformRegToImmOffset(MI->getOpcode());
4227}