blob: c148b9caddcef44963d6985aaa50ca28283c6d4d [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonInstrInfo.cpp - Hexagon Instruction Information ------------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Hexagon implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000014#include "HexagonHazardRecognizer.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000015#include "HexagonInstrInfo.h"
Craig Topperb25fda92012-03-17 18:46:09 +000016#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000017#include "HexagonSubtarget.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000018#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SmallVector.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000020#include "llvm/CodeGen/DFAPacketizer.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000023#include "llvm/CodeGen/MachineMemOperand.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000025#include "llvm/CodeGen/PseudoSourceValue.h"
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000026#include "llvm/CodeGen/ScheduleDAG.h"
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000027#include "llvm/MC/MCAsmInfo.h"
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +000028#include "llvm/Support/CommandLine.h"
Jyotsna Verma5ed51812013-05-01 21:37:34 +000029#include "llvm/Support/Debug.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000030#include "llvm/Support/MathExtras.h"
Reid Kleckner1c76f1552013-05-03 00:54:56 +000031#include "llvm/Support/raw_ostream.h"
Krzysztof Parzyszekaa935752015-11-24 15:11:13 +000032#include <cctype>
Tony Linthicum1213a7a2011-12-12 21:14:40 +000033
Tony Linthicum1213a7a2011-12-12 21:14:40 +000034using namespace llvm;
35
Chandler Carruthe96dd892014-04-21 22:55:11 +000036#define DEBUG_TYPE "hexagon-instrinfo"
37
Chandler Carruthd174b722014-04-22 02:03:14 +000038#define GET_INSTRINFO_CTOR_DTOR
39#define GET_INSTRMAP_INFO
40#include "HexagonGenInstrInfo.inc"
41#include "HexagonGenDFAPacketizer.inc"
42
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000043cl::opt<bool> ScheduleInlineAsm("hexagon-sched-inline-asm", cl::Hidden,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000044 cl::init(false), cl::desc("Do not consider inline-asm a scheduling/"
45 "packetization boundary."));
46
47static cl::opt<bool> EnableBranchPrediction("hexagon-enable-branch-prediction",
48 cl::Hidden, cl::init(true), cl::desc("Enable branch prediction"));
49
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000050static cl::opt<bool> DisableNVSchedule("disable-hexagon-nv-schedule",
51 cl::Hidden, cl::ZeroOrMore, cl::init(false),
52 cl::desc("Disable schedule adjustment for new value stores."));
53
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000054static cl::opt<bool> EnableTimingClassLatency(
55 "enable-timing-class-latency", cl::Hidden, cl::init(false),
56 cl::desc("Enable timing class latency"));
57
58static cl::opt<bool> EnableALUForwarding(
59 "enable-alu-forwarding", cl::Hidden, cl::init(true),
60 cl::desc("Enable vec alu forwarding"));
61
62static cl::opt<bool> EnableACCForwarding(
63 "enable-acc-forwarding", cl::Hidden, cl::init(true),
64 cl::desc("Enable vec acc forwarding"));
65
66static cl::opt<bool> BranchRelaxAsmLarge("branch-relax-asm-large",
67 cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("branch relax asm"));
68
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +000069static cl::opt<bool> UseDFAHazardRec("dfa-hazard-rec",
70 cl::init(true), cl::Hidden, cl::ZeroOrMore,
71 cl::desc("Use the DFA based hazard recognizer."));
72
Tony Linthicum1213a7a2011-12-12 21:14:40 +000073///
74/// Constants for Hexagon instructions.
75///
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +000076const int Hexagon_MEMV_OFFSET_MAX_128B = 896; // #s4: -8*128...7*128
77const int Hexagon_MEMV_OFFSET_MIN_128B = -1024; // #s4
78const int Hexagon_MEMV_OFFSET_MAX = 448; // #s4: -8*64...7*64
79const int Hexagon_MEMV_OFFSET_MIN = -512; // #s4
Tony Linthicum1213a7a2011-12-12 21:14:40 +000080const int Hexagon_MEMW_OFFSET_MAX = 4095;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000081const int Hexagon_MEMW_OFFSET_MIN = -4096;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000082const int Hexagon_MEMD_OFFSET_MAX = 8191;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000083const int Hexagon_MEMD_OFFSET_MIN = -8192;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000084const int Hexagon_MEMH_OFFSET_MAX = 2047;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000085const int Hexagon_MEMH_OFFSET_MIN = -2048;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000086const int Hexagon_MEMB_OFFSET_MAX = 1023;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000087const int Hexagon_MEMB_OFFSET_MIN = -1024;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000088const int Hexagon_ADDI_OFFSET_MAX = 32767;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000089const int Hexagon_ADDI_OFFSET_MIN = -32768;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000090const int Hexagon_MEMD_AUTOINC_MAX = 56;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000091const int Hexagon_MEMD_AUTOINC_MIN = -64;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000092const int Hexagon_MEMW_AUTOINC_MAX = 28;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000093const int Hexagon_MEMW_AUTOINC_MIN = -32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000094const int Hexagon_MEMH_AUTOINC_MAX = 14;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000095const int Hexagon_MEMH_AUTOINC_MIN = -16;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000096const int Hexagon_MEMB_AUTOINC_MAX = 7;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000097const int Hexagon_MEMB_AUTOINC_MIN = -8;
Krzysztof Parzyszek6bd42682016-05-05 21:58:02 +000098const int Hexagon_MEMV_AUTOINC_MAX = 192; // #s3
99const int Hexagon_MEMV_AUTOINC_MIN = -256; // #s3
100const int Hexagon_MEMV_AUTOINC_MAX_128B = 384; // #s3
101const int Hexagon_MEMV_AUTOINC_MIN_128B = -512; // #s3
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000102
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000103// Pin the vtable to this file.
104void HexagonInstrInfo::anchor() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000105
106HexagonInstrInfo::HexagonInstrInfo(HexagonSubtarget &ST)
Eric Christopherc4d31402015-03-10 23:45:55 +0000107 : HexagonGenInstrInfo(Hexagon::ADJCALLSTACKDOWN, Hexagon::ADJCALLSTACKUP),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000108 RI() {}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000109
110
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000111static bool isIntRegForSubInst(unsigned Reg) {
112 return (Reg >= Hexagon::R0 && Reg <= Hexagon::R7) ||
113 (Reg >= Hexagon::R16 && Reg <= Hexagon::R23);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000114}
115
116
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000117static bool isDblRegForSubInst(unsigned Reg, const HexagonRegisterInfo &HRI) {
118 return isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::subreg_loreg)) &&
119 isIntRegForSubInst(HRI.getSubReg(Reg, Hexagon::subreg_hireg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000120}
121
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000122
123/// Calculate number of instructions excluding the debug instructions.
124static unsigned nonDbgMICount(MachineBasicBlock::const_instr_iterator MIB,
125 MachineBasicBlock::const_instr_iterator MIE) {
126 unsigned Count = 0;
127 for (; MIB != MIE; ++MIB) {
128 if (!MIB->isDebugValue())
129 ++Count;
130 }
131 return Count;
132}
133
134
135/// Find the hardware loop instruction used to set-up the specified loop.
136/// On Hexagon, we have two instructions used to set-up the hardware loop
137/// (LOOP0, LOOP1) with corresponding endloop (ENDLOOP0, ENDLOOP1) instructions
138/// to indicate the end of a loop.
139static MachineInstr *findLoopInstr(MachineBasicBlock *BB, int EndLoopOp,
140 SmallPtrSet<MachineBasicBlock *, 8> &Visited) {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000141 int LOOPi;
142 int LOOPr;
143 if (EndLoopOp == Hexagon::ENDLOOP0) {
144 LOOPi = Hexagon::J2_loop0i;
145 LOOPr = Hexagon::J2_loop0r;
146 } else { // EndLoopOp == Hexagon::EndLOOP1
147 LOOPi = Hexagon::J2_loop1i;
148 LOOPr = Hexagon::J2_loop1r;
149 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000150
Brendon Cahoondf43e682015-05-08 16:16:29 +0000151 // The loop set-up instruction will be in a predecessor block
152 for (MachineBasicBlock::pred_iterator PB = BB->pred_begin(),
153 PE = BB->pred_end(); PB != PE; ++PB) {
154 // If this has been visited, already skip it.
155 if (!Visited.insert(*PB).second)
156 continue;
157 if (*PB == BB)
158 continue;
159 for (MachineBasicBlock::reverse_instr_iterator I = (*PB)->instr_rbegin(),
160 E = (*PB)->instr_rend(); I != E; ++I) {
161 int Opc = I->getOpcode();
162 if (Opc == LOOPi || Opc == LOOPr)
163 return &*I;
164 // We've reached a different loop, which means the loop0 has been removed.
165 if (Opc == EndLoopOp)
166 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000167 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000168 // Check the predecessors for the LOOP instruction.
169 MachineInstr *loop = findLoopInstr(*PB, EndLoopOp, Visited);
170 if (loop)
171 return loop;
172 }
173 return 0;
174}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000175
Brendon Cahoondf43e682015-05-08 16:16:29 +0000176
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000177/// Gather register def/uses from MI.
178/// This treats possible (predicated) defs as actually happening ones
179/// (conservatively).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000180static inline void parseOperands(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000181 SmallVector<unsigned, 4> &Defs, SmallVector<unsigned, 8> &Uses) {
182 Defs.clear();
183 Uses.clear();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000184
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000185 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
186 const MachineOperand &MO = MI.getOperand(i);
Brendon Cahoondf43e682015-05-08 16:16:29 +0000187
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000188 if (!MO.isReg())
189 continue;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000190
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000191 unsigned Reg = MO.getReg();
192 if (!Reg)
193 continue;
194
195 if (MO.isUse())
196 Uses.push_back(MO.getReg());
197
198 if (MO.isDef())
199 Defs.push_back(MO.getReg());
200 }
201}
202
203
204// Position dependent, so check twice for swap.
205static bool isDuplexPairMatch(unsigned Ga, unsigned Gb) {
206 switch (Ga) {
207 case HexagonII::HSIG_None:
208 default:
209 return false;
210 case HexagonII::HSIG_L1:
211 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_A);
212 case HexagonII::HSIG_L2:
213 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
214 Gb == HexagonII::HSIG_A);
215 case HexagonII::HSIG_S1:
216 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
217 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_A);
218 case HexagonII::HSIG_S2:
219 return (Gb == HexagonII::HSIG_L1 || Gb == HexagonII::HSIG_L2 ||
220 Gb == HexagonII::HSIG_S1 || Gb == HexagonII::HSIG_S2 ||
221 Gb == HexagonII::HSIG_A);
222 case HexagonII::HSIG_A:
223 return (Gb == HexagonII::HSIG_A);
224 case HexagonII::HSIG_Compound:
225 return (Gb == HexagonII::HSIG_Compound);
226 }
227 return false;
228}
229
230
231
232/// isLoadFromStackSlot - If the specified machine instruction is a direct
233/// load from a stack slot, return the virtual or physical register number of
234/// the destination along with the FrameIndex of the loaded stack slot. If
235/// not, return 0. This predicate must return 0 if the instruction has
236/// any side effects other than loading from the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000237unsigned HexagonInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000238 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000239 switch (MI.getOpcode()) {
240 default:
241 break;
242 case Hexagon::L2_loadrb_io:
243 case Hexagon::L2_loadrub_io:
244 case Hexagon::L2_loadrh_io:
245 case Hexagon::L2_loadruh_io:
246 case Hexagon::L2_loadri_io:
247 case Hexagon::L2_loadrd_io:
248 case Hexagon::V6_vL32b_ai:
249 case Hexagon::V6_vL32b_ai_128B:
250 case Hexagon::V6_vL32Ub_ai:
251 case Hexagon::V6_vL32Ub_ai_128B:
252 case Hexagon::LDriw_pred:
253 case Hexagon::LDriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000254 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000255 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000256 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000257 case Hexagon::PS_vloadrw_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000258 const MachineOperand OpFI = MI.getOperand(1);
259 if (!OpFI.isFI())
260 return 0;
261 const MachineOperand OpOff = MI.getOperand(2);
262 if (!OpOff.isImm() || OpOff.getImm() != 0)
263 return 0;
264 FrameIndex = OpFI.getIndex();
265 return MI.getOperand(0).getReg();
266 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000267
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000268 case Hexagon::L2_ploadrbt_io:
269 case Hexagon::L2_ploadrbf_io:
270 case Hexagon::L2_ploadrubt_io:
271 case Hexagon::L2_ploadrubf_io:
272 case Hexagon::L2_ploadrht_io:
273 case Hexagon::L2_ploadrhf_io:
274 case Hexagon::L2_ploadruht_io:
275 case Hexagon::L2_ploadruhf_io:
276 case Hexagon::L2_ploadrit_io:
277 case Hexagon::L2_ploadrif_io:
278 case Hexagon::L2_ploadrdt_io:
279 case Hexagon::L2_ploadrdf_io: {
280 const MachineOperand OpFI = MI.getOperand(2);
281 if (!OpFI.isFI())
282 return 0;
283 const MachineOperand OpOff = MI.getOperand(3);
284 if (!OpOff.isImm() || OpOff.getImm() != 0)
285 return 0;
286 FrameIndex = OpFI.getIndex();
287 return MI.getOperand(0).getReg();
288 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000289 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000290
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000291 return 0;
292}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000293
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000294
295/// isStoreToStackSlot - If the specified machine instruction is a direct
296/// store to a stack slot, return the virtual or physical register number of
297/// the source reg along with the FrameIndex of the loaded stack slot. If
298/// not, return 0. This predicate must return 0 if the instruction has
299/// any side effects other than storing to the stack slot.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000300unsigned HexagonInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000301 int &FrameIndex) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000302 switch (MI.getOpcode()) {
303 default:
304 break;
305 case Hexagon::S2_storerb_io:
306 case Hexagon::S2_storerh_io:
307 case Hexagon::S2_storeri_io:
308 case Hexagon::S2_storerd_io:
309 case Hexagon::V6_vS32b_ai:
310 case Hexagon::V6_vS32b_ai_128B:
311 case Hexagon::V6_vS32Ub_ai:
312 case Hexagon::V6_vS32Ub_ai_128B:
313 case Hexagon::STriw_pred:
314 case Hexagon::STriw_mod:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000315 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000316 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000317 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000318 case Hexagon::PS_vstorerw_ai_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000319 const MachineOperand &OpFI = MI.getOperand(0);
320 if (!OpFI.isFI())
321 return 0;
322 const MachineOperand &OpOff = MI.getOperand(1);
323 if (!OpOff.isImm() || OpOff.getImm() != 0)
324 return 0;
325 FrameIndex = OpFI.getIndex();
326 return MI.getOperand(2).getReg();
327 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000328
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000329 case Hexagon::S2_pstorerbt_io:
330 case Hexagon::S2_pstorerbf_io:
331 case Hexagon::S2_pstorerht_io:
332 case Hexagon::S2_pstorerhf_io:
333 case Hexagon::S2_pstorerit_io:
334 case Hexagon::S2_pstorerif_io:
335 case Hexagon::S2_pstorerdt_io:
336 case Hexagon::S2_pstorerdf_io: {
337 const MachineOperand &OpFI = MI.getOperand(1);
338 if (!OpFI.isFI())
339 return 0;
340 const MachineOperand &OpOff = MI.getOperand(2);
341 if (!OpOff.isImm() || OpOff.getImm() != 0)
342 return 0;
343 FrameIndex = OpFI.getIndex();
344 return MI.getOperand(3).getReg();
345 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000346 }
Krzysztof Parzyszekfeb65a32016-02-12 20:54:15 +0000347
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000348 return 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000349}
350
351
Brendon Cahoondf43e682015-05-08 16:16:29 +0000352/// This function can analyze one/two way branching only and should (mostly) be
353/// called by target independent side.
354/// First entry is always the opcode of the branching instruction, except when
355/// the Cond vector is supposed to be empty, e.g., when AnalyzeBranch fails, a
356/// BB with only unconditional jump. Subsequent entries depend upon the opcode,
357/// e.g. Jump_c p will have
358/// Cond[0] = Jump_c
359/// Cond[1] = p
360/// HW-loop ENDLOOP:
361/// Cond[0] = ENDLOOP
362/// Cond[1] = MBB
363/// New value jump:
364/// Cond[0] = Hexagon::CMPEQri_f_Jumpnv_t_V4 -- specific opcode
365/// Cond[1] = R
366/// Cond[2] = Imm
Brendon Cahoondf43e682015-05-08 16:16:29 +0000367///
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000368bool HexagonInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000369 MachineBasicBlock *&TBB,
Brendon Cahoondf43e682015-05-08 16:16:29 +0000370 MachineBasicBlock *&FBB,
371 SmallVectorImpl<MachineOperand> &Cond,
372 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000373 TBB = nullptr;
374 FBB = nullptr;
Brendon Cahoondf43e682015-05-08 16:16:29 +0000375 Cond.clear();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000376
377 // If the block has no terminators, it just falls into the block after it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000378 MachineBasicBlock::instr_iterator I = MBB.instr_end();
379 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000380 return false;
381
382 // A basic block may looks like this:
383 //
384 // [ insn
385 // EH_LABEL
386 // insn
387 // insn
388 // insn
389 // EH_LABEL
390 // insn ]
391 //
392 // It has two succs but does not have a terminator
393 // Don't know how to handle it.
394 do {
395 --I;
396 if (I->isEHLabel())
Brendon Cahoondf43e682015-05-08 16:16:29 +0000397 // Don't analyze EH branches.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000398 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000399 } while (I != MBB.instr_begin());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000400
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000401 I = MBB.instr_end();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000402 --I;
403
404 while (I->isDebugValue()) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000405 if (I == MBB.instr_begin())
406 return false;
407 --I;
408 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000409
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000410 bool JumpToBlock = I->getOpcode() == Hexagon::J2_jump &&
411 I->getOperand(0).isMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000412 // Delete the J2_jump if it's equivalent to a fall-through.
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000413 if (AllowModify && JumpToBlock &&
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000414 MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
415 DEBUG(dbgs()<< "\nErasing the jump to successor block\n";);
416 I->eraseFromParent();
417 I = MBB.instr_end();
418 if (I == MBB.instr_begin())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000419 return false;
420 --I;
421 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000422 if (!isUnpredicatedTerminator(*I))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000423 return false;
424
425 // Get the last instruction in the block.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000426 MachineInstr *LastInst = &*I;
Craig Topper062a2ba2014-04-25 05:30:21 +0000427 MachineInstr *SecondLastInst = nullptr;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000428 // Find one more terminator if present.
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000429 for (;;) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000430 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000431 if (!SecondLastInst)
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000432 SecondLastInst = &*I;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000433 else
434 // This is a third branch.
435 return true;
436 }
437 if (I == MBB.instr_begin())
438 break;
439 --I;
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000440 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000441
442 int LastOpcode = LastInst->getOpcode();
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000443 int SecLastOpcode = SecondLastInst ? SecondLastInst->getOpcode() : 0;
444 // If the branch target is not a basic block, it could be a tail call.
445 // (It is, if the target is a function.)
446 if (LastOpcode == Hexagon::J2_jump && !LastInst->getOperand(0).isMBB())
447 return true;
448 if (SecLastOpcode == Hexagon::J2_jump &&
449 !SecondLastInst->getOperand(0).isMBB())
450 return true;
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000451
452 bool LastOpcodeHasJMP_c = PredOpcodeHasJMP_c(LastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000453 bool LastOpcodeHasNVJump = isNewValueJump(*LastInst);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000454
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000455 if (LastOpcodeHasJMP_c && !LastInst->getOperand(1).isMBB())
456 return true;
457
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000458 // If there is only one terminator instruction, process it.
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000459 if (LastInst && !SecondLastInst) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000460 if (LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000461 TBB = LastInst->getOperand(0).getMBB();
462 return false;
463 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000464 if (isEndLoopN(LastOpcode)) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000465 TBB = LastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000466 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000467 Cond.push_back(LastInst->getOperand(0));
468 return false;
469 }
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000470 if (LastOpcodeHasJMP_c) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000471 TBB = LastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000472 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000473 Cond.push_back(LastInst->getOperand(0));
474 return false;
475 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000476 // Only supporting rr/ri versions of new-value jumps.
477 if (LastOpcodeHasNVJump && (LastInst->getNumExplicitOperands() == 3)) {
478 TBB = LastInst->getOperand(2).getMBB();
479 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
480 Cond.push_back(LastInst->getOperand(0));
481 Cond.push_back(LastInst->getOperand(1));
482 return false;
483 }
484 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
485 << " with one jump\n";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000486 // Otherwise, don't know what this is.
487 return true;
488 }
489
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000490 bool SecLastOpcodeHasJMP_c = PredOpcodeHasJMP_c(SecLastOpcode);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000491 bool SecLastOpcodeHasNVJump = isNewValueJump(*SecondLastInst);
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000492 if (SecLastOpcodeHasJMP_c && (LastOpcode == Hexagon::J2_jump)) {
Krzysztof Parzyszekb28ae102016-01-14 15:05:27 +0000493 if (!SecondLastInst->getOperand(1).isMBB())
494 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000495 TBB = SecondLastInst->getOperand(1).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000496 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000497 Cond.push_back(SecondLastInst->getOperand(0));
498 FBB = LastInst->getOperand(0).getMBB();
499 return false;
500 }
501
Brendon Cahoondf43e682015-05-08 16:16:29 +0000502 // Only supporting rr/ri versions of new-value jumps.
503 if (SecLastOpcodeHasNVJump &&
504 (SecondLastInst->getNumExplicitOperands() == 3) &&
505 (LastOpcode == Hexagon::J2_jump)) {
506 TBB = SecondLastInst->getOperand(2).getMBB();
507 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
508 Cond.push_back(SecondLastInst->getOperand(0));
509 Cond.push_back(SecondLastInst->getOperand(1));
510 FBB = LastInst->getOperand(0).getMBB();
511 return false;
512 }
513
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000514 // If the block ends with two Hexagon:JMPs, handle it. The second one is not
515 // executed, so remove it.
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000516 if (SecLastOpcode == Hexagon::J2_jump && LastOpcode == Hexagon::J2_jump) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000517 TBB = SecondLastInst->getOperand(0).getMBB();
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000518 I = LastInst->getIterator();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000519 if (AllowModify)
520 I->eraseFromParent();
521 return false;
522 }
523
Brendon Cahoondf43e682015-05-08 16:16:29 +0000524 // If the block ends with an ENDLOOP, and J2_jump, handle it.
525 if (isEndLoopN(SecLastOpcode) && LastOpcode == Hexagon::J2_jump) {
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000526 TBB = SecondLastInst->getOperand(0).getMBB();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000527 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
Jyotsna Verma5ed51812013-05-01 21:37:34 +0000528 Cond.push_back(SecondLastInst->getOperand(0));
529 FBB = LastInst->getOperand(0).getMBB();
530 return false;
531 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000532 DEBUG(dbgs() << "\nCant analyze BB#" << MBB.getNumber()
533 << " with two jumps";);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000534 // Otherwise, can't handle this.
535 return true;
536}
537
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000538
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000539unsigned HexagonInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +0000540 DEBUG(dbgs() << "\nRemoving branches out of BB#" << MBB.getNumber());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000541 MachineBasicBlock::iterator I = MBB.end();
Brendon Cahoondf43e682015-05-08 16:16:29 +0000542 unsigned Count = 0;
543 while (I != MBB.begin()) {
544 --I;
545 if (I->isDebugValue())
546 continue;
547 // Only removing branches from end of MBB.
548 if (!I->isBranch())
549 return Count;
550 if (Count && (I->getOpcode() == Hexagon::J2_jump))
551 llvm_unreachable("Malformed basic block: unconditional branch not last");
552 MBB.erase(&MBB.back());
553 I = MBB.end();
554 ++Count;
Krzysztof Parzyszek78cc36f2015-03-18 15:56:43 +0000555 }
Brendon Cahoondf43e682015-05-08 16:16:29 +0000556 return Count;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000557}
558
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000559unsigned HexagonInstrInfo::InsertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000560 MachineBasicBlock *TBB,
561 MachineBasicBlock *FBB,
562 ArrayRef<MachineOperand> Cond,
563 const DebugLoc &DL) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000564 unsigned BOpc = Hexagon::J2_jump;
565 unsigned BccOpc = Hexagon::J2_jumpt;
566 assert(validateBranchCond(Cond) && "Invalid branching condition");
567 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
568
569 // Check if ReverseBranchCondition has asked to reverse this branch
570 // If we want to reverse the branch an odd number of times, we want
571 // J2_jumpf.
572 if (!Cond.empty() && Cond[0].isImm())
573 BccOpc = Cond[0].getImm();
574
575 if (!FBB) {
576 if (Cond.empty()) {
577 // Due to a bug in TailMerging/CFG Optimization, we need to add a
578 // special case handling of a predicated jump followed by an
579 // unconditional jump. If not, Tail Merging and CFG Optimization go
580 // into an infinite loop.
581 MachineBasicBlock *NewTBB, *NewFBB;
582 SmallVector<MachineOperand, 4> Cond;
Duncan P. N. Exon Smith25b132e2016-07-08 18:26:20 +0000583 auto Term = MBB.getFirstTerminator();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000584 if (Term != MBB.end() && isPredicated(*Term) &&
Duncan P. N. Exon Smithe04fe1a2016-08-17 00:34:00 +0000585 !analyzeBranch(MBB, NewTBB, NewFBB, Cond, false) &&
586 MachineFunction::iterator(NewTBB) == ++MBB.getIterator()) {
587 ReverseBranchCondition(Cond);
588 RemoveBranch(MBB);
589 return InsertBranch(MBB, TBB, nullptr, Cond, DL);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000590 }
591 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
592 } else if (isEndLoopN(Cond[0].getImm())) {
593 int EndLoopOp = Cond[0].getImm();
594 assert(Cond[1].isMBB());
595 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
596 // Check for it, and change the BB target if needed.
597 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
598 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
599 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
600 Loop->getOperand(0).setMBB(TBB);
601 // Add the ENDLOOP after the finding the LOOP0.
602 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
603 } else if (isNewValueJump(Cond[0].getImm())) {
604 assert((Cond.size() == 3) && "Only supporting rr/ri version of nvjump");
605 // New value jump
606 // (ins IntRegs:$src1, IntRegs:$src2, brtarget:$offset)
607 // (ins IntRegs:$src1, u5Imm:$src2, brtarget:$offset)
608 unsigned Flags1 = getUndefRegState(Cond[1].isUndef());
609 DEBUG(dbgs() << "\nInserting NVJump for BB#" << MBB.getNumber(););
610 if (Cond[2].isReg()) {
611 unsigned Flags2 = getUndefRegState(Cond[2].isUndef());
612 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
613 addReg(Cond[2].getReg(), Flags2).addMBB(TBB);
614 } else if(Cond[2].isImm()) {
615 BuildMI(&MBB, DL, get(BccOpc)).addReg(Cond[1].getReg(), Flags1).
616 addImm(Cond[2].getImm()).addMBB(TBB);
617 } else
618 llvm_unreachable("Invalid condition for branching");
619 } else {
620 assert((Cond.size() == 2) && "Malformed cond vector");
621 const MachineOperand &RO = Cond[1];
622 unsigned Flags = getUndefRegState(RO.isUndef());
623 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
624 }
625 return 1;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000626 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000627 assert((!Cond.empty()) &&
628 "Cond. cannot be empty when multiple branchings are required");
629 assert((!isNewValueJump(Cond[0].getImm())) &&
630 "NV-jump cannot be inserted with another branch");
631 // Special case for hardware loops. The condition is a basic block.
632 if (isEndLoopN(Cond[0].getImm())) {
633 int EndLoopOp = Cond[0].getImm();
634 assert(Cond[1].isMBB());
635 // Since we're adding an ENDLOOP, there better be a LOOP instruction.
636 // Check for it, and change the BB target if needed.
637 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
638 MachineInstr *Loop = findLoopInstr(TBB, EndLoopOp, VisitedBBs);
639 assert(Loop != 0 && "Inserting an ENDLOOP without a LOOP");
640 Loop->getOperand(0).setMBB(TBB);
641 // Add the ENDLOOP after the finding the LOOP0.
642 BuildMI(&MBB, DL, get(EndLoopOp)).addMBB(TBB);
643 } else {
644 const MachineOperand &RO = Cond[1];
645 unsigned Flags = getUndefRegState(RO.isUndef());
646 BuildMI(&MBB, DL, get(BccOpc)).addReg(RO.getReg(), Flags).addMBB(TBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000647 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000648 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000649
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000650 return 2;
651}
652
Brendon Cahoon254f8892016-07-29 16:44:44 +0000653/// Analyze the loop code to find the loop induction variable and compare used
654/// to compute the number of iterations. Currently, we analyze loop that are
655/// controlled using hardware loops. In this case, the induction variable
656/// instruction is null. For all other cases, this function returns true, which
657/// means we're unable to analyze it.
658bool HexagonInstrInfo::analyzeLoop(MachineLoop &L,
659 MachineInstr *&IndVarInst,
660 MachineInstr *&CmpInst) const {
661
662 MachineBasicBlock *LoopEnd = L.getBottomBlock();
663 MachineBasicBlock::iterator I = LoopEnd->getFirstTerminator();
664 // We really "analyze" only hardware loops right now.
665 if (I != LoopEnd->end() && isEndLoopN(I->getOpcode())) {
666 IndVarInst = nullptr;
667 CmpInst = &*I;
668 return false;
669 }
670 return true;
671}
672
673/// Generate code to reduce the loop iteration by one and check if the loop is
674/// finished. Return the value/register of the new loop count. this function
675/// assumes the nth iteration is peeled first.
676unsigned HexagonInstrInfo::reduceLoopCount(MachineBasicBlock &MBB,
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000677 MachineInstr *IndVar, MachineInstr &Cmp,
Brendon Cahoon254f8892016-07-29 16:44:44 +0000678 SmallVectorImpl<MachineOperand> &Cond,
679 SmallVectorImpl<MachineInstr *> &PrevInsts,
680 unsigned Iter, unsigned MaxIter) const {
681 // We expect a hardware loop currently. This means that IndVar is set
682 // to null, and the compare is the ENDLOOP instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000683 assert((!IndVar) && isEndLoopN(Cmp.getOpcode())
Brendon Cahoon254f8892016-07-29 16:44:44 +0000684 && "Expecting a hardware loop");
685 MachineFunction *MF = MBB.getParent();
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000686 DebugLoc DL = Cmp.getDebugLoc();
Brendon Cahoon254f8892016-07-29 16:44:44 +0000687 SmallPtrSet<MachineBasicBlock *, 8> VisitedBBs;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000688 MachineInstr *Loop = findLoopInstr(&MBB, Cmp.getOpcode(), VisitedBBs);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000689 if (!Loop)
690 return 0;
691 // If the loop trip count is a compile-time value, then just change the
692 // value.
693 if (Loop->getOpcode() == Hexagon::J2_loop0i ||
694 Loop->getOpcode() == Hexagon::J2_loop1i) {
695 int64_t Offset = Loop->getOperand(1).getImm();
696 if (Offset <= 1)
697 Loop->eraseFromParent();
698 else
699 Loop->getOperand(1).setImm(Offset - 1);
700 return Offset - 1;
701 }
702 // The loop trip count is a run-time value. We generate code to subtract
703 // one from the trip count, and update the loop instruction.
704 assert(Loop->getOpcode() == Hexagon::J2_loop0r && "Unexpected instruction");
705 unsigned LoopCount = Loop->getOperand(1).getReg();
706 // Check if we're done with the loop.
707 unsigned LoopEnd = createVR(MF, MVT::i1);
708 MachineInstr *NewCmp = BuildMI(&MBB, DL, get(Hexagon::C2_cmpgtui), LoopEnd).
709 addReg(LoopCount).addImm(1);
710 unsigned NewLoopCount = createVR(MF, MVT::i32);
711 MachineInstr *NewAdd = BuildMI(&MBB, DL, get(Hexagon::A2_addi), NewLoopCount).
712 addReg(LoopCount).addImm(-1);
713 // Update the previously generated instructions with the new loop counter.
714 for (SmallVectorImpl<MachineInstr *>::iterator I = PrevInsts.begin(),
715 E = PrevInsts.end(); I != E; ++I)
716 (*I)->substituteRegister(LoopCount, NewLoopCount, 0, getRegisterInfo());
717 PrevInsts.clear();
718 PrevInsts.push_back(NewCmp);
719 PrevInsts.push_back(NewAdd);
720 // Insert the new loop instruction if this is the last time the loop is
721 // decremented.
722 if (Iter == MaxIter)
723 BuildMI(&MBB, DL, get(Hexagon::J2_loop0r)).
724 addMBB(Loop->getOperand(0).getMBB()).addReg(NewLoopCount);
725 // Delete the old loop instruction.
726 if (Iter == 0)
727 Loop->eraseFromParent();
728 Cond.push_back(MachineOperand::CreateImm(Hexagon::J2_jumpf));
729 Cond.push_back(NewCmp->getOperand(0));
730 return NewLoopCount;
731}
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000732
733bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
734 unsigned NumCycles, unsigned ExtraPredCycles,
735 BranchProbability Probability) const {
736 return nonDbgBBSize(&MBB) <= 3;
737}
738
739
740bool HexagonInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
741 unsigned NumTCycles, unsigned ExtraTCycles, MachineBasicBlock &FMBB,
742 unsigned NumFCycles, unsigned ExtraFCycles, BranchProbability Probability)
743 const {
744 return nonDbgBBSize(&TMBB) <= 3 && nonDbgBBSize(&FMBB) <= 3;
745}
746
747
748bool HexagonInstrInfo::isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
749 unsigned NumInstrs, BranchProbability Probability) const {
750 return NumInstrs <= 4;
Krzysztof Parzyszekcfe285e2013-02-11 20:04:29 +0000751}
752
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000753void HexagonInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000754 MachineBasicBlock::iterator I,
755 const DebugLoc &DL, unsigned DestReg,
756 unsigned SrcReg, bool KillSrc) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000757 auto &HRI = getRegisterInfo();
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000758 unsigned KillFlag = getKillRegState(KillSrc);
759
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000760 if (Hexagon::IntRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000761 BuildMI(MBB, I, DL, get(Hexagon::A2_tfr), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000762 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000763 return;
764 }
765 if (Hexagon::DoubleRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000766 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrp), DestReg)
767 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000768 return;
769 }
770 if (Hexagon::PredRegsRegClass.contains(SrcReg, DestReg)) {
771 // Map Pd = Ps to Pd = or(Ps, Ps).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000772 BuildMI(MBB, I, DL, get(Hexagon::C2_or), DestReg)
773 .addReg(SrcReg).addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000774 return;
775 }
Colin LeMahieu402f7722014-12-19 18:56:10 +0000776 if (Hexagon::CtrRegsRegClass.contains(DestReg) &&
Sirish Pande8bb97452012-05-12 05:54:15 +0000777 Hexagon::IntRegsRegClass.contains(SrcReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000778 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
779 .addReg(SrcReg, KillFlag);
780 return;
781 }
782 if (Hexagon::IntRegsRegClass.contains(DestReg) &&
783 Hexagon::CtrRegsRegClass.contains(SrcReg)) {
784 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrcrr), DestReg)
785 .addReg(SrcReg, KillFlag);
786 return;
787 }
788 if (Hexagon::ModRegsRegClass.contains(DestReg) &&
789 Hexagon::IntRegsRegClass.contains(SrcReg)) {
790 BuildMI(MBB, I, DL, get(Hexagon::A2_tfrrcr), DestReg)
791 .addReg(SrcReg, KillFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000792 return;
Sirish Pande30804c22012-02-15 18:52:27 +0000793 }
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000794 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
795 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000796 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
797 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000798 return;
799 }
800 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
801 Hexagon::PredRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000802 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrrp), DestReg)
803 .addReg(SrcReg, KillFlag);
Anshuman Dasguptae96f8042013-02-13 22:56:34 +0000804 return;
805 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000806 if (Hexagon::PredRegsRegClass.contains(SrcReg) &&
807 Hexagon::IntRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000808 BuildMI(MBB, I, DL, get(Hexagon::C2_tfrpr), DestReg)
809 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000810 return;
811 }
812 if (Hexagon::VectorRegsRegClass.contains(SrcReg, DestReg)) {
813 BuildMI(MBB, I, DL, get(Hexagon::V6_vassign), DestReg).
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000814 addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000815 return;
816 }
817 if (Hexagon::VecDblRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000818 BuildMI(MBB, I, DL, get(Hexagon::V6_vcombine), DestReg)
819 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg), KillFlag)
820 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg), KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000821 return;
822 }
823 if (Hexagon::VecPredRegsRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000824 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DestReg)
825 .addReg(SrcReg)
826 .addReg(SrcReg, KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000827 return;
828 }
829 if (Hexagon::VecPredRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000830 Hexagon::VectorRegsRegClass.contains(DestReg)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000831 llvm_unreachable("Unimplemented pred to vec");
832 return;
833 }
834 if (Hexagon::VecPredRegsRegClass.contains(DestReg) &&
835 Hexagon::VectorRegsRegClass.contains(SrcReg)) {
836 llvm_unreachable("Unimplemented vec to pred");
837 return;
838 }
839 if (Hexagon::VecPredRegs128BRegClass.contains(SrcReg, DestReg)) {
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000840 unsigned DstHi = HRI.getSubReg(DestReg, Hexagon::subreg_hireg);
841 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DstHi)
842 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg), KillFlag);
843 unsigned DstLo = HRI.getSubReg(DestReg, Hexagon::subreg_loreg);
844 BuildMI(MBB, I, DL, get(Hexagon::V6_pred_and), DstLo)
845 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg), KillFlag);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000846 return;
847 }
Sirish Pande30804c22012-02-15 18:52:27 +0000848
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000849#ifndef NDEBUG
850 // Show the invalid registers to ease debugging.
851 dbgs() << "Invalid registers for copy in BB#" << MBB.getNumber()
852 << ": " << PrintReg(DestReg, &HRI)
853 << " = " << PrintReg(SrcReg, &HRI) << '\n';
854#endif
Sirish Pande30804c22012-02-15 18:52:27 +0000855 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000856}
857
858
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000859void HexagonInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
860 MachineBasicBlock::iterator I, unsigned SrcReg, bool isKill, int FI,
861 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000862 DebugLoc DL = MBB.findDebugLoc(I);
863 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000864 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000865 unsigned Align = MFI.getObjectAlignment(FI);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000866 unsigned KillFlag = getKillRegState(isKill);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000867
Alex Lorenze40c8a22015-08-11 23:09:45 +0000868 MachineMemOperand *MMO = MF.getMachineMemOperand(
869 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
870 MFI.getObjectSize(FI), Align);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000871
Craig Topperc7242e02012-04-20 07:30:17 +0000872 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000873 BuildMI(MBB, I, DL, get(Hexagon::S2_storeri_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000874 .addFrameIndex(FI).addImm(0)
875 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000876 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieubda31b42014-12-29 20:44:51 +0000877 BuildMI(MBB, I, DL, get(Hexagon::S2_storerd_io))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000878 .addFrameIndex(FI).addImm(0)
879 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Craig Topperc7242e02012-04-20 07:30:17 +0000880 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000881 BuildMI(MBB, I, DL, get(Hexagon::STriw_pred))
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000882 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000883 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000884 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
885 BuildMI(MBB, I, DL, get(Hexagon::STriw_mod))
886 .addFrameIndex(FI).addImm(0)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000887 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
888 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000889 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai_128B))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000890 .addFrameIndex(FI).addImm(0)
891 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
892 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000893 BuildMI(MBB, I, DL, get(Hexagon::PS_vstorerq_ai))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000894 .addFrameIndex(FI).addImm(0)
895 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
896 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000897 unsigned Opc = Align < 128 ? Hexagon::V6_vS32Ub_ai_128B
898 : Hexagon::V6_vS32b_ai_128B;
899 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000900 .addFrameIndex(FI).addImm(0)
901 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
902 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000903 unsigned Opc = Align < 64 ? Hexagon::V6_vS32Ub_ai
904 : Hexagon::V6_vS32b_ai;
905 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000906 .addFrameIndex(FI).addImm(0)
907 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
908 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000909 unsigned Opc = Align < 64 ? Hexagon::PS_vstorerwu_ai
910 : Hexagon::PS_vstorerw_ai;
911 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000912 .addFrameIndex(FI).addImm(0)
913 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
914 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000915 unsigned Opc = Align < 128 ? Hexagon::PS_vstorerwu_ai_128B
916 : Hexagon::PS_vstorerw_ai_128B;
917 BuildMI(MBB, I, DL, get(Opc))
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000918 .addFrameIndex(FI).addImm(0)
919 .addReg(SrcReg, KillFlag).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000920 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000921 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000922 }
923}
924
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000925void HexagonInstrInfo::loadRegFromStackSlot(
926 MachineBasicBlock &MBB, MachineBasicBlock::iterator I, unsigned DestReg,
927 int FI, const TargetRegisterClass *RC,
928 const TargetRegisterInfo *TRI) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000929 DebugLoc DL = MBB.findDebugLoc(I);
930 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000931 MachineFrameInfo &MFI = MF.getFrameInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000932 unsigned Align = MFI.getObjectAlignment(FI);
933
Alex Lorenze40c8a22015-08-11 23:09:45 +0000934 MachineMemOperand *MMO = MF.getMachineMemOperand(
935 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
936 MFI.getObjectSize(FI), Align);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000937
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000938 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +0000939 BuildMI(MBB, I, DL, get(Hexagon::L2_loadri_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000940 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000941 } else if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) {
Colin LeMahieu947cd702014-12-23 20:44:59 +0000942 BuildMI(MBB, I, DL, get(Hexagon::L2_loadrd_io), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000943 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000944 } else if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000945 BuildMI(MBB, I, DL, get(Hexagon::LDriw_pred), DestReg)
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +0000946 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
947 } else if (Hexagon::ModRegsRegClass.hasSubClassEq(RC)) {
948 BuildMI(MBB, I, DL, get(Hexagon::LDriw_mod), DestReg)
949 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000950 } else if (Hexagon::VecPredRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000951 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai_128B), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000952 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
953 } else if (Hexagon::VecPredRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +0000954 BuildMI(MBB, I, DL, get(Hexagon::PS_vloadrq_ai), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000955 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
956 } else if (Hexagon::VecDblRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000957 unsigned Opc = Align < 128 ? Hexagon::PS_vloadrwu_ai_128B
958 : Hexagon::PS_vloadrw_ai_128B;
959 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000960 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
961 } else if (Hexagon::VectorRegs128BRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000962 unsigned Opc = Align < 128 ? Hexagon::V6_vL32Ub_ai_128B
963 : Hexagon::V6_vL32b_ai_128B;
964 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000965 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
966 } else if (Hexagon::VectorRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000967 unsigned Opc = Align < 64 ? Hexagon::V6_vL32Ub_ai
968 : Hexagon::V6_vL32b_ai;
969 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000970 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
971 } else if (Hexagon::VecDblRegsRegClass.hasSubClassEq(RC)) {
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +0000972 unsigned Opc = Align < 64 ? Hexagon::PS_vloadrwu_ai
973 : Hexagon::PS_vloadrw_ai;
974 BuildMI(MBB, I, DL, get(Opc), DestReg)
Krzysztof Parzyszek79a886b2016-02-12 21:56:41 +0000975 .addFrameIndex(FI).addImm(0).addMemOperand(MMO);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000976 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000977 llvm_unreachable("Can't store this register to stack slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000978 }
979}
980
981
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000982/// expandPostRAPseudo - This function is called for all pseudo instructions
983/// that remain after register allocation. Many pseudo instructions are
984/// created to help register allocation. This is the place to convert them
985/// into real instructions. The target can edit MI in place, or it can insert
986/// new instructions and erase MI. The function should return true if
987/// anything was changed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000988bool HexagonInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000989 const HexagonRegisterInfo &HRI = getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000990 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
991 MachineBasicBlock &MBB = *MI.getParent();
992 DebugLoc DL = MI.getDebugLoc();
993 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +0000994 const unsigned VecOffset = 1;
Colin LeMahieu7b1799c2015-03-09 22:05:21 +0000995
996 switch (Opc) {
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +0000997 case TargetOpcode::COPY: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000998 MachineOperand &MD = MI.getOperand(0);
999 MachineOperand &MS = MI.getOperand(1);
1000 MachineBasicBlock::iterator MBBI = MI.getIterator();
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001001 if (MD.getReg() != MS.getReg() && !MS.isUndef()) {
1002 copyPhysReg(MBB, MI, DL, MD.getReg(), MS.getReg(), MS.isKill());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001003 std::prev(MBBI)->copyImplicitOps(*MBB.getParent(), MI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001004 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001005 MBB.erase(MBBI);
Krzysztof Parzyszek3d6fc832016-06-02 14:33:08 +00001006 return true;
1007 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001008 case Hexagon::PS_aligna:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001009 BuildMI(MBB, MI, DL, get(Hexagon::A2_andir), MI.getOperand(0).getReg())
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001010 .addReg(HRI.getFrameRegister())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001011 .addImm(-MI.getOperand(1).getImm());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00001012 MBB.erase(MI);
1013 return true;
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001014 case Hexagon::V6_vassignp_128B:
1015 case Hexagon::V6_vassignp: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001016 unsigned SrcReg = MI.getOperand(1).getReg();
1017 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001018 unsigned Kill = getKillRegState(MI.getOperand(1).isKill());
1019 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcombine), DstReg)
1020 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_hireg), Kill)
1021 .addReg(HRI.getSubReg(SrcReg, Hexagon::subreg_loreg), Kill);
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001022 MBB.erase(MI);
1023 return true;
1024 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001025 case Hexagon::V6_lo_128B:
1026 case Hexagon::V6_lo: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001027 unsigned SrcReg = MI.getOperand(1).getReg();
1028 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001029 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001030 copyPhysReg(MBB, MI, DL, DstReg, SrcSubLo, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001031 MBB.erase(MI);
1032 MRI.clearKillFlags(SrcSubLo);
1033 return true;
1034 }
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001035 case Hexagon::V6_hi_128B:
1036 case Hexagon::V6_hi: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001037 unsigned SrcReg = MI.getOperand(1).getReg();
1038 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001039 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::subreg_hireg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001040 copyPhysReg(MBB, MI, DL, DstReg, SrcSubHi, MI.getOperand(1).isKill());
Krzysztof Parzyszek4eb6d4d2015-11-26 16:54:33 +00001041 MBB.erase(MI);
1042 MRI.clearKillFlags(SrcSubHi);
1043 return true;
1044 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001045 case Hexagon::PS_vstorerw_ai:
1046 case Hexagon::PS_vstorerwu_ai:
1047 case Hexagon::PS_vstorerw_ai_128B:
1048 case Hexagon::PS_vstorerwu_ai_128B: {
1049 bool Is128B = (Opc == Hexagon::PS_vstorerw_ai_128B ||
1050 Opc == Hexagon::PS_vstorerwu_ai_128B);
1051 bool Aligned = (Opc == Hexagon::PS_vstorerw_ai ||
1052 Opc == Hexagon::PS_vstorerw_ai_128B);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001053 unsigned SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001054 unsigned SrcSubHi = HRI.getSubReg(SrcReg, Hexagon::subreg_hireg);
1055 unsigned SrcSubLo = HRI.getSubReg(SrcReg, Hexagon::subreg_loreg);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001056 unsigned NewOpc;
1057 if (Aligned)
1058 NewOpc = Is128B ? Hexagon::V6_vS32b_ai_128B
1059 : Hexagon::V6_vS32b_ai;
1060 else
1061 NewOpc = Is128B ? Hexagon::V6_vS32Ub_ai_128B
1062 : Hexagon::V6_vS32Ub_ai;
1063
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001064 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001065 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001066 BuildMI(MBB, MI, DL, get(NewOpc))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001067 .addOperand(MI.getOperand(0))
1068 .addImm(MI.getOperand(1).getImm())
1069 .addReg(SrcSubLo)
1070 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001071 MI1New->getOperand(0).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001072 BuildMI(MBB, MI, DL, get(NewOpc))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001073 .addOperand(MI.getOperand(0))
1074 // The Vectors are indexed in multiples of vector size.
1075 .addImm(MI.getOperand(1).getImm() + Offset)
1076 .addReg(SrcSubHi)
1077 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001078 MBB.erase(MI);
1079 return true;
1080 }
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001081 case Hexagon::PS_vloadrw_ai:
1082 case Hexagon::PS_vloadrwu_ai:
1083 case Hexagon::PS_vloadrw_ai_128B:
1084 case Hexagon::PS_vloadrwu_ai_128B: {
1085 bool Is128B = (Opc == Hexagon::PS_vloadrw_ai_128B ||
1086 Opc == Hexagon::PS_vloadrwu_ai_128B);
1087 bool Aligned = (Opc == Hexagon::PS_vloadrw_ai ||
1088 Opc == Hexagon::PS_vloadrw_ai_128B);
1089 unsigned NewOpc;
1090 if (Aligned)
1091 NewOpc = Is128B ? Hexagon::V6_vL32b_ai_128B
1092 : Hexagon::V6_vL32b_ai;
1093 else
1094 NewOpc = Is128B ? Hexagon::V6_vL32Ub_ai_128B
1095 : Hexagon::V6_vL32Ub_ai;
1096
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001097 unsigned DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001098 unsigned Offset = Is128B ? VecOffset << 7 : VecOffset << 6;
1099 MachineInstr *MI1New =
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001100 BuildMI(MBB, MI, DL, get(NewOpc),
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001101 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001102 .addOperand(MI.getOperand(1))
1103 .addImm(MI.getOperand(2).getImm());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001104 MI1New->getOperand(1).setIsKill(false);
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00001105 BuildMI(MBB, MI, DL, get(NewOpc),
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001106 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001107 .addOperand(MI.getOperand(1))
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001108 // The Vectors are indexed in multiples of vector size.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001109 .addImm(MI.getOperand(2).getImm() + Offset)
1110 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Krzysztof Parzyszek195dc8d2015-11-26 04:33:11 +00001111 MBB.erase(MI);
1112 return true;
1113 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001114 case Hexagon::PS_true: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001115 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001116 BuildMI(MBB, MI, DL, get(Hexagon::C2_orn), Reg)
1117 .addReg(Reg, RegState::Undef)
1118 .addReg(Reg, RegState::Undef);
1119 MBB.erase(MI);
1120 return true;
1121 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001122 case Hexagon::PS_false: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001123 unsigned Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszek36ccfa52015-03-18 19:07:53 +00001124 BuildMI(MBB, MI, DL, get(Hexagon::C2_andn), Reg)
1125 .addReg(Reg, RegState::Undef)
1126 .addReg(Reg, RegState::Undef);
1127 MBB.erase(MI);
1128 return true;
1129 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001130 case Hexagon::PS_vmulw: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001131 // Expand a 64-bit vector multiply into 2 32-bit scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001132 unsigned DstReg = MI.getOperand(0).getReg();
1133 unsigned Src1Reg = MI.getOperand(1).getReg();
1134 unsigned Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001135 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
1136 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
1137 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
1138 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001139 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
1140 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1141 .addReg(Src1SubHi)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001142 .addReg(Src2SubHi);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001143 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_mpyi),
1144 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1145 .addReg(Src1SubLo)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001146 .addReg(Src2SubLo);
1147 MBB.erase(MI);
1148 MRI.clearKillFlags(Src1SubHi);
1149 MRI.clearKillFlags(Src1SubLo);
1150 MRI.clearKillFlags(Src2SubHi);
1151 MRI.clearKillFlags(Src2SubLo);
1152 return true;
1153 }
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00001154 case Hexagon::PS_vmulw_acc: {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001155 // Expand 64-bit vector multiply with addition into 2 scalar multiplies.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001156 unsigned DstReg = MI.getOperand(0).getReg();
1157 unsigned Src1Reg = MI.getOperand(1).getReg();
1158 unsigned Src2Reg = MI.getOperand(2).getReg();
1159 unsigned Src3Reg = MI.getOperand(3).getReg();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001160 unsigned Src1SubHi = HRI.getSubReg(Src1Reg, Hexagon::subreg_hireg);
1161 unsigned Src1SubLo = HRI.getSubReg(Src1Reg, Hexagon::subreg_loreg);
1162 unsigned Src2SubHi = HRI.getSubReg(Src2Reg, Hexagon::subreg_hireg);
1163 unsigned Src2SubLo = HRI.getSubReg(Src2Reg, Hexagon::subreg_loreg);
1164 unsigned Src3SubHi = HRI.getSubReg(Src3Reg, Hexagon::subreg_hireg);
1165 unsigned Src3SubLo = HRI.getSubReg(Src3Reg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001166 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
1167 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1168 .addReg(Src1SubHi)
1169 .addReg(Src2SubHi)
1170 .addReg(Src3SubHi);
1171 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::M2_maci),
1172 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1173 .addReg(Src1SubLo)
1174 .addReg(Src2SubLo)
1175 .addReg(Src3SubLo);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001176 MBB.erase(MI);
1177 MRI.clearKillFlags(Src1SubHi);
1178 MRI.clearKillFlags(Src1SubLo);
1179 MRI.clearKillFlags(Src2SubHi);
1180 MRI.clearKillFlags(Src2SubLo);
1181 MRI.clearKillFlags(Src3SubHi);
1182 MRI.clearKillFlags(Src3SubLo);
1183 return true;
1184 }
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +00001185 case Hexagon::Insert4: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001186 unsigned DstReg = MI.getOperand(0).getReg();
1187 unsigned Src1Reg = MI.getOperand(1).getReg();
1188 unsigned Src2Reg = MI.getOperand(2).getReg();
1189 unsigned Src3Reg = MI.getOperand(3).getReg();
1190 unsigned Src4Reg = MI.getOperand(4).getReg();
1191 unsigned Src1RegIsKill = getKillRegState(MI.getOperand(1).isKill());
1192 unsigned Src2RegIsKill = getKillRegState(MI.getOperand(2).isKill());
1193 unsigned Src3RegIsKill = getKillRegState(MI.getOperand(3).isKill());
1194 unsigned Src4RegIsKill = getKillRegState(MI.getOperand(4).isKill());
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +00001195 unsigned DstSubHi = HRI.getSubReg(DstReg, Hexagon::subreg_hireg);
1196 unsigned DstSubLo = HRI.getSubReg(DstReg, Hexagon::subreg_loreg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001197 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1198 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1199 .addReg(DstSubLo)
1200 .addReg(Src1Reg, Src1RegIsKill)
1201 .addImm(16)
1202 .addImm(0);
1203 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1204 HRI.getSubReg(DstReg, Hexagon::subreg_loreg))
1205 .addReg(DstSubLo)
1206 .addReg(Src2Reg, Src2RegIsKill)
1207 .addImm(16)
1208 .addImm(16);
1209 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1210 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1211 .addReg(DstSubHi)
1212 .addReg(Src3Reg, Src3RegIsKill)
1213 .addImm(16)
1214 .addImm(0);
1215 BuildMI(MBB, MI, MI.getDebugLoc(), get(Hexagon::S2_insert),
1216 HRI.getSubReg(DstReg, Hexagon::subreg_hireg))
1217 .addReg(DstSubHi)
1218 .addReg(Src4Reg, Src4RegIsKill)
1219 .addImm(16)
1220 .addImm(16);
Krzysztof Parzyszek237b9612016-01-14 15:37:16 +00001221 MBB.erase(MI);
1222 MRI.clearKillFlags(DstReg);
1223 MRI.clearKillFlags(DstSubHi);
1224 MRI.clearKillFlags(DstSubLo);
1225 return true;
1226 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001227 case Hexagon::PS_pselect: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001228 const MachineOperand &Op0 = MI.getOperand(0);
1229 const MachineOperand &Op1 = MI.getOperand(1);
1230 const MachineOperand &Op2 = MI.getOperand(2);
1231 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001232 unsigned Rd = Op0.getReg();
1233 unsigned Pu = Op1.getReg();
1234 unsigned Rs = Op2.getReg();
1235 unsigned Rt = Op3.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001236 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00001237 unsigned K1 = getKillRegState(Op1.isKill());
1238 unsigned K2 = getKillRegState(Op2.isKill());
1239 unsigned K3 = getKillRegState(Op3.isKill());
1240 if (Rd != Rs)
1241 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpt), Rd)
1242 .addReg(Pu, (Rd == Rt) ? K1 : 0)
1243 .addReg(Rs, K2);
1244 if (Rd != Rt)
1245 BuildMI(MBB, MI, DL, get(Hexagon::A2_tfrpf), Rd)
1246 .addReg(Pu, K1)
1247 .addReg(Rt, K3);
1248 MBB.erase(MI);
1249 return true;
1250 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001251 case Hexagon::PS_vselect:
1252 case Hexagon::PS_vselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001253 const MachineOperand &Op0 = MI.getOperand(0);
1254 const MachineOperand &Op1 = MI.getOperand(1);
1255 const MachineOperand &Op2 = MI.getOperand(2);
1256 const MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001257 BuildMI(MBB, MI, DL, get(Hexagon::V6_vcmov))
1258 .addOperand(Op0)
1259 .addOperand(Op1)
1260 .addOperand(Op2);
1261 BuildMI(MBB, MI, DL, get(Hexagon::V6_vncmov))
1262 .addOperand(Op0)
1263 .addOperand(Op1)
1264 .addOperand(Op3);
1265 MBB.erase(MI);
1266 return true;
1267 }
Krzysztof Parzyszek258af192016-08-11 19:12:18 +00001268 case Hexagon::PS_wselect:
1269 case Hexagon::PS_wselect_128B: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001270 MachineOperand &Op0 = MI.getOperand(0);
1271 MachineOperand &Op1 = MI.getOperand(1);
1272 MachineOperand &Op2 = MI.getOperand(2);
1273 MachineOperand &Op3 = MI.getOperand(3);
Krzysztof Parzyszek4afed552016-05-12 19:16:02 +00001274 unsigned SrcLo = HRI.getSubReg(Op2.getReg(), Hexagon::subreg_loreg);
1275 unsigned SrcHi = HRI.getSubReg(Op2.getReg(), Hexagon::subreg_hireg);
1276 BuildMI(MBB, MI, DL, get(Hexagon::V6_vccombine))
1277 .addOperand(Op0)
1278 .addOperand(Op1)
1279 .addReg(SrcHi)
1280 .addReg(SrcLo);
1281 SrcLo = HRI.getSubReg(Op3.getReg(), Hexagon::subreg_loreg);
1282 SrcHi = HRI.getSubReg(Op3.getReg(), Hexagon::subreg_hireg);
1283 BuildMI(MBB, MI, DL, get(Hexagon::V6_vnccombine))
1284 .addOperand(Op0)
1285 .addOperand(Op1)
1286 .addReg(SrcHi)
1287 .addReg(SrcLo);
1288 MBB.erase(MI);
1289 return true;
1290 }
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001291 case Hexagon::PS_tailcall_i:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001292 MI.setDesc(get(Hexagon::J2_jump));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001293 return true;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001294 case Hexagon::PS_tailcall_r:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001295 MI.setDesc(get(Hexagon::J2_jumpr));
Colin LeMahieu7b1799c2015-03-09 22:05:21 +00001296 return true;
1297 }
1298
1299 return false;
1300}
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001301
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001302
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001303// We indicate that we want to reverse the branch by
1304// inserting the reversed branching opcode.
1305bool HexagonInstrInfo::ReverseBranchCondition(
1306 SmallVectorImpl<MachineOperand> &Cond) const {
1307 if (Cond.empty())
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001308 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001309 assert(Cond[0].isImm() && "First entry in the cond vector not imm-val");
1310 unsigned opcode = Cond[0].getImm();
1311 //unsigned temp;
1312 assert(get(opcode).isBranch() && "Should be a branching condition.");
1313 if (isEndLoopN(opcode))
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001314 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001315 unsigned NewOpcode = getInvertedPredicatedOpcode(opcode);
1316 Cond[0].setImm(NewOpcode);
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001317 return false;
1318}
1319
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001320
1321void HexagonInstrInfo::insertNoop(MachineBasicBlock &MBB,
1322 MachineBasicBlock::iterator MI) const {
1323 DebugLoc DL;
1324 BuildMI(MBB, MI, DL, get(Hexagon::A2_nop));
1325}
1326
1327
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001328bool HexagonInstrInfo::isPostIncrement(const MachineInstr &MI) const {
1329 return getAddrMode(MI) == HexagonII::PostInc;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001330}
1331
1332
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001333// Returns true if an instruction is predicated irrespective of the predicate
1334// sense. For example, all of the following will return true.
1335// if (p0) R1 = add(R2, R3)
1336// if (!p0) R1 = add(R2, R3)
1337// if (p0.new) R1 = add(R2, R3)
1338// if (!p0.new) R1 = add(R2, R3)
1339// Note: New-value stores are not included here as in the current
1340// implementation, we don't need to check their predicate sense.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001341bool HexagonInstrInfo::isPredicated(const MachineInstr &MI) const {
1342 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001343 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001344}
1345
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001346
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001347bool HexagonInstrInfo::PredicateInstruction(
1348 MachineInstr &MI, ArrayRef<MachineOperand> Cond) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001349 if (Cond.empty() || isNewValueJump(Cond[0].getImm()) ||
1350 isEndLoopN(Cond[0].getImm())) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001351 DEBUG(dbgs() << "\nCannot predicate:"; MI.dump(););
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001352 return false;
1353 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001354 int Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001355 assert (isPredicable(MI) && "Expected predicable instruction");
1356 bool invertJump = predOpcodeHasNot(Cond);
1357
1358 // We have to predicate MI "in place", i.e. after this function returns,
1359 // MI will need to be transformed into a predicated form. To avoid com-
1360 // plicated manipulations with the operands (handling tied operands,
1361 // etc.), build a new temporary instruction, then overwrite MI with it.
1362
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001363 MachineBasicBlock &B = *MI.getParent();
1364 DebugLoc DL = MI.getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001365 unsigned PredOpc = getCondOpcode(Opc, invertJump);
1366 MachineInstrBuilder T = BuildMI(B, MI, DL, get(PredOpc));
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001367 unsigned NOp = 0, NumOps = MI.getNumOperands();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001368 while (NOp < NumOps) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001369 MachineOperand &Op = MI.getOperand(NOp);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001370 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1371 break;
1372 T.addOperand(Op);
1373 NOp++;
1374 }
1375
1376 unsigned PredReg, PredRegPos, PredRegFlags;
1377 bool GotPredReg = getPredReg(Cond, PredReg, PredRegPos, PredRegFlags);
1378 (void)GotPredReg;
1379 assert(GotPredReg);
1380 T.addReg(PredReg, PredRegFlags);
1381 while (NOp < NumOps)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001382 T.addOperand(MI.getOperand(NOp++));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001383
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001384 MI.setDesc(get(PredOpc));
1385 while (unsigned n = MI.getNumOperands())
1386 MI.RemoveOperand(n-1);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001387 for (unsigned i = 0, n = T->getNumOperands(); i < n; ++i)
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001388 MI.addOperand(T->getOperand(i));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001389
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001390 MachineBasicBlock::instr_iterator TI = T->getIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001391 B.erase(TI);
1392
1393 MachineRegisterInfo &MRI = B.getParent()->getRegInfo();
1394 MRI.clearKillFlags(PredReg);
1395 return true;
Brendon Cahoondf43e682015-05-08 16:16:29 +00001396}
1397
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001398
1399bool HexagonInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1400 ArrayRef<MachineOperand> Pred2) const {
1401 // TODO: Fix this
1402 return false;
1403}
1404
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001405
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001406bool HexagonInstrInfo::DefinesPredicate(
1407 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001408 auto &HRI = getRegisterInfo();
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001409 for (unsigned oper = 0; oper < MI.getNumOperands(); ++oper) {
1410 MachineOperand MO = MI.getOperand(oper);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001411 if (MO.isReg() && MO.isDef()) {
1412 const TargetRegisterClass* RC = HRI.getMinimalPhysRegClass(MO.getReg());
1413 if (RC == &Hexagon::PredRegsRegClass) {
1414 Pred.push_back(MO);
1415 return true;
1416 }
1417 }
1418 }
1419 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001420}
Andrew Trickd06df962012-02-01 22:13:57 +00001421
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001422
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001423bool HexagonInstrInfo::isPredicable(MachineInstr &MI) const {
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00001424 return MI.getDesc().isPredicable();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001425}
1426
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001427bool HexagonInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
1428 const MachineBasicBlock *MBB,
1429 const MachineFunction &MF) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001430 // Debug info is never a scheduling boundary. It's necessary to be explicit
1431 // due to the special treatment of IT instructions below, otherwise a
1432 // dbg_value followed by an IT will result in the IT instruction being
1433 // considered a scheduling hazard, which is wrong. It should be the actual
1434 // instruction preceding the dbg_value instruction(s), just like it is
1435 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001436 if (MI.isDebugValue())
Brendon Cahoondf43e682015-05-08 16:16:29 +00001437 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001438
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001439 // Throwing call is a boundary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001440 if (MI.isCall()) {
Krzysztof Parzyszekab9127c2016-08-12 11:01:10 +00001441 // Don't mess around with no return calls.
1442 if (doesNotReturn(MI))
1443 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001444 // If any of the block's successors is a landing pad, this could be a
1445 // throwing call.
1446 for (auto I : MBB->successors())
1447 if (I->isEHPad())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001448 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001449 }
1450
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001451 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001452 if (MI.getDesc().isTerminator() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001453 return true;
1454
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001455 if (MI.isInlineAsm() && !ScheduleInlineAsm)
1456 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001457
1458 return false;
1459}
1460
1461
1462/// Measure the specified inline asm to determine an approximation of its
1463/// length.
1464/// Comments (which run till the next SeparatorString or newline) do not
1465/// count as an instruction.
1466/// Any other non-whitespace text is considered an instruction, with
1467/// multiple instructions separated by SeparatorString or newlines.
1468/// Variable-length instructions are not handled here; this function
1469/// may be overloaded in the target code to do that.
1470/// Hexagon counts the number of ##'s and adjust for that many
1471/// constant exenders.
1472unsigned HexagonInstrInfo::getInlineAsmLength(const char *Str,
1473 const MCAsmInfo &MAI) const {
1474 StringRef AStr(Str);
1475 // Count the number of instructions in the asm.
1476 bool atInsnStart = true;
1477 unsigned Length = 0;
1478 for (; *Str; ++Str) {
1479 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
1480 strlen(MAI.getSeparatorString())) == 0)
1481 atInsnStart = true;
1482 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
1483 Length += MAI.getMaxInstLength();
1484 atInsnStart = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001485 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001486 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
1487 strlen(MAI.getCommentString())) == 0)
1488 atInsnStart = false;
1489 }
1490
1491 // Add to size number of constant extenders seen * 4.
1492 StringRef Occ("##");
1493 Length += AStr.count(Occ)*4;
1494 return Length;
1495}
1496
1497
1498ScheduleHazardRecognizer*
1499HexagonInstrInfo::CreateTargetPostRAHazardRecognizer(
1500 const InstrItineraryData *II, const ScheduleDAG *DAG) const {
Krzysztof Parzyszeke95e9552016-07-29 13:59:09 +00001501 if (UseDFAHazardRec) {
1502 auto &HST = DAG->MF.getSubtarget<HexagonSubtarget>();
1503 return new HexagonHazardRecognizer(II, this, HST);
1504 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001505 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
1506}
1507
1508
1509/// \brief For a comparison instruction, return the source registers in
1510/// \p SrcReg and \p SrcReg2 if having two register operands, and the value it
1511/// compares against in CmpValue. Return true if the comparison instruction
1512/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001513bool HexagonInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
1514 unsigned &SrcReg2, int &Mask,
1515 int &Value) const {
1516 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001517
1518 // Set mask and the first source register.
1519 switch (Opc) {
1520 case Hexagon::C2_cmpeq:
1521 case Hexagon::C2_cmpeqp:
1522 case Hexagon::C2_cmpgt:
1523 case Hexagon::C2_cmpgtp:
1524 case Hexagon::C2_cmpgtu:
1525 case Hexagon::C2_cmpgtup:
1526 case Hexagon::C4_cmpneq:
1527 case Hexagon::C4_cmplte:
1528 case Hexagon::C4_cmplteu:
1529 case Hexagon::C2_cmpeqi:
1530 case Hexagon::C2_cmpgti:
1531 case Hexagon::C2_cmpgtui:
1532 case Hexagon::C4_cmpneqi:
1533 case Hexagon::C4_cmplteui:
1534 case Hexagon::C4_cmpltei:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001535 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001536 Mask = ~0;
1537 break;
1538 case Hexagon::A4_cmpbeq:
1539 case Hexagon::A4_cmpbgt:
1540 case Hexagon::A4_cmpbgtu:
1541 case Hexagon::A4_cmpbeqi:
1542 case Hexagon::A4_cmpbgti:
1543 case Hexagon::A4_cmpbgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001544 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001545 Mask = 0xFF;
1546 break;
1547 case Hexagon::A4_cmpheq:
1548 case Hexagon::A4_cmphgt:
1549 case Hexagon::A4_cmphgtu:
1550 case Hexagon::A4_cmpheqi:
1551 case Hexagon::A4_cmphgti:
1552 case Hexagon::A4_cmphgtui:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001553 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001554 Mask = 0xFFFF;
1555 break;
1556 }
1557
1558 // Set the value/second source register.
1559 switch (Opc) {
1560 case Hexagon::C2_cmpeq:
1561 case Hexagon::C2_cmpeqp:
1562 case Hexagon::C2_cmpgt:
1563 case Hexagon::C2_cmpgtp:
1564 case Hexagon::C2_cmpgtu:
1565 case Hexagon::C2_cmpgtup:
1566 case Hexagon::A4_cmpbeq:
1567 case Hexagon::A4_cmpbgt:
1568 case Hexagon::A4_cmpbgtu:
1569 case Hexagon::A4_cmpheq:
1570 case Hexagon::A4_cmphgt:
1571 case Hexagon::A4_cmphgtu:
1572 case Hexagon::C4_cmpneq:
1573 case Hexagon::C4_cmplte:
1574 case Hexagon::C4_cmplteu:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001575 SrcReg2 = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001576 return true;
1577
1578 case Hexagon::C2_cmpeqi:
1579 case Hexagon::C2_cmpgtui:
1580 case Hexagon::C2_cmpgti:
1581 case Hexagon::C4_cmpneqi:
1582 case Hexagon::C4_cmplteui:
1583 case Hexagon::C4_cmpltei:
1584 case Hexagon::A4_cmpbeqi:
1585 case Hexagon::A4_cmpbgti:
1586 case Hexagon::A4_cmpbgtui:
1587 case Hexagon::A4_cmpheqi:
1588 case Hexagon::A4_cmphgti:
1589 case Hexagon::A4_cmphgtui:
1590 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001591 Value = MI.getOperand(2).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001592 return true;
1593 }
1594
1595 return false;
1596}
1597
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001598unsigned HexagonInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001599 const MachineInstr &MI,
1600 unsigned *PredCost) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001601 return getInstrTimingClassLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001602}
1603
1604
1605DFAPacketizer *HexagonInstrInfo::CreateTargetScheduleState(
1606 const TargetSubtargetInfo &STI) const {
1607 const InstrItineraryData *II = STI.getInstrItineraryData();
1608 return static_cast<const HexagonSubtarget&>(STI).createDFAPacketizer(II);
1609}
1610
1611
1612// Inspired by this pair:
1613// %R13<def> = L2_loadri_io %R29, 136; mem:LD4[FixedStack0]
1614// S2_storeri_io %R29, 132, %R1<kill>; flags: mem:ST4[FixedStack1]
1615// Currently AA considers the addresses in these instructions to be aliasing.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001616bool HexagonInstrInfo::areMemAccessesTriviallyDisjoint(
1617 MachineInstr &MIa, MachineInstr &MIb, AliasAnalysis *AA) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001618 int OffsetA = 0, OffsetB = 0;
1619 unsigned SizeA = 0, SizeB = 0;
1620
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001621 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1622 MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001623 return false;
1624
1625 // Instructions that are pure loads, not loads and stores like memops are not
1626 // dependent.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001627 if (MIa.mayLoad() && !isMemOp(MIa) && MIb.mayLoad() && !isMemOp(MIb))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001628 return true;
1629
1630 // Get base, offset, and access size in MIa.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001631 unsigned BaseRegA = getBaseAndOffset(MIa, OffsetA, SizeA);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001632 if (!BaseRegA || !SizeA)
1633 return false;
1634
1635 // Get base, offset, and access size in MIb.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001636 unsigned BaseRegB = getBaseAndOffset(MIb, OffsetB, SizeB);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001637 if (!BaseRegB || !SizeB)
1638 return false;
1639
1640 if (BaseRegA != BaseRegB)
1641 return false;
1642
1643 // This is a mem access with the same base register and known offsets from it.
1644 // Reason about it.
1645 if (OffsetA > OffsetB) {
1646 uint64_t offDiff = (uint64_t)((int64_t)OffsetA - (int64_t)OffsetB);
1647 return (SizeB <= offDiff);
1648 } else if (OffsetA < OffsetB) {
1649 uint64_t offDiff = (uint64_t)((int64_t)OffsetB - (int64_t)OffsetA);
1650 return (SizeA <= offDiff);
1651 }
1652
1653 return false;
1654}
1655
1656
Brendon Cahoon254f8892016-07-29 16:44:44 +00001657/// If the instruction is an increment of a constant value, return the amount.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001658bool HexagonInstrInfo::getIncrementValue(const MachineInstr &MI,
Brendon Cahoon254f8892016-07-29 16:44:44 +00001659 int &Value) const {
1660 if (isPostIncrement(MI)) {
1661 unsigned AccessSize;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001662 return getBaseAndOffset(MI, Value, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00001663 }
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00001664 if (MI.getOpcode() == Hexagon::A2_addi) {
1665 Value = MI.getOperand(2).getImm();
Brendon Cahoon254f8892016-07-29 16:44:44 +00001666 return true;
1667 }
1668
1669 return false;
1670}
1671
1672
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001673unsigned HexagonInstrInfo::createVR(MachineFunction *MF, MVT VT) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001674 MachineRegisterInfo &MRI = MF->getRegInfo();
1675 const TargetRegisterClass *TRC;
1676 if (VT == MVT::i1) {
1677 TRC = &Hexagon::PredRegsRegClass;
1678 } else if (VT == MVT::i32 || VT == MVT::f32) {
1679 TRC = &Hexagon::IntRegsRegClass;
1680 } else if (VT == MVT::i64 || VT == MVT::f64) {
1681 TRC = &Hexagon::DoubleRegsRegClass;
1682 } else {
1683 llvm_unreachable("Cannot handle this register class");
1684 }
1685
1686 unsigned NewReg = MRI.createVirtualRegister(TRC);
1687 return NewReg;
1688}
1689
1690
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001691bool HexagonInstrInfo::isAbsoluteSet(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001692 return (getAddrMode(MI) == HexagonII::AbsoluteSet);
1693}
1694
1695
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001696bool HexagonInstrInfo::isAccumulator(const MachineInstr &MI) const {
1697 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001698 return((F >> HexagonII::AccumulatorPos) & HexagonII::AccumulatorMask);
1699}
1700
1701
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001702bool HexagonInstrInfo::isComplex(const MachineInstr &MI) const {
1703 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001704 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1705 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1706
1707 if (!(isTC1(MI))
1708 && !(QII->isTC2Early(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001709 && !(MI.getDesc().mayLoad())
1710 && !(MI.getDesc().mayStore())
1711 && (MI.getDesc().getOpcode() != Hexagon::S2_allocframe)
1712 && (MI.getDesc().getOpcode() != Hexagon::L2_deallocframe)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001713 && !(QII->isMemOp(MI))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001714 && !(MI.isBranch())
1715 && !(MI.isReturn())
1716 && !MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001717 return true;
1718
1719 return false;
1720}
1721
1722
Sanjay Patele4b9f502015-12-07 19:21:39 +00001723// Return true if the instruction is a compund branch instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001724bool HexagonInstrInfo::isCompoundBranchInstr(const MachineInstr &MI) const {
1725 return (getType(MI) == HexagonII::TypeCOMPOUND && MI.isBranch());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001726}
1727
1728
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001729bool HexagonInstrInfo::isCondInst(const MachineInstr &MI) const {
1730 return (MI.isBranch() && isPredicated(MI)) ||
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001731 isConditionalTransfer(MI) ||
1732 isConditionalALU32(MI) ||
1733 isConditionalLoad(MI) ||
1734 // Predicated stores which don't have a .new on any operands.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001735 (MI.mayStore() && isPredicated(MI) && !isNewValueStore(MI) &&
1736 !isPredicatedNew(MI));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001737}
1738
1739
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001740bool HexagonInstrInfo::isConditionalALU32(const MachineInstr &MI) const {
1741 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001742 case Hexagon::A2_paddf:
1743 case Hexagon::A2_paddfnew:
1744 case Hexagon::A2_paddif:
1745 case Hexagon::A2_paddifnew:
1746 case Hexagon::A2_paddit:
1747 case Hexagon::A2_padditnew:
1748 case Hexagon::A2_paddt:
1749 case Hexagon::A2_paddtnew:
1750 case Hexagon::A2_pandf:
1751 case Hexagon::A2_pandfnew:
1752 case Hexagon::A2_pandt:
1753 case Hexagon::A2_pandtnew:
1754 case Hexagon::A2_porf:
1755 case Hexagon::A2_porfnew:
1756 case Hexagon::A2_port:
1757 case Hexagon::A2_portnew:
1758 case Hexagon::A2_psubf:
1759 case Hexagon::A2_psubfnew:
1760 case Hexagon::A2_psubt:
1761 case Hexagon::A2_psubtnew:
1762 case Hexagon::A2_pxorf:
1763 case Hexagon::A2_pxorfnew:
1764 case Hexagon::A2_pxort:
1765 case Hexagon::A2_pxortnew:
1766 case Hexagon::A4_paslhf:
1767 case Hexagon::A4_paslhfnew:
1768 case Hexagon::A4_paslht:
1769 case Hexagon::A4_paslhtnew:
1770 case Hexagon::A4_pasrhf:
1771 case Hexagon::A4_pasrhfnew:
1772 case Hexagon::A4_pasrht:
1773 case Hexagon::A4_pasrhtnew:
1774 case Hexagon::A4_psxtbf:
1775 case Hexagon::A4_psxtbfnew:
1776 case Hexagon::A4_psxtbt:
1777 case Hexagon::A4_psxtbtnew:
1778 case Hexagon::A4_psxthf:
1779 case Hexagon::A4_psxthfnew:
1780 case Hexagon::A4_psxtht:
1781 case Hexagon::A4_psxthtnew:
1782 case Hexagon::A4_pzxtbf:
1783 case Hexagon::A4_pzxtbfnew:
1784 case Hexagon::A4_pzxtbt:
1785 case Hexagon::A4_pzxtbtnew:
1786 case Hexagon::A4_pzxthf:
1787 case Hexagon::A4_pzxthfnew:
1788 case Hexagon::A4_pzxtht:
1789 case Hexagon::A4_pzxthtnew:
1790 case Hexagon::C2_ccombinewf:
1791 case Hexagon::C2_ccombinewt:
1792 return true;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001793 }
1794 return false;
1795}
1796
1797
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001798// FIXME - Function name and it's functionality don't match.
1799// It should be renamed to hasPredNewOpcode()
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001800bool HexagonInstrInfo::isConditionalLoad(const MachineInstr &MI) const {
1801 if (!MI.getDesc().mayLoad() || !isPredicated(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001802 return false;
1803
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001804 int PNewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001805 // Instruction with valid predicated-new opcode can be promoted to .new.
1806 return PNewOpcode >= 0;
1807}
1808
1809
1810// Returns true if an instruction is a conditional store.
1811//
1812// Note: It doesn't include conditional new-value stores as they can't be
1813// converted to .new predicate.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001814bool HexagonInstrInfo::isConditionalStore(const MachineInstr &MI) const {
1815 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001816 default: return false;
1817 case Hexagon::S4_storeirbt_io:
1818 case Hexagon::S4_storeirbf_io:
1819 case Hexagon::S4_pstorerbt_rr:
1820 case Hexagon::S4_pstorerbf_rr:
1821 case Hexagon::S2_pstorerbt_io:
1822 case Hexagon::S2_pstorerbf_io:
1823 case Hexagon::S2_pstorerbt_pi:
1824 case Hexagon::S2_pstorerbf_pi:
1825 case Hexagon::S2_pstorerdt_io:
1826 case Hexagon::S2_pstorerdf_io:
1827 case Hexagon::S4_pstorerdt_rr:
1828 case Hexagon::S4_pstorerdf_rr:
1829 case Hexagon::S2_pstorerdt_pi:
1830 case Hexagon::S2_pstorerdf_pi:
1831 case Hexagon::S2_pstorerht_io:
1832 case Hexagon::S2_pstorerhf_io:
1833 case Hexagon::S4_storeirht_io:
1834 case Hexagon::S4_storeirhf_io:
1835 case Hexagon::S4_pstorerht_rr:
1836 case Hexagon::S4_pstorerhf_rr:
1837 case Hexagon::S2_pstorerht_pi:
1838 case Hexagon::S2_pstorerhf_pi:
1839 case Hexagon::S2_pstorerit_io:
1840 case Hexagon::S2_pstorerif_io:
1841 case Hexagon::S4_storeirit_io:
1842 case Hexagon::S4_storeirif_io:
1843 case Hexagon::S4_pstorerit_rr:
1844 case Hexagon::S4_pstorerif_rr:
1845 case Hexagon::S2_pstorerit_pi:
1846 case Hexagon::S2_pstorerif_pi:
1847
1848 // V4 global address store before promoting to dot new.
1849 case Hexagon::S4_pstorerdt_abs:
1850 case Hexagon::S4_pstorerdf_abs:
1851 case Hexagon::S4_pstorerbt_abs:
1852 case Hexagon::S4_pstorerbf_abs:
1853 case Hexagon::S4_pstorerht_abs:
1854 case Hexagon::S4_pstorerhf_abs:
1855 case Hexagon::S4_pstorerit_abs:
1856 case Hexagon::S4_pstorerif_abs:
1857 return true;
1858
1859 // Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
1860 // from the "Conditional Store" list. Because a predicated new value store
1861 // would NOT be promoted to a double dot new store.
1862 // This function returns yes for those stores that are predicated but not
1863 // yet promoted to predicate dot new instructions.
1864 }
1865}
1866
1867
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001868bool HexagonInstrInfo::isConditionalTransfer(const MachineInstr &MI) const {
1869 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001870 case Hexagon::A2_tfrt:
1871 case Hexagon::A2_tfrf:
1872 case Hexagon::C2_cmoveit:
1873 case Hexagon::C2_cmoveif:
1874 case Hexagon::A2_tfrtnew:
1875 case Hexagon::A2_tfrfnew:
1876 case Hexagon::C2_cmovenewit:
1877 case Hexagon::C2_cmovenewif:
1878 case Hexagon::A2_tfrpt:
1879 case Hexagon::A2_tfrpf:
1880 return true;
1881
1882 default:
1883 return false;
1884 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001885 return false;
1886}
1887
1888
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001889// TODO: In order to have isExtendable for fpimm/f32Ext, we need to handle
1890// isFPImm and later getFPImm as well.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001891bool HexagonInstrInfo::isConstExtended(const MachineInstr &MI) const {
1892 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001893 unsigned isExtended = (F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
1894 if (isExtended) // Instruction must be extended.
Krzysztof Parzyszekc6f19332015-03-19 15:18:57 +00001895 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001896
1897 unsigned isExtendable =
1898 (F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
1899 if (!isExtendable)
1900 return false;
1901
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001902 if (MI.isCall())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001903 return false;
1904
1905 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001906 const MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001907 // Use MO operand flags to determine if MO
1908 // has the HMOTF_ConstExtended flag set.
1909 if (MO.getTargetFlags() && HexagonII::HMOTF_ConstExtended)
Brendon Cahoondf43e682015-05-08 16:16:29 +00001910 return true;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001911 // If this is a Machine BB address we are talking about, and it is
1912 // not marked as extended, say so.
1913 if (MO.isMBB())
1914 return false;
1915
1916 // We could be using an instruction with an extendable immediate and shoehorn
1917 // a global address into it. If it is a global address it will be constant
1918 // extended. We do this for COMBINE.
1919 // We currently only handle isGlobal() because it is the only kind of
1920 // object we are going to end up with here for now.
1921 // In the future we probably should add isSymbol(), etc.
1922 if (MO.isGlobal() || MO.isSymbol() || MO.isBlockAddress() ||
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001923 MO.isJTI() || MO.isCPI() || MO.isFPImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001924 return true;
1925
1926 // If the extendable operand is not 'Immediate' type, the instruction should
1927 // have 'isExtended' flag set.
1928 assert(MO.isImm() && "Extendable operand must be Immediate type");
1929
1930 int MinValue = getMinValue(MI);
1931 int MaxValue = getMaxValue(MI);
1932 int ImmValue = MO.getImm();
1933
1934 return (ImmValue < MinValue || ImmValue > MaxValue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001935}
1936
1937
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001938bool HexagonInstrInfo::isDeallocRet(const MachineInstr &MI) const {
1939 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001940 case Hexagon::L4_return :
1941 case Hexagon::L4_return_t :
1942 case Hexagon::L4_return_f :
1943 case Hexagon::L4_return_tnew_pnt :
1944 case Hexagon::L4_return_fnew_pnt :
1945 case Hexagon::L4_return_tnew_pt :
1946 case Hexagon::L4_return_fnew_pt :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001947 return true;
1948 }
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001949 return false;
1950}
1951
1952
1953// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001954bool HexagonInstrInfo::isDependent(const MachineInstr &ProdMI,
1955 const MachineInstr &ConsMI) const {
1956 if (!ProdMI.getDesc().getNumDefs())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001957 return false;
1958
1959 auto &HRI = getRegisterInfo();
1960
1961 SmallVector<unsigned, 4> DefsA;
1962 SmallVector<unsigned, 4> DefsB;
1963 SmallVector<unsigned, 8> UsesA;
1964 SmallVector<unsigned, 8> UsesB;
1965
1966 parseOperands(ProdMI, DefsA, UsesA);
1967 parseOperands(ConsMI, DefsB, UsesB);
1968
1969 for (auto &RegA : DefsA)
1970 for (auto &RegB : UsesB) {
1971 // True data dependency.
1972 if (RegA == RegB)
1973 return true;
1974
1975 if (Hexagon::DoubleRegsRegClass.contains(RegA))
1976 for (MCSubRegIterator SubRegs(RegA, &HRI); SubRegs.isValid(); ++SubRegs)
1977 if (RegB == *SubRegs)
1978 return true;
1979
1980 if (Hexagon::DoubleRegsRegClass.contains(RegB))
1981 for (MCSubRegIterator SubRegs(RegB, &HRI); SubRegs.isValid(); ++SubRegs)
1982 if (RegA == *SubRegs)
1983 return true;
1984 }
1985
1986 return false;
1987}
1988
1989
1990// Returns true if the instruction is alread a .cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001991bool HexagonInstrInfo::isDotCurInst(const MachineInstr &MI) const {
1992 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00001993 case Hexagon::V6_vL32b_cur_pi:
1994 case Hexagon::V6_vL32b_cur_ai:
1995 case Hexagon::V6_vL32b_cur_pi_128B:
1996 case Hexagon::V6_vL32b_cur_ai_128B:
1997 return true;
1998 }
1999 return false;
2000}
2001
2002
2003// Returns true, if any one of the operands is a dot new
2004// insn, whether it is predicated dot new or register dot new.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002005bool HexagonInstrInfo::isDotNewInst(const MachineInstr &MI) const {
2006 if (isNewValueInst(MI) || (isPredicated(MI) && isPredicatedNew(MI)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002007 return true;
2008
2009 return false;
2010}
2011
2012
2013/// Symmetrical. See if these two instructions are fit for duplex pair.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002014bool HexagonInstrInfo::isDuplexPair(const MachineInstr &MIa,
2015 const MachineInstr &MIb) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002016 HexagonII::SubInstructionGroup MIaG = getDuplexCandidateGroup(MIa);
2017 HexagonII::SubInstructionGroup MIbG = getDuplexCandidateGroup(MIb);
2018 return (isDuplexPairMatch(MIaG, MIbG) || isDuplexPairMatch(MIbG, MIaG));
2019}
2020
2021
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002022bool HexagonInstrInfo::isEarlySourceInstr(const MachineInstr &MI) const {
2023 if (MI.mayLoad() || MI.mayStore() || MI.isCompare())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002024 return true;
2025
2026 // Multiply
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002027 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002028 if (SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23)
2029 return true;
2030 return false;
2031}
2032
2033
2034bool HexagonInstrInfo::isEndLoopN(unsigned Opcode) const {
2035 return (Opcode == Hexagon::ENDLOOP0 ||
2036 Opcode == Hexagon::ENDLOOP1);
2037}
2038
2039
2040bool HexagonInstrInfo::isExpr(unsigned OpType) const {
2041 switch(OpType) {
2042 case MachineOperand::MO_MachineBasicBlock:
2043 case MachineOperand::MO_GlobalAddress:
2044 case MachineOperand::MO_ExternalSymbol:
2045 case MachineOperand::MO_JumpTableIndex:
2046 case MachineOperand::MO_ConstantPoolIndex:
2047 case MachineOperand::MO_BlockAddress:
2048 return true;
2049 default:
2050 return false;
2051 }
2052}
2053
2054
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002055bool HexagonInstrInfo::isExtendable(const MachineInstr &MI) const {
2056 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002057 const uint64_t F = MID.TSFlags;
2058 if ((F >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask)
2059 return true;
2060
2061 // TODO: This is largely obsolete now. Will need to be removed
2062 // in consecutive patches.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002063 switch (MI.getOpcode()) {
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002064 // PS_fi and PS_fia remain special cases.
2065 case Hexagon::PS_fi:
2066 case Hexagon::PS_fia:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002067 return true;
2068 default:
2069 return false;
2070 }
2071 return false;
2072}
2073
2074
2075// This returns true in two cases:
2076// - The OP code itself indicates that this is an extended instruction.
2077// - One of MOs has been marked with HMOTF_ConstExtended flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002078bool HexagonInstrInfo::isExtended(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002079 // First check if this is permanently extended op code.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002080 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002081 if ((F >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask)
2082 return true;
2083 // Use MO operand flags to determine if one of MI's operands
2084 // has HMOTF_ConstExtended flag set.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002085 for (MachineInstr::const_mop_iterator I = MI.operands_begin(),
2086 E = MI.operands_end(); I != E; ++I) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002087 if (I->getTargetFlags() && HexagonII::HMOTF_ConstExtended)
2088 return true;
2089 }
2090 return false;
2091}
2092
2093
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002094bool HexagonInstrInfo::isFloat(const MachineInstr &MI) const {
2095 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002096 const uint64_t F = get(Opcode).TSFlags;
2097 return (F >> HexagonII::FPPos) & HexagonII::FPMask;
2098}
2099
2100
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002101// No V60 HVX VMEM with A_INDIRECT.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002102bool HexagonInstrInfo::isHVXMemWithAIndirect(const MachineInstr &I,
2103 const MachineInstr &J) const {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002104 if (!isV60VectorInstruction(I))
2105 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002106 if (!I.mayLoad() && !I.mayStore())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002107 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002108 return J.isIndirectBranch() || isIndirectCall(J) || isIndirectL4Return(J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002109}
2110
2111
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002112bool HexagonInstrInfo::isIndirectCall(const MachineInstr &MI) const {
2113 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002114 case Hexagon::J2_callr :
2115 case Hexagon::J2_callrf :
2116 case Hexagon::J2_callrt :
2117 return true;
2118 }
2119 return false;
2120}
2121
2122
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002123bool HexagonInstrInfo::isIndirectL4Return(const MachineInstr &MI) const {
2124 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002125 case Hexagon::L4_return :
2126 case Hexagon::L4_return_t :
2127 case Hexagon::L4_return_f :
2128 case Hexagon::L4_return_fnew_pnt :
2129 case Hexagon::L4_return_fnew_pt :
2130 case Hexagon::L4_return_tnew_pnt :
2131 case Hexagon::L4_return_tnew_pt :
2132 return true;
2133 }
2134 return false;
2135}
2136
2137
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002138bool HexagonInstrInfo::isJumpR(const MachineInstr &MI) const {
2139 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002140 case Hexagon::J2_jumpr :
2141 case Hexagon::J2_jumprt :
2142 case Hexagon::J2_jumprf :
2143 case Hexagon::J2_jumprtnewpt :
2144 case Hexagon::J2_jumprfnewpt :
2145 case Hexagon::J2_jumprtnew :
2146 case Hexagon::J2_jumprfnew :
2147 return true;
2148 }
2149 return false;
2150}
2151
2152
2153// Return true if a given MI can accomodate given offset.
2154// Use abs estimate as oppose to the exact number.
2155// TODO: This will need to be changed to use MC level
2156// definition of instruction extendable field size.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002157bool HexagonInstrInfo::isJumpWithinBranchRange(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002158 unsigned offset) const {
2159 // This selection of jump instructions matches to that what
2160 // AnalyzeBranch can parse, plus NVJ.
2161 if (isNewValueJump(MI)) // r9:2
2162 return isInt<11>(offset);
2163
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002164 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002165 // Still missing Jump to address condition on register value.
2166 default:
2167 return false;
2168 case Hexagon::J2_jump: // bits<24> dst; // r22:2
2169 case Hexagon::J2_call:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002170 case Hexagon::PS_call_nr:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002171 return isInt<24>(offset);
2172 case Hexagon::J2_jumpt: //bits<17> dst; // r15:2
2173 case Hexagon::J2_jumpf:
2174 case Hexagon::J2_jumptnew:
2175 case Hexagon::J2_jumptnewpt:
2176 case Hexagon::J2_jumpfnew:
2177 case Hexagon::J2_jumpfnewpt:
2178 case Hexagon::J2_callt:
2179 case Hexagon::J2_callf:
2180 return isInt<17>(offset);
2181 case Hexagon::J2_loop0i:
2182 case Hexagon::J2_loop0iext:
2183 case Hexagon::J2_loop0r:
2184 case Hexagon::J2_loop0rext:
2185 case Hexagon::J2_loop1i:
2186 case Hexagon::J2_loop1iext:
2187 case Hexagon::J2_loop1r:
2188 case Hexagon::J2_loop1rext:
2189 return isInt<9>(offset);
2190 // TODO: Add all the compound branches here. Can we do this in Relation model?
2191 case Hexagon::J4_cmpeqi_tp0_jump_nt:
2192 case Hexagon::J4_cmpeqi_tp1_jump_nt:
2193 return isInt<11>(offset);
2194 }
2195}
2196
2197
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002198bool HexagonInstrInfo::isLateInstrFeedsEarlyInstr(const MachineInstr &LRMI,
2199 const MachineInstr &ESMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002200 bool isLate = isLateResultInstr(LRMI);
2201 bool isEarly = isEarlySourceInstr(ESMI);
2202
2203 DEBUG(dbgs() << "V60" << (isLate ? "-LR " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002204 DEBUG(LRMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002205 DEBUG(dbgs() << "V60" << (isEarly ? "-ES " : " -- "));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002206 DEBUG(ESMI.dump());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002207
2208 if (isLate && isEarly) {
2209 DEBUG(dbgs() << "++Is Late Result feeding Early Source\n");
2210 return true;
2211 }
2212
2213 return false;
2214}
2215
2216
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002217bool HexagonInstrInfo::isLateResultInstr(const MachineInstr &MI) const {
2218 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002219 case TargetOpcode::EXTRACT_SUBREG:
2220 case TargetOpcode::INSERT_SUBREG:
2221 case TargetOpcode::SUBREG_TO_REG:
2222 case TargetOpcode::REG_SEQUENCE:
2223 case TargetOpcode::IMPLICIT_DEF:
2224 case TargetOpcode::COPY:
2225 case TargetOpcode::INLINEASM:
2226 case TargetOpcode::PHI:
2227 return false;
2228 default:
2229 break;
2230 }
2231
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002232 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002233
2234 switch (SchedClass) {
2235 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2236 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2237 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2238 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2239 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2240 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2241 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2242 case Hexagon::Sched::V2LDST_tc_ld_SLOT01:
2243 case Hexagon::Sched::V2LDST_tc_st_SLOT0:
2244 case Hexagon::Sched::V2LDST_tc_st_SLOT01:
2245 case Hexagon::Sched::V4LDST_tc_ld_SLOT01:
2246 case Hexagon::Sched::V4LDST_tc_st_SLOT0:
2247 case Hexagon::Sched::V4LDST_tc_st_SLOT01:
2248 return false;
2249 }
2250 return true;
2251}
2252
2253
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002254bool HexagonInstrInfo::isLateSourceInstr(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002255 // Instructions with iclass A_CVI_VX and attribute A_CVI_LATE uses a multiply
2256 // resource, but all operands can be received late like an ALU instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002257 return MI.getDesc().getSchedClass() == Hexagon::Sched::CVI_VX_LATE;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002258}
2259
2260
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002261bool HexagonInstrInfo::isLoopN(const MachineInstr &MI) const {
2262 unsigned Opcode = MI.getOpcode();
Krzysztof Parzyszek5e6f2bd2015-12-14 21:32:25 +00002263 return Opcode == Hexagon::J2_loop0i ||
2264 Opcode == Hexagon::J2_loop0r ||
2265 Opcode == Hexagon::J2_loop0iext ||
2266 Opcode == Hexagon::J2_loop0rext ||
2267 Opcode == Hexagon::J2_loop1i ||
2268 Opcode == Hexagon::J2_loop1r ||
2269 Opcode == Hexagon::J2_loop1iext ||
2270 Opcode == Hexagon::J2_loop1rext;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002271}
2272
2273
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002274bool HexagonInstrInfo::isMemOp(const MachineInstr &MI) const {
2275 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002276 default: return false;
2277 case Hexagon::L4_iadd_memopw_io :
2278 case Hexagon::L4_isub_memopw_io :
2279 case Hexagon::L4_add_memopw_io :
2280 case Hexagon::L4_sub_memopw_io :
2281 case Hexagon::L4_and_memopw_io :
2282 case Hexagon::L4_or_memopw_io :
2283 case Hexagon::L4_iadd_memoph_io :
2284 case Hexagon::L4_isub_memoph_io :
2285 case Hexagon::L4_add_memoph_io :
2286 case Hexagon::L4_sub_memoph_io :
2287 case Hexagon::L4_and_memoph_io :
2288 case Hexagon::L4_or_memoph_io :
2289 case Hexagon::L4_iadd_memopb_io :
2290 case Hexagon::L4_isub_memopb_io :
2291 case Hexagon::L4_add_memopb_io :
2292 case Hexagon::L4_sub_memopb_io :
2293 case Hexagon::L4_and_memopb_io :
2294 case Hexagon::L4_or_memopb_io :
2295 case Hexagon::L4_ior_memopb_io:
2296 case Hexagon::L4_ior_memoph_io:
2297 case Hexagon::L4_ior_memopw_io:
2298 case Hexagon::L4_iand_memopb_io:
2299 case Hexagon::L4_iand_memoph_io:
2300 case Hexagon::L4_iand_memopw_io:
2301 return true;
2302 }
2303 return false;
2304}
2305
2306
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002307bool HexagonInstrInfo::isNewValue(const MachineInstr &MI) const {
2308 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002309 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2310}
2311
2312
2313bool HexagonInstrInfo::isNewValue(unsigned Opcode) const {
2314 const uint64_t F = get(Opcode).TSFlags;
2315 return (F >> HexagonII::NewValuePos) & HexagonII::NewValueMask;
2316}
2317
2318
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002319bool HexagonInstrInfo::isNewValueInst(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002320 return isNewValueJump(MI) || isNewValueStore(MI);
2321}
2322
2323
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002324bool HexagonInstrInfo::isNewValueJump(const MachineInstr &MI) const {
2325 return isNewValue(MI) && MI.isBranch();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002326}
2327
2328
2329bool HexagonInstrInfo::isNewValueJump(unsigned Opcode) const {
2330 return isNewValue(Opcode) && get(Opcode).isBranch() && isPredicated(Opcode);
2331}
2332
2333
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002334bool HexagonInstrInfo::isNewValueStore(const MachineInstr &MI) const {
2335 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002336 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2337}
2338
2339
2340bool HexagonInstrInfo::isNewValueStore(unsigned Opcode) const {
2341 const uint64_t F = get(Opcode).TSFlags;
2342 return (F >> HexagonII::NVStorePos) & HexagonII::NVStoreMask;
2343}
2344
2345
2346// Returns true if a particular operand is extendable for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002347bool HexagonInstrInfo::isOperandExtended(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002348 unsigned OperandNum) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002349 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002350 return ((F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask)
2351 == OperandNum;
2352}
2353
2354
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002355bool HexagonInstrInfo::isPredicatedNew(const MachineInstr &MI) const {
2356 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002357 assert(isPredicated(MI));
2358 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2359}
2360
2361
2362bool HexagonInstrInfo::isPredicatedNew(unsigned Opcode) const {
2363 const uint64_t F = get(Opcode).TSFlags;
2364 assert(isPredicated(Opcode));
2365 return (F >> HexagonII::PredicatedNewPos) & HexagonII::PredicatedNewMask;
2366}
2367
2368
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002369bool HexagonInstrInfo::isPredicatedTrue(const MachineInstr &MI) const {
2370 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002371 return !((F >> HexagonII::PredicatedFalsePos) &
2372 HexagonII::PredicatedFalseMask);
2373}
2374
2375
2376bool HexagonInstrInfo::isPredicatedTrue(unsigned Opcode) const {
2377 const uint64_t F = get(Opcode).TSFlags;
2378 // Make sure that the instruction is predicated.
2379 assert((F>> HexagonII::PredicatedPos) & HexagonII::PredicatedMask);
2380 return !((F >> HexagonII::PredicatedFalsePos) &
2381 HexagonII::PredicatedFalseMask);
2382}
2383
2384
2385bool HexagonInstrInfo::isPredicated(unsigned Opcode) const {
2386 const uint64_t F = get(Opcode).TSFlags;
2387 return (F >> HexagonII::PredicatedPos) & HexagonII::PredicatedMask;
2388}
2389
2390
2391bool HexagonInstrInfo::isPredicateLate(unsigned Opcode) const {
2392 const uint64_t F = get(Opcode).TSFlags;
2393 return ~(F >> HexagonII::PredicateLatePos) & HexagonII::PredicateLateMask;
2394}
2395
2396
2397bool HexagonInstrInfo::isPredictedTaken(unsigned Opcode) const {
2398 const uint64_t F = get(Opcode).TSFlags;
2399 assert(get(Opcode).isBranch() &&
2400 (isPredicatedNew(Opcode) || isNewValue(Opcode)));
2401 return (F >> HexagonII::TakenPos) & HexagonII::TakenMask;
2402}
2403
2404
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002405bool HexagonInstrInfo::isSaveCalleeSavedRegsCall(const MachineInstr &MI) const {
2406 return MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4 ||
2407 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT ||
2408 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_PIC ||
2409 MI.getOpcode() == Hexagon::SAVE_REGISTERS_CALL_V4_EXT_PIC;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002410}
2411
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002412bool HexagonInstrInfo::isSignExtendingLoad(const MachineInstr &MI) const {
2413 switch (MI.getOpcode()) {
2414 // Byte
2415 case Hexagon::L2_loadrb_io:
2416 case Hexagon::L4_loadrb_ur:
2417 case Hexagon::L4_loadrb_ap:
2418 case Hexagon::L2_loadrb_pr:
2419 case Hexagon::L2_loadrb_pbr:
2420 case Hexagon::L2_loadrb_pi:
2421 case Hexagon::L2_loadrb_pci:
2422 case Hexagon::L2_loadrb_pcr:
2423 case Hexagon::L2_loadbsw2_io:
2424 case Hexagon::L4_loadbsw2_ur:
2425 case Hexagon::L4_loadbsw2_ap:
2426 case Hexagon::L2_loadbsw2_pr:
2427 case Hexagon::L2_loadbsw2_pbr:
2428 case Hexagon::L2_loadbsw2_pi:
2429 case Hexagon::L2_loadbsw2_pci:
2430 case Hexagon::L2_loadbsw2_pcr:
2431 case Hexagon::L2_loadbsw4_io:
2432 case Hexagon::L4_loadbsw4_ur:
2433 case Hexagon::L4_loadbsw4_ap:
2434 case Hexagon::L2_loadbsw4_pr:
2435 case Hexagon::L2_loadbsw4_pbr:
2436 case Hexagon::L2_loadbsw4_pi:
2437 case Hexagon::L2_loadbsw4_pci:
2438 case Hexagon::L2_loadbsw4_pcr:
2439 case Hexagon::L4_loadrb_rr:
2440 case Hexagon::L2_ploadrbt_io:
2441 case Hexagon::L2_ploadrbt_pi:
2442 case Hexagon::L2_ploadrbf_io:
2443 case Hexagon::L2_ploadrbf_pi:
2444 case Hexagon::L2_ploadrbtnew_io:
2445 case Hexagon::L2_ploadrbfnew_io:
2446 case Hexagon::L4_ploadrbt_rr:
2447 case Hexagon::L4_ploadrbf_rr:
2448 case Hexagon::L4_ploadrbtnew_rr:
2449 case Hexagon::L4_ploadrbfnew_rr:
2450 case Hexagon::L2_ploadrbtnew_pi:
2451 case Hexagon::L2_ploadrbfnew_pi:
2452 case Hexagon::L4_ploadrbt_abs:
2453 case Hexagon::L4_ploadrbf_abs:
2454 case Hexagon::L4_ploadrbtnew_abs:
2455 case Hexagon::L4_ploadrbfnew_abs:
2456 case Hexagon::L2_loadrbgp:
2457 // Half
2458 case Hexagon::L2_loadrh_io:
2459 case Hexagon::L4_loadrh_ur:
2460 case Hexagon::L4_loadrh_ap:
2461 case Hexagon::L2_loadrh_pr:
2462 case Hexagon::L2_loadrh_pbr:
2463 case Hexagon::L2_loadrh_pi:
2464 case Hexagon::L2_loadrh_pci:
2465 case Hexagon::L2_loadrh_pcr:
2466 case Hexagon::L4_loadrh_rr:
2467 case Hexagon::L2_ploadrht_io:
2468 case Hexagon::L2_ploadrht_pi:
2469 case Hexagon::L2_ploadrhf_io:
2470 case Hexagon::L2_ploadrhf_pi:
2471 case Hexagon::L2_ploadrhtnew_io:
2472 case Hexagon::L2_ploadrhfnew_io:
2473 case Hexagon::L4_ploadrht_rr:
2474 case Hexagon::L4_ploadrhf_rr:
2475 case Hexagon::L4_ploadrhtnew_rr:
2476 case Hexagon::L4_ploadrhfnew_rr:
2477 case Hexagon::L2_ploadrhtnew_pi:
2478 case Hexagon::L2_ploadrhfnew_pi:
2479 case Hexagon::L4_ploadrht_abs:
2480 case Hexagon::L4_ploadrhf_abs:
2481 case Hexagon::L4_ploadrhtnew_abs:
2482 case Hexagon::L4_ploadrhfnew_abs:
2483 case Hexagon::L2_loadrhgp:
2484 return true;
2485 default:
2486 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002487 }
2488}
2489
2490
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002491bool HexagonInstrInfo::isSolo(const MachineInstr &MI) const {
2492 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002493 return (F >> HexagonII::SoloPos) & HexagonII::SoloMask;
2494}
2495
2496
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002497bool HexagonInstrInfo::isSpillPredRegOp(const MachineInstr &MI) const {
2498 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002499 case Hexagon::STriw_pred :
2500 case Hexagon::LDriw_pred :
2501 return true;
2502 default:
2503 return false;
2504 }
2505}
2506
2507
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002508bool HexagonInstrInfo::isTailCall(const MachineInstr &MI) const {
2509 if (!MI.isBranch())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002510 return false;
2511
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002512 for (auto &Op : MI.operands())
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00002513 if (Op.isGlobal() || Op.isSymbol())
2514 return true;
2515 return false;
2516}
2517
2518
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002519// Returns true when SU has a timing class TC1.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002520bool HexagonInstrInfo::isTC1(const MachineInstr &MI) const {
2521 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002522 switch (SchedClass) {
2523 case Hexagon::Sched::ALU32_2op_tc_1_SLOT0123:
2524 case Hexagon::Sched::ALU32_3op_tc_1_SLOT0123:
2525 case Hexagon::Sched::ALU32_ADDI_tc_1_SLOT0123:
2526 case Hexagon::Sched::ALU64_tc_1_SLOT23:
2527 case Hexagon::Sched::EXTENDER_tc_1_SLOT0123:
2528 //case Hexagon::Sched::M_tc_1_SLOT23:
2529 case Hexagon::Sched::S_2op_tc_1_SLOT23:
2530 case Hexagon::Sched::S_3op_tc_1_SLOT23:
2531 return true;
2532
2533 default:
2534 return false;
2535 }
2536}
2537
2538
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002539bool HexagonInstrInfo::isTC2(const MachineInstr &MI) const {
2540 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002541 switch (SchedClass) {
2542 case Hexagon::Sched::ALU32_3op_tc_2_SLOT0123:
2543 case Hexagon::Sched::ALU64_tc_2_SLOT23:
2544 case Hexagon::Sched::CR_tc_2_SLOT3:
2545 case Hexagon::Sched::M_tc_2_SLOT23:
2546 case Hexagon::Sched::S_2op_tc_2_SLOT23:
2547 case Hexagon::Sched::S_3op_tc_2_SLOT23:
2548 return true;
2549
2550 default:
2551 return false;
2552 }
2553}
2554
2555
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002556bool HexagonInstrInfo::isTC2Early(const MachineInstr &MI) const {
2557 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002558 switch (SchedClass) {
2559 case Hexagon::Sched::ALU32_2op_tc_2early_SLOT0123:
2560 case Hexagon::Sched::ALU32_3op_tc_2early_SLOT0123:
2561 case Hexagon::Sched::ALU64_tc_2early_SLOT23:
2562 case Hexagon::Sched::CR_tc_2early_SLOT23:
2563 case Hexagon::Sched::CR_tc_2early_SLOT3:
2564 case Hexagon::Sched::J_tc_2early_SLOT0123:
2565 case Hexagon::Sched::J_tc_2early_SLOT2:
2566 case Hexagon::Sched::J_tc_2early_SLOT23:
2567 case Hexagon::Sched::S_2op_tc_2early_SLOT23:
2568 case Hexagon::Sched::S_3op_tc_2early_SLOT23:
2569 return true;
2570
2571 default:
2572 return false;
2573 }
2574}
2575
2576
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002577bool HexagonInstrInfo::isTC4x(const MachineInstr &MI) const {
2578 unsigned SchedClass = MI.getDesc().getSchedClass();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002579 return SchedClass == Hexagon::Sched::M_tc_3or4x_SLOT23;
2580}
2581
2582
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002583// Schedule this ASAP.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002584bool HexagonInstrInfo::isToBeScheduledASAP(const MachineInstr &MI1,
2585 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002586 if (mayBeCurLoad(MI1)) {
2587 // if (result of SU is used in Next) return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002588 unsigned DstReg = MI1.getOperand(0).getReg();
2589 int N = MI2.getNumOperands();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002590 for (int I = 0; I < N; I++)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002591 if (MI2.getOperand(I).isReg() && DstReg == MI2.getOperand(I).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002592 return true;
2593 }
2594 if (mayBeNewStore(MI2))
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002595 if (MI2.getOpcode() == Hexagon::V6_vS32b_pi)
2596 if (MI1.getOperand(0).isReg() && MI2.getOperand(3).isReg() &&
2597 MI1.getOperand(0).getReg() == MI2.getOperand(3).getReg())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002598 return true;
2599 return false;
2600}
2601
2602
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002603bool HexagonInstrInfo::isV60VectorInstruction(const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002604 const uint64_t V = getType(MI);
2605 return HexagonII::TypeCVI_FIRST <= V && V <= HexagonII::TypeCVI_LAST;
2606}
2607
2608
2609// Check if the Offset is a valid auto-inc imm by Load/Store Type.
2610//
2611bool HexagonInstrInfo::isValidAutoIncImm(const EVT VT, const int Offset) const {
2612 if (VT == MVT::v16i32 || VT == MVT::v8i64 ||
2613 VT == MVT::v32i16 || VT == MVT::v64i8) {
2614 return (Offset >= Hexagon_MEMV_AUTOINC_MIN &&
2615 Offset <= Hexagon_MEMV_AUTOINC_MAX &&
2616 (Offset & 0x3f) == 0);
2617 }
2618 // 128B
2619 if (VT == MVT::v32i32 || VT == MVT::v16i64 ||
2620 VT == MVT::v64i16 || VT == MVT::v128i8) {
2621 return (Offset >= Hexagon_MEMV_AUTOINC_MIN_128B &&
2622 Offset <= Hexagon_MEMV_AUTOINC_MAX_128B &&
2623 (Offset & 0x7f) == 0);
2624 }
2625 if (VT == MVT::i64) {
2626 return (Offset >= Hexagon_MEMD_AUTOINC_MIN &&
2627 Offset <= Hexagon_MEMD_AUTOINC_MAX &&
2628 (Offset & 0x7) == 0);
2629 }
2630 if (VT == MVT::i32) {
2631 return (Offset >= Hexagon_MEMW_AUTOINC_MIN &&
2632 Offset <= Hexagon_MEMW_AUTOINC_MAX &&
2633 (Offset & 0x3) == 0);
2634 }
2635 if (VT == MVT::i16) {
2636 return (Offset >= Hexagon_MEMH_AUTOINC_MIN &&
2637 Offset <= Hexagon_MEMH_AUTOINC_MAX &&
2638 (Offset & 0x1) == 0);
2639 }
2640 if (VT == MVT::i8) {
2641 return (Offset >= Hexagon_MEMB_AUTOINC_MIN &&
2642 Offset <= Hexagon_MEMB_AUTOINC_MAX);
2643 }
2644 llvm_unreachable("Not an auto-inc opc!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002645}
2646
2647
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002648bool HexagonInstrInfo::isValidOffset(unsigned Opcode, int Offset,
2649 bool Extend) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002650 // This function is to check whether the "Offset" is in the correct range of
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002651 // the given "Opcode". If "Offset" is not in the correct range, "A2_addi" is
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002652 // inserted to calculate the final address. Due to this reason, the function
2653 // assumes that the "Offset" has correct alignment.
Jyotsna Vermaec613662013-03-14 19:08:03 +00002654 // We used to assert if the offset was not properly aligned, however,
2655 // there are cases where a misaligned pointer recast can cause this
2656 // problem, and we need to allow for it. The front end warns of such
2657 // misaligns with respect to load size.
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002658
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002659 switch (Opcode) {
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002660 case Hexagon::PS_vstorerq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002661 case Hexagon::PS_vstorerw_ai:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002662 case Hexagon::PS_vloadrq_ai:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002663 case Hexagon::PS_vloadrw_ai:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002664 case Hexagon::V6_vL32b_ai:
2665 case Hexagon::V6_vS32b_ai:
2666 case Hexagon::V6_vL32Ub_ai:
2667 case Hexagon::V6_vS32Ub_ai:
2668 return (Offset >= Hexagon_MEMV_OFFSET_MIN) &&
2669 (Offset <= Hexagon_MEMV_OFFSET_MAX);
2670
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002671 case Hexagon::PS_vstorerq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002672 case Hexagon::PS_vstorerw_ai_128B:
Krzysztof Parzyszek17aa4132016-08-16 15:43:54 +00002673 case Hexagon::PS_vloadrq_ai_128B:
Krzysztof Parzyszekf2859632016-08-12 21:05:05 +00002674 case Hexagon::PS_vloadrw_ai_128B:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002675 case Hexagon::V6_vL32b_ai_128B:
2676 case Hexagon::V6_vS32b_ai_128B:
2677 case Hexagon::V6_vL32Ub_ai_128B:
2678 case Hexagon::V6_vS32Ub_ai_128B:
2679 return (Offset >= Hexagon_MEMV_OFFSET_MIN_128B) &&
2680 (Offset <= Hexagon_MEMV_OFFSET_MAX_128B);
2681
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002682 case Hexagon::J2_loop0i:
2683 case Hexagon::J2_loop1i:
2684 return isUInt<10>(Offset);
Krzysztof Parzyszekbba0bf72016-07-15 15:35:52 +00002685
2686 case Hexagon::S4_storeirb_io:
2687 case Hexagon::S4_storeirbt_io:
2688 case Hexagon::S4_storeirbf_io:
2689 return isUInt<6>(Offset);
2690
2691 case Hexagon::S4_storeirh_io:
2692 case Hexagon::S4_storeirht_io:
2693 case Hexagon::S4_storeirhf_io:
2694 return isShiftedUInt<6,1>(Offset);
2695
2696 case Hexagon::S4_storeiri_io:
2697 case Hexagon::S4_storeirit_io:
2698 case Hexagon::S4_storeirif_io:
2699 return isShiftedUInt<6,2>(Offset);
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002700 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002701
Krzysztof Parzyszek05902162015-04-22 17:51:26 +00002702 if (Extend)
2703 return true;
2704
2705 switch (Opcode) {
Colin LeMahieu026e88d2014-12-23 20:02:16 +00002706 case Hexagon::L2_loadri_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002707 case Hexagon::S2_storeri_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002708 return (Offset >= Hexagon_MEMW_OFFSET_MIN) &&
2709 (Offset <= Hexagon_MEMW_OFFSET_MAX);
2710
Colin LeMahieu947cd702014-12-23 20:44:59 +00002711 case Hexagon::L2_loadrd_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002712 case Hexagon::S2_storerd_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002713 return (Offset >= Hexagon_MEMD_OFFSET_MIN) &&
2714 (Offset <= Hexagon_MEMD_OFFSET_MAX);
2715
Colin LeMahieu8e39cad2014-12-23 17:25:57 +00002716 case Hexagon::L2_loadrh_io:
Colin LeMahieua9386d22014-12-23 16:42:57 +00002717 case Hexagon::L2_loadruh_io:
Colin LeMahieubda31b42014-12-29 20:44:51 +00002718 case Hexagon::S2_storerh_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002719 return (Offset >= Hexagon_MEMH_OFFSET_MIN) &&
2720 (Offset <= Hexagon_MEMH_OFFSET_MAX);
2721
Colin LeMahieu4b1eac42014-12-22 21:40:43 +00002722 case Hexagon::L2_loadrb_io:
Colin LeMahieuaf1e5de2014-12-22 21:20:03 +00002723 case Hexagon::L2_loadrub_io:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002724 case Hexagon::S2_storerb_io:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002725 return (Offset >= Hexagon_MEMB_OFFSET_MIN) &&
2726 (Offset <= Hexagon_MEMB_OFFSET_MAX);
2727
Colin LeMahieuf297dbe2015-02-05 17:49:13 +00002728 case Hexagon::A2_addi:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002729 return (Offset >= Hexagon_ADDI_OFFSET_MIN) &&
2730 (Offset <= Hexagon_ADDI_OFFSET_MAX);
2731
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002732 case Hexagon::L4_iadd_memopw_io :
2733 case Hexagon::L4_isub_memopw_io :
2734 case Hexagon::L4_add_memopw_io :
2735 case Hexagon::L4_sub_memopw_io :
2736 case Hexagon::L4_and_memopw_io :
2737 case Hexagon::L4_or_memopw_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002738 return (0 <= Offset && Offset <= 255);
2739
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002740 case Hexagon::L4_iadd_memoph_io :
2741 case Hexagon::L4_isub_memoph_io :
2742 case Hexagon::L4_add_memoph_io :
2743 case Hexagon::L4_sub_memoph_io :
2744 case Hexagon::L4_and_memoph_io :
2745 case Hexagon::L4_or_memoph_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002746 return (0 <= Offset && Offset <= 127);
2747
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002748 case Hexagon::L4_iadd_memopb_io :
2749 case Hexagon::L4_isub_memopb_io :
2750 case Hexagon::L4_add_memopb_io :
2751 case Hexagon::L4_sub_memopb_io :
2752 case Hexagon::L4_and_memopb_io :
2753 case Hexagon::L4_or_memopb_io :
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002754 return (0 <= Offset && Offset <= 63);
2755
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002756 // LDriw_xxx and STriw_xxx are pseudo operations, so it has to take offset of
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002757 // any size. Later pass knows how to handle it.
2758 case Hexagon::STriw_pred:
2759 case Hexagon::LDriw_pred:
Krzysztof Parzyszek7b413c62016-01-22 19:15:58 +00002760 case Hexagon::STriw_mod:
2761 case Hexagon::LDriw_mod:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002762 return true;
2763
Krzysztof Parzyszek1d01a792016-08-16 18:08:40 +00002764 case Hexagon::PS_fi:
2765 case Hexagon::PS_fia:
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002766 case Hexagon::INLINEASM:
2767 return true;
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002768
2769 case Hexagon::L2_ploadrbt_io:
2770 case Hexagon::L2_ploadrbf_io:
2771 case Hexagon::L2_ploadrubt_io:
2772 case Hexagon::L2_ploadrubf_io:
2773 case Hexagon::S2_pstorerbt_io:
2774 case Hexagon::S2_pstorerbf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002775 return isUInt<6>(Offset);
2776
2777 case Hexagon::L2_ploadrht_io:
2778 case Hexagon::L2_ploadrhf_io:
2779 case Hexagon::L2_ploadruht_io:
2780 case Hexagon::L2_ploadruhf_io:
2781 case Hexagon::S2_pstorerht_io:
2782 case Hexagon::S2_pstorerhf_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002783 return isShiftedUInt<6,1>(Offset);
2784
2785 case Hexagon::L2_ploadrit_io:
2786 case Hexagon::L2_ploadrif_io:
2787 case Hexagon::S2_pstorerit_io:
2788 case Hexagon::S2_pstorerif_io:
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +00002789 return isShiftedUInt<6,2>(Offset);
2790
2791 case Hexagon::L2_ploadrdt_io:
2792 case Hexagon::L2_ploadrdf_io:
2793 case Hexagon::S2_pstorerdt_io:
2794 case Hexagon::S2_pstorerdf_io:
2795 return isShiftedUInt<6,3>(Offset);
2796 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002797
Benjamin Kramerb6684012011-12-27 11:41:05 +00002798 llvm_unreachable("No offset range is defined for this opcode. "
2799 "Please define it in the above switch statement!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002800}
2801
2802
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002803bool HexagonInstrInfo::isVecAcc(const MachineInstr &MI) const {
2804 return isV60VectorInstruction(MI) && isAccumulator(MI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002805}
2806
2807
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002808bool HexagonInstrInfo::isVecALU(const MachineInstr &MI) const {
2809 const uint64_t F = get(MI.getOpcode()).TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002810 const uint64_t V = ((F >> HexagonII::TypePos) & HexagonII::TypeMask);
2811 return
2812 V == HexagonII::TypeCVI_VA ||
2813 V == HexagonII::TypeCVI_VA_DV;
2814}
Andrew Trickd06df962012-02-01 22:13:57 +00002815
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002816
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002817bool HexagonInstrInfo::isVecUsableNextPacket(const MachineInstr &ProdMI,
2818 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002819 if (EnableACCForwarding && isVecAcc(ProdMI) && isVecAcc(ConsMI))
2820 return true;
2821
2822 if (EnableALUForwarding && (isVecALU(ConsMI) || isLateSourceInstr(ConsMI)))
2823 return true;
2824
2825 if (mayBeNewStore(ConsMI))
Andrew Trickd06df962012-02-01 22:13:57 +00002826 return true;
2827
2828 return false;
2829}
Jyotsna Verma84256432013-03-01 17:37:13 +00002830
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002831bool HexagonInstrInfo::isZeroExtendingLoad(const MachineInstr &MI) const {
2832 switch (MI.getOpcode()) {
2833 // Byte
2834 case Hexagon::L2_loadrub_io:
2835 case Hexagon::L4_loadrub_ur:
2836 case Hexagon::L4_loadrub_ap:
2837 case Hexagon::L2_loadrub_pr:
2838 case Hexagon::L2_loadrub_pbr:
2839 case Hexagon::L2_loadrub_pi:
2840 case Hexagon::L2_loadrub_pci:
2841 case Hexagon::L2_loadrub_pcr:
2842 case Hexagon::L2_loadbzw2_io:
2843 case Hexagon::L4_loadbzw2_ur:
2844 case Hexagon::L4_loadbzw2_ap:
2845 case Hexagon::L2_loadbzw2_pr:
2846 case Hexagon::L2_loadbzw2_pbr:
2847 case Hexagon::L2_loadbzw2_pi:
2848 case Hexagon::L2_loadbzw2_pci:
2849 case Hexagon::L2_loadbzw2_pcr:
2850 case Hexagon::L2_loadbzw4_io:
2851 case Hexagon::L4_loadbzw4_ur:
2852 case Hexagon::L4_loadbzw4_ap:
2853 case Hexagon::L2_loadbzw4_pr:
2854 case Hexagon::L2_loadbzw4_pbr:
2855 case Hexagon::L2_loadbzw4_pi:
2856 case Hexagon::L2_loadbzw4_pci:
2857 case Hexagon::L2_loadbzw4_pcr:
2858 case Hexagon::L4_loadrub_rr:
2859 case Hexagon::L2_ploadrubt_io:
2860 case Hexagon::L2_ploadrubt_pi:
2861 case Hexagon::L2_ploadrubf_io:
2862 case Hexagon::L2_ploadrubf_pi:
2863 case Hexagon::L2_ploadrubtnew_io:
2864 case Hexagon::L2_ploadrubfnew_io:
2865 case Hexagon::L4_ploadrubt_rr:
2866 case Hexagon::L4_ploadrubf_rr:
2867 case Hexagon::L4_ploadrubtnew_rr:
2868 case Hexagon::L4_ploadrubfnew_rr:
2869 case Hexagon::L2_ploadrubtnew_pi:
2870 case Hexagon::L2_ploadrubfnew_pi:
2871 case Hexagon::L4_ploadrubt_abs:
2872 case Hexagon::L4_ploadrubf_abs:
2873 case Hexagon::L4_ploadrubtnew_abs:
2874 case Hexagon::L4_ploadrubfnew_abs:
2875 case Hexagon::L2_loadrubgp:
2876 // Half
2877 case Hexagon::L2_loadruh_io:
2878 case Hexagon::L4_loadruh_ur:
2879 case Hexagon::L4_loadruh_ap:
2880 case Hexagon::L2_loadruh_pr:
2881 case Hexagon::L2_loadruh_pbr:
2882 case Hexagon::L2_loadruh_pi:
2883 case Hexagon::L2_loadruh_pci:
2884 case Hexagon::L2_loadruh_pcr:
2885 case Hexagon::L4_loadruh_rr:
2886 case Hexagon::L2_ploadruht_io:
2887 case Hexagon::L2_ploadruht_pi:
2888 case Hexagon::L2_ploadruhf_io:
2889 case Hexagon::L2_ploadruhf_pi:
2890 case Hexagon::L2_ploadruhtnew_io:
2891 case Hexagon::L2_ploadruhfnew_io:
2892 case Hexagon::L4_ploadruht_rr:
2893 case Hexagon::L4_ploadruhf_rr:
2894 case Hexagon::L4_ploadruhtnew_rr:
2895 case Hexagon::L4_ploadruhfnew_rr:
2896 case Hexagon::L2_ploadruhtnew_pi:
2897 case Hexagon::L2_ploadruhfnew_pi:
2898 case Hexagon::L4_ploadruht_abs:
2899 case Hexagon::L4_ploadruhf_abs:
2900 case Hexagon::L4_ploadruhtnew_abs:
2901 case Hexagon::L4_ploadruhfnew_abs:
2902 case Hexagon::L2_loadruhgp:
2903 return true;
2904 default:
2905 return false;
Krzysztof Parzyszekfd02aad2016-02-12 18:37:23 +00002906 }
2907}
2908
2909
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002910// Add latency to instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002911bool HexagonInstrInfo::addLatencyToSchedule(const MachineInstr &MI1,
2912 const MachineInstr &MI2) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +00002913 if (isV60VectorInstruction(MI1) && isV60VectorInstruction(MI2))
2914 if (!isVecUsableNextPacket(MI1, MI2))
2915 return true;
2916 return false;
2917}
2918
2919
Brendon Cahoon254f8892016-07-29 16:44:44 +00002920/// \brief Get the base register and byte offset of a load/store instr.
2921bool HexagonInstrInfo::getMemOpBaseRegImmOfs(MachineInstr &LdSt,
2922 unsigned &BaseReg, int64_t &Offset, const TargetRegisterInfo *TRI)
2923 const {
2924 unsigned AccessSize = 0;
2925 int OffsetVal = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002926 BaseReg = getBaseAndOffset(LdSt, OffsetVal, AccessSize);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002927 Offset = OffsetVal;
2928 return BaseReg != 0;
2929}
2930
2931
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002932/// \brief Can these instructions execute at the same time in a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002933bool HexagonInstrInfo::canExecuteInBundle(const MachineInstr &First,
2934 const MachineInstr &Second) const {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002935 if (DisableNVSchedule)
2936 return false;
2937 if (mayBeNewStore(Second)) {
2938 // Make sure the definition of the first instruction is the value being
2939 // stored.
2940 const MachineOperand &Stored =
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002941 Second.getOperand(Second.getNumOperands() - 1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002942 if (!Stored.isReg())
2943 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002944 for (unsigned i = 0, e = First.getNumOperands(); i < e; ++i) {
2945 const MachineOperand &Op = First.getOperand(i);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00002946 if (Op.isReg() && Op.isDef() && Op.getReg() == Stored.getReg())
2947 return true;
2948 }
2949 }
2950 return false;
2951}
2952
2953
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002954bool HexagonInstrInfo::doesNotReturn(const MachineInstr &CallMI) const {
2955 unsigned Opc = CallMI.getOpcode();
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00002956 return Opc == Hexagon::PS_call_nr || Opc == Hexagon::PS_callr_nr;
Krzysztof Parzyszek1b689da2016-08-11 21:14:25 +00002957}
2958
2959
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002960bool HexagonInstrInfo::hasEHLabel(const MachineBasicBlock *B) const {
2961 for (auto &I : *B)
2962 if (I.isEHLabel())
2963 return true;
2964 return false;
Jyotsna Verma84256432013-03-01 17:37:13 +00002965}
2966
Jyotsna Verma84256432013-03-01 17:37:13 +00002967
2968// Returns true if an instruction can be converted into a non-extended
2969// equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002970bool HexagonInstrInfo::hasNonExtEquivalent(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00002971 short NonExtOpcode;
2972 // Check if the instruction has a register form that uses register in place
2973 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002974 if (Hexagon::getRegForm(MI.getOpcode()) >= 0)
Jyotsna Verma84256432013-03-01 17:37:13 +00002975 return true;
2976
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002977 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00002978 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00002979
2980 switch (getAddrMode(MI)) {
2981 case HexagonII::Absolute :
2982 // Load/store with absolute addressing mode can be converted into
2983 // base+offset mode.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002984 NonExtOpcode = Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002985 break;
2986 case HexagonII::BaseImmOffset :
2987 // Load/store with base+offset addressing mode can be converted into
2988 // base+register offset addressing mode. However left shift operand should
2989 // be set to 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002990 NonExtOpcode = Hexagon::getBaseWithRegOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00002991 break;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002992 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00002993 NonExtOpcode = Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00002994 break;
Jyotsna Verma84256432013-03-01 17:37:13 +00002995 default:
2996 return false;
2997 }
2998 if (NonExtOpcode < 0)
2999 return false;
3000 return true;
3001 }
3002 return false;
3003}
3004
Jyotsna Verma84256432013-03-01 17:37:13 +00003005
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003006bool HexagonInstrInfo::hasPseudoInstrPair(const MachineInstr &MI) const {
3007 return Hexagon::getRealHWInstr(MI.getOpcode(),
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003008 Hexagon::InstrType_Pseudo) >= 0;
3009}
3010
3011
3012bool HexagonInstrInfo::hasUncondBranch(const MachineBasicBlock *B)
3013 const {
3014 MachineBasicBlock::const_iterator I = B->getFirstTerminator(), E = B->end();
3015 while (I != E) {
3016 if (I->isBarrier())
3017 return true;
3018 ++I;
3019 }
3020 return false;
3021}
3022
3023
3024// Returns true, if a LD insn can be promoted to a cur load.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003025bool HexagonInstrInfo::mayBeCurLoad(const MachineInstr &MI) const {
3026 auto &HST = MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>();
3027 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003028 return ((F >> HexagonII::mayCVLoadPos) & HexagonII::mayCVLoadMask) &&
3029 HST.hasV60TOps();
3030}
3031
3032
3033// Returns true, if a ST insn can be promoted to a new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003034bool HexagonInstrInfo::mayBeNewStore(const MachineInstr &MI) const {
3035 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003036 return (F >> HexagonII::mayNVStorePos) & HexagonII::mayNVStoreMask;
3037}
3038
3039
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003040bool HexagonInstrInfo::producesStall(const MachineInstr &ProdMI,
3041 const MachineInstr &ConsMI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003042 // There is no stall when ProdMI is not a V60 vector.
3043 if (!isV60VectorInstruction(ProdMI))
3044 return false;
3045
3046 // There is no stall when ProdMI and ConsMI are not dependent.
3047 if (!isDependent(ProdMI, ConsMI))
3048 return false;
3049
3050 // When Forward Scheduling is enabled, there is no stall if ProdMI and ConsMI
3051 // are scheduled in consecutive packets.
3052 if (isVecUsableNextPacket(ProdMI, ConsMI))
3053 return false;
3054
3055 return true;
3056}
3057
3058
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003059bool HexagonInstrInfo::producesStall(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003060 MachineBasicBlock::const_instr_iterator BII) const {
3061 // There is no stall when I is not a V60 vector.
3062 if (!isV60VectorInstruction(MI))
3063 return false;
3064
3065 MachineBasicBlock::const_instr_iterator MII = BII;
3066 MachineBasicBlock::const_instr_iterator MIE = MII->getParent()->instr_end();
3067
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003068 if (!MII->isBundle()) {
3069 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003070 if (!isV60VectorInstruction(J))
3071 return false;
3072 else if (isVecUsableNextPacket(J, MI))
3073 return false;
3074 return true;
3075 }
3076
3077 for (++MII; MII != MIE && MII->isInsideBundle(); ++MII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003078 const MachineInstr &J = *MII;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003079 if (producesStall(J, MI))
3080 return true;
3081 }
3082 return false;
3083}
3084
3085
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003086bool HexagonInstrInfo::predCanBeUsedAsDotNew(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003087 unsigned PredReg) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003088 for (unsigned opNum = 0; opNum < MI.getNumOperands(); opNum++) {
3089 const MachineOperand &MO = MI.getOperand(opNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003090 if (MO.isReg() && MO.isDef() && MO.isImplicit() && (MO.getReg() == PredReg))
3091 return false; // Predicate register must be explicitly defined.
3092 }
3093
3094 // Hexagon Programmer's Reference says that decbin, memw_locked, and
3095 // memd_locked cannot be used as .new as well,
3096 // but we don't seem to have these instructions defined.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003097 return MI.getOpcode() != Hexagon::A4_tlbmatch;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003098}
3099
3100
3101bool HexagonInstrInfo::PredOpcodeHasJMP_c(unsigned Opcode) const {
3102 return (Opcode == Hexagon::J2_jumpt) ||
3103 (Opcode == Hexagon::J2_jumpf) ||
3104 (Opcode == Hexagon::J2_jumptnew) ||
3105 (Opcode == Hexagon::J2_jumpfnew) ||
3106 (Opcode == Hexagon::J2_jumptnewpt) ||
3107 (Opcode == Hexagon::J2_jumpfnewpt);
3108}
3109
3110
3111bool HexagonInstrInfo::predOpcodeHasNot(ArrayRef<MachineOperand> Cond) const {
3112 if (Cond.empty() || !isPredicated(Cond[0].getImm()))
3113 return false;
3114 return !isPredicatedTrue(Cond[0].getImm());
3115}
3116
3117
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003118short HexagonInstrInfo::getAbsoluteForm(const MachineInstr &MI) const {
3119 return Hexagon::getAbsoluteForm(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003120}
3121
3122
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003123unsigned HexagonInstrInfo::getAddrMode(const MachineInstr &MI) const {
3124 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003125 return (F >> HexagonII::AddrModePos) & HexagonII::AddrModeMask;
3126}
3127
3128
3129// Returns the base register in a memory access (load/store). The offset is
3130// returned in Offset and the access size is returned in AccessSize.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003131unsigned HexagonInstrInfo::getBaseAndOffset(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003132 int &Offset, unsigned &AccessSize) const {
3133 // Return if it is not a base+offset type instruction or a MemOp.
3134 if (getAddrMode(MI) != HexagonII::BaseImmOffset &&
3135 getAddrMode(MI) != HexagonII::BaseLongOffset &&
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003136 !isMemOp(MI) && !isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003137 return 0;
3138
3139 // Since it is a memory access instruction, getMemAccessSize() should never
3140 // return 0.
3141 assert (getMemAccessSize(MI) &&
3142 "BaseImmOffset or BaseLongOffset or MemOp without accessSize");
3143
3144 // Return Values of getMemAccessSize() are
3145 // 0 - Checked in the assert above.
3146 // 1, 2, 3, 4 & 7, 8 - The statement below is correct for all these.
3147 // MemAccessSize is represented as 1+log2(N) where N is size in bits.
3148 AccessSize = (1U << (getMemAccessSize(MI) - 1));
3149
3150 unsigned basePos = 0, offsetPos = 0;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003151 if (!getBaseAndOffsetPosition(MI, basePos, offsetPos))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003152 return 0;
3153
3154 // Post increment updates its EA after the mem access,
3155 // so we need to treat its offset as zero.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003156 if (isPostIncrement(MI))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003157 Offset = 0;
3158 else {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003159 Offset = MI.getOperand(offsetPos).getImm();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003160 }
3161
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003162 return MI.getOperand(basePos).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003163}
3164
3165
3166/// Return the position of the base and offset operands for this instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003167bool HexagonInstrInfo::getBaseAndOffsetPosition(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003168 unsigned &BasePos, unsigned &OffsetPos) const {
3169 // Deal with memops first.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003170 if (isMemOp(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003171 BasePos = 0;
3172 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003173 } else if (MI.mayStore()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003174 BasePos = 0;
3175 OffsetPos = 1;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003176 } else if (MI.mayLoad()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003177 BasePos = 1;
3178 OffsetPos = 2;
3179 } else
3180 return false;
3181
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003182 if (isPredicated(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003183 BasePos++;
3184 OffsetPos++;
3185 }
3186 if (isPostIncrement(MI)) {
3187 BasePos++;
3188 OffsetPos++;
3189 }
3190
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003191 if (!MI.getOperand(BasePos).isReg() || !MI.getOperand(OffsetPos).isImm())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003192 return false;
3193
3194 return true;
3195}
3196
3197
3198// Inserts branching instructions in reverse order of their occurence.
3199// e.g. jump_t t1 (i1)
3200// jump t2 (i2)
3201// Jumpers = {i2, i1}
3202SmallVector<MachineInstr*, 2> HexagonInstrInfo::getBranchingInstrs(
3203 MachineBasicBlock& MBB) const {
3204 SmallVector<MachineInstr*, 2> Jumpers;
3205 // If the block has no terminators, it just falls into the block after it.
3206 MachineBasicBlock::instr_iterator I = MBB.instr_end();
3207 if (I == MBB.instr_begin())
3208 return Jumpers;
3209
3210 // A basic block may looks like this:
3211 //
3212 // [ insn
3213 // EH_LABEL
3214 // insn
3215 // insn
3216 // insn
3217 // EH_LABEL
3218 // insn ]
3219 //
3220 // It has two succs but does not have a terminator
3221 // Don't know how to handle it.
3222 do {
3223 --I;
3224 if (I->isEHLabel())
3225 return Jumpers;
3226 } while (I != MBB.instr_begin());
3227
3228 I = MBB.instr_end();
3229 --I;
3230
3231 while (I->isDebugValue()) {
3232 if (I == MBB.instr_begin())
3233 return Jumpers;
3234 --I;
3235 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003236 if (!isUnpredicatedTerminator(*I))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003237 return Jumpers;
3238
3239 // Get the last instruction in the block.
3240 MachineInstr *LastInst = &*I;
3241 Jumpers.push_back(LastInst);
3242 MachineInstr *SecondLastInst = nullptr;
3243 // Find one more terminator if present.
3244 do {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00003245 if (&*I != LastInst && !I->isBundle() && isUnpredicatedTerminator(*I)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003246 if (!SecondLastInst) {
3247 SecondLastInst = &*I;
3248 Jumpers.push_back(SecondLastInst);
3249 } else // This is a third branch.
3250 return Jumpers;
3251 }
3252 if (I == MBB.instr_begin())
3253 break;
3254 --I;
3255 } while (true);
3256 return Jumpers;
3257}
3258
3259
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003260short HexagonInstrInfo::getBaseWithLongOffset(short Opcode) const {
3261 if (Opcode < 0)
3262 return -1;
3263 return Hexagon::getBaseWithLongOffset(Opcode);
3264}
3265
3266
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003267short HexagonInstrInfo::getBaseWithLongOffset(const MachineInstr &MI) const {
3268 return Hexagon::getBaseWithLongOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003269}
3270
3271
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003272short HexagonInstrInfo::getBaseWithRegOffset(const MachineInstr &MI) const {
3273 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00003274}
3275
3276
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003277// Returns Operand Index for the constant extended instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003278unsigned HexagonInstrInfo::getCExtOpNum(const MachineInstr &MI) const {
3279 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003280 return (F >> HexagonII::ExtendableOpPos) & HexagonII::ExtendableOpMask;
3281}
3282
3283// See if instruction could potentially be a duplex candidate.
3284// If so, return its group. Zero otherwise.
3285HexagonII::CompoundGroup HexagonInstrInfo::getCompoundCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003286 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003287 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3288
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003289 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003290 default:
3291 return HexagonII::HCG_None;
3292 //
3293 // Compound pairs.
3294 // "p0=cmp.eq(Rs16,Rt16); if (p0.new) jump:nt #r9:2"
3295 // "Rd16=#U6 ; jump #r9:2"
3296 // "Rd16=Rs16 ; jump #r9:2"
3297 //
3298 case Hexagon::C2_cmpeq:
3299 case Hexagon::C2_cmpgt:
3300 case Hexagon::C2_cmpgtu:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003301 DstReg = MI.getOperand(0).getReg();
3302 Src1Reg = MI.getOperand(1).getReg();
3303 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003304 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3305 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
3306 isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg))
3307 return HexagonII::HCG_A;
3308 break;
3309 case Hexagon::C2_cmpeqi:
3310 case Hexagon::C2_cmpgti:
3311 case Hexagon::C2_cmpgtui:
3312 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003313 DstReg = MI.getOperand(0).getReg();
3314 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003315 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3316 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003317 isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3318 ((isUInt<5>(MI.getOperand(2).getImm())) ||
3319 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003320 return HexagonII::HCG_A;
3321 break;
3322 case Hexagon::A2_tfr:
3323 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003324 DstReg = MI.getOperand(0).getReg();
3325 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003326 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3327 return HexagonII::HCG_A;
3328 break;
3329 case Hexagon::A2_tfrsi:
3330 // Rd = #u6
3331 // Do not test for #u6 size since the const is getting extended
3332 // regardless and compound could be formed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003333 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003334 if (isIntRegForSubInst(DstReg))
3335 return HexagonII::HCG_A;
3336 break;
3337 case Hexagon::S2_tstbit_i:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003338 DstReg = MI.getOperand(0).getReg();
3339 Src1Reg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003340 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3341 (Hexagon::P0 == DstReg || Hexagon::P1 == DstReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003342 MI.getOperand(2).isImm() &&
3343 isIntRegForSubInst(Src1Reg) && (MI.getOperand(2).getImm() == 0))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003344 return HexagonII::HCG_A;
3345 break;
3346 // The fact that .new form is used pretty much guarantees
3347 // that predicate register will match. Nevertheless,
3348 // there could be some false positives without additional
3349 // checking.
3350 case Hexagon::J2_jumptnew:
3351 case Hexagon::J2_jumpfnew:
3352 case Hexagon::J2_jumptnewpt:
3353 case Hexagon::J2_jumpfnewpt:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003354 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003355 if (Hexagon::PredRegsRegClass.contains(Src1Reg) &&
3356 (Hexagon::P0 == Src1Reg || Hexagon::P1 == Src1Reg))
3357 return HexagonII::HCG_B;
3358 break;
3359 // Transfer and jump:
3360 // Rd=#U6 ; jump #r9:2
3361 // Rd=Rs ; jump #r9:2
3362 // Do not test for jump range here.
3363 case Hexagon::J2_jump:
3364 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3365 return HexagonII::HCG_C;
3366 break;
3367 }
3368
3369 return HexagonII::HCG_None;
3370}
3371
3372
3373// Returns -1 when there is no opcode found.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003374unsigned HexagonInstrInfo::getCompoundOpcode(const MachineInstr &GA,
3375 const MachineInstr &GB) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003376 assert(getCompoundCandidateGroup(GA) == HexagonII::HCG_A);
3377 assert(getCompoundCandidateGroup(GB) == HexagonII::HCG_B);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003378 if ((GA.getOpcode() != Hexagon::C2_cmpeqi) ||
3379 (GB.getOpcode() != Hexagon::J2_jumptnew))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003380 return -1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003381 unsigned DestReg = GA.getOperand(0).getReg();
3382 if (!GB.readsRegister(DestReg))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003383 return -1;
3384 if (DestReg == Hexagon::P0)
3385 return Hexagon::J4_cmpeqi_tp0_jump_nt;
3386 if (DestReg == Hexagon::P1)
3387 return Hexagon::J4_cmpeqi_tp1_jump_nt;
3388 return -1;
3389}
3390
3391
3392int HexagonInstrInfo::getCondOpcode(int Opc, bool invertPredicate) const {
3393 enum Hexagon::PredSense inPredSense;
3394 inPredSense = invertPredicate ? Hexagon::PredSense_false :
3395 Hexagon::PredSense_true;
3396 int CondOpcode = Hexagon::getPredOpcode(Opc, inPredSense);
3397 if (CondOpcode >= 0) // Valid Conditional opcode/instruction
3398 return CondOpcode;
3399
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003400 llvm_unreachable("Unexpected predicable instruction");
3401}
3402
3403
3404// Return the cur value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003405int HexagonInstrInfo::getDotCurOp(const MachineInstr &MI) const {
3406 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003407 default: llvm_unreachable("Unknown .cur type");
3408 case Hexagon::V6_vL32b_pi:
3409 return Hexagon::V6_vL32b_cur_pi;
3410 case Hexagon::V6_vL32b_ai:
3411 return Hexagon::V6_vL32b_cur_ai;
3412 //128B
3413 case Hexagon::V6_vL32b_pi_128B:
3414 return Hexagon::V6_vL32b_cur_pi_128B;
3415 case Hexagon::V6_vL32b_ai_128B:
3416 return Hexagon::V6_vL32b_cur_ai_128B;
3417 }
3418 return 0;
3419}
3420
3421
3422
3423// The diagram below shows the steps involved in the conversion of a predicated
3424// store instruction to its .new predicated new-value form.
3425//
3426// p.new NV store [ if(p0.new)memw(R0+#0)=R2.new ]
3427// ^ ^
3428// / \ (not OK. it will cause new-value store to be
3429// / X conditional on p0.new while R2 producer is
3430// / \ on p0)
3431// / \.
3432// p.new store p.old NV store
3433// [if(p0.new)memw(R0+#0)=R2] [if(p0)memw(R0+#0)=R2.new]
3434// ^ ^
3435// \ /
3436// \ /
3437// \ /
3438// p.old store
3439// [if (p0)memw(R0+#0)=R2]
3440//
3441//
3442// The following set of instructions further explains the scenario where
3443// conditional new-value store becomes invalid when promoted to .new predicate
3444// form.
3445//
3446// { 1) if (p0) r0 = add(r1, r2)
3447// 2) p0 = cmp.eq(r3, #0) }
3448//
3449// 3) if (p0) memb(r1+#0) = r0 --> this instruction can't be grouped with
3450// the first two instructions because in instr 1, r0 is conditional on old value
3451// of p0 but its use in instr 3 is conditional on p0 modified by instr 2 which
3452// is not valid for new-value stores.
3453// Predicated new value stores (i.e. if (p0) memw(..)=r0.new) are excluded
3454// from the "Conditional Store" list. Because a predicated new value store
3455// would NOT be promoted to a double dot new store. See diagram below:
3456// This function returns yes for those stores that are predicated but not
3457// yet promoted to predicate dot new instructions.
3458//
3459// +---------------------+
3460// /-----| if (p0) memw(..)=r0 |---------\~
3461// || +---------------------+ ||
3462// promote || /\ /\ || promote
3463// || /||\ /||\ ||
3464// \||/ demote || \||/
3465// \/ || || \/
3466// +-------------------------+ || +-------------------------+
3467// | if (p0.new) memw(..)=r0 | || | if (p0) memw(..)=r0.new |
3468// +-------------------------+ || +-------------------------+
3469// || || ||
3470// || demote \||/
3471// promote || \/ NOT possible
3472// || || /\~
3473// \||/ || /||\~
3474// \/ || ||
3475// +-----------------------------+
3476// | if (p0.new) memw(..)=r0.new |
3477// +-----------------------------+
3478// Double Dot New Store
3479//
3480// Returns the most basic instruction for the .new predicated instructions and
3481// new-value stores.
3482// For example, all of the following instructions will be converted back to the
3483// same instruction:
3484// 1) if (p0.new) memw(R0+#0) = R1.new --->
3485// 2) if (p0) memw(R0+#0)= R1.new -------> if (p0) memw(R0+#0) = R1
3486// 3) if (p0.new) memw(R0+#0) = R1 --->
3487//
3488// To understand the translation of instruction 1 to its original form, consider
3489// a packet with 3 instructions.
3490// { p0 = cmp.eq(R0,R1)
3491// if (p0.new) R2 = add(R3, R4)
3492// R5 = add (R3, R1)
3493// }
3494// if (p0) memw(R5+#0) = R2 <--- trying to include it in the previous packet
3495//
3496// This instruction can be part of the previous packet only if both p0 and R2
3497// are promoted to .new values. This promotion happens in steps, first
3498// predicate register is promoted to .new and in the next iteration R2 is
3499// promoted. Therefore, in case of dependence check failure (due to R5) during
3500// next iteration, it should be converted back to its most basic form.
3501
3502
3503// Return the new value instruction for a given store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003504int HexagonInstrInfo::getDotNewOp(const MachineInstr &MI) const {
3505 int NVOpcode = Hexagon::getNewValueOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003506 if (NVOpcode >= 0) // Valid new-value store instruction.
3507 return NVOpcode;
3508
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003509 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003510 default: llvm_unreachable("Unknown .new type");
3511 case Hexagon::S4_storerb_ur:
3512 return Hexagon::S4_storerbnew_ur;
3513
3514 case Hexagon::S2_storerb_pci:
3515 return Hexagon::S2_storerb_pci;
3516
3517 case Hexagon::S2_storeri_pci:
3518 return Hexagon::S2_storeri_pci;
3519
3520 case Hexagon::S2_storerh_pci:
3521 return Hexagon::S2_storerh_pci;
3522
3523 case Hexagon::S2_storerd_pci:
3524 return Hexagon::S2_storerd_pci;
3525
3526 case Hexagon::S2_storerf_pci:
3527 return Hexagon::S2_storerf_pci;
3528
3529 case Hexagon::V6_vS32b_ai:
3530 return Hexagon::V6_vS32b_new_ai;
3531
3532 case Hexagon::V6_vS32b_pi:
3533 return Hexagon::V6_vS32b_new_pi;
3534
3535 // 128B
3536 case Hexagon::V6_vS32b_ai_128B:
3537 return Hexagon::V6_vS32b_new_ai_128B;
3538
3539 case Hexagon::V6_vS32b_pi_128B:
3540 return Hexagon::V6_vS32b_new_pi_128B;
3541 }
3542 return 0;
3543}
3544
Krzysztof Parzyszek0a04ac22016-05-16 16:56:10 +00003545
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003546// Returns the opcode to use when converting MI, which is a conditional jump,
3547// into a conditional instruction which uses the .new value of the predicate.
3548// We also use branch probabilities to add a hint to the jump.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003549int HexagonInstrInfo::getDotNewPredJumpOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003550 const MachineBranchProbabilityInfo *MBPI) const {
3551 // We assume that block can have at most two successors.
3552 bool taken = false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003553 const MachineBasicBlock *Src = MI.getParent();
3554 const MachineOperand &BrTarget = MI.getOperand(1);
3555 const MachineBasicBlock *Dst = BrTarget.getMBB();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003556
3557 const BranchProbability Prediction = MBPI->getEdgeProbability(Src, Dst);
3558 if (Prediction >= BranchProbability(1,2))
3559 taken = true;
3560
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003561 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003562 case Hexagon::J2_jumpt:
3563 return taken ? Hexagon::J2_jumptnewpt : Hexagon::J2_jumptnew;
3564 case Hexagon::J2_jumpf:
3565 return taken ? Hexagon::J2_jumpfnewpt : Hexagon::J2_jumpfnew;
3566
3567 default:
3568 llvm_unreachable("Unexpected jump instruction.");
3569 }
3570}
3571
3572
3573// Return .new predicate version for an instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003574int HexagonInstrInfo::getDotNewPredOp(const MachineInstr &MI,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003575 const MachineBranchProbabilityInfo *MBPI) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003576 int NewOpcode = Hexagon::getPredNewOpcode(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003577 if (NewOpcode >= 0) // Valid predicate new instruction
3578 return NewOpcode;
3579
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003580 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003581 // Condtional Jumps
3582 case Hexagon::J2_jumpt:
3583 case Hexagon::J2_jumpf:
3584 return getDotNewPredJumpOp(MI, MBPI);
3585
3586 default:
3587 assert(0 && "Unknown .new type");
3588 }
3589 return 0;
3590}
3591
3592
3593int HexagonInstrInfo::getDotOldOp(const int opc) const {
3594 int NewOp = opc;
3595 if (isPredicated(NewOp) && isPredicatedNew(NewOp)) { // Get predicate old form
3596 NewOp = Hexagon::getPredOldOpcode(NewOp);
3597 assert(NewOp >= 0 &&
3598 "Couldn't change predicate new instruction to its old form.");
3599 }
3600
3601 if (isNewValueStore(NewOp)) { // Convert into non-new-value format
3602 NewOp = Hexagon::getNonNVStore(NewOp);
3603 assert(NewOp >= 0 && "Couldn't change new-value store to its old form.");
3604 }
3605 return NewOp;
3606}
3607
3608
3609// See if instruction could potentially be a duplex candidate.
3610// If so, return its group. Zero otherwise.
3611HexagonII::SubInstructionGroup HexagonInstrInfo::getDuplexCandidateGroup(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003612 const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003613 unsigned DstReg, SrcReg, Src1Reg, Src2Reg;
3614 auto &HRI = getRegisterInfo();
3615
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003616 switch (MI.getOpcode()) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003617 default:
3618 return HexagonII::HSIG_None;
3619 //
3620 // Group L1:
3621 //
3622 // Rd = memw(Rs+#u4:2)
3623 // Rd = memub(Rs+#u4:0)
3624 case Hexagon::L2_loadri_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003625 DstReg = MI.getOperand(0).getReg();
3626 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003627 // Special case this one from Group L2.
3628 // Rd = memw(r29+#u5:2)
3629 if (isIntRegForSubInst(DstReg)) {
3630 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
3631 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003632 MI.getOperand(2).isImm() &&
3633 isShiftedUInt<5,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003634 return HexagonII::HSIG_L2;
3635 // Rd = memw(Rs+#u4:2)
3636 if (isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003637 (MI.getOperand(2).isImm() &&
3638 isShiftedUInt<4,2>(MI.getOperand(2).getImm())))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003639 return HexagonII::HSIG_L1;
3640 }
3641 break;
3642 case Hexagon::L2_loadrub_io:
3643 // Rd = memub(Rs+#u4:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003644 DstReg = MI.getOperand(0).getReg();
3645 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003646 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003647 MI.getOperand(2).isImm() && isUInt<4>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003648 return HexagonII::HSIG_L1;
3649 break;
3650 //
3651 // Group L2:
3652 //
3653 // Rd = memh/memuh(Rs+#u3:1)
3654 // Rd = memb(Rs+#u3:0)
3655 // Rd = memw(r29+#u5:2) - Handled above.
3656 // Rdd = memd(r29+#u5:3)
3657 // deallocframe
3658 // [if ([!]p0[.new])] dealloc_return
3659 // [if ([!]p0[.new])] jumpr r31
3660 case Hexagon::L2_loadrh_io:
3661 case Hexagon::L2_loadruh_io:
3662 // Rd = memh/memuh(Rs+#u3:1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003663 DstReg = MI.getOperand(0).getReg();
3664 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003665 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003666 MI.getOperand(2).isImm() &&
3667 isShiftedUInt<3,1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003668 return HexagonII::HSIG_L2;
3669 break;
3670 case Hexagon::L2_loadrb_io:
3671 // Rd = memb(Rs+#u3:0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003672 DstReg = MI.getOperand(0).getReg();
3673 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003674 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003675 MI.getOperand(2).isImm() &&
3676 isUInt<3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003677 return HexagonII::HSIG_L2;
3678 break;
3679 case Hexagon::L2_loadrd_io:
3680 // Rdd = memd(r29+#u5:3)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003681 DstReg = MI.getOperand(0).getReg();
3682 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003683 if (isDblRegForSubInst(DstReg, HRI) &&
3684 Hexagon::IntRegsRegClass.contains(SrcReg) &&
3685 HRI.getStackRegister() == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003686 MI.getOperand(2).isImm() &&
3687 isShiftedUInt<5,3>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003688 return HexagonII::HSIG_L2;
3689 break;
3690 // dealloc_return is not documented in Hexagon Manual, but marked
3691 // with A_SUBINSN attribute in iset_v4classic.py.
3692 case Hexagon::RESTORE_DEALLOC_RET_JMP_V4:
3693 case Hexagon::L4_return:
3694 case Hexagon::L2_deallocframe:
3695 return HexagonII::HSIG_L2;
3696 case Hexagon::EH_RETURN_JMPR:
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003697 case Hexagon::PS_jmpret:
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003698 // jumpr r31
3699 // Actual form JMPR %PC<imp-def>, %R31<imp-use>, %R0<imp-use,internal>.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003700 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003701 if (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg))
3702 return HexagonII::HSIG_L2;
3703 break;
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00003704 case Hexagon::PS_jmprett:
3705 case Hexagon::PS_jmpretf:
3706 case Hexagon::PS_jmprettnewpt:
3707 case Hexagon::PS_jmpretfnewpt:
3708 case Hexagon::PS_jmprettnew:
3709 case Hexagon::PS_jmpretfnew:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003710 DstReg = MI.getOperand(1).getReg();
3711 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003712 // [if ([!]p0[.new])] jumpr r31
3713 if ((Hexagon::PredRegsRegClass.contains(SrcReg) &&
3714 (Hexagon::P0 == SrcReg)) &&
3715 (Hexagon::IntRegsRegClass.contains(DstReg) && (Hexagon::R31 == DstReg)))
3716 return HexagonII::HSIG_L2;
3717 break;
3718 case Hexagon::L4_return_t :
3719 case Hexagon::L4_return_f :
3720 case Hexagon::L4_return_tnew_pnt :
3721 case Hexagon::L4_return_fnew_pnt :
3722 case Hexagon::L4_return_tnew_pt :
3723 case Hexagon::L4_return_fnew_pt :
3724 // [if ([!]p0[.new])] dealloc_return
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003725 SrcReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003726 if (Hexagon::PredRegsRegClass.contains(SrcReg) && (Hexagon::P0 == SrcReg))
3727 return HexagonII::HSIG_L2;
3728 break;
3729 //
3730 // Group S1:
3731 //
3732 // memw(Rs+#u4:2) = Rt
3733 // memb(Rs+#u4:0) = Rt
3734 case Hexagon::S2_storeri_io:
3735 // Special case this one from Group S2.
3736 // memw(r29+#u5:2) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003737 Src1Reg = MI.getOperand(0).getReg();
3738 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003739 if (Hexagon::IntRegsRegClass.contains(Src1Reg) &&
3740 isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003741 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3742 isShiftedUInt<5,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003743 return HexagonII::HSIG_S2;
3744 // memw(Rs+#u4:2) = Rt
3745 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003746 MI.getOperand(1).isImm() &&
3747 isShiftedUInt<4,2>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003748 return HexagonII::HSIG_S1;
3749 break;
3750 case Hexagon::S2_storerb_io:
3751 // memb(Rs+#u4:0) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003752 Src1Reg = MI.getOperand(0).getReg();
3753 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003754 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003755 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003756 return HexagonII::HSIG_S1;
3757 break;
3758 //
3759 // Group S2:
3760 //
3761 // memh(Rs+#u3:1) = Rt
3762 // memw(r29+#u5:2) = Rt
3763 // memd(r29+#s6:3) = Rtt
3764 // memw(Rs+#u4:2) = #U1
3765 // memb(Rs+#u4) = #U1
3766 // allocframe(#u5:3)
3767 case Hexagon::S2_storerh_io:
3768 // memh(Rs+#u3:1) = Rt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003769 Src1Reg = MI.getOperand(0).getReg();
3770 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003771 if (isIntRegForSubInst(Src1Reg) && isIntRegForSubInst(Src2Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003772 MI.getOperand(1).isImm() &&
3773 isShiftedUInt<3,1>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003774 return HexagonII::HSIG_S1;
3775 break;
3776 case Hexagon::S2_storerd_io:
3777 // memd(r29+#s6:3) = Rtt
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003778 Src1Reg = MI.getOperand(0).getReg();
3779 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003780 if (isDblRegForSubInst(Src2Reg, HRI) &&
3781 Hexagon::IntRegsRegClass.contains(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003782 HRI.getStackRegister() == Src1Reg && MI.getOperand(1).isImm() &&
3783 isShiftedInt<6,3>(MI.getOperand(1).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003784 return HexagonII::HSIG_S2;
3785 break;
3786 case Hexagon::S4_storeiri_io:
3787 // memw(Rs+#u4:2) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003788 Src1Reg = MI.getOperand(0).getReg();
3789 if (isIntRegForSubInst(Src1Reg) && MI.getOperand(1).isImm() &&
3790 isShiftedUInt<4,2>(MI.getOperand(1).getImm()) &&
3791 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003792 return HexagonII::HSIG_S2;
3793 break;
3794 case Hexagon::S4_storeirb_io:
3795 // memb(Rs+#u4) = #U1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003796 Src1Reg = MI.getOperand(0).getReg();
Krzysztof Parzyszekf2a4f8f2016-06-15 21:05:04 +00003797 if (isIntRegForSubInst(Src1Reg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003798 MI.getOperand(1).isImm() && isUInt<4>(MI.getOperand(1).getImm()) &&
3799 MI.getOperand(2).isImm() && isUInt<1>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003800 return HexagonII::HSIG_S2;
3801 break;
3802 case Hexagon::S2_allocframe:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003803 if (MI.getOperand(0).isImm() &&
3804 isShiftedUInt<5,3>(MI.getOperand(0).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003805 return HexagonII::HSIG_S1;
3806 break;
3807 //
3808 // Group A:
3809 //
3810 // Rx = add(Rx,#s7)
3811 // Rd = Rs
3812 // Rd = #u6
3813 // Rd = #-1
3814 // if ([!]P0[.new]) Rd = #0
3815 // Rd = add(r29,#u6:2)
3816 // Rx = add(Rx,Rs)
3817 // P0 = cmp.eq(Rs,#u2)
3818 // Rdd = combine(#0,Rs)
3819 // Rdd = combine(Rs,#0)
3820 // Rdd = combine(#u2,#U2)
3821 // Rd = add(Rs,#1)
3822 // Rd = add(Rs,#-1)
3823 // Rd = sxth/sxtb/zxtb/zxth(Rs)
3824 // Rd = and(Rs,#1)
3825 case Hexagon::A2_addi:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003826 DstReg = MI.getOperand(0).getReg();
3827 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003828 if (isIntRegForSubInst(DstReg)) {
3829 // Rd = add(r29,#u6:2)
3830 if (Hexagon::IntRegsRegClass.contains(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003831 HRI.getStackRegister() == SrcReg && MI.getOperand(2).isImm() &&
3832 isShiftedUInt<6,2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003833 return HexagonII::HSIG_A;
3834 // Rx = add(Rx,#s7)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003835 if ((DstReg == SrcReg) && MI.getOperand(2).isImm() &&
3836 isInt<7>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003837 return HexagonII::HSIG_A;
3838 // Rd = add(Rs,#1)
3839 // Rd = add(Rs,#-1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003840 if (isIntRegForSubInst(SrcReg) && MI.getOperand(2).isImm() &&
3841 ((MI.getOperand(2).getImm() == 1) ||
3842 (MI.getOperand(2).getImm() == -1)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003843 return HexagonII::HSIG_A;
3844 }
3845 break;
3846 case Hexagon::A2_add:
3847 // Rx = add(Rx,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003848 DstReg = MI.getOperand(0).getReg();
3849 Src1Reg = MI.getOperand(1).getReg();
3850 Src2Reg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003851 if (isIntRegForSubInst(DstReg) && (DstReg == Src1Reg) &&
3852 isIntRegForSubInst(Src2Reg))
3853 return HexagonII::HSIG_A;
3854 break;
3855 case Hexagon::A2_andir:
3856 // Same as zxtb.
3857 // Rd16=and(Rs16,#255)
3858 // Rd16=and(Rs16,#1)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003859 DstReg = MI.getOperand(0).getReg();
3860 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003861 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003862 MI.getOperand(2).isImm() &&
3863 ((MI.getOperand(2).getImm() == 1) ||
3864 (MI.getOperand(2).getImm() == 255)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003865 return HexagonII::HSIG_A;
3866 break;
3867 case Hexagon::A2_tfr:
3868 // Rd = Rs
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003869 DstReg = MI.getOperand(0).getReg();
3870 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003871 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3872 return HexagonII::HSIG_A;
3873 break;
3874 case Hexagon::A2_tfrsi:
3875 // Rd = #u6
3876 // Do not test for #u6 size since the const is getting extended
3877 // regardless and compound could be formed.
3878 // Rd = #-1
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003879 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003880 if (isIntRegForSubInst(DstReg))
3881 return HexagonII::HSIG_A;
3882 break;
3883 case Hexagon::C2_cmoveit:
3884 case Hexagon::C2_cmovenewit:
3885 case Hexagon::C2_cmoveif:
3886 case Hexagon::C2_cmovenewif:
3887 // if ([!]P0[.new]) Rd = #0
3888 // Actual form:
3889 // %R16<def> = C2_cmovenewit %P0<internal>, 0, %R16<imp-use,undef>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003890 DstReg = MI.getOperand(0).getReg();
3891 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003892 if (isIntRegForSubInst(DstReg) &&
3893 Hexagon::PredRegsRegClass.contains(SrcReg) && Hexagon::P0 == SrcReg &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003894 MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0)
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003895 return HexagonII::HSIG_A;
3896 break;
3897 case Hexagon::C2_cmpeqi:
3898 // P0 = cmp.eq(Rs,#u2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003899 DstReg = MI.getOperand(0).getReg();
3900 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003901 if (Hexagon::PredRegsRegClass.contains(DstReg) &&
3902 Hexagon::P0 == DstReg && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003903 MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm()))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003904 return HexagonII::HSIG_A;
3905 break;
3906 case Hexagon::A2_combineii:
3907 case Hexagon::A4_combineii:
3908 // Rdd = combine(#u2,#U2)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003909 DstReg = MI.getOperand(0).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003910 if (isDblRegForSubInst(DstReg, HRI) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003911 ((MI.getOperand(1).isImm() && isUInt<2>(MI.getOperand(1).getImm())) ||
3912 (MI.getOperand(1).isGlobal() &&
3913 isUInt<2>(MI.getOperand(1).getOffset()))) &&
3914 ((MI.getOperand(2).isImm() && isUInt<2>(MI.getOperand(2).getImm())) ||
3915 (MI.getOperand(2).isGlobal() &&
3916 isUInt<2>(MI.getOperand(2).getOffset()))))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003917 return HexagonII::HSIG_A;
3918 break;
3919 case Hexagon::A4_combineri:
3920 // Rdd = combine(Rs,#0)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003921 DstReg = MI.getOperand(0).getReg();
3922 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003923 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003924 ((MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0) ||
3925 (MI.getOperand(2).isGlobal() && MI.getOperand(2).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003926 return HexagonII::HSIG_A;
3927 break;
3928 case Hexagon::A4_combineir:
3929 // Rdd = combine(#0,Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003930 DstReg = MI.getOperand(0).getReg();
3931 SrcReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003932 if (isDblRegForSubInst(DstReg, HRI) && isIntRegForSubInst(SrcReg) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003933 ((MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) ||
3934 (MI.getOperand(1).isGlobal() && MI.getOperand(1).getOffset() == 0)))
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003935 return HexagonII::HSIG_A;
3936 break;
3937 case Hexagon::A2_sxtb:
3938 case Hexagon::A2_sxth:
3939 case Hexagon::A2_zxtb:
3940 case Hexagon::A2_zxth:
3941 // Rd = sxth/sxtb/zxtb/zxth(Rs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003942 DstReg = MI.getOperand(0).getReg();
3943 SrcReg = MI.getOperand(1).getReg();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003944 if (isIntRegForSubInst(DstReg) && isIntRegForSubInst(SrcReg))
3945 return HexagonII::HSIG_A;
3946 break;
3947 }
3948
3949 return HexagonII::HSIG_None;
3950}
3951
3952
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003953short HexagonInstrInfo::getEquivalentHWInstr(const MachineInstr &MI) const {
3954 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Real);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003955}
3956
3957
3958// Return first non-debug instruction in the basic block.
3959MachineInstr *HexagonInstrInfo::getFirstNonDbgInst(MachineBasicBlock *BB)
3960 const {
3961 for (auto MII = BB->instr_begin(), End = BB->instr_end(); MII != End; MII++) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003962 MachineInstr &MI = *MII;
3963 if (MI.isDebugValue())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003964 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003965 return &MI;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003966 }
3967 return nullptr;
3968}
3969
3970
3971unsigned HexagonInstrInfo::getInstrTimingClassLatency(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003972 const InstrItineraryData *ItinData, const MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003973 // Default to one cycle for no itinerary. However, an "empty" itinerary may
3974 // still have a MinLatency property, which getStageLatency checks.
3975 if (!ItinData)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003976 return getInstrLatency(ItinData, MI);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003977
3978 // Get the latency embedded in the itinerary. If we're not using timing class
3979 // latencies or if we using BSB scheduling, then restrict the maximum latency
3980 // to 1 (that is, either 0 or 1).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003981 if (MI.isTransient())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003982 return 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003983 unsigned Latency = ItinData->getStageLatency(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003984 if (!EnableTimingClassLatency ||
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00003985 MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>().
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00003986 useBSBScheduling())
3987 if (Latency > 1)
3988 Latency = 1;
3989 return Latency;
3990}
3991
3992
3993// inverts the predication logic.
3994// p -> NotP
3995// NotP -> P
3996bool HexagonInstrInfo::getInvertedPredSense(
3997 SmallVectorImpl<MachineOperand> &Cond) const {
3998 if (Cond.empty())
3999 return false;
4000 unsigned Opc = getInvertedPredicatedOpcode(Cond[0].getImm());
4001 Cond[0].setImm(Opc);
4002 return true;
4003}
4004
4005
4006unsigned HexagonInstrInfo::getInvertedPredicatedOpcode(const int Opc) const {
4007 int InvPredOpcode;
4008 InvPredOpcode = isPredicatedTrue(Opc) ? Hexagon::getFalsePredOpcode(Opc)
4009 : Hexagon::getTruePredOpcode(Opc);
4010 if (InvPredOpcode >= 0) // Valid instruction with the inverted predicate.
4011 return InvPredOpcode;
4012
4013 llvm_unreachable("Unexpected predicated instruction");
4014}
4015
4016
4017// Returns the max value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004018int HexagonInstrInfo::getMaxValue(const MachineInstr &MI) const {
4019 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004020 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4021 & HexagonII::ExtentSignedMask;
4022 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4023 & HexagonII::ExtentBitsMask;
4024
4025 if (isSigned) // if value is signed
4026 return ~(-1U << (bits - 1));
4027 else
4028 return ~(-1U << bits);
4029}
4030
4031
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004032unsigned HexagonInstrInfo::getMemAccessSize(const MachineInstr &MI) const {
4033 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004034 return (F >> HexagonII::MemAccessSizePos) & HexagonII::MemAccesSizeMask;
4035}
4036
4037
4038// Returns the min value that doesn't need to be extended.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004039int HexagonInstrInfo::getMinValue(const MachineInstr &MI) const {
4040 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004041 unsigned isSigned = (F >> HexagonII::ExtentSignedPos)
4042 & HexagonII::ExtentSignedMask;
4043 unsigned bits = (F >> HexagonII::ExtentBitsPos)
4044 & HexagonII::ExtentBitsMask;
4045
4046 if (isSigned) // if value is signed
4047 return -1U << (bits - 1);
4048 else
4049 return 0;
4050}
4051
4052
4053// Returns opcode of the non-extended equivalent instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004054short HexagonInstrInfo::getNonExtOpcode(const MachineInstr &MI) const {
Jyotsna Verma84256432013-03-01 17:37:13 +00004055 // Check if the instruction has a register form that uses register in place
4056 // of the extended operand, if so return that as the non-extended form.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004057 short NonExtOpcode = Hexagon::getRegForm(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004058 if (NonExtOpcode >= 0)
4059 return NonExtOpcode;
4060
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004061 if (MI.getDesc().mayLoad() || MI.getDesc().mayStore()) {
Alp Tokercb402912014-01-24 17:20:08 +00004062 // Check addressing mode and retrieve non-ext equivalent instruction.
Jyotsna Verma84256432013-03-01 17:37:13 +00004063 switch (getAddrMode(MI)) {
4064 case HexagonII::Absolute :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004065 return Hexagon::getBaseWithImmOffset(MI.getOpcode());
Jyotsna Verma84256432013-03-01 17:37:13 +00004066 case HexagonII::BaseImmOffset :
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004067 return Hexagon::getBaseWithRegOffset(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004068 case HexagonII::BaseLongOffset:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004069 return Hexagon::getRegShlForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004070
Jyotsna Verma84256432013-03-01 17:37:13 +00004071 default:
4072 return -1;
4073 }
4074 }
4075 return -1;
4076}
Jyotsna Verma5ed51812013-05-01 21:37:34 +00004077
Brendon Cahoondf43e682015-05-08 16:16:29 +00004078
Ahmed Bougachac88bf542015-06-11 19:30:37 +00004079bool HexagonInstrInfo::getPredReg(ArrayRef<MachineOperand> Cond,
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004080 unsigned &PredReg, unsigned &PredRegPos, unsigned &PredRegFlags) const {
Brendon Cahoondf43e682015-05-08 16:16:29 +00004081 if (Cond.empty())
4082 return false;
4083 assert(Cond.size() == 2);
4084 if (isNewValueJump(Cond[0].getImm()) || Cond[1].isMBB()) {
4085 DEBUG(dbgs() << "No predregs for new-value jumps/endloop");
4086 return false;
4087 }
4088 PredReg = Cond[1].getReg();
4089 PredRegPos = 1;
4090 // See IfConversion.cpp why we add RegState::Implicit | RegState::Undef
4091 PredRegFlags = 0;
4092 if (Cond[1].isImplicit())
4093 PredRegFlags = RegState::Implicit;
4094 if (Cond[1].isUndef())
4095 PredRegFlags |= RegState::Undef;
4096 return true;
4097}
4098
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004099
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004100short HexagonInstrInfo::getPseudoInstrPair(const MachineInstr &MI) const {
4101 return Hexagon::getRealHWInstr(MI.getOpcode(), Hexagon::InstrType_Pseudo);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004102}
4103
4104
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004105short HexagonInstrInfo::getRegForm(const MachineInstr &MI) const {
4106 return Hexagon::getRegForm(MI.getOpcode());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004107}
4108
4109
4110// Return the number of bytes required to encode the instruction.
4111// Hexagon instructions are fixed length, 4 bytes, unless they
4112// use a constant extender, which requires another 4 bytes.
4113// For debug instructions and prolog labels, return 0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004114unsigned HexagonInstrInfo::getSize(const MachineInstr &MI) const {
4115 if (MI.isDebugValue() || MI.isPosition())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004116 return 0;
4117
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004118 unsigned Size = MI.getDesc().getSize();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004119 if (!Size)
4120 // Assume the default insn size in case it cannot be determined
4121 // for whatever reason.
4122 Size = HEXAGON_INSTR_SIZE;
4123
4124 if (isConstExtended(MI) || isExtended(MI))
4125 Size += HEXAGON_INSTR_SIZE;
4126
4127 // Try and compute number of instructions in asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004128 if (BranchRelaxAsmLarge && MI.getOpcode() == Hexagon::INLINEASM) {
4129 const MachineBasicBlock &MBB = *MI.getParent();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004130 const MachineFunction *MF = MBB.getParent();
4131 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
4132
4133 // Count the number of register definitions to find the asm string.
4134 unsigned NumDefs = 0;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004135 for (; MI.getOperand(NumDefs).isReg() && MI.getOperand(NumDefs).isDef();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004136 ++NumDefs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004137 assert(NumDefs != MI.getNumOperands()-2 && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004138
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004139 assert(MI.getOperand(NumDefs).isSymbol() && "No asm string?");
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004140 // Disassemble the AsmStr and approximate number of instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004141 const char *AsmStr = MI.getOperand(NumDefs).getSymbolName();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004142 Size = getInlineAsmLength(AsmStr, *MAI);
4143 }
4144
4145 return Size;
4146}
4147
4148
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004149uint64_t HexagonInstrInfo::getType(const MachineInstr &MI) const {
4150 const uint64_t F = MI.getDesc().TSFlags;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004151 return (F >> HexagonII::TypePos) & HexagonII::TypeMask;
4152}
4153
4154
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004155unsigned HexagonInstrInfo::getUnits(const MachineInstr &MI) const {
4156 const TargetSubtargetInfo &ST = MI.getParent()->getParent()->getSubtarget();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004157 const InstrItineraryData &II = *ST.getInstrItineraryData();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004158 const InstrStage &IS = *II.beginStage(MI.getDesc().getSchedClass());
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004159
4160 return IS.getUnits();
4161}
4162
4163
4164unsigned HexagonInstrInfo::getValidSubTargets(const unsigned Opcode) const {
4165 const uint64_t F = get(Opcode).TSFlags;
4166 return (F >> HexagonII::validSubTargetPos) & HexagonII::validSubTargetMask;
4167}
4168
4169
4170// Calculate size of the basic block without debug instructions.
4171unsigned HexagonInstrInfo::nonDbgBBSize(const MachineBasicBlock *BB) const {
4172 return nonDbgMICount(BB->instr_begin(), BB->instr_end());
4173}
4174
4175
4176unsigned HexagonInstrInfo::nonDbgBundleSize(
4177 MachineBasicBlock::const_iterator BundleHead) const {
4178 assert(BundleHead->isBundle() && "Not a bundle header");
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00004179 auto MII = BundleHead.getInstrIterator();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004180 // Skip the bundle header.
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00004181 return nonDbgMICount(++MII, getBundleEnd(*BundleHead));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004182}
4183
4184
4185/// immediateExtend - Changes the instruction in place to one using an immediate
4186/// extender.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004187void HexagonInstrInfo::immediateExtend(MachineInstr &MI) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004188 assert((isExtendable(MI)||isConstExtended(MI)) &&
4189 "Instruction must be extendable");
4190 // Find which operand is extendable.
4191 short ExtOpNum = getCExtOpNum(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004192 MachineOperand &MO = MI.getOperand(ExtOpNum);
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004193 // This needs to be something we understand.
4194 assert((MO.isMBB() || MO.isImm()) &&
4195 "Branch with unknown extendable field type");
4196 // Mark given operand as extended.
4197 MO.addTargetFlag(HexagonII::HMOTF_ConstExtended);
4198}
4199
4200
4201bool HexagonInstrInfo::invertAndChangeJumpTarget(
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004202 MachineInstr &MI, MachineBasicBlock *NewTarget) const {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004203 DEBUG(dbgs() << "\n[invertAndChangeJumpTarget] to BB#"
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004204 << NewTarget->getNumber(); MI.dump(););
4205 assert(MI.isBranch());
4206 unsigned NewOpcode = getInvertedPredicatedOpcode(MI.getOpcode());
4207 int TargetPos = MI.getNumOperands() - 1;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004208 // In general branch target is the last operand,
4209 // but some implicit defs added at the end might change it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004210 while ((TargetPos > -1) && !MI.getOperand(TargetPos).isMBB())
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004211 --TargetPos;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004212 assert((TargetPos >= 0) && MI.getOperand(TargetPos).isMBB());
4213 MI.getOperand(TargetPos).setMBB(NewTarget);
4214 if (EnableBranchPrediction && isPredicatedNew(MI)) {
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004215 NewOpcode = reversePrediction(NewOpcode);
4216 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004217 MI.setDesc(get(NewOpcode));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004218 return true;
4219}
4220
4221
4222void HexagonInstrInfo::genAllInsnTimingClasses(MachineFunction &MF) const {
4223 /* +++ The code below is used to generate complete set of Hexagon Insn +++ */
4224 MachineFunction::iterator A = MF.begin();
4225 MachineBasicBlock &B = *A;
4226 MachineBasicBlock::iterator I = B.begin();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004227 DebugLoc DL = I->getDebugLoc();
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004228 MachineInstr *NewMI;
4229
4230 for (unsigned insn = TargetOpcode::GENERIC_OP_END+1;
4231 insn < Hexagon::INSTRUCTION_LIST_END; ++insn) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004232 NewMI = BuildMI(B, I, DL, get(insn));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004233 DEBUG(dbgs() << "\n" << getName(NewMI->getOpcode()) <<
4234 " Class: " << NewMI->getDesc().getSchedClass());
4235 NewMI->eraseFromParent();
4236 }
4237 /* --- The code above is used to generate complete set of Hexagon Insn --- */
4238}
4239
4240
4241// inverts the predication logic.
4242// p -> NotP
4243// NotP -> P
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004244bool HexagonInstrInfo::reversePredSense(MachineInstr &MI) const {
4245 DEBUG(dbgs() << "\nTrying to reverse pred. sense of:"; MI.dump());
4246 MI.setDesc(get(getInvertedPredicatedOpcode(MI.getOpcode())));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +00004247 return true;
4248}
4249
4250
4251// Reverse the branch prediction.
4252unsigned HexagonInstrInfo::reversePrediction(unsigned Opcode) const {
4253 int PredRevOpcode = -1;
4254 if (isPredictedTaken(Opcode))
4255 PredRevOpcode = Hexagon::notTakenBranchPrediction(Opcode);
4256 else
4257 PredRevOpcode = Hexagon::takenBranchPrediction(Opcode);
4258 assert(PredRevOpcode > 0);
4259 return PredRevOpcode;
4260}
4261
4262
4263// TODO: Add more rigorous validation.
4264bool HexagonInstrInfo::validateBranchCond(const ArrayRef<MachineOperand> &Cond)
4265 const {
4266 return Cond.empty() || (Cond[0].isImm() && (Cond.size() != 1));
4267}
4268
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004269
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00004270short HexagonInstrInfo::xformRegToImmOffset(const MachineInstr &MI) const {
4271 return Hexagon::xformRegToImmOffset(MI.getOpcode());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00004272}